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