]> icculus.org git repositories - taylor/freespace2.git/blob - src/render/3dlaser.cpp
Initial revision
[taylor/freespace2.git] / src / render / 3dlaser.cpp
1 /*
2  * $Logfile: /Freespace2/code/Render/3dLaser.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Code to draw 3d looking lasers
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:10  root
11  * Initial revision
12  *
13  * 
14  * 5     7/30/99 7:01p Dave
15  * Dogfight escort gauge. Fixed up laser rendering in Glide.
16  * 
17  * 4     7/27/99 3:09p Dave
18  * Made g400 work. Whee.
19  * 
20  * 3     5/27/99 6:17p Dave
21  * Added in laser glows.
22  * 
23  * 2     10/07/98 10:53a Dave
24  * Initial checkin.
25  * 
26  * 1     10/07/98 10:51a Dave
27  * 
28  * 26    5/13/98 3:10p John
29  * made detail slider for weapon rendering change the distance that lasers
30  * become non-textured.  The lowest setting turns off missile trail
31  * rendering.
32  * 
33  * 25    5/05/98 11:15p Lawrance
34  * Optimize weapon flyby sound determination
35  * 
36  * 24    4/10/98 5:20p John
37  * Changed RGB in lighting structure to be ubytes.  Removed old
38  * not-necessary 24 bpp software stuff.
39  * 
40  * 23    3/17/98 3:02p John
41  * made lasers draw as non-textured only on software.
42  * 
43  * 22    3/17/98 3:01p John
44  * Make thicker lasers not render as polys further away.
45  * 
46  * 21    2/26/98 3:26p John
47  * Added code to turn off laser rendering
48  * 
49  * 20    1/29/98 9:46a John
50  * Capped debris length
51  * 
52  * 19    1/23/98 5:08p John
53  * Took L out of vertex structure used B (blue) instead.   Took all small
54  * fireballs out of fireball types and used particles instead.  Fixed some
55  * debris explosion things.  Restructured fireball code.   Restructured
56  * some lighting code.   Made dynamic lighting on by default. Made groups
57  * of lasers only cast one light.  Made fireballs not cast light.
58  * 
59  * 18    1/19/98 8:51a John
60  * Did a quick out if both points of laser off same side of view pyramid.
61  * 
62  * 17    1/06/98 6:18p John
63  * Made debris render as a blur.  Had to make g3_draw_laser take tmap
64  * flags.
65  * 
66  * 16    12/15/97 10:09p John
67  * put in some debug laser code.
68  * 
69  * 15    12/15/97 11:32a John
70  * New Laser Code
71  * 
72  * 14    10/23/97 8:32p John
73  * Increased laser to dot distance
74  * 
75  * 13    9/20/97 9:45a John
76  * made lasers not draw as pixels as fast as before if viewing from the
77  * side.
78  * 
79  * 
80  * 12    8/19/97 11:42p Lawrance
81  * use atan2_safe() instead of atan2()
82  * 
83  * 11    8/19/97 12:54p Sandeep
84  * Fixed lasers to work in optimized build
85  * 
86  * 10    6/24/97 6:22p John
87  * added detail flags.
88  * sped up motion debris system a bit.
89  * 
90  * 9     5/29/97 3:10p John
91  * Took out debug menu.  
92  * Made software scaler draw larger bitmaps.
93  * Optimized Direct3D some.
94  * 
95  * 8     4/29/97 9:55a John
96  * 
97  * 7     4/22/97 4:11p John
98  * New debug var stuff
99  * 
100  * 6     3/28/97 6:10p Lawrance
101  * draw lasers so end appears at gun mount tip on first frame
102  * 
103  * 5     3/06/97 8:48a John
104  * fixed bug with lasers drawing too close.
105  * 
106  * 4     2/07/97 9:07a John
107  * made lasers not fade by default.
108  * 
109  * 3     2/03/97 8:01p John
110  * Added debug code to fade lasers out with distance.
111  * 
112  * 2     12/11/96 12:41p John
113  * Added new code to draw 3d laser using 2d ellipses.
114  * 
115  * 1     12/11/96 12:13p John
116  *
117  * $NoKeywords: $
118  */
119
120 #include "2d.h"
121 #include "3d.h"
122 #include "3dinternal.h"
123 #include "systemvars.h"
124 #include "key.h"
125
126 int Lasers = 1;
127 DCF_BOOL( lasers, Lasers );
128
129 // This assumes you have already set a color with gr_set_color or gr_set_color_fast
130 // and a bitmap with gr_set_bitmap.  If it is very far away, it draws the laser
131 // as flat-shaded using current color, else textured using current texture.
132 // If max_len is > 1.0, then this caps the length to be no longer than max_len pixels.
133 float g3_draw_laser(vector *headp, float head_width, vector *tailp, float tail_width, uint tmap_flags, float max_len )
134 {
135         if (!Lasers){
136                 return 0.0f;
137         }
138
139         float headx, heady, headr, tailx, taily, tailr;
140         vertex pt1, pt2;
141         float depth;
142         int head_on = 0;
143
144         Assert( G3_count == 1 );
145
146         g3_rotate_vertex(&pt1,headp);
147
148         g3_project_vertex(&pt1);
149         if (pt1.flags & PF_OVERFLOW) 
150                 return 0.0f;
151
152         g3_rotate_vertex(&pt2,tailp);
153
154         g3_project_vertex(&pt2);
155         if (pt2.flags & PF_OVERFLOW) 
156                 return 0.0f;
157
158         if ( (pt1.codes & pt2.codes) != 0 )     {
159                 // Both off the same side
160                 return 0.0f;
161         }
162
163         headx = pt1.sx;
164         heady = pt1.sy;
165         headr = (head_width*Matrix_scale.x*Canv_w2*pt1.sw);
166
167         tailx = pt2.sx;
168         taily = pt2.sy;
169         tailr = (tail_width*Matrix_scale.x*Canv_w2*pt2.sw);
170
171         float len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
172
173         // Cap the length if needed.
174         if ( (max_len > 1.0f) && (len_2d > max_len) )   {
175                 float ratio = max_len / len_2d;
176         
177                 tailx = headx + ( tailx - headx ) * ratio;
178                 taily = heady + ( taily - heady ) * ratio;
179                 tailr = headr + ( tailr - headr ) * ratio;
180
181                 len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
182         }
183
184         depth = (pt1.z+pt2.z)*0.5f;
185
186         float max_r  = headr;
187         float a;
188         if ( tailr > max_r ) 
189                 max_r = tailr;
190
191         if ( max_r < 1.0f )
192                 max_r = 1.0f;
193
194         float mx, my, w, h1,h2;
195
196         if ( len_2d < max_r ) {
197
198                 h1 = headr + (max_r-len_2d);
199                 if ( h1 > max_r ) h1 = max_r;
200                 h2 = tailr + (max_r-len_2d);
201                 if ( h2 > max_r ) h2 = max_r;
202
203                 len_2d = max_r;
204                 if ( fl_abs(tailx - headx) > 0.01f )    {
205                         a = (float)atan2( taily-heady, tailx-headx );
206                 } else {
207                         a = 0.0f;
208                 }
209
210                 w = len_2d;
211                 head_on = 1;
212
213         } else {
214                 a = atan2_safe( taily-heady, tailx-headx );
215
216                 w = len_2d;
217
218                 h1 = headr;
219                 h2 = tailr;
220                 head_on = 0;
221         }
222         
223         mx = (tailx+headx)/2.0f;
224         my = (taily+heady)/2.0f;
225
226 //      gr_set_color(255,0,0);
227 //      g3_draw_line( &pt1, &pt2 );
228
229 //      gr_set_color( 255, 0, 0 );
230 //      gr_pixel( fl2i(mx),fl2i(my) );
231
232         // Draw box with width 'w' and height 'h' at angle 'a' from horizontal
233         // centered around mx, my
234
235         if ( h1 < 1.0f ) h1 = 1.0f;
236         if ( h2 < 1.0f ) h2 = 1.0f;
237
238         float sa, ca;
239
240         sa = (float)sin(a);
241         ca = (float)cos(a);
242
243         vertex v[4];
244         vertex *vertlist[4] = { &v[3], &v[2], &v[1], &v[0] };
245
246         float sw;
247         if ( depth < 0.0f ) depth = 0.0f;
248         sw = 1.0f / depth;
249         
250         v[0].sx = (-w/2.0f)*ca + (-h1/2.0f)*sa + mx;
251         v[0].sy = (-w/2.0f)*sa - (-h1/2.0f)*ca + my;
252         v[0].z = pt1.z;
253         v[0].sw = pt1.sw;
254         v[0].u = 0.0f;
255         v[0].v = 0.0f;
256         v[0].b = 191;
257
258         v[1].sx = (w/2.0f)*ca + (-h2/2.0f)*sa + mx;
259         v[1].sy = (w/2.0f)*sa - (-h2/2.0f)*ca + my;
260         v[1].z = pt2.z;
261         v[1].sw = pt2.sw;
262         v[1].u = 1.0f;
263         v[1].v = 0.0f;
264         v[1].b = 191;
265
266         v[2].sx = (w/2.0f)*ca + (h2/2.0f)*sa + mx;
267         v[2].sy = (w/2.0f)*sa - (h2/2.0f)*ca + my;
268         v[2].z = pt2.z;
269         v[2].sw = pt2.sw;
270         v[2].u = 1.0f;
271         v[2].v = 1.0f;
272         v[2].b = 191;
273
274         v[3].sx = (-w/2.0f)*ca + (h1/2.0f)*sa + mx;
275         v[3].sy = (-w/2.0f)*sa - (h1/2.0f)*ca + my;
276         v[3].z = pt1.z;
277         v[3].sw = pt1.sw;
278         v[3].u = 0.0f;
279         v[3].v = 1.0f;
280         v[3].b = 191;
281
282         if(gr_screen.mode == GR_GLIDE){
283                 gr_tmapper(4, vertlist, tmap_flags);    
284         } else {
285                 gr_tmapper(4, vertlist, tmap_flags | TMAP_FLAG_CORRECT);        
286         }
287
288         return depth;
289 }
290
291 // Draw a laser shaped 3d looking thing using vertex coloring (useful for things like colored laser glows)
292 // If max_len is > 1.0, then this caps the length to be no longer than max_len pixels
293 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 )
294 {
295         if (!Lasers){
296                 return 0.0f;
297         }
298
299         float headx, heady, headr, tailx, taily, tailr;
300         vertex pt1, pt2;
301         float depth;
302         int head_on = 0;
303
304         Assert( G3_count == 1 );
305
306         g3_rotate_vertex(&pt1,headp);
307
308         g3_project_vertex(&pt1);
309         if (pt1.flags & PF_OVERFLOW) 
310                 return 0.0f;
311
312         g3_rotate_vertex(&pt2,tailp);
313
314         g3_project_vertex(&pt2);
315         if (pt2.flags & PF_OVERFLOW) 
316                 return 0.0f;
317
318         if ( (pt1.codes & pt2.codes) != 0 )     {
319                 // Both off the same side
320                 return 0.0f;
321         }
322
323         headx = pt1.sx;
324         heady = pt1.sy;
325         headr = (head_width*Matrix_scale.x*Canv_w2*pt1.sw);
326
327         tailx = pt2.sx;
328         taily = pt2.sy;
329         tailr = (tail_width*Matrix_scale.x*Canv_w2*pt2.sw);
330
331         float len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
332
333         // Cap the length if needed.
334         if ( (max_len > 1.0f) && (len_2d > max_len) )   {
335                 float ratio = max_len / len_2d;
336         
337                 tailx = headx + ( tailx - headx ) * ratio;
338                 taily = heady + ( taily - heady ) * ratio;
339                 tailr = headr + ( tailr - headr ) * ratio;
340
341                 len_2d = fl_sqrt( (tailx-headx)*(tailx-headx) + (taily-heady)*(taily-heady) );
342         }
343
344         depth = (pt1.z+pt2.z)*0.5f;
345
346         float max_r  = headr;
347         float a;
348         if ( tailr > max_r ) 
349                 max_r = tailr;
350
351         if ( max_r < 1.0f )
352                 max_r = 1.0f;
353
354         float mx, my, w, h1,h2;
355
356         if ( len_2d < max_r ) {
357
358                 h1 = headr + (max_r-len_2d);
359                 if ( h1 > max_r ) h1 = max_r;
360                 h2 = tailr + (max_r-len_2d);
361                 if ( h2 > max_r ) h2 = max_r;
362
363                 len_2d = max_r;
364                 if ( fl_abs(tailx - headx) > 0.01f )    {
365                         a = (float)atan2( taily-heady, tailx-headx );
366                 } else {
367                         a = 0.0f;
368                 }
369
370                 w = len_2d;
371                 head_on = 1;
372
373         } else {
374                 a = atan2_safe( taily-heady, tailx-headx );
375
376                 w = len_2d;
377
378                 h1 = headr;
379                 h2 = tailr;
380                 head_on = 0;
381         }
382         
383         mx = (tailx+headx)/2.0f;
384         my = (taily+heady)/2.0f;
385
386 //      gr_set_color(255,0,0);
387 //      g3_draw_line( &pt1, &pt2 );
388
389 //      gr_set_color( 255, 0, 0 );
390 //      gr_pixel( fl2i(mx),fl2i(my) );
391
392         // Draw box with width 'w' and height 'h' at angle 'a' from horizontal
393         // centered around mx, my
394
395         if ( h1 < 1.0f ) h1 = 1.0f;
396         if ( h2 < 1.0f ) h2 = 1.0f;
397
398         float sa, ca;
399
400         sa = (float)sin(a);
401         ca = (float)cos(a);
402
403         vertex v[4];
404         vertex *vertlist[4] = { &v[3], &v[2], &v[1], &v[0] };
405
406         float sw;
407         if ( depth < 0.0f ) depth = 0.0f;
408         sw = 1.0f / depth;
409         
410         v[0].sx = (-w/2.0f)*ca + (-h1/2.0f)*sa + mx;
411         v[0].sy = (-w/2.0f)*sa - (-h1/2.0f)*ca + my;
412         v[0].z = pt1.z;
413         v[0].sw = pt1.sw;
414         v[0].u = 0.0f;
415         v[0].v = 0.0f;
416         v[0].r = (ubyte)r;
417         v[0].g = (ubyte)g;
418         v[0].b = (ubyte)b;
419         v[0].a = 255;
420
421         v[1].sx = (w/2.0f)*ca + (-h2/2.0f)*sa + mx;
422         v[1].sy = (w/2.0f)*sa - (-h2/2.0f)*ca + my;
423         v[1].z = pt2.z;
424         v[1].sw = pt2.sw;
425         v[1].u = 1.0f;
426         v[1].v = 0.0f;
427         v[1].r = (ubyte)r;
428         v[1].g = (ubyte)g;
429         v[1].b = (ubyte)b;
430         v[1].a = 255;
431
432         v[2].sx = (w/2.0f)*ca + (h2/2.0f)*sa + mx;
433         v[2].sy = (w/2.0f)*sa - (h2/2.0f)*ca + my;
434         v[2].z = pt2.z;
435         v[2].sw = pt2.sw;
436         v[2].u = 1.0f;
437         v[2].v = 1.0f;
438         v[2].r = (ubyte)r;
439         v[2].g = (ubyte)g;
440         v[2].b = (ubyte)b;
441         v[2].a = 255;
442
443         v[3].sx = (-w/2.0f)*ca + (h1/2.0f)*sa + mx;
444         v[3].sy = (-w/2.0f)*sa - (h1/2.0f)*ca + my;
445         v[3].z = pt1.z;
446         v[3].sw = pt1.sw;
447         v[3].u = 0.0f;
448         v[3].v = 1.0f;
449         v[3].r = (ubyte)r;
450         v[3].g = (ubyte)g;
451         v[3].b = (ubyte)b;
452         v[3].a = 255;
453         
454         if(gr_screen.mode == GR_GLIDE){
455                 gr_tmapper(4, vertlist, tmap_flags | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD);
456         } else {
457                 gr_tmapper(4, vertlist, tmap_flags | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD | TMAP_FLAG_CORRECT);
458         }       
459
460         return depth;
461 }