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