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/Render/3dLaser.cpp $
15 * Code to draw 3d looking lasers
18 * Revision 1.4 2002/06/17 06:33:10 relnev
19 * ryan's struct patch for gcc 2.95
21 * Revision 1.3 2002/06/09 04:41:25 relnev
22 * added copyright header
24 * Revision 1.2 2002/05/07 03:16:51 theoddone33
25 * The Great Newline Fix
27 * Revision 1.1.1.1 2002/05/03 03:28:10 root
31 * 5 7/30/99 7:01p Dave
32 * Dogfight escort gauge. Fixed up laser rendering in Glide.
34 * 4 7/27/99 3:09p Dave
35 * Made g400 work. Whee.
37 * 3 5/27/99 6:17p Dave
38 * Added in laser glows.
40 * 2 10/07/98 10:53a Dave
43 * 1 10/07/98 10:51a Dave
45 * 26 5/13/98 3:10p John
46 * made detail slider for weapon rendering change the distance that lasers
47 * become non-textured. The lowest setting turns off missile trail
50 * 25 5/05/98 11:15p Lawrance
51 * Optimize weapon flyby sound determination
53 * 24 4/10/98 5:20p John
54 * Changed RGB in lighting structure to be ubytes. Removed old
55 * not-necessary 24 bpp software stuff.
57 * 23 3/17/98 3:02p John
58 * made lasers draw as non-textured only on software.
60 * 22 3/17/98 3:01p John
61 * Make thicker lasers not render as polys further away.
63 * 21 2/26/98 3:26p John
64 * Added code to turn off laser rendering
66 * 20 1/29/98 9:46a John
67 * Capped debris length
69 * 19 1/23/98 5:08p John
70 * Took L out of vertex structure used B (blue) instead. Took all small
71 * fireballs out of fireball types and used particles instead. Fixed some
72 * debris explosion things. Restructured fireball code. Restructured
73 * some lighting code. Made dynamic lighting on by default. Made groups
74 * of lasers only cast one light. Made fireballs not cast light.
76 * 18 1/19/98 8:51a John
77 * Did a quick out if both points of laser off same side of view pyramid.
79 * 17 1/06/98 6:18p John
80 * Made debris render as a blur. Had to make g3_draw_laser take tmap
83 * 16 12/15/97 10:09p John
84 * put in some debug laser code.
86 * 15 12/15/97 11:32a John
89 * 14 10/23/97 8:32p John
90 * Increased laser to dot distance
92 * 13 9/20/97 9:45a John
93 * made lasers not draw as pixels as fast as before if viewing from the
97 * 12 8/19/97 11:42p Lawrance
98 * use atan2_safe() instead of atan2()
100 * 11 8/19/97 12:54p Sandeep
101 * Fixed lasers to work in optimized build
103 * 10 6/24/97 6:22p John
104 * added detail flags.
105 * sped up motion debris system a bit.
107 * 9 5/29/97 3:10p John
108 * Took out debug menu.
109 * Made software scaler draw larger bitmaps.
110 * Optimized Direct3D some.
112 * 8 4/29/97 9:55a John
114 * 7 4/22/97 4:11p John
115 * New debug var stuff
117 * 6 3/28/97 6:10p Lawrance
118 * draw lasers so end appears at gun mount tip on first frame
120 * 5 3/06/97 8:48a John
121 * fixed bug with lasers drawing too close.
123 * 4 2/07/97 9:07a John
124 * made lasers not fade by default.
126 * 3 2/03/97 8:01p John
127 * Added debug code to fade lasers out with distance.
129 * 2 12/11/96 12:41p John
130 * Added new code to draw 3d laser using 2d ellipses.
132 * 1 12/11/96 12:13p John
139 #include "3dinternal.h"
140 #include "systemvars.h"
144 DCF_BOOL( lasers, Lasers );
146 // This assumes you have already set a color with gr_set_color or gr_set_color_fast
147 // and a bitmap with gr_set_bitmap. If it is very far away, it draws the laser
148 // as flat-shaded using current color, else textured using current texture.
149 // If max_len is > 1.0, then this caps the length to be no longer than max_len pixels.
150 float g3_draw_laser(vector *headp, float head_width, vector *tailp, float tail_width, uint tmap_flags, float max_len )
156 float headx, heady, headr, tailx, taily, tailr;
160 SDL_assert( G3_count == 1 );
162 g3_rotate_vertex(&pt1,headp);
164 g3_project_vertex(&pt1);
165 if (pt1.flags & PF_OVERFLOW)
168 g3_rotate_vertex(&pt2,tailp);
170 g3_project_vertex(&pt2);
171 if (pt2.flags & PF_OVERFLOW)
174 if ( (pt1.codes & pt2.codes) != 0 ) {
175 // Both off the same side
181 headr = (head_width*Matrix_scale.xyz.x*Canv_w2*pt1.sw);
185 tailr = (tail_width*Matrix_scale.xyz.x*Canv_w2*pt2.sw);
187 float len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
189 // Cap the length if needed.
190 if ( (max_len > 1.0f) && (len_2d > max_len) ) {
191 float ratio = max_len / len_2d;
193 tailx = headx + ( tailx - headx ) * ratio;
194 taily = heady + ( taily - heady ) * ratio;
195 tailr = headr + ( tailr - headr ) * ratio;
197 len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
200 depth = (pt1.z+pt2.z)*0.5f;
210 float mx, my, w, h1,h2;
212 if ( len_2d < max_r ) {
214 h1 = headr + (max_r-len_2d);
215 if ( h1 > max_r ) h1 = max_r;
216 h2 = tailr + (max_r-len_2d);
217 if ( h2 > max_r ) h2 = max_r;
220 if ( fl_abs(tailx - headx) > 0.01f ) {
221 a = (float)atan2( taily-heady, tailx-headx );
228 a = atan2_safe( taily-heady, tailx-headx );
236 mx = (tailx+headx)/2.0f;
237 my = (taily+heady)/2.0f;
239 // gr_set_color(255,0,0);
240 // g3_draw_line( &pt1, &pt2 );
242 // gr_set_color( 255, 0, 0 );
243 // gr_pixel( fl2i(mx),fl2i(my) );
245 // Draw box with width 'w' and height 'h' at angle 'a' from horizontal
246 // centered around mx, my
248 if ( h1 < 1.0f ) h1 = 1.0f;
249 if ( h2 < 1.0f ) h2 = 1.0f;
257 vertex *vertlist[4] = { &v[3], &v[2], &v[1], &v[0] };
259 if ( depth < 0.0f ) depth = 0.0f;
261 v[0].sx = (-w/2.0f)*ca + (-h1/2.0f)*sa + mx;
262 v[0].sy = (-w/2.0f)*sa - (-h1/2.0f)*ca + my;
269 v[1].sx = (w/2.0f)*ca + (-h2/2.0f)*sa + mx;
270 v[1].sy = (w/2.0f)*sa - (-h2/2.0f)*ca + my;
277 v[2].sx = (w/2.0f)*ca + (h2/2.0f)*sa + mx;
278 v[2].sy = (w/2.0f)*sa - (h2/2.0f)*ca + my;
285 v[3].sx = (-w/2.0f)*ca + (h1/2.0f)*sa + mx;
286 v[3].sy = (-w/2.0f)*sa - (h1/2.0f)*ca + my;
293 gr_tmapper(4, vertlist, tmap_flags | TMAP_FLAG_CORRECT);
298 // Draw a laser shaped 3d looking thing using vertex coloring (useful for things like colored laser glows)
299 // If max_len is > 1.0, then this caps the length to be no longer than max_len pixels
300 float g3_draw_laser_rgb(vector *headp, float head_width, vector *tailp, float tail_width, int r, int g, int b, uint tmap_flags, float max_len )
306 float headx, heady, headr, tailx, taily, tailr;
310 SDL_assert( G3_count == 1 );
312 g3_rotate_vertex(&pt1,headp);
314 g3_project_vertex(&pt1);
315 if (pt1.flags & PF_OVERFLOW)
318 g3_rotate_vertex(&pt2,tailp);
320 g3_project_vertex(&pt2);
321 if (pt2.flags & PF_OVERFLOW)
324 if ( (pt1.codes & pt2.codes) != 0 ) {
325 // Both off the same side
331 headr = (head_width*Matrix_scale.xyz.x*Canv_w2*pt1.sw);
335 tailr = (tail_width*Matrix_scale.xyz.x*Canv_w2*pt2.sw);
337 float len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
339 // Cap the length if needed.
340 if ( (max_len > 1.0f) && (len_2d > max_len) ) {
341 float ratio = max_len / len_2d;
343 tailx = headx + ( tailx - headx ) * ratio;
344 taily = heady + ( taily - heady ) * ratio;
345 tailr = headr + ( tailr - headr ) * ratio;
347 len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
350 depth = (pt1.z+pt2.z)*0.5f;
360 float mx, my, w, h1,h2;
362 if ( len_2d < max_r ) {
364 h1 = headr + (max_r-len_2d);
365 if ( h1 > max_r ) h1 = max_r;
366 h2 = tailr + (max_r-len_2d);
367 if ( h2 > max_r ) h2 = max_r;
370 if ( fl_abs(tailx - headx) > 0.01f ) {
371 a = (float)atan2( taily-heady, tailx-headx );
378 a = atan2_safe( taily-heady, tailx-headx );
386 mx = (tailx+headx)/2.0f;
387 my = (taily+heady)/2.0f;
389 // gr_set_color(255,0,0);
390 // g3_draw_line( &pt1, &pt2 );
392 // gr_set_color( 255, 0, 0 );
393 // gr_pixel( fl2i(mx),fl2i(my) );
395 // Draw box with width 'w' and height 'h' at angle 'a' from horizontal
396 // centered around mx, my
398 if ( h1 < 1.0f ) h1 = 1.0f;
399 if ( h2 < 1.0f ) h2 = 1.0f;
407 vertex *vertlist[4] = { &v[3], &v[2], &v[1], &v[0] };
409 if ( depth < 0.0f ) depth = 0.0f;
411 v[0].sx = (-w/2.0f)*ca + (-h1/2.0f)*sa + mx;
412 v[0].sy = (-w/2.0f)*sa - (-h1/2.0f)*ca + my;
422 v[1].sx = (w/2.0f)*ca + (-h2/2.0f)*sa + mx;
423 v[1].sy = (w/2.0f)*sa - (-h2/2.0f)*ca + my;
433 v[2].sx = (w/2.0f)*ca + (h2/2.0f)*sa + mx;
434 v[2].sy = (w/2.0f)*sa - (h2/2.0f)*ca + my;
444 v[3].sx = (-w/2.0f)*ca + (h1/2.0f)*sa + mx;
445 v[3].sy = (-w/2.0f)*sa - (h1/2.0f)*ca + my;
455 gr_tmapper(4, vertlist, tmap_flags | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD | TMAP_FLAG_CORRECT);