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"};
13 rtexture_t *r_lightningbeamtexture;
14 rtexture_t *r_lightningbeamqmbtexture;
15 rtexturepool_t *r_lightningbeamtexturepool;
17 int r_lightningbeamelements[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
19 void r_lightningbeams_start(void)
21 r_lightningbeamtexturepool = R_AllocTexturePool();
22 r_lightningbeamtexture = NULL;
23 r_lightningbeamqmbtexture = NULL;
26 void r_lightningbeams_setupqmbtexture(void)
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);
33 void r_lightningbeams_setuptexture(void)
39 int i, j, px, py, nearestpathindex, imagenumber;
40 float particlex, particley, particlexv, particleyv, dx, dy, s, maxpathstrength;
43 struct {float x, y, strength;} path[PATHPOINTS], temppath;
45 image = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
46 pixels = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(qbyte[4]));
48 for (imagenumber = 0, maxpathstrength = 0.0339476;maxpathstrength < 0.5;imagenumber++, maxpathstrength += 0.01)
50 for (i = 0;i < PATHPOINTS;i++)
52 path[i].x = lhrandom(0, 1);
53 path[i].y = lhrandom(0.2, 0.8);
54 path[i].strength = lhrandom(0, 1);
56 for (i = 0;i < PATHPOINTS;i++)
58 for (j = i + 1;j < PATHPOINTS;j++)
60 if (path[j].x < path[i].x)
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++)
75 for (nearestpathindex = 0;nearestpathindex < PATHPOINTS;nearestpathindex++)
76 if (path[nearestpathindex].x > particlex)
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;
92 for (py = 0;py < BEAMHEIGHT;py++)
94 for (px = 0;px < BEAMWIDTH;px++)
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;
103 Image_WriteTGARGBA(va("lightningbeam%i.tga", imagenumber), BEAMWIDTH, BEAMHEIGHT, pixels);
106 r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, pixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
112 #define BEAMHEIGHT 128
113 float r, g, b, intensity, fx, width, center;
115 qbyte *data, *noise1, *noise2;
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);
123 for (y = 0;y < BEAMHEIGHT;y++)
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++)
129 fx = (((float) x / BEAMWIDTH) - center) / width;
130 intensity = 1.0f - sqrt(fx * fx);
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;
144 r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
151 void r_lightningbeams_shutdown(void)
153 r_lightningbeamtexture = NULL;
154 r_lightningbeamqmbtexture = NULL;
155 R_FreeTexturePool(&r_lightningbeamtexturepool);
158 void r_lightningbeams_newmap(void)
162 void R_LightningBeams_Init(void)
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);
174 void R_CalcLightningBeamPolygonVertex3f(float *v, const float *start, const float *end, const float *offset)
177 VectorAdd (start, offset, (v + 0));
179 VectorSubtract(start, offset, (v + 3));
181 VectorSubtract(end , offset, (v + 6));
183 VectorAdd (end , offset, (v + 9));
186 void R_CalcLightningBeamPolygonTexCoord2f(float *tc, float t1, float t2)
188 if (r_lightningbeam_qmbtexture.integer)
191 tc[0] = t1;tc[1] = 0;
193 tc[2] = t1;tc[3] = 1;
195 tc[4] = t2;tc[5] = 1;
197 tc[6] = t2;tc[7] = 0;
202 tc[0] = 0;tc[1] = t1;
204 tc[2] = 1;tc[3] = t1;
206 tc[4] = 1;tc[5] = t2;
208 tc[6] = 0;tc[7] = t2;
212 void R_FogLightningBeam_Vertex3f_Color4f(const float *v, float *c, int numverts, float r, float g, float b, float a)
217 for (i = 0;i < numverts;i++, v += 3, c += 4)
219 VectorSubtract(v, r_vieworigin, fogvec);
220 ifog = 1 - exp(fogdensity/DotProduct(fogvec,fogvec));
228 float beamrepeatscale;
230 void R_DrawLightningBeamCallback(const void *calldata1, int calldata2)
232 const beam_t *b = calldata1;
234 vec3_t beamdir, right, up, offset;
235 float length, t1, t2;
237 R_Mesh_Matrix(&r_identitymatrix);
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)
246 // scale beamdir so it is now normalized
247 VectorScale(beamdir, t1, beamdir);
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);
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);
265 t2 = t1 + beamrepeatscale * length;
267 // the beam is 3 polygons in this configuration:
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
278 memset(&m, 0, sizeof(m));
279 if (r_lightningbeam_qmbtexture.integer)
280 m.tex[0] = R_GetTexture(r_lightningbeamqmbtexture);
282 m.tex[0] = R_GetTexture(r_lightningbeamtexture);
283 m.pointer_texcoord[0] = varray_texcoord2f[0];
284 R_Mesh_State_Texture(&m);
286 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
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();
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);
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);
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);
322 // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
323 R_Mesh_Draw(12, 6, r_lightningbeamelements);
326 void R_DrawLightningBeams(void)
332 if (!cl_beams_polygons.integer)
335 beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
336 for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
338 if (b->model && b->endtime >= cl.time && b->lightning)
340 VectorAdd(b->start, b->end, org);
341 VectorScale(org, 0.5f, org);
342 R_MeshQueue_AddTransparent(org, R_DrawLightningBeamCallback, b, 0);