]> icculus.org git repositories - btb/d2x.git/blob - main/polyobj.c
Unified arch headers
[btb/d2x.git] / main / polyobj.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14
15 #ifdef RCS
16 static char rcsid[] = "$Id: polyobj.c,v 1.3 2001-01-24 04:29:48 bradleyb Exp $";
17 #endif
18
19 #include <conf.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 // -- I hate this warning in make depend! -- #ifdef DRIVE
26 // -- I hate this warning in make depend! -- #include "drive.h"
27 // -- I hate this warning in make depend! -- #else
28 #include "inferno.h"
29 // -- I hate this warning in make depend! -- #endif
30
31 #include "polyobj.h"
32
33 #include "vecmat.h"
34 #include "3d.h"
35 #include "error.h"
36 #include "mono.h"
37 #include "u_mem.h"
38 #include "args.h"
39
40 #ifndef DRIVE
41 #include "texmap.h"
42 #include "bm.h"
43 #include "textures.h"
44 #include "object.h"
45 #include "lighting.h"
46 #include "cfile.h"
47 #include "piggy.h"
48 #endif
49
50 #include "pa_enabl.h"
51
52 #ifdef _3DFX
53 #include "3dfx_des.h"
54 #endif
55
56 polymodel Polygon_models[MAX_POLYGON_MODELS];   // = {&bot11,&bot17,&robot_s2,&robot_b2,&bot11,&bot17,&robot_s2,&robot_b2};
57
58 int N_polygon_models = 0;
59
60 #define MAX_POLYGON_VECS 1000
61 g3s_point robot_points[MAX_POLYGON_VECS];
62
63 #define PM_COMPATIBLE_VERSION 6
64 #define PM_OBJFILE_VERSION 8
65
66 int     Pof_file_end;
67 int     Pof_addr;
68
69 #define MODEL_BUF_SIZE  32768
70
71 void _pof_cfseek(int len,int type)
72 {
73         switch (type) {
74                 case SEEK_SET:  Pof_addr = len; break;
75                 case SEEK_CUR:  Pof_addr += len;        break;
76                 case SEEK_END:
77                         Assert(len <= 0);       //      seeking from end, better be moving back.
78                         Pof_addr = Pof_file_end + len;
79                         break;
80         }
81
82         if (Pof_addr > MODEL_BUF_SIZE)
83                 Int3();
84 }
85
86 #define pof_cfseek(_buf,_len,_type) _pof_cfseek((_len),(_type))
87
88 int pof_read_int(ubyte *bufp)
89 {
90         int i;
91
92         i = *((int *) &bufp[Pof_addr]);
93         Pof_addr += 4;
94         return i;
95
96 //      if (cfread(&i,sizeof(i),1,f) != 1)
97 //              Error("Unexpected end-of-file while reading object");
98 //
99 //      return i;
100 }
101
102 size_t pof_cfread(void *dst, size_t elsize, size_t nelem, ubyte *bufp)
103 {
104         if (Pof_addr + nelem*elsize > Pof_file_end)
105                 return 0;
106
107         memcpy(dst, &bufp[Pof_addr], elsize*nelem);
108
109         Pof_addr += elsize*nelem;
110
111         if (Pof_addr > MODEL_BUF_SIZE)
112                 Int3();
113
114         return nelem;
115 }
116
117 // #define new_read_int(i,f) cfread(&(i),sizeof(i),1,(f))
118 #define new_pof_read_int(i,f) pof_cfread(&(i),sizeof(i),1,(f))
119
120 short pof_read_short(ubyte *bufp)
121 {
122         short s;
123
124         s = *((short *) &bufp[Pof_addr]);
125         Pof_addr += 2;
126         return s;
127 //      if (cfread(&s,sizeof(s),1,f) != 1)
128 //              Error("Unexpected end-of-file while reading object");
129 //
130 //      return s;
131 }
132
133 void pof_read_string(char *buf,int max_char, ubyte *bufp)
134 {
135         int     i;
136
137         for (i=0; i<max_char; i++) {
138                 if ((*buf++ = bufp[Pof_addr++]) == 0)
139                         break;
140         }
141
142 //      while (max_char-- && (*buf=cfgetc(f)) != 0) buf++;
143
144 }
145
146 void pof_read_vecs(vms_vector *vecs,int n,ubyte *bufp)
147 {
148 //      cfread(vecs,sizeof(vms_vector),n,f);
149
150         memcpy(vecs, &bufp[Pof_addr], n*sizeof(*vecs));
151         Pof_addr += n*sizeof(*vecs);
152         
153         if (Pof_addr > MODEL_BUF_SIZE)
154                 Int3();
155 }
156
157 #define ID_OHDR 0x5244484f // 'RDHO'  //Object header
158 #define ID_SOBJ 0x4a424f53 // 'JBOS'  //Subobject header
159 #define ID_GUNS 0x534e5547 // 'SNUG'  //List of guns on this object
160 #define ID_ANIM 0x4d494e41 // 'MINA'  //Animation data
161 #define ID_IDTA 0x41544449 // 'ATDI'  //Interpreter data
162 #define ID_TXTR 0x52545854 // 'RTXT'  //Texture filename list
163
164 #ifdef DRIVE
165 #define robot_info void
166 #else
167 vms_angvec anim_angs[N_ANIM_STATES][MAX_SUBMODELS];
168
169 //set the animation angles for this robot.  Gun fields of robot info must
170 //be filled in.
171 void robot_set_angles(robot_info *r,polymodel *pm,vms_angvec angs[N_ANIM_STATES][MAX_SUBMODELS]);
172 #endif
173
174 //reads a binary file containing a 3d model
175 polymodel *read_model_file(polymodel *pm,char *filename,robot_info *r)
176 {
177         CFILE *ifile;
178         short version;
179         int id,len, next_chunk;
180         int anim_flag = 0;
181         ubyte *model_buf;
182         
183         model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
184         if (!model_buf)
185                 Error("Can't allocate space to read model %s\n", filename);
186
187         if ((ifile=cfopen(filename,"rb"))==NULL) 
188                 Error("Can't open file <%s>",filename);
189
190         Assert(ifile->size <= MODEL_BUF_SIZE);
191
192         Pof_addr = 0;
193         Pof_file_end = cfread(model_buf, 1, cfilelength(ifile), ifile);
194         cfclose(ifile);
195
196         id = pof_read_int(model_buf);
197
198         if (id!=0x4f505350) /* 'OPSP' */
199                 Error("Bad ID in model file <%s>",filename);
200
201         version = pof_read_short(model_buf);
202         
203         if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
204                 Error("Bad version (%d) in model file <%s>",version,filename);
205
206         if ( FindArg( "-bspgen" )) 
207                 printf( "bspgen -c1" );
208
209         while (new_pof_read_int(id,model_buf) == 1) {
210
211                 //id  = pof_read_int(model_buf);
212                 len = pof_read_int(model_buf);
213                 next_chunk = Pof_addr + len;
214
215                 switch (id) {
216
217                         case ID_OHDR: {         //Object header
218                                 vms_vector pmmin,pmmax;
219
220                                 //mprintf(0,"Got chunk OHDR, len=%d\n",len);
221
222                                 pm->n_models = pof_read_int(model_buf);
223                                 pm->rad = pof_read_int(model_buf);
224
225                                 Assert(pm->n_models <= MAX_SUBMODELS);
226
227                                 pof_read_vecs(&pmmin,1,model_buf);
228                                 pof_read_vecs(&pmmax,1,model_buf);
229
230                                 if ( FindArg( "-bspgen" ))      {
231                                         vms_vector v;
232                                         fix l;
233                                 
234                                         vm_vec_sub(&v, &pmmax, &pmmin );
235                                         l = v.x;
236                                         if ( v.y > l ) l = v.y;                                 
237                                         if ( v.z > l ) l = v.z;                                 
238                                                                                                         
239                                         printf( " -l%.3f", f2fl(l) );
240                                 }
241
242                                 break;
243                         }
244                         
245                         case ID_SOBJ: {         //Subobject header
246                                 int n;
247
248                                 anim_flag++;
249
250                                 //mprintf(0,"Got chunk SOBJ, len=%d\n",len);
251
252                                 n = pof_read_short(model_buf);
253
254                                 Assert(n < MAX_SUBMODELS);
255
256                                 pm->submodel_parents[n] = pof_read_short(model_buf);
257
258                                 pof_read_vecs(&pm->submodel_norms[n],1,model_buf);
259                                 pof_read_vecs(&pm->submodel_pnts[n],1,model_buf);
260                                 pof_read_vecs(&pm->submodel_offsets[n],1,model_buf);
261
262                                 pm->submodel_rads[n] = pof_read_int(model_buf);         //radius
263
264                                 pm->submodel_ptrs[n] = pof_read_int(model_buf); //offset
265
266                                 break;
267
268                         }
269                         
270                         #ifndef DRIVE
271                         case ID_GUNS: {         //List of guns on this object
272
273                                 //mprintf(0,"Got chunk GUNS, len=%d\n",len);
274
275                                 if (r) {
276                                         int i;
277                                         vms_vector gun_dir;
278                                         ubyte gun_used[MAX_GUNS];
279
280                                         r->n_guns = pof_read_int(model_buf);
281
282                                         if ( r->n_guns )
283                                                 anim_flag++;
284
285                                         Assert(r->n_guns <= MAX_GUNS);
286
287                                         for (i=0;i<r->n_guns;i++)
288                                                 gun_used[i] = 0;
289
290                                         for (i=0;i<r->n_guns;i++) {
291                                                 int id;
292
293                                                 id = pof_read_short(model_buf);
294                                                 Assert(id < r->n_guns);
295                                                 Assert(gun_used[id] == 0);
296                                                 gun_used[id] = 1;
297                                                 r->gun_submodels[id] = pof_read_short(model_buf);
298                                                 Assert(r->gun_submodels[id] != 0xff);
299                                                 pof_read_vecs(&r->gun_points[id],1,model_buf);
300
301                                                 if (version >= 7)
302                                                         pof_read_vecs(&gun_dir,1,model_buf);
303                                         }
304                                 }
305                                 else
306                                         pof_cfseek(model_buf,len,SEEK_CUR);
307
308                                 break;
309                         }
310                         
311                         case ID_ANIM:           //Animation data
312                                 //mprintf(0,"Got chunk ANIM, len=%d\n",len);
313
314                                 anim_flag++;
315
316                                 if (r) {
317                                         int n_frames,f,m;
318
319                                         n_frames = pof_read_short(model_buf);
320
321                                         Assert(n_frames == N_ANIM_STATES);
322
323                                         for (m=0;m<pm->n_models;m++)
324                                                 for (f=0;f<n_frames;f++)
325                                                         pof_cfread(&anim_angs[f][m],1,sizeof(vms_angvec),model_buf);
326
327                                         robot_set_angles(r,pm,anim_angs);
328                                 
329                                 }
330                                 else
331                                         pof_cfseek(model_buf,len,SEEK_CUR);
332
333                                 break;
334                         #endif
335                         
336                         case ID_TXTR: {         //Texture filename list
337                                 int n;
338                                 char name_buf[128];
339
340                                 //mprintf(0,"Got chunk TXTR, len=%d\n",len);
341
342                                 n = pof_read_short(model_buf);
343                                 //mprintf(0,"  num textures = %d\n",n);
344                                 while (n--) {
345                                         pof_read_string(name_buf,128,model_buf);
346                                         //mprintf(0,"<%s>\n",name_buf);
347                                 }
348
349                                 break;
350                         }
351                         
352                         case ID_IDTA:           //Interpreter data
353                                 //mprintf(0,"Got chunk IDTA, len=%d\n",len);
354
355                                 pm->model_data = d_malloc(len);
356                                 pm->model_data_size = len;
357                         
358                                 pof_cfread(pm->model_data,1,len,model_buf);
359                         
360                                 break;
361
362                         default:
363                                 //mprintf(0,"Unknown chunk <%c%c%c%c>, len = %d\n",id,id>>8,id>>16,id>>24,len);
364                                 pof_cfseek(model_buf,len,SEEK_CUR);
365                                 break;
366
367                 }
368                 if ( version >= 8 )             // Version 8 needs 4-byte alignment!!!
369                         pof_cfseek(model_buf,next_chunk,SEEK_SET);
370         }
371
372 //      for (i=0;i<pm->n_models;i++)
373 //              pm->submodel_ptrs[i] += (int) pm->model_data;
374
375         if ( FindArg( "-bspgen" )) {
376                 char *p = strchr( filename, '.' );
377                 *p = 0;
378
379                 if ( anim_flag > 1 )
380                         printf( " -a" );
381
382                 printf( " %s.3ds\n", filename );
383                 *p = '.';
384         }
385         
386         d_free(model_buf);
387
388         return pm;
389 }
390
391 //reads the gun information for a model
392 //fills in arrays gun_points & gun_dirs, returns the number of guns read
393 int read_model_guns(char *filename,vms_vector *gun_points, vms_vector *gun_dirs, int *gun_submodels)
394 {
395         CFILE *ifile;
396         short version;
397         int id,len;
398         int n_guns=0;
399         ubyte   *model_buf;
400
401         model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
402         if (!model_buf)
403                 Error("Can't allocate space to read model %s\n", filename);
404
405         if ((ifile=cfopen(filename,"rb"))==NULL) 
406                 Error("Can't open file <%s>",filename);
407
408         Assert(ifile->size <= MODEL_BUF_SIZE);
409
410         Pof_addr = 0;
411         Pof_file_end = cfread(model_buf, 1, ifile->size, ifile);
412         cfclose(ifile);
413
414         id = pof_read_int(model_buf);
415
416         if (id!=0x4f505350) /* 'OPSP' */
417                 Error("Bad ID in model file <%s>",filename);
418
419         version = pof_read_short(model_buf);
420         
421         Assert(version >= 7);           //must be 7 or higher for this data
422
423         if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
424                 Error("Bad version (%d) in model file <%s>",version,filename);
425
426         while (new_pof_read_int(id,model_buf) == 1) {
427
428                 //id  = pof_read_int(model_buf);
429                 len = pof_read_int(model_buf);
430
431                 if (id == ID_GUNS) {            //List of guns on this object
432
433                         //mprintf(0,"Got chunk GUNS, len=%d\n",len);
434
435                         int i;
436
437                         n_guns = pof_read_int(model_buf);
438
439                         for (i=0;i<n_guns;i++) {
440                                 int id,sm;
441
442                                 id = pof_read_short(model_buf);
443                                 sm = pof_read_short(model_buf);
444                                 if (gun_submodels)
445                                         gun_submodels[id] = sm;
446                                 else if (sm!=0)
447                                         Error("Invalid gun submodel in file <%s>",filename);
448                                 pof_read_vecs(&gun_points[id],1,model_buf);
449
450                                 pof_read_vecs(&gun_dirs[id],1,model_buf);
451                         }
452
453                 }
454                 else
455                         pof_cfseek(model_buf,len,SEEK_CUR);
456
457         }
458
459         d_free(model_buf);
460         
461         return n_guns;
462 }
463
464 //free up a model, getting rid of all its memory
465 void free_model(polymodel *po)
466 {
467         d_free(po->model_data);
468 }
469
470 grs_bitmap *texture_list[MAX_POLYOBJ_TEXTURES];
471 bitmap_index texture_list_index[MAX_POLYOBJ_TEXTURES];
472
473 int Simple_model_threshhold_scale=5;            //switch when this times radius far away
474
475
476 //draw a polygon model
477
478 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[])
479 {
480         polymodel *po;
481         int i;
482    PA_DFX (int save_light);
483
484         Assert(model_num < N_polygon_models);
485
486         po=&Polygon_models[model_num];
487
488         //check if should use simple model
489         if (po->simpler_model )                                 //must have a simpler model
490                 if (flags==0)                                                   //can't switch if this is debris
491                         //!!if (!alt_textures) {                                //alternate textures might not match
492                         //alt textures might not match, but in the one case we're using this
493                         //for on 11/14/94, they do match.  So we leave it in.
494                         {
495                                 int cnt=1;
496                                 fix depth;
497         
498                                 depth = g3_calc_point_depth(pos);               //gets 3d depth
499
500                                 while (po->simpler_model && depth > cnt++ * Simple_model_threshhold_scale * po->rad)
501                                         po = &Polygon_models[po->simpler_model-1];
502                         }
503
504         if (alt_textures)
505    {
506                 for (i=0;i<po->n_textures;i++)  {
507                         texture_list_index[i] = alt_textures[i];
508                         texture_list[i] = &GameBitmaps[alt_textures[i].index];
509
510          #ifdef _3DFX
511          texture_list[i]->bm_handle = texture_list_index[i].index;
512          #endif
513                 }
514    }
515         else
516    {
517                 for (i=0;i<po->n_textures;i++)  {
518                         texture_list_index[i] = ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]];
519                         texture_list[i] = &GameBitmaps[ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]].index];
520
521          #ifdef _3DFX
522          texture_list[i]->bm_handle = texture_list_index[i].index;
523          #endif
524                 }
525    }
526
527 #ifdef PIGGY_USE_PAGING
528         // Make sure the textures for this object are paged in...
529         piggy_page_flushed = 0;
530         for (i=0;i<po->n_textures;i++)  
531                 PIGGY_PAGE_IN( texture_list_index[i] );
532         // Hmmm... cache got flushed in the middle of paging all these in,
533         // so we need to reread them all in.
534         if (piggy_page_flushed) {
535                 piggy_page_flushed = 0;
536                 for (i=0;i<po->n_textures;i++)  
537                         PIGGY_PAGE_IN( texture_list_index[i] );
538         }
539         // Make sure that they can all fit in memory.
540         Assert( piggy_page_flushed == 0 );
541 #endif
542
543         g3_start_instance_matrix(pos,orient);
544
545         g3_set_interp_points(robot_points);
546
547 #ifdef _3DFX
548    _3dfx_rendering_poly_obj = 1;
549 #endif
550         PA_DFX(save_light = Lighting_on);
551         PA_DFX(Lighting_on = 0);
552
553         if (flags == 0)         //draw entire object
554
555                 g3_draw_polygon_model(po->model_data,texture_list,anim_angles,light,glow_values);
556
557         else {
558                 int i;
559         
560                 for (i=0;flags;flags>>=1,i++)
561                         if (flags & 1) {
562                                 vms_vector ofs;
563
564                                 Assert(i < po->n_models);
565
566                                 //if submodel, rotate around its center point, not pivot point
567         
568                                 vm_vec_avg(&ofs,&po->submodel_mins[i],&po->submodel_maxs[i]);
569                                 vm_vec_negate(&ofs);
570                                 g3_start_instance_matrix(&ofs,NULL);
571         
572                                 g3_draw_polygon_model(&po->model_data[po->submodel_ptrs[i]],texture_list,anim_angles,light,glow_values);
573         
574                                 g3_done_instance();
575                         }       
576         }
577
578         g3_done_instance();
579
580 #ifdef _3DFX
581    _3dfx_rendering_poly_obj = 0;
582 #endif
583
584         PA_DFX (Lighting_on = save_light);
585
586
587 }
588
589 void free_polygon_models()
590 {
591         int i;
592
593         for (i=0;i<N_polygon_models;i++) {
594                 free_model(&Polygon_models[i]);
595         }
596
597 }
598
599 void polyobj_find_min_max(polymodel *pm)
600 {
601         ushort nverts;
602         vms_vector *vp;
603         ushort *data,type;
604         int m;
605         vms_vector *big_mn,*big_mx;
606         
607         big_mn = &pm->mins;
608         big_mx = &pm->maxs;
609
610         for (m=0;m<pm->n_models;m++) {
611                 vms_vector *mn,*mx,*ofs;
612
613                 mn = &pm->submodel_mins[m];
614                 mx = &pm->submodel_maxs[m];
615                 ofs= &pm->submodel_offsets[m];
616
617                 data = (ushort *)&pm->model_data[pm->submodel_ptrs[m]];
618         
619                 type = *data++;
620         
621                 Assert(type == 7 || type == 1);
622         
623                 nverts = *data++;
624         
625                 if (type==7)
626                         data+=2;                //skip start & pad
627         
628                 vp = (vms_vector *) data;
629         
630                 *mn = *mx = *vp++; nverts--;
631
632                 if (m==0)
633                         *big_mn = *big_mx = *mn;
634         
635                 while (nverts--) {
636                         if (vp->x > mx->x) mx->x = vp->x;
637                         if (vp->y > mx->y) mx->y = vp->y;
638                         if (vp->z > mx->z) mx->z = vp->z;
639         
640                         if (vp->x < mn->x) mn->x = vp->x;
641                         if (vp->y < mn->y) mn->y = vp->y;
642                         if (vp->z < mn->z) mn->z = vp->z;
643         
644                         if (vp->x+ofs->x > big_mx->x) big_mx->x = vp->x+ofs->x;
645                         if (vp->y+ofs->y > big_mx->y) big_mx->y = vp->y+ofs->y;
646                         if (vp->z+ofs->z > big_mx->z) big_mx->z = vp->z+ofs->z;
647         
648                         if (vp->x+ofs->x < big_mn->x) big_mn->x = vp->x+ofs->x;
649                         if (vp->y+ofs->y < big_mn->y) big_mn->y = vp->y+ofs->y;
650                         if (vp->z+ofs->z < big_mn->z) big_mn->z = vp->z+ofs->z;
651         
652                         vp++;
653                 }
654
655 //              printf("Submodel %d:  (%8x,%8x) (%8x,%8x) (%8x,%8x)\n",m,mn->x,mx->x,mn->y,mx->y,mn->z,mx->z);
656         }
657
658 //      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);
659
660 }
661
662 extern short highest_texture_num;       //from the 3d
663
664 char Pof_names[MAX_POLYGON_MODELS][FILENAME_LEN];
665
666 //returns the number of this model
667 #ifndef DRIVE
668 int load_polygon_model(char *filename,int n_textures,int first_texture,robot_info *r)
669 #else
670 int load_polygon_model(char *filename,int n_textures,grs_bitmap ***textures)
671 #endif
672 {
673         #ifdef DRIVE
674         #define r NULL
675         #endif
676
677         Assert(N_polygon_models < MAX_POLYGON_MODELS);
678         Assert(n_textures < MAX_POLYOBJ_TEXTURES);
679
680         //      MK was real tired of those useless, slow mprintfs...
681         if (N_polygon_models > MAX_POLYGON_MODELS - 10)
682                 mprintf(( 0, "Used %d/%d polygon model slots\n", N_polygon_models+1, MAX_POLYGON_MODELS ));
683
684         Assert(strlen(filename) <= 12);
685         strcpy(Pof_names[N_polygon_models],filename);
686
687         read_model_file(&Polygon_models[N_polygon_models],filename,r);
688
689         polyobj_find_min_max(&Polygon_models[N_polygon_models]);
690
691         g3_init_polygon_model(Polygon_models[N_polygon_models].model_data);
692
693         if (highest_texture_num+1 != n_textures)
694                 Error("Model <%s> references %d textures but specifies %d.",filename,highest_texture_num+1,n_textures);
695
696         Polygon_models[N_polygon_models].n_textures = n_textures;
697         Polygon_models[N_polygon_models].first_texture = first_texture;
698         Polygon_models[N_polygon_models].simpler_model = 0;
699
700 //      Assert(polygon_models[N_polygon_models]!=NULL);
701
702         N_polygon_models++;
703
704         return N_polygon_models-1;
705
706 }
707
708
709 void init_polygon_models()
710 {
711         N_polygon_models = 0;
712
713         atexit((void (*)())free_polygon_models);
714
715 }
716
717 //compare against this size when figuring how far to place eye for picture
718 #define BASE_MODEL_SIZE 0x28000
719
720 #define DEFAULT_VIEW_DIST 0x60000
721
722 //draws the given model in the current canvas.  The distance is set to
723 //more-or-less fill the canvas.  Note that this routine actually renders
724 //into an off-screen canvas that it creates, then copies to the current
725 //canvas.
726 void draw_model_picture(int mn,vms_angvec *orient_angles)
727 {
728         vms_vector      temp_pos=ZERO_VECTOR;
729         vms_matrix      temp_orient = IDENTITY_MATRIX;
730         grs_canvas      *save_canv = grd_curcanv,*temp_canv;
731
732         Assert(mn>=0 && mn<N_polygon_models);
733
734         temp_canv = gr_create_canvas(save_canv->cv_bitmap.bm_w,save_canv->cv_bitmap.bm_h);
735         gr_set_current_canvas(temp_canv);
736         gr_clear_canvas( BM_XRGB(0,0,0) );
737
738         g3_start_frame();
739         g3_set_view_matrix(&temp_pos,&temp_orient,0x9000);
740
741         if (Polygon_models[mn].rad != 0)
742                 temp_pos.z = fixmuldiv(DEFAULT_VIEW_DIST,Polygon_models[mn].rad,BASE_MODEL_SIZE);
743         else
744                 temp_pos.z = DEFAULT_VIEW_DIST;
745
746         vm_angles_2_matrix(&temp_orient, orient_angles);
747
748         PA_DFX(save_light = Lighting_on);
749         PA_DFX(Lighting_on = 0);
750         draw_polygon_model(&temp_pos,&temp_orient,NULL,mn,0,f1_0,NULL,NULL);
751         PA_DFX (Lighting_on = save_light);
752
753         gr_set_current_canvas(save_canv);
754
755         gr_bitmap(0,0,&temp_canv->cv_bitmap);
756
757         gr_free_canvas(temp_canv);
758 }
759