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