]> icculus.org git repositories - btb/d2x.git/blob - main/polyobj.c
when no movies are found, divide by six the number of warning lines sent to stdout
[btb/d2x.git] / main / polyobj.c
1 /* $Id: polyobj.c,v 1.16 2003-10-10 09:36:35 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Hacked-in polygon objects
18  *
19  * Old Log:
20  * Revision 1.3  1995/10/25  14:07:07  allender
21  * removed load_poly_model function
22  *
23  * Revision 1.2  1995/09/14  14:10:20  allender
24  * two funtions should be void
25  *
26  * Revision 1.1  1995/05/16  15:30:08  allender
27  * Initial revision
28  *
29  * Revision 2.1  1995/05/26  16:10:37  john
30  * Support for new 4-byte align v8 pof files.
31  *
32  * Revision 2.0  1995/02/27  11:32:44  john
33  * New version 2.0, which has no anonymous unions, builds with
34  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
35  *
36  * Revision 1.64  1995/01/14  19:16:43  john
37  * First version of new bitmap paging code.
38  *
39  * Revision 1.63  1994/12/14  18:06:54  matt
40  * Removed compile warnings
41  *
42  * Revision 1.62  1994/12/09  17:54:31  john
43  * Made the CFILE's close right after reading in data.
44  *
45  * Revision 1.61  1994/12/09  16:13:28  mike
46  * speedup pof file reading, but still horribly slow using hog file...problem somewhere else.
47  *
48  * Revision 1.60  1994/12/08  17:41:20  yuan
49  * Cfiling stuff.
50  *
51  * Revision 1.59  1994/11/21  11:02:19  matt
52  * Added error checking
53  *
54  * Revision 1.58  1994/11/14  11:32:49  matt
55  * Allow switching to simpler models even when alt_textures specified
56  *
57  * Revision 1.57  1994/11/13  21:15:24  matt
58  * Added basic support for more than one level of detail simplification
59  *
60  * Revision 1.56  1994/11/11  19:29:25  matt
61  * Added code to show low detail polygon models
62  *
63  * Revision 1.55  1994/11/10  14:02:57  matt
64  * Hacked in support for player ships with different textures
65  *
66  * Revision 1.54  1994/11/03  11:01:59  matt
67  * Made robot pics lighted
68  *
69  * Revision 1.53  1994/11/02  16:18:34  matt
70  * Moved draw_model_picture() out of editor
71  *
72  * Revision 1.52  1994/10/18  14:38:11  matt
73  * Restored assert now that bug is fixed
74  *
75  * Revision 1.51  1994/10/17  21:35:03  matt
76  * Added support for new Control Center/Main Reactor
77  *
78  * Revision 1.50  1994/10/14  17:46:23  yuan
79  * Made the soft Int3 only work in net mode.
80  *
81  * Revision 1.49  1994/10/14  17:43:47  yuan
82  * Added soft int3's instead of Asserts  for some common network bugs.
83  *
84  * Revision 1.48  1994/10/14  17:09:04  yuan
85  * Made Assert on line 610 be if in an attempt
86  * to bypass.
87  *
88  * Revision 1.47  1994/09/09  14:23:42  matt
89  * Added glow code to polygon models for engine glow
90  *
91  * Revision 1.46  1994/08/26  18:03:30  matt
92  * Added code to remap polygon model numbers by matching filenames
93  *
94  * Revision 1.45  1994/08/26  15:35:58  matt
95  * Made eclips usable on more than one object at a time
96  *
97  * Revision 1.44  1994/08/25  18:11:58  matt
98  * Made player's weapons and flares fire from the positions on the 3d model.
99  * Also added support for quad lasers.
100  *
101  * Revision 1.43  1994/07/25  00:14:18  matt
102  * Made a couple of minor changes for the drivethrough
103  *
104  * Revision 1.42  1994/07/25  00:02:41  matt
105  * Various changes to accomodate new 3d, which no longer takes point numbers
106  * as parms, and now only takes pointers to points.
107  *
108  */
109
110
111 #ifdef HAVE_CONFIG_H
112 #include <conf.h>
113 #endif
114
115 #ifdef RCS
116 static char rcsid[] = "$Id: polyobj.c,v 1.16 2003-10-10 09:36:35 btb Exp $";
117 #endif
118
119 #include <stdio.h>
120 #include <stdlib.h>
121 #include <string.h>
122
123 // -- I hate this warning in make depend! -- #ifdef DRIVE
124 // -- I hate this warning in make depend! -- #include "drive.h"
125 // -- I hate this warning in make depend! -- #else
126 #include "inferno.h"
127 // -- I hate this warning in make depend! -- #endif
128
129 #include "polyobj.h"
130
131 #include "vecmat.h"
132 #include "interp.h"
133 #include "error.h"
134 #include "mono.h"
135 #include "u_mem.h"
136 #include "args.h"
137 #include "byteswap.h"
138
139 #ifndef DRIVE
140 #include "texmap.h"
141 #include "bm.h"
142 #include "textures.h"
143 #include "object.h"
144 #include "lighting.h"
145 #include "cfile.h"
146 #include "piggy.h"
147 #endif
148
149 #include "pa_enabl.h"
150
151 #ifdef _3DFX
152 #include "3dfx_des.h"
153 #endif
154
155 polymodel Polygon_models[MAX_POLYGON_MODELS];   // = {&bot11,&bot17,&robot_s2,&robot_b2,&bot11,&bot17,&robot_s2,&robot_b2};
156
157 int N_polygon_models = 0;
158
159 #define MAX_POLYGON_VECS 1000
160 g3s_point robot_points[MAX_POLYGON_VECS];
161
162 #define PM_COMPATIBLE_VERSION 6
163 #define PM_OBJFILE_VERSION 8
164
165 int     Pof_file_end;
166 int     Pof_addr;
167
168 #define MODEL_BUF_SIZE  32768
169
170 void _pof_cfseek(int len,int type)
171 {
172         switch (type) {
173                 case SEEK_SET:  Pof_addr = len; break;
174                 case SEEK_CUR:  Pof_addr += len;        break;
175                 case SEEK_END:
176                         Assert(len <= 0);       //      seeking from end, better be moving back.
177                         Pof_addr = Pof_file_end + len;
178                         break;
179         }
180
181         if (Pof_addr > MODEL_BUF_SIZE)
182                 Int3();
183 }
184
185 #define pof_cfseek(_buf,_len,_type) _pof_cfseek((_len),(_type))
186
187 int pof_read_int(ubyte *bufp)
188 {
189         int i;
190
191         i = *((int *) &bufp[Pof_addr]);
192         Pof_addr += 4;
193         return INTEL_INT(i);
194
195 //      if (cfread(&i,sizeof(i),1,f) != 1)
196 //              Error("Unexpected end-of-file while reading object");
197 //
198 //      return i;
199 }
200
201 size_t pof_cfread(void *dst, size_t elsize, size_t nelem, ubyte *bufp)
202 {
203         if (Pof_addr + nelem*elsize > Pof_file_end)
204                 return 0;
205
206         memcpy(dst, &bufp[Pof_addr], elsize*nelem);
207
208         Pof_addr += elsize*nelem;
209
210         if (Pof_addr > MODEL_BUF_SIZE)
211                 Int3();
212
213         return nelem;
214 }
215
216 // #define new_read_int(i,f) cfread(&(i),sizeof(i),1,(f))
217 #define new_pof_read_int(i,f) pof_cfread(&(i),sizeof(i),1,(f))
218
219 short pof_read_short(ubyte *bufp)
220 {
221         short s;
222
223         s = *((short *) &bufp[Pof_addr]);
224         Pof_addr += 2;
225         return INTEL_SHORT(s);
226 //      if (cfread(&s,sizeof(s),1,f) != 1)
227 //              Error("Unexpected end-of-file while reading object");
228 //
229 //      return s;
230 }
231
232 void pof_read_string(char *buf,int max_char, ubyte *bufp)
233 {
234         int     i;
235
236         for (i=0; i<max_char; i++) {
237                 if ((*buf++ = bufp[Pof_addr++]) == 0)
238                         break;
239         }
240
241 //      while (max_char-- && (*buf=cfgetc(f)) != 0) buf++;
242
243 }
244
245 void pof_read_vecs(vms_vector *vecs,int n,ubyte *bufp)
246 {
247 //      cfread(vecs,sizeof(vms_vector),n,f);
248
249         memcpy(vecs, &bufp[Pof_addr], n*sizeof(*vecs));
250         Pof_addr += n*sizeof(*vecs);
251
252 #ifdef WORDS_BIGENDIAN
253         while (n > 0)
254                 vms_vector_swap(&vecs[--n]);
255 #endif
256
257         if (Pof_addr > MODEL_BUF_SIZE)
258                 Int3();
259 }
260
261 void pof_read_angs(vms_angvec *angs,int n,ubyte *bufp)
262 {
263         memcpy(angs, &bufp[Pof_addr], n*sizeof(*angs));
264         Pof_addr += n*sizeof(*angs);
265
266 #ifdef WORDS_BIGENDIAN
267         while (n > 0)
268                 vms_angvec_swap(&angs[--n]);
269 #endif
270
271         if (Pof_addr > MODEL_BUF_SIZE)
272                 Int3();
273 }
274
275 #define ID_OHDR 0x5244484f // 'RDHO'  //Object header
276 #define ID_SOBJ 0x4a424f53 // 'JBOS'  //Subobject header
277 #define ID_GUNS 0x534e5547 // 'SNUG'  //List of guns on this object
278 #define ID_ANIM 0x4d494e41 // 'MINA'  //Animation data
279 #define ID_IDTA 0x41544449 // 'ATDI'  //Interpreter data
280 #define ID_TXTR 0x52545854 // 'RTXT'  //Texture filename list
281
282 #ifdef DRIVE
283 #define robot_info void
284 #else
285 vms_angvec anim_angs[N_ANIM_STATES][MAX_SUBMODELS];
286
287 //set the animation angles for this robot.  Gun fields of robot info must
288 //be filled in.
289 void robot_set_angles(robot_info *r,polymodel *pm,vms_angvec angs[N_ANIM_STATES][MAX_SUBMODELS]);
290 #endif
291
292 #define DEBUG_LEVEL CON_NORMAL
293
294 #ifdef WORDS_NEED_ALIGNMENT
295 ubyte * old_dest(chunk o) // return where chunk is (in unaligned struct)
296 {
297         return o.old_base + INTEL_SHORT(*((short *)(o.old_base + o.offset)));
298 }
299 ubyte * new_dest(chunk o) // return where chunk is (in aligned struct)
300 {
301         return o.new_base + INTEL_SHORT(*((short *)(o.old_base + o.offset))) + o.correction;
302 }
303 /*
304  * find chunk with smallest address
305  */
306 int get_first_chunks_index(chunk *chunk_list, int no_chunks)
307 {
308         int i, first_index = 0;
309         Assert(no_chunks >= 1);
310         for (i = 1; i < no_chunks; i++)
311                 if (old_dest(chunk_list[i]) < old_dest(chunk_list[first_index]))
312                         first_index = i;
313         return first_index;
314 }
315 #define SHIFT_SPACE 500 // increase if insufficent
316
317 void align_polygon_model_data(polymodel *pm)
318 {
319         int i, chunk_len;
320         int total_correction = 0;
321         ubyte *cur_old, *cur_new;
322         chunk cur_ch;
323         chunk ch_list[MAX_CHUNKS];
324         int no_chunks = 0;
325         int tmp_size = pm->model_data_size + SHIFT_SPACE;
326         ubyte *tmp = d_malloc(tmp_size); // where we build the aligned version of pm->model_data
327
328         Assert(tmp != NULL);
329         //start with first chunk (is always aligned!)
330         cur_old = pm->model_data;
331         cur_new = tmp;
332         chunk_len = get_chunks(cur_old, cur_new, ch_list, &no_chunks);
333         memcpy(cur_new, cur_old, chunk_len);
334         while (no_chunks > 0) {
335                 int first_index = get_first_chunks_index(ch_list, no_chunks);
336                 cur_ch = ch_list[first_index];
337                 // remove first chunk from array:
338                 no_chunks--;
339                 for (i = first_index; i < no_chunks; i++)
340                         ch_list[i] = ch_list[i + 1];
341                 // if (new) address unaligned:
342                 if ((u_int32_t)new_dest(cur_ch) % 4L != 0) {
343                         // calculate how much to move to be aligned
344                         short to_shift = 4 - (u_int32_t)new_dest(cur_ch) % 4L;
345                         // correct chunks' addresses
346                         cur_ch.correction += to_shift;
347                         for (i = 0; i < no_chunks; i++)
348                                 ch_list[i].correction += to_shift;
349                         total_correction += to_shift;
350                         Assert((u_int32_t)new_dest(cur_ch) % 4L == 0);
351                         Assert(total_correction <= SHIFT_SPACE); // if you get this, increase SHIFT_SPACE
352                 }
353                 //write (corrected) chunk for current chunk:
354                 *((short *)(cur_ch.new_base + cur_ch.offset))
355                   = INTEL_SHORT(cur_ch.correction
356                                 + INTEL_SHORT(*((short *)(cur_ch.old_base + cur_ch.offset))));
357                 //write (correctly aligned) chunk:
358                 cur_old = old_dest(cur_ch);
359                 cur_new = new_dest(cur_ch);
360                 chunk_len = get_chunks(cur_old, cur_new, ch_list, &no_chunks);
361                 memcpy(cur_new, cur_old, chunk_len);
362                 //correct submodel_ptr's for pm, too
363                 for (i = 0; i < MAX_SUBMODELS; i++)
364                         if (pm->model_data + pm->submodel_ptrs[i] >= cur_old
365                             && pm->model_data + pm->submodel_ptrs[i] < cur_old + chunk_len)
366                                 pm->submodel_ptrs[i] += (cur_new - tmp) - (cur_old - pm->model_data);
367         }
368         d_free(pm->model_data);
369         pm->model_data_size += total_correction;
370         pm->model_data = d_malloc(pm->model_data_size);
371         Assert(pm->model_data != NULL);
372         memcpy(pm->model_data, tmp, pm->model_data_size);
373         d_free(tmp);
374 }
375 #endif //def WORDS_NEED_ALIGNMENT
376
377 //reads a binary file containing a 3d model
378 polymodel *read_model_file(polymodel *pm,char *filename,robot_info *r)
379 {
380         CFILE *ifile;
381         short version;
382         int id,len, next_chunk;
383         int anim_flag = 0;
384         ubyte *model_buf;
385
386         model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
387         if (!model_buf)
388                 Error("Can't allocate space to read model %s\n", filename);
389
390         if ((ifile=cfopen(filename,"rb"))==NULL)
391                 Error("Can't open file <%s>",filename);
392
393         Assert(cfilelength(ifile) <= MODEL_BUF_SIZE);
394
395         Pof_addr = 0;
396         Pof_file_end = cfread(model_buf, 1, cfilelength(ifile), ifile);
397         cfclose(ifile);
398
399         id = pof_read_int(model_buf);
400
401         if (id!=0x4f505350) /* 'OPSP' */
402                 Error("Bad ID in model file <%s>",filename);
403
404         version = pof_read_short(model_buf);
405         
406         if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
407                 Error("Bad version (%d) in model file <%s>",version,filename);
408
409         if ( FindArg( "-bspgen" ))
410                 printf( "bspgen -c1" );
411
412         while (new_pof_read_int(id,model_buf) == 1) {
413                 id = INTEL_INT(id);
414                 //id  = pof_read_int(model_buf);
415                 len = pof_read_int(model_buf);
416                 next_chunk = Pof_addr + len;
417
418                 switch (id) {
419
420                         case ID_OHDR: {         //Object header
421                                 vms_vector pmmin,pmmax;
422
423                                 //con_printf(DEBUG_LEVEL, "Got chunk OHDR, len=%d\n",len);
424
425                                 pm->n_models = pof_read_int(model_buf);
426                                 pm->rad = pof_read_int(model_buf);
427
428                                 Assert(pm->n_models <= MAX_SUBMODELS);
429
430                                 pof_read_vecs(&pmmin,1,model_buf);
431                                 pof_read_vecs(&pmmax,1,model_buf);
432
433                                 if ( FindArg( "-bspgen" ))      {
434                                         vms_vector v;
435                                         fix l;
436                                 
437                                         vm_vec_sub(&v, &pmmax, &pmmin );
438                                         l = v.x;
439                                         if ( v.y > l ) l = v.y;                                 
440                                         if ( v.z > l ) l = v.z;                                 
441                                                                                                         
442                                         printf( " -l%.3f", f2fl(l) );
443                                 }
444
445                                 break;
446                         }
447                         
448                         case ID_SOBJ: {         //Subobject header
449                                 int n;
450
451                                 anim_flag++;
452
453                                 //con_printf(DEBUG_LEVEL, "Got chunk SOBJ, len=%d\n",len);
454
455                                 n = pof_read_short(model_buf);
456
457                                 Assert(n < MAX_SUBMODELS);
458
459                                 pm->submodel_parents[n] = pof_read_short(model_buf);
460
461                                 pof_read_vecs(&pm->submodel_norms[n],1,model_buf);
462                                 pof_read_vecs(&pm->submodel_pnts[n],1,model_buf);
463                                 pof_read_vecs(&pm->submodel_offsets[n],1,model_buf);
464
465                                 pm->submodel_rads[n] = pof_read_int(model_buf);         //radius
466
467                                 pm->submodel_ptrs[n] = pof_read_int(model_buf); //offset
468
469                                 break;
470
471                         }
472                         
473                         #ifndef DRIVE
474                         case ID_GUNS: {         //List of guns on this object
475
476                                 //con_printf(DEBUG_LEVEL, "Got chunk GUNS, len=%d\n",len);
477
478                                 if (r) {
479                                         int i;
480                                         vms_vector gun_dir;
481                                         ubyte gun_used[MAX_GUNS];
482
483                                         r->n_guns = pof_read_int(model_buf);
484
485                                         if ( r->n_guns )
486                                                 anim_flag++;
487
488                                         Assert(r->n_guns <= MAX_GUNS);
489
490                                         for (i=0;i<r->n_guns;i++)
491                                                 gun_used[i] = 0;
492
493                                         for (i=0;i<r->n_guns;i++) {
494                                                 int id;
495
496                                                 id = pof_read_short(model_buf);
497                                                 Assert(id < r->n_guns);
498                                                 Assert(gun_used[id] == 0);
499                                                 gun_used[id] = 1;
500                                                 r->gun_submodels[id] = pof_read_short(model_buf);
501                                                 Assert(r->gun_submodels[id] != 0xff);
502                                                 pof_read_vecs(&r->gun_points[id],1,model_buf);
503
504                                                 if (version >= 7)
505                                                         pof_read_vecs(&gun_dir,1,model_buf);
506                                         }
507                                 }
508                                 else
509                                         pof_cfseek(model_buf,len,SEEK_CUR);
510
511                                 break;
512                         }
513                         
514                         case ID_ANIM:           //Animation data
515                                 //con_printf(DEBUG_LEVEL, "Got chunk ANIM, len=%d\n",len);
516
517                                 anim_flag++;
518
519                                 if (r) {
520                                         int n_frames,f,m;
521
522                                         n_frames = pof_read_short(model_buf);
523
524                                         Assert(n_frames == N_ANIM_STATES);
525
526                                         for (m=0;m<pm->n_models;m++)
527                                                 for (f=0;f<n_frames;f++)
528                                                         pof_read_angs(&anim_angs[f][m], 1, model_buf);
529
530
531                                         robot_set_angles(r,pm,anim_angs);
532                                 
533                                 }
534                                 else
535                                         pof_cfseek(model_buf,len,SEEK_CUR);
536
537                                 break;
538                         #endif
539                         
540                         case ID_TXTR: {         //Texture filename list
541                                 int n;
542                                 char name_buf[128];
543
544                                 //con_printf(DEBUG_LEVEL, "Got chunk TXTR, len=%d\n",len);
545
546                                 n = pof_read_short(model_buf);
547                                 //con_printf(DEBUG_LEVEL, "  num textures = %d\n",n);
548                                 while (n--) {
549                                         pof_read_string(name_buf,128,model_buf);
550                                         //con_printf(DEBUG_LEVEL, "<%s>\n",name_buf);
551                                 }
552
553                                 break;
554                         }
555                         
556                         case ID_IDTA:           //Interpreter data
557                                 //con_printf(DEBUG_LEVEL, "Got chunk IDTA, len=%d\n",len);
558
559                                 pm->model_data = d_malloc(len);
560                                 pm->model_data_size = len;
561
562                                 pof_cfread(pm->model_data,1,len,model_buf);
563
564                                 break;
565
566                         default:
567                                 //con_printf(DEBUG_LEVEL, "Unknown chunk <%c%c%c%c>, len = %d\n",id,id>>8,id>>16,id>>24,len);
568                                 pof_cfseek(model_buf,len,SEEK_CUR);
569                                 break;
570
571                 }
572                 if ( version >= 8 )             // Version 8 needs 4-byte alignment!!!
573                         pof_cfseek(model_buf,next_chunk,SEEK_SET);
574         }
575
576 //      for (i=0;i<pm->n_models;i++)
577 //              pm->submodel_ptrs[i] += (int) pm->model_data;
578
579         if ( FindArg( "-bspgen" )) {
580                 char *p = strchr( filename, '.' );
581                 *p = 0;
582
583                 if ( anim_flag > 1 )
584                         printf( " -a" );
585
586                 printf( " %s.3ds\n", filename );
587                 *p = '.';
588         }
589         
590         d_free(model_buf);
591
592 #ifdef WORDS_NEED_ALIGNMENT
593         align_polygon_model_data(pm);
594 #endif
595 #ifdef WORDS_BIGENDIAN
596         swap_polygon_model_data(pm->model_data);
597 #endif
598         //verify(pm->model_data);
599
600         return pm;
601 }
602
603 //reads the gun information for a model
604 //fills in arrays gun_points & gun_dirs, returns the number of guns read
605 int read_model_guns(char *filename,vms_vector *gun_points, vms_vector *gun_dirs, int *gun_submodels)
606 {
607         CFILE *ifile;
608         short version;
609         int id,len;
610         int n_guns=0;
611         ubyte   *model_buf;
612
613         model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
614         if (!model_buf)
615                 Error("Can't allocate space to read model %s\n", filename);
616
617         if ((ifile=cfopen(filename,"rb"))==NULL)
618                 Error("Can't open file <%s>",filename);
619
620         Assert(cfilelength(ifile) <= MODEL_BUF_SIZE);
621
622         Pof_addr = 0;
623         Pof_file_end = cfread(model_buf, 1, cfilelength(ifile), ifile);
624         cfclose(ifile);
625
626         id = pof_read_int(model_buf);
627
628         if (id!=0x4f505350) /* 'OPSP' */
629                 Error("Bad ID in model file <%s>",filename);
630
631         version = pof_read_short(model_buf);
632
633         Assert(version >= 7);           //must be 7 or higher for this data
634
635         if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
636                 Error("Bad version (%d) in model file <%s>",version,filename);
637
638         while (new_pof_read_int(id,model_buf) == 1) {
639                 id = INTEL_INT(id);
640                 //id  = pof_read_int(model_buf);
641                 len = pof_read_int(model_buf);
642
643                 if (id == ID_GUNS) {            //List of guns on this object
644
645                         //con_printf(DEBUG_LEVEL, "Got chunk GUNS, len=%d\n",len);
646
647                         int i;
648
649                         n_guns = pof_read_int(model_buf);
650
651                         for (i=0;i<n_guns;i++) {
652                                 int id,sm;
653
654                                 id = pof_read_short(model_buf);
655                                 sm = pof_read_short(model_buf);
656                                 if (gun_submodels)
657                                         gun_submodels[id] = sm;
658                                 else if (sm!=0)
659                                         Error("Invalid gun submodel in file <%s>",filename);
660                                 pof_read_vecs(&gun_points[id],1,model_buf);
661
662                                 pof_read_vecs(&gun_dirs[id],1,model_buf);
663                         }
664
665                 }
666                 else
667                         pof_cfseek(model_buf,len,SEEK_CUR);
668
669         }
670
671         d_free(model_buf);
672         
673         return n_guns;
674 }
675
676 //free up a model, getting rid of all its memory
677 void free_model(polymodel *po)
678 {
679         d_free(po->model_data);
680 }
681
682 grs_bitmap *texture_list[MAX_POLYOBJ_TEXTURES];
683 bitmap_index texture_list_index[MAX_POLYOBJ_TEXTURES];
684
685 int Simple_model_threshhold_scale=5;            //switch when this times radius far away
686
687
688 //draw a polygon model
689
690 void draw_polygon_model(vms_vector *pos,vms_matrix *orient,vms_angvec *anim_angles,int model_num,int flags,fix light,fix *glow_values,bitmap_index alt_textures[])
691 {
692         polymodel *po;
693         int i;
694         PA_DFX (int save_light);
695
696         Assert(model_num < N_polygon_models);
697
698         po=&Polygon_models[model_num];
699
700         //check if should use simple model
701         if (po->simpler_model )                                 //must have a simpler model
702                 if (flags==0)                                                   //can't switch if this is debris
703                         //!!if (!alt_textures) {                                //alternate textures might not match
704                         //alt textures might not match, but in the one case we're using this
705                         //for on 11/14/94, they do match.  So we leave it in.
706                         {
707                                 int cnt=1;
708                                 fix depth;
709         
710                                 depth = g3_calc_point_depth(pos);               //gets 3d depth
711
712                                 while (po->simpler_model && depth > cnt++ * Simple_model_threshhold_scale * po->rad)
713                                         po = &Polygon_models[po->simpler_model-1];
714                         }
715
716         if (alt_textures)
717    {
718                 for (i=0;i<po->n_textures;i++)  {
719                         texture_list_index[i] = alt_textures[i];
720                         texture_list[i] = &GameBitmaps[alt_textures[i].index];
721
722          #ifdef _3DFX
723          texture_list[i]->bm_handle = texture_list_index[i].index;
724          #endif
725                 }
726    }
727         else
728    {
729                 for (i=0;i<po->n_textures;i++)  {
730                         texture_list_index[i] = ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]];
731                         texture_list[i] = &GameBitmaps[ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]].index];
732
733          #ifdef _3DFX
734          texture_list[i]->bm_handle = texture_list_index[i].index;
735          #endif
736                 }
737    }
738
739 #ifdef PIGGY_USE_PAGING
740         // Make sure the textures for this object are paged in...
741         piggy_page_flushed = 0;
742         for (i=0;i<po->n_textures;i++)  
743                 PIGGY_PAGE_IN( texture_list_index[i] );
744         // Hmmm... cache got flushed in the middle of paging all these in,
745         // so we need to reread them all in.
746         if (piggy_page_flushed) {
747                 piggy_page_flushed = 0;
748                 for (i=0;i<po->n_textures;i++)  
749                         PIGGY_PAGE_IN( texture_list_index[i] );
750         }
751         // Make sure that they can all fit in memory.
752         Assert( piggy_page_flushed == 0 );
753 #endif
754
755         g3_start_instance_matrix(pos,orient);
756
757         g3_set_interp_points(robot_points);
758
759 #ifdef _3DFX
760    _3dfx_rendering_poly_obj = 1;
761 #endif
762         PA_DFX(save_light = Lighting_on);
763         PA_DFX(Lighting_on = 0);
764
765         if (flags == 0)         //draw entire object
766
767                 g3_draw_polygon_model(po->model_data,texture_list,anim_angles,light,glow_values);
768
769         else {
770                 int i;
771         
772                 for (i=0;flags;flags>>=1,i++)
773                         if (flags & 1) {
774                                 vms_vector ofs;
775
776                                 Assert(i < po->n_models);
777
778                                 //if submodel, rotate around its center point, not pivot point
779         
780                                 vm_vec_avg(&ofs,&po->submodel_mins[i],&po->submodel_maxs[i]);
781                                 vm_vec_negate(&ofs);
782                                 g3_start_instance_matrix(&ofs,NULL);
783         
784                                 g3_draw_polygon_model(&po->model_data[po->submodel_ptrs[i]],texture_list,anim_angles,light,glow_values);
785         
786                                 g3_done_instance();
787                         }       
788         }
789
790         g3_done_instance();
791
792 #ifdef _3DFX
793    _3dfx_rendering_poly_obj = 0;
794 #endif
795
796         PA_DFX (Lighting_on = save_light);
797
798
799 }
800
801 void free_polygon_models()
802 {
803         int i;
804
805         for (i=0;i<N_polygon_models;i++) {
806                 free_model(&Polygon_models[i]);
807         }
808
809 }
810
811 void polyobj_find_min_max(polymodel *pm)
812 {
813         ushort nverts;
814         vms_vector *vp;
815         ushort *data,type;
816         int m;
817         vms_vector *big_mn,*big_mx;
818         
819         big_mn = &pm->mins;
820         big_mx = &pm->maxs;
821
822         for (m=0;m<pm->n_models;m++) {
823                 vms_vector *mn,*mx,*ofs;
824
825                 mn = &pm->submodel_mins[m];
826                 mx = &pm->submodel_maxs[m];
827                 ofs= &pm->submodel_offsets[m];
828
829                 data = (ushort *)&pm->model_data[pm->submodel_ptrs[m]];
830         
831                 type = *data++;
832         
833                 Assert(type == 7 || type == 1);
834         
835                 nverts = *data++;
836         
837                 if (type==7)
838                         data+=2;                //skip start & pad
839         
840                 vp = (vms_vector *) data;
841         
842                 *mn = *mx = *vp++; nverts--;
843
844                 if (m==0)
845                         *big_mn = *big_mx = *mn;
846         
847                 while (nverts--) {
848                         if (vp->x > mx->x) mx->x = vp->x;
849                         if (vp->y > mx->y) mx->y = vp->y;
850                         if (vp->z > mx->z) mx->z = vp->z;
851         
852                         if (vp->x < mn->x) mn->x = vp->x;
853                         if (vp->y < mn->y) mn->y = vp->y;
854                         if (vp->z < mn->z) mn->z = vp->z;
855         
856                         if (vp->x+ofs->x > big_mx->x) big_mx->x = vp->x+ofs->x;
857                         if (vp->y+ofs->y > big_mx->y) big_mx->y = vp->y+ofs->y;
858                         if (vp->z+ofs->z > big_mx->z) big_mx->z = vp->z+ofs->z;
859         
860                         if (vp->x+ofs->x < big_mn->x) big_mn->x = vp->x+ofs->x;
861                         if (vp->y+ofs->y < big_mn->y) big_mn->y = vp->y+ofs->y;
862                         if (vp->z+ofs->z < big_mn->z) big_mn->z = vp->z+ofs->z;
863         
864                         vp++;
865                 }
866
867 //              printf("Submodel %d:  (%8x,%8x) (%8x,%8x) (%8x,%8x)\n",m,mn->x,mx->x,mn->y,mx->y,mn->z,mx->z);
868         }
869
870 //      printf("Whole model: (%8x,%8x) (%8x,%8x) (%8x,%8x)\n",big_mn->x,big_mx->x,big_mn->y,big_mx->y,big_mn->z,big_mx->z);
871
872 }
873
874 extern short highest_texture_num;       //from the 3d
875
876 char Pof_names[MAX_POLYGON_MODELS][FILENAME_LEN];
877
878 //returns the number of this model
879 #ifndef DRIVE
880 int load_polygon_model(char *filename,int n_textures,int first_texture,robot_info *r)
881 #else
882 int load_polygon_model(char *filename,int n_textures,grs_bitmap ***textures)
883 #endif
884 {
885         #ifdef DRIVE
886         #define r NULL
887         #endif
888
889         Assert(N_polygon_models < MAX_POLYGON_MODELS);
890         Assert(n_textures < MAX_POLYOBJ_TEXTURES);
891
892         //      MK was real tired of those useless, slow mprintfs...
893         if (N_polygon_models > MAX_POLYGON_MODELS - 10)
894                 mprintf(( 0, "Used %d/%d polygon model slots\n", N_polygon_models+1, MAX_POLYGON_MODELS ));
895
896         Assert(strlen(filename) <= 12);
897         strcpy(Pof_names[N_polygon_models],filename);
898
899         read_model_file(&Polygon_models[N_polygon_models],filename,r);
900
901         polyobj_find_min_max(&Polygon_models[N_polygon_models]);
902
903         g3_init_polygon_model(Polygon_models[N_polygon_models].model_data);
904
905         if (highest_texture_num+1 != n_textures)
906                 Error("Model <%s> references %d textures but specifies %d.",filename,highest_texture_num+1,n_textures);
907
908         Polygon_models[N_polygon_models].n_textures = n_textures;
909         Polygon_models[N_polygon_models].first_texture = first_texture;
910         Polygon_models[N_polygon_models].simpler_model = 0;
911
912 //      Assert(polygon_models[N_polygon_models]!=NULL);
913
914         N_polygon_models++;
915
916         return N_polygon_models-1;
917
918 }
919
920
921 void init_polygon_models()
922 {
923         N_polygon_models = 0;
924
925         atexit((void (*)())free_polygon_models);
926
927 }
928
929 //compare against this size when figuring how far to place eye for picture
930 #define BASE_MODEL_SIZE 0x28000
931
932 #define DEFAULT_VIEW_DIST 0x60000
933
934 //draws the given model in the current canvas.  The distance is set to
935 //more-or-less fill the canvas.  Note that this routine actually renders
936 //into an off-screen canvas that it creates, then copies to the current
937 //canvas.
938 void draw_model_picture(int mn,vms_angvec *orient_angles)
939 {
940         vms_vector      temp_pos=ZERO_VECTOR;
941         vms_matrix      temp_orient = IDENTITY_MATRIX;
942         grs_canvas      *save_canv = grd_curcanv,*temp_canv;
943
944         Assert(mn>=0 && mn<N_polygon_models);
945
946         temp_canv = gr_create_canvas(save_canv->cv_bitmap.bm_w,save_canv->cv_bitmap.bm_h);
947         gr_set_current_canvas(temp_canv);
948         gr_clear_canvas( BM_XRGB(0,0,0) );
949
950         g3_start_frame();
951         g3_set_view_matrix(&temp_pos,&temp_orient,0x9000);
952
953         if (Polygon_models[mn].rad != 0)
954                 temp_pos.z = fixmuldiv(DEFAULT_VIEW_DIST,Polygon_models[mn].rad,BASE_MODEL_SIZE);
955         else
956                 temp_pos.z = DEFAULT_VIEW_DIST;
957
958         vm_angles_2_matrix(&temp_orient, orient_angles);
959
960         PA_DFX(save_light = Lighting_on);
961         PA_DFX(Lighting_on = 0);
962         draw_polygon_model(&temp_pos,&temp_orient,NULL,mn,0,f1_0,NULL,NULL);
963         PA_DFX (Lighting_on = save_light);
964
965         gr_set_current_canvas(save_canv);
966
967         gr_bitmap(0,0,&temp_canv->cv_bitmap);
968
969         gr_free_canvas(temp_canv);
970 }
971
972 #ifndef FAST_FILE_IO
973 /*
974  * reads a polymodel structure from a CFILE
975  */
976 extern void polymodel_read(polymodel *pm, CFILE *fp)
977 {
978         int i;
979
980         pm->n_models = cfile_read_int(fp);
981         pm->model_data_size = cfile_read_int(fp);
982         pm->model_data = (ubyte *) cfile_read_int(fp);
983         for (i = 0; i < MAX_SUBMODELS; i++)
984                 pm->submodel_ptrs[i] = cfile_read_int(fp);
985         for (i = 0; i < MAX_SUBMODELS; i++)
986                 cfile_read_vector(&(pm->submodel_offsets[i]), fp);
987         for (i = 0; i < MAX_SUBMODELS; i++)
988                 cfile_read_vector(&(pm->submodel_norms[i]), fp);
989         for (i = 0; i < MAX_SUBMODELS; i++)
990                 cfile_read_vector(&(pm->submodel_pnts[i]), fp);
991         for (i = 0; i < MAX_SUBMODELS; i++)
992                 pm->submodel_rads[i] = cfile_read_fix(fp);
993         cfread(pm->submodel_parents, MAX_SUBMODELS, 1, fp);
994         for (i = 0; i < MAX_SUBMODELS; i++)
995                 cfile_read_vector(&(pm->submodel_mins[i]), fp);
996         for (i = 0; i < MAX_SUBMODELS; i++)
997                 cfile_read_vector(&(pm->submodel_maxs[i]), fp);
998         cfile_read_vector(&(pm->mins), fp);
999         cfile_read_vector(&(pm->maxs), fp);
1000         pm->rad = cfile_read_fix(fp);
1001         pm->n_textures = cfile_read_byte(fp);
1002         pm->first_texture = cfile_read_short(fp);
1003         pm->simpler_model = cfile_read_byte(fp);
1004 }
1005
1006 /*
1007  * reads n polymodel structs from a CFILE
1008  */
1009 extern int polymodel_read_n(polymodel *pm, int n, CFILE *fp)
1010 {
1011         int i, j;
1012
1013         for (i = 0; i < n; i++) {
1014                 pm[i].n_models = cfile_read_int(fp);
1015                 pm[i].model_data_size = cfile_read_int(fp);
1016                 pm[i].model_data = (ubyte *) cfile_read_int(fp);
1017                 for (j = 0; j < MAX_SUBMODELS; j++)
1018                         pm[i].submodel_ptrs[j] = cfile_read_int(fp);
1019                 for (j = 0; j < MAX_SUBMODELS; j++)
1020                         cfile_read_vector(&(pm[i].submodel_offsets[j]), fp);
1021                 for (j = 0; j < MAX_SUBMODELS; j++)
1022                         cfile_read_vector(&(pm[i].submodel_norms[j]), fp);
1023                 for (j = 0; j < MAX_SUBMODELS; j++)
1024                         cfile_read_vector(&(pm[i].submodel_pnts[j]), fp);
1025                 for (j = 0; j < MAX_SUBMODELS; j++)
1026                         pm[i].submodel_rads[j] = cfile_read_fix(fp);
1027                 cfread(pm[i].submodel_parents, MAX_SUBMODELS, 1, fp);
1028                 for (j = 0; j < MAX_SUBMODELS; j++)
1029                         cfile_read_vector(&(pm[i].submodel_mins[j]), fp);
1030                 for (j = 0; j < MAX_SUBMODELS; j++)
1031                         cfile_read_vector(&(pm[i].submodel_maxs[j]), fp);
1032                 cfile_read_vector(&(pm[i].mins), fp);
1033                 cfile_read_vector(&(pm[i].maxs), fp);
1034                 pm[i].rad = cfile_read_fix(fp);
1035                 pm[i].n_textures = cfile_read_byte(fp);
1036                 pm[i].first_texture = cfile_read_short(fp);
1037                 pm[i].simpler_model = cfile_read_byte(fp);
1038         }
1039         return i;
1040 }
1041 #endif
1042
1043
1044 /*
1045  * routine which allocates, reads, and inits a polymodel's model_data
1046  */
1047 void polygon_model_data_read(polymodel *pm, CFILE *fp)
1048 {
1049         pm->model_data = d_malloc(pm->model_data_size);
1050         Assert(pm->model_data != NULL);
1051         cfread(pm->model_data, sizeof(ubyte), pm->model_data_size, fp );
1052 #ifdef WORDS_NEED_ALIGNMENT
1053         align_polygon_model_data(pm);
1054 #endif
1055 #ifdef WORDS_BIGENDIAN
1056         swap_polygon_model_data(pm->model_data);
1057 #endif
1058         //verify(pm->model_data);
1059         g3_init_polygon_model(pm->model_data);
1060 }