make infobar height configurable
[divverent/darkplaces.git] / r_lightning.c
1
2 #include "quakedef.h"
3 #include "image.h"
4
5 cvar_t r_lightningbeam_thickness = {CVAR_SAVE, "r_lightningbeam_thickness", "4", "thickness of the lightning beam effect"};
6 cvar_t r_lightningbeam_scroll = {CVAR_SAVE, "r_lightningbeam_scroll", "5", "speed of texture scrolling on the lightning beam effect"};
7 cvar_t r_lightningbeam_repeatdistance = {CVAR_SAVE, "r_lightningbeam_repeatdistance", "128", "how far to stretch the texture along the lightning beam effect"};
8 cvar_t r_lightningbeam_color_red = {CVAR_SAVE, "r_lightningbeam_color_red", "1", "color of the lightning beam effect"};
9 cvar_t r_lightningbeam_color_green = {CVAR_SAVE, "r_lightningbeam_color_green", "1", "color of the lightning beam effect"};
10 cvar_t r_lightningbeam_color_blue = {CVAR_SAVE, "r_lightningbeam_color_blue", "1", "color of the lightning beam effect"};
11 cvar_t r_lightningbeam_qmbtexture = {CVAR_SAVE, "r_lightningbeam_qmbtexture", "0", "load the qmb textures/particles/lightning.pcx texture instead of generating one, can look better"};
12
13 skinframe_t *r_lightningbeamtexture;
14 skinframe_t *r_lightningbeamqmbtexture;
15
16 int r_lightningbeamelement3i[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
17 unsigned short r_lightningbeamelement3s[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
18
19 void r_lightningbeams_start(void)
20 {
21         r_lightningbeamtexture = NULL;
22         r_lightningbeamqmbtexture = NULL;
23 }
24
25 void r_lightningbeams_setupqmbtexture(void)
26 {
27         r_lightningbeamqmbtexture = R_SkinFrame_LoadExternal("textures/particles/lightning.pcx", TEXF_ALPHA | TEXF_FORCELINEAR, false);
28         if (r_lightningbeamqmbtexture == NULL)
29                 Cvar_SetValueQuick(&r_lightningbeam_qmbtexture, false);
30 }
31
32 void r_lightningbeams_setuptexture(void)
33 {
34 #if 0
35 #define BEAMWIDTH 128
36 #define BEAMHEIGHT 64
37 #define PATHPOINTS 8
38         int i, j, px, py, nearestpathindex, imagenumber;
39         float particlex, particley, particlexv, particleyv, dx, dy, s, maxpathstrength;
40         unsigned char *pixels;
41         int *image;
42         struct lightningpathnode_s
43         {
44                 float x, y, strength;
45         }
46         path[PATHPOINTS], temppath;
47
48         image = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
49         pixels = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(unsigned char[4]));
50
51         for (imagenumber = 0, maxpathstrength = 0.0339476;maxpathstrength < 0.5;imagenumber++, maxpathstrength += 0.01)
52         {
53                 for (i = 0;i < PATHPOINTS;i++)
54                 {
55                         path[i].x = lhrandom(0, 1);
56                         path[i].y = lhrandom(0.2, 0.8);
57                         path[i].strength = lhrandom(0, 1);
58                 }
59                 for (i = 0;i < PATHPOINTS;i++)
60                 {
61                         for (j = i + 1;j < PATHPOINTS;j++)
62                         {
63                                 if (path[j].x < path[i].x)
64                                 {
65                                         temppath = path[j];
66                                         path[j] = path[i];
67                                         path[i] = temppath;
68                                 }
69                         }
70                 }
71                 particlex = path[0].x;
72                 particley = path[0].y;
73                 particlexv = lhrandom(0, 0.02);
74                 particlexv = lhrandom(-0.02, 0.02);
75                 memset(image, 0, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
76                 for (i = 0;i < 65536;i++)
77                 {
78                         for (nearestpathindex = 0;nearestpathindex < PATHPOINTS;nearestpathindex++)
79                                 if (path[nearestpathindex].x > particlex)
80                                         break;
81                         nearestpathindex %= PATHPOINTS;
82                         dx = path[nearestpathindex].x + lhrandom(-0.01, 0.01);dx = bound(0, dx, 1) - particlex;if (dx < 0) dx += 1;
83                         dy = path[nearestpathindex].y + lhrandom(-0.01, 0.01);dy = bound(0, dy, 1) - particley;
84                         s = path[nearestpathindex].strength / sqrt(dx*dx+dy*dy);
85                         particlexv = particlexv /* (1 - lhrandom(0.08, 0.12))*/ + dx * s;
86                         particleyv = particleyv /* (1 - lhrandom(0.08, 0.12))*/ + dy * s;
87                         particlex += particlexv * maxpathstrength;particlex -= (int) particlex;
88                         particley += particleyv * maxpathstrength;particley = bound(0, particley, 1);
89                         px = particlex * BEAMWIDTH;
90                         py = particley * BEAMHEIGHT;
91                         if (px >= 0 && py >= 0 && px < BEAMWIDTH && py < BEAMHEIGHT)
92                                 image[py*BEAMWIDTH+px] += 16;
93                 }
94
95                 for (py = 0;py < BEAMHEIGHT;py++)
96                 {
97                         for (px = 0;px < BEAMWIDTH;px++)
98                         {
99                                 pixels[(py*BEAMWIDTH+px)*4+2] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
100                                 pixels[(py*BEAMWIDTH+px)*4+1] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
101                                 pixels[(py*BEAMWIDTH+px)*4+0] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
102                                 pixels[(py*BEAMWIDTH+px)*4+3] = 255;
103                         }
104                 }
105
106                 Image_WriteTGABGRA(va("lightningbeam%i.tga", imagenumber), BEAMWIDTH, BEAMHEIGHT, pixels);
107         }
108
109         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, pixels, TEXTYPE_BGRA, TEXF_FORCELINEAR, NULL);
110
111         Mem_Free(pixels);
112         Mem_Free(image);
113 #else
114 #define BEAMWIDTH 64
115 #define BEAMHEIGHT 128
116         float r, g, b, intensity, fx, width, center;
117         int x, y;
118         unsigned char *data, *noise1, *noise2;
119
120         data = (unsigned char *)Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * 4);
121         noise1 = (unsigned char *)Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
122         noise2 = (unsigned char *)Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
123         fractalnoise(noise1, BEAMHEIGHT, BEAMHEIGHT / 8);
124         fractalnoise(noise2, BEAMHEIGHT, BEAMHEIGHT / 16);
125
126         for (y = 0;y < BEAMHEIGHT;y++)
127         {
128                 width = 0.15;//((noise1[y * BEAMHEIGHT] * (1.0f / 256.0f)) * 0.1f + 0.1f);
129                 center = (noise1[y * BEAMHEIGHT + (BEAMHEIGHT / 2)] / 256.0f) * (1.0f - width * 2.0f) + width;
130                 for (x = 0;x < BEAMWIDTH;x++, fx++)
131                 {
132                         fx = (((float) x / BEAMWIDTH) - center) / width;
133                         intensity = 1.0f - sqrt(fx * fx);
134                         if (intensity > 0)
135                                 intensity = pow(intensity, 2) * ((noise2[y * BEAMHEIGHT + x] * (1.0f / 256.0f)) * 0.33f + 0.66f);
136                         intensity = bound(0, intensity, 1);
137                         r = intensity * 1.0f;
138                         g = intensity * 1.0f;
139                         b = intensity * 1.0f;
140                         data[(y * BEAMWIDTH + x) * 4 + 2] = (unsigned char)(bound(0, r, 1) * 255.0f);
141                         data[(y * BEAMWIDTH + x) * 4 + 1] = (unsigned char)(bound(0, g, 1) * 255.0f);
142                         data[(y * BEAMWIDTH + x) * 4 + 0] = (unsigned char)(bound(0, b, 1) * 255.0f);
143                         data[(y * BEAMWIDTH + x) * 4 + 3] = (unsigned char)255;
144                 }
145         }
146
147         r_lightningbeamtexture = R_SkinFrame_LoadInternalBGRA("lightningbeam", TEXF_FORCELINEAR, data, BEAMWIDTH, BEAMHEIGHT);
148         Mem_Free(noise1);
149         Mem_Free(noise2);
150         Mem_Free(data);
151 #endif
152 }
153
154 void r_lightningbeams_shutdown(void)
155 {
156         r_lightningbeamtexture = NULL;
157         r_lightningbeamqmbtexture = NULL;
158 }
159
160 void r_lightningbeams_newmap(void)
161 {
162         if (r_lightningbeamtexture)
163                 R_SkinFrame_MarkUsed(r_lightningbeamtexture);
164         if (r_lightningbeamqmbtexture)
165                 R_SkinFrame_MarkUsed(r_lightningbeamqmbtexture);
166 }
167
168 void R_LightningBeams_Init(void)
169 {
170         Cvar_RegisterVariable(&r_lightningbeam_thickness);
171         Cvar_RegisterVariable(&r_lightningbeam_scroll);
172         Cvar_RegisterVariable(&r_lightningbeam_repeatdistance);
173         Cvar_RegisterVariable(&r_lightningbeam_color_red);
174         Cvar_RegisterVariable(&r_lightningbeam_color_green);
175         Cvar_RegisterVariable(&r_lightningbeam_color_blue);
176         Cvar_RegisterVariable(&r_lightningbeam_qmbtexture);
177         R_RegisterModule("R_LightningBeams", r_lightningbeams_start, r_lightningbeams_shutdown, r_lightningbeams_newmap, NULL, NULL);
178 }
179
180 void R_CalcLightningBeamPolygonVertex3f(float *v, const float *start, const float *end, const float *offset)
181 {
182         // near right corner
183         VectorAdd     (start, offset, (v + 0));
184         // near left corner
185         VectorSubtract(start, offset, (v + 3));
186         // far left corner
187         VectorSubtract(end  , offset, (v + 6));
188         // far right corner
189         VectorAdd     (end  , offset, (v + 9));
190 }
191
192 void R_CalcLightningBeamPolygonTexCoord2f(float *tc, float t1, float t2)
193 {
194         if (r_lightningbeam_qmbtexture.integer)
195         {
196                 // near right corner
197                 tc[0] = t1;tc[1] = 0;
198                 // near left corner
199                 tc[2] = t1;tc[3] = 1;
200                 // far left corner
201                 tc[4] = t2;tc[5] = 1;
202                 // far right corner
203                 tc[6] = t2;tc[7] = 0;
204         }
205         else
206         {
207                 // near right corner
208                 tc[0] = 0;tc[1] = t1;
209                 // near left corner
210                 tc[2] = 1;tc[3] = t1;
211                 // far left corner
212                 tc[4] = 1;tc[5] = t2;
213                 // far right corner
214                 tc[6] = 0;tc[7] = t2;
215         }
216 }
217
218 float beamrepeatscale;
219
220 void R_DrawLightningBeam_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
221 {
222         int surfacelistindex;
223         float vertex3f[12*3];
224         float texcoord2f[12*2];
225
226         RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, 0, 0, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1, 12, vertex3f, texcoord2f, NULL, NULL, NULL, NULL, 6, r_lightningbeamelement3i, r_lightningbeamelement3s, false, false);
227
228         if (r_lightningbeam_qmbtexture.integer && r_lightningbeamqmbtexture == NULL)
229                 r_lightningbeams_setupqmbtexture();
230         if (!r_lightningbeam_qmbtexture.integer && r_lightningbeamtexture == NULL)
231                 r_lightningbeams_setuptexture();
232
233         for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
234         {
235                 const beam_t *b = cl.beams + surfacelist[surfacelistindex];
236                 vec3_t beamdir, right, up, offset, start, end;
237                 float length, t1, t2;
238
239                 CL_Beam_CalculatePositions(b, start, end);
240
241                 // calculate beam direction (beamdir) vector and beam length
242                 // get difference vector
243                 VectorSubtract(end, start, beamdir);
244                 // find length of difference vector
245                 length = sqrt(DotProduct(beamdir, beamdir));
246                 // calculate scale to make beamdir a unit vector (normalized)
247                 t1 = 1.0f / length;
248                 // scale beamdir so it is now normalized
249                 VectorScale(beamdir, t1, beamdir);
250
251                 // calculate up vector such that it points toward viewer, and rotates around the beamdir
252                 // get direction from start of beam to viewer
253                 VectorSubtract(r_refdef.view.origin, start, up);
254                 // remove the portion of the vector that moves along the beam
255                 // (this leaves only a vector pointing directly away from the beam)
256                 t1 = -DotProduct(up, beamdir);
257                 VectorMA(up, t1, beamdir, up);
258                 // generate right vector from forward and up, the result is unnormalized
259                 CrossProduct(beamdir, up, right);
260                 // now normalize the right vector and up vector
261                 VectorNormalize(right);
262                 VectorNormalize(up);
263
264                 // calculate T coordinate scrolling (start and end texcoord along the beam)
265                 t1 = r_refdef.scene.time * -r_lightningbeam_scroll.value;// + beamrepeatscale * DotProduct(start, beamdir);
266                 t1 = t1 - (int) t1;
267                 t2 = t1 + beamrepeatscale * length;
268
269                 // the beam is 3 polygons in this configuration:
270                 //  *   2
271                 //   * *
272                 // 1******
273                 //   * *
274                 //  *   3
275                 // they are showing different portions of the beam texture, creating an
276                 // illusion of a beam that appears to curl around in 3D space
277                 // (and realize that the whole polygon assembly orients itself to face
278                 //  the viewer)
279
280                 // polygon 1, verts 0-3
281                 VectorScale(right, r_lightningbeam_thickness.value, offset);
282                 R_CalcLightningBeamPolygonVertex3f(vertex3f + 0, start, end, offset);
283                 // polygon 2, verts 4-7
284                 VectorAdd(right, up, offset);
285                 VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
286                 R_CalcLightningBeamPolygonVertex3f(vertex3f + 12, start, end, offset);
287                 // polygon 3, verts 8-11
288                 VectorSubtract(right, up, offset);
289                 VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
290                 R_CalcLightningBeamPolygonVertex3f(vertex3f + 24, start, end, offset);
291                 R_CalcLightningBeamPolygonTexCoord2f(texcoord2f + 0, t1, t2);
292                 R_CalcLightningBeamPolygonTexCoord2f(texcoord2f + 8, t1 + 0.33, t2 + 0.33);
293                 R_CalcLightningBeamPolygonTexCoord2f(texcoord2f + 16, t1 + 0.66, t2 + 0.66);
294
295                 // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
296                 R_DrawCustomSurface(r_lightningbeam_qmbtexture.integer ? r_lightningbeamqmbtexture : r_lightningbeamtexture, &identitymatrix, MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 12, 0, 6, false, false);
297         }
298 }
299
300 extern cvar_t cl_beams_polygons;
301 void R_DrawLightningBeams(void)
302 {
303         int i;
304         beam_t *b;
305
306         if (!cl_beams_polygons.integer)
307                 return;
308
309         beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
310         for (i = 0, b = cl.beams;i < cl.num_beams;i++, b++)
311         {
312                 if (b->model && b->lightning)
313                 {
314                         vec3_t org, start, end, dir;
315                         vec_t dist;
316                         CL_Beam_CalculatePositions(b, start, end);
317                         // calculate the nearest point on the line (beam) for depth sorting
318                         VectorSubtract(end, start, dir);
319                         dist = (DotProduct(r_refdef.view.origin, dir) - DotProduct(start, dir)) / (DotProduct(end, dir) - DotProduct(start, dir));
320                         dist = bound(0, dist, 1);
321                         VectorLerp(start, dist, end, org);
322                         // now we have the nearest point on the line, so sort with it
323                         R_MeshQueue_AddTransparent(org, R_DrawLightningBeam_TransparentCallback, NULL, i, NULL);
324                 }
325         }
326 }
327