1 /* $Id: polyobj.c,v 1.16 2003-10-10 09:36:35 btb Exp $ */
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.
17 * Hacked-in polygon objects
20 * Revision 1.3 1995/10/25 14:07:07 allender
21 * removed load_poly_model function
23 * Revision 1.2 1995/09/14 14:10:20 allender
24 * two funtions should be void
26 * Revision 1.1 1995/05/16 15:30:08 allender
29 * Revision 2.1 1995/05/26 16:10:37 john
30 * Support for new 4-byte align v8 pof files.
32 * Revision 2.0 1995/02/27 11:32:44 john
33 * New version 2.0, which has no anonymous unions, builds with
34 * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
36 * Revision 1.64 1995/01/14 19:16:43 john
37 * First version of new bitmap paging code.
39 * Revision 1.63 1994/12/14 18:06:54 matt
40 * Removed compile warnings
42 * Revision 1.62 1994/12/09 17:54:31 john
43 * Made the CFILE's close right after reading in data.
45 * Revision 1.61 1994/12/09 16:13:28 mike
46 * speedup pof file reading, but still horribly slow using hog file...problem somewhere else.
48 * Revision 1.60 1994/12/08 17:41:20 yuan
51 * Revision 1.59 1994/11/21 11:02:19 matt
52 * Added error checking
54 * Revision 1.58 1994/11/14 11:32:49 matt
55 * Allow switching to simpler models even when alt_textures specified
57 * Revision 1.57 1994/11/13 21:15:24 matt
58 * Added basic support for more than one level of detail simplification
60 * Revision 1.56 1994/11/11 19:29:25 matt
61 * Added code to show low detail polygon models
63 * Revision 1.55 1994/11/10 14:02:57 matt
64 * Hacked in support for player ships with different textures
66 * Revision 1.54 1994/11/03 11:01:59 matt
67 * Made robot pics lighted
69 * Revision 1.53 1994/11/02 16:18:34 matt
70 * Moved draw_model_picture() out of editor
72 * Revision 1.52 1994/10/18 14:38:11 matt
73 * Restored assert now that bug is fixed
75 * Revision 1.51 1994/10/17 21:35:03 matt
76 * Added support for new Control Center/Main Reactor
78 * Revision 1.50 1994/10/14 17:46:23 yuan
79 * Made the soft Int3 only work in net mode.
81 * Revision 1.49 1994/10/14 17:43:47 yuan
82 * Added soft int3's instead of Asserts for some common network bugs.
84 * Revision 1.48 1994/10/14 17:09:04 yuan
85 * Made Assert on line 610 be if in an attempt
88 * Revision 1.47 1994/09/09 14:23:42 matt
89 * Added glow code to polygon models for engine glow
91 * Revision 1.46 1994/08/26 18:03:30 matt
92 * Added code to remap polygon model numbers by matching filenames
94 * Revision 1.45 1994/08/26 15:35:58 matt
95 * Made eclips usable on more than one object at a time
97 * Revision 1.44 1994/08/25 18:11:58 matt
98 * Made player's weapons and flares fire from the positions on the 3d model.
99 * Also added support for quad lasers.
101 * Revision 1.43 1994/07/25 00:14:18 matt
102 * Made a couple of minor changes for the drivethrough
104 * Revision 1.42 1994/07/25 00:02:41 matt
105 * Various changes to accomodate new 3d, which no longer takes point numbers
106 * as parms, and now only takes pointers to points.
116 static char rcsid[] = "$Id: polyobj.c,v 1.16 2003-10-10 09:36:35 btb Exp $";
123 // -- I hate this warning in make depend! -- #ifdef DRIVE
124 // -- I hate this warning in make depend! -- #include "drive.h"
125 // -- I hate this warning in make depend! -- #else
127 // -- I hate this warning in make depend! -- #endif
137 #include "byteswap.h"
142 #include "textures.h"
144 #include "lighting.h"
149 #include "pa_enabl.h"
152 #include "3dfx_des.h"
155 polymodel Polygon_models[MAX_POLYGON_MODELS]; // = {&bot11,&bot17,&robot_s2,&robot_b2,&bot11,&bot17,&robot_s2,&robot_b2};
157 int N_polygon_models = 0;
159 #define MAX_POLYGON_VECS 1000
160 g3s_point robot_points[MAX_POLYGON_VECS];
162 #define PM_COMPATIBLE_VERSION 6
163 #define PM_OBJFILE_VERSION 8
168 #define MODEL_BUF_SIZE 32768
170 void _pof_cfseek(int len,int type)
173 case SEEK_SET: Pof_addr = len; break;
174 case SEEK_CUR: Pof_addr += len; break;
176 Assert(len <= 0); // seeking from end, better be moving back.
177 Pof_addr = Pof_file_end + len;
181 if (Pof_addr > MODEL_BUF_SIZE)
185 #define pof_cfseek(_buf,_len,_type) _pof_cfseek((_len),(_type))
187 int pof_read_int(ubyte *bufp)
191 i = *((int *) &bufp[Pof_addr]);
195 // if (cfread(&i,sizeof(i),1,f) != 1)
196 // Error("Unexpected end-of-file while reading object");
201 size_t pof_cfread(void *dst, size_t elsize, size_t nelem, ubyte *bufp)
203 if (Pof_addr + nelem*elsize > Pof_file_end)
206 memcpy(dst, &bufp[Pof_addr], elsize*nelem);
208 Pof_addr += elsize*nelem;
210 if (Pof_addr > MODEL_BUF_SIZE)
216 // #define new_read_int(i,f) cfread(&(i),sizeof(i),1,(f))
217 #define new_pof_read_int(i,f) pof_cfread(&(i),sizeof(i),1,(f))
219 short pof_read_short(ubyte *bufp)
223 s = *((short *) &bufp[Pof_addr]);
225 return INTEL_SHORT(s);
226 // if (cfread(&s,sizeof(s),1,f) != 1)
227 // Error("Unexpected end-of-file while reading object");
232 void pof_read_string(char *buf,int max_char, ubyte *bufp)
236 for (i=0; i<max_char; i++) {
237 if ((*buf++ = bufp[Pof_addr++]) == 0)
241 // while (max_char-- && (*buf=cfgetc(f)) != 0) buf++;
245 void pof_read_vecs(vms_vector *vecs,int n,ubyte *bufp)
247 // cfread(vecs,sizeof(vms_vector),n,f);
249 memcpy(vecs, &bufp[Pof_addr], n*sizeof(*vecs));
250 Pof_addr += n*sizeof(*vecs);
252 #ifdef WORDS_BIGENDIAN
254 vms_vector_swap(&vecs[--n]);
257 if (Pof_addr > MODEL_BUF_SIZE)
261 void pof_read_angs(vms_angvec *angs,int n,ubyte *bufp)
263 memcpy(angs, &bufp[Pof_addr], n*sizeof(*angs));
264 Pof_addr += n*sizeof(*angs);
266 #ifdef WORDS_BIGENDIAN
268 vms_angvec_swap(&angs[--n]);
271 if (Pof_addr > MODEL_BUF_SIZE)
275 #define ID_OHDR 0x5244484f // 'RDHO' //Object header
276 #define ID_SOBJ 0x4a424f53 // 'JBOS' //Subobject header
277 #define ID_GUNS 0x534e5547 // 'SNUG' //List of guns on this object
278 #define ID_ANIM 0x4d494e41 // 'MINA' //Animation data
279 #define ID_IDTA 0x41544449 // 'ATDI' //Interpreter data
280 #define ID_TXTR 0x52545854 // 'RTXT' //Texture filename list
283 #define robot_info void
285 vms_angvec anim_angs[N_ANIM_STATES][MAX_SUBMODELS];
287 //set the animation angles for this robot. Gun fields of robot info must
289 void robot_set_angles(robot_info *r,polymodel *pm,vms_angvec angs[N_ANIM_STATES][MAX_SUBMODELS]);
292 #define DEBUG_LEVEL CON_NORMAL
294 #ifdef WORDS_NEED_ALIGNMENT
295 ubyte * old_dest(chunk o) // return where chunk is (in unaligned struct)
297 return o.old_base + INTEL_SHORT(*((short *)(o.old_base + o.offset)));
299 ubyte * new_dest(chunk o) // return where chunk is (in aligned struct)
301 return o.new_base + INTEL_SHORT(*((short *)(o.old_base + o.offset))) + o.correction;
304 * find chunk with smallest address
306 int get_first_chunks_index(chunk *chunk_list, int no_chunks)
308 int i, first_index = 0;
309 Assert(no_chunks >= 1);
310 for (i = 1; i < no_chunks; i++)
311 if (old_dest(chunk_list[i]) < old_dest(chunk_list[first_index]))
315 #define SHIFT_SPACE 500 // increase if insufficent
317 void align_polygon_model_data(polymodel *pm)
320 int total_correction = 0;
321 ubyte *cur_old, *cur_new;
323 chunk ch_list[MAX_CHUNKS];
325 int tmp_size = pm->model_data_size + SHIFT_SPACE;
326 ubyte *tmp = d_malloc(tmp_size); // where we build the aligned version of pm->model_data
329 //start with first chunk (is always aligned!)
330 cur_old = pm->model_data;
332 chunk_len = get_chunks(cur_old, cur_new, ch_list, &no_chunks);
333 memcpy(cur_new, cur_old, chunk_len);
334 while (no_chunks > 0) {
335 int first_index = get_first_chunks_index(ch_list, no_chunks);
336 cur_ch = ch_list[first_index];
337 // remove first chunk from array:
339 for (i = first_index; i < no_chunks; i++)
340 ch_list[i] = ch_list[i + 1];
341 // if (new) address unaligned:
342 if ((u_int32_t)new_dest(cur_ch) % 4L != 0) {
343 // calculate how much to move to be aligned
344 short to_shift = 4 - (u_int32_t)new_dest(cur_ch) % 4L;
345 // correct chunks' addresses
346 cur_ch.correction += to_shift;
347 for (i = 0; i < no_chunks; i++)
348 ch_list[i].correction += to_shift;
349 total_correction += to_shift;
350 Assert((u_int32_t)new_dest(cur_ch) % 4L == 0);
351 Assert(total_correction <= SHIFT_SPACE); // if you get this, increase SHIFT_SPACE
353 //write (corrected) chunk for current chunk:
354 *((short *)(cur_ch.new_base + cur_ch.offset))
355 = INTEL_SHORT(cur_ch.correction
356 + INTEL_SHORT(*((short *)(cur_ch.old_base + cur_ch.offset))));
357 //write (correctly aligned) chunk:
358 cur_old = old_dest(cur_ch);
359 cur_new = new_dest(cur_ch);
360 chunk_len = get_chunks(cur_old, cur_new, ch_list, &no_chunks);
361 memcpy(cur_new, cur_old, chunk_len);
362 //correct submodel_ptr's for pm, too
363 for (i = 0; i < MAX_SUBMODELS; i++)
364 if (pm->model_data + pm->submodel_ptrs[i] >= cur_old
365 && pm->model_data + pm->submodel_ptrs[i] < cur_old + chunk_len)
366 pm->submodel_ptrs[i] += (cur_new - tmp) - (cur_old - pm->model_data);
368 d_free(pm->model_data);
369 pm->model_data_size += total_correction;
370 pm->model_data = d_malloc(pm->model_data_size);
371 Assert(pm->model_data != NULL);
372 memcpy(pm->model_data, tmp, pm->model_data_size);
375 #endif //def WORDS_NEED_ALIGNMENT
377 //reads a binary file containing a 3d model
378 polymodel *read_model_file(polymodel *pm,char *filename,robot_info *r)
382 int id,len, next_chunk;
386 model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
388 Error("Can't allocate space to read model %s\n", filename);
390 if ((ifile=cfopen(filename,"rb"))==NULL)
391 Error("Can't open file <%s>",filename);
393 Assert(cfilelength(ifile) <= MODEL_BUF_SIZE);
396 Pof_file_end = cfread(model_buf, 1, cfilelength(ifile), ifile);
399 id = pof_read_int(model_buf);
401 if (id!=0x4f505350) /* 'OPSP' */
402 Error("Bad ID in model file <%s>",filename);
404 version = pof_read_short(model_buf);
406 if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
407 Error("Bad version (%d) in model file <%s>",version,filename);
409 if ( FindArg( "-bspgen" ))
410 printf( "bspgen -c1" );
412 while (new_pof_read_int(id,model_buf) == 1) {
414 //id = pof_read_int(model_buf);
415 len = pof_read_int(model_buf);
416 next_chunk = Pof_addr + len;
420 case ID_OHDR: { //Object header
421 vms_vector pmmin,pmmax;
423 //con_printf(DEBUG_LEVEL, "Got chunk OHDR, len=%d\n",len);
425 pm->n_models = pof_read_int(model_buf);
426 pm->rad = pof_read_int(model_buf);
428 Assert(pm->n_models <= MAX_SUBMODELS);
430 pof_read_vecs(&pmmin,1,model_buf);
431 pof_read_vecs(&pmmax,1,model_buf);
433 if ( FindArg( "-bspgen" )) {
437 vm_vec_sub(&v, &pmmax, &pmmin );
439 if ( v.y > l ) l = v.y;
440 if ( v.z > l ) l = v.z;
442 printf( " -l%.3f", f2fl(l) );
448 case ID_SOBJ: { //Subobject header
453 //con_printf(DEBUG_LEVEL, "Got chunk SOBJ, len=%d\n",len);
455 n = pof_read_short(model_buf);
457 Assert(n < MAX_SUBMODELS);
459 pm->submodel_parents[n] = pof_read_short(model_buf);
461 pof_read_vecs(&pm->submodel_norms[n],1,model_buf);
462 pof_read_vecs(&pm->submodel_pnts[n],1,model_buf);
463 pof_read_vecs(&pm->submodel_offsets[n],1,model_buf);
465 pm->submodel_rads[n] = pof_read_int(model_buf); //radius
467 pm->submodel_ptrs[n] = pof_read_int(model_buf); //offset
474 case ID_GUNS: { //List of guns on this object
476 //con_printf(DEBUG_LEVEL, "Got chunk GUNS, len=%d\n",len);
481 ubyte gun_used[MAX_GUNS];
483 r->n_guns = pof_read_int(model_buf);
488 Assert(r->n_guns <= MAX_GUNS);
490 for (i=0;i<r->n_guns;i++)
493 for (i=0;i<r->n_guns;i++) {
496 id = pof_read_short(model_buf);
497 Assert(id < r->n_guns);
498 Assert(gun_used[id] == 0);
500 r->gun_submodels[id] = pof_read_short(model_buf);
501 Assert(r->gun_submodels[id] != 0xff);
502 pof_read_vecs(&r->gun_points[id],1,model_buf);
505 pof_read_vecs(&gun_dir,1,model_buf);
509 pof_cfseek(model_buf,len,SEEK_CUR);
514 case ID_ANIM: //Animation data
515 //con_printf(DEBUG_LEVEL, "Got chunk ANIM, len=%d\n",len);
522 n_frames = pof_read_short(model_buf);
524 Assert(n_frames == N_ANIM_STATES);
526 for (m=0;m<pm->n_models;m++)
527 for (f=0;f<n_frames;f++)
528 pof_read_angs(&anim_angs[f][m], 1, model_buf);
531 robot_set_angles(r,pm,anim_angs);
535 pof_cfseek(model_buf,len,SEEK_CUR);
540 case ID_TXTR: { //Texture filename list
544 //con_printf(DEBUG_LEVEL, "Got chunk TXTR, len=%d\n",len);
546 n = pof_read_short(model_buf);
547 //con_printf(DEBUG_LEVEL, " num textures = %d\n",n);
549 pof_read_string(name_buf,128,model_buf);
550 //con_printf(DEBUG_LEVEL, "<%s>\n",name_buf);
556 case ID_IDTA: //Interpreter data
557 //con_printf(DEBUG_LEVEL, "Got chunk IDTA, len=%d\n",len);
559 pm->model_data = d_malloc(len);
560 pm->model_data_size = len;
562 pof_cfread(pm->model_data,1,len,model_buf);
567 //con_printf(DEBUG_LEVEL, "Unknown chunk <%c%c%c%c>, len = %d\n",id,id>>8,id>>16,id>>24,len);
568 pof_cfseek(model_buf,len,SEEK_CUR);
572 if ( version >= 8 ) // Version 8 needs 4-byte alignment!!!
573 pof_cfseek(model_buf,next_chunk,SEEK_SET);
576 // for (i=0;i<pm->n_models;i++)
577 // pm->submodel_ptrs[i] += (int) pm->model_data;
579 if ( FindArg( "-bspgen" )) {
580 char *p = strchr( filename, '.' );
586 printf( " %s.3ds\n", filename );
592 #ifdef WORDS_NEED_ALIGNMENT
593 align_polygon_model_data(pm);
595 #ifdef WORDS_BIGENDIAN
596 swap_polygon_model_data(pm->model_data);
598 //verify(pm->model_data);
603 //reads the gun information for a model
604 //fills in arrays gun_points & gun_dirs, returns the number of guns read
605 int read_model_guns(char *filename,vms_vector *gun_points, vms_vector *gun_dirs, int *gun_submodels)
613 model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
615 Error("Can't allocate space to read model %s\n", filename);
617 if ((ifile=cfopen(filename,"rb"))==NULL)
618 Error("Can't open file <%s>",filename);
620 Assert(cfilelength(ifile) <= MODEL_BUF_SIZE);
623 Pof_file_end = cfread(model_buf, 1, cfilelength(ifile), ifile);
626 id = pof_read_int(model_buf);
628 if (id!=0x4f505350) /* 'OPSP' */
629 Error("Bad ID in model file <%s>",filename);
631 version = pof_read_short(model_buf);
633 Assert(version >= 7); //must be 7 or higher for this data
635 if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
636 Error("Bad version (%d) in model file <%s>",version,filename);
638 while (new_pof_read_int(id,model_buf) == 1) {
640 //id = pof_read_int(model_buf);
641 len = pof_read_int(model_buf);
643 if (id == ID_GUNS) { //List of guns on this object
645 //con_printf(DEBUG_LEVEL, "Got chunk GUNS, len=%d\n",len);
649 n_guns = pof_read_int(model_buf);
651 for (i=0;i<n_guns;i++) {
654 id = pof_read_short(model_buf);
655 sm = pof_read_short(model_buf);
657 gun_submodels[id] = sm;
659 Error("Invalid gun submodel in file <%s>",filename);
660 pof_read_vecs(&gun_points[id],1,model_buf);
662 pof_read_vecs(&gun_dirs[id],1,model_buf);
667 pof_cfseek(model_buf,len,SEEK_CUR);
676 //free up a model, getting rid of all its memory
677 void free_model(polymodel *po)
679 d_free(po->model_data);
682 grs_bitmap *texture_list[MAX_POLYOBJ_TEXTURES];
683 bitmap_index texture_list_index[MAX_POLYOBJ_TEXTURES];
685 int Simple_model_threshhold_scale=5; //switch when this times radius far away
688 //draw a polygon model
690 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[])
694 PA_DFX (int save_light);
696 Assert(model_num < N_polygon_models);
698 po=&Polygon_models[model_num];
700 //check if should use simple model
701 if (po->simpler_model ) //must have a simpler model
702 if (flags==0) //can't switch if this is debris
703 //!!if (!alt_textures) { //alternate textures might not match
704 //alt textures might not match, but in the one case we're using this
705 //for on 11/14/94, they do match. So we leave it in.
710 depth = g3_calc_point_depth(pos); //gets 3d depth
712 while (po->simpler_model && depth > cnt++ * Simple_model_threshhold_scale * po->rad)
713 po = &Polygon_models[po->simpler_model-1];
718 for (i=0;i<po->n_textures;i++) {
719 texture_list_index[i] = alt_textures[i];
720 texture_list[i] = &GameBitmaps[alt_textures[i].index];
723 texture_list[i]->bm_handle = texture_list_index[i].index;
729 for (i=0;i<po->n_textures;i++) {
730 texture_list_index[i] = ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]];
731 texture_list[i] = &GameBitmaps[ObjBitmaps[ObjBitmapPtrs[po->first_texture+i]].index];
734 texture_list[i]->bm_handle = texture_list_index[i].index;
739 #ifdef PIGGY_USE_PAGING
740 // Make sure the textures for this object are paged in...
741 piggy_page_flushed = 0;
742 for (i=0;i<po->n_textures;i++)
743 PIGGY_PAGE_IN( texture_list_index[i] );
744 // Hmmm... cache got flushed in the middle of paging all these in,
745 // so we need to reread them all in.
746 if (piggy_page_flushed) {
747 piggy_page_flushed = 0;
748 for (i=0;i<po->n_textures;i++)
749 PIGGY_PAGE_IN( texture_list_index[i] );
751 // Make sure that they can all fit in memory.
752 Assert( piggy_page_flushed == 0 );
755 g3_start_instance_matrix(pos,orient);
757 g3_set_interp_points(robot_points);
760 _3dfx_rendering_poly_obj = 1;
762 PA_DFX(save_light = Lighting_on);
763 PA_DFX(Lighting_on = 0);
765 if (flags == 0) //draw entire object
767 g3_draw_polygon_model(po->model_data,texture_list,anim_angles,light,glow_values);
772 for (i=0;flags;flags>>=1,i++)
776 Assert(i < po->n_models);
778 //if submodel, rotate around its center point, not pivot point
780 vm_vec_avg(&ofs,&po->submodel_mins[i],&po->submodel_maxs[i]);
782 g3_start_instance_matrix(&ofs,NULL);
784 g3_draw_polygon_model(&po->model_data[po->submodel_ptrs[i]],texture_list,anim_angles,light,glow_values);
793 _3dfx_rendering_poly_obj = 0;
796 PA_DFX (Lighting_on = save_light);
801 void free_polygon_models()
805 for (i=0;i<N_polygon_models;i++) {
806 free_model(&Polygon_models[i]);
811 void polyobj_find_min_max(polymodel *pm)
817 vms_vector *big_mn,*big_mx;
822 for (m=0;m<pm->n_models;m++) {
823 vms_vector *mn,*mx,*ofs;
825 mn = &pm->submodel_mins[m];
826 mx = &pm->submodel_maxs[m];
827 ofs= &pm->submodel_offsets[m];
829 data = (ushort *)&pm->model_data[pm->submodel_ptrs[m]];
833 Assert(type == 7 || type == 1);
838 data+=2; //skip start & pad
840 vp = (vms_vector *) data;
842 *mn = *mx = *vp++; nverts--;
845 *big_mn = *big_mx = *mn;
848 if (vp->x > mx->x) mx->x = vp->x;
849 if (vp->y > mx->y) mx->y = vp->y;
850 if (vp->z > mx->z) mx->z = vp->z;
852 if (vp->x < mn->x) mn->x = vp->x;
853 if (vp->y < mn->y) mn->y = vp->y;
854 if (vp->z < mn->z) mn->z = vp->z;
856 if (vp->x+ofs->x > big_mx->x) big_mx->x = vp->x+ofs->x;
857 if (vp->y+ofs->y > big_mx->y) big_mx->y = vp->y+ofs->y;
858 if (vp->z+ofs->z > big_mx->z) big_mx->z = vp->z+ofs->z;
860 if (vp->x+ofs->x < big_mn->x) big_mn->x = vp->x+ofs->x;
861 if (vp->y+ofs->y < big_mn->y) big_mn->y = vp->y+ofs->y;
862 if (vp->z+ofs->z < big_mn->z) big_mn->z = vp->z+ofs->z;
867 // printf("Submodel %d: (%8x,%8x) (%8x,%8x) (%8x,%8x)\n",m,mn->x,mx->x,mn->y,mx->y,mn->z,mx->z);
870 // 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);
874 extern short highest_texture_num; //from the 3d
876 char Pof_names[MAX_POLYGON_MODELS][FILENAME_LEN];
878 //returns the number of this model
880 int load_polygon_model(char *filename,int n_textures,int first_texture,robot_info *r)
882 int load_polygon_model(char *filename,int n_textures,grs_bitmap ***textures)
889 Assert(N_polygon_models < MAX_POLYGON_MODELS);
890 Assert(n_textures < MAX_POLYOBJ_TEXTURES);
892 // MK was real tired of those useless, slow mprintfs...
893 if (N_polygon_models > MAX_POLYGON_MODELS - 10)
894 mprintf(( 0, "Used %d/%d polygon model slots\n", N_polygon_models+1, MAX_POLYGON_MODELS ));
896 Assert(strlen(filename) <= 12);
897 strcpy(Pof_names[N_polygon_models],filename);
899 read_model_file(&Polygon_models[N_polygon_models],filename,r);
901 polyobj_find_min_max(&Polygon_models[N_polygon_models]);
903 g3_init_polygon_model(Polygon_models[N_polygon_models].model_data);
905 if (highest_texture_num+1 != n_textures)
906 Error("Model <%s> references %d textures but specifies %d.",filename,highest_texture_num+1,n_textures);
908 Polygon_models[N_polygon_models].n_textures = n_textures;
909 Polygon_models[N_polygon_models].first_texture = first_texture;
910 Polygon_models[N_polygon_models].simpler_model = 0;
912 // Assert(polygon_models[N_polygon_models]!=NULL);
916 return N_polygon_models-1;
921 void init_polygon_models()
923 N_polygon_models = 0;
925 atexit((void (*)())free_polygon_models);
929 //compare against this size when figuring how far to place eye for picture
930 #define BASE_MODEL_SIZE 0x28000
932 #define DEFAULT_VIEW_DIST 0x60000
934 //draws the given model in the current canvas. The distance is set to
935 //more-or-less fill the canvas. Note that this routine actually renders
936 //into an off-screen canvas that it creates, then copies to the current
938 void draw_model_picture(int mn,vms_angvec *orient_angles)
940 vms_vector temp_pos=ZERO_VECTOR;
941 vms_matrix temp_orient = IDENTITY_MATRIX;
942 grs_canvas *save_canv = grd_curcanv,*temp_canv;
944 Assert(mn>=0 && mn<N_polygon_models);
946 temp_canv = gr_create_canvas(save_canv->cv_bitmap.bm_w,save_canv->cv_bitmap.bm_h);
947 gr_set_current_canvas(temp_canv);
948 gr_clear_canvas( BM_XRGB(0,0,0) );
951 g3_set_view_matrix(&temp_pos,&temp_orient,0x9000);
953 if (Polygon_models[mn].rad != 0)
954 temp_pos.z = fixmuldiv(DEFAULT_VIEW_DIST,Polygon_models[mn].rad,BASE_MODEL_SIZE);
956 temp_pos.z = DEFAULT_VIEW_DIST;
958 vm_angles_2_matrix(&temp_orient, orient_angles);
960 PA_DFX(save_light = Lighting_on);
961 PA_DFX(Lighting_on = 0);
962 draw_polygon_model(&temp_pos,&temp_orient,NULL,mn,0,f1_0,NULL,NULL);
963 PA_DFX (Lighting_on = save_light);
965 gr_set_current_canvas(save_canv);
967 gr_bitmap(0,0,&temp_canv->cv_bitmap);
969 gr_free_canvas(temp_canv);
974 * reads a polymodel structure from a CFILE
976 extern void polymodel_read(polymodel *pm, CFILE *fp)
980 pm->n_models = cfile_read_int(fp);
981 pm->model_data_size = cfile_read_int(fp);
982 pm->model_data = (ubyte *) cfile_read_int(fp);
983 for (i = 0; i < MAX_SUBMODELS; i++)
984 pm->submodel_ptrs[i] = cfile_read_int(fp);
985 for (i = 0; i < MAX_SUBMODELS; i++)
986 cfile_read_vector(&(pm->submodel_offsets[i]), fp);
987 for (i = 0; i < MAX_SUBMODELS; i++)
988 cfile_read_vector(&(pm->submodel_norms[i]), fp);
989 for (i = 0; i < MAX_SUBMODELS; i++)
990 cfile_read_vector(&(pm->submodel_pnts[i]), fp);
991 for (i = 0; i < MAX_SUBMODELS; i++)
992 pm->submodel_rads[i] = cfile_read_fix(fp);
993 cfread(pm->submodel_parents, MAX_SUBMODELS, 1, fp);
994 for (i = 0; i < MAX_SUBMODELS; i++)
995 cfile_read_vector(&(pm->submodel_mins[i]), fp);
996 for (i = 0; i < MAX_SUBMODELS; i++)
997 cfile_read_vector(&(pm->submodel_maxs[i]), fp);
998 cfile_read_vector(&(pm->mins), fp);
999 cfile_read_vector(&(pm->maxs), fp);
1000 pm->rad = cfile_read_fix(fp);
1001 pm->n_textures = cfile_read_byte(fp);
1002 pm->first_texture = cfile_read_short(fp);
1003 pm->simpler_model = cfile_read_byte(fp);
1007 * reads n polymodel structs from a CFILE
1009 extern int polymodel_read_n(polymodel *pm, int n, CFILE *fp)
1013 for (i = 0; i < n; i++) {
1014 pm[i].n_models = cfile_read_int(fp);
1015 pm[i].model_data_size = cfile_read_int(fp);
1016 pm[i].model_data = (ubyte *) cfile_read_int(fp);
1017 for (j = 0; j < MAX_SUBMODELS; j++)
1018 pm[i].submodel_ptrs[j] = cfile_read_int(fp);
1019 for (j = 0; j < MAX_SUBMODELS; j++)
1020 cfile_read_vector(&(pm[i].submodel_offsets[j]), fp);
1021 for (j = 0; j < MAX_SUBMODELS; j++)
1022 cfile_read_vector(&(pm[i].submodel_norms[j]), fp);
1023 for (j = 0; j < MAX_SUBMODELS; j++)
1024 cfile_read_vector(&(pm[i].submodel_pnts[j]), fp);
1025 for (j = 0; j < MAX_SUBMODELS; j++)
1026 pm[i].submodel_rads[j] = cfile_read_fix(fp);
1027 cfread(pm[i].submodel_parents, MAX_SUBMODELS, 1, fp);
1028 for (j = 0; j < MAX_SUBMODELS; j++)
1029 cfile_read_vector(&(pm[i].submodel_mins[j]), fp);
1030 for (j = 0; j < MAX_SUBMODELS; j++)
1031 cfile_read_vector(&(pm[i].submodel_maxs[j]), fp);
1032 cfile_read_vector(&(pm[i].mins), fp);
1033 cfile_read_vector(&(pm[i].maxs), fp);
1034 pm[i].rad = cfile_read_fix(fp);
1035 pm[i].n_textures = cfile_read_byte(fp);
1036 pm[i].first_texture = cfile_read_short(fp);
1037 pm[i].simpler_model = cfile_read_byte(fp);
1045 * routine which allocates, reads, and inits a polymodel's model_data
1047 void polygon_model_data_read(polymodel *pm, CFILE *fp)
1049 pm->model_data = d_malloc(pm->model_data_size);
1050 Assert(pm->model_data != NULL);
1051 cfread(pm->model_data, sizeof(ubyte), pm->model_data_size, fp );
1052 #ifdef WORDS_NEED_ALIGNMENT
1053 align_polygon_model_data(pm);
1055 #ifdef WORDS_BIGENDIAN
1056 swap_polygon_model_data(pm->model_data);
1058 //verify(pm->model_data);
1059 g3_init_polygon_model(pm->model_data);