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.
16 static char rcsid[] = "$Id: polyobj.c,v 1.1.1.1 2001-01-19 03:30:01 bradleyb Exp $";
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
29 // -- I hate this warning in make depend! -- #endif
56 polymodel Polygon_models[MAX_POLYGON_MODELS]; // = {&bot11,&bot17,&robot_s2,&robot_b2,&bot11,&bot17,&robot_s2,&robot_b2};
58 int N_polygon_models = 0;
60 #define MAX_POLYGON_VECS 1000
61 g3s_point robot_points[MAX_POLYGON_VECS];
63 #define PM_COMPATIBLE_VERSION 6
64 #define PM_OBJFILE_VERSION 8
69 #define MODEL_BUF_SIZE 32768
71 void _pof_cfseek(int len,int type)
74 case SEEK_SET: Pof_addr = len; break;
75 case SEEK_CUR: Pof_addr += len; break;
77 Assert(len <= 0); // seeking from end, better be moving back.
78 Pof_addr = Pof_file_end + len;
82 if (Pof_addr > MODEL_BUF_SIZE)
86 #define pof_cfseek(_buf,_len,_type) _pof_cfseek((_len),(_type))
88 int pof_read_int(ubyte *bufp)
92 i = *((int *) &bufp[Pof_addr]);
96 // if (cfread(&i,sizeof(i),1,f) != 1)
97 // Error("Unexpected end-of-file while reading object");
102 size_t pof_cfread(void *dst, size_t elsize, size_t nelem, ubyte *bufp)
104 if (Pof_addr + nelem*elsize > Pof_file_end)
107 memcpy(dst, &bufp[Pof_addr], elsize*nelem);
109 Pof_addr += elsize*nelem;
111 if (Pof_addr > MODEL_BUF_SIZE)
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))
120 short pof_read_short(ubyte *bufp)
124 s = *((short *) &bufp[Pof_addr]);
127 // if (cfread(&s,sizeof(s),1,f) != 1)
128 // Error("Unexpected end-of-file while reading object");
133 void pof_read_string(char *buf,int max_char, ubyte *bufp)
137 for (i=0; i<max_char; i++) {
138 if ((*buf++ = bufp[Pof_addr++]) == 0)
142 // while (max_char-- && (*buf=cfgetc(f)) != 0) buf++;
146 void pof_read_vecs(vms_vector *vecs,int n,ubyte *bufp)
148 // cfread(vecs,sizeof(vms_vector),n,f);
150 memcpy(vecs, &bufp[Pof_addr], n*sizeof(*vecs));
151 Pof_addr += n*sizeof(*vecs);
153 if (Pof_addr > MODEL_BUF_SIZE)
157 #define ID_OHDR 'RDHO' //Object header
158 #define ID_SOBJ 'JBOS' //Subobject header
159 #define ID_GUNS 'SNUG' //List of guns on this object
160 #define ID_ANIM 'MINA' //Animation data
161 #define ID_IDTA 'ATDI' //Interpreter data
162 #define ID_TXTR 'RTXT' //Texture filename list
165 #define robot_info void
167 vms_angvec anim_angs[N_ANIM_STATES][MAX_SUBMODELS];
169 //set the animation angles for this robot. Gun fields of robot info must
171 void robot_set_angles(robot_info *r,polymodel *pm,vms_angvec angs[N_ANIM_STATES][MAX_SUBMODELS]);
174 //reads a binary file containing a 3d model
175 polymodel *read_model_file(polymodel *pm,char *filename,robot_info *r)
179 int id,len, next_chunk;
183 model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
185 Error("Can't allocate space to read model %s\n", filename);
187 if ((ifile=cfopen(filename,"rb"))==NULL)
188 Error("Can't open file <%s>",filename);
190 Assert(ifile->size <= MODEL_BUF_SIZE);
193 Pof_file_end = cfread(model_buf, 1, cfilelength(ifile), ifile);
196 id = pof_read_int(model_buf);
199 Error("Bad ID in model file <%s>",filename);
201 version = pof_read_short(model_buf);
203 if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
204 Error("Bad version (%d) in model file <%s>",version,filename);
206 if ( args_find( "-bspgen" ))
207 printf( "bspgen -c1" );
209 while (new_pof_read_int(id,model_buf) == 1) {
211 //id = pof_read_int(model_buf);
212 len = pof_read_int(model_buf);
213 next_chunk = Pof_addr + len;
217 case ID_OHDR: { //Object header
218 vms_vector pmmin,pmmax;
220 //mprintf(0,"Got chunk OHDR, len=%d\n",len);
222 pm->n_models = pof_read_int(model_buf);
223 pm->rad = pof_read_int(model_buf);
225 Assert(pm->n_models <= MAX_SUBMODELS);
227 pof_read_vecs(&pmmin,1,model_buf);
228 pof_read_vecs(&pmmax,1,model_buf);
230 if ( args_find( "-bspgen" )) {
234 vm_vec_sub(&v, &pmmax, &pmmin );
236 if ( v.y > l ) l = v.y;
237 if ( v.z > l ) l = v.z;
239 printf( " -l%.3f", f2fl(l) );
245 case ID_SOBJ: { //Subobject header
250 //mprintf(0,"Got chunk SOBJ, len=%d\n",len);
252 n = pof_read_short(model_buf);
254 Assert(n < MAX_SUBMODELS);
256 pm->submodel_parents[n] = pof_read_short(model_buf);
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);
262 pm->submodel_rads[n] = pof_read_int(model_buf); //radius
264 pm->submodel_ptrs[n] = pof_read_int(model_buf); //offset
271 case ID_GUNS: { //List of guns on this object
273 //mprintf(0,"Got chunk GUNS, len=%d\n",len);
278 ubyte gun_used[MAX_GUNS];
280 r->n_guns = pof_read_int(model_buf);
285 Assert(r->n_guns <= MAX_GUNS);
287 for (i=0;i<r->n_guns;i++)
290 for (i=0;i<r->n_guns;i++) {
293 id = pof_read_short(model_buf);
294 Assert(id < r->n_guns);
295 Assert(gun_used[id] == 0);
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);
302 pof_read_vecs(&gun_dir,1,model_buf);
306 pof_cfseek(model_buf,len,SEEK_CUR);
311 case ID_ANIM: //Animation data
312 //mprintf(0,"Got chunk ANIM, len=%d\n",len);
319 n_frames = pof_read_short(model_buf);
321 Assert(n_frames == N_ANIM_STATES);
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);
327 robot_set_angles(r,pm,anim_angs);
331 pof_cfseek(model_buf,len,SEEK_CUR);
336 case ID_TXTR: { //Texture filename list
340 //mprintf(0,"Got chunk TXTR, len=%d\n",len);
342 n = pof_read_short(model_buf);
343 //mprintf(0," num textures = %d\n",n);
345 pof_read_string(name_buf,128,model_buf);
346 //mprintf(0,"<%s>\n",name_buf);
352 case ID_IDTA: //Interpreter data
353 //mprintf(0,"Got chunk IDTA, len=%d\n",len);
355 pm->model_data = d_malloc(len);
356 pm->model_data_size = len;
358 pof_cfread(pm->model_data,1,len,model_buf);
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);
368 if ( version >= 8 ) // Version 8 needs 4-byte alignment!!!
369 pof_cfseek(model_buf,next_chunk,SEEK_SET);
372 // for (i=0;i<pm->n_models;i++)
373 // pm->submodel_ptrs[i] += (int) pm->model_data;
375 if ( args_find( "-bspgen" )) {
376 char *p = strchr( filename, '.' );
382 printf( " %s.3ds\n", filename );
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)
401 model_buf = (ubyte *)d_malloc( MODEL_BUF_SIZE * sizeof(ubyte) );
403 Error("Can't allocate space to read model %s\n", filename);
405 if ((ifile=cfopen(filename,"rb"))==NULL)
406 Error("Can't open file <%s>",filename);
408 Assert(ifile->size <= MODEL_BUF_SIZE);
411 Pof_file_end = cfread(model_buf, 1, ifile->size, ifile);
414 id = pof_read_int(model_buf);
417 Error("Bad ID in model file <%s>",filename);
419 version = pof_read_short(model_buf);
421 Assert(version >= 7); //must be 7 or higher for this data
423 if (version < PM_COMPATIBLE_VERSION || version > PM_OBJFILE_VERSION)
424 Error("Bad version (%d) in model file <%s>",version,filename);
426 while (new_pof_read_int(id,model_buf) == 1) {
428 //id = pof_read_int(model_buf);
429 len = pof_read_int(model_buf);
431 if (id == ID_GUNS) { //List of guns on this object
433 //mprintf(0,"Got chunk GUNS, len=%d\n",len);
437 n_guns = pof_read_int(model_buf);
439 for (i=0;i<n_guns;i++) {
442 id = pof_read_short(model_buf);
443 sm = pof_read_short(model_buf);
445 gun_submodels[id] = sm;
447 Error("Invalid gun submodel in file <%s>",filename);
448 pof_read_vecs(&gun_points[id],1,model_buf);
450 pof_read_vecs(&gun_dirs[id],1,model_buf);
455 pof_cfseek(model_buf,len,SEEK_CUR);
464 //free up a model, getting rid of all its memory
465 void free_model(polymodel *po)
467 d_free(po->model_data);
470 grs_bitmap *texture_list[MAX_POLYOBJ_TEXTURES];
471 bitmap_index texture_list_index[MAX_POLYOBJ_TEXTURES];
473 int Simple_model_threshhold_scale=5; //switch when this times radius far away
476 //draw a polygon model
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[])
482 PA_DFX (int save_light);
484 Assert(model_num < N_polygon_models);
486 po=&Polygon_models[model_num];
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.
498 depth = g3_calc_point_depth(pos); //gets 3d depth
500 while (po->simpler_model && depth > cnt++ * Simple_model_threshhold_scale * po->rad)
501 po = &Polygon_models[po->simpler_model-1];
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];
511 texture_list[i]->bm_handle = texture_list_index[i].index;
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];
522 texture_list[i]->bm_handle = texture_list_index[i].index;
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] );
539 // Make sure that they can all fit in memory.
540 Assert( piggy_page_flushed == 0 );
543 g3_start_instance_matrix(pos,orient);
545 g3_set_interp_points(robot_points);
548 _3dfx_rendering_poly_obj = 1;
550 PA_DFX(save_light = Lighting_on);
551 PA_DFX(Lighting_on = 0);
553 if (flags == 0) //draw entire object
555 g3_draw_polygon_model(po->model_data,texture_list,anim_angles,light,glow_values);
560 for (i=0;flags;flags>>=1,i++)
564 Assert(i < po->n_models);
566 //if submodel, rotate around its center point, not pivot point
568 vm_vec_avg(&ofs,&po->submodel_mins[i],&po->submodel_maxs[i]);
570 g3_start_instance_matrix(&ofs,NULL);
572 g3_draw_polygon_model(&po->model_data[po->submodel_ptrs[i]],texture_list,anim_angles,light,glow_values);
581 _3dfx_rendering_poly_obj = 0;
584 PA_DFX (Lighting_on = save_light);
589 void free_polygon_models()
593 for (i=0;i<N_polygon_models;i++) {
594 free_model(&Polygon_models[i]);
599 void polyobj_find_min_max(polymodel *pm)
605 vms_vector *big_mn,*big_mx;
610 for (m=0;m<pm->n_models;m++) {
611 vms_vector *mn,*mx,*ofs;
613 mn = &pm->submodel_mins[m];
614 mx = &pm->submodel_maxs[m];
615 ofs= &pm->submodel_offsets[m];
617 data = (ushort *)&pm->model_data[pm->submodel_ptrs[m]];
621 Assert(type == 7 || type == 1);
626 data+=2; //skip start & pad
628 vp = (vms_vector *) data;
630 *mn = *mx = *vp++; nverts--;
633 *big_mn = *big_mx = *mn;
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;
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;
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;
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;
655 // printf("Submodel %d: (%8x,%8x) (%8x,%8x) (%8x,%8x)\n",m,mn->x,mx->x,mn->y,mx->y,mn->z,mx->z);
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);
662 extern short highest_texture_num; //from the 3d
664 char Pof_names[MAX_POLYGON_MODELS][FILENAME_LEN];
666 //returns the number of this model
668 int load_polygon_model(char *filename,int n_textures,int first_texture,robot_info *r)
670 int load_polygon_model(char *filename,int n_textures,grs_bitmap ***textures)
677 Assert(N_polygon_models < MAX_POLYGON_MODELS);
678 Assert(n_textures < MAX_POLYOBJ_TEXTURES);
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 ));
684 Assert(strlen(filename) <= 12);
685 strcpy(Pof_names[N_polygon_models],filename);
687 read_model_file(&Polygon_models[N_polygon_models],filename,r);
689 polyobj_find_min_max(&Polygon_models[N_polygon_models]);
691 g3_init_polygon_model(Polygon_models[N_polygon_models].model_data);
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);
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;
700 // Assert(polygon_models[N_polygon_models]!=NULL);
704 return N_polygon_models-1;
709 void init_polygon_models()
711 N_polygon_models = 0;
713 atexit((void (*)())free_polygon_models);
717 //compare against this size when figuring how far to place eye for picture
718 #define BASE_MODEL_SIZE 0x28000
720 #define DEFAULT_VIEW_DIST 0x60000
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
726 void draw_model_picture(int mn,vms_angvec *orient_angles)
728 vms_vector temp_pos=ZERO_VECTOR;
729 vms_matrix temp_orient = IDENTITY_MATRIX;
730 grs_canvas *save_canv = grd_curcanv,*temp_canv;
732 Assert(mn>=0 && mn<N_polygon_models);
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) );
739 g3_set_view_matrix(&temp_pos,&temp_orient,0x9000);
741 if (Polygon_models[mn].rad != 0)
742 temp_pos.z = fixmuldiv(DEFAULT_VIEW_DIST,Polygon_models[mn].rad,BASE_MODEL_SIZE);
744 temp_pos.z = DEFAULT_VIEW_DIST;
746 vm_angles_2_matrix(&temp_orient, orient_angles);
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);
753 gr_set_current_canvas(save_canv);
755 gr_bitmap(0,0,&temp_canv->cv_bitmap);
757 gr_free_canvas(temp_canv);