Added the togglemenu command for both menus and the chr builtin command (menu qc)
[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"};
6 cvar_t r_lightningbeam_scroll = {CVAR_SAVE, "r_lightningbeam_scroll", "5"};
7 cvar_t r_lightningbeam_repeatdistance = {CVAR_SAVE, "r_lightningbeam_repeatdistance", "1024"};
8 cvar_t r_lightningbeam_color_red = {CVAR_SAVE, "r_lightningbeam_color_red", "1"};
9 cvar_t r_lightningbeam_color_green = {CVAR_SAVE, "r_lightningbeam_color_green", "1"};
10 cvar_t r_lightningbeam_color_blue = {CVAR_SAVE, "r_lightningbeam_color_blue", "1"};
11 cvar_t r_lightningbeam_qmbtexture = {CVAR_SAVE, "r_lightningbeam_qmbtexture", "0"};
12
13 rtexture_t *r_lightningbeamtexture;
14 rtexture_t *r_lightningbeamqmbtexture;
15 rtexturepool_t *r_lightningbeamtexturepool;
16
17 int r_lightningbeamelements[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_lightningbeamtexturepool = R_AllocTexturePool();
22         r_lightningbeamtexture = NULL;
23         r_lightningbeamqmbtexture = NULL;
24 }
25
26 void r_lightningbeams_setupqmbtexture(void)
27 {
28         r_lightningbeamqmbtexture = loadtextureimage(r_lightningbeamtexturepool, "textures/particles/lightning.pcx", 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE);
29         if (r_lightningbeamqmbtexture == NULL)
30                 Cvar_SetValueQuick(&r_lightningbeam_qmbtexture, false);
31 }
32
33 void r_lightningbeams_setuptexture(void)
34 {
35 #if 0
36 #define BEAMWIDTH 128
37 #define BEAMHEIGHT 64
38 #define PATHPOINTS 8
39         int i, j, px, py, nearestpathindex, imagenumber;
40         float particlex, particley, particlexv, particleyv, dx, dy, s, maxpathstrength;
41         qbyte *pixels;
42         int *image;
43         struct {float x, y, strength;} path[PATHPOINTS], temppath;
44
45         image = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
46         pixels = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(qbyte[4]));
47
48         for (imagenumber = 0, maxpathstrength = 0.0339476;maxpathstrength < 0.5;imagenumber++, maxpathstrength += 0.01)
49         {
50         for (i = 0;i < PATHPOINTS;i++)
51         {
52                 path[i].x = lhrandom(0, 1);
53                 path[i].y = lhrandom(0.2, 0.8);
54                 path[i].strength = lhrandom(0, 1);
55         }
56         for (i = 0;i < PATHPOINTS;i++)
57         {
58                 for (j = i + 1;j < PATHPOINTS;j++)
59                 {
60                         if (path[j].x < path[i].x)
61                         {
62                                 temppath = path[j];
63                                 path[j] = path[i];
64                                 path[i] = temppath;
65                         }
66                 }
67         }
68         particlex = path[0].x;
69         particley = path[0].y;
70         particlexv = lhrandom(0, 0.02);
71         particlexv = lhrandom(-0.02, 0.02);
72         memset(image, 0, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
73         for (i = 0;i < 65536;i++)
74         {
75                 for (nearestpathindex = 0;nearestpathindex < PATHPOINTS;nearestpathindex++)
76                         if (path[nearestpathindex].x > particlex)
77                                 break;
78                 nearestpathindex %= PATHPOINTS;
79                 dx = path[nearestpathindex].x + lhrandom(-0.01, 0.01);dx = bound(0, dx, 1) - particlex;if (dx < 0) dx += 1;
80                 dy = path[nearestpathindex].y + lhrandom(-0.01, 0.01);dy = bound(0, dy, 1) - particley;
81                 s = path[nearestpathindex].strength / sqrt(dx*dx+dy*dy);
82                 particlexv = particlexv /* (1 - lhrandom(0.08, 0.12))*/ + dx * s;
83                 particleyv = particleyv /* (1 - lhrandom(0.08, 0.12))*/ + dy * s;
84                 particlex += particlexv * maxpathstrength;particlex -= (int) particlex;
85                 particley += particleyv * maxpathstrength;particley = bound(0, particley, 1);
86                 px = particlex * BEAMWIDTH;
87                 py = particley * BEAMHEIGHT;
88                 if (px >= 0 && py >= 0 && px < BEAMWIDTH && py < BEAMHEIGHT)
89                         image[py*BEAMWIDTH+px] += 16;
90         }
91
92         for (py = 0;py < BEAMHEIGHT;py++)
93         {
94                 for (px = 0;px < BEAMWIDTH;px++)
95                 {
96                         pixels[(py*BEAMWIDTH+px)*4+0] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
97                         pixels[(py*BEAMWIDTH+px)*4+1] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
98                         pixels[(py*BEAMWIDTH+px)*4+2] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
99                         pixels[(py*BEAMWIDTH+px)*4+3] = 255;
100                 }
101         }
102
103         Image_WriteTGARGBA(va("lightningbeam%i.tga", imagenumber), BEAMWIDTH, BEAMHEIGHT, pixels);
104         }
105
106         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, pixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
107
108         Mem_Free(pixels);
109         Mem_Free(image);
110 #else
111 #define BEAMWIDTH 64
112 #define BEAMHEIGHT 128
113         float r, g, b, intensity, fx, width, center;
114         int x, y;
115         qbyte *data, *noise1, *noise2;
116
117         data = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * 4);
118         noise1 = Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
119         noise2 = Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
120         fractalnoise(noise1, BEAMHEIGHT, BEAMHEIGHT / 8);
121         fractalnoise(noise2, BEAMHEIGHT, BEAMHEIGHT / 16);
122
123         for (y = 0;y < BEAMHEIGHT;y++)
124         {
125                 width = 0.15;//((noise1[y * BEAMHEIGHT] * (1.0f / 256.0f)) * 0.1f + 0.1f);
126                 center = (noise1[y * BEAMHEIGHT + (BEAMHEIGHT / 2)] / 256.0f) * (1.0f - width * 2.0f) + width;
127                 for (x = 0;x < BEAMWIDTH;x++, fx++)
128                 {
129                         fx = (((float) x / BEAMWIDTH) - center) / width;
130                         intensity = 1.0f - sqrt(fx * fx);
131                         if (intensity > 0)
132                                 intensity = pow(intensity, 2) * ((noise2[y * BEAMHEIGHT + x] * (1.0f / 256.0f)) * 0.33f + 0.66f);
133                         intensity = bound(0, intensity, 1);
134                         r = intensity * 1.0f;
135                         g = intensity * 1.0f;
136                         b = intensity * 1.0f;
137                         data[(y * BEAMWIDTH + x) * 4 + 0] = (qbyte)(bound(0, r, 1) * 255.0f);
138                         data[(y * BEAMWIDTH + x) * 4 + 1] = (qbyte)(bound(0, g, 1) * 255.0f);
139                         data[(y * BEAMWIDTH + x) * 4 + 2] = (qbyte)(bound(0, b, 1) * 255.0f);
140                         data[(y * BEAMWIDTH + x) * 4 + 3] = (qbyte)255;
141                 }
142         }
143
144         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
145         Mem_Free(noise1);
146         Mem_Free(noise2);
147         Mem_Free(data);
148 #endif
149 }
150
151 void r_lightningbeams_shutdown(void)
152 {
153         r_lightningbeamtexture = NULL;
154         r_lightningbeamqmbtexture = NULL;
155         R_FreeTexturePool(&r_lightningbeamtexturepool);
156 }
157
158 void r_lightningbeams_newmap(void)
159 {
160 }
161
162 void R_LightningBeams_Init(void)
163 {
164         Cvar_RegisterVariable(&r_lightningbeam_thickness);
165         Cvar_RegisterVariable(&r_lightningbeam_scroll);
166         Cvar_RegisterVariable(&r_lightningbeam_repeatdistance);
167         Cvar_RegisterVariable(&r_lightningbeam_color_red);
168         Cvar_RegisterVariable(&r_lightningbeam_color_green);
169         Cvar_RegisterVariable(&r_lightningbeam_color_blue);
170         Cvar_RegisterVariable(&r_lightningbeam_qmbtexture);
171         R_RegisterModule("R_LightningBeams", r_lightningbeams_start, r_lightningbeams_shutdown, r_lightningbeams_newmap);
172 }
173
174 void R_CalcLightningBeamPolygonVertex3f(float *v, const float *start, const float *end, const float *offset)
175 {
176         // near right corner
177         VectorAdd     (start, offset, (v + 0));
178         // near left corner
179         VectorSubtract(start, offset, (v + 3));
180         // far left corner
181         VectorSubtract(end  , offset, (v + 6));
182         // far right corner
183         VectorAdd     (end  , offset, (v + 9));
184 }
185
186 void R_CalcLightningBeamPolygonTexCoord2f(float *tc, float t1, float t2)
187 {
188         if (r_lightningbeam_qmbtexture.integer)
189         {
190                 // near right corner
191                 tc[0] = t1;tc[1] = 0;
192                 // near left corner
193                 tc[2] = t1;tc[3] = 1;
194                 // far left corner
195                 tc[4] = t2;tc[5] = 1;
196                 // far right corner
197                 tc[6] = t2;tc[7] = 0;
198         }
199         else
200         {
201                 // near right corner
202                 tc[0] = 0;tc[1] = t1;
203                 // near left corner
204                 tc[2] = 1;tc[3] = t1;
205                 // far left corner
206                 tc[4] = 1;tc[5] = t2;
207                 // far right corner
208                 tc[6] = 0;tc[7] = t2;
209         }
210 }
211
212 void R_FogLightningBeam_Vertex3f_Color4f(const float *v, float *c, int numverts, float r, float g, float b, float a)
213 {
214         int i;
215         vec3_t fogvec;
216         float ifog;
217         for (i = 0;i < numverts;i++, v += 3, c += 4)
218         {
219                 VectorSubtract(v, r_vieworigin, fogvec);
220                 ifog = 1 - exp(fogdensity/DotProduct(fogvec,fogvec));
221                 c[0] = r * ifog;
222                 c[1] = g * ifog;
223                 c[2] = b * ifog;
224                 c[3] = a;
225         }
226 }
227
228 float beamrepeatscale;
229
230 void R_DrawLightningBeamCallback(const void *calldata1, int calldata2)
231 {
232         const beam_t *b = calldata1;
233         rmeshstate_t m;
234         vec3_t beamdir, right, up, offset;
235         float length, t1, t2;
236
237         R_Mesh_Matrix(&r_identitymatrix);
238
239         // calculate beam direction (beamdir) vector and beam length
240         // get difference vector
241         VectorSubtract(b->end, b->start, beamdir);
242         // find length of difference vector
243         length = sqrt(DotProduct(beamdir, beamdir));
244         // calculate scale to make beamdir a unit vector (normalized)
245         t1 = 1.0f / length;
246         // scale beamdir so it is now normalized
247         VectorScale(beamdir, t1, beamdir);
248
249         // calculate up vector such that it points toward viewer, and rotates around the beamdir
250         // get direction from start of beam to viewer
251         VectorSubtract(r_vieworigin, b->start, up);
252         // remove the portion of the vector that moves along the beam
253         // (this leaves only a vector pointing directly away from the beam)
254         t1 = -DotProduct(up, beamdir);
255         VectorMA(up, t1, beamdir, up);
256         // now we have a vector pointing away from the beam, now we need to normalize it
257         VectorNormalizeFast(up);
258         // generate right vector from forward and up, the result is already normalized
259         // (CrossProduct returns a vector of multiplied length of the two inputs)
260         CrossProduct(beamdir, up, right);
261
262         // calculate T coordinate scrolling (start and end texcoord along the beam)
263         t1 = cl.time * -r_lightningbeam_scroll.value;// + beamrepeatscale * DotProduct(b->start, beamdir);
264         t1 = t1 - (int) t1;
265         t2 = t1 + beamrepeatscale * length;
266
267         // the beam is 3 polygons in this configuration:
268         //  *   2
269         //   * *
270         // 1******
271         //   * *
272         //  *   3
273         // they are showing different portions of the beam texture, creating an
274         // illusion of a beam that appears to curl around in 3D space
275         // (and realize that the whole polygon assembly orients itself to face
276         //  the viewer)
277
278         memset(&m, 0, sizeof(m));
279         if (r_lightningbeam_qmbtexture.integer)
280                 m.tex[0] = R_GetTexture(r_lightningbeamqmbtexture);
281         else
282                 m.tex[0] = R_GetTexture(r_lightningbeamtexture);
283         m.pointer_texcoord[0] = varray_texcoord2f[0];
284         R_Mesh_State_Texture(&m);
285
286         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
287         GL_DepthMask(false);
288         GL_DepthTest(true);
289         if (r_lightningbeam_qmbtexture.integer && r_lightningbeamqmbtexture == NULL)
290                 r_lightningbeams_setupqmbtexture();
291         if (!r_lightningbeam_qmbtexture.integer && r_lightningbeamtexture == NULL)
292                 r_lightningbeams_setuptexture();
293
294         // polygon 1, verts 0-3
295         VectorScale(right, r_lightningbeam_thickness.value, offset);
296         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 0, b->start, b->end, offset);
297         // polygon 2, verts 4-7
298         VectorAdd(right, up, offset);
299         VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
300         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 12, b->start, b->end, offset);
301         // polygon 3, verts 8-11
302         VectorSubtract(right, up, offset);
303         VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
304         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 24, b->start, b->end, offset);
305         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 0, t1, t2);
306         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 8, t1 + 0.33, t2 + 0.33);
307         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 16, t1 + 0.66, t2 + 0.66);
308         GL_VertexPointer(varray_vertex3f);
309
310         if (fogenabled)
311         {
312                 // per vertex colors if fog is used
313                 GL_ColorPointer(varray_color4f);
314                 R_FogLightningBeam_Vertex3f_Color4f(varray_vertex3f, varray_color4f, 12, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
315         }
316         else
317         {
318                 // solid color if fog is not used
319                 GL_Color(r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
320         }
321
322         // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
323         R_Mesh_Draw(12, 6, r_lightningbeamelements);
324 }
325
326 void R_DrawLightningBeams(void)
327 {
328         int i;
329         beam_t *b;
330         vec3_t org;
331
332         if (!cl_beams_polygons.integer)
333                 return;
334
335         beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
336         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
337         {
338                 if (b->model && b->endtime >= cl.time && b->lightning)
339                 {
340                         VectorAdd(b->start, b->end, org);
341                         VectorScale(org, 0.5f, org);
342                         R_MeshQueue_AddTransparent(org, R_DrawLightningBeamCallback, b, 0);
343                 }
344         }
345 }
346