]> icculus.org git repositories - btb/d2x.git/blob - main/polyobj.c
support playback of demos recorded in Descent 1
[btb/d2x.git] / main / polyobj.c
1 /* $Id: polyobj.c,v 1.22 2005-07-30 07:46:03 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.22 2005-07-30 07:46:03 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 OGL
61 #include "ogl_init.h"
62 #endif
63
64 polymodel Polygon_models[MAX_POLYGON_MODELS];   // = {&bot11,&bot17,&robot_s2,&robot_b2,&bot11,&bot17,&robot_s2,&robot_b2};
65
66 int D1_Polymodel_map[] = {
67         0, 1, 2, 4, 5,
68         6, 7, 8, 9, 10,
69         11, 12, 13, 14, 15,
70         16, 17, 18, 19, 20,
71         21, 22, 23, 24, 25,
72         26, 27, 28, 29, 30,
73         31, 32, 33, 34, 35,
74         36, 37, 38, 39, 93,
75         94, -1, -1, 108, 109,
76         110, 111, 112, 113, 114,
77         115, 116, 117, 118, 119,
78         120, 121, 122, 123, 124,
79         125, 126, 127, 128, 129,
80         130, 131, 132, 133, 134,
81         135, 136, 137, 138, 139,
82         140, 141, 142, -1, -1,
83         -1, -1, -1, -1, -1,
84 };
85
86 int N_polygon_models = 0;
87
88 #define MAX_POLYGON_VECS 1000
89 g3s_point robot_points[MAX_POLYGON_VECS];
90
91 #define PM_COMPATIBLE_VERSION 6
92 #define PM_OBJFILE_VERSION 8
93
94 int     Pof_file_end;
95 int     Pof_addr;
96
97 #define MODEL_BUF_SIZE  32768
98
99 void _pof_cfseek(int len,int type)
100 {
101         switch (type) {
102                 case SEEK_SET:  Pof_addr = len; break;
103                 case SEEK_CUR:  Pof_addr += len;        break;
104                 case SEEK_END:
105                         Assert(len <= 0);       //      seeking from end, better be moving back.
106                         Pof_addr = Pof_file_end + len;
107                         break;
108         }
109
110         if (Pof_addr > MODEL_BUF_SIZE)
111                 Int3();
112 }
113
114 #define pof_cfseek(_buf,_len,_type) _pof_cfseek((_len),(_type))
115
116 int pof_read_int(ubyte *bufp)
117 {
118         int i;
119
120         i = *((int *) &bufp[Pof_addr]);
121         Pof_addr += 4;
122         return INTEL_INT(i);
123
124 //      if (cfread(&i,sizeof(i),1,f) != 1)
125 //              Error("Unexpected end-of-file while reading object");
126 //
127 //      return i;
128 }
129
130 size_t pof_cfread(void *dst, size_t elsize, size_t nelem, ubyte *bufp)
131 {
132         if (Pof_addr + nelem*elsize > Pof_file_end)
133                 return 0;
134
135         memcpy(dst, &bufp[Pof_addr], elsize*nelem);
136
137         Pof_addr += elsize*nelem;
138
139         if (Pof_addr > MODEL_BUF_SIZE)
140                 Int3();
141
142         return nelem;
143 }
144
145 // #define new_read_int(i,f) cfread(&(i),sizeof(i),1,(f))
146 #define new_pof_read_int(i,f) pof_cfread(&(i),sizeof(i),1,(f))
147
148 short pof_read_short(ubyte *bufp)
149 {
150         short s;
151
152         s = *((short *) &bufp[Pof_addr]);
153         Pof_addr += 2;
154         return INTEL_SHORT(s);
155 //      if (cfread(&s,sizeof(s),1,f) != 1)
156 //              Error("Unexpected end-of-file while reading object");
157 //
158 //      return s;
159 }
160
161 void pof_read_string(char *buf,int max_char, ubyte *bufp)
162 {
163         int     i;
164
165         for (i=0; i<max_char; i++) {
166                 if ((*buf++ = bufp[Pof_addr++]) == 0)
167                         break;
168         }
169
170 //      while (max_char-- && (*buf=cfgetc(f)) != 0) buf++;
171
172 }
173
174 void pof_read_vecs(vms_vector *vecs,int n,ubyte *bufp)
175 {
176 //      cfread(vecs,sizeof(vms_vector),n,f);
177
178         memcpy(vecs, &bufp[Pof_addr], n*sizeof(*vecs));
179         Pof_addr += n*sizeof(*vecs);
180
181 #ifdef WORDS_BIGENDIAN
182         while (n > 0)
183                 vms_vector_swap(&vecs[--n]);
184 #endif
185
186         if (Pof_addr > MODEL_BUF_SIZE)
187                 Int3();
188 }
189
190 void pof_read_angs(vms_angvec *angs,int n,ubyte *bufp)
191 {
192         memcpy(angs, &bufp[Pof_addr], n*sizeof(*angs));
193         Pof_addr += n*sizeof(*angs);
194
195 #ifdef WORDS_BIGENDIAN
196         while (n > 0)
197                 vms_angvec_swap(&angs[--n]);
198 #endif
199
200         if (Pof_addr > MODEL_BUF_SIZE)
201                 Int3();
202 }
203
204 #define ID_OHDR 0x5244484f // 'RDHO'  //Object header
205 #define ID_SOBJ 0x4a424f53 // 'JBOS'  //Subobject header
206 #define ID_GUNS 0x534e5547 // 'SNUG'  //List of guns on this object
207 #define ID_ANIM 0x4d494e41 // 'MINA'  //Animation data
208 #define ID_IDTA 0x41544449 // 'ATDI'  //Interpreter data
209 #define ID_TXTR 0x52545854 // 'RTXT'  //Texture filename list
210
211 #ifdef DRIVE
212 #define robot_info void
213 #else
214 vms_angvec anim_angs[N_ANIM_STATES][MAX_SUBMODELS];
215
216 //set the animation angles for this robot.  Gun fields of robot info must
217 //be filled in.
218 void robot_set_angles(robot_info *r,polymodel *pm,vms_angvec angs[N_ANIM_STATES][MAX_SUBMODELS]);
219 #endif
220
221 #define DEBUG_LEVEL CON_NORMAL
222
223 #ifdef WORDS_NEED_ALIGNMENT
224 ubyte * old_dest(chunk o) // return where chunk is (in unaligned struct)
225 {
226         return o.old_base + INTEL_SHORT(*((short *)(o.old_base + o.offset)));
227 }
228 ubyte * new_dest(chunk o) // return where chunk is (in aligned struct)
229 {
230         return o.new_base + INTEL_SHORT(*((short *)(o.old_base + o.offset))) + o.correction;
231 }
232 /*
233  * find chunk with smallest address
234  */
235 int get_first_chunks_index(chunk *chunk_list, int no_chunks)
236 {
237         int i, first_index = 0;
238         Assert(no_chunks >= 1);
239         for (i = 1; i < no_chunks; i++)
240                 if (old_dest(chunk_list[i]) < old_dest(chunk_list[first_index]))
241                         first_index = i;
242         return first_index;
243 }
244 #define SHIFT_SPACE 500 // increase if insufficent
245
246 void align_polygon_model_data(polymodel *pm)
247 {
248         int i, chunk_len;
249         int total_correction = 0;
250         ubyte *cur_old, *cur_new;
251         chunk cur_ch;
252         chunk ch_list[MAX_CHUNKS];
253         int no_chunks = 0;
254         int tmp_size = pm->model_data_size + SHIFT_SPACE;
255         ubyte *tmp = d_malloc(tmp_size); // where we build the aligned version of pm->model_data
256
257         Assert(tmp != NULL);
258         //start with first chunk (is always aligned!)
259         cur_old = pm->model_data;
260         cur_new = tmp;
261         chunk_len = get_chunks(cur_old, cur_new, ch_list, &no_chunks);
262         memcpy(cur_new, cur_old, chunk_len);
263         while (no_chunks > 0) {
264                 int first_index = get_first_chunks_index(ch_list, no_chunks);
265                 cur_ch = ch_list[first_index];
266                 // remove first chunk from array:
267                 no_chunks--;
268                 for (i = first_index; i < no_chunks; i++)
269                         ch_list[i] = ch_list[i + 1];
270                 // if (new) address unaligned:
271                 if ((u_int32_t)new_dest(cur_ch) % 4L != 0) {
272                         // calculate how much to move to be aligned
273                         short to_shift = 4 - (u_int32_t)new_dest(cur_ch) % 4L;
274                         // correct chunks' addresses
275                         cur_ch.correction += to_shift;
276                         for (i = 0; i < no_chunks; i++)
277                                 ch_list[i].correction += to_shift;
278                         total_correction += to_shift;
279                         Assert((u_int32_t)new_dest(cur_ch) % 4L == 0);
280                         Assert(total_correction <= SHIFT_SPACE); // if you get this, increase SHIFT_SPACE
281                 }
282                 //write (corrected) chunk for current chunk:
283                 *((short *)(cur_ch.new_base + cur_ch.offset))
284                   = INTEL_SHORT(cur_ch.correction
285                                 + INTEL_SHORT(*((short *)(cur_ch.old_base + cur_ch.offset))));
286                 //write (correctly aligned) chunk:
287                 cur_old = old_dest(cur_ch);
288                 cur_new = new_dest(cur_ch);
289                 chunk_len = get_chunks(cur_old, cur_new, ch_list, &no_chunks);
290                 memcpy(cur_new, cur_old, chunk_len);
291                 //correct submodel_ptr's for pm, too
292                 for (i = 0; i < MAX_SUBMODELS; i++)
293                         if (pm->model_data + pm->submodel_ptrs[i] >= cur_old
294                             && pm->model_data + pm->submodel_ptrs[i] < cur_old + chunk_len)
295                                 pm->submodel_ptrs[i] += (cur_new - tmp) - (cur_old - pm->model_data);
296         }
297         d_free(pm->model_data);
298         pm->model_data_size += total_correction;
299         pm->model_data = d_malloc(pm->model_data_size);
300         Assert(pm->model_data != NULL);
301         memcpy(pm->model_data, tmp, pm->model_data_size);
302         d_free(tmp);
303 }
304 #endif //def WORDS_NEED_ALIGNMENT
305
306 //reads a binary file containing a 3d model
307 polymodel *read_model_file(polymodel *pm,char *filename,robot_info *r)
308 {
309         CFILE *ifile;
310         short version;
311         int id,len, next_chunk;
312         int anim_flag = 0;
313         ubyte *model_buf;
314
315         model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
316         if (!model_buf)
317                 Error("Can't allocate space to read model %s\n", filename);
318
319         if ((ifile=cfopen(filename,"rb"))==NULL)
320                 Error("Can't open file <%s>",filename);
321
322         Assert(cfilelength(ifile) <= MODEL_BUF_SIZE);
323
324         Pof_addr = 0;
325         Pof_file_end = cfread(model_buf, 1, cfilelength(ifile), ifile);
326         cfclose(ifile);
327
328         id = pof_read_int(model_buf);
329
330         if (id!=0x4f505350) /* 'OPSP' */
331                 Error("Bad ID in model file <%s>",filename);
332
333         version = pof_read_short(model_buf);
334         
335         if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
336                 Error("Bad version (%d) in model file <%s>",version,filename);
337
338         if ( FindArg( "-bspgen" ))
339                 printf( "bspgen -c1" );
340
341         while (new_pof_read_int(id,model_buf) == 1) {
342                 id = INTEL_INT(id);
343                 //id  = pof_read_int(model_buf);
344                 len = pof_read_int(model_buf);
345                 next_chunk = Pof_addr + len;
346
347                 switch (id) {
348
349                         case ID_OHDR: {         //Object header
350                                 vms_vector pmmin,pmmax;
351
352                                 //con_printf(DEBUG_LEVEL, "Got chunk OHDR, len=%d\n",len);
353
354                                 pm->n_models = pof_read_int(model_buf);
355                                 pm->rad = pof_read_int(model_buf);
356
357                                 Assert(pm->n_models <= MAX_SUBMODELS);
358
359                                 pof_read_vecs(&pmmin,1,model_buf);
360                                 pof_read_vecs(&pmmax,1,model_buf);
361
362                                 if ( FindArg( "-bspgen" ))      {
363                                         vms_vector v;
364                                         fix l;
365                                 
366                                         vm_vec_sub(&v, &pmmax, &pmmin );
367                                         l = v.x;
368                                         if ( v.y > l ) l = v.y;                                 
369                                         if ( v.z > l ) l = v.z;                                 
370                                                                                                         
371                                         printf( " -l%.3f", f2fl(l) );
372                                 }
373
374                                 break;
375                         }
376                         
377                         case ID_SOBJ: {         //Subobject header
378                                 int n;
379
380                                 anim_flag++;
381
382                                 //con_printf(DEBUG_LEVEL, "Got chunk SOBJ, len=%d\n",len);
383
384                                 n = pof_read_short(model_buf);
385
386                                 Assert(n < MAX_SUBMODELS);
387
388                                 pm->submodel_parents[n] = pof_read_short(model_buf);
389
390                                 pof_read_vecs(&pm->submodel_norms[n],1,model_buf);
391                                 pof_read_vecs(&pm->submodel_pnts[n],1,model_buf);
392                                 pof_read_vecs(&pm->submodel_offsets[n],1,model_buf);
393
394                                 pm->submodel_rads[n] = pof_read_int(model_buf);         //radius
395
396                                 pm->submodel_ptrs[n] = pof_read_int(model_buf); //offset
397
398                                 break;
399
400                         }
401                         
402                         #ifndef DRIVE
403                         case ID_GUNS: {         //List of guns on this object
404
405                                 //con_printf(DEBUG_LEVEL, "Got chunk GUNS, len=%d\n",len);
406
407                                 if (r) {
408                                         int i;
409                                         vms_vector gun_dir;
410                                         ubyte gun_used[MAX_GUNS];
411
412                                         r->n_guns = pof_read_int(model_buf);
413
414                                         if ( r->n_guns )
415                                                 anim_flag++;
416
417                                         Assert(r->n_guns <= MAX_GUNS);
418
419                                         for (i=0;i<r->n_guns;i++)
420                                                 gun_used[i] = 0;
421
422                                         for (i=0;i<r->n_guns;i++) {
423                                                 int id;
424
425                                                 id = pof_read_short(model_buf);
426                                                 Assert(id < r->n_guns);
427                                                 Assert(gun_used[id] == 0);
428                                                 gun_used[id] = 1;
429                                                 r->gun_submodels[id] = pof_read_short(model_buf);
430                                                 Assert(r->gun_submodels[id] != 0xff);
431                                                 pof_read_vecs(&r->gun_points[id],1,model_buf);
432
433                                                 if (version >= 7)
434                                                         pof_read_vecs(&gun_dir,1,model_buf);
435                                         }
436                                 }
437                                 else
438                                         pof_cfseek(model_buf,len,SEEK_CUR);
439
440                                 break;
441                         }
442                         
443                         case ID_ANIM:           //Animation data
444                                 //con_printf(DEBUG_LEVEL, "Got chunk ANIM, len=%d\n",len);
445
446                                 anim_flag++;
447
448                                 if (r) {
449                                         int n_frames,f,m;
450
451                                         n_frames = pof_read_short(model_buf);
452
453                                         Assert(n_frames == N_ANIM_STATES);
454
455                                         for (m=0;m<pm->n_models;m++)
456                                                 for (f=0;f<n_frames;f++)
457                                                         pof_read_angs(&anim_angs[f][m], 1, model_buf);
458
459
460                                         robot_set_angles(r,pm,anim_angs);
461                                 
462                                 }
463                                 else
464                                         pof_cfseek(model_buf,len,SEEK_CUR);
465
466                                 break;
467                         #endif
468                         
469                         case ID_TXTR: {         //Texture filename list
470                                 int n;
471                                 char name_buf[128];
472
473                                 //con_printf(DEBUG_LEVEL, "Got chunk TXTR, len=%d\n",len);
474
475                                 n = pof_read_short(model_buf);
476                                 //con_printf(DEBUG_LEVEL, "  num textures = %d\n",n);
477                                 while (n--) {
478                                         pof_read_string(name_buf,128,model_buf);
479                                         //con_printf(DEBUG_LEVEL, "<%s>\n",name_buf);
480                                 }
481
482                                 break;
483                         }
484                         
485                         case ID_IDTA:           //Interpreter data
486                                 //con_printf(DEBUG_LEVEL, "Got chunk IDTA, len=%d\n",len);
487
488                                 pm->model_data = d_malloc(len);
489                                 pm->model_data_size = len;
490
491                                 pof_cfread(pm->model_data,1,len,model_buf);
492
493                                 break;
494
495                         default:
496                                 //con_printf(DEBUG_LEVEL, "Unknown chunk <%c%c%c%c>, len = %d\n",id,id>>8,id>>16,id>>24,len);
497                                 pof_cfseek(model_buf,len,SEEK_CUR);
498                                 break;
499
500                 }
501                 if ( version >= 8 )             // Version 8 needs 4-byte alignment!!!
502                         pof_cfseek(model_buf,next_chunk,SEEK_SET);
503         }
504
505 //      for (i=0;i<pm->n_models;i++)
506 //              pm->submodel_ptrs[i] += (int) pm->model_data;
507
508         if ( FindArg( "-bspgen" )) {
509                 char *p = strchr( filename, '.' );
510                 *p = 0;
511
512                 if ( anim_flag > 1 )
513                         printf( " -a" );
514
515                 printf( " %s.3ds\n", filename );
516                 *p = '.';
517         }
518         
519         d_free(model_buf);
520
521 #ifdef WORDS_NEED_ALIGNMENT
522         align_polygon_model_data(pm);
523 #endif
524 #ifdef WORDS_BIGENDIAN
525         swap_polygon_model_data(pm->model_data);
526 #endif
527         //verify(pm->model_data);
528
529         return pm;
530 }
531
532 //reads the gun information for a model
533 //fills in arrays gun_points & gun_dirs, returns the number of guns read
534 int read_model_guns(char *filename,vms_vector *gun_points, vms_vector *gun_dirs, int *gun_submodels)
535 {
536         CFILE *ifile;
537         short version;
538         int id,len;
539         int n_guns=0;
540         ubyte   *model_buf;
541
542         model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
543         if (!model_buf)
544                 Error("Can't allocate space to read model %s\n", filename);
545
546         if ((ifile=cfopen(filename,"rb"))==NULL)
547                 Error("Can't open file <%s>",filename);
548
549         Assert(cfilelength(ifile) <= MODEL_BUF_SIZE);
550
551         Pof_addr = 0;
552         Pof_file_end = cfread(model_buf, 1, cfilelength(ifile), ifile);
553         cfclose(ifile);
554
555         id = pof_read_int(model_buf);
556
557         if (id!=0x4f505350) /* 'OPSP' */
558                 Error("Bad ID in model file <%s>",filename);
559
560         version = pof_read_short(model_buf);
561
562         Assert(version >= 7);           //must be 7 or higher for this data
563
564         if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
565                 Error("Bad version (%d) in model file <%s>",version,filename);
566
567         while (new_pof_read_int(id,model_buf) == 1) {
568                 id = INTEL_INT(id);
569                 //id  = pof_read_int(model_buf);
570                 len = pof_read_int(model_buf);
571
572                 if (id == ID_GUNS) {            //List of guns on this object
573
574                         //con_printf(DEBUG_LEVEL, "Got chunk GUNS, len=%d\n",len);
575
576                         int i;
577
578                         n_guns = pof_read_int(model_buf);
579
580                         for (i=0;i<n_guns;i++) {
581                                 int id,sm;
582
583                                 id = pof_read_short(model_buf);
584                                 sm = pof_read_short(model_buf);
585                                 if (gun_submodels)
586                                         gun_submodels[id] = sm;
587                                 else if (sm!=0)
588                                         Error("Invalid gun submodel in file <%s>",filename);
589                                 pof_read_vecs(&gun_points[id],1,model_buf);
590
591                                 pof_read_vecs(&gun_dirs[id],1,model_buf);
592                         }
593
594                 }
595                 else
596                         pof_cfseek(model_buf,len,SEEK_CUR);
597
598         }
599
600         d_free(model_buf);
601         
602         return n_guns;
603 }
604
605 //free up a model, getting rid of all its memory
606 void free_model(polymodel *po)
607 {
608         d_free(po->model_data);
609 }
610
611 grs_bitmap *texture_list[MAX_POLYOBJ_TEXTURES];
612 bitmap_index texture_list_index[MAX_POLYOBJ_TEXTURES];
613
614 int Simple_model_threshhold_scale=5;            //switch when this times radius far away
615
616
617 //draw a polygon model
618
619 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[])
620 {
621         polymodel *po;
622         int i;
623
624         Assert(model_num < N_polygon_models);
625
626         po=&Polygon_models[model_num];
627
628         //check if should use simple model
629         if (po->simpler_model )                                 //must have a simpler model
630                 if (flags==0)                                                   //can't switch if this is debris
631                         //!!if (!alt_textures) {                                //alternate textures might not match
632                         //alt textures might not match, but in the one case we're using this
633                         //for on 11/14/94, they do match.  So we leave it in.
634                         {
635                                 int cnt=1;
636                                 fix depth;
637         
638                                 depth = g3_calc_point_depth(pos);               //gets 3d depth
639
640                                 while (po->simpler_model && depth > cnt++ * Simple_model_threshhold_scale * po->rad)
641                                         po = &Polygon_models[po->simpler_model-1];
642                         }
643
644         if (alt_textures)
645    {
646                 for (i=0;i<po->n_textures;i++)  {
647                         texture_list_index[i] = alt_textures[i];
648                         texture_list[i] = &GameBitmaps[alt_textures[i].index];
649                 }
650    }
651         else
652    {
653                 for (i=0;i<po->n_textures;i++)  {
654                         texture_list_index[i] = ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]];
655                         texture_list[i] = &GameBitmaps[ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]].index];
656                 }
657    }
658
659 #ifdef PIGGY_USE_PAGING
660         // Make sure the textures for this object are paged in...
661         piggy_page_flushed = 0;
662         for (i=0;i<po->n_textures;i++)  
663                 PIGGY_PAGE_IN( texture_list_index[i] );
664         // Hmmm... cache got flushed in the middle of paging all these in,
665         // so we need to reread them all in.
666         if (piggy_page_flushed) {
667                 piggy_page_flushed = 0;
668                 for (i=0;i<po->n_textures;i++)  
669                         PIGGY_PAGE_IN( texture_list_index[i] );
670         }
671         // Make sure that they can all fit in memory.
672         Assert( piggy_page_flushed == 0 );
673 #endif
674
675         g3_start_instance_matrix(pos,orient);
676
677         g3_set_interp_points(robot_points);
678
679         if (flags == 0)         //draw entire object
680
681                 g3_draw_polygon_model(po->model_data,texture_list,anim_angles,light,glow_values);
682
683         else {
684                 int i;
685         
686                 for (i=0;flags;flags>>=1,i++)
687                         if (flags & 1) {
688                                 vms_vector ofs;
689
690                                 Assert(i < po->n_models);
691
692                                 //if submodel, rotate around its center point, not pivot point
693         
694                                 vm_vec_avg(&ofs,&po->submodel_mins[i],&po->submodel_maxs[i]);
695                                 vm_vec_negate(&ofs);
696                                 g3_start_instance_matrix(&ofs,NULL);
697         
698                                 g3_draw_polygon_model(&po->model_data[po->submodel_ptrs[i]],texture_list,anim_angles,light,glow_values);
699         
700                                 g3_done_instance();
701                         }       
702         }
703
704         g3_done_instance();
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 }