added TEXF_FORCELINEAR to many special effect textures
[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 rtexture_t *r_lightningbeamtexture;
14 rtexture_t *r_lightningbeamqmbtexture;
15 rtexturepool_t *r_lightningbeamtexturepool;
16
17 unsigned short 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", false, TEXF_ALPHA | TEXF_PRECACHE | TEXF_FORCELINEAR, false);
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         unsigned char *pixels;
42         int *image;
43         struct lightningpathnode_s
44         {
45                 float x, y, strength;
46         }
47         path[PATHPOINTS], temppath;
48
49         image = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
50         pixels = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(unsigned char[4]));
51
52         for (imagenumber = 0, maxpathstrength = 0.0339476;maxpathstrength < 0.5;imagenumber++, maxpathstrength += 0.01)
53         {
54                 for (i = 0;i < PATHPOINTS;i++)
55                 {
56                         path[i].x = lhrandom(0, 1);
57                         path[i].y = lhrandom(0.2, 0.8);
58                         path[i].strength = lhrandom(0, 1);
59                 }
60                 for (i = 0;i < PATHPOINTS;i++)
61                 {
62                         for (j = i + 1;j < PATHPOINTS;j++)
63                         {
64                                 if (path[j].x < path[i].x)
65                                 {
66                                         temppath = path[j];
67                                         path[j] = path[i];
68                                         path[i] = temppath;
69                                 }
70                         }
71                 }
72                 particlex = path[0].x;
73                 particley = path[0].y;
74                 particlexv = lhrandom(0, 0.02);
75                 particlexv = lhrandom(-0.02, 0.02);
76                 memset(image, 0, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
77                 for (i = 0;i < 65536;i++)
78                 {
79                         for (nearestpathindex = 0;nearestpathindex < PATHPOINTS;nearestpathindex++)
80                                 if (path[nearestpathindex].x > particlex)
81                                         break;
82                         nearestpathindex %= PATHPOINTS;
83                         dx = path[nearestpathindex].x + lhrandom(-0.01, 0.01);dx = bound(0, dx, 1) - particlex;if (dx < 0) dx += 1;
84                         dy = path[nearestpathindex].y + lhrandom(-0.01, 0.01);dy = bound(0, dy, 1) - particley;
85                         s = path[nearestpathindex].strength / sqrt(dx*dx+dy*dy);
86                         particlexv = particlexv /* (1 - lhrandom(0.08, 0.12))*/ + dx * s;
87                         particleyv = particleyv /* (1 - lhrandom(0.08, 0.12))*/ + dy * s;
88                         particlex += particlexv * maxpathstrength;particlex -= (int) particlex;
89                         particley += particleyv * maxpathstrength;particley = bound(0, particley, 1);
90                         px = particlex * BEAMWIDTH;
91                         py = particley * BEAMHEIGHT;
92                         if (px >= 0 && py >= 0 && px < BEAMWIDTH && py < BEAMHEIGHT)
93                                 image[py*BEAMWIDTH+px] += 16;
94                 }
95
96                 for (py = 0;py < BEAMHEIGHT;py++)
97                 {
98                         for (px = 0;px < BEAMWIDTH;px++)
99                         {
100                                 pixels[(py*BEAMWIDTH+px)*4+2] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
101                                 pixels[(py*BEAMWIDTH+px)*4+1] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
102                                 pixels[(py*BEAMWIDTH+px)*4+0] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
103                                 pixels[(py*BEAMWIDTH+px)*4+3] = 255;
104                         }
105                 }
106
107                 Image_WriteTGABGRA(va("lightningbeam%i.tga", imagenumber), BEAMWIDTH, BEAMHEIGHT, pixels);
108         }
109
110         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, pixels, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR, NULL);
111
112         Mem_Free(pixels);
113         Mem_Free(image);
114 #else
115 #define BEAMWIDTH 64
116 #define BEAMHEIGHT 128
117         float r, g, b, intensity, fx, width, center;
118         int x, y;
119         unsigned char *data, *noise1, *noise2;
120
121         data = (unsigned char *)Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * 4);
122         noise1 = (unsigned char *)Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
123         noise2 = (unsigned char *)Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
124         fractalnoise(noise1, BEAMHEIGHT, BEAMHEIGHT / 8);
125         fractalnoise(noise2, BEAMHEIGHT, BEAMHEIGHT / 16);
126
127         for (y = 0;y < BEAMHEIGHT;y++)
128         {
129                 width = 0.15;//((noise1[y * BEAMHEIGHT] * (1.0f / 256.0f)) * 0.1f + 0.1f);
130                 center = (noise1[y * BEAMHEIGHT + (BEAMHEIGHT / 2)] / 256.0f) * (1.0f - width * 2.0f) + width;
131                 for (x = 0;x < BEAMWIDTH;x++, fx++)
132                 {
133                         fx = (((float) x / BEAMWIDTH) - center) / width;
134                         intensity = 1.0f - sqrt(fx * fx);
135                         if (intensity > 0)
136                                 intensity = pow(intensity, 2) * ((noise2[y * BEAMHEIGHT + x] * (1.0f / 256.0f)) * 0.33f + 0.66f);
137                         intensity = bound(0, intensity, 1);
138                         r = intensity * 1.0f;
139                         g = intensity * 1.0f;
140                         b = intensity * 1.0f;
141                         data[(y * BEAMWIDTH + x) * 4 + 2] = (unsigned char)(bound(0, r, 1) * 255.0f);
142                         data[(y * BEAMWIDTH + x) * 4 + 1] = (unsigned char)(bound(0, g, 1) * 255.0f);
143                         data[(y * BEAMWIDTH + x) * 4 + 0] = (unsigned char)(bound(0, b, 1) * 255.0f);
144                         data[(y * BEAMWIDTH + x) * 4 + 3] = (unsigned char)255;
145                 }
146         }
147
148         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR, NULL);
149         Mem_Free(noise1);
150         Mem_Free(noise2);
151         Mem_Free(data);
152 #endif
153 }
154
155 void r_lightningbeams_shutdown(void)
156 {
157         r_lightningbeamtexture = NULL;
158         r_lightningbeamqmbtexture = NULL;
159         R_FreeTexturePool(&r_lightningbeamtexturepool);
160 }
161
162 void r_lightningbeams_newmap(void)
163 {
164 }
165
166 void R_LightningBeams_Init(void)
167 {
168         Cvar_RegisterVariable(&r_lightningbeam_thickness);
169         Cvar_RegisterVariable(&r_lightningbeam_scroll);
170         Cvar_RegisterVariable(&r_lightningbeam_repeatdistance);
171         Cvar_RegisterVariable(&r_lightningbeam_color_red);
172         Cvar_RegisterVariable(&r_lightningbeam_color_green);
173         Cvar_RegisterVariable(&r_lightningbeam_color_blue);
174         Cvar_RegisterVariable(&r_lightningbeam_qmbtexture);
175         R_RegisterModule("R_LightningBeams", r_lightningbeams_start, r_lightningbeams_shutdown, r_lightningbeams_newmap);
176 }
177
178 void R_CalcLightningBeamPolygonVertex3f(float *v, const float *start, const float *end, const float *offset)
179 {
180         // near right corner
181         VectorAdd     (start, offset, (v + 0));
182         // near left corner
183         VectorSubtract(start, offset, (v + 3));
184         // far left corner
185         VectorSubtract(end  , offset, (v + 6));
186         // far right corner
187         VectorAdd     (end  , offset, (v + 9));
188 }
189
190 void R_CalcLightningBeamPolygonTexCoord2f(float *tc, float t1, float t2)
191 {
192         if (r_lightningbeam_qmbtexture.integer)
193         {
194                 // near right corner
195                 tc[0] = t1;tc[1] = 0;
196                 // near left corner
197                 tc[2] = t1;tc[3] = 1;
198                 // far left corner
199                 tc[4] = t2;tc[5] = 1;
200                 // far right corner
201                 tc[6] = t2;tc[7] = 0;
202         }
203         else
204         {
205                 // near right corner
206                 tc[0] = 0;tc[1] = t1;
207                 // near left corner
208                 tc[2] = 1;tc[3] = t1;
209                 // far left corner
210                 tc[4] = 1;tc[5] = t2;
211                 // far right corner
212                 tc[6] = 0;tc[7] = t2;
213         }
214 }
215
216 void R_FogLightningBeam_Vertex3f_Color4f(const float *v, float *c, int numverts, float r, float g, float b, float a)
217 {
218         int i;
219         float fog;
220         for (i = 0;i < numverts;i++, v += 3, c += 4)
221         {
222                 fog = FogPoint_World(v);
223                 c[0] = r * fog;
224                 c[1] = g * fog;
225                 c[2] = b * fog;
226                 c[3] = a;
227         }
228 }
229
230 float beamrepeatscale;
231
232 void R_DrawLightningBeam_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
233 {
234         int surfacelistindex;
235         rmeshstate_t m;
236         float vertex3f[12*3];
237         float texcoord2f[12*2];
238         float color4f[12*4];
239         R_Mesh_Matrix(&identitymatrix);
240         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
241         GL_DepthMask(false);
242         GL_DepthRange(0, 1);
243         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
244         GL_DepthTest(true);
245         GL_CullFace(GL_NONE);
246         if (r_lightningbeam_qmbtexture.integer && r_lightningbeamqmbtexture == NULL)
247                 r_lightningbeams_setupqmbtexture();
248         if (!r_lightningbeam_qmbtexture.integer && r_lightningbeamtexture == NULL)
249                 r_lightningbeams_setuptexture();
250
251         R_Mesh_VertexPointer(vertex3f, 0, 0);
252         R_SetupGenericShader(true);
253         // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
254         if (r_refdef.fogenabled)
255         {
256                 // per vertex colors if fog is used
257                 R_Mesh_ColorPointer(color4f, 0, 0);
258         }
259         else
260         {
261                 // solid color if fog is not used
262                 R_Mesh_ColorPointer(NULL, 0, 0);
263                 GL_Color(r_lightningbeam_color_red.value * r_refdef.view.colorscale, r_lightningbeam_color_green.value * r_refdef.view.colorscale, r_lightningbeam_color_blue.value * r_refdef.view.colorscale, 1);
264         }
265         memset(&m, 0, sizeof(m));
266         if (r_lightningbeam_qmbtexture.integer)
267                 m.tex[0] = R_GetTexture(r_lightningbeamqmbtexture);
268         else
269                 m.tex[0] = R_GetTexture(r_lightningbeamtexture);
270         m.pointer_texcoord[0] = texcoord2f;
271         R_Mesh_TextureState(&m);
272
273         for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
274         {
275                 const beam_t *b = cl.beams + surfacelist[surfacelistindex];
276                 vec3_t beamdir, right, up, offset, start, end;
277                 float length, t1, t2;
278
279                 CL_Beam_CalculatePositions(b, start, end);
280
281                 // calculate beam direction (beamdir) vector and beam length
282                 // get difference vector
283                 VectorSubtract(end, start, beamdir);
284                 // find length of difference vector
285                 length = sqrt(DotProduct(beamdir, beamdir));
286                 // calculate scale to make beamdir a unit vector (normalized)
287                 t1 = 1.0f / length;
288                 // scale beamdir so it is now normalized
289                 VectorScale(beamdir, t1, beamdir);
290
291                 // calculate up vector such that it points toward viewer, and rotates around the beamdir
292                 // get direction from start of beam to viewer
293                 VectorSubtract(r_refdef.view.origin, start, up);
294                 // remove the portion of the vector that moves along the beam
295                 // (this leaves only a vector pointing directly away from the beam)
296                 t1 = -DotProduct(up, beamdir);
297                 VectorMA(up, t1, beamdir, up);
298                 // generate right vector from forward and up, the result is unnormalized
299                 CrossProduct(beamdir, up, right);
300                 // now normalize the right vector and up vector
301                 VectorNormalize(right);
302                 VectorNormalize(up);
303
304                 // calculate T coordinate scrolling (start and end texcoord along the beam)
305                 t1 = r_refdef.scene.time * -r_lightningbeam_scroll.value;// + beamrepeatscale * DotProduct(start, beamdir);
306                 t1 = t1 - (int) t1;
307                 t2 = t1 + beamrepeatscale * length;
308
309                 // the beam is 3 polygons in this configuration:
310                 //  *   2
311                 //   * *
312                 // 1******
313                 //   * *
314                 //  *   3
315                 // they are showing different portions of the beam texture, creating an
316                 // illusion of a beam that appears to curl around in 3D space
317                 // (and realize that the whole polygon assembly orients itself to face
318                 //  the viewer)
319
320                 // polygon 1, verts 0-3
321                 VectorScale(right, r_lightningbeam_thickness.value, offset);
322                 R_CalcLightningBeamPolygonVertex3f(vertex3f + 0, start, end, offset);
323                 // polygon 2, verts 4-7
324                 VectorAdd(right, up, offset);
325                 VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
326                 R_CalcLightningBeamPolygonVertex3f(vertex3f + 12, start, end, offset);
327                 // polygon 3, verts 8-11
328                 VectorSubtract(right, up, offset);
329                 VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
330                 R_CalcLightningBeamPolygonVertex3f(vertex3f + 24, start, end, offset);
331                 R_CalcLightningBeamPolygonTexCoord2f(texcoord2f + 0, t1, t2);
332                 R_CalcLightningBeamPolygonTexCoord2f(texcoord2f + 8, t1 + 0.33, t2 + 0.33);
333                 R_CalcLightningBeamPolygonTexCoord2f(texcoord2f + 16, t1 + 0.66, t2 + 0.66);
334                 if (r_refdef.fogenabled)
335                 {
336                         // per vertex colors if fog is used
337                         R_FogLightningBeam_Vertex3f_Color4f(vertex3f, color4f, 12, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
338                 }
339
340                 // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
341                 GL_LockArrays(0, 12);
342                 R_Mesh_Draw(0, 12, 0, 6, NULL, r_lightningbeamelements, 0, 0);
343                 GL_LockArrays(0, 0);
344         }
345 }
346
347 extern cvar_t cl_beams_polygons;
348 void R_DrawLightningBeams(void)
349 {
350         int i;
351         beam_t *b;
352
353         if (!cl_beams_polygons.integer)
354                 return;
355
356         beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
357         for (i = 0, b = cl.beams;i < cl.num_beams;i++, b++)
358         {
359                 if (b->model && b->lightning)
360                 {
361                         vec3_t org, start, end, dir;
362                         vec_t dist;
363                         CL_Beam_CalculatePositions(b, start, end);
364                         // calculate the nearest point on the line (beam) for depth sorting
365                         VectorSubtract(end, start, dir);
366                         dist = (DotProduct(r_refdef.view.origin, dir) - DotProduct(start, dir)) / (DotProduct(end, dir) - DotProduct(start, dir));
367                         dist = bound(0, dist, 1);
368                         VectorLerp(start, dist, end, org);
369                         // now we have the nearest point on the line, so sort with it
370                         R_MeshQueue_AddTransparent(org, R_DrawLightningBeam_TransparentCallback, NULL, i, NULL);
371                 }
372         }
373 }
374