2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Weapon/Trails.cpp $
15 * Code for missile trails
18 * Revision 1.3 2002/06/09 04:41:29 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/07 03:16:53 theoddone33
22 * The Great Newline Fix
24 * Revision 1.1.1.1 2002/05/03 03:28:11 root
28 * 7 6/23/99 2:23p Mattk
29 * Fixed detail level trail rendering problem.
31 * 6 6/22/99 7:03p Dave
32 * New detail options screen.
34 * 5 2/23/99 8:11p Dave
35 * Tidied up dogfight mode. Fixed TvT ship type problems for alpha wing.
36 * Small pass over todolist items.
38 * 4 2/17/99 2:11p Dave
39 * First full run of squad war. All freespace and tracker side stuff
42 * 3 11/14/98 5:33p Dave
43 * Lots of nebula work. Put in ship contrails.
45 * 2 10/07/98 10:54a Dave
48 * 1 10/07/98 10:51a Dave
50 * 9 5/13/98 3:10p John
51 * made detail slider for weapon rendering change the distance that lasers
52 * become non-textured. The lowest setting turns off missile trail
55 * 8 5/08/98 7:09p Dave
56 * Lots of UI tweaking.
58 * 7 4/10/98 5:20p John
59 * Changed RGB in lighting structure to be ubytes. Removed old
60 * not-necessary 24 bpp software stuff.
62 * 6 3/31/98 5:19p John
63 * Removed demo/save/restore. Made NDEBUG defined compile. Removed a
64 * bunch of debug stuff out of player file. Made model code be able to
65 * unload models and malloc out only however many models are needed.
68 * 5 3/23/98 5:00p John
69 * Improved missile trails. Made smooth alpha under hardware. Made end
70 * taper. Made trail touch weapon.
72 * 4 1/23/98 5:08p John
73 * Took L out of vertex structure used B (blue) instead. Took all small
74 * fireballs out of fireball types and used particles instead. Fixed some
75 * debris explosion things. Restructured fireball code. Restructured
76 * some lighting code. Made dynamic lighting on by default. Made groups
77 * of lasers only cast one light. Made fireballs not cast light.
79 * 3 1/15/98 11:13a John
80 * Added code for specifying weapon trail bitmaps in weapons.tbl
82 * 2 12/21/97 6:15p John
83 * Made a seperate system for missile trails
85 * 1 12/21/97 5:30p John
92 #include "freespace.h"
96 #include "3dinternal.h"
101 #define MAX_TRAILS MAX_WEAPONS
103 // Stuff for missile trails doesn't need to be saved or restored... or does it?
104 typedef struct trail {
105 int head, tail; // pointers into the queue for the trail points
106 vector pos[NUM_TRAIL_SECTIONS]; // positions of trail points
107 float val[NUM_TRAIL_SECTIONS]; // for each point, a value that tells how much to fade out
108 int object_died; // set to zero as long as object
109 int trail_stamp; // trail timestamp
112 trail_info info; // this is passed when creating a trail
120 trail Trails[MAX_TRAILS];
122 trail Trail_free_list;
123 trail Trail_used_list;
125 // Reset everything between levels
126 void trail_level_init()
131 list_init( &Trail_free_list );
132 list_init( &Trail_used_list );
134 // Link all object slots into the free list
135 for (i=0; i<MAX_TRAILS; i++) {
136 list_append(&Trail_free_list, &Trails[i] );
140 //returns the number of a free trail
141 //returns -1 if no free trails
142 int trail_create(trail_info info)
147 // standalone server should never create trails
148 if(Game_mode & GM_STANDALONE_SERVER){
152 if ( !Detail.weapon_extras ) {
153 // No trails at slot 0
157 if (Num_trails >= MAX_TRAILS ) {
159 mprintf(("Trail creation failed - too many trails!\n" ));
164 // Find next available trail
165 trailp = GET_FIRST(&Trail_free_list);
166 Assert( trailp != &Trail_free_list ); // shouldn't have the dummy element
168 // remove trailp from the free list
169 list_remove( &Trail_free_list, trailp );
171 // insert trailp onto the end of used list
172 list_append( &Trail_used_list, trailp );
178 trail_num = trailp-Trails;
180 // Init the trail data
184 trailp->object_died = 0;
185 trailp->trail_stamp = timestamp(trailp->info.stamp);
190 // output top and bottom vectors
191 // fvec == forward vector (eye viewpoint basically. in world coords)
192 // pos == world coordinate of the point we're calculating "around"
193 // w == width of the diff between top and bottom around pos
194 void trail_calc_facing_pts( vector *top, vector *bot, vector *fvec, vector *pos, float w )
198 vm_vec_sub( &rvec, &Eye_position, pos );
199 vm_vec_normalize( &rvec );
201 vm_vec_crossprod(&uvec,fvec,&rvec);
202 vm_vec_normalize(&uvec);
204 vm_vec_scale_add( top, pos, &uvec, w/2.0f );
205 vm_vec_scale_add( bot, pos, &uvec, -w/2.0f );
209 int trail_is_on_ship(int trail_index, ship *shipp)
213 for(idx=0; idx<MAX_SHIP_CONTRAILS; idx++){
214 if(shipp->trail_num[idx] == (short)trail_index){
223 // Render the trail behind a missile.
224 // Basically a queue of points that face the viewer.
225 void trail_render( trail * trailp )
229 if ( trailp->tail == trailp->head ) return;
233 vector topv, botv, *fvec, last_pos, tmp_fvec;
234 vertex last_top, last_bot, top, bot;
236 int sections[NUM_TRAIL_SECTIONS];
237 int num_sections = 0;
239 int n = trailp->tail;
241 // if this trail is on the player ship, and he's in any padlock view except rear view, don't draw
242 if((Player_ship != NULL) && trail_is_on_ship(trailp - Trails, Player_ship) && (Viewer_mode & (VM_PADLOCK_UP | VM_PADLOCK_LEFT | VM_PADLOCK_RIGHT)) ){
248 if ( n < 0 ) n = NUM_TRAIL_SECTIONS-1;
251 if ( trailp->val[n] > 1.0f ) {
255 sections[num_sections++] = n;
257 } while ( n != trailp->head );
261 for (i=0; i<num_sections; i++ ) {
268 w = trailp->val[n]*(ti->w_end - ti->w_start) + ti->w_start;
269 l = (ubyte)fl2i((trailp->val[n]*(ti->a_end - ti->a_start) + ti->a_start)*255.0f);
273 pos = trailp->pos[n];
277 //&objp->orient.fvec;
278 if ( num_sections > 1 ) {
280 vm_vec_sub(&tmp_fvec, &pos, &trailp->pos[sections[i+1]] );
281 vm_vec_normalize_safe(&tmp_fvec);
291 vm_vec_sub(&tmp_fvec, &last_pos, &pos );
292 vm_vec_normalize_safe(&tmp_fvec);
296 trail_calc_facing_pts( &topv, &botv, fvec, &pos, w );
298 g3_rotate_vertex( &top, &topv );
299 g3_rotate_vertex( &bot, &botv );
304 if ( i == num_sections-1 ) {
307 vm_vec_avg( ¢erv, &topv, &botv );
309 g3_rotate_vertex( ¢er, ¢erv );
313 vlist[0] = &last_top;
314 vlist[1] = &last_bot;
317 vlist[0]->u = 0.0f; vlist[0]->v = 1.0f;
318 vlist[1]->u = 0.0f; vlist[1]->v = 0.0f;
319 vlist[2]->u = 1.0f; vlist[2]->v = 0.5f;
321 gr_set_bitmap(ti->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, l/255.0f );
323 g3_draw_poly( 3, vlist, TMAP_FLAG_TEXTURED|TMAP_FLAG_ALPHA|TMAP_FLAG_GOURAUD );
325 g3_draw_poly( 3, vlist, TMAP_FLAG_TEXTURED );
331 vlist[0] = &last_bot;
334 vlist[3] = &last_top;
336 vlist[0]->u = 0.0f; vlist[0]->v = 0.0f;
337 vlist[1]->u = 1.0f; vlist[1]->v = 0.0f;
338 vlist[2]->u = 1.0f; vlist[2]->v = 1.0f;
339 vlist[3]->u = 0.0f; vlist[3]->v = 1.0f;
341 gr_set_bitmap(ti->bitmap, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, l/255.0f );
343 g3_draw_poly( 4, vlist, TMAP_FLAG_TEXTURED|TMAP_FLAG_ALPHA|TMAP_FLAG_GOURAUD );
345 g3_draw_poly( 4, vlist, TMAP_FLAG_TEXTURED );
356 void trail_add_segment( int trail_num, vector *pos )
358 if (trail_num < 0 ) return;
359 if (trail_num >= MAX_TRAILS ) return;
361 trail *trailp = &Trails[trail_num];
363 int next = trailp->tail;
365 if ( trailp->tail >= NUM_TRAIL_SECTIONS )
368 if ( trailp->head == trailp->tail ) {
371 if ( trailp->head >= NUM_TRAIL_SECTIONS )
375 trailp->pos[next] = *pos;
376 trailp->val[next] = 0.0f;
379 void trail_set_segment( int trail_num, vector *pos )
381 if (trail_num < 0 ) return;
382 if (trail_num >= MAX_TRAILS ) return;
384 trail *trailp = &Trails[trail_num];
386 int next = trailp->tail-1;
388 next = NUM_TRAIL_SECTIONS-1;
391 trailp->pos[next] = *pos;
394 void trail_move_all(float frametime)
398 trailp=GET_FIRST(&Trail_used_list);
400 while ( trailp!=END_OF_LIST(&Trail_used_list) ) {
402 int num_alive_segments = 0;
404 if ( trailp->tail != trailp->head ) {
405 int n = trailp->tail;
406 float time_delta = frametime / trailp->info.max_life;
409 if ( n < 0 ) n = NUM_TRAIL_SECTIONS-1;
411 trailp->val[n] += time_delta;
413 if ( trailp->val[n] <= 1.0f ) {
414 num_alive_segments++; // Record how many still alive.
417 } while ( n != trailp->head );
420 if ( trailp->object_died && (num_alive_segments < 1) ) {
421 // delete it from the list!
422 trail *next_one = GET_NEXT(trailp);
424 // remove objp from the used list
425 list_remove( &Trail_used_list, trailp);
427 // add objp to the end of the free
428 list_append( &Trail_free_list, trailp );
433 Assert(Num_trails >= 0);
437 trailp=GET_NEXT(trailp);
442 void trail_object_died( int trail_num )
444 if (trail_num < 0 ) return;
445 if (trail_num >= MAX_TRAILS ) return;
447 trail *trailp = &Trails[trail_num];
449 trailp->object_died++;
452 void trail_render_all()
456 if ( !Detail.weapon_extras ) {
457 // No trails at slot 0
461 trailp=GET_FIRST(&Trail_used_list);
463 while ( trailp!=END_OF_LIST(&Trail_used_list) ) {
464 trail_render(trailp);
465 trailp=GET_NEXT(trailp);
469 int trail_stamp_elapsed(int trail_num)
471 return timestamp_elapsed(Trails[trail_num].trail_stamp);
474 void trail_set_stamp(int trail_num)
476 Trails[trail_num].trail_stamp = timestamp(Trails[trail_num].info.stamp);