2 * $Logfile: /Freespace2/code/Render/3dLaser.cpp $
7 * Code to draw 3d looking lasers
10 * Revision 1.2 2002/05/07 03:16:51 theoddone33
11 * The Great Newline Fix
13 * Revision 1.1.1.1 2002/05/03 03:28:10 root
17 * 5 7/30/99 7:01p Dave
18 * Dogfight escort gauge. Fixed up laser rendering in Glide.
20 * 4 7/27/99 3:09p Dave
21 * Made g400 work. Whee.
23 * 3 5/27/99 6:17p Dave
24 * Added in laser glows.
26 * 2 10/07/98 10:53a Dave
29 * 1 10/07/98 10:51a Dave
31 * 26 5/13/98 3:10p John
32 * made detail slider for weapon rendering change the distance that lasers
33 * become non-textured. The lowest setting turns off missile trail
36 * 25 5/05/98 11:15p Lawrance
37 * Optimize weapon flyby sound determination
39 * 24 4/10/98 5:20p John
40 * Changed RGB in lighting structure to be ubytes. Removed old
41 * not-necessary 24 bpp software stuff.
43 * 23 3/17/98 3:02p John
44 * made lasers draw as non-textured only on software.
46 * 22 3/17/98 3:01p John
47 * Make thicker lasers not render as polys further away.
49 * 21 2/26/98 3:26p John
50 * Added code to turn off laser rendering
52 * 20 1/29/98 9:46a John
53 * Capped debris length
55 * 19 1/23/98 5:08p John
56 * Took L out of vertex structure used B (blue) instead. Took all small
57 * fireballs out of fireball types and used particles instead. Fixed some
58 * debris explosion things. Restructured fireball code. Restructured
59 * some lighting code. Made dynamic lighting on by default. Made groups
60 * of lasers only cast one light. Made fireballs not cast light.
62 * 18 1/19/98 8:51a John
63 * Did a quick out if both points of laser off same side of view pyramid.
65 * 17 1/06/98 6:18p John
66 * Made debris render as a blur. Had to make g3_draw_laser take tmap
69 * 16 12/15/97 10:09p John
70 * put in some debug laser code.
72 * 15 12/15/97 11:32a John
75 * 14 10/23/97 8:32p John
76 * Increased laser to dot distance
78 * 13 9/20/97 9:45a John
79 * made lasers not draw as pixels as fast as before if viewing from the
83 * 12 8/19/97 11:42p Lawrance
84 * use atan2_safe() instead of atan2()
86 * 11 8/19/97 12:54p Sandeep
87 * Fixed lasers to work in optimized build
89 * 10 6/24/97 6:22p John
91 * sped up motion debris system a bit.
93 * 9 5/29/97 3:10p John
94 * Took out debug menu.
95 * Made software scaler draw larger bitmaps.
96 * Optimized Direct3D some.
98 * 8 4/29/97 9:55a John
100 * 7 4/22/97 4:11p John
101 * New debug var stuff
103 * 6 3/28/97 6:10p Lawrance
104 * draw lasers so end appears at gun mount tip on first frame
106 * 5 3/06/97 8:48a John
107 * fixed bug with lasers drawing too close.
109 * 4 2/07/97 9:07a John
110 * made lasers not fade by default.
112 * 3 2/03/97 8:01p John
113 * Added debug code to fade lasers out with distance.
115 * 2 12/11/96 12:41p John
116 * Added new code to draw 3d laser using 2d ellipses.
118 * 1 12/11/96 12:13p John
125 #include "3dinternal.h"
126 #include "systemvars.h"
130 DCF_BOOL( lasers, Lasers );
132 // This assumes you have already set a color with gr_set_color or gr_set_color_fast
133 // and a bitmap with gr_set_bitmap. If it is very far away, it draws the laser
134 // as flat-shaded using current color, else textured using current texture.
135 // If max_len is > 1.0, then this caps the length to be no longer than max_len pixels.
136 float g3_draw_laser(vector *headp, float head_width, vector *tailp, float tail_width, uint tmap_flags, float max_len )
142 float headx, heady, headr, tailx, taily, tailr;
147 Assert( G3_count == 1 );
149 g3_rotate_vertex(&pt1,headp);
151 g3_project_vertex(&pt1);
152 if (pt1.flags & PF_OVERFLOW)
155 g3_rotate_vertex(&pt2,tailp);
157 g3_project_vertex(&pt2);
158 if (pt2.flags & PF_OVERFLOW)
161 if ( (pt1.codes & pt2.codes) != 0 ) {
162 // Both off the same side
168 headr = (head_width*Matrix_scale.x*Canv_w2*pt1.sw);
172 tailr = (tail_width*Matrix_scale.x*Canv_w2*pt2.sw);
174 float len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
176 // Cap the length if needed.
177 if ( (max_len > 1.0f) && (len_2d > max_len) ) {
178 float ratio = max_len / len_2d;
180 tailx = headx + ( tailx - headx ) * ratio;
181 taily = heady + ( taily - heady ) * ratio;
182 tailr = headr + ( tailr - headr ) * ratio;
184 len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
187 depth = (pt1.z+pt2.z)*0.5f;
197 float mx, my, w, h1,h2;
199 if ( len_2d < max_r ) {
201 h1 = headr + (max_r-len_2d);
202 if ( h1 > max_r ) h1 = max_r;
203 h2 = tailr + (max_r-len_2d);
204 if ( h2 > max_r ) h2 = max_r;
207 if ( fl_abs(tailx - headx) > 0.01f ) {
208 a = (float)atan2( taily-heady, tailx-headx );
217 a = atan2_safe( taily-heady, tailx-headx );
226 mx = (tailx+headx)/2.0f;
227 my = (taily+heady)/2.0f;
229 // gr_set_color(255,0,0);
230 // g3_draw_line( &pt1, &pt2 );
232 // gr_set_color( 255, 0, 0 );
233 // gr_pixel( fl2i(mx),fl2i(my) );
235 // Draw box with width 'w' and height 'h' at angle 'a' from horizontal
236 // centered around mx, my
238 if ( h1 < 1.0f ) h1 = 1.0f;
239 if ( h2 < 1.0f ) h2 = 1.0f;
247 vertex *vertlist[4] = { &v[3], &v[2], &v[1], &v[0] };
250 if ( depth < 0.0f ) depth = 0.0f;
253 v[0].sx = (-w/2.0f)*ca + (-h1/2.0f)*sa + mx;
254 v[0].sy = (-w/2.0f)*sa - (-h1/2.0f)*ca + my;
261 v[1].sx = (w/2.0f)*ca + (-h2/2.0f)*sa + mx;
262 v[1].sy = (w/2.0f)*sa - (-h2/2.0f)*ca + my;
269 v[2].sx = (w/2.0f)*ca + (h2/2.0f)*sa + mx;
270 v[2].sy = (w/2.0f)*sa - (h2/2.0f)*ca + my;
277 v[3].sx = (-w/2.0f)*ca + (h1/2.0f)*sa + mx;
278 v[3].sy = (-w/2.0f)*sa - (h1/2.0f)*ca + my;
285 if(gr_screen.mode == GR_GLIDE){
286 gr_tmapper(4, vertlist, tmap_flags);
288 gr_tmapper(4, vertlist, tmap_flags | TMAP_FLAG_CORRECT);
294 // Draw a laser shaped 3d looking thing using vertex coloring (useful for things like colored laser glows)
295 // If max_len is > 1.0, then this caps the length to be no longer than max_len pixels
296 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 )
302 float headx, heady, headr, tailx, taily, tailr;
307 Assert( G3_count == 1 );
309 g3_rotate_vertex(&pt1,headp);
311 g3_project_vertex(&pt1);
312 if (pt1.flags & PF_OVERFLOW)
315 g3_rotate_vertex(&pt2,tailp);
317 g3_project_vertex(&pt2);
318 if (pt2.flags & PF_OVERFLOW)
321 if ( (pt1.codes & pt2.codes) != 0 ) {
322 // Both off the same side
328 headr = (head_width*Matrix_scale.x*Canv_w2*pt1.sw);
332 tailr = (tail_width*Matrix_scale.x*Canv_w2*pt2.sw);
334 float len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
336 // Cap the length if needed.
337 if ( (max_len > 1.0f) && (len_2d > max_len) ) {
338 float ratio = max_len / len_2d;
340 tailx = headx + ( tailx - headx ) * ratio;
341 taily = heady + ( taily - heady ) * ratio;
342 tailr = headr + ( tailr - headr ) * ratio;
344 len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
347 depth = (pt1.z+pt2.z)*0.5f;
357 float mx, my, w, h1,h2;
359 if ( len_2d < max_r ) {
361 h1 = headr + (max_r-len_2d);
362 if ( h1 > max_r ) h1 = max_r;
363 h2 = tailr + (max_r-len_2d);
364 if ( h2 > max_r ) h2 = max_r;
367 if ( fl_abs(tailx - headx) > 0.01f ) {
368 a = (float)atan2( taily-heady, tailx-headx );
377 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] };
410 if ( depth < 0.0f ) depth = 0.0f;
413 v[0].sx = (-w/2.0f)*ca + (-h1/2.0f)*sa + mx;
414 v[0].sy = (-w/2.0f)*sa - (-h1/2.0f)*ca + my;
424 v[1].sx = (w/2.0f)*ca + (-h2/2.0f)*sa + mx;
425 v[1].sy = (w/2.0f)*sa - (-h2/2.0f)*ca + my;
435 v[2].sx = (w/2.0f)*ca + (h2/2.0f)*sa + mx;
436 v[2].sy = (w/2.0f)*sa - (h2/2.0f)*ca + my;
446 v[3].sx = (-w/2.0f)*ca + (h1/2.0f)*sa + mx;
447 v[3].sy = (-w/2.0f)*sa - (h1/2.0f)*ca + my;
457 if(gr_screen.mode == GR_GLIDE){
458 gr_tmapper(4, vertlist, tmap_flags | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD);
460 gr_tmapper(4, vertlist, tmap_flags | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD | TMAP_FLAG_CORRECT);