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