2 * $Logfile: /Freespace2/code/Model/ModelInterp.cpp $
7 * Rendering models, I think.
10 * Revision 1.2 2002/05/07 03:16:46 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:10 root
17 * 37 9/13/99 11:25p Dave
18 * Fixed problem with mode-switching and D3D movies.
20 * 36 9/13/99 10:09a Andsager
21 * Add debug console commands to lower model render detail and fireball
22 * LOD for big ship explosiosns.
24 * 35 9/08/99 12:03a Dave
25 * Make squad logos render properly in D3D all the time. Added intel anim
28 * 34 9/01/99 10:09a Dave
31 * 33 8/30/99 5:01p Dave
32 * Made d3d do less state changing in the nebula. Use new chat server for
35 * 32 8/24/99 8:55p Dave
36 * Make sure nondimming pixels work properly in tech menu.
38 * 31 7/29/99 10:47p Dave
39 * Standardized D3D fogging using vertex fog. Shook out Savage 4 bugs.
41 * 30 7/29/99 12:05a Dave
42 * Nebula speed optimizations.
44 * 29 7/27/99 3:09p Dave
45 * Made g400 work. Whee.
47 * 28 7/24/99 4:19p Dave
48 * Fixed dumb code with briefing bitmaps. Made d3d zbuffer work much
49 * better. Made model code use zbuffer more intelligently.
51 * 27 7/19/99 7:20p Dave
52 * Beam tooling. Specialized player-killed-self messages. Fixed d3d nebula
55 * 26 7/18/99 5:20p Dave
56 * Jump node icon. Fixed debris fogging. Framerate warning stuff.
58 * 25 7/15/99 2:13p Dave
59 * Added 32 bit detection.
61 * 24 6/22/99 7:03p Dave
62 * New detail options screen.
64 * 23 6/18/99 5:16p Dave
65 * Added real beam weapon lighting. Fixed beam weapon sounds. Added MOTD
66 * dialog to PXO screen.
68 * 22 5/26/99 11:46a Dave
69 * Added ship-blasting lighting and made the randomization of lighting
70 * much more customizable.
72 * 21 5/24/99 5:45p Dave
73 * Added detail levels to the nebula, with a decent speedup. Split nebula
74 * lightning into its own section.
76 * 20 5/12/99 10:05a Johnson
77 * DKA: Fix fred bug from engine wash
79 * 19 5/08/99 8:25p Dave
80 * Upped object pairs. First run of nebula lightning.
82 * 18 4/26/99 8:49p Dave
83 * Made all pof based nebula stuff full customizable through fred.
85 * 17 4/23/99 5:53p Dave
86 * Started putting in new pof nebula support into Fred.
88 * 16 4/20/99 3:30p Andsager
89 * Let get_model_closest_box_point_with_delta() take NULL as pointer to
92 * 15 4/19/99 12:51p Andsager
93 * Add function to find the nearest point on extneded bounding box and
94 * check if inside bounding box.
96 * 14 3/31/99 8:24p Dave
97 * Beefed up all kinds of stuff, incluging beam weapons, nebula effects
98 * and background nebulae. Added per-ship non-dimming pixel colors.
100 * 13 3/24/99 6:14p Dave
101 * Added position and orientation checksumming for multiplayer. Fixed LOD
102 * rendering bugs for squad insignias
104 * 12 3/23/99 5:17p Dave
105 * Changed model file format somewhat to account for LOD's on insignias
107 * 11 3/19/99 9:51a Dave
108 * Checkin to repair massive source safe crash. Also added support for
109 * pof-style nebulae, and some new weapons code.
111 * 10 3/08/99 7:03p Dave
112 * First run of new object update system. Looks very promising.
114 * 9 3/02/99 9:25p Dave
115 * Added a bunch of model rendering debug code. Started work on fixing
116 * beam weapon wacky firing.
118 * 8 2/19/99 11:42a Dave
119 * Put in model rendering autocentering.
121 * 7 1/14/99 6:06p Dave
122 * 100% full squad logo support for single player and multiplayer.
124 * 6 1/08/99 2:08p Dave
125 * Fixed software rendering for pofview. Super early support for AWACS and
128 * 5 1/06/99 2:24p Dave
129 * Stubs and release build fixes.
131 * 4 12/09/98 7:34p Dave
132 * Cleanup up nebula effect. Tweaked many values.
134 * 3 12/06/98 2:36p Dave
135 * Drastically improved nebula fogging.
137 * 2 10/07/98 10:53a Dave
140 * 1 10/07/98 10:50a Dave
142 * 193 8/28/98 3:29p Dave
143 * EMP effect done. AI effects may need some tweaking as required.
145 * 192 6/13/98 3:18p Hoffoss
146 * NOX()ed out a bunch of strings that shouldn't be translated.
148 * 191 5/24/98 4:53p John
149 * changed rounding for model caching to get rid of some empty lines in
152 * 190 5/16/98 4:47p John
153 * Put back in my version 188 changes that someone so rudely (but
154 * hopefully unintentionally) deleted.
156 * 189 5/13/98 11:34p Mike
157 * Model caching system.
159 * 187 5/12/98 11:32a Mike
160 * Support for weapon pof detail levels.
162 * 186 5/08/98 1:32p John
163 * Added code for using two layered subspace effects.
165 * 185 4/29/98 11:03a John
166 * Added code to show octants.
168 * 184 4/22/98 9:58p John
169 * Added code to view invisible faces.
171 * 183 4/22/98 9:43p John
172 * Added code to allow checking of invisible faces, flagged by any texture
173 * name with invisible in it.
175 * 182 4/20/98 4:44p John
176 * Fixed problems with black being xparent on model cache rneders. Made
177 * model cache key off of detail level setting and framerate.
179 * 181 4/18/98 4:00p John
180 * Made highest model caching detail level disable model caching.
182 * 180 4/14/98 11:11p John
183 * Made ships with < 50% hull left show electrical damage arcs.
185 * 179 4/13/98 4:54p John
186 * Made uv rotate independently on subspace effect. Put in DCF function
187 * for setting subspace speeds.
189 * 178 4/12/98 5:54p John
190 * Made models work with subspace. Made subspace rotate also.
192 * 177 4/12/98 9:56a John
193 * Made lighting detail flags work. Made explosions cast light on
196 * 176 4/11/98 6:53p John
197 * Added first rev of subspace effect.
199 * 175 4/10/98 5:20p John
200 * Changed RGB in lighting structure to be ubytes. Removed old
201 * not-necessary 24 bpp software stuff.
203 * 174 4/09/98 11:40p John
204 * Fixed some bugs with hardware lighting.
206 * 173 4/09/98 4:38p John
207 * Made non-darkening and transparent textures work under Glide. Fixed
208 * bug with Jim's computer not drawing any bitmaps.
221 #include "floating.h"
223 #include "lighting.h"
224 #include "modelsinc.h"
225 #include "fireballs.h"
228 #include "systemvars.h"
230 #include "3dinternal.h"
232 #include "grinternal.h"
234 #include "object.h" // For MAX_OBJECTS
235 #include "missionparse.h"
239 // Some debug variables used externally for displaying stats
241 int modelstats_num_polys = 0;
242 int modelstats_num_polys_drawn = 0;
243 int modelstats_num_verts = 0;
244 int modelstats_num_sortnorms = 0;
245 int modelstats_num_boxes = 0;
249 typedef struct model_light_object {
250 ubyte r[MAX_POLYGON_NORMS];
251 ubyte g[MAX_POLYGON_NORMS];
252 ubyte b[MAX_POLYGON_NORMS];
256 } model_light_object;
258 // -----------------------
262 // Vertices used internally to rotate model points
263 static vertex Interp_points[MAX_POLYGON_VECS];
264 vector *Interp_verts[MAX_POLYGON_VECS];
265 static int Interp_num_verts;
268 // -------------------------------------------------------------------
269 // lighting save stuff
271 #define MAX_MODEL_LIGHTING_SAVE 30
272 int hack_skip_max = 1;
276 hack_skip_max = Dc_arg_int;
278 // model_light_object Interp_lighting_save[MAX_MODEL_LIGHTING_SAVE];
279 model_light_object Interp_lighting_temp;
280 model_light_object *Interp_lighting = &Interp_lighting_temp;
281 int Interp_use_saved_lighting = 0;
282 int Interp_saved_lighting_full = 0;
284 // lighting save stuff
285 // -------------------------------------------------------------------
288 static ubyte Interp_light_applied[MAX_POLYGON_NORMS];
289 static vector *Interp_norms[MAX_POLYGON_NORMS];
290 static int Interp_num_norms = 0;
291 static ubyte *Interp_lights;
293 static float Interp_fog_level = 0.0f;
295 // Stuff to control rendering parameters
296 static color Interp_outline_color;
297 static int Interp_detail_level = 0;
298 static uint Interp_flags = 0;
299 static uint Interp_tmap_flags = 0;
301 // If non-zero, then the subobject gets scaled by Interp_thrust_scale.
302 static int Interp_thrust_scale_subobj=0;
303 static float Interp_thrust_scale = 0.1f;
304 static int Interp_thrust_bitmap = -1;
305 static int Interp_thrust_glow_bitmap = -1;
306 static float Interp_thrust_glow_noise = 1.0f;
308 static int Interp_objnum = -1;
310 // if != -1, use this bitmap when rendering ship insignias
311 static int Interp_insignia_bitmap = -1;
313 // if != -1, use this bitmap when rendering with a forced texture
314 static int Interp_forced_bitmap = -1;
316 // for rendering with the MR_ALL_XPARENT FLAG SET
317 static float Interp_xparent_alpha = 1.0f;
319 float Interp_light = 0.0f;
321 // forward references
322 int model_interp_sub(void *model_ptr, polymodel * pm, bsp_info *sm, int do_box_check );
324 // call at the beginning of a level. after the level has been loaded
325 void model_level_post_init()
330 // reset lighting stuff
331 for(idx=0; idx<MAX_MODEL_LIGHTING_SAVE; idx++){
332 Interp_lighting_save[idx].objnum = -1;
333 Interp_lighting_save[idx].skip = 0;
336 // saved lighting is not full
337 Interp_saved_lighting_full = 0;
341 // call to select an object for using "saved" lighting
342 void model_set_saved_lighting(int objnum, int skip_max)
347 // always set to not using saved light to start with
348 Interp_use_saved_lighting = 0;
349 Interp_lighting = &Interp_lighting_temp;
351 // if he passed a -1 for either value, no saved lighting
352 if((objnum == -1) || (skip_max == -1)){
356 // see if the object is already on the list
357 for(idx=0; idx<MAX_MODEL_LIGHTING_SAVE; idx++){
358 // ahha, he is on the list
359 if(Interp_lighting_save[idx].objnum == objnum){
360 // if he's entered a new skip max
361 if(Interp_lighting_save[idx].skip_max != skip_max){
362 Interp_lighting_save[idx].skip = 0;
363 Interp_lighting_save[idx].skip_max = skip_max;
364 Interp_use_saved_lighting = 0;
365 Interp_lighting = &Interp_lighting_save[idx];
367 // if we're reached the "skip" frame, re-render lighting for this guy
368 else if(Interp_lighting_save[idx].skip == Interp_lighting_save[idx].skip_max){
369 Interp_lighting_save[idx].skip = 0;
370 Interp_use_saved_lighting = 0;
371 Interp_lighting = &Interp_lighting_save[idx];
373 // otherwise, use his saved lighting values
375 Interp_lighting_save[idx].skip++;
376 Interp_use_saved_lighting = 1;
377 Interp_lighting = &Interp_lighting_save[idx];
385 // no free saved lighting slots
386 if(Interp_saved_lighting_full){
392 for(idx=0; idx<MAX_MODEL_LIGHTING_SAVE; idx++){
394 if(Interp_lighting_save[idx].objnum == -1){
395 Interp_lighting_save[idx].objnum = objnum;
396 Interp_lighting_save[idx].skip = 0;
397 Interp_lighting_save[idx].skip_max = skip_max;
399 Interp_use_saved_lighting = 0;
400 Interp_lighting = &Interp_lighting_save[idx];
407 // oops. out of free slots
409 Interp_saved_lighting_full = 1;
414 // notify the model system that a ship has died
415 void model_notify_dead_ship(int objnum)
420 // see if this guy was on the lighting list
421 for(idx=0; idx<MAX_MODEL_LIGHTING_SAVE; idx++){
423 if(objnum == Interp_lighting_save[idx].objnum){
424 Interp_lighting_save[idx].objnum = -1;
425 Interp_saved_lighting_full = 0;
432 void interp_clear_instance()
434 Interp_thrust_scale = 0.1f;
435 Interp_thrust_bitmap = -1;
436 Interp_thrust_glow_bitmap = -1;
437 Interp_thrust_glow_noise = 1.0f;
438 Interp_insignia_bitmap = -1;
441 // Scales the engines thrusters by this much
442 void model_set_thrust( int model_num, float length, int bitmap, int glow_bitmap, float glow_noise )
444 Interp_thrust_scale = length;
445 Interp_thrust_bitmap = bitmap;
446 Interp_thrust_glow_bitmap = glow_bitmap;
447 Interp_thrust_glow_noise = glow_noise;
449 if ( Interp_thrust_scale < 0.1f ) {
450 Interp_thrust_scale = 0.1f;
451 } else if ( Interp_thrust_scale > 1.0f ) {
452 Interp_thrust_scale = 1.0f;
455 polymodel * pm = model_get( model_num );
458 // If thrust is set up, use it.
459 for (i=0; i<pm->num_lights; i++ ) {
460 if ( pm->lights[i].type == BSP_LIGHT_TYPE_THRUSTER ) {
461 float scale = (Interp_thrust_scale-0.1f)*0.5f;
463 pm->lights[i].value += (scale+Interp_thrust_glow_noise*0.2f) / 255.0f;
474 // +16 int offset from start of chunk to vertex data
475 // +20 n_verts*char norm_counts
476 // +offset vertex data. Each vertex n is a point followed by norm_counts[n] normals.
477 void model_interp_defpoints(ubyte * p, polymodel *pm, bsp_info *sm)
481 int offset = w(p+16);
484 ubyte * normcount = p+20;
485 vertex *dest = Interp_points;
486 vector *src = vp(p+offset);
488 // Get pointer to lights
489 Interp_lights = p+20+nverts;
491 Assert( nverts < MAX_POLYGON_VECS );
492 // Assert( nnorms < MAX_POLYGON_NORMS );
494 Interp_num_verts = nverts;
496 modelstats_num_verts += nverts;
500 static int Max_vecs = 0;
501 static int Max_norms = 0;
503 if ( Max_vecs < nverts ) {
505 mprintf(( "MAX NORMS = %d\n", Max_norms ));
506 mprintf(( "MAX VECS = %d\n", Max_vecs ));
509 if ( Max_norms < nnorms ) {
511 mprintf(( "MAX NORMS = %d\n", Max_norms ));
512 mprintf(( "MAX VECS = %d\n", Max_vecs ));
516 if (Interp_thrust_scale_subobj) {
518 // Only scale vertices that aren't on the "base" of
519 // the effect. Base is something Adam decided to be
520 // anything under 1.5 meters, hence the 1.5f.
521 float min_thruster_dist = -1.5f;
523 if ( Interp_flags & MR_IS_MISSILE ) {
524 min_thruster_dist = 0.5f;
527 for (n=0; n<nverts; n++ ) {
530 Interp_verts[n] = src;
532 // Only scale vertices that aren't on the "base" of
533 // the effect. Base is something Adam decided to be
534 // anything under 1.5 meters, hence the 1.5f.
535 if ( src->z < min_thruster_dist ) {
536 tmp.x = src->x * 1.0f;
537 tmp.y = src->y * 1.0f;
538 tmp.z = src->z * Interp_thrust_scale;
543 g3_rotate_vertex(dest,&tmp);
545 src++; // move to normal
547 for (i=0; i<normcount[n]; i++ ) {
548 Interp_light_applied[next_norm] = 0;
549 Interp_norms[next_norm] = src;
558 for (n=0; n<nverts; n++ ) {
559 Interp_verts[n] = src;
564 if(Interp_thrust_twist > 0.0f){
568 // determine theta for this vertex
569 theta = fl_radian(20.0f + Interp_thrust_twist2);
574 tmp.z = (src->z * ct) - (src->y * st);
575 tmp.y = (src->z * st) + (src->y * ct);
578 tmp.z += Interp_thrust_twist;
581 g3_rotate_vertex(dest, &tmp);
584 g3_rotate_vertex(dest, src);
586 src++; // move to normal
588 for (i=0; i<normcount[n]; i++ ) {
589 Interp_light_applied[next_norm] = 0;
590 Interp_norms[next_norm] = src;
599 Interp_num_norms = next_norm;
603 matrix *Interp_orient;
607 void interp_compute_environment_mapping( vector *nrm, vertex * pnt, vector *vert)
611 matrix * m = &View_matrix;
613 vm_vec_rotate( &R, nrm, &View_matrix );
614 vm_vec_normalize(&R);
617 R.x = a * R.x; // reflected R = 2N(N.E) -E; E = eye
620 vm_vec_normalize(&R);
621 a = (float)fl_sqrt( 1.0f - R.y * R.y);
622 pnt->u = (float)atan2( R.x, -R.z) / (2.0f * 3.14159f);
623 if (pnt->u < 0.0) pnt->u += 1.0f;
624 pnt->v = 1.0f - (float)atan2( a, R.y) / 3.14159f;
640 // +44 nverts*short*short vertlist, smoothlist
641 void model_interp_flatpoly(ubyte * p,polymodel * pm)
643 vertex *Interp_list[TMAP_MAX_VERTS];
646 if ( nv < 0 ) return;
649 modelstats_num_polys++;
652 if (!g3_check_normal_facing(vp(p+20),vp(p+8)) ) return;
656 short * verts = (short *)(p+44);
659 Interp_list[i] = &Interp_points[verts[i*2]];
661 if ( Interp_flags & MR_NO_LIGHTING ) {
663 Interp_list[i]->r = 191;
664 Interp_list[i]->g = 191;
665 Interp_list[i]->b = 191;
667 Interp_list[i]->b = 191;
670 int vertnum = verts[i*2+0];
671 int norm = verts[i*2+1];
673 if ( Interp_flags & MR_NO_SMOOTHING ) {
675 light_apply_rgb( &Interp_list[i]->r, &Interp_list[i]->g, &Interp_list[i]->b, Interp_verts[vertnum], vp(p+8), Interp_light );
677 Interp_list[i]->b = light_apply( Interp_verts[vertnum], vp(p+8), Interp_light );
680 // if we're not using saved lighting
681 if ( !Interp_use_saved_lighting && !Interp_light_applied[norm] ) {
683 light_apply_rgb( &Interp_lighting->r[norm], &Interp_lighting->g[norm], &Interp_lighting->b[norm], Interp_verts[vertnum], vp(p+8), Interp_light );
685 Interp_lighting->b[norm] = light_apply( Interp_verts[vertnum], Interp_norms[norm], Interp_light );
687 Interp_light_applied[norm] = 1;
691 Interp_list[i]->r = Interp_lighting->r[norm];
692 Interp_list[i]->g = Interp_lighting->g[norm];
693 Interp_list[i]->b = Interp_lighting->b[norm];
695 Interp_list[i]->b = Interp_lighting->b[norm];
701 // HACK!!! FIX ME!!! I'M SLOW!!!!
702 if ( !(Interp_flags & MR_SHOW_OUTLINE_PRESET) ) {
703 gr_set_color( *(p+40), *(p+41), *(p+42) );
706 if ( !(Interp_flags & MR_NO_POLYS)) {
708 g3_draw_poly( nv, Interp_list, TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB );
710 g3_draw_poly( nv, Interp_list, TMAP_FLAG_GOURAUD | TMAP_FLAG_RAMP );
714 if (Interp_flags & (MR_SHOW_OUTLINE|MR_SHOW_OUTLINE_PRESET)) {
717 if ( Interp_flags & MR_SHOW_OUTLINE ) {
718 gr_set_color_fast( &Interp_outline_color );
721 for (i=0; i<nv; i++ ) {
723 g3_draw_line(Interp_list[i], Interp_list[j] );
736 // +44 nverts*(model_tmap_vert) vertlist (n,u,v)
737 extern int Tmap_show_layers;
739 int Interp_subspace = 0;
740 float Interp_subspace_offset_u = 0.0f;
741 float Interp_subspace_offset_v = 0.0f;
742 ubyte Interp_subspace_r = 255;
743 ubyte Interp_subspace_g = 255;
744 ubyte Interp_subspace_b = 255;
746 void model_interp_tmappoly(ubyte * p,polymodel * pm)
748 vertex *Interp_list[TMAP_MAX_VERTS];
751 model_tmap_vert *verts;
753 int is_invisible = 0;
755 if ((!Interp_thrust_scale_subobj) && (pm->textures[w(p+40)]<0)) {
756 // Don't draw invisible polygons.
757 if ( !(Interp_flags & MR_SHOW_INVISIBLE_FACES)) {
766 // Tmap_show_layers = 1;
769 modelstats_num_polys++;
772 if (!g3_check_normal_facing(vp(p+20),vp(p+8)) && !(Interp_flags & MR_NO_CULL)) return;
774 if ( nv < 0 ) return;
776 verts = (model_tmap_vert *)(p+44);
779 Interp_list[i] = &Interp_points[verts[i].vertnum];
781 Interp_list[i]->u = verts[i].u;
782 Interp_list[i]->v = verts[i].v;
784 if ( Interp_subspace ) {
785 Interp_list[i]->v += Interp_subspace_offset_u;
786 Interp_list[i]->u += Interp_subspace_offset_v;
787 Interp_list[i]->r = Interp_subspace_r;
788 Interp_list[i]->g = Interp_subspace_g;
789 Interp_list[i]->b = Interp_subspace_b;
792 // if ( !(pm->flags & PM_FLAG_ALLOW_TILING) ) {
793 // Assert(verts[i].u <= 1.0f );
794 // Assert(verts[i].v <= 1.0f );
797 // Assert( verts[i].normnum == verts[i].vertnum );
799 if ( Interp_flags & MR_NO_LIGHTING ) {
801 Interp_list[i]->r = 191;
802 Interp_list[i]->g = 191;
803 Interp_list[i]->b = 191;
805 Interp_list[i]->b = 191;
808 int vertnum = verts[i].vertnum;
809 int norm = verts[i].normnum;
811 if ( Interp_flags & MR_NO_SMOOTHING ) {
813 light_apply_rgb( &Interp_list[i]->r, &Interp_list[i]->g, &Interp_list[i]->b, Interp_verts[vertnum], vp(p+8), Interp_light );
815 Interp_list[i]->b = light_apply( Interp_verts[vertnum], vp(p+8), Interp_light );
818 // if we're applying lighting as normal, and not using saved lighting
819 if ( !Interp_use_saved_lighting && !Interp_light_applied[norm] ) {
822 light_apply_rgb( &Interp_lighting->r[norm], &Interp_lighting->g[norm], &Interp_lighting->b[norm], Interp_verts[vertnum], Interp_norms[norm], Interp_light );
827 l = light_apply( Interp_verts[vertnum], Interp_norms[norm], Interp_light );
830 if ( Detail.lighting > 1 ) {
831 // Add in precalculated muzzle flashes
832 float fl = i2fl(l)/255.0f;
833 ubyte *tmp = &Interp_lights[norm*pm->num_lights];
835 for ( li=0; li<pm->num_lights; li++ ) {
836 fl += i2fl(tmp[li])*pm->lights[li].value;
841 } else if ( fl > 1.0f ) {
845 l = (ubyte)fl2i(fl*255.0f);
849 Interp_lighting->b[norm] = l;
853 Interp_light_applied[norm] = 1;
857 Interp_list[i]->r = Interp_lighting->r[norm];
858 Interp_list[i]->g = Interp_lighting->g[norm];
859 Interp_list[i]->b = Interp_lighting->b[norm];
861 Interp_list[i]->b = Interp_lighting->b[norm];
867 // Assert(verts[i].u >= 0.0f );
868 // Assert(verts[i].v >= 0.0f );
872 modelstats_num_polys_drawn++;
875 if (!(Interp_flags & MR_NO_POLYS) ) {
876 if ( is_invisible ) {
877 gr_set_color( 0, 255, 0 );
878 g3_draw_poly( nv, Interp_list, 0 );
879 } else if (Interp_thrust_scale_subobj) {
880 if ((Interp_thrust_bitmap>-1) && (Interp_thrust_scale > 0.0f) && !Pofview_running) {
881 gr_set_bitmap( Interp_thrust_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 1.2f );
882 g3_draw_poly( nv, Interp_list, TMAP_FLAG_TEXTURED );
883 } else if(!Pofview_running){
884 if ( !(Interp_flags & MR_SHOW_OUTLINE_PRESET) ) {
885 gr_set_color( 128, 128, 255 );
887 uint tflags = Interp_tmap_flags;
888 tflags &= (~(TMAP_FLAG_TEXTURED|TMAP_FLAG_TILED|TMAP_FLAG_CORRECT));
889 g3_draw_poly( nv, Interp_list, tflags );
892 // all textured polys go through here
893 if ( Interp_tmap_flags & TMAP_FLAG_TEXTURED ) {
894 // subspace special case
895 if ( Interp_subspace && D3D_enabled ) {
896 gr_set_bitmap( pm->textures[w(p+40)], GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, 1.2f );
898 // all other textures
902 // if we're rendering a nebula background pof, maybe select a custom texture
903 if((Interp_flags & MR_FORCE_TEXTURE) && (Interp_forced_bitmap >= 0)){
904 texture = Interp_forced_bitmap;
906 texture = pm->textures[w(p+40)];
909 // muzzle flashes draw xparent
910 if(Interp_flags & MR_ALL_XPARENT){
911 gr_set_bitmap( texture, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, Interp_xparent_alpha );
913 gr_set_bitmap( texture );
917 if ( !(Interp_flags & MR_SHOW_OUTLINE_PRESET) ) {
918 gr_set_color( 128, 128, 128 );
922 if ( Interp_subspace ) {
923 g3_draw_poly( nv, Interp_list, TMAP_FLAG_TEXTURED|TMAP_FLAG_TILED|TMAP_FLAG_CORRECT );
925 if(Interp_flags & MR_ALL_XPARENT){
926 g3_draw_poly( nv, Interp_list, Interp_tmap_flags );
928 g3_draw_poly( nv, Interp_list, Interp_tmap_flags|TMAP_FLAG_NONDARKENING );
934 if (Interp_flags & (MR_SHOW_OUTLINE|MR_SHOW_OUTLINE_PRESET) ) {
936 if ( Interp_flags & MR_SHOW_OUTLINE ) {
937 gr_set_color_fast( &Interp_outline_color );
940 for (i=0; i<nv; i++ ) {
941 int j = (i + 1) % nv;
942 g3_draw_line(Interp_list[i], Interp_list[j] );
948 void interp_draw_box( vector *min, vector *max )
952 vector bounding_box[8];
956 model_calc_bound_box(bounding_box,min,max);
958 for (i=0; i<8; i++ ) {
959 g3_rotate_vertex( &pts[i], &bounding_box[i] );
962 gr_set_color(128,0,0);
964 Tmap_show_layers = 1;
966 l[3] = &pts[0]; l[2] = &pts[1]; l[1] = &pts[2]; l[0] = &pts[3];
967 g3_draw_poly( 4, l, 0 );
969 l[3] = &pts[3]; l[2] = &pts[2]; l[1] = &pts[6]; l[0] = &pts[7];
970 g3_draw_poly( 4, l, 0 );
972 l[3] = &pts[2]; l[2] = &pts[1]; l[1] = &pts[5]; l[0] = &pts[6];
973 g3_draw_poly( 4, l, 0 );
975 l[3] = &pts[1]; l[2] = &pts[0]; l[1] = &pts[4]; l[0] = &pts[5];
976 g3_draw_poly( 4, l, 0 );
978 l[3] = &pts[0]; l[2] = &pts[3]; l[1] = &pts[7]; l[0] = &pts[4];
979 g3_draw_poly( 4, l, 0 );
981 l[3] = &pts[4]; l[2] = &pts[7]; l[1] = &pts[6]; l[0] = &pts[5];
982 g3_draw_poly( 4, l, 0 );
991 // +20 vector normal_point
993 // 36 int front offset
994 // 40 int back offset
995 // 44 int prelist offset
996 // 48 int postlist offset
997 // 52 int online offset
998 void model_interp_sortnorm(ubyte * p,polymodel * pm, bsp_info *sm, int do_box_check)
1001 modelstats_num_sortnorms++;
1004 // Assert( w(p+4) == 56 );
1006 int frontlist = w(p+36);
1007 int backlist = w(p+40);
1008 int prelist = w(p+44);
1009 int postlist = w(p+48);
1010 int onlist = w(p+52);
1014 // light_filter_push_box( vp(p+56), vp(p+68) );
1016 #if 1 //def BACK_TO_FRONT
1018 if (prelist) model_interp_sub(p+prelist,pm,sm,do_box_check); // prelist
1020 if (g3_check_normal_facing(vp(p+20),vp(p+8))) { //facing
1022 //draw back then front
1024 if (backlist) model_interp_sub(p+backlist,pm,sm,do_box_check);
1026 if (onlist) model_interp_sub(p+onlist,pm,sm,do_box_check); //onlist
1028 if (frontlist) model_interp_sub(p+frontlist,pm,sm,do_box_check);
1030 } else { //not facing. draw front then back
1032 if (frontlist) model_interp_sub(p+frontlist,pm,sm,do_box_check);
1034 if (onlist) model_interp_sub(p+onlist,pm,sm,do_box_check); //onlist
1036 if (backlist) model_interp_sub(p+backlist,pm,sm,do_box_check);
1039 if (postlist) model_interp_sub(p+postlist,pm,sm,do_box_check); // postlist
1042 if (postlist) model_interp_sub(p+postlist,pm,sm,do_box_check); // postlist
1044 if (g3_check_normal_facing(vp(p+20),vp(p+8))) { //facing
1048 if (frontlist) model_interp_sub(p+frontlist,pm,sm,do_box_check);
1050 if (onlist) model_interp_sub(p+onlist,pm,sm,do_box_check); //onlist
1052 if (backlist) model_interp_sub(p+backlist,pm,sm,do_box_check);
1054 } else { //not facing. draw front then back
1056 //draw back then front
1058 if (backlist) model_interp_sub(p+backlist,pm,sm,do_box_check);
1060 if (onlist) model_interp_sub(p+onlist,pm,sm,do_box_check); //onlist
1062 if (frontlist) model_interp_sub(p+frontlist,pm,sm,do_box_check);
1066 if (prelist) model_interp_sub(p+prelist,pm,sm,do_box_check); // prelist
1070 // light_filter_pop();
1074 void model_draw_debug_points( polymodel *pm, bsp_info * submodel )
1076 if ( Interp_flags & MR_SHOW_OUTLINE_PRESET ) {
1080 // Draw the brown "core" mass
1081 // if ( submodel && (submodel->parent==-1) ) {
1082 // gr_set_color(128,128,0);
1083 // g3_draw_sphere_ez( &vmd_zero_vector, pm->core_radius );
1086 // Draw a red pivot point
1087 gr_set_color(128,0,0);
1088 g3_draw_sphere_ez(&vmd_zero_vector, 2.0f );
1090 // Draw a green center of mass when drawing the hull
1091 if ( submodel && (submodel->parent==-1) ) {
1092 gr_set_color(0,128,0);
1093 g3_draw_sphere_ez( &pm->center_of_mass, 1.0f );
1097 // Draw a blue center point
1098 gr_set_color(0,0,128);
1099 g3_draw_sphere_ez( &submodel->geometric_center, 0.9f );
1102 // Draw the bounding box
1107 for (i=0; i<8; i++ ) {
1108 g3_rotate_vertex( &pts[i], &submodel->bounding_box[i] );
1110 gr_set_color(128,128,128);
1111 g3_draw_line( &pts[0], &pts[1] );
1112 g3_draw_line( &pts[1], &pts[2] );
1113 g3_draw_line( &pts[2], &pts[3] );
1114 g3_draw_line( &pts[3], &pts[0] );
1116 g3_draw_line( &pts[4], &pts[5] );
1117 g3_draw_line( &pts[5], &pts[6] );
1118 g3_draw_line( &pts[6], &pts[7] );
1119 g3_draw_line( &pts[7], &pts[4] );
1121 g3_draw_line( &pts[0], &pts[4] );
1122 g3_draw_line( &pts[1], &pts[5] );
1123 g3_draw_line( &pts[2], &pts[6] );
1124 g3_draw_line( &pts[3], &pts[7] );
1126 //for (i=0; i<8; i++ ) {
1127 // g3_rotate_vertex( &pts[i], &pm->bounding_box[i] );
1129 gr_set_color(0,255,0);
1132 for (j=0; j<8; j++ ) {
1134 vector bounding_box[8]; // caclulated fron min/max
1135 model_calc_bound_box(bounding_box,&pm->octants[j].min,&pm->octants[j].max);
1137 for (i=0; i<8; i++ ) {
1138 g3_rotate_vertex( &pts[i], &bounding_box[i] );
1140 gr_set_color(128,0,0);
1141 g3_draw_line( &pts[0], &pts[1] );
1142 g3_draw_line( &pts[1], &pts[2] );
1143 g3_draw_line( &pts[2], &pts[3] );
1144 g3_draw_line( &pts[3], &pts[0] );
1146 g3_draw_line( &pts[4], &pts[5] );
1147 g3_draw_line( &pts[5], &pts[6] );
1148 g3_draw_line( &pts[6], &pts[7] );
1149 g3_draw_line( &pts[7], &pts[4] );
1151 g3_draw_line( &pts[0], &pts[4] );
1152 g3_draw_line( &pts[1], &pts[5] );
1153 g3_draw_line( &pts[2], &pts[6] );
1154 g3_draw_line( &pts[3], &pts[7] );
1159 // Debug code to show all the paths of a model
1160 void model_draw_paths( int model_num )
1166 if ( Interp_flags & MR_SHOW_OUTLINE_PRESET ) {
1170 pm = model_get(model_num);
1176 for (i=0; i<pm->n_paths; i++ ) {
1179 for (j=0; j<pm->paths[i].nverts; j++ ) {
1180 // Rotate point into world coordinates
1181 pnt = pm->paths[i].verts[j].pos;
1183 // Pnt is now the x,y,z world coordinates of this vert.
1184 // For this example, I am just drawing a sphere at that
1188 g3_rotate_vertex(&tmp,&pnt);
1190 if ( pm->paths[i].verts[j].nturrets > 0 ){
1191 gr_set_color( 0, 0, 255 ); // draw points covered by turrets in blue
1193 gr_set_color( 255, 0, 0 );
1196 // g3_draw_sphere( &tmp, pm->paths[i].verts[j].radius );
1197 g3_draw_sphere( &tmp, 0.5f );
1199 gr_set_color( 255, 0, 0 );
1201 g3_draw_line(&prev_pnt, &tmp);
1211 // docking bay and fighter bay paths
1212 void model_draw_bay_paths(int model_num)
1218 polymodel *pm = model_get(model_num);
1223 // render docking bay normals
1224 gr_set_color(0, 255, 0);
1225 for(idx=0; idx<pm->n_docks; idx++){
1226 for(s_idx=0; s_idx<pm->docking_bays[idx].num_slots; s_idx++){
1227 v1 = pm->docking_bays[idx].pnt[s_idx];
1228 vm_vec_scale_add(&v2, &v1, &pm->docking_bays[idx].norm[s_idx], 10.0f);
1230 // rotate the points
1231 g3_rotate_vertex(&l1, &v1);
1232 g3_rotate_vertex(&l2, &v2);
1234 // draw the point and normal
1235 g3_draw_sphere(&l1, 2.0);
1236 g3_draw_line(&l1, &l2);
1240 // render figher bay paths
1241 gr_set_color(0, 255, 255);
1243 // iterate through the paths that exist in the polymodel, searching for $bayN pathnames
1244 for (idx = 0; idx<pm->n_paths; idx++) {
1245 if ( !strnicmp(pm->paths[idx].name, NOX("$bay"), 4) ) {
1246 for(s_idx=0; s_idx<pm->paths[idx].nverts-1; s_idx++){
1247 v1 = pm->paths[idx].verts[s_idx].pos;
1248 v2 = pm->paths[idx].verts[s_idx+1].pos;
1251 g3_rotate_vertex(&l1, &v1);
1252 g3_rotate_vertex(&l2, &v2);
1253 g3_draw_line(&l1, &l2);
1259 // struct that holds the indicies into path information associated with a fighter bay on a capital ship
1260 // NOTE: Fighter bay paths are identified by the path_name $bayN (where N is numbered from 1).
1261 // Capital ships only have ONE fighter bay on the entire ship
1262 #define MAX_SHIP_BAY_PATHS 10
1263 typedef struct ship_bay {
1264 int num_paths; // how many paths are associated with the model's fighter bay
1265 int paths[MAX_SHIP_BAY_PATHS]; // index into polymodel->paths[] array
1266 int arrive_flags; // bitfield, set to 1 when that path number is reserved for an arrival
1267 int depart_flags; // bitfield, set to 1 when that path number is reserved for a departure
1270 typedef struct mp_vert {
1271 vector pos; // xyz coordinates of vertex in object's frame of reference
1272 int nturrets; // number of turrets guarding this vertex
1273 int *turret_ids; // array of indices into ship_subsys linked list (can't index using [] though)
1274 float radius; // How far the closest obstruction is from this vertex
1277 typedef struct model_path {
1278 char name[MAX_NAME_LEN]; // name of the subsystem. Probably displayed on HUD
1279 char parent_name[MAX_NAME_LEN]; // parent name of submodel that path is linked to in POF
1280 int parent_submodel;
1283 int goal; // Which of the verts is the one closest to the goal of this path
1284 int type; // What this path takes you to... See MP_TYPE_??? defines above for details
1285 int value; // This depends on the type.
1286 // For MP_TYPE_UNUSED, this means nothing.
1287 // For MP_TYPE_SUBSYS, this is the subsystem number this path takes you to.
1292 void interp_render_arc_segment( vector *v1, vector *v2, int depth )
1294 float d = vm_vec_dist_quick( v1, v2 );
1296 if ( d < 0.30f || (depth>4) ) {
1298 g3_rotate_vertex( &p1, v1 );
1299 g3_rotate_vertex( &p2, v2 );
1301 //g3_draw_rod( v1, 0.2f, v2, 0.2f, NULL, 0);
1302 g3_draw_line( &p1, &p2 );
1306 vm_vec_avg( &tmp, v1, v2 );
1308 float scaler = 0.30f;
1309 tmp.x += (frand()-0.5f)*d*scaler;
1310 tmp.y += (frand()-0.5f)*d*scaler;
1311 tmp.z += (frand()-0.5f)*d*scaler;
1313 interp_render_arc_segment( v1, &tmp, depth+1 );
1314 interp_render_arc_segment( &tmp, v2, depth+1 );
1318 int Interp_lightning = 1;
1319 DCF_BOOL( Arcs, Interp_lightning )
1327 void interp_render_lightning( polymodel *pm, bsp_info * sm )
1329 Assert( sm->num_arcs > 0 );
1333 if ( Interp_flags & MR_SHOW_OUTLINE_PRESET ) {
1337 if (!Interp_lightning) return;
1339 // if ( keyd_pressed[KEY_LSHIFT] ) return;
1340 // if ( rad < 3.0f ) return;
1342 for (i=0; i<sm->num_arcs; i++ ) {
1343 // pick a color based upon arc type
1344 switch(sm->arc_type[i]){
1345 // "normal", Freespace 1 style arcs
1346 case MARC_TYPE_NORMAL:
1347 if ( (rand()>>4) & 1 ) {
1348 gr_set_color( 64, 64, 255 );
1350 gr_set_color( 128, 128, 255 );
1356 if ( (rand()>>4) & 1 ) {
1357 gr_set_color( AR, AG, AB );
1359 gr_set_color( AR2, AG2, AB2 );
1367 // render the actual arc segment
1368 interp_render_arc_segment( &sm->arc_pts[i][0], &sm->arc_pts[i][1], 0 );
1372 void model_interp_subcall(polymodel * pm, int mn, int detail_level)
1375 int zbuf_mode = gr_zbuffering_mode;
1377 if ( (mn < 0) || (mn>=pm->n_models) )
1381 Assert( mn < pm->n_models );
1383 // mprintf(( "Name = '%s'\n", pm->submodel[mn].name ));
1384 // char * p = pm->submodel[mn].name;
1386 if (pm->submodel[mn].blown_off){
1390 if (pm->submodel[mn].is_thruster ) {
1391 if ( !(Interp_flags & MR_SHOW_THRUSTERS) ){
1394 Interp_thrust_scale_subobj=1;
1396 Interp_thrust_scale_subobj=0;
1399 g3_start_instance_angles(&pm->submodel[mn].offset, &pm->submodel[mn].angs);
1400 if ( !(Interp_flags & MR_NO_LIGHTING ) ) {
1404 model_interp_sub( pm->submodel[mn].bsp_data, pm, &pm->submodel[mn], 0 );
1406 if (Interp_flags & MR_SHOW_PIVOTS )
1407 model_draw_debug_points( pm, &pm->submodel[mn] );
1409 if ( pm->submodel[mn].num_arcs ) {
1410 interp_render_lightning( pm, &pm->submodel[mn]);
1413 i = pm->submodel[mn].first_child;
1415 if (!pm->submodel[i].is_thruster ) {
1416 if(Interp_flags & MR_NO_ZBUFFER){
1417 zbuf_mode = GR_ZBUFF_NONE;
1419 zbuf_mode = GR_ZBUFF_FULL; // read only
1422 gr_zbuffer_set(zbuf_mode);
1424 model_interp_subcall( pm, i, detail_level );
1426 i = pm->submodel[i].next_sibling;
1434 // Returns one of the following
1435 #define IBOX_ALL_OFF 0
1436 #define IBOX_ALL_ON 1
1437 #define IBOX_SOME_ON_SOME_OFF 2
1439 int interp_box_offscreen( vector *min, vector *max )
1441 if ( keyd_pressed[KEY_LSHIFT] ) {
1446 v[0].x = min->x; v[0].y = min->y; v[0].z = min->z;
1447 v[1].x = max->x; v[1].y = min->y; v[1].z = min->z;
1448 v[2].x = max->x; v[2].y = max->y; v[2].z = min->z;
1449 v[3].x = min->x; v[3].y = max->y; v[3].z = min->z;
1451 v[4].x = min->x; v[4].y = min->y; v[4].z = max->z;
1452 v[5].x = max->x; v[5].y = min->y; v[5].z = max->z;
1453 v[6].x = max->x; v[6].y = max->y; v[6].z = max->z;
1454 v[7].x = min->x; v[7].y = max->y; v[7].z = max->z;
1456 ubyte and_codes = 0xff;
1457 ubyte or_codes = 0xff;
1460 for (i=0; i<8; i++ ) {
1462 ubyte codes=g3_rotate_vertex( &tmp, &v[i] );
1463 // Early out which we cannot do because we want to differentiate btwn
1464 // IBOX_SOME_ON_SOME_OFF and IBOX_ALL_ON
1466 // //mprintf(( "A point is inside, so render it.\n" ));
1467 // return 0; // this point is in, so return 0
1473 // If and_codes is set this means that all points lie off to the
1474 // same side of the screen.
1476 //mprintf(( "All points offscreen, so don't render it.\n" ));
1477 return IBOX_ALL_OFF; //all points off screen
1480 // If this is set it means at least one of the points is offscreen,
1481 // but they aren't all off to the same side.
1483 return IBOX_SOME_ON_SOME_OFF;
1486 // They are all onscreen.
1491 //calls the object interpreter to render an object.
1492 //returns true if drew
1493 int model_interp_sub(void *model_ptr, polymodel * pm, bsp_info *sm, int do_box_check )
1495 ubyte *p = (ubyte *)model_ptr;
1496 int chunk_type, chunk_size;
1500 chunk_size = w(p+4);
1502 while ( chunk_type != OP_EOF ) {
1504 // mprintf(( "Processing chunk type %d, len=%d\n", chunk_type, chunk_size ));
1506 switch (chunk_type) {
1507 case OP_EOF: return 1;
1508 case OP_DEFPOINTS: model_interp_defpoints(p,pm,sm); break;
1509 case OP_FLATPOLY: model_interp_flatpoly(p,pm); break;
1510 case OP_TMAPPOLY: model_interp_tmappoly(p,pm); break;
1511 case OP_SORTNORM: model_interp_sortnorm(p,pm,sm,do_box_check); break;
1515 if ( do_box_check ) {
1516 int retval = interp_box_offscreen( vp(p+8), vp(p+20) );
1519 goto DoneWithThis; // Don't need to draw any more polys from this box
1523 do_box_check = 0; // Don't need to check boxes any more
1526 case IBOX_SOME_ON_SOME_OFF:
1527 // continue like we were
1535 if (Interp_flags & MR_SHOW_PIVOTS ) {
1537 modelstats_num_boxes++;
1539 interp_draw_box( vp(p+8), vp(p+20) );
1542 if ( !(Interp_flags & MR_NO_LIGHTING ) ) {
1548 light_filter_push_box( vp(p+8), vp(p+20) );
1554 mprintf(( "Bad chunk type %d, len=%d in model_interp_sub\n", chunk_type, chunk_size ));
1555 Int3(); // Bad chunk type!
1560 chunk_size = w(p+4);
1565 if ( !(Interp_flags & MR_NO_LIGHTING ) ) {
1576 void model_render_shields( polymodel * pm )
1580 vertex pnt0, tmp, prev_pnt;
1582 if ( Interp_flags & MR_SHOW_OUTLINE_PRESET ) {
1586 gr_set_color(0, 0, 200 );
1588 // Scan all the triangles in the mesh.
1589 for (i=0; i<pm->shield.ntris; i++ ) {
1591 tri = &pm->shield.tris[i];
1593 if (g3_check_normal_facing(&pm->shield.verts[tri->verts[0]].pos,&tri->norm ) ) {
1595 // Process the vertices.
1596 // Note this rotates each vertex each time it's needed, very dumb.
1597 for (j=0; j<3; j++ ) {
1599 g3_rotate_vertex(&tmp, &pm->shield.verts[tri->verts[j]].pos );
1602 g3_draw_line(&prev_pnt, &tmp);
1608 g3_draw_line(&pnt0, &prev_pnt);
1613 void model_render_insignias(polymodel *pm, int detail_level)
1617 vertex *vlist[3] = { &vecs[0], &vecs[1], &vecs[2] };
1621 // if the model has no insignias we're done
1622 if(pm->num_ins <= 0){
1626 // set the proper texture
1627 if(Interp_insignia_bitmap >= 0){
1628 gr_set_bitmap(Interp_insignia_bitmap);
1630 // otherwise don't even bother rendering
1635 // otherwise render them
1636 for(idx=0; idx<pm->num_ins; idx++){
1637 // skip insignias not on our detail level
1638 if(pm->ins[idx].detail_level != detail_level){
1642 for(s_idx=0; s_idx<pm->ins[idx].num_faces; s_idx++){
1643 // get vertex indices
1644 i1 = pm->ins[idx].faces[s_idx][0];
1645 i2 = pm->ins[idx].faces[s_idx][1];
1646 i3 = pm->ins[idx].faces[s_idx][2];
1648 // transform vecs and setup vertices
1649 vm_vec_add(&t1, &pm->ins[idx].vecs[i1], &pm->ins[idx].offset);
1650 vm_vec_add(&t2, &pm->ins[idx].vecs[i2], &pm->ins[idx].offset);
1651 vm_vec_add(&t3, &pm->ins[idx].vecs[i3], &pm->ins[idx].offset);
1652 g3_rotate_vertex(&vecs[0], &t1);
1653 g3_rotate_vertex(&vecs[1], &t2);
1654 g3_rotate_vertex(&vecs[2], &t3);
1656 // setup texture coords
1657 vecs[0].u = pm->ins[idx].u[s_idx][0]; vecs[0].v = pm->ins[idx].v[s_idx][0];
1658 vecs[1].u = pm->ins[idx].u[s_idx][1]; vecs[1].v = pm->ins[idx].v[s_idx][1];
1659 vecs[2].u = pm->ins[idx].u[s_idx][2]; vecs[2].v = pm->ins[idx].v[s_idx][2];
1662 g3_draw_poly(3, vlist, TMAP_FLAG_TEXTURED);
1667 int Model_texturing = 1;
1668 int Model_polys = 1;
1670 DCF_BOOL( model_texturing, Model_texturing )
1671 DCF_BOOL( model_polys, Model_polys )
1673 MONITOR( NumModelsRend );
1674 MONITOR( NumHiModelsRend );
1675 MONITOR( NumMedModelsRend );
1676 MONITOR( NumLowModelsRend );
1679 typedef struct model_cache {
1699 int thrust_glow_bitmap;
1700 float thrust_glow_noise;
1702 int last_frame_rendered; // last frame in which this model was rendered not from the cache
1705 #define MAX_MODEL_CACHE MAX_OBJECTS
1706 model_cache Model_cache[MAX_MODEL_CACHE]; // Indexed by objnum
1707 int Model_cache_inited = 0;
1711 // Returns 0 if not valid points
1712 int model_cache_calc_coords(vector *pnt,float rad, float *cx, float *cy, float *cr)
1717 flags = g3_rotate_vertex(&pt,pnt);
1721 g3_project_vertex(&pt);
1723 if (!(pt.flags & (PF_OVERFLOW|CC_BEHIND))) {
1727 *cr = rad*Matrix_scale.x*Canv_w2/pt.z;
1736 if ( x1 < gr_screen.clip_left ) return 0;
1738 if ( x2 > gr_screen.clip_right ) return 0;
1740 if ( y1 < gr_screen.clip_top ) return 0;
1742 if ( y2 > gr_screen.clip_bottom ) return 0;
1750 void model_really_render(int model_num, matrix *orient, vector * pos, uint flags, int light_ignore_id );
1753 //draws a bitmap with the specified 3d width & height
1754 //returns 1 if off screen, 0 if not
1755 int model_get_rotated_bitmap_points(vertex *pnt,float angle, float rad, vertex *v)
1760 Assert( G3_count == 1 );
1766 sa = (float)sin(angle);
1767 ca = (float)cos(angle);
1769 float width, height;
1771 width = height = rad;
1773 v[0].x = (-width*ca - height*sa)*Matrix_scale.x + pnt->x;
1774 v[0].y = (-width*sa + height*ca)*Matrix_scale.y + pnt->y;
1780 v[1].x = (width*ca - height*sa)*Matrix_scale.x + pnt->x;
1781 v[1].y = (width*sa + height*ca)*Matrix_scale.y + pnt->y;
1787 v[2].x = (width*ca + height*sa)*Matrix_scale.x + pnt->x;
1788 v[2].y = (width*sa - height*ca)*Matrix_scale.y + pnt->y;
1794 v[3].x = (-width*ca + height*sa)*Matrix_scale.x + pnt->x;
1795 v[3].y = (-width*sa - height*ca)*Matrix_scale.y + pnt->y;
1801 ubyte codes_and=0xff;
1804 z = pnt->z - rad / 4.0f;
1805 if ( z < 0.0f ) z = 0.0f;
1808 for (i=0; i<4; i++ ) {
1809 //now code the four points
1810 codes_and &= g3_code_vertex(&v[i]);
1811 v[i].flags = 0; // mark as not yet projected
1812 g3_project_vertex(&v[i]);
1817 return 1; //1 means off screen
1823 int Model_caching = 1;
1824 DCF_BOOL( model_caching, Model_caching );
1826 extern int Tmap_scan_read; // 0 = normal mapper, 1=read, 2=write
1828 #define MODEL_MAX_BITMAP_SIZE 128
1829 ubyte tmp_bitmap[MODEL_MAX_BITMAP_SIZE*MODEL_MAX_BITMAP_SIZE];
1831 void mc_get_bmp( ubyte *data, int x, int y, int w, int h )
1837 for (i = 0; i < h; i++) {
1838 ubyte *dptr = GR_SCREEN_PTR(ubyte,x,i+y);
1839 ubyte *sptr = data+(i*w);
1840 for (j=0; j<w; j++ ) {
1842 *dptr++ = 255; // XPARENT!
1849 void mc_put_bmp( ubyte *data, int x, int y, int w, int h )
1855 for (i = 0; i < h; i++) {
1856 ubyte *dptr = GR_SCREEN_PTR(ubyte,x,i+y);
1857 ubyte *sptr = data+(i*w);
1858 for (j=0; j<w; j++ ) {
1866 float Interp_depth_scale = 1500.0f;
1868 DCF(model_darkening,"Makes models darker with distance")
1871 dc_get_arg(ARG_FLOAT);
1872 Interp_depth_scale = Dc_arg_float;
1876 dc_printf( "Usage: model_darkening float\n" );
1877 Dc_status = 0; // don't print status if help is printed. Too messy.
1881 dc_printf( "model_darkening = %.1f\n", Interp_depth_scale );
1885 void model_try_cache_render(int model_num, matrix *orient, vector * pos, uint flags, int objnum, int num_lights );
1887 // Compare it to 999.75f at R = 64.0f
1888 // 0.0000f at R = 4.0f
1890 //float cmp_val = 999.75f; // old
1892 // Return a number from 'min' to 'max' where it is
1893 // 'v' is between v1 and v2.
1894 float scale_it( float min, float max, float v, float v1, float v2 )
1896 if ( v < v1 ) return min;
1897 if ( v > v2 ) return max;
1899 v = (v - v1)/(v2-v1);
1900 v = v*(max-min)+min;
1905 void model_render(int model_num, matrix *orient, vector * pos, uint flags, int objnum, int lighting_skip )
1907 polymodel *pm = model_get(model_num);
1909 // maybe turn off (hardware) culling
1910 if(flags & MR_NO_CULL){
1914 Interp_objnum = objnum;
1916 if ( flags & MR_NO_LIGHTING ) {
1917 Interp_light = 1.0f;
1919 // never use saved lighitng for this object
1920 model_set_saved_lighting(-1, -1);
1921 } else if ( flags & MR_IS_ASTEROID ) {
1922 // Dim it based on distance
1923 float depth = vm_vec_dist_quick( pos, &Eye_position );
1924 if ( depth > Interp_depth_scale ) {
1925 Interp_light = Interp_depth_scale/depth;
1926 // If it is too far, exit
1927 if ( Interp_light < (1.0f/32.0f) ) {
1928 Interp_light = 0.0f;
1930 } else if ( Interp_light > 1.0f ) {
1931 Interp_light = 1.0f;
1934 Interp_light = 1.0f;
1937 // never use saved lighitng for this object
1938 model_set_saved_lighting(-1, -1);
1940 Interp_light = 1.0f;
1942 // maybe use saved lighting
1943 model_set_saved_lighting(objnum, hack_skip_max);
1948 if ( !(flags & MR_NO_LIGHTING ) ) {
1949 if ( D3D_enabled ) {
1950 num_lights = light_filter_push( objnum, pos, pm->rad );
1952 num_lights = light_filter_push( objnum, pos, pm->rad );
1956 model_try_cache_render(model_num, orient, pos, flags, objnum, num_lights );
1958 if ( !(flags & MR_NO_LIGHTING ) ) {
1962 // maybe turn culling back on
1963 if(flags & MR_NO_CULL){
1967 // turn off fog after each model renders
1968 if(The_mission.flags & MISSION_FLAG_FULLNEB){
1969 gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
1974 void model_cache_init()
1976 if ( !Model_cache_inited ) {
1979 Model_cache_inited = 1;
1981 for (i=0; i<MAX_MODEL_CACHE; i++ ) {
1982 Model_cache[i].cached_valid = 0;
1983 Model_cache[i].data = NULL;
1984 Model_cache[i].bitmap_id = -1;
1985 Model_cache[i].last_frame_rendered = -1;
1990 void model_cache_reset()
1992 if ( Model_cache_inited ) {
1995 for (i=0; i<MAX_MODEL_CACHE; i++ ) {
1996 Model_cache[i].cached_valid = 0;
1997 if ( Model_cache[i].data ) {
1998 free(Model_cache[i].data);
1999 Model_cache[i].data = NULL;
2001 if ( Model_cache[i].bitmap_id != -1 ) {
2002 bm_release(Model_cache[i].bitmap_id);
2003 Model_cache[i].bitmap_id = -1;
2016 #if MAX_DETAIL_LEVEL != 4
2017 #error MAX_DETAIL_LEVEL is assumed to be 4 in ModelInterp.cpp
2020 // Given detail level, what is the threshold for how far viewer
2021 // can move in the object's frame of reference before a redraw.
2022 float Mc_viewer_pos_factor[MAX_DETAIL_LEVEL+1] = { 0.080f, 0.040f, 0.020f, 0.010f, 0.0f };
2023 float Mc_size_factor[MAX_DETAIL_LEVEL+1] = { 1.40f, 1.30f, 1.20f, 1.10f, 0.0f };
2025 int Model_object_caching_tmp = MAX_DETAIL_LEVEL;
2027 // When framerate goes below this, knock it down a notch.
2028 float Mc_framerate_lo[MAX_DETAIL_LEVEL+1] = { 0.0f, 10.0f, 15.0f, 20.0f, 25.0f };
2029 // When it goes above this, knock it up a notch.
2030 float Mc_framerate_hi[MAX_DETAIL_LEVEL+1] = { 15.0f, 20.0f, 25.0f, 30.0f, 100000.0f };
2032 int Mc_detail_add[MAX_DETAIL_LEVEL+1] = { -2, -1, +1, +2, +4 };
2034 extern float flFrametime;
2036 void model_try_cache_render(int model_num, matrix *orient, vector * pos, uint flags, int objnum, int num_lights )
2038 model_really_render(model_num, orient, pos, flags, objnum);
2042 model_cache *mc = NULL;
2044 if ( (objnum>-1) && (objnum<MAX_MODEL_CACHE) ) {
2045 mc = &Model_cache[objnum];
2048 if ( (!mc) || (!Model_caching) || (D3D_enabled) || (!Model_cache_inited) || (flags & MR_ALWAYS_REDRAW) || (Detail.object_caching > 3) ) {
2050 mc->cached_valid = 0;
2052 model_really_render(model_num, orient, pos, flags, objnum );
2056 Assert( mc != NULL );
2058 // Fake the detail level based on framerate.
2059 if ( 1.0f / flFrametime < Mc_framerate_lo[Model_object_caching_tmp] ) {
2060 Model_object_caching_tmp--;
2061 // mprintf(( "Model cache level bumped down to %d\n", Model_object_caching ));
2062 } else if ( 1.0f / flFrametime > Mc_framerate_hi[Model_object_caching_tmp] ) {
2063 Model_object_caching_tmp++;
2064 // mprintf(( "Model cache level bumped up to %d\n", Model_object_caching ));
2067 int tmp_detail_level = Model_object_caching_tmp + Mc_detail_add[Detail.object_caching];
2069 if ( tmp_detail_level < 0 ) {
2070 tmp_detail_level = 0;
2071 } else if (tmp_detail_level > MAX_DETAIL_LEVEL ) {
2072 tmp_detail_level = MAX_DETAIL_LEVEL;
2075 if ( tmp_detail_level > 3 ) {
2077 mc->cached_valid = 0;
2079 model_really_render(model_num, orient, pos, flags, objnum );
2084 // static int last_one = -1;
2085 // if ( last_one != tmp_detail_level ) {
2086 // last_one = tmp_detail_level;
2087 // mprintf(( "Detail level %d\n", tmp_detail_level ));
2090 // if ( keyd_pressed[KEY_LSHIFT] ) {
2091 // mc->cached_valid = 0;
2092 // model_really_render(model_num, orient, pos, flags, objnum );
2097 // mprintf(( "Rendering cache model\n" ));
2099 polymodel *pm = model_get(model_num);
2101 vertex *vertlist[4] = { &v[0], &v[1], &v[2], &v[3] };
2106 matrix tempm, tempm2;
2109 vm_copy_transpose_matrix(&tempm2,orient);
2110 vm_matrix_x_matrix(&tempm,&tempm2,&Eye_matrix);
2111 vm_extract_angles_matrix(&new_angles, &tempm );
2113 if ( !model_cache_calc_coords(pos,pm->rad, &cx, &cy, &cr) ) {
2114 // Not onscreen, do a real render and exit
2115 mc->cached_valid = 0;
2116 model_really_render(model_num, orient, pos, flags, objnum );
2120 //================================================================
2121 // A bunch of checks to see if we need to redraw the model or not
2126 vm_vec_sub( &ship_to_eye, &Eye_position, pos );
2127 vm_vec_normalize_safe(&ship_to_eye);
2128 float this_dot = vm_vec_dot( &ship_to_eye, &orient->fvec );
2129 this_dot += vm_vec_dot( &ship_to_eye, &orient->rvec );
2133 if ( !mc->cached_valid ) {
2138 Assert( mc->data != NULL );
2140 if (Framecount - mc->last_frame_rendered > 1 + 2*(MAX_DETAIL_LEVEL - Detail.object_caching - 1)) {
2144 diff = fl_abs( this_dot - mc->last_dot );
2146 if ( diff > Mc_viewer_pos_factor[tmp_detail_level] ) {
2147 // mprintf(( "Redraw!!! %.4f\n", diff ));
2151 // if ( keyd_pressed[KEY_LSHIFT] ) {
2155 if (tmp_detail_level > 2) {
2156 if ( mc->thrust_glow_bitmap != Interp_thrust_glow_bitmap ) {
2157 // Engline glow bitmap changed
2158 // mprintf(( "MC: Glow bitmap changed! %d -> %d\n", mc->thrust_glow_bitmap, Interp_thrust_glow_bitmap ));
2163 if (tmp_detail_level > 2) {
2165 float diff = fl_abs( mc->thrust_scale - Interp_thrust_scale );
2167 if ( diff > 0.1f ) {
2168 // Thruster size has changed
2169 //mprintf(( "MC: Thruster size changed! %.2f -> %.2f\n", mc->thrust_scale, Interp_thrust_scale ));
2176 // float diff = fl_abs( mc->thrust_glow_noise - Interp_thrust_glow_noise );
2178 // if ( diff > 0.1f ) {
2179 // Glow noise has changed
2180 //mprintf(( "MC: Thruster glow changed! %.2f -> %.2f\n", mc->thrust_glow_noise, Interp_thrust_glow_noise ));
2186 if ( mc->model_num != model_num ) {
2191 if ( cr>mc->cr*Mc_size_factor[tmp_detail_level] ) {
2192 // Scaling up too far
2196 if (tmp_detail_level > 2) {
2198 if ( !(Interp_flags & MR_NO_LIGHTING ) ) {
2199 if (mc->num_lights != num_lights) {
2207 // This method is correct, but rotating ship makes things redraw which is too slow.
2210 // Check orientation
2211 float angle_error = max( fl_abs( mc->angs.p-new_angles.p ),fl_abs( mc->angs.h-new_angles.h ));
2214 //if ( angle_error > 0.075f ) {
2217 if ( angle_error > 0.40f ) {
2218 // Ship/view turned too much
2219 //mprintf(( "Ship/view turned too much %.4f\n", angle_error ));
2227 // mprintf(( "Dot = %.5f\n", dot ));
2233 dx = vm_vec_dot( &orient->rvec, &mc->orient.rvec )+1.0f;
2234 dy = vm_vec_dot( &orient->uvec, &mc->orient.uvec )+1.0f;
2235 dz = vm_vec_dot( &orient->fvec, &mc->orient.fvec )+1.0f;
2237 float angle_error = (dx+dy+dz)*1000.0f/6.0f;
2239 //mprintf(( "Angle_error = %.4f\n", angle_error ));
2241 // Compare it to 999.75f at R = 64.0f
2242 // 0.0000f at R = 0.0f
2244 float cmp_val = 999.75f; // old
2245 // if ( is_asteroid ) {
2246 // cmp_val = scale_it( 0.0f, 999.75f, cr, 0.0f, 64.0f );
2249 if ( angle_error < cmp_val ) {
2250 // Ship turned too much
2257 // Have a valid cache entry, mc
2258 ccflags = g3_rotate_vertex(&pt,pos);
2265 if ( model_get_rotated_bitmap_points(&pt,mc->angs.b - new_angles.b, pm->rad, v )) {
2271 gr_set_bitmap( mc->bitmap_id );
2274 g3_draw_poly(4, vertlist, TMAP_FLAG_TEXTURED );
2277 // if ( keyd_pressed[KEY_LSHIFT] ) {
2278 // gr_set_color( 255, 0, 0 );
2279 // gr_pixel( fl2i(v[0].sx), fl2i(v[0].sy) );
2282 //if ( keyd_pressed[KEY_RSHIFT] ) {
2283 // gr_line( fl2i(v[0].sx), fl2i(v[0].sy), fl2i(v[1].sx), fl2i(v[1].sy) );
2284 // gr_line( fl2i(v[1].sx), fl2i(v[1].sy), fl2i(v[2].sx), fl2i(v[2].sy) );
2285 // gr_line( fl2i(v[2].sx), fl2i(v[2].sy), fl2i(v[3].sx), fl2i(v[3].sy) );
2286 // gr_line( fl2i(v[3].sx), fl2i(v[3].sy), fl2i(v[0].sx), fl2i(v[0].sy) );
2293 //==========================================================
2294 // Cache is bad for model, so draw it and save it
2298 // if ( mc->data != NULL ) {
2303 if ( mc->bitmap_id != -1 ) {
2304 bm_release(mc->bitmap_id);
2308 mc->cached_valid = 0;
2309 mc->model_num = model_num;
2311 //mc->orient = *orient;
2313 mc->angs = new_angles; //-Physics_viewer_bank;
2315 mc->thrust_scale = Interp_thrust_scale;
2316 mc->thrust_bitmap = Interp_thrust_bitmap;
2317 mc->thrust_glow_bitmap = Interp_thrust_glow_bitmap;
2318 mc->thrust_glow_noise = Interp_thrust_glow_noise;
2320 mc->last_dot = this_dot;
2322 if ( cr > MODEL_MAX_BITMAP_SIZE/2-1 )
2325 //Physics_viewer_bank
2327 ccflags = g3_rotate_vertex(&pt,pos);
2333 model_get_rotated_bitmap_points(&pt,0.0f, pm->rad, v );
2335 int x1, y1, x2, y2, w, h;
2337 x1 = fl_round_2048( v[0].sx );
2338 y1 = fl_round_2048( v[0].sy );
2340 x2 = fl_round_2048( v[2].sx ); //+0.5f );
2341 y2 = fl_round_2048( v[2].sy ); //+0.5f );
2343 if ( x1 < gr_screen.clip_left)
2346 if ( y1 < gr_screen.clip_top )
2349 if ( x2 > gr_screen.clip_right)
2352 if ( y2 > gr_screen.clip_bottom)
2370 if ( w > MODEL_MAX_BITMAP_SIZE )
2373 if ( h > MODEL_MAX_BITMAP_SIZE )
2379 // mprintf(( "Mallocing a %dx%d bitmap\n", w, h ));
2381 if ( mc->data == NULL ) {
2382 mc->data = (ubyte *)malloc( MODEL_MAX_BITMAP_SIZE * MODEL_MAX_BITMAP_SIZE );
2385 // mprintf(( "Done mallocing a %dx%d bitmap\n", w, h ));
2387 if ( mc->data == NULL ) {
2390 for (i = 0; i < w*h; i++) {
2395 mc->bitmap_id = bm_create( 8, mc->w, mc->h, mc->data, 0 );
2397 if ( mc->bitmap_id < 0 ) {
2401 // Save stars and stuff on screen
2402 mc_get_bmp( tmp_bitmap, x1, y1, w, h );
2404 mc->num_lights = num_lights;
2406 // Didn't render a cached one... so render it and then save it in the cache
2408 // Turn on stippling
2409 model_really_render(model_num, orient, pos, flags, objnum );
2411 // Save screen to bitmap
2412 gr_set_bitmap( mc->bitmap_id );
2414 g3_draw_poly(4, vertlist, TMAP_FLAG_TEXTURED );
2417 // Restore stars and stuff to screen
2418 mc_put_bmp( tmp_bitmap, x1, y1, w, h );
2421 gr_set_bitmap( mc->bitmap_id );
2423 g3_draw_poly(4, vertlist, TMAP_FLAG_TEXTURED );
2426 mc->cached_valid = 1;
2427 mc->last_frame_rendered = Framecount;
2433 model_really_render(model_num, orient, pos, flags, objnum );
2437 // Find the distance from p0 to the closest point on a box.
2438 // The box's dimensions from 'min' to 'max'.
2439 float interp_closest_dist_to_box( vector *hitpt, vector *p0, vector *min, vector *max )
2441 float *origin = (float *)&p0->x;
2442 float *minB = (float *)min;
2443 float *maxB = (float *)max;
2444 float *coord = (float *)&hitpt->x;
2448 for (i=0; i<3; i++ ) {
2449 if ( origin[i] < minB[i] ) {
2452 } else if (origin[i] > maxB[i] ) {
2456 coord[i] = origin[i];
2464 return vm_vec_dist(hitpt,p0);
2468 // Finds the closest point on a model to a point in space. Actually only finds a point
2469 // on the bounding box of the model.
2471 // model_num Which model
2472 // orient Orientation of the model
2473 // pos Position of the model
2474 // eye_pos Point that you want to find the closest point to
2476 // distance from eye_pos to closest_point. 0 means eye_pos is
2477 // on or inside the bounding box.
2478 // Also fills in outpnt with the actual closest point.
2479 float model_find_closest_point( vector *outpnt, int model_num, int submodel_num, matrix *orient, vector * pos, vector *eye_pos )
2481 vector closest_pos, tempv, eye_rel_pos;
2483 polymodel *pm = model_get(model_num);
2485 if ( submodel_num < 0 ) {
2486 submodel_num = pm->detail[0];
2489 // Rotate eye pos into object coordinates
2490 vm_vec_sub(&tempv,pos,eye_pos );
2491 vm_vec_rotate(&eye_rel_pos,&tempv,orient );
2493 return interp_closest_dist_to_box( &closest_pos, &eye_rel_pos, &pm->submodel[submodel_num].min, &pm->submodel[submodel_num].max );
2501 dc_printf("Tiled textures\n");
2503 dc_printf("Non-tiled textures\n");
2507 extern void d3d_zbias(int bias);
2508 void model_really_render(int model_num, matrix *orient, vector * pos, uint flags, int light_ignore_id )
2510 int i, detail_level;
2512 uint save_gr_zbuffering_mode;
2515 MONITOR_INC( NumModelsRend, 1 );
2517 Interp_orient = orient;
2520 int tmp_detail_level = Game_detail_level;
2522 // if ( D3D_enabled ) {
2523 // tmp_detail_level = -1; // Force no hires models for Direct3D
2526 // Tmap_show_layers = 1;
2527 // model_set_detail_level(0);
2528 // flags |= MR_LOCK_DETAIL|MR_NO_TEXTURING|MR_NO_LIGHTING; //MR_LOCK_DETAIL | |MR_NO_LIGHTING|MR_NO_SMOOTHINGMR_NO_TEXTURING |
2530 // Turn off engine effect
2531 Interp_thrust_scale_subobj=0;
2533 if (!Model_texturing)
2534 flags |= MR_NO_TEXTURING;
2536 if ( !Model_polys ) {
2537 flags |= MR_NO_POLYS;
2540 Interp_flags = flags;
2542 pm = model_get(model_num);
2544 // Set the flags we will pass to the tmapper
2545 if ( D3D_enabled ) {
2546 Interp_tmap_flags = TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB;
2548 Interp_tmap_flags = TMAP_FLAG_GOURAUD | TMAP_FLAG_RAMP;
2551 // if we're in nebula mode
2552 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
2553 Interp_tmap_flags |= TMAP_FLAG_PIXEL_FOG;
2556 if ( !(Interp_flags & MR_NO_TEXTURING) ) {
2557 Interp_tmap_flags |= TMAP_FLAG_TEXTURED;
2559 if ( (pm->flags & PM_FLAG_ALLOW_TILING) && tiling)
2560 Interp_tmap_flags |= TMAP_FLAG_TILED;
2562 if ( !(Interp_flags & MR_NO_CORRECT) ) {
2563 Interp_tmap_flags |= TMAP_FLAG_CORRECT;
2567 save_gr_zbuffering_mode = gr_zbuffering_mode;
2568 zbuf_mode = gr_zbuffering_mode;
2570 if (!(Game_detail_flags & DETAIL_FLAG_MODELS) ) {
2571 gr_set_color(0,128,0);
2572 g3_draw_sphere_ez( pos, pm->rad );
2576 g3_start_instance_matrix(pos,orient);
2578 if ( Interp_flags & MR_SHOW_RADIUS ) {
2579 if ( !(Interp_flags & MR_SHOW_OUTLINE_PRESET) ) {
2580 gr_set_color(0,64,0);
2581 g3_draw_sphere_ez(&vmd_zero_vector,pm->rad);
2585 Assert( pm->n_detail_levels < MAX_MODEL_DETAIL_LEVELS );
2588 float depth = model_find_closest_point( &closest_pos, model_num, -1, orient, pos, &Eye_position );
2589 if ( pm->n_detail_levels > 1 ) {
2591 if ( Interp_flags & MR_LOCK_DETAIL ) {
2592 i = Interp_detail_level+1;
2595 //gr_set_color(0,128,0);
2596 //g3_draw_sphere_ez( &closest_pos, 2.0f );
2598 #if MAX_DETAIL_LEVEL != 4
2599 #error Code in modelInterp.cpp assumes MAX_DETAIL_LEVEL == 4
2602 switch( Detail.detail_distance ) {
2606 case 1: // lower than normal
2609 case 2: // default (leave the same)
2611 case 3: // above normal
2614 case 4: // even more normal
2620 if(The_mission.flags & MISSION_FLAG_FULLNEB){
2621 depth *= neb2_get_lod_scale(Interp_objnum);
2624 for ( i=0; i<pm->n_detail_levels; i++ ) {
2625 if ( depth<=pm->detail_depth[i] ){
2630 // If no valid detail depths specified, use highest.
2631 if ( (i > 1) && (pm->detail_depth[i-1] < 1.0f)) {
2637 // maybe force lower detail
2638 if (Interp_flags & MR_FORCE_LOWER_DETAIL) {
2642 //detail_level = fl2i(depth/10.0f);
2644 detail_level = i-1-tmp_detail_level;
2646 if ( detail_level < 0 )
2648 else if (detail_level >= pm->n_detail_levels )
2649 detail_level = pm->n_detail_levels-1;
2651 //mprintf(( "Depth = %.2f, detail = %d\n", depth, detail_level ));
2658 if ( detail_level==0 ) {
2659 MONITOR_INC( NumHiModelsRend, 1 );
2660 } else if ( detail_level ==pm->n_detail_levels-1 ) {
2661 MONITOR_INC( NumLowModelsRend, 1 );
2663 MONITOR_INC( NumMedModelsRend, 1 );
2667 if((Interp_flags & MR_AUTOCENTER) && (pm->flags & PM_FLAG_AUTOCEN)){
2668 vector auto_back = pm->autocenter;
2669 vm_vec_scale(&auto_back, -1.0f);
2670 g3_start_instance_matrix(&auto_back, NULL);
2673 if(gr_screen.mode == GR_DIRECT3D){
2677 // Draw the subobjects
2678 i = pm->submodel[pm->detail[detail_level]].first_child;
2681 if (!pm->submodel[i].is_thruster ) {
2682 zbuf_mode = GR_ZBUFF_WRITE;
2685 if(Interp_flags & MR_NO_ZBUFFER){
2686 zbuf_mode = GR_ZBUFF_NONE;
2689 gr_zbuffer_set(zbuf_mode);
2691 model_interp_subcall( pm, i, detail_level );
2693 i = pm->submodel[i].next_sibling;
2696 // rotate lights for the hull
2697 if ( !(Interp_flags & MR_NO_LIGHTING ) ) {
2701 if ( pm->submodel[pm->detail[detail_level]].num_children > 0 ){
2702 // zbuf_mode |= GR_ZBUFF_WRITE; // write only
2703 zbuf_mode = GR_ZBUFF_FULL;
2707 if(Interp_flags & MR_NO_ZBUFFER){
2708 zbuf_mode = GR_ZBUFF_NONE;
2711 gr_zbuffer_set(zbuf_mode);
2713 if(gr_screen.mode == GR_DIRECT3D){
2717 // draw the hull of the ship
2718 model_interp_sub( (ubyte *)pm->submodel[pm->detail[detail_level]].bsp_data, pm, &pm->submodel[pm->detail[detail_level]], 0 );
2720 if (Interp_flags & MR_SHOW_PIVOTS ) {
2721 model_draw_debug_points( pm, NULL );
2722 model_draw_debug_points( pm, &pm->submodel[pm->detail[detail_level]] );
2724 if(pm->flags & PM_FLAG_AUTOCEN){
2725 gr_set_color(255, 255, 255);
2726 g3_draw_sphere_ez(&pm->autocenter, pm->rad / 4.5f);
2730 if ( pm->submodel[pm->detail[0]].num_arcs ) {
2731 interp_render_lightning( pm, &pm->submodel[pm->detail[0]]);
2734 if ( Interp_flags & MR_SHOW_SHIELDS ) {
2735 model_render_shields(pm);
2738 // render model insignias
2739 if(gr_screen.mode == GR_DIRECT3D){
2742 gr_zbuffer_set(GR_ZBUFF_READ);
2743 model_render_insignias(pm, detail_level);
2746 if(gr_screen.mode == GR_DIRECT3D){
2750 // Draw the thruster glow
2751 if ( (Interp_thrust_glow_bitmap != -1) && (Interp_flags & MR_SHOW_THRUSTERS) /*&& (Detail.engine_glows)*/ ) {
2753 for (i = 0; i < pm->n_thrusters; i++ ) {
2754 thruster_bank *bank = &pm->thrusters[i];
2757 for ( j=0; j<bank->num_slots; j++ ) {
2760 vm_vec_sub(&tempv,&View_position,&bank->pnt[j]);
2761 vm_vec_normalize(&tempv);
2763 d = vm_vec_dot(&tempv,&bank->norm[j]);
2768 // Make glow bitmap fade in/out quicker from sides.
2770 if ( d > 1.0f ) d = 1.0f;
2772 // fade them in the nebula as well
2773 if(The_mission.flags & MISSION_FLAG_FULLNEB){
2774 d *= (1.0f - Interp_fog_level);
2777 //ADAM: Min throttle draws rad*MIN_SCALE, max uses max.
2778 #define NOISE_SCALE 0.5f
2779 #define MIN_SCALE 3.4f
2780 #define MAX_SCALE 4.7f
2781 float scale = MIN_SCALE;
2783 scale = (Interp_thrust_scale-0.1f)*(MAX_SCALE-MIN_SCALE)+MIN_SCALE;
2785 float w = bank->radius[j]*(scale+Interp_thrust_glow_noise*NOISE_SCALE );
2788 gr_fog_set(GR_FOGMODE_NONE, 0, 0, 0);
2790 g3_rotate_vertex( &p, &bank->pnt[j] );
2791 gr_set_bitmap( Interp_thrust_glow_bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, d );
2793 extern int Gr_scaler_zbuffering;
2794 Gr_scaler_zbuffering = 1;
2795 g3_draw_bitmap(&p,0,w*0.5f, TMAP_FLAG_TEXTURED );
2796 //g3_draw_rotated_bitmap(&p,0.0f,w,w, TMAP_FLAG_TEXTURED );
2797 Gr_scaler_zbuffering = 0;
2805 Interp_thrust_glow_bitmap = -1;
2809 // Draw the thruster subobjects
2810 i = pm->submodel[pm->detail[detail_level]].first_child;
2812 if (pm->submodel[i].is_thruster ) {
2813 zbuf_mode = GR_ZBUFF_READ;
2816 if(Interp_flags & MR_NO_ZBUFFER){
2817 zbuf_mode = GR_ZBUFF_NONE;
2820 gr_zbuffer_set(zbuf_mode);
2822 model_interp_subcall( pm, i, detail_level );
2824 i = pm->submodel[i].next_sibling;
2829 if ( Interp_flags & MR_SHOW_PATHS ){
2830 model_draw_paths(model_num);
2833 if (Interp_flags & MR_BAY_PATHS ){
2834 model_draw_bay_paths(model_num);
2837 if((Interp_flags & MR_AUTOCENTER) && (pm->flags & PM_FLAG_AUTOCEN)){
2842 gr_zbuffer_set(save_gr_zbuffering_mode);
2846 void submodel_render(int model_num, int submodel_num, matrix *orient, vector * pos, uint flags, int light_ignore_id)
2850 MONITOR_INC( NumModelsRend, 1 );
2852 if (!(Game_detail_flags & DETAIL_FLAG_MODELS) ) return;
2854 // Turn off engine effect
2855 Interp_thrust_scale_subobj=0;
2857 if (!Model_texturing)
2858 flags |= MR_NO_TEXTURING;
2860 Interp_flags = flags;
2862 pm = model_get(model_num);
2864 // Set the flags we will pass to the tmapper
2865 if ( D3D_enabled ) {
2866 Interp_tmap_flags = TMAP_FLAG_GOURAUD | TMAP_FLAG_RGB;
2868 Interp_tmap_flags = TMAP_FLAG_GOURAUD | TMAP_FLAG_RAMP;
2871 // if we're in nebula mode
2872 if((The_mission.flags & MISSION_FLAG_FULLNEB) && (Neb2_render_mode != NEB2_RENDER_NONE)){
2873 Interp_tmap_flags |= TMAP_FLAG_PIXEL_FOG;
2876 if ( !(Interp_flags & MR_NO_TEXTURING) ) {
2877 Interp_tmap_flags |= TMAP_FLAG_TEXTURED;
2879 if ( (pm->flags & PM_FLAG_ALLOW_TILING) && tiling )
2880 Interp_tmap_flags |= TMAP_FLAG_TILED;
2882 if ( !(Interp_flags & MR_NO_CORRECT) ) {
2883 Interp_tmap_flags |= TMAP_FLAG_CORRECT;
2887 if ( !(Interp_flags & MR_NO_LIGHTING ) ) {
2888 if ( D3D_enabled ) {
2889 light_filter_push( -1, pos, pm->submodel[submodel_num].rad );
2891 light_filter_push( light_ignore_id, pos, pm->submodel[submodel_num].rad );
2895 g3_start_instance_matrix(pos,orient);
2897 if ( !(Interp_flags & MR_NO_LIGHTING ) ) {
2901 model_interp_sub( pm->submodel[submodel_num].bsp_data, pm, &pm->submodel[submodel_num], 0 );
2902 if ( pm->submodel[submodel_num].num_arcs ) {
2903 interp_render_lightning( pm, &pm->submodel[submodel_num]);
2906 if (Interp_flags & MR_SHOW_PIVOTS )
2907 model_draw_debug_points( pm, &pm->submodel[submodel_num] );
2909 if ( !(Interp_flags & MR_NO_LIGHTING ) ) {
2918 // Fills in an array with points from a model.
2919 // Only gets up to max_num verts;
2920 // Returns number of verts found;
2921 static int submodel_get_points_internal(int model_num, int submodel_num, int max_num, vector **pnts, vector **norms )
2925 pm = model_get(model_num);
2927 if ( submodel_num < 0 ) {
2928 submodel_num = pm->detail[0];
2931 ubyte *p = pm->submodel[submodel_num].bsp_data;
2932 int chunk_type, chunk_size;
2935 chunk_size = w(p+4);
2937 while (chunk_type != OP_EOF) {
2938 switch (chunk_type) {
2939 case OP_EOF: return 1;
2940 case OP_DEFPOINTS: {
2942 int nverts = w(p+8);
2943 int offset = w(p+16);
2945 ubyte * normcount = p+20;
2946 vector *src = vp(p+offset);
2948 if ( nverts > max_num )
2951 for (n=0; n<nverts; n++ ) {
2953 *norms++ = src + 1; // first normal associated with the point
2955 src += normcount[n]+1;
2957 return nverts; // Read in 'n' points
2960 case OP_FLATPOLY: break;
2961 case OP_TMAPPOLY: break;
2962 case OP_SORTNORM: break;
2963 case OP_BOUNDBOX: break;
2965 mprintf(( "Bad chunk type %d, len=%d in submodel_get_points\n", chunk_type, chunk_size ));
2966 Int3(); // Bad chunk type!
2971 chunk_size = w(p+4);
2973 return 0; // Couldn't find 'em
2976 // Gets two random points on a model
2977 void submodel_get_two_random_points(int model_num, int submodel_num, vector *v1, vector *v2, vector *n1, vector *n2 )
2979 int nv = submodel_get_points_internal(model_num, submodel_num, MAX_POLYGON_VECS, Interp_verts, Interp_norms );
2981 int vn1 = (myrand()>>5) % nv;
2982 int vn2 = (myrand()>>5) % nv;
2984 *v1 = *Interp_verts[vn1];
2985 *v2 = *Interp_verts[vn2];
2988 *n1 = *Interp_norms[vn1];
2991 *n2 = *Interp_norms[vn2];
2995 // If MR_FLAG_OUTLINE bit set this color will be used for outlines.
2996 // This defaults to black.
2997 void model_set_outline_color(int r, int g, int b )
2999 gr_init_color( &Interp_outline_color, r, g, b );
3003 // If MR_FLAG_OUTLINE bit set this color will be used for outlines.
3004 // This defaults to black.
3005 void model_set_outline_color_fast(void *outline_color)
3007 Interp_outline_color = *((color*)(outline_color));
3010 // IF MR_LOCK_DETAIL is set, then it will always draw detail level 'n'
3011 // This defaults to 0. (0=highest, larger=lower)
3012 void model_set_detail_level(int n)
3014 Interp_detail_level = n;
3018 // Returns number of verts in a submodel;
3019 int submodel_get_num_verts(int model_num, int submodel_num )
3023 pm = model_get(model_num);
3025 ubyte *p = pm->submodel[submodel_num].bsp_data;
3026 int chunk_type, chunk_size;
3029 chunk_size = w(p+4);
3031 while (chunk_type != OP_EOF) {
3032 switch (chunk_type) {
3033 case OP_EOF: return 0;
3034 case OP_DEFPOINTS: {
3036 return n; // Read in 'n' points
3039 case OP_FLATPOLY: break;
3040 case OP_TMAPPOLY: break;
3041 case OP_SORTNORM: break;
3042 case OP_BOUNDBOX: break;
3044 mprintf(( "Bad chunk type %d, len=%d in submodel_get_num_verts\n", chunk_type, chunk_size ));
3045 Int3(); // Bad chunk type!
3050 chunk_size = w(p+4);
3052 return 0; // Couldn't find 'em
3055 // Returns number of tmaps & flat polys in a submodel;
3056 int submodel_get_num_polys_sub( ubyte *p )
3058 int chunk_type = w(p);
3059 int chunk_size = w(p+4);
3062 while (chunk_type != OP_EOF) {
3063 switch (chunk_type) {
3064 case OP_EOF: return n;
3065 case OP_DEFPOINTS: break;
3066 case OP_FLATPOLY: n++; break;
3067 case OP_TMAPPOLY: n++; break;
3069 int frontlist = w(p+36);
3070 int backlist = w(p+40);
3071 int prelist = w(p+44);
3072 int postlist = w(p+48);
3073 int onlist = w(p+52);
3074 n += submodel_get_num_polys_sub(p+frontlist);
3075 n += submodel_get_num_polys_sub(p+backlist);
3076 n += submodel_get_num_polys_sub(p+prelist);
3077 n += submodel_get_num_polys_sub(p+postlist );
3078 n += submodel_get_num_polys_sub(p+onlist );
3081 case OP_BOUNDBOX: break;
3083 mprintf(( "Bad chunk type %d, len=%d in submodel_get_num_polys\n", chunk_type, chunk_size ));
3084 Int3(); // Bad chunk type!
3089 chunk_size = w(p+4);
3094 // Returns number of tmaps & flat polys in a submodel;
3095 int submodel_get_num_polys(int model_num, int submodel_num )
3099 pm = model_get(model_num);
3101 return submodel_get_num_polys_sub( pm->submodel[submodel_num].bsp_data );
3105 // Sets the submodel instance data in a submodel
3106 // If show_damaged is true it shows only damaged submodels.
3107 // If it is false it shows only undamaged submodels.
3108 void model_show_damaged(int model_num, int show_damaged )
3113 pm = model_get(model_num);
3115 for (i=0; i<pm->n_models; i++ ) {
3116 bsp_info *sm = &pm->submodel[i];
3118 // Set the "blown out" flags
3122 for (i=0; i<pm->n_models; i++ ) {
3123 bsp_info *sm = &pm->submodel[i];
3125 // Set the "blown out" flags
3126 if ( show_damaged ) {
3127 if ( sm->my_replacement > -1 ) {
3128 pm->submodel[sm->my_replacement].blown_off = 0;
3132 if ( sm->my_replacement > -1 ) {
3133 pm->submodel[sm->my_replacement].blown_off = 1;
3140 // set the insignia bitmap to be used when rendering a ship with an insignia (-1 switches it off altogether)
3141 void model_set_insignia_bitmap(int bmap)
3143 Interp_insignia_bitmap = bmap;
3146 // set the forces bitmap
3147 void model_set_forced_texture(int bmap)
3149 Interp_forced_bitmap = bmap;
3152 // set model transparency for use with MR_ALL_XPARENT
3153 void model_set_alpha(float alpha)
3155 Interp_xparent_alpha = alpha;
3158 // see if the given texture is used by the passed model. 0 if not used, 1 if used, -1 on error
3159 int model_find_texture(int model_num, int bitmap)
3164 // get a handle to the model
3165 pm = model_get(model_num);
3171 for(idx=0; idx<pm->n_textures; idx++){
3172 if(pm->textures[idx] == bitmap){
3181 // find closest point on extended bounding box (the bounding box plus all the planes that make it up)
3182 // returns closest distance to extended box
3183 // positive return value means start_point is outside extended box
3184 // displaces closest point an optional amount delta to the outside of the box
3185 // closest_box_point can be NULL.
3186 float get_model_closest_box_point_with_delta(vector *closest_box_point, vector *start_point, int modelnum, int *is_inside, float delta)
3189 vector box_point, ray_direction, *extremes;
3190 float dist, best_dist;
3193 int masks[6] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20};
3194 int mask_inside = 0x3f;
3196 best_dist = FLT_MAX;
3197 pm = model_get(modelnum);
3199 for (i=0; i<6; i++) {
3200 idx = i / 2; // which row vector of Identity matrix
3202 memcpy(&ray_direction, vmd_identity_matrix.a2d[idx], sizeof(vector));
3204 // do negative, then positive plane for each axis
3206 extremes = &pm->mins;
3207 vm_vec_negate(&ray_direction);
3209 extremes = &pm->maxs;
3212 // a negative distance means started outside the box
3213 dist = fvi_ray_plane(&box_point, extremes, &ray_direction, start_point, &ray_direction, 0.0f);
3217 if (fabs(dist) < fabs(best_dist)) {
3219 if (closest_box_point) {
3220 vm_vec_scale_add(closest_box_point, &box_point, &ray_direction, delta);
3225 // is start_point inside the box
3227 *is_inside = (inside == mask_inside);
3233 // find closest point on extended bounding box (the bounding box plus all the planes that make it up)
3234 // returns closest distance to extended box
3235 // positive return value means start_point is outside extended box
3236 // displaces closest point an optional amount delta to the outside of the box
3237 // closest_box_point can be NULL.
3238 float get_world_closest_box_point_with_delta(vector *closest_box_point, object *box_obj, vector *start_point, int *is_inside, float delta)
3240 vector temp, box_start;
3245 modelnum = Ships[box_obj->instance].modelnum;
3247 // rotate start_point to box_obj RF
3248 vm_vec_sub(&temp, start_point, &box_obj->pos);
3249 vm_vec_rotate(&box_start, &temp, &box_obj->orient);
3251 dist = get_model_closest_box_point_with_delta(closest_box_point, &box_start, modelnum, is_inside, delta);
3253 // rotate closest_box_point to world RF
3254 if (closest_box_point) {
3255 vm_vec_unrotate(&temp, closest_box_point, &box_obj->orient);
3256 vm_vec_add(closest_box_point, &temp, &box_obj->pos);
3262 void model_set_fog_level(float l)
3264 Interp_fog_level = l;
3267 // given a newly loaded model, page in all textures
3268 void model_page_in_textures(int modelnum, int ship_info_index)
3274 if((ship_info_index < 0) || (ship_info_index >= Num_ship_types)){
3277 sip = &Ship_info[ship_info_index];
3279 polymodel *pm = model_get(modelnum);
3286 // set nondarkening pixels
3287 if(sip->num_nondark_colors){
3288 palman_set_nondarkening(sip->nondark_colors, sip->num_nondark_colors);
3290 // use the colors from the default table
3292 palman_set_nondarkening(Palman_non_darkening_default, Palman_num_nondarkening_default);
3295 for (idx=0; idx<pm->n_textures; idx++ ){
3296 int bitmap_num = pm->original_textures[idx];
3298 if ( bitmap_num > -1 ) {
3299 // if we're in Glide (and maybe later with D3D), use nondarkening textures
3300 if(gr_screen.mode == GR_GLIDE){
3301 bm_lock(bitmap_num, 16, BMP_TEX_NONDARK);
3302 bm_unlock(bitmap_num);
3304 bm_lock(bitmap_num, 16, BMP_TEX_OTHER);
3305 bm_unlock(bitmap_num);
3311 // is the given model a pirate ship?
3312 int model_is_pirate_ship(int modelnum)