Debian Codegraph @

model/polymodel.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/model/polymodel.cpp $
21 * $Revision: 152 $
22 * $Date: 10/08/01 4:20p $
23 * $Author: Matt $
24 *
25 * Jason should put something here
26 *
27 * $Log: /DescentIII/Main/model/polymodel.cpp $
28 *
29 * 152 10/08/01 4:20p Matt
30 * Added system to check for errors when reading in add-on data.
31 *
32 * 151 10/01/01 1:58p Matt
33 * In release build, Increment the used count when loading a model that's
34 * already in memory. This was already done in non-release builds.
35 *
36 * 150 5/10/00 2:43p Jeff
37 * fixed bug when paging in data if the tablefile specifies a full path to
38 * a primative instead of just a filename (bug in 3rd party tablefile
39 * editors)
40 *
41 * 149 4/19/00 5:34p Matt
42 * From Duane for 1.4
43 * Check for glow_info memory already allocated before mallocing
44 *
45 * 148 10/21/99 2:40p Kevin
46 * Mac merge
47 *
48 * 147 10/16/99 6:40p Jeff
49 * swaped way of determining textures for objects on load
50 *
51 * 146 10/14/99 3:02p Jeff
52 * correctly look up texture names
53 *
54 * 145 7/08/99 5:48p Jason
55 * changes for new bumpmapping system in 1.1 update patch
56 *
57 * 144 6/08/99 1:00p Jason
58 * changes for bumpmapping
59 *
60 * 143 5/18/99 10:31a Jason
61 * polymodel stuff wasn't getting paged in before drawing
62 *
63 * 142 5/14/99 2:04p Jason
64 * added polymodel errors to catch bugs
65 *
66 * 141 4/28/99 1:55a Samir
67 * error instead of assert when we can't open a model for paging in a
68 * polymodel.
69 *
70 * 140 4/19/99 3:50p Jeff
71 * fixed mem_free/copy-paste bug, wasn't setting the correct pointer to
72 * NULL after free
73 *
74 * 139 4/19/99 4:00a Jeff
75 * fixed min/max for Linux
76 *
77 * 138 4/09/99 12:51p Jason
78 * fixed bug with new lookup code
79 *
80 * 137 4/09/99 12:06p Jason
81 * made model setup code faster
82 *
83 * 136 4/08/99 11:45a Jason
84 * greatly sped up the time it takes to get model anges/positions by
85 * precalculation
86 *
87 * 135 3/05/99 11:24a Jason
88 * fixed pesky no-lightmap drawing problem that has plagued me for the
89 * last 10 months
90 *
91 * 134 2/16/99 12:42p Kevin
92 * Improvements to the paging data progress indicator
93 *
94 * 133 2/10/99 4:58p Jeff
95 * put assert if pageinpolymodel file open fails
96 *
97 * 132 2/09/99 7:01p Kevin
98 * First work for new and improved progress screen while loading a level.
99 * Note that this is a hack at this point, while I get the details worked
100 * out, then I'll make it cleaner.
101 *
102 * 131 1/15/99 3:04p Jason
103 * fixed polymodel data freeing problem
104 *
105 * 130 1/13/99 12:43p Jason
106 * added some more detail settings
107 *
108 * 129 10/16/98 1:54p Kevin
109 * Changes for Demo Beta 4
110 *
111 * 128 10/13/98 5:25p Jason
112 * fixed zero faced submodel problem
113 *
114 * 127 10/12/98 10:59a Jason
115 * fixed zero length malloc
116 *
117 * 126 10/12/98 9:35a Jason
118 * New polymodel memory scheme
119 *
120 * 125 10/08/98 4:24p Kevin
121 * Changed code to comply with memory library usage. Always use mem_malloc
122 * , mem_free and mem_strdup
123 *
124 * 124 10/08/98 2:27p Jason
125 * sped up table file loading
126 *
127 * 123 10/07/98 5:29p Chris
128 * Added SOF_THRUSTER support
129 *
130 * 122 10/07/98 3:40p Chris
131 * Added $thruster
132 *
133 * 121 9/11/98 4:04p Jason
134 * added better multitexture support
135 *
136 * 120 8/27/98 11:16a Jason
137 * fixed math bug in GetPolymodelPointInWorld
138 *
139 * 119 8/25/98 12:22p Jason
140 * more changes for cockpits
141 *
142 * 118 8/24/98 6:43p Keneta
143 * Long proofed the turrets and weapon batteries
144 *
145 * 117 8/24/98 2:44p Jason
146 * checked in so Samir can test
147 *
148 * 116 8/21/98 5:14p Jason
149 * made better memory use of primitives
150 *
151 * 115 8/18/98 11:38a Jason
152 * fixed polymodel fog lighting
153 *
154 * 114 8/18/98 11:26a Matt
155 * Changed outline code to only outline polymodel objects when they're
156 * being rendered in the mine, and not when they're being drawn in the
157 * editor, main menu, cockpit, etc.
158 *
159 * 113 8/17/98 12:10p Chris
160 * Fixed MAJOR bug in getting gunpoint positions
161 *
162 * 112 8/16/98 9:52p Chris
163 * Added some error checking for the attach system
164 *
165 * 111 8/14/98 4:00p Jason
166 * added specular objects outside
167 *
168 * 110 8/13/98 6:56p Jason
169 * made objects foggable correctly
170 *
171 * 109 8/12/98 12:04p Chris
172 * Attach system version .5 (still needs more work to be multiplayer
173 * friendly)
174 *
175 * 108 8/07/98 2:49p Keneta
176 * fixed number of subobjects assertion
177 *
178 * 107 8/04/98 2:32p Chris
179 * Improved attach code added more fixes to the AABB partial computation
180 * patch
181 *
182 * 106 7/31/98 11:52a Chris
183 * Weapons can be persistent. Added ability for objects to be manually
184 * set for no object collisions.
185 *
186 * 105 7/30/98 1:13p Chris
187 * Improved attach point code
188 *
189 * 104 7/29/98 5:05p Chris
190 * Made more of the wb info dynamically allocated. Added Attach points to
191 * the polymodel structure
192 *
193 * 103 7/27/98 10:39a Jason
194 * added customizable skins
195 *
196 * 102 7/02/98 2:49p Chris
197 *
198 * 101 7/02/98 2:47p Chris
199 * Dynamic weapon info is now dynamically allocated
200 * Dynamic weapon info is now dynamically allocated
201 *
202 * 100 6/15/98 4:00p Jason
203 * replaced monochromatic polymodel lighting with rgb lighting
204 *
205 * 99 5/27/98 5:17p Jason
206 * fixed some bugs for the E3 Demo
207 *
208 * 98 5/15/98 3:24p Keneta
209 *
210 * 97 4/30/98 11:32a Chris
211 * ClearWB to WBClear
212 *
213 * 96 4/24/98 1:35p Jason
214 * Don't sort non-alphaed polymodels
215 *
216 * 95 4/22/98 12:10p Chris
217 * Fixed path length problems
218 *
219 * 94 4/17/98 12:45p Jason
220 * various changes for multiplayer
221 *
222 * 93 4/15/98 3:28p Jason
223 * changed glow stuff to work with new system
224 *
225 * 92 4/07/98 3:35p Jason
226 * possible fix for polymodel paging bug
227 *
228 * 91 4/06/98 3:03p Jason
229 * added polygon rendering overlays
230 *
231 * 90 4/03/98 12:23p Jason
232 * dealt with overlay types being loaded from disk more than once
233 *
234 * 89 4/03/98 11:55a Jason
235 * fixed polymodel paging problem
236 *
237 * 88 4/03/98 10:07a Chris
238 * Added support for objects getting their size computed when the
239 * polymodel is paged in the first time as an object
240 *
241 * 87 4/02/98 3:54p Jason
242 * first pass in getting polymodel paging to work
243 *
244 * 86 4/01/98 6:23p Jason
245 * added a slew of stuff for multiplayer
246 *
247 * 85 3/31/98 3:49p Jason
248 * added memory lib
249 *
250 * 84 3/31/98 11:12a Brent
251 * added assert for max textures
252 *
253 * 83 3/30/98 12:27a Jason
254 * fixed memory leaks as reported by BoundsChecker
255 *
256 * 82 3/25/98 5:51p Chris
257 * Added full model/body animations for weapon firing
258 *
259 * 81 3/24/98 2:12p Brent
260 * fixed another glowcount bug
261 *
262 * 80 3/24/98 12:47p Brent
263 * fixed glow bug
264 *
265 * 79 3/24/98 12:38p Brent
266 * added assert
267 *
268 * 78 3/23/98 10:03a Chris
269 * Added independant wb animations
270 *
271 * 77 3/19/98 4:30p Samir
272 * added ability to mark subobjects as layered.
273 *
274 * 76 3/18/98 6:24p Samir
275 * Fixed some bugs with normalizing MONITORs and added VIEWER flag.
276 *
277 * 75 3/16/98 11:34a Chris
278 * Polymodels have 3 levels of AABB (object, subobject, and face level)
279 *
280 * 74 3/10/98 2:03p Chris
281 * Added support for 0.0 angle fov
282 *
283 * 73 2/16/98 4:05p Jason
284 * render polymodels sorted or unsorted depending on whether or not they
285 * have alpha faces
286 *
287 * 72 2/16/98 2:49p Chris
288 * Made the MAX_SUBOBJECTS determine the number of normalized_time values
289 * to be processed. No longer a 'literal' problem.
290 *
291 * 71 2/16/98 2:25p Jason
292 * fixed bad indexing into subobjects problem
293 *
294 * 70 2/16/98 2:12p Jason
295 * fixed bug with lightmaps and door shells
296 *
297 * 69 2/13/98 6:59p Chris
298 * FIxed the normalized_time array from being smaller than the number of
299 * subobjects. Also update newstyle_fi.cpp
300 *
301 * 68 2/13/98 12:44p Jason
302 * upped max number of subobjects
303 *
304 * 67 2/10/98 3:50p Jason
305 * added pulsing walls
306 *
307 * 66 2/06/98 8:08p Jason
308 * fixed size problem with facing subobjects
309 *
310 * 65 2/06/98 6:44p Jason
311 * made polymodels replace themselves if they find the same name
312 *
313 * 64 2/06/98 4:33p Jason
314 * fixed facing model sizes
315 *
316 * 63 2/06/98 1:45p Jason
317 * No really, I fixed the min max problem.
318 *
319 * 62 2/06/98 12:53p Jason
320 * fixed object min max stuff
321 *
322 * 61 2/04/98 9:28p Jason
323 * added the ability to have models that always face you
324 *
325 * 60 1/26/98 4:32p Jason
326 * took out some goofy mprintfs
327 *
328 * 59 1/23/98 5:39p Matt
329 * Removed some door frontface error checking now more thoroughly handled
330 * in WorldObjectsDoorDialog.cpp
331 *
332 * 58 1/23/98 11:16a Luke
333 * Remove a really old Assert that limitted which types could animate
334 *
335 * 57 1/13/98 3:46p Jason
336 * fixed memory corruption bug introduced by my last rev
337 *
338 * 56 1/13/98 3:09p Jason
339 * added glow effect for engines
340 *
341 * 55 1/02/98 1:03p Jason
342 * fixed memory deallocation leak in FreePolymodel
343 *
344 * 54 12/31/97 3:34p Jason
345 * added alpha per vertex for polymodels
346 *
347 * 53 12/08/97 6:18p Jason
348 * more tweaks for destroyable buildings
349 *
350 * 52 11/17/97 6:28p Mark
351 * FROM Jason:fixed memory leak
352 *
353 * 51 11/17/97 12:02p Jason
354 * added extra newstyle variable set
355 *
356 * 50 11/10/97 3:33p Jason
357 * ignore whitespace
358 *
359 * 49 11/04/97 4:58p Samir
360 * Added 4 more monitors.
361 *
362 * 48 10/30/97 10:53a Jason
363 * set color model to be MONO when drawing gouraud shaded polymodels
364 *
365 * 47 10/28/97 6:37p Samir
366 * Submodels now can be monitors.
367 *
368 * 46 10/28/97 11:10a Jason
369 * tweaked subojbect rotation
370 *
371 * 45 10/24/97 11:57a Jason
372 * fixed memory leak
373 *
374 * 44 10/23/97 2:07p Jason
375 * fixed some problems with debris not spinning about their center point
376 *
377 * 43 10/20/97 4:46p Jason
378 * changes for explosions
379 *
380 * 42 10/06/97 6:39p Jason
381 * fixed frame min/max problem
382 *
383 * 41 10/05/97 5:30a Chris
384 * Added more support for TIMED animations
385 *
386 * 40 10/01/97 7:00p Jason
387 * did more work on object lightmaps
388 *
389 * 39 9/30/97 6:40p Jason
390 * got lightmap stuff sort of working with objects
391 *
392 * 38 9/29/97 3:48p Jason
393 * added more code to support new timed models
394 *
395 * 37 9/26/97 12:09p Jason
396 * made positional/rotational animations have differing
397 * track_min/track_max
398 *
399 * 36 9/25/97 4:54p Jason
400 * added timer info to polymodels
401 *
402 * 35 9/18/97 1:27p Matt
403 * Cleaned up object struct
404 *
405 * 34 9/17/97 10:59a Chris
406 * Added a new way to compute radi
407 *
408 * 33 9/15/97 11:11a Jason
409 * fixed mins/maxs to be based on the center of the model
410 *
411 * 32 9/12/97 5:38p Jason
412 * got doors working
413 *
414 * 31 9/11/97 5:38p Jason
415 * initial door coding for room engine
416 *
417 * 30 9/10/97 6:01p Jason
418 * fixed double used variable bug
419 *
420 * 29 9/10/97 5:17p Jason
421 * more lighting improvements
422 *
423 * 28 9/09/97 6:55p Jason
424 * better error checking for model usage counts
425 *
426 * 27 9/09/97 6:15p Jason
427 * made dynamic lighting on objects more memory efficient
428 *
429 * 26 9/09/97 11:05a Jason
430 * fixed wacky bizarre problem with instancing and positional
431 * interpolation
432 *
433 * 25 9/05/97 1:29p Jason
434 * revamped generic object lighting
435 *
436 * 24 9/04/97 5:38p Jason
437 * fixed dumb bug that was fixed a long time ago...model positional
438 * interpolation
439 *
440 * 23 9/04/97 3:49p Chris
441 * Added additional turret information from pofgen, added ground plane
442 * stuff
443 *
444 * 22 9/03/97 4:41p Chris
445 * Moved some code so that pofview will compile
446 *
447 * 21 9/03/97 2:12p Chris
448 * Added new weapon battery system and made the animation system usable.
449 *
450 * 20 8/25/97 6:19p Chris
451 * Added support for knowing which subobject a gun point is linked to
452 *
453 * 19 8/24/97 1:40p Jason
454 * don't compute normals on model load...let pofgen do it
455 *
456 * 18 8/22/97 12:13p Jason
457 * made rotators work on keyframe 1 instead of keyframe 0 since max
458 * insists on some weird "start axis" for keyframe 0
459 *
460 * 17 8/20/97 4:24p Matt
461 * Removed unused code
462 *
463 * 16 8/20/97 3:16p Chris
464 * Added some hooks for turrets
465 *
466 * 15 8/08/97 4:38p Jason
467 * added sliding textures for models
468 *
469 * 14 8/08/97 2:30p Jason
470 * added error checking for rotators without keyframes
471 *
472 * 13 8/08/97 10:58a Jason
473 * fixed out of bounds error
474 *
475 * 12 8/07/97 3:19p Jason
476 * only remap models when explicitely told to
477 *
478 * 11 8/06/97 3:07p Jason
479 * fixed problem where polymodels were being freed twice
480 *
481 * 10 8/06/97 12:40p Jason
482 * fixed some potentially serious memory problems
483 *
484 * 9 8/05/97 5:18p Jason
485 * fixed circular dependencies with child/parents
486 *
487 * 8 8/03/97 2:25p Jason
488 * made polymodels use less memory
489 *
490 * 7 7/28/97 1:14p Chris
491 * Added support for sub-object visability. Plus, debris.
492 *
493 * 6 7/23/97 11:48a Jason
494 * added support for newstyle pof format
495 *
496 * 5 7/22/97 1:34a Jason
497 * added code to support newstyle polymodels
498 *
499 * 4 7/15/97 4:27p Mark
500 *
501 * 36 6/24/97 6:13p Matt
502 * Got rid of DrawPolygonObject(), since it required the inclusion of
503 * object.h, which is a main directory header file (& thus shouldn't be
504 * accessible by a library).
505 *
506 * 34 5/20/97 7:21p Jason
507 * fixed stupid off-by-one bug
508 *
509 * 33 5/20/97 5:52p Jason
510 * tweaked a couple of things with magnitude division
511 *
512 * 32 5/19/97 5:10p Jason
513 * changes for our new abstracted renderer code
514 *
515 * 31 5/19/97 11:46a Jason
516 * normalize rotation vectors
517 *
518 * 30 5/16/97 3:13p Jason
519 * fixed more problems with model rotation
520 *
521 * 29 5/16/97 1:24p Jason
522 * changed the way models work their angle magic
523 * Now its much more intuitive
524 *
525 * 28 5/16/97 12:04p Jason
526 * better memory use for rotational keyframes
527 *
528 * 27 5/15/97 4:23p Jason
529 * swap interpreted model data for mac
530 *
531 * 26 5/14/97 11:57p Jason
532 * made polymodels use far less memory
533 *
534 * 25 5/14/97 7:45p Jason
535 * fixed polymodel bug where it would not get deallocated correctly
536 *
537 *
538 * 24 5/13/97 5:59p Jason
539 * fixed yet another local transform bug
540 *
541 * 23 5/13/97 12:43p Jason
542 * fixed a delta rotation problem, plus add interpolated rotational axis
543 *
544 * 22 5/08/97 5:11p Jason
545 * fixed bug with animation rotation keyframes
546 *
547 * 21 5/08/97 1:16p Jason
548 * made ChangeEndName work with device independant calls
549 *
550 * 20 4/30/97 5:43p Jason
551 * remap polymodels when pagefile is done loading
552 *
553 * 19 4/28/97 6:46p Jason
554 * made ships have multiple gun points
555 *
556 * 18 4/21/97 5:29p Jason
557 * got animating textures to work on polygonal objects
558 *
559 * 17 4/21/97 4:09p Jason
560 * added GetNormalizedKeyframe function
561 *
562 * 16 4/04/97 11:48a Jason
563 * render polygon objects as linear maps
564 *
565 * 15 4/02/97 5:21p Jason
566 * added the ability for pages to free the models that they point to
567 *
568 * 14 3/26/97 3:29p Jason
569 * made robots work/render
570 *
571 * 13 3/18/97 5:22p Jason
572 * took out dumb mprintfs
573 *
574 * 12 3/14/97 5:54p Jason
575 * made positional interpolation work with 3d doors
576 *
577 * 11 3/14/97 10:55a Jason
578 * made polymodels use delta angles instead of absolute angles
579 *
580 * 10 3/13/97 6:13p Jason
581 * got poly doors working
582 *
583 * 9 3/07/97 3:59p Samir
584 * Took out include to memory.h for portability.
585 *
586 * 8 3/05/97 3:10p Jason
587 * added FreeAllModels call
588 *
589 * 7 3/05/97 12:17p Jason
590 * took out autoloading of the ship.pof upon startup
591 *
592 * 6 3/04/97 11:52a Jason
593 * made polymodels remap correctly
594 *
595 * 5 3/03/97 6:21p Matt
596 * Changed cfile functions to use D3 naming convention
597 *
598 * $NoKeywords: $
599 */
600
601 #include <algorithm>
602 #include <cstdlib>
603 #include <cstring>
604 #include <filesystem>
605
606 #include "3d.h"
607 #include "cfile.h"
608 #include "game.h"
609 #include "gamesequence.h"
610 #include "mem.h"
611 #include "mono.h"
612 #include "objinfo.h"
613 #include "polymodel.h"
614 #include "pserror.h"
615
616 int Num_poly_models = 0;
617 poly_model Poly_models[MAX_POLY_MODELS];
618
619 g3Point Robot_points[MAX_POLYGON_VECS];
620
621 vector Interp_pos_instance_vec = {0, 0, 0};
622 vector Instance_vec_stack[MAX_SUBOBJECTS];
623 int Instance_vec_cnt = 0;
624
625 #ifdef _DEBUG
626 // Flag to draw an outline around the faces
627 bool Polymodel_outline_mode = false;
628 #endif
629
630 #define ID_OHDR 'RDHO' // POF file header
631 #define ID_SOBJ 'JBOS' // Subobject header
632 #define ID_IDTA 'ATDI' // Interpreter data
633 #define ID_TXTR 'RTXT' // Texture filename list
634 #define ID_INFO 'FNIP' // POF file information, like command line, etc
635 #define ID_GRID 'DIRG' // Grid information
636 #define ID_GPNT 'TNPG' // gun points
637 #define ID_ROT_ANIM 'INAR' // angular animation data
638 #define ID_POS_ANIM 'INAP' // positional animation data
639 #define ID_ANIM 'MINA' // angular information
640 #define ID_WBS 'TABW' // Weapon Battery Info
641 #define ID_GROUND 'DNRG' // Ground Plane info
642 #define ID_ATTACH 'HCTA' // Attach points
643 #define ID_ATTACH_NORMALS 'HTAN' // Attach uvecs
644
645 int Polymodel_use_effect = 0;
646 polymodel_effect Polymodel_effect;
647 polymodel_light_type Polymodel_light_type = POLYMODEL_LIGHTING_STATIC;
648 float Polylighting_static_red;
649 float Polylighting_static_green;
650 float Polylighting_static_blue;
651 lightmap_object *Polylighting_lightmap_object;
652
653 vector *Polymodel_light_direction, Polymodel_fog_plane, Polymodel_specular_pos, Polymodel_fog_portal_vert,
654 Polymodel_bump_pos;
655
656 static inline void RecursiveAssignWB(poly_model *pm, int sm_index, int wb_index);
657 static void FindWBSubobjects(poly_model *pm);
658 /// Sets aside a polymodel for use.
659 /// Errors and returns -1 if none free.
660 static int AllocPolyModel();
661 static void ReadModelVector(vector *vec, CFILE *infile);
662 static void ReadModelStringLen(char *ptr, int len, CFILE *infile);
663 /// Given a modelnumber, opens the original pof file and attempts to rematch that
664 /// models textures with the bitmaps with have in memory.
665 static int ReloadModelTextures(int modelnum);
666 static void SetPolymodelProperties(bsp_info *subobj, char *props);
667 static void MinMaxSubmodel(poly_model *pm, bsp_info *sm, vector offset);
668 static void FindMinMaxForModel(poly_model *pm);
669 static int ReadNewModelFile(int polynum, CFILE *infile);
670 static void SetNormalizedTimeObjTimed(object *obj, float *normalized_time);
671 static void SetNormalizedTimeAnimTimed(float frame, float *normalized_time, poly_model *pm);
672 static void FreeAllModels();
673 /// Given a model pointer and an array of floats that go from 0..1, calculate the interpolated
674 /// position/angle of each corresponding subobject.
675 static void SetModelAnglesAndPosTimed(poly_model *po, float *normalized_time, uint32_t subobj_flags);
676 static void BuildModelAngleMatrix(matrix *mat, angle ang, vector *axis);
677
678 void WBClearInfo(poly_model *pm) { pm->num_wbs = 0; }
679
680 inline void RecursiveAssignWB(poly_model *pm, int sm_index, int wb_index) {
681 int flags;
682 int i;
683
684 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
685
686 flags = wb_index << WB_INDEX_SHIFT;
687
688 pm->submodel[sm_index].flags |= flags | SOF_WB;
689
690 for (i = 0; i < pm->submodel[sm_index].num_children; i++) {
691 RecursiveAssignWB(pm, pm->submodel[sm_index].children[i], wb_index);
692 }
693 }
694
695 void FindWBSubobjects(poly_model *pm) {
696 int i;
697 bool found;
698
699 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
700
701 for (i = 0; i < pm->n_models; i++) {
702 if (pm->submodel[i].flags & SOF_TURRET) {
703 int j;
704 found = false;
705
706 for (j = 0; j < pm->num_wbs; j++) {
707 int k;
708
709 for (k = 0; k < pm->poly_wb[j].num_turrets; k++) {
710 if (pm->poly_wb[j].turret_index[k] == i) {
711 found = true;
712 RecursiveAssignWB(pm, i, j);
713 }
714
715 if (found)
716 break;
717 }
718
719 if (found)
720 break;
721 }
722 }
723 }
724 }
725
726 // Sets aside a polymodel for use
727 // Errors and returns -1 if none free
728 int AllocPolyModel() {
729 for (int i = 0; i < MAX_POLY_MODELS; i++)
730 if (Poly_models[i].used == 0) {
731 WBClearInfo(&Poly_models[i]);
732 memset(&Poly_models[i], 0, sizeof(poly_model));
733 Poly_models[i].used = 1;
734 Poly_models[i].flags |= PMF_NOT_RESIDENT; // not in memory yet!
735 return i;
736 }
737
738 Error("Couldn't find a free polymodel!");
739 return -1;
740 }
741
742 // Frees all the polymodel data, but doesn't free the actual polymodel itself
743 void FreePolymodelData(int i) {
744 int t;
745
746 for (t = 0; t < Poly_models[i].n_models; t++) {
747 if (Poly_models[i].submodel) {
748 if (Poly_models[i].submodel[t].keyframe_axis) {
749 mem_free(Poly_models[i].submodel[t].keyframe_axis);
750 Poly_models[i].submodel[t].keyframe_axis = nullptr;
751 }
752
753 if (Poly_models[i].submodel[t].tick_pos_remap) {
754 mem_free(Poly_models[i].submodel[t].tick_pos_remap);
755 Poly_models[i].submodel[t].tick_pos_remap = nullptr;
756 }
757
758 if (Poly_models[i].submodel[t].tick_ang_remap) {
759 mem_free(Poly_models[i].submodel[t].tick_ang_remap);
760 Poly_models[i].submodel[t].tick_ang_remap = nullptr;
761 }
762
763 if (Poly_models[i].submodel[t].keyframe_angles) {
764 mem_free(Poly_models[i].submodel[t].keyframe_angles);
765 Poly_models[i].submodel[t].keyframe_angles = nullptr;
766 }
767 if (Poly_models[i].submodel[t].keyframe_matrix) {
768 mem_free(Poly_models[i].submodel[t].keyframe_matrix);
769 Poly_models[i].submodel[t].keyframe_matrix = nullptr;
770 }
771
772 if (Poly_models[i].submodel[t].keyframe_pos) {
773 mem_free(Poly_models[i].submodel[t].keyframe_pos);
774 Poly_models[i].submodel[t].keyframe_pos = nullptr;
775 }
776
777 if (Poly_models[i].submodel[t].verts) {
778 mem_free(Poly_models[i].submodel[t].verts);
779 Poly_models[i].submodel[t].verts = nullptr;
780 }
781
782 if (Poly_models[i].submodel[t].vertnorms) {
783 mem_free(Poly_models[i].submodel[t].vertnorms);
784 Poly_models[i].submodel[t].vertnorms = nullptr;
785 }
786
787 if (Poly_models[i].submodel[t].alpha) {
788 mem_free(Poly_models[i].submodel[t].alpha);
789 Poly_models[i].submodel[t].alpha = nullptr;
790 }
791
792 if (Poly_models[i].submodel[t].vertnum_memory) {
793 mem_free(Poly_models[i].submodel[t].vertnum_memory);
794 Poly_models[i].submodel[t].vertnum_memory = nullptr;
795 }
796
797 if (Poly_models[i].submodel[t].u_memory) {
798 mem_free(Poly_models[i].submodel[t].u_memory);
799 Poly_models[i].submodel[t].u_memory = nullptr;
800 }
801
802 if (Poly_models[i].submodel[t].v_memory) {
803 mem_free(Poly_models[i].submodel[t].v_memory);
804 Poly_models[i].submodel[t].v_memory = nullptr;
805 }
806
807 if (Poly_models[i].flags & PMF_TIMED) {
808 if (Poly_models[i].submodel[t].rot_start_time) {
809 mem_free(Poly_models[i].submodel[t].rot_start_time);
810 Poly_models[i].submodel[t].rot_start_time = nullptr;
811 }
812
813 if (Poly_models[i].submodel[t].pos_start_time) {
814 mem_free(Poly_models[i].submodel[t].pos_start_time);
815 Poly_models[i].submodel[t].pos_start_time = nullptr;
816 }
817 }
818
819 if (Poly_models[i].submodel[t].flags & (SOF_GLOW | SOF_THRUSTER)) {
820 mem_free(Poly_models[i].submodel[t].glow_info);
821 Poly_models[i].submodel[t].glow_info = nullptr;
822 }
823
824 if (Poly_models[i].submodel[t].faces) {
825 mem_free(Poly_models[i].submodel[t].faces);
826 Poly_models[i].submodel[t].faces = nullptr;
827
828 if (Poly_models[i].submodel[t].face_min) {
829 mem_free(Poly_models[i].submodel[t].face_min);
830 Poly_models[i].submodel[t].face_min = nullptr;
831 }
832 if (Poly_models[i].submodel[t].face_max) {
833 mem_free(Poly_models[i].submodel[t].face_max);
834 Poly_models[i].submodel[t].face_max = nullptr;
835 }
836 }
837 }
838 }
839
840 if (Poly_models[i].model_data) {
841 mem_free(Poly_models[i].model_data);
842 Poly_models[i].model_data = nullptr;
843 }
844 if (Poly_models[i].gun_slots) {
845 mem_free(Poly_models[i].gun_slots);
846 Poly_models[i].gun_slots = nullptr;
847 }
848 if (Poly_models[i].poly_wb) {
849 mem_free(Poly_models[i].poly_wb);
850 Poly_models[i].poly_wb = nullptr;
851 }
852 if (Poly_models[i].attach_slots) {
853 mem_free(Poly_models[i].attach_slots);
854 Poly_models[i].attach_slots = nullptr;
855 }
856
857 if (Poly_models[i].ground_slots) {
858 mem_free(Poly_models[i].ground_slots);
859 Poly_models[i].ground_slots = nullptr;
860 }
861 if (Poly_models[i].submodel) {
862 mem_free(Poly_models[i].submodel);
863 Poly_models[i].submodel = nullptr;
864 }
865
866 Poly_models[i].flags |= PMF_NOT_RESIDENT;
867 Poly_models[i].n_models = 0;
868 }
869
870 // Frees polymodel located in index of Poly_models array
871 void FreePolyModel(int i) {
872 ASSERT(Poly_models[i].used > 0);
873
874 Poly_models[i].used--;
875
876 if (Poly_models[i].used)
877 return;
878
879 FreePolymodelData(i);
880
881 Poly_models[i].used = 0;
882 Poly_models[i].flags |= PMF_NOT_RESIDENT;
883 }
884
885 void ReadModelVector(vector *vec, CFILE *infile) {
886 vec->x = cf_ReadFloat(infile);
887 vec->y = cf_ReadFloat(infile);
888 vec->z = cf_ReadFloat(infile);
889 }
890
891 void ReadModelStringLen(char *ptr, int len, CFILE *infile) {
892 int i;
893
894 int mlen = cf_ReadInt(infile);
895 for (i = 0; i < mlen; i++)
896 ptr[i] = cf_ReadByte(infile);
897 }
898
899 // Given a modelnumber, opens the original pof file and attempts to rematch that
900 // models textures with the bitmaps with have in memory
901 int ReloadModelTextures(int modelnum) {
902 CFILE *infile;
903 int done = 0, id, len;
904 poly_model *pm = &Poly_models[modelnum];
905
906 ASSERT(!(Poly_models[modelnum].flags & PMF_NOT_RESIDENT));
907
908 infile = cfopen(Poly_models[modelnum].name, "rb");
909 if (!infile)
910 return 0;
911
912 id = cf_ReadInt(infile);
913
914 if (id != 'OPSP')
915 Error("Bad ID in model file!");
916 cf_ReadInt(infile); // skip version
917
918 while (!done) {
919 if (cfeof(infile)) {
920 done = 1;
921 continue;
922 }
923
924 id = cf_ReadInt(infile); // read chunk type
925 len = cf_ReadInt(infile); // read chunk length
926
927 switch (id) {
928 case ID_TXTR: {
929 // Texture filename list
930 int i, n;
931 char name_buf[128];
932
933 n = cf_ReadInt(infile);
934 if (n != pm->n_textures) {
935 Int3(); // Get Jason, new model doesn't match old model!!!
936 cfclose(infile);
937 return 0;
938 }
939
940 for (i = 0; i < n; i++) {
941 int ret;
942 char temp[256];
943
944 // Read the name of this texture
945 ReadModelStringLen(name_buf, 127, infile);
946
947 strcpy(temp, name_buf);
948 strcat(temp, ".OGF");
949 ret = FindTextureBitmapName(temp);
950 if (ret == -1) {
951 // See if its already in memory
952 ret = FindTextureName(name_buf);
953 if (ret == -1) {
954 ret = 0;
955 // mprintf(0,"Object texture %s is not in memory!\n",name_buf);
956 }
957 }
958
959 pm->textures[i] = ret;
960 if (GameTextures[ret].alpha < .99)
961 pm->flags |= PMF_ALPHA;
962 }
963
964 done = 1;
965 break;
966 }
967
968 default:
969 cfseek(infile, len, SEEK_CUR);
970 break;
971 }
972 }
973
974 cfclose(infile);
975 return 1;
976 }
977
978 void BuildModelAngleMatrix(matrix *mat, angle ang, vector *axis) {
979 float x, y, z;
980 float s, c, t;
981
982 x = axis->x;
983 y = axis->y;
984 z = axis->z;
985
986 s = (float)FixSin(ang);
987 c = (float)FixCos(ang);
988 t = 1.0f - c;
989
990 mat->rvec.x = t * x * x + c;
991 mat->rvec.y = t * x * y + s * z;
992 mat->rvec.z = t * x * z - s * y;
993
994 mat->uvec.x = t * x * y - s * z;
995 mat->uvec.y = t * y * y + c;
996 mat->uvec.z = t * y * z + s * x;
997
998 mat->fvec.x = t * x * z + s * y;
999 mat->fvec.y = t * y * z - s * x;
1000 mat->fvec.z = t * z * z + c;
1001 }
1002
1003 void SetPolymodelProperties(bsp_info *subobj, char *props) {
1004 // first, extract the command
1005
1006 int len, i;
1007 char command[200], data[200];
1008
1009 len = strlen(props);
1010
1011 if (len < 3)
1012 return; // nothing to set!
1013
1014 for (i = 0; i < len; i++) {
1015 command[i] = props[i];
1016 if (command[i] == '=') {
1017 i++;
1018 break; // we've found some data!
1019 }
1020 }
1021
1022 command[i] = 0;
1023
1024 if (len != i) {
1025 int datastart = i;
1026
1027 for (; i < len; i++) {
1028 data[i - datastart] = props[i];
1029 }
1030 data[i - datastart] = 0;
1031 }
1032
1033 // now act on the command/data pair
1034
1035 if (!stricmp(command, "$rotate=")) {
1036 // constant rotation for a subobject
1037 float spinrate = atof(data);
1038
1039 if (spinrate <= 0 || spinrate > 20)
1040 return; // bad data
1041
1042 subobj->flags |= SOF_ROTATE;
1043 subobj->rps = 1.0f / spinrate;
1044
1045 return;
1046 }
1047
1048 if (!strnicmp(command, "$jitter", 7)) {
1049 // this subobject is a jittery object
1050 subobj->flags |= SOF_JITTER;
1051
1052 return;
1053 }
1054
1055 if (!strnicmp(command, "$shell", 6)) {
1056 // this subobject is a door shell
1057
1058 subobj->flags |= SOF_SHELL;
1059
1060 return;
1061 }
1062 if (!strnicmp(command, "$facing", 7)) {
1063 // this subobject always faces you
1064 subobj->flags |= SOF_FACING;
1065 return;
1066 }
1067
1068 if (!strnicmp(command, "$frontface", 10)) {
1069 // this subobject is a door front
1070
1071 subobj->flags |= SOF_FRONTFACE;
1072
1073 return;
1074 }
1075
1076 if (!stricmp(command, "$glow=")) {
1077 float r, g, b;
1078 float size;
1079 int num_found;
1080
1081 ASSERT(!(subobj->flags & (SOF_GLOW | SOF_THRUSTER)));
1082
1083 num_found = sscanf(data, " %f, %f, %f, %f", &r, &g, &b, &size);
1084
1085 ASSERT(num_found == 4);
1086
1087 subobj->flags |= SOF_GLOW;
1088
1089 if (subobj->glow_info == nullptr) // DAJ may already exist
1090 subobj->glow_info = (glowinfo *)mem_malloc(sizeof(glowinfo));
1091
1092 subobj->glow_info->glow_r = r;
1093 subobj->glow_info->glow_g = g;
1094 subobj->glow_info->glow_b = b;
1095 subobj->glow_info->glow_size = size;
1096
1097 return;
1098 }
1099
1100 if (!stricmp(command, "$thruster=")) {
1101 float r, g, b;
1102 float size;
1103 int num_found;
1104
1105 ASSERT(!(subobj->flags & (SOF_GLOW | SOF_THRUSTER)));
1106
1107 num_found = sscanf(data, " %f, %f, %f, %f", &r, &g, &b, &size);
1108
1109 ASSERT(num_found == 4);
1110
1111 subobj->flags |= SOF_THRUSTER;
1112
1113 if (subobj->glow_info == nullptr) // DAJ may already exist
1114 subobj->glow_info = (glowinfo *)mem_malloc(sizeof(glowinfo));
1115
1116 subobj->glow_info->glow_r = r;
1117 subobj->glow_info->glow_g = g;
1118 subobj->glow_info->glow_b = b;
1119 subobj->glow_info->glow_size = size;
1120
1121 return;
1122 }
1123
1124 if (!stricmp(command, "$fov=")) {
1125 float fov_angle;
1126 float turret_spr;
1127 float reaction_time;
1128 int num_found;
1129
1130 num_found = sscanf(data, " %f, %f, %f", &fov_angle, &turret_spr, &reaction_time);
1131
1132 ASSERT(num_found == 3);
1133
1134 if (fov_angle < 0.0f || fov_angle > 360.0f) {
1135 // Bad data
1136 ASSERT(0);
1137 fov_angle = 1.0;
1138 }
1139
1140 // .4 is really fast and really arbitrary
1141 if (turret_spr < 0.0f || turret_spr > 60.0f) {
1142 // Bad data
1143 ASSERT(0);
1144 turret_spr = 1.0f;
1145 }
1146
1147 // 10 seconds is really slow and really arbitrary
1148 if (reaction_time < 0.0f || reaction_time > 10.0f) {
1149 // Bad data
1150 ASSERT(0);
1151 reaction_time = 10.0f;
1152 }
1153
1154 subobj->flags |= SOF_TURRET;
1155 subobj->fov = fov_angle / 720.0f; // 720 = 360 * 2 and we want to make fov the amount we can move in either
1156 // direction it has a minimum value of (0.0) to [0.5]
1157 subobj->rps = 1.0f / turret_spr; // convert spr to rps (rotations per second)
1158 subobj->think_interval = reaction_time;
1159
1160 return;
1161 }
1162
1163 if (!stricmp(command, "$monitor01")) {
1164 // this subobject is a monitor
1165 subobj->flags |= SOF_MONITOR1;
1166 return;
1167 }
1168
1169 if (!stricmp(command, "$monitor02")) {
1170 // this subobject is a 2nd monitor
1171 subobj->flags |= SOF_MONITOR2;
1172 return;
1173 }
1174
1175 if (!stricmp(command, "$monitor03")) {
1176 // this subobject is a 3rd monitor
1177 subobj->flags |= SOF_MONITOR3;
1178 return;
1179 }
1180
1181 if (!stricmp(command, "$monitor04")) {
1182 // this subobject is a 4th monitor
1183 subobj->flags |= SOF_MONITOR4;
1184 return;
1185 }
1186
1187 if (!stricmp(command, "$monitor05")) {
1188 // this subobject is a 4th monitor
1189 subobj->flags |= SOF_MONITOR5;
1190 return;
1191 }
1192
1193 if (!stricmp(command, "$monitor06")) {
1194 // this subobject is a 4th monitor
1195 subobj->flags |= SOF_MONITOR6;
1196 return;
1197 }
1198
1199 if (!stricmp(command, "$monitor07")) {
1200 // this subobject is a 4th monitor
1201 subobj->flags |= SOF_MONITOR7;
1202 return;
1203 }
1204
1205 if (!stricmp(command, "$monitor08")) {
1206 // this subobject is a 4th monitor
1207 subobj->flags |= SOF_MONITOR8;
1208 return;
1209 }
1210
1211 if (!stricmp(command, "$viewer")) {
1212 // this subobject is a viewer
1213 subobj->flags |= SOF_VIEWER;
1214 return;
1215 }
1216
1217 if (!stricmp(command, "$layer")) {
1218 // this subobject is a layer to be drawn after original object.
1219 subobj->flags |= SOF_LAYER;
1220 return;
1221 }
1222 if (!stricmp(command, "$custom")) {
1223 // this subobject has custom textures/colors
1224 subobj->flags |= SOF_CUSTOM;
1225
1226 return;
1227 }
1228 }
1229
1230 void MinMaxSubmodel(poly_model *pm, bsp_info *sm, vector offset) {
1231
1232 offset += sm->offset;
1233 // Get max
1234 if ((sm->max.x + offset.x) > pm->maxs.x)
1235 pm->maxs.x = sm->max.x + offset.x;
1236 if ((sm->max.y + offset.y) > pm->maxs.y)
1237 pm->maxs.y = sm->max.y + offset.y;
1238 if ((sm->max.z + offset.z) > pm->maxs.z)
1239 pm->maxs.z = sm->max.z + offset.z;
1240
1241 // Get min
1242 if ((sm->min.x + offset.x) < pm->mins.x)
1243 pm->mins.x = sm->min.x + offset.x;
1244 if ((sm->min.y + offset.y) < pm->mins.y)
1245 pm->mins.y = sm->min.y + offset.y;
1246 if ((sm->min.z + offset.z) < pm->mins.z)
1247 pm->mins.z = sm->min.z + offset.z;
1248
1249 for (int i = 0; i < sm->num_children; i++)
1250 MinMaxSubmodel(pm, &pm->submodel[sm->children[i]], offset);
1251 }
1252
1253 void FindMinMaxForModel(poly_model *pm) {
1254
1255 vector zero_vec;
1256
1257 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
1258
1259 vm_MakeZero(&zero_vec);
1260 pm->mins.x = pm->mins.y = pm->mins.z = 90000;
1261 pm->maxs.x = pm->maxs.y = pm->maxs.z = -90000;
1262
1263 for (int i = 0; i < pm->n_models; i++) {
1264 bsp_info *sm = &pm->submodel[i];
1265 if (sm->parent == -1)
1266 MinMaxSubmodel(pm, sm, zero_vec);
1267 }
1268 }
1269
1270 int ReadNewModelFile(int polynum, CFILE *infile) {
1271 int version, done = 0, i, t, version_major;
1272 int id, len;
1273 poly_model *pm = &Poly_models[polynum];
1274 int timed = 0;
1275
1276 ASSERT(pm->new_style);
1277
1278 id = cf_ReadInt(infile);
1279
1280 if (id != 'OPSP')
1281 Error("Bad ID in model file!");
1282
1283 // Version is major*100+minor
1284 // So, major = version / 100;
1285 // minor = version % 100;
1286
1287 version = cf_ReadInt(infile);
1288 if (version < 18) {
1289 mprintf(0, "Old POF Version of %d fixed up to %d\n", version, version * 100);
1290 version *= 100;
1291 }
1292
1293 if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION) {
1294 mprintf(0, "Bad version (%d) in model file!\n", version);
1295 Int3();
1296 return 0;
1297 }
1298
1299 pm->version = version;
1300 version_major = version / 100;
1301
1302 if (version_major >= 21)
1303 pm->flags |= PMF_LIGHTMAP_RES;
1304 if (version_major >= 22) {
1305 timed = 1;
1306 pm->flags |= PMF_TIMED;
1307 pm->frame_min = 0;
1308 pm->frame_max = 0;
1309 }
1310
1311 pm->n_attach = 0;
1312
1313 memset(&pm->view_pos, 0, sizeof(pm->view_pos));
1314
1315 while (!done) {
1316 if (cfeof(infile)) {
1317 done = 1;
1318 continue;
1319 }
1320
1321 id = cf_ReadInt(infile);
1322 len = cf_ReadInt(infile);
1323
1324 switch (id) {
1325 case ID_OHDR: {
1326 // Object header
1327
1328 // mprintf(0,"Object header...\n");
1329
1330 pm->n_models = cf_ReadInt(infile);
1331 pm->rad = cf_ReadFloat(infile);
1332
1333 pm->submodel = (bsp_info *)mem_malloc(sizeof(bsp_info) * pm->n_models);
1334 ASSERT(pm->submodel != nullptr);
1335 memset(pm->submodel, 0, sizeof(bsp_info) * pm->n_models);
1336
1337 ReadModelVector(&pm->mins, infile);
1338 ReadModelVector(&pm->maxs, infile);
1339
1340 // pm->n_detail_levels = cf_ReadInt(infile);
1341 int det = cf_ReadInt(infile); // skip detail
1342
1343 for (i = 0; i < det; i++) {
1344 // pm->detail[i] = cf_ReadInt(infile);
1345 // pm->detail_depth[i] = 0.0f;
1346 cf_ReadInt(infile);
1347 }
1348
1349 // Adjust min/maxs to be based on zero
1350 /*vector temp_vec=pm->maxs-pm->mins;
1351
1352 temp_vec/=2;
1353
1354 pm->mins=-temp_vec;
1355 pm->maxs=temp_vec;*/
1356
1357 break;
1358 }
1359
1360 case ID_SOBJ: {
1361 // Subobject header
1362 int n;
1363 char props[MAX_PROP_LEN];
1364 float d;
1365
1366 // mprintf(0,"Got chunk SOBJ, len=%d\n",len);
1367
1368 n = cf_ReadInt(infile);
1369
1370 ASSERT(n < pm->n_models);
1371
1372 pm->submodel[n].min.x = pm->submodel[n].min.y = pm->submodel[n].min.z = 90000;
1373 pm->submodel[n].max.x = pm->submodel[n].max.y = pm->submodel[n].max.z = -90000;
1374
1375 pm->submodel[n].parent = cf_ReadInt(infile);
1376
1377 ReadModelVector(&pm->submodel[n].norm, infile);
1378
1379 d = cf_ReadFloat(infile);
1380 ReadModelVector(&pm->submodel[n].pnt, infile);
1381 ReadModelVector(&pm->submodel[n].offset, infile);
1382
1383 pm->submodel[n].rad = cf_ReadFloat(infile); // radius
1384
1385 pm->submodel[n].tree_offset = cf_ReadInt(infile); // offset
1386 pm->submodel[n].data_offset = cf_ReadInt(infile); // offset
1387
1388 if (pm->version > 1805)
1389 ReadModelVector(&pm->submodel[n].geometric_center, infile);
1390 else {
1391 // for old models, hack in 0,0,0, which says the geometric center is
1392 // equal to pivot point, which is not always true.
1393 vm_MakeZero(&pm->submodel[n].geometric_center);
1394 }
1395
1396 pm->submodel[n].name[0] = '\0';
1397
1398 ReadModelStringLen(pm->submodel[n].name, PAGENAME_LEN, infile);
1399 ReadModelStringLen(props, MAX_PROP_LEN, infile); // and the user properites
1400
1401 SetPolymodelProperties(&pm->submodel[n], props);
1402
1403 pm->submodel[n].movement_type = cf_ReadInt(infile);
1404 pm->submodel[n].movement_axis = cf_ReadInt(infile);
1405
1406 if (pm->submodel[n].name[0] == '\0')
1407 strcpy(pm->submodel[n].name, "unknown object name");
1408
1409 memset(&pm->submodel[n].angs, 0, sizeof(angvec));
1410
1411 // Skip freespace chunks
1412 int n_chunks = cf_ReadInt(infile);
1413 if (n_chunks > 0) {
1414 for (i = 0; i < n_chunks; i++)
1415 cf_ReadInt(infile);
1416 }
1417
1418 int nverts = cf_ReadInt(infile);
1419 ASSERT(nverts < MAX_POLYGON_VECS);
1420
1421 if (nverts) {
1422 pm->submodel[n].verts = (vector *)mem_malloc(nverts * sizeof(vector));
1423 pm->submodel[n].vertnorms = (vector *)mem_malloc(nverts * sizeof(vector));
1424 pm->submodel[n].alpha = (float *)mem_malloc(nverts * sizeof(float));
1425 ASSERT(pm->submodel[n].verts);
1426 ASSERT(pm->submodel[n].vertnorms);
1427 ASSERT(pm->submodel[n].alpha);
1428 } else {
1429 // Let me take a moment right here to say how annoying it is that I can't
1430 // set all these pointers to nullptr with one assignment on the same line due
1431 // to the stupid strong typying they've added to C.
1432 pm->submodel[n].verts = nullptr;
1433 pm->submodel[n].vertnorms = nullptr;
1434 pm->submodel[n].alpha = nullptr;
1435 }
1436
1437 pm->submodel[n].nverts = nverts;
1438
1439 for (i = 0; i < nverts; i++) {
1440 ReadModelVector(&pm->submodel[n].verts[i], infile);
1441
1442 // Get max
1443 if (pm->submodel[n].verts[i].x > pm->submodel[n].max.x)
1444 pm->submodel[n].max.x = pm->submodel[n].verts[i].x;
1445 if (pm->submodel[n].verts[i].y > pm->submodel[n].max.y)
1446 pm->submodel[n].max.y = pm->submodel[n].verts[i].y;
1447 if (pm->submodel[n].verts[i].z > pm->submodel[n].max.z)
1448 pm->submodel[n].max.z = pm->submodel[n].verts[i].z;
1449
1450 // Get min
1451 if (pm->submodel[n].verts[i].x < pm->submodel[n].min.x)
1452 pm->submodel[n].min.x = pm->submodel[n].verts[i].x;
1453 if (pm->submodel[n].verts[i].y < pm->submodel[n].min.y)
1454 pm->submodel[n].min.y = pm->submodel[n].verts[i].y;
1455 if (pm->submodel[n].verts[i].z < pm->submodel[n].min.z)
1456 pm->submodel[n].min.z = pm->submodel[n].verts[i].z;
1457 }
1458 for (i = 0; i < nverts; i++)
1459 ReadModelVector(&pm->submodel[n].vertnorms[i], infile);
1460
1461 // Read alpha per vertex
1462 if (version_major >= 23) {
1463 for (i = 0; i < nverts; i++) {
1464 pm->submodel[n].alpha[i] = cf_ReadFloat(infile);
1465 if (pm->submodel[n].alpha[i] < .99)
1466 pm->flags |= PMF_ALPHA;
1467 }
1468 } else {
1469 for (i = 0; i < nverts; i++)
1470 pm->submodel[n].alpha[i] = 1.0;
1471 }
1472
1473 int nfaces = cf_ReadInt(infile);
1474 pm->submodel[n].num_faces = nfaces;
1475
1476 if (nfaces) {
1477 pm->submodel[n].faces = (polyface *)mem_malloc(nfaces * sizeof(polyface));
1478 pm->submodel[n].face_min = (vector *)mem_malloc(nfaces * sizeof(vector));
1479 pm->submodel[n].face_max = (vector *)mem_malloc(nfaces * sizeof(vector));
1480 ASSERT(pm->submodel[n].faces);
1481 } else {
1482 pm->submodel[n].faces = nullptr;
1483 pm->submodel[n].face_max = nullptr;
1484 pm->submodel[n].face_min = nullptr;
1485 }
1486
1487 // Find out how much space we'll need
1488 bsp_info *sm = &pm->submodel[n];
1489
1490 vector tvec;
1491 int current_count = 0;
1492
1493 int save_position = cftell(infile);
1494 int *start_index;
1495
1496 if (nfaces) {
1497 start_index = (int *)mem_malloc(sizeof(int) * nfaces);
1498 ASSERT(start_index);
1499 } else
1500 start_index = nullptr;
1501
1502 uint8_t tempbuf[2000];
1503
1504 for (i = 0; i < nfaces; i++) {
1505 ReadModelVector(&tvec, infile);
1506 int num_verts = cf_ReadInt(infile);
1507
1508 ASSERT(num_verts < 100);
1509 ASSERT(num_verts > 0);
1510 start_index[i] = current_count;
1511 current_count += num_verts;
1512
1513 // Skip the rest
1514 if (cf_ReadInt(infile))
1515 cf_ReadInt(infile);
1516 else
1517 cf_ReadBytes(tempbuf, 3, infile);
1518
1519 cf_ReadBytes(tempbuf, 12 * num_verts, infile);
1520
1521 if (version_major >= 21)
1522 cf_ReadBytes(tempbuf, 8, infile);
1523 }
1524
1525 // Allocate our space
1526 if (current_count) {
1527 sm->vertnum_memory = (int16_t *)mem_malloc(current_count * sizeof(int16_t));
1528 ASSERT(sm->vertnum_memory);
1529
1530 sm->u_memory = (float *)mem_malloc(current_count * sizeof(float));
1531 ASSERT(sm->u_memory);
1532
1533 sm->v_memory = (float *)mem_malloc(current_count * sizeof(float));
1534 ASSERT(sm->v_memory);
1535 } else {
1536 sm->vertnum_memory = nullptr;
1537 sm->u_memory = nullptr;
1538 sm->v_memory = nullptr;
1539 }
1540
1541 // Now go through and set up our fake pointers
1542 for (i = 0; i < nfaces; i++) {
1543 sm->faces[i].vertnums = &sm->vertnum_memory[start_index[i]];
1544 sm->faces[i].u = &sm->u_memory[start_index[i]];
1545 sm->faces[i].v = &sm->v_memory[start_index[i]];
1546 }
1547
1548 // Reset our file pointer and free the temp memory
1549 cfseek(infile, save_position, SEEK_SET);
1550
1551 if (start_index)
1552 mem_free(start_index);
1553
1554 for (i = 0; i < nfaces; i++) {
1555 ReadModelVector(&pm->submodel[n].faces[i].normal, infile);
1556
1557 int num_verts = cf_ReadInt(infile);
1558 pm->submodel[n].faces[i].nverts = num_verts;
1559
1560 int textured = cf_ReadInt(infile);
1561 if (textured) {
1562 pm->submodel[n].faces[i].texnum = cf_ReadInt(infile);
1563
1564 } else {
1565 pm->submodel[n].faces[i].texnum = -1;
1566 uint8_t r, g, b;
1567 r = cf_ReadByte(infile);
1568 g = cf_ReadByte(infile);
1569 b = cf_ReadByte(infile);
1570 pm->submodel[n].faces[i].color = GR_RGB(r, g, b);
1571 }
1572
1573 for (t = 0; t < pm->submodel[n].faces[i].nverts; t++) {
1574 int val;
1575 vector *min_ptr = &pm->submodel[n].face_min[i];
1576 vector *max_ptr = &pm->submodel[n].face_max[i];
1577 vector *v_ptr;
1578
1579 val = cf_ReadInt(infile);
1580 pm->submodel[n].faces[i].vertnums[t] = val;
1581 pm->submodel[n].faces[i].u[t] = cf_ReadFloat(infile);
1582 pm->submodel[n].faces[i].v[t] = cf_ReadFloat(infile);
1583
1584 v_ptr = &pm->submodel[n].verts[pm->submodel[n].faces[i].vertnums[t]];
1585
1586 if (t == 0) {
1587 *min_ptr = *max_ptr = *v_ptr;
1588 } else {
1589 if (v_ptr->x < min_ptr->x)
1590 min_ptr->x = v_ptr->x;
1591 else if (v_ptr->x > max_ptr->x)
1592 max_ptr->x = v_ptr->x;
1593
1594 if (v_ptr->y < min_ptr->y)
1595 min_ptr->y = v_ptr->y;
1596 else if (v_ptr->y > max_ptr->y)
1597 max_ptr->y = v_ptr->y;
1598
1599 if (v_ptr->z < min_ptr->z)
1600 min_ptr->z = v_ptr->z;
1601 else if (v_ptr->z > max_ptr->z)
1602 max_ptr->z = v_ptr->z;
1603 }
1604 }
1605
1606 // Do lightmap res computation
1607 if (version_major >= 21) {
1608 float xdiff, ydiff;
1609
1610 xdiff = cf_ReadFloat(infile);
1611 ydiff = cf_ReadFloat(infile);
1612 }
1613 }
1614
1615 break;
1616 }
1617
1618 case ID_GPNT:
1619 pm->n_guns = cf_ReadInt(infile);
1620 pm->gun_slots = (w_bank *)mem_malloc(sizeof(w_bank) * pm->n_guns);
1621 ASSERT(pm->gun_slots != nullptr);
1622
1623 for (i = 0; i < pm->n_guns; i++) {
1624 w_bank *bank = &pm->gun_slots[i];
1625
1626 // In Version 19.08 and beyond, gunpoints are associated to their parent object.
1627 if (version >= 19 * 100 + 8) {
1628 bank->parent = cf_ReadInt(infile);
1629 } else {
1630 bank->parent = 0;
1631 }
1632
1633 ReadModelVector(&bank->pnt, infile);
1634 ReadModelVector(&bank->norm, infile);
1635 }
1636 break;
1637
1638 case ID_ATTACH:
1639 pm->n_attach = cf_ReadInt(infile);
1640 if (pm->n_attach) {
1641 pm->attach_slots = (a_bank *)mem_malloc(sizeof(a_bank) * pm->n_attach);
1642 ASSERT(pm->attach_slots != nullptr);
1643
1644 for (i = 0; i < pm->n_attach; i++) {
1645 a_bank *bank = &pm->attach_slots[i];
1646
1647 bank->parent = cf_ReadInt(infile);
1648 ReadModelVector(&bank->pnt, infile);
1649 ReadModelVector(&bank->norm, infile);
1650 bank->f_uvec = false;
1651 }
1652 } else {
1653 pm->attach_slots = nullptr;
1654 }
1655 break;
1656
1657 case ID_ATTACH_NORMALS: {
1658 bool f_uvec = false;
1659 int num_normals;
1660
1661 if (pm->n_attach == (num_normals = cf_ReadInt(infile)))
1662 f_uvec = true;
1663 else {
•1664 mprintf(0, "WARNING: Ignoring ATTACH normals - total number doesn't match number of attach points\n");
1665 // Int3();
1666 DataError("Model <%s> specifies %d attach points but only contains %d attach normals\n", pm->name, pm->n_attach,
1667 num_normals);
1668 }
1669
1670 if (num_normals) {
1671 for (i = 0; i < num_normals; i++) {
1672 vector temp;
1673 a_bank *bank = &pm->attach_slots[i];
1674
1675 cf_ReadInt(infile);
1676 ReadModelVector(&temp, infile);
1677 ReadModelVector(&bank->uvec, infile);
1678 bank->f_uvec = f_uvec;
1679 }
1680 } else {
1681 pm->attach_slots = nullptr;
1682 }
1683 break;
1684 }
1685
1686 case ID_WBS: {
1687 int i;
1688 int j;
1689
1690 // Get the number of weapon batteries on this object
1691 pm->num_wbs = cf_ReadInt(infile);
1692
1693 if (pm->num_wbs) {
1694 pm->poly_wb = (poly_wb_info *)mem_malloc(sizeof(poly_wb_info) * pm->num_wbs);
1695
1696 // Get each individual wb info struct
1697 for (i = 0; i < pm->num_wbs; i++) {
1698 pm->poly_wb[i].num_gps = cf_ReadInt(infile);
1699 for (j = 0; j < pm->poly_wb[i].num_gps; j++) {
1700 if (j <= 7)
1701 pm->poly_wb[i].gp_index[j] = cf_ReadInt(infile);
1702 else
1703 cf_ReadInt(infile);
1704 }
1705
1706 if (pm->poly_wb[i].num_gps > 8)
1707 pm->poly_wb[i].num_gps = 8;
1708
1709 pm->poly_wb[i].num_turrets = cf_ReadInt(infile);
1710 for (j = 0; j < pm->poly_wb[i].num_turrets; j++) {
1711 if (j <= 7)
1712 pm->poly_wb[i].turret_index[j] = cf_ReadInt(infile);
1713 else
1714 cf_ReadInt(infile);
1715 }
1716 if (pm->poly_wb[i].num_turrets > 8)
1717 pm->poly_wb[i].num_turrets = 8;
1718 }
1719 } else {
1720 pm->poly_wb = nullptr;
1721 }
1722
1723 break;
1724 }
1725
1726 case ID_GROUND:
1727 pm->n_ground = cf_ReadInt(infile);
1728 pm->ground_slots = (w_bank *)mem_malloc(sizeof(w_bank) * pm->n_ground);
1729 ASSERT(pm->ground_slots != nullptr);
1730
1731 for (i = 0; i < pm->n_ground; i++) {
1732 w_bank *bank = &pm->ground_slots[i];
1733
1734 bank->parent = cf_ReadInt(infile);
1735
1736 ReadModelVector(&bank->pnt, infile);
1737 ReadModelVector(&bank->norm, infile);
1738 }
1739 break;
1740
1741 case ID_TXTR: {
1742 // Texture filename list
1743 int i, n;
1744 char name_buf[128];
1745
1746 // mprintf(0,"Got chunk TXTR, len=%d\n",len);
1747
1748 n = cf_ReadInt(infile);
1749 pm->n_textures = n;
1750 ASSERT(n < MAX_MODEL_TEXTURES);
1751
1752 for (i = 0; i < n; i++) {
1753 int ret;
1754 char temp[256];
1755
1756 // Read the name of this texture
1757 ReadModelStringLen(name_buf, 127, infile);
1758
1759 strcpy(temp, name_buf);
1760 strcat(temp, ".OGF");
1761
1762 ret = FindTextureBitmapName(temp);
1763 if (ret == -1) {
1764 // See if its already in memory
1765 ret = FindTextureName(name_buf);
1766 if (ret == -1) {
1767 ret = 0;
1768 // mprintf(0,"Object texture %s is not in memory!\n",name_buf);
1769 }
1770 }
1771
1772 pm->textures[i] = ret;
1773 if (GameTextures[ret].alpha < .99)
1774 pm->flags |= PMF_ALPHA;
1775 }
1776
1777 break;
1778 }
1779
1780 case ID_ROT_ANIM:
1781 case ID_ANIM: {
1782 int nframes = 0;
1783 // mprintf(0,"ROT ANIM chunk!!!\n");
1784
1785 if (!timed) {
1786 nframes = cf_ReadInt(infile);
1787 pm->num_key_angles = nframes;
1788 }
1789
1790 for (i = 0; i < pm->n_models; i++) {
1791
1792 if (timed) {
1793 pm->submodel[i].num_key_angles = cf_ReadInt(infile);
1794 pm->submodel[i].rot_track_min = cf_ReadInt(infile);
1795 pm->submodel[i].rot_track_max = cf_ReadInt(infile);
1796
1797 if (pm->submodel[i].rot_track_min < pm->frame_min)
1798 pm->frame_min = pm->submodel[i].rot_track_min;
1799 if (pm->submodel[i].rot_track_max > pm->frame_max)
1800 pm->frame_max = pm->submodel[i].rot_track_max;
1801 } else {
1802 pm->submodel[i].num_key_angles = nframes;
1803 }
1804
1805 pm->submodel[i].keyframe_axis = (vector *)mem_malloc((pm->submodel[i].num_key_angles + 1) * sizeof(vector));
1806 pm->submodel[i].keyframe_angles = (int *)mem_malloc((pm->submodel[i].num_key_angles + 1) * sizeof(int));
1807 pm->submodel[i].keyframe_matrix = (matrix *)mem_malloc((pm->submodel[i].num_key_angles + 1) * sizeof(matrix));
1808 if (timed) {
1809 pm->submodel[i].rot_start_time = (int *)mem_malloc((pm->submodel[i].num_key_angles + 1) * sizeof(int));
1810 ASSERT(pm->submodel[i].rot_start_time != nullptr);
1811
1812 int num_ticks = (pm->submodel[i].rot_track_max - pm->submodel[i].rot_track_min);
1813
1814 if (num_ticks > 0) {
1815 pm->submodel[i].tick_ang_remap = (uint16_t *)mem_malloc(num_ticks * 2);
1816 ASSERT(pm->submodel[i].tick_ang_remap);
1817 } else {
1818 pm->submodel[i].tick_ang_remap = nullptr;
1819 }
1820 }
1821
1822 ASSERT(pm->submodel[i].keyframe_axis != nullptr);
1823 ASSERT(pm->submodel[i].keyframe_angles != nullptr);
1824 ASSERT(pm->submodel[i].keyframe_matrix != nullptr);
1825
1826 for (t = 0; t < pm->submodel[i].num_key_angles; t++) {
1827 vector *axis;
1828 float mag;
1829
1830 if (timed)
1831 pm->submodel[i].rot_start_time[t] = cf_ReadInt(infile);
1832
1833 ReadModelVector(&pm->submodel[i].keyframe_axis[t + 0], infile);
1834 axis = &pm->submodel[i].keyframe_axis[t + 0];
1835
1836 mag = vm_GetMagnitude(axis);
1837 if (mag > 0)
1838 pm->submodel[i].keyframe_axis[t] /= mag;
1839
1840 pm->submodel[i].keyframe_angles[t + 0] = cf_ReadInt(infile);
1841 // if this axis is backwards, rotate the angle the other direction
1842 if (t == 0) {
1843 /*if (pm->submodel[i].keyframe_angles[0]>=32768)
1844 {
1845 mprintf(0,"\nFlipping suboject %s!\n",pm->submodel[i].name);
1846 pm->submodel[i].keyframe_angles[0]*=-1;
1847 }*/
1848 }
1849 /*else
1850 dir=vm_DotProduct (axis,&pm->submodel[i].keyframe_axis[1]);
1851
1852 if (dir<0)
1853 {
1854 pm->submodel[i].keyframe_angles[t+0]*=-1;
1855 pm->submodel[i].keyframe_axis[t+0]*=-1.0;
1856 }*/
1857 }
1858 }
1859
1860 break;
1861 }
1862
1863 case ID_POS_ANIM: {
1864 int nframes = 0;
1865
1866 // mprintf(0,"POS ANIM chunk!!!\n");
1867 if (!timed) {
1868 nframes = cf_ReadInt(infile);
1869 pm->num_key_pos = nframes;
1870 }
1871
1872 for (i = 0; i < pm->n_models; i++) {
1873 if (timed) {
1874 pm->submodel[i].num_key_pos = cf_ReadInt(infile);
1875 pm->submodel[i].pos_track_min = cf_ReadInt(infile);
1876 pm->submodel[i].pos_track_max = cf_ReadInt(infile);
1877
1878 if (pm->submodel[i].pos_track_min < pm->frame_min)
1879 pm->frame_min = pm->submodel[i].pos_track_min;
1880 if (pm->submodel[i].pos_track_max > pm->frame_max)
1881 pm->frame_max = pm->submodel[i].pos_track_max;
1882
1883 int num_ticks = (pm->submodel[i].pos_track_max - pm->submodel[i].pos_track_min);
1884
1885 if (num_ticks > 0) {
1886 pm->submodel[i].tick_pos_remap = (uint16_t *)mem_malloc(num_ticks * 2);
1887 ASSERT(pm->submodel[i].tick_pos_remap);
1888 } else {
1889 pm->submodel[i].tick_pos_remap = nullptr;
1890 }
1891
1892 } else
1893 pm->submodel[i].num_key_pos = nframes;
1894
1895 pm->submodel[i].keyframe_pos = (vector *)mem_malloc((pm->submodel[i].num_key_pos + 1) * sizeof(vector));
1896 ASSERT(pm->submodel[i].keyframe_pos != nullptr);
1897
1898 if (timed) {
1899 pm->submodel[i].pos_start_time = (int *)mem_malloc((pm->submodel[i].num_key_pos + 1) * sizeof(int));
1900 ASSERT(pm->submodel[i].pos_start_time != nullptr);
1901 }
1902
1903 for (t = 0; t < pm->submodel[i].num_key_pos; t++) {
1904 if (timed)
1905 pm->submodel[i].pos_start_time[t] = cf_ReadInt(infile);
1906
1907 ReadModelVector(&pm->submodel[i].keyframe_pos[t + 0], infile);
1908 }
1909 }
1910
1911 break;
1912 }
1913 default:
1914 // mprintf(0,"Unknown chunk <%c%c%c%c>, len = %d\n",id,id>>8,id>>16,id>>24,len);
1915 cfseek(infile, len, SEEK_CUR);
1916 break;
1917 }
1918 }
1919
1920 // Set the greater of keyframe of positions or keyframe angles to to be max keyframes
1921 pm->max_keys = std::max(pm->num_key_pos, pm->num_key_angles);
1922
1923 // Build animation keyframe matrices
1924
1925 matrix base_matrix, dest_matrix, temp_matrix;
1926 int cur_angle;
1927 int parent;
1928
1929 for (i = 0; i < pm->n_models; i++) {
1930 vm_MakeIdentity(&base_matrix);
1931
1932 bsp_info *sm = &pm->submodel[i];
1933
1934 for (int newt = 0; newt < sm->num_key_angles; newt++) {
1935 cur_angle = sm->keyframe_angles[newt];
1936 BuildModelAngleMatrix(&temp_matrix, cur_angle, &sm->keyframe_axis[newt]);
1937 vm_MakeIdentity(&dest_matrix);
1938 dest_matrix = temp_matrix * base_matrix;
1939 base_matrix = dest_matrix;
1940 sm->keyframe_matrix[newt] = base_matrix;
1941 }
1942
1943 // Build children
1944 parent = pm->submodel[i].parent;
1945
1946 if (parent == i) {
1947 pm->submodel[i].parent = -1;
1948
1949 } else if (parent != -1) {
1950 pm->submodel[parent].children[pm->submodel[parent].num_children] = i;
1951 pm->submodel[parent].num_children++;
1952 }
1953
1954 if (pm->submodel[i].num_key_angles == 0 && (pm->submodel[i].flags & SOF_ROTATE)) {
1955 mprintf(0, "You have a rotator that has no keyframe on model %s.\n", pm->name);
1956 pm->submodel[i].flags &= ~SOF_ROTATE;
1957 } else if (pm->submodel[i].num_key_angles == 0 && (pm->submodel[i].flags & SOF_TURRET)) {
1958 mprintf(0, "You have a turret that has no keyframe on model %s.\n", pm->name);
1959 pm->submodel[i].flags &= ~SOF_TURRET;
1960 }
1961
1962 // Figure out the size of this facing subobject
1963 if (pm->submodel[i].flags & SOF_FACING) {
1964 ASSERT(pm->submodel[i].num_faces == 1); // This facing has more than one face
1965 vector vecs[30];
1966 vector avg;
1967
1968 for (int newt = 0; newt < pm->submodel[i].faces[0].nverts; newt++) {
1969 vecs[newt] = pm->submodel[i].verts[pm->submodel[i].faces[0].vertnums[newt]];
1970 }
1971
1972 pm->submodel[i].rad = (sqrt(vm_GetCentroid(&avg, vecs, pm->submodel[i].faces[0].nverts)) / 2);
1973
1974 pm->flags |= PMF_FACING;
1975 }
1976
1977 if (pm->submodel[i].flags & (SOF_GLOW | SOF_THRUSTER)) {
1978 ASSERT(pm->submodel[i].num_faces == 1); // This glow has more than one face
1979 vector vecs[30];
1980
1981 for (t = 0; t < pm->submodel[i].faces[0].nverts; t++)
1982 vecs[t] = pm->submodel[i].verts[pm->submodel[i].faces[0].vertnums[t]];
1983
1984 vm_GetNormal(&pm->submodel[i].glow_info->normal, &vecs[0], &vecs[1], &vecs[2]);
1985
1986 pm->flags |= PMF_FACING; // Set this so we know when to draw
1987 }
1988
1989 // Now build the tick/keyframe remap list
1990 int num_ticks = (pm->submodel[i].pos_track_max - pm->submodel[i].pos_track_min);
1991 for (t = 0; timed && t < num_ticks; t++) {
1992 int done = 0;
1993 int current_tick = sm->pos_track_min + t;
1994
1995 if (sm->num_key_pos > 1) {
1996 for (int k = sm->num_key_pos - 1; k >= 0 && !done; k--) {
1997 if (current_tick >= sm->pos_start_time[k]) {
1998 sm->tick_pos_remap[t] = k;
1999 done = 1;
2000 }
2001 }
2002
2003 if (done == 0) {
2004 mprintf(0, "Couldn't get a good keyframe!\n");
2005 Int3();
2006 }
2007 }
2008 }
2009
2010 // Now build the tick/keyframe remap list
2011 num_ticks = (pm->submodel[i].rot_track_max - pm->submodel[i].rot_track_min);
2012 for (t = 0; timed && t < num_ticks; t++) {
2013 if (sm->num_key_angles > 1) {
2014 int done = 0;
2015 int current_tick = sm->rot_track_min + t;
2016 for (int k = sm->num_key_angles - 1; k >= 0 && !done; k--) {
2017 if (current_tick >= sm->rot_start_time[k]) {
2018 sm->tick_ang_remap[t] = k;
2019 done = 1;
2020 }
2021 }
2022
2023 if (done == 0) {
2024 mprintf(0, "Couldn't get a good keyframe!\n");
2025 Int3();
2026 }
2027 }
2028 }
2029 }
2030
2031 pm->flags &= ~PMF_NOT_RESIDENT; // mark it as in memory
2032
2033 // Find Min/Max of whole model
2034 FindMinMaxForModel(pm);
2035
2036 // adjust positional interpolation frames
2037
2038
2039 FindWBSubobjects(pm);
2040
2041 // Set as newstyle
2042 pm->new_style = 1;
2043
2044 if (pm->n_models > MAX_SUBOBJECTS) {
2045 mprintf(0, "This model has more than the max number of subobjects! (%d)\n", MAX_SUBOBJECTS);
2046 Int3();
2047 FreePolyModel(pm - Poly_models);
2048 return 0;
2049 }
2050
2051 return 1;
2052 }
2053
2054 // given a filename, reads in a POF and returns an index into the Poly_models array
2055 // returns -1 if something is wrong
2056 int LoadPolyModel(const std::filesystem::path &filename, int pageable) {
2057 int i, polynum = -1;
2058 CFILE *infile = nullptr;
2059 int overlay = 0;
2060
2061 ASSERT(Num_poly_models >= 0);
2062 ASSERT(Num_poly_models < MAX_POLY_MODELS);
2063
2064 std::filesystem::path name = ChangePolyModelName(filename);
2065
2066 // If this polymodel is already in memory, just use that index
2067 i = FindPolyModelName(name);
2068 if (i != -1) {
2069 #ifdef RELEASE
2070 Poly_models[i].used++;
2071 return i;
2072 #endif
2073
2074 int old_used = Poly_models[i].used;
2075 int not_res = 0;
2076
2077 if (Poly_models[i].flags & PMF_NOT_RESIDENT)
2078 not_res = 1;
2079
2080 mprintf(1, "Model '%s' usage count is now %d.\n", Poly_models[i].name, Poly_models[i].used + 1);
2081
2082 Poly_models[i].used = 1;
2083 FreePolyModel(i);
2084
2085 memset(&Poly_models[i], 0, sizeof(poly_model));
2086 WBClearInfo(&Poly_models[i]);
2087 Poly_models[i].used = old_used + 1;
2088 if (not_res)
2089 Poly_models[i].flags = PMF_NOT_RESIDENT;
2090
2091 overlay = 1;
2092 polynum = i;
2093 }
2094
2095 // Not in memory, so we must load it
2096
2097 if (!pageable) {
2098 infile = cfopen(filename, "rb");
2099 if (!infile)
2100 return -1;
2101 }
2102
2103 if (!overlay)
2104 polynum = AllocPolyModel();
2105 else {
2106 if (!(Poly_models[polynum].flags & PMF_NOT_RESIDENT)) {
2107 if (pageable) {
2108 infile = cfopen(filename, "rb");
2109 if (!infile)
2110 return -1;
2111 }
2112 pageable = 0;
2113 }
2114 }
2115
2116 ASSERT(polynum >= 0);
2117
2118 // Used for progress bar when loading the level
2119 if (infile) {
2120 paged_in_count += cfilelength(infile);
2121 paged_in_num++;
2122 }
2123
2124 // if this is an oof instead of a pof, flag it as such
2125 if (!stricmp(".oof", filename.extension().u8string().c_str())) {
2126 Poly_models[polynum].new_style = 1;
2127 } else
2128 Poly_models[polynum].new_style = 0;
2129
2130 strcpy(Poly_models[polynum].name, name.u8string().c_str());
2131
2132 int ret = 0;
2133 if (!pageable)
2134 ret = ReadNewModelFile(polynum, infile);
2135 else
2136 ret = 1;
2137
2138 if (infile)
2139 cfclose(infile);
2140
2141 Poly_models[polynum].id = polynum;
2142
2143 if (ret)
2144 return polynum; // loaded successfully
2145
2146 return -1; // damn, didn't load
2147 }
2148
2149 // Pages in a polymodel if its not already in memory
2150 void PageInPolymodel(int polynum, int type, float *size_ptr) {
2151 if (!(Poly_models[polynum].flags & PMF_NOT_RESIDENT)) {
2152 if (!(Poly_models[polynum].flags & PMF_SIZE_COMPUTED))
2153 if (type != -1) {
2154 ComputeDefaultSize(type, polynum, size_ptr);
2155 }
2156 return;
2157 }
2158
2159 mprintf(0, "Paging in polymodel %s.\n", Poly_models[polynum].name);
2160
2161 CFILE *infile;
2162 infile = (CFILE *)cfopen(Poly_models[polynum].name, "rb");
2163
2164 if (!infile) {
2165 // due to a bug in some 3rd party tablefile editors, full paths might
2166 // have been used when they shouldn't have been
2167 char *end_ptr, *start_ptr;
2168 start_ptr = Poly_models[polynum].name;
2169 end_ptr = start_ptr + strlen(start_ptr) - 1;
2170 while ((end_ptr >= start_ptr) && (*end_ptr != '\\'))
2171 end_ptr--;
2172 if (end_ptr < start_ptr) {
2173 Error("Failed to page in %s.", Poly_models[polynum].name);
2174 return;
2175 }
2176
2177 ASSERT(*end_ptr == '\\');
2178 end_ptr++;
2179
2180 infile = (CFILE *)cfopen(end_ptr, "rb");
2181 if (!infile) {
2182 Error("Failed to page in %s.", Poly_models[polynum].name);
2183 return;
2184 }
2185 }
2186
2187 // ASSERT(infile);
2188
2189 int ret = ReadNewModelFile(polynum, infile);
2190
2191 cfclose(infile);
2192 ASSERT(ret > 0);
2193
2194 // See if textures need to be remapped
2195 int remap = 0;
2196 for (int t = 0; t < Poly_models[polynum].n_textures; t++)
2197 if (Poly_models[polynum].textures[t] == 0) // is there a bad texture?
2198 remap = 1;
2199
2200 if (remap == 1) {
2201 // remap the damn textures
2202 mprintf(0, "Remapping model textures for model %s.\n", Poly_models[polynum].name);
2203 ReloadModelTextures(polynum);
2204 }
2205
2206 if (type != -1) {
2207 ComputeDefaultSize(type, polynum, size_ptr);
2208 }
2209 }
2210
2211 // Gets a pointer to a polymodel. Pages it in if neccessary
2212 poly_model *GetPolymodelPointer(int polynum) {
2213 if (Poly_models[polynum].flags & PMF_NOT_RESIDENT)
2214 PageInPolymodel(polynum);
2215
2216 return (&Poly_models[polynum]);
2217 }
2218
2219 // gets the filename from a path
2220 std::filesystem::path ChangePolyModelName(const std::filesystem::path &src) {
2221 // Make sure we don't go over our name length limit
2222 std::string dest = src.stem().string().substr(0, PAGENAME_LEN - 5);
2223 std::filesystem::path filename = std::filesystem::path(dest).replace_extension(src.extension());
2224 return filename;
2225 }
2226
2227 // Searches thru all polymodels for a specific name, returns -1 if not found
2228 // or index of polymodel with name
2229 int FindPolyModelName(const std::filesystem::path &name) {
2230 for (int i = 0; i < MAX_POLY_MODELS; i++) {
2231 if (Poly_models[i].used && !stricmp(Poly_models[i].name, name.u8string().c_str())) {
2232 return i;
2233 }
2234 }
2235
2236 return -1;
2237 }
2238
2239 // Sets a positional instance
2240 void StartPolyModelPosInstance(vector *posvec) {
2241
2242 Interp_pos_instance_vec = *posvec;
2243
2244 Instance_vec_stack[Instance_vec_cnt] = Interp_pos_instance_vec;
2245 Instance_vec_cnt++;
2246 }
2247
2248 // Pops a positional instance
2249 void DonePolyModelPosInstance() {
2250 Instance_vec_cnt--;
2251
2252 if (Instance_vec_cnt == 0)
2253 memset(&Interp_pos_instance_vec, 0, sizeof(vector));
2254 else
2255 Interp_pos_instance_vec = Instance_vec_stack[Instance_vec_cnt];
2256 }
2257
2258 void SetNormalizedTimeObjTimed(object *obj, float *normalized_time) {
2259 int i, j;
2260 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
2261 object_info *obj_info = &Object_info[obj->id];
2262
2263 if (obj->type == OBJ_PLAYER || obj->type == OBJ_WEAPON)
2264 return;
2265
2266 // Setup all the subobjects for the keyframe
2267 float frame = obj->rtype.pobj_info.anim_frame;
2268
2269 for (i = 0; i < pm->n_models; i++) {
2270 bsp_info *sm = &pm->submodel[i];
2271 int x = (sm->flags & SOF_WB_MASKS) >> WB_INDEX_SHIFT;
2272
2273 if (!(sm->flags & SOF_TURRET) &&
2274 !((sm->flags & SOF_WB) && ((obj_info->static_wb[x].flags & WBF_ANIM_MASKS) == WBF_ANIM_LOCAL))) {
2275 if (frame <= sm->rot_track_min)
2276 normalized_time[i] = 0.0;
2277 else if (frame >= sm->rot_track_max)
2278 normalized_time[i] = 1.0;
2279 else {
2280 float total_time = sm->rot_track_max - sm->rot_track_min;
2281 normalized_time[i] = (frame - sm->rot_track_min) / total_time;
2282 }
2283 } else if ((sm->flags & SOF_WB) && !(sm->flags & SOF_TURRET)) {
2284 static float w_frame;
2285 ASSERT(x >= 0 && x < MAX_WBS_PER_OBJ);
2286
2287 if (obj->dynamic_wb)
2288 w_frame = obj->dynamic_wb[x].wb_anim_frame;
2289 else
2290 w_frame = 0;
2291
2292 if (w_frame <= sm->rot_track_min)
2293 normalized_time[i] = 0.0;
2294 else if (w_frame >= sm->rot_track_max)
2295 normalized_time[i] = 1.0;
2296 else {
2297 float total_time = sm->rot_track_max - sm->rot_track_min;
2298 normalized_time[i] = (w_frame - sm->rot_track_min) / total_time;
2299 }
2300 }
2301 }
2302
2303 // Now, override angles of weapon bank turrets
2304 if (obj->dynamic_wb) {
2305 for (i = 0; i < pm->num_wbs; i++) {
2306 for (j = 0; j < pm->poly_wb[i].num_turrets; j++) {
2307 int sobj_index;
2308
2309 sobj_index = pm->poly_wb[i].turret_index[j];
2310 normalized_time[sobj_index] = obj->dynamic_wb[i].norm_turret_angle[j];
2311 }
2312 }
2313 } else {
2314 for (i = 0; i < pm->num_wbs; i++) {
2315 for (j = 0; j < pm->poly_wb[i].num_turrets; j++) {
2316 int sobj_index;
2317
2318 sobj_index = pm->poly_wb[i].turret_index[j];
2319 normalized_time[sobj_index] = 0;
2320 }
2321 }
2322 }
2323 }
2324
2325 void SetNormalizedTimeObj(object *obj, float *normalized_time) {
2326 int i, j;
2327
2328 if (Poly_models[obj->rtype.pobj_info.model_num].flags & PMF_TIMED) {
2329 SetNormalizedTimeObjTimed(obj, normalized_time);
2330 return;
2331 }
2332
2333 float norm_anim_frame = GetNormalizedKeyframe(obj->rtype.pobj_info.model_num, obj->rtype.pobj_info.anim_frame);
2334
2335 // Setup all the subobjects for the keyframe
2336 for (i = 0; i < Poly_models[obj->rtype.pobj_info.model_num].n_models; i++)
2337 normalized_time[i] = norm_anim_frame;
2338
2339 // Currently, we are not handling player weapons in this manner
2340 // chrishack -- weapons with turrets -- COOL.
2341 if (obj->type == OBJ_PLAYER || obj->type == OBJ_WEAPON)
2342 return;
2343
2344 ASSERT(obj->type == OBJ_POWERUP || obj->type == OBJ_ROBOT || obj->type == OBJ_BUILDING || obj->type == OBJ_DEBRIS ||
2345 obj->type == OBJ_DOOR || obj->type == OBJ_CLUTTER);
2346
2347 // Now, override angles of weapon bank turrets
2348 for (i = 0; i < Poly_models[obj->rtype.pobj_info.model_num].num_wbs; i++) {
2349 for (j = 0; j < Poly_models[obj->rtype.pobj_info.model_num].poly_wb[i].num_turrets; j++) {
2350 int sobj_index;
2351
2352 sobj_index = Poly_models[obj->rtype.pobj_info.model_num].poly_wb[i].turret_index[j];
2353 normalized_time[sobj_index] = obj->dynamic_wb[i].norm_turret_angle[j];
2354 }
2355 }
2356 }
2357
2358 void SetNormalizedTimeAnimTimed(float frame, float *normalized_time, poly_model *pm) {
2359 int i, j;
2360
2361 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
2362
2363 for (i = 0; i < pm->n_models; i++) {
2364 bsp_info *sm = &pm->submodel[i];
2365
2366 if (frame <= sm->rot_track_min)
2367 normalized_time[i] = 0.0;
2368 else if (frame >= sm->rot_track_max)
2369 normalized_time[i] = 1.0;
2370 else {
2371 float total_time = sm->rot_track_max - sm->rot_track_min;
2372 normalized_time[i] = (frame - sm->rot_track_min) / total_time;
2373 }
2374 }
2375
2376 // Now, override angles of weapon bank turrets
2377 for (i = 0; i < pm->num_wbs; i++) {
2378 for (j = 0; j < pm->poly_wb[i].num_turrets; j++) {
2379 int sobj_index;
2380
2381 sobj_index = pm->poly_wb[i].turret_index[j];
2382 normalized_time[sobj_index] = 0.0;
2383 }
2384 }
2385 }
2386
2387 // This is for non-rotated turrets or rotators
2388 void SetNormalizedTimeAnim(float anim_frame, float *normalized_time, poly_model *pm) {
2389 int i, j;
2390
2391 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
2392
2393 float norm_anim_frame = anim_frame / pm->max_keys;
2394
2395 if (pm->flags & PMF_TIMED) {
2396 SetNormalizedTimeAnimTimed(anim_frame, normalized_time, pm);
2397 return;
2398 }
2399
2400 // Setup all the subobjects for the keyframe
2401 for (i = 0; i < pm->n_models; i++)
2402 normalized_time[i] = norm_anim_frame;
2403
2404 // Now, override angles of weapon bank turrets
2405 for (i = 0; i < pm->num_wbs; i++) {
2406 for (j = 0; j < pm->poly_wb[i].num_turrets; j++) {
2407 int sobj_index;
2408
2409 sobj_index = pm->poly_wb[i].turret_index[j];
2410 normalized_time[sobj_index] = 0.0;
2411 }
2412 }
2413 }
2414
2415 // Given a model pointer and an array of floats that go from 0..1, calculate the angles of each
2416 // corresponding subobject
2417 void SetModelAngles(poly_model *po, const float *normalized_angles) {
2418 int i;
2419
2420 ASSERT(!(po->flags & PMF_NOT_RESIDENT));
2421
2422 if (po->num_key_angles > 0 && normalized_angles) {
2423 // get time per keyframe state
2424 float state_time = 1.0 / (po->num_key_angles - 1);
2425 float cur_state_time;
2426 int cur_state;
2427 float normal_state_time;
2428
2429 for (i = 0; i < po->n_models; i++) {
2430 // Don't rotate turrets or auto-rotators or weapon battery submodels
2431 if (!(po->submodel[i].flags & (SOF_ROTATE | SOF_TURRET))) {
2432
2433 // Find out which keyframe we're at
2434
2435 cur_state = normalized_angles[i] / state_time;
2436 matrix dest_matrix;
2437
2438 // Find out how far into that keyframe we are
2439 cur_state_time = normalized_angles[i] - ((float)cur_state * state_time);
2440 normal_state_time = cur_state_time / state_time;
2441
2442 // Now do a parametric adjustment on the angles
2443
2444 int cur_angle = 0;
2445
2446 // If we're already at the high point of the interpolation then just
2447 // stuff some values
2448 if (cur_state == po->num_key_angles - 1) {
2449 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &po->submodel[i].keyframe_matrix[po->num_key_angles - 1]);
2450 continue;
2451 }
2452
2453 dest_matrix = po->submodel[i].keyframe_matrix[cur_state] +
2454 ((po->submodel[i].keyframe_matrix[cur_state + 1] - po->submodel[i].keyframe_matrix[cur_state]) *
2455 normal_state_time);
2456
2457 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &dest_matrix);
2458 // po->submodel[i].mod_matrix=dest_matrix;
2459
2460 } else if (po->submodel[i].flags & SOF_ROTATE) {
2461 float flrot = Gametime * po->submodel[i].rps;
2462 int introt = flrot;
2463
2464 float fdiff = flrot - introt;
2465
2466 ASSERT(fdiff >= 0 && fdiff <= 1);
2467
2468 // fdiff now equals 0 to 1
2469 matrix temp_matrix;
2470 vector temp_vec;
2471 if (po->new_style)
2472 temp_vec = po->submodel[i].keyframe_axis[1];
2473 else
2474 temp_vec = po->submodel[i].norm;
2475
2476 BuildModelAngleMatrix(&temp_matrix, fdiff * 65535, &temp_vec);
2477 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &temp_matrix);
2478 } else if (po->submodel[i].flags & SOF_TURRET) {
2479 matrix temp_matrix;
2480 if (po->new_style)
2481 BuildModelAngleMatrix(&temp_matrix, normalized_angles[i] * 65535, &po->submodel[i].keyframe_axis[1]);
2482 else
2483 BuildModelAngleMatrix(&temp_matrix, normalized_angles[i] * 65535, &po->submodel[i].norm);
2484
2485 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &temp_matrix);
2486 }
2487 }
2488 } else {
2489 for (i = 0; i < po->n_models; i++) {
2490 if (po->submodel[i].flags & SOF_ROTATE) {
2491 float flrot = Gametime * po->submodel[i].rps;
2492 int introt = flrot;
2493
2494 float fdiff = flrot - introt;
2495
2496 ASSERT(fdiff >= 0 && fdiff <= 1);
2497
2498 // fdiff now equals 0 to 1
2499 matrix temp_matrix;
2500 vector temp_vec;
2501 if (po->new_style)
2502 temp_vec = po->submodel[i].keyframe_axis[1];
2503 else
2504 temp_vec = po->submodel[i].norm;
2505
2506 BuildModelAngleMatrix(&temp_matrix, fdiff * 65535, &temp_vec);
2507 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &temp_matrix);
2508 } else if (po->submodel[i].flags & SOF_TURRET) {
2509 // We need to find where in this rotation is relative to gametime
2510 float flrot = Gametime;
2511 int introt = flrot;
2512
2513 float fdiff = flrot - introt;
2514
2515 // fdiff now equals 0 to 1
2516 matrix temp_matrix;
2517 if (po->new_style)
2518 BuildModelAngleMatrix(&temp_matrix, fdiff * 65535, &po->submodel[i].keyframe_axis[0]);
2519 else
2520 BuildModelAngleMatrix(&temp_matrix, fdiff * 65535, &po->submodel[i].norm);
2521
2522 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &temp_matrix);
2523 } else {
2524 po->submodel[i].angs.h = 0;
2525 po->submodel[i].angs.p = 0;
2526 po->submodel[i].angs.h = 0;
2527 }
2528 }
2529 }
2530 }
2531
2532 // Given a model pointer and an array of floats that go from 0..1, calculate the interpolated
2533 // position of each corresponding subobject
2534 void SetModelInterpPos(poly_model *po, const float *normalized_pos) {
2535 int i;
2536
2537 ASSERT(!(po->flags & PMF_NOT_RESIDENT));
2538
2539 if (normalized_pos && po->num_key_pos > 0) {
2540 for (i = 0; i < po->n_models; i++) {
2541 bsp_info *sm = &po->submodel[i];
2542
2543 // get time per keyframe state
2544 float state_time = 1.0 / (po->num_key_pos - 1);
2545 float cur_state_time;
2546 int cur_state;
2547 float normal_state_time;
2548
2549 // Find out which keyframe we're at
2550 cur_state = normalized_pos[i] / state_time;
2551
2552 // Find out how far into that keyframe we are
2553 cur_state_time = normalized_pos[i] - ((float)cur_state * state_time);
2554 normal_state_time = cur_state_time / state_time;
2555
2556 // Now do a parametric adjustment on the positions
2557
2558 vector total_delta_pos = {0, 0, 0};
2559 vector subpos;
2560 vector final_pos;
2561
2562 // If we're already at the high point of the interpolation then just
2563 // stuff some values
2564 if (cur_state == sm->num_key_pos - 1) {
2565 po->submodel[i].mod_pos = po->submodel[i].keyframe_pos[po->num_key_pos - 1];
2566 continue;
2567 }
2568
2569 if (cur_state == 0)
2570 po->submodel[i].mod_pos = normal_state_time * po->submodel[i].keyframe_pos[1];
2571 else {
2572 vm_SubVectors(&subpos, &po->submodel[i].keyframe_pos[cur_state + 1], &po->submodel[i].keyframe_pos[cur_state]);
2573 final_pos = po->submodel[i].keyframe_pos[cur_state] + (subpos * normal_state_time);
2574 po->submodel[i].mod_pos = final_pos;
2575 }
2576 }
2577 } else {
2578 for (i = 0; i < po->n_models; i++) {
2579 po->submodel[i].mod_pos.x = 0;
2580 po->submodel[i].mod_pos.y = 0;
2581 po->submodel[i].mod_pos.z = 0;
2582 }
2583 }
2584 }
2585
2586 // Sets the effect used by a polymodel
2587 void SetPolymodelEffect(polymodel_effect *poly_effect) { Polymodel_effect = *poly_effect; }
2588
2589 // Given a model pointer and an array of floats that go from 0..1, calculate the interpolated
2590 // position/angle of each corresponding subobject
2591 void SetModelAnglesAndPosTimed(poly_model *po, float *normalized_time, uint32_t subobj_flags) {
2592 int i;
2593
2594 ASSERT(!(po->flags & PMF_NOT_RESIDENT));
2595
2596 if (normalized_time) {
2597 // get time per keyframe state
2598
2599 for (i = 0; i < po->n_models; i++) {
2600 bsp_info *sm = &po->submodel[i];
2601 int int_tick, current_frame, total_ticks;
2602 float normal_state_time, current_tick;
2603
2604 if (!(subobj_flags & (1 << i)))
2605 continue;
2606
2607 if (sm->num_key_pos <= 1) {
2608 vm_MakeZero(&sm->mod_pos);
2609 goto do_angles;
2610 }
2611
2612 if (normalized_time[i] == 1.0) {
2613 sm->mod_pos = sm->keyframe_pos[sm->num_key_pos - 1];
2614 goto do_angles;
2615 }
2616
2617 // Find out which keyframe we're at
2618 total_ticks = sm->pos_track_max - sm->pos_track_min;
2619 current_tick = (normalized_time[i] * total_ticks) + sm->pos_track_min;
2620 int_tick = current_tick;
2621 current_frame = sm->tick_pos_remap[int_tick - sm->pos_track_min];
2622
2623 ASSERT(current_frame >= 0 && current_frame <= sm->num_key_pos - 1);
2624
2625 if (current_frame == sm->num_key_pos - 1) {
2626 sm->mod_pos = sm->keyframe_pos[sm->num_key_pos - 1];
2627 goto do_angles;
2628 } else {
2629 int ticks_between_frames = sm->pos_start_time[current_frame + 1] - sm->pos_start_time[current_frame];
2630 float this_tick = current_tick - sm->pos_start_time[current_frame];
2631 normal_state_time = (float)this_tick / (float)ticks_between_frames;
2632
2633 vector subpos;
2634 vm_SubVectors(&subpos, &sm->keyframe_pos[current_frame + 1], &sm->keyframe_pos[current_frame]);
2635 sm->mod_pos = sm->keyframe_pos[current_frame] + (subpos * normal_state_time);
2636 }
2637
2638 // Now do angle stuff here
2639 do_angles:
2640 // Don't rotate turrets or auto-rotators
2641 if (!(sm->flags & (SOF_TURRET | SOF_ROTATE))) {
2642 if (sm->num_key_angles <= 1) {
2643 sm->angs.p = 0;
2644 sm->angs.h = 0;
2645 sm->angs.b = 0;
2646 continue;
2647 }
2648
2649 if (normalized_time[i] == 1.0) {
2650 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &po->submodel[i].keyframe_matrix[sm->num_key_angles - 1]);
2651 continue;
2652 }
2653
2654 // Find out which keyframe we're at
2655 total_ticks = sm->rot_track_max - sm->rot_track_min;
2656 current_tick = (normalized_time[i] * total_ticks) + sm->rot_track_min;
2657 int_tick = current_tick;
2658 current_frame = sm->tick_ang_remap[int_tick - sm->rot_track_min];
2659
2660 ASSERT(current_frame >= 0 && current_frame <= sm->num_key_angles - 1);
2661
2662 if (current_frame == sm->num_key_angles - 1) {
2663 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &po->submodel[i].keyframe_matrix[sm->num_key_angles - 1]);
2664 continue;
2665 } else {
2666 int ticks_between_frames = sm->rot_start_time[current_frame + 1] - sm->rot_start_time[current_frame];
2667 float this_tick = current_tick - sm->rot_start_time[current_frame];
2668 normal_state_time = (float)this_tick / (float)ticks_between_frames;
2669
2670 // Now do a parametric adjustment on the angles
2671
2672 matrix dest_matrix =
2673 sm->keyframe_matrix[current_frame] +
2674 ((sm->keyframe_matrix[current_frame + 1] - sm->keyframe_matrix[current_frame]) * normal_state_time);
2675
2676 vm_ExtractAnglesFromMatrix(&sm->angs, &dest_matrix);
2677 }
2678
2679 } else {
2680 // Adjust special subobjects
2681 if (po->submodel[i].flags & SOF_ROTATE) {
2682 float flrot = Gametime * po->submodel[i].rps;
2683 int introt = flrot;
2684
2685 float fdiff = flrot - introt;
2686
2687 ASSERT(fdiff >= 0 && fdiff <= 1);
2688
2689 // fdiff now equals 0 to 1
2690 matrix temp_matrix;
2691 vector temp_vec;
2692 if (po->new_style)
2693 temp_vec = po->submodel[i].keyframe_axis[1];
2694 else
2695 temp_vec = po->submodel[i].norm;
2696
2697 BuildModelAngleMatrix(&temp_matrix, fdiff * 65535, &temp_vec);
2698 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &temp_matrix);
2699 } else if (po->submodel[i].flags & SOF_TURRET) {
2700 matrix temp_matrix;
2701 if (po->new_style)
2702 BuildModelAngleMatrix(&temp_matrix, normalized_time[i] * 65535, &po->submodel[i].keyframe_axis[1]);
2703 else
2704 BuildModelAngleMatrix(&temp_matrix, normalized_time[i] * 65535, &po->submodel[i].norm);
2705
2706 vm_ExtractAnglesFromMatrix(&po->submodel[i].angs, &temp_matrix);
2707 }
2708 }
2709 }
2710
2711 } else {
2712 for (i = 0; i < po->n_models; i++) {
2713 po->submodel[i].mod_pos.x = 0;
2714 po->submodel[i].mod_pos.y = 0;
2715 po->submodel[i].mod_pos.z = 0;
2716
2717 po->submodel[i].angs.p = 0;
2718 po->submodel[i].angs.h = 0;
2719 po->submodel[i].angs.b = 0;
2720 }
2721 }
2722 }
2723
2724 // Sets the position and rotation of a polymodel. Used for rendering and collision detection
2725 void SetModelAnglesAndPos(poly_model *po, float *normalized_time, uint32_t subobj_flags) {
2726 ASSERT(!(po->flags & PMF_NOT_RESIDENT));
2727
2728 if (po->flags & PMF_TIMED) {
2729 SetModelAnglesAndPosTimed(po, normalized_time, subobj_flags);
2730 return;
2731 } else {
2732 SetModelAngles(po, normalized_time);
2733 SetModelInterpPos(po, normalized_time);
2734 }
2735 }
2736
2737 static vector Instance_fog_plane_stack[MAX_SUBOBJECTS];
2738 static vector Instance_fog_portal_vert_stack[MAX_SUBOBJECTS];
2739 static vector Instance_light_stack[MAX_SUBOBJECTS];
2740 static vector Instance_specular_pos[MAX_SUBOBJECTS];
2741 static vector Instance_bump_pos[MAX_SUBOBJECTS];
2742
2743 static int Instance_light_cnt = 0;
2744
2745 void StartLightInstance(vector *pos, matrix *orient) {
2746 int gouraud = 0, specular = 0, fogged = 0, bumped = 0;
2747
2748 if (Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD)
2749 gouraud = 1;
2750 if (Polymodel_use_effect && Polymodel_effect.type & PEF_FOGGED_MODEL)
2751 fogged = 1;
2752 if (Polymodel_use_effect && Polymodel_effect.type & (PEF_SPECULAR_MODEL | PEF_SPECULAR_FACES))
2753 specular = 1;
2754 if (Polymodel_use_effect && (Polymodel_effect.type & PEF_BUMPMAPPED))
2755 bumped = 1;
2756
2757 if (gouraud)
2758 Instance_light_stack[Instance_light_cnt] = *Polymodel_light_direction;
2759 if (fogged) {
2760 Instance_fog_plane_stack[Instance_light_cnt] = Polymodel_fog_plane;
2761 Instance_fog_portal_vert_stack[Instance_light_cnt] = Polymodel_fog_portal_vert;
2762 }
2763
2764 if (specular)
2765 Instance_specular_pos[Instance_light_cnt] = Polymodel_specular_pos;
2766
2767 if (bumped)
2768 Instance_bump_pos[Instance_light_cnt] = Polymodel_bump_pos;
2769
2770 Instance_light_cnt++;
2771
2772 vector temp_vec;
2773
2774 if (gouraud) {
2775 vm_MatrixMulVector(&temp_vec, Polymodel_light_direction, orient);
2776 *Polymodel_light_direction = temp_vec;
2777 }
2778 if (fogged) {
2779 vector tempv = Polymodel_fog_portal_vert - *pos;
2780 vm_MatrixMulVector(&temp_vec, &tempv, orient);
2781 Polymodel_fog_portal_vert = temp_vec;
2782
2783 vm_MatrixMulVector(&temp_vec, &Polymodel_fog_plane, orient);
2784 Polymodel_fog_plane = temp_vec;
2785 }
2786 if (specular) {
2787 vector tempv = Polymodel_specular_pos - *pos;
2788 vm_MatrixMulVector(&temp_vec, &tempv, orient);
2789 Polymodel_specular_pos = temp_vec;
2790 }
2791
2792 if (bumped) {
2793 vector tempv = Polymodel_bump_pos - *pos;
2794 vm_MatrixMulVector(&temp_vec, &tempv, orient);
2795 Polymodel_bump_pos = temp_vec;
2796 }
2797 }
2798 void DoneLightInstance() {
2799 ASSERT(Instance_light_cnt != 0);
2800 Instance_light_cnt--;
2801
2802 if (Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD)
2803 *Polymodel_light_direction = Instance_light_stack[Instance_light_cnt];
2804
2805 if (Polymodel_use_effect && Polymodel_effect.type & PEF_FOGGED_MODEL) {
2806 Polymodel_fog_plane = Instance_fog_plane_stack[Instance_light_cnt];
2807 Polymodel_fog_portal_vert = Instance_fog_portal_vert_stack[Instance_light_cnt];
2808 }
2809
2810 if (Polymodel_use_effect && Polymodel_effect.type & (PEF_SPECULAR_MODEL | PEF_SPECULAR_FACES))
2811 Polymodel_specular_pos = Instance_specular_pos[Instance_light_cnt];
2812
2813 if (Polymodel_use_effect && (Polymodel_effect.type & PEF_BUMPMAPPED))
2814 Polymodel_bump_pos = Instance_bump_pos[Instance_light_cnt];
2815 }
2816
2817 // Draws a polygon model to the viewport
2818 // Normalized_time is an array of floats from 0 to 1 that represent how far into
2819 // an animation state we are
2820
2821 // This is the static light version
2822 void DrawPolygonModel(vector *pos, matrix *orient, int model_num, float *normalized_time, int flags, float r, float g,
2823 float b, uint32_t f_render_sub, uint8_t use_effect, uint8_t overlay) {
2824 poly_model *po;
2825
2826 ASSERT(Poly_models[model_num].used);
2827 ASSERT(!(Poly_models[model_num].flags & PMF_NOT_RESIDENT));
2828
2829 GetPolymodelPointer(model_num);
2830
2831 Polymodel_use_effect = use_effect;
2832 Polymodel_light_type = POLYMODEL_LIGHTING_STATIC;
2833 Polylighting_static_red = r;
2834 Polylighting_static_green = g;
2835 Polylighting_static_blue = b;
2836
2837 rend_SetOverlayType(OT_NONE);
2838 rend_SetLighting(LS_NONE);
2839
2840 po = &Poly_models[model_num];
2841
2842 g3_StartInstanceMatrix(pos, orient);
2843
2844 if (Polymodel_use_effect && Polymodel_effect.type & PEF_FOGGED_MODEL) {
2845 Polymodel_fog_plane = Polymodel_effect.fog_plane;
2846 Polymodel_fog_portal_vert = Polymodel_effect.fog_portal_vert;
2847 }
2848
2849 if (Polymodel_use_effect && Polymodel_effect.type & (PEF_SPECULAR_MODEL | PEF_SPECULAR_FACES))
2850 Polymodel_specular_pos = Polymodel_effect.spec_light_pos;
2851
2852 if (Polymodel_use_effect && (Polymodel_effect.type & PEF_BUMPMAPPED))
2853 Polymodel_bump_pos = Polymodel_effect.bump_light_pos;
2854
2855 StartLightInstance(pos, orient);
2856
2857 SetModelAnglesAndPos(po, normalized_time, f_render_sub);
2858
2859 if (f_render_sub == 0xFFFFFFFF || overlay) // draw entire object
2860 {
2861 if (po->new_style) {
2862 RenderPolygonModel(po, f_render_sub);
2863 }
2864 } else {
2865 ASSERT(po->new_style == 1);
2866
2867 rend_SetAlphaType(ATF_CONSTANT + ATF_VERTEX);
2868
2869 int i;
2870 for (i = 0; i < po->n_models; i++) {
2871 if ((f_render_sub & (1 << i))) {
2872 if (i != 0) {
2873 // if submodel, rotate around its center point, not pivot point
2874
2875 vector ofs = (po->submodel[i].min + po->submodel[i].max) / 2;
2876 vector save_offset = po->submodel[i].offset;
2877 vm_MakeZero(&po->submodel[i].offset);
2878 vm_MakeZero(&po->submodel[i].mod_pos);
2879 memset(&po->submodel[i].angs, 0, sizeof(angvec));
2880 ofs *= -1;
2881
2882 po->submodel[i].offset = ofs;
2883 RenderSubmodel(po, &po->submodel[i], f_render_sub);
2884
2885 po->submodel[i].offset = save_offset;
2886 } else
2887 RenderSubmodel(po, &po->submodel[i], f_render_sub);
2888 }
2889 }
2890 }
2891
2892 DoneLightInstance();
2893 g3_DoneInstance();
2894 }
2895
2896 // This draws a gouraud shaded version
2897 void DrawPolygonModel(vector *pos, matrix *orient, int model_num, float *normalized_time, int flags, vector *lightdir,
2898 float r, float g, float b, uint32_t f_render_sub, uint8_t use_effect, uint8_t overlay) {
2899 poly_model *po;
2900 vector light_vec = *lightdir;
2901
2902 Polymodel_use_effect = use_effect;
2903
2904 ASSERT(Poly_models[model_num].used);
2905 ASSERT(!(Poly_models[model_num].flags & PMF_NOT_RESIDENT));
2906
2907 GetPolymodelPointer(model_num);
2908
2909 rend_SetOverlayType(OT_NONE);
2910 rend_SetLighting(LS_GOURAUD);
2911 rend_SetColorModel(CM_RGB);
2912
2913 Polymodel_light_type = POLYMODEL_LIGHTING_GOURAUD;
2914 Polylighting_static_red = r;
2915 Polylighting_static_green = g;
2916 Polylighting_static_blue = b;
2917
2918 Polymodel_light_direction = &light_vec;
2919
2920 if (Polymodel_use_effect && Polymodel_effect.type & PEF_FOGGED_MODEL) {
2921 Polymodel_fog_plane = Polymodel_effect.fog_plane;
2922 Polymodel_fog_portal_vert = Polymodel_effect.fog_portal_vert;
2923 }
2924
2925 if (Polymodel_use_effect && Polymodel_effect.type & (PEF_SPECULAR_MODEL | PEF_SPECULAR_FACES))
2926 Polymodel_specular_pos = Polymodel_effect.spec_light_pos;
2927
2928 if (Polymodel_use_effect && (Polymodel_effect.type & PEF_BUMPMAPPED))
2929 Polymodel_bump_pos = Polymodel_effect.bump_light_pos;
2930
2931 g3_StartInstanceMatrix(pos, orient);
2932 StartLightInstance(pos, orient);
2933
2934 po = &Poly_models[model_num];
2935 SetModelAnglesAndPos(po, normalized_time, f_render_sub);
2936
2937 if (f_render_sub == 0xFFFFFFFF || overlay) // draw entire object
2938 {
2939 if (po->new_style)
2940 RenderPolygonModel(po, f_render_sub);
2941 } else {
2942 ASSERT(po->new_style == 1);
2943
2944 rend_SetAlphaType(ATF_CONSTANT + ATF_VERTEX);
2945
2946 int i;
2947 for (i = 0; i < po->n_models; i++) {
2948 if ((f_render_sub & (1 << i))) {
2949 if (i != 0) {
2950 // if submodel, rotate around its center point, not pivot point
2951
2952 vector ofs = (po->submodel[i].min + po->submodel[i].max) / 2;
2953 vector save_offset = po->submodel[i].offset;
2954 vm_MakeZero(&po->submodel[i].offset);
2955 vm_MakeZero(&po->submodel[i].mod_pos);
2956 memset(&po->submodel[i].angs, 0, sizeof(angvec));
2957 ofs *= -1;
2958
2959 po->submodel[i].offset = ofs;
2960 RenderSubmodel(po, &po->submodel[i], f_render_sub);
2961
2962 po->submodel[i].offset = save_offset;
2963 } else
2964 RenderSubmodel(po, &po->submodel[i], f_render_sub);
2965 }
2966 }
2967 }
2968
2969 g3_DoneInstance();
2970 DoneLightInstance();
2971 }
2972
2973 // This draws a lightmap shaded version
2974 void DrawPolygonModel(vector *pos, matrix *orient, int model_num, float *normalized_time, int flags,
2975 lightmap_object *lm_object, uint32_t f_render_sub, uint8_t use_effect, uint8_t overlay) {
2976 poly_model *po;
2977
2978 ASSERT(Poly_models[model_num].used);
2979 ASSERT(!(Poly_models[model_num].flags & PMF_NOT_RESIDENT));
2980
2981 GetPolymodelPointer(model_num);
2982
2983 Polymodel_use_effect = use_effect;
2984
2985 rend_SetLighting(LS_NONE);
2986 Polymodel_light_type = POLYMODEL_LIGHTING_LIGHTMAP;
2987 Polylighting_lightmap_object = lm_object;
2988
2989 if (Polymodel_use_effect && Polymodel_effect.type & PEF_FOGGED_MODEL) {
2990 Polymodel_fog_plane = Polymodel_effect.fog_plane;
2991 Polymodel_fog_portal_vert = Polymodel_effect.fog_portal_vert;
2992 }
2993
2994 if (Polymodel_use_effect && Polymodel_effect.type & (PEF_SPECULAR_MODEL | PEF_SPECULAR_FACES))
2995 Polymodel_specular_pos = Polymodel_effect.spec_light_pos;
2996
2997 if (Polymodel_use_effect && (Polymodel_effect.type & PEF_BUMPMAPPED))
2998 Polymodel_bump_pos = Polymodel_effect.bump_light_pos;
2999
3000 g3_StartInstanceMatrix(pos, orient);
3001 StartLightInstance(pos, orient);
3002
3003 po = &Poly_models[model_num];
3004 SetModelAnglesAndPos(po, normalized_time, f_render_sub);
3005
3006 if (f_render_sub == 0xFFFFFFFF || overlay) // draw entire object
3007 {
3008 if (po->new_style)
3009 RenderPolygonModel(po, f_render_sub);
3010 } else {
3011 ASSERT(po->new_style == 1);
3012
3013 rend_SetAlphaType(ATF_CONSTANT + ATF_VERTEX);
3014
3015 int i;
3016 for (i = 0; i < po->n_models; i++) {
3017 if ((f_render_sub & (1 << i))) {
3018 if (i != 0) {
3019 // if submodel, rotate around its center point, not pivot point
3020
3021 vector ofs = (po->submodel[i].min + po->submodel[i].max) / 2;
3022 vector save_offset = po->submodel[i].offset;
3023 vm_MakeZero(&po->submodel[i].offset);
3024 vm_MakeZero(&po->submodel[i].mod_pos);
3025 memset(&po->submodel[i].angs, 0, sizeof(angvec));
3026 ofs *= -1;
3027
3028 po->submodel[i].offset = ofs;
3029 RenderSubmodel(po, &po->submodel[i], f_render_sub);
3030
3031 po->submodel[i].offset = save_offset;
3032 } else
3033 RenderSubmodel(po, &po->submodel[i], f_render_sub);
3034 }
3035 }
3036 }
3037
3038 g3_DoneInstance();
3039 DoneLightInstance();
3040 rend_SetOverlayType(OT_NONE);
3041 }
3042
3043 void FreeAllModels() {
3044 for (int i = 0; i < MAX_POLY_MODELS; i++) {
3045 if (Poly_models[i].used > 0) {
3046 Poly_models[i].used = 1;
3047
3048 FreePolyModel(i);
3049 }
3050 }
3051 }
3052
3053 // Inits our models array and loads our ship pof
3054 int InitModels() {
3055 for (auto &Poly_model : Poly_models) {
3056 memset(&Poly_model, 0, sizeof(poly_model));
3057 Poly_model.used = 0;
3058 }
3059
3060 atexit(FreeAllModels);
3061
3062 return 1;
3063 }
3064
3065 // MTS: used only in this file.
3066 // Given an actual keyframe number, returns the normalized (0 to 1) position of that
3067 // keyframe
3068 // Handle is an index into the Poly_models array
3069 float GetNormalizedKeyframe(int handle, float num) {
3070 poly_model *pm = &Poly_models[handle];
3071
3072 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
3073
3074 ASSERT(pm->used);
3075 ASSERT(num >= 0 && num <= pm->max_keys);
3076
3077 return ((num / (float)pm->max_keys));
3078 }
3079
3080 // Goes through all poly models and gets all missing textures
3081 void RemapPolyModels() {
3082 int i;
3083
3084 for (i = 0; i < MAX_POLY_MODELS; i++) {
3085 int remap = 0;
3086
3087 if (Poly_models[i].used == 0 || (Poly_models[i].flags & PMF_NOT_RESIDENT))
3088 continue;
3089
3090 for (int t = 0; t < Poly_models[i].n_textures; t++)
3091 if (Poly_models[i].textures[t] == 0) // is there a bad texture?
3092 remap = 1;
3093
3094 if (remap == 1) {
3095 // remap the damn textures
3096 mprintf(0, "Remapping model textures for model %s.\n", Poly_models[i].name);
3097 ReloadModelTextures(i);
3098 }
3099 }
3100 }
3101
3102 // MTS: not used?
3103 // Returns the total number of faces in a model
3104 int CountFacesInPolymodel(poly_model *pm) {
3105 int i;
3106 ASSERT(pm->used > 0);
3107 int count = 0;
3108
3109 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
3110
3111 for (i = 0; i < pm->n_models; i++) {
3112 if (IsNonRenderableSubmodel(pm, i))
3113 continue;
3114
3115 count += pm->submodel[i].num_faces;
3116 }
3117
3118 return count;
3119 }
3120
3121 // Given an object, a submodel, and a vertex number, calculates the world position
3122 // of that point
3123 void GetPolyModelPointInWorld(vector *dest, poly_model *pm, vector *wpos, matrix *orient, int subnum, vector *pos,
3124 vector *norm) {
3125 bsp_info *sm = &pm->submodel[subnum];
3126 float normalized_time[MAX_SUBOBJECTS];
3127 int i;
3128
3129 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
3130
3131 if (!pm->new_style)
3132 return;
3133
3134 for (i = 0; i < MAX_SUBOBJECTS; i++)
3135 normalized_time[i] = 0.0;
3136
3137 SetModelAnglesAndPos(pm, normalized_time);
3138
3139 vector pnt = *pos;
3140 int mn = subnum;
3141 vector cur_norm;
3142
3143 if (norm != nullptr)
3144 cur_norm = *norm;
3145
3146 matrix m;
3147
3148 // Instance up the tree for this gun
3149 while (mn != -1) {
3150 vector tpnt;
3151
3152 vm_AnglesToMatrix(&m, pm->submodel[mn].angs.p, pm->submodel[mn].angs.h, pm->submodel[mn].angs.b);
3153 vm_TransposeMatrix(&m);
3154
3155 tpnt = pnt * m;
3156
3157 if (norm != nullptr)
3158 cur_norm = cur_norm * m;
3159
3160 pnt = tpnt + pm->submodel[mn].offset + pm->submodel[mn].mod_pos;
3161
3162 mn = pm->submodel[mn].parent;
3163 }
3164
3165 // now instance for the entire object
3166 m = *orient;
3167 vm_TransposeMatrix(&m);
3168
3169 if (norm != nullptr)
3170 *norm = (cur_norm * m);
3171 *dest = pnt * m;
3172 *dest += (*wpos);
3173 }
3174
3175 void GetPolyModelPointInWorld(vector *dest, poly_model *pm, vector *wpos, matrix *orient, int subnum,
3176 float *normalized_time, vector *pos, vector *norm) {
3177 bsp_info *sm = &pm->submodel[subnum];
3178
3179 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
3180
3181 if (!pm->new_style)
3182 return;
3183
3184 SetModelAnglesAndPos(pm, normalized_time);
3185
3186 vector pnt = *pos;
3187 int mn = subnum;
3188 vector cur_norm;
3189
3190 if (norm != nullptr)
3191 cur_norm = *norm;
3192
3193 matrix m;
3194
3195 // Instance up the tree for this gun
3196 while (mn != -1) {
3197 vector tpnt;
3198
3199 vm_AnglesToMatrix(&m, pm->submodel[mn].angs.p, pm->submodel[mn].angs.h, pm->submodel[mn].angs.b);
3200 vm_TransposeMatrix(&m);
3201
3202 tpnt = pnt * m;
3203
3204 if (norm != nullptr)
3205 cur_norm = cur_norm * m;
3206
3207 pnt = tpnt + pm->submodel[mn].offset + pm->submodel[mn].mod_pos;
3208
3209 mn = pm->submodel[mn].parent;
3210 }
3211
3212 // now instance for the entire object
3213 m = *orient;
3214 vm_TransposeMatrix(&m);
3215
3216 if (norm != nullptr)
3217 *norm = (cur_norm * m);
3218 *dest = pnt * m;
3219 *dest += (*wpos);
3220 }
3221
3222 // Returns 1 if this submodel shouldn't be rendered
3223 int IsNonRenderableSubmodel(poly_model *pm, int submodelnum) {
3224 ASSERT(pm->used);
3225 ASSERT(!(pm->flags & PMF_NOT_RESIDENT));
3226
3227 if ((pm->submodel[submodelnum].flags & SOF_FRONTFACE) || (pm->submodel[submodelnum].flags & SOF_SHELL))
3228 return 1;
3229
3230 if (pm->submodel[submodelnum].num_faces == 0)
3231 return 1;
3232
3233 return 0;
3234 }