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