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", "128"};
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;
41 unsigned char *pixels;
43 struct lightningpathnode_s
47 path[PATHPOINTS], temppath;
49 image = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
50 pixels = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(unsigned char[4]));
52 for (imagenumber = 0, maxpathstrength = 0.0339476;maxpathstrength < 0.5;imagenumber++, maxpathstrength += 0.01)
54 for (i = 0;i < PATHPOINTS;i++)
56 path[i].x = lhrandom(0, 1);
57 path[i].y = lhrandom(0.2, 0.8);
58 path[i].strength = lhrandom(0, 1);
60 for (i = 0;i < PATHPOINTS;i++)
62 for (j = i + 1;j < PATHPOINTS;j++)
64 if (path[j].x < path[i].x)
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++)
79 for (nearestpathindex = 0;nearestpathindex < PATHPOINTS;nearestpathindex++)
80 if (path[nearestpathindex].x > particlex)
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;
96 for (py = 0;py < BEAMHEIGHT;py++)
98 for (px = 0;px < BEAMWIDTH;px++)
100 pixels[(py*BEAMWIDTH+px)*4+0] = 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+2] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
103 pixels[(py*BEAMWIDTH+px)*4+3] = 255;
107 Image_WriteTGARGBA(va("lightningbeam%i.tga", imagenumber), BEAMWIDTH, BEAMHEIGHT, pixels);
110 r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, pixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
116 #define BEAMHEIGHT 128
117 float r, g, b, intensity, fx, width, center;
119 unsigned char *data, *noise1, *noise2;
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);
127 for (y = 0;y < BEAMHEIGHT;y++)
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++)
133 fx = (((float) x / BEAMWIDTH) - center) / width;
134 intensity = 1.0f - sqrt(fx * fx);
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 + 0] = (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 + 2] = (unsigned char)(bound(0, b, 1) * 255.0f);
144 data[(y * BEAMWIDTH + x) * 4 + 3] = (unsigned char)255;
148 r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
155 void r_lightningbeams_shutdown(void)
157 r_lightningbeamtexture = NULL;
158 r_lightningbeamqmbtexture = NULL;
159 R_FreeTexturePool(&r_lightningbeamtexturepool);
162 void r_lightningbeams_newmap(void)
166 void R_LightningBeams_Init(void)
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);
178 void R_CalcLightningBeamPolygonVertex3f(float *v, const float *start, const float *end, const float *offset)
181 VectorAdd (start, offset, (v + 0));
183 VectorSubtract(start, offset, (v + 3));
185 VectorSubtract(end , offset, (v + 6));
187 VectorAdd (end , offset, (v + 9));
190 void R_CalcLightningBeamPolygonTexCoord2f(float *tc, float t1, float t2)
192 if (r_lightningbeam_qmbtexture.integer)
195 tc[0] = t1;tc[1] = 0;
197 tc[2] = t1;tc[3] = 1;
199 tc[4] = t2;tc[5] = 1;
201 tc[6] = t2;tc[7] = 0;
206 tc[0] = 0;tc[1] = t1;
208 tc[2] = 1;tc[3] = t1;
210 tc[4] = 1;tc[5] = t2;
212 tc[6] = 0;tc[7] = t2;
216 void R_FogLightningBeam_Vertex3f_Color4f(const float *v, float *c, int numverts, float r, float g, float b, float a)
220 for (i = 0;i < numverts;i++, v += 3, c += 4)
222 ifog = 1 - VERTEXFOGTABLE(VectorDistance(v, r_vieworigin));
230 float beamrepeatscale;
232 void R_DrawLightningBeam_TransparentCallback(const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
234 const beam_t *b = cl_beams + surfacenumber;
236 vec3_t beamdir, right, up, offset;
237 float length, t1, t2;
239 R_Mesh_Matrix(&r_identitymatrix);
241 // calculate beam direction (beamdir) vector and beam length
242 // get difference vector
243 VectorSubtract(b->end, b->start, beamdir);
244 // find length of difference vector
245 length = sqrt(DotProduct(beamdir, beamdir));
246 // calculate scale to make beamdir a unit vector (normalized)
248 // scale beamdir so it is now normalized
249 VectorScale(beamdir, t1, beamdir);
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_vieworigin, b->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);
264 // calculate T coordinate scrolling (start and end texcoord along the beam)
265 t1 = r_refdef.time * -r_lightningbeam_scroll.value;// + beamrepeatscale * DotProduct(b->start, beamdir);
267 t2 = t1 + beamrepeatscale * length;
269 // the beam is 3 polygons in this configuration:
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
280 memset(&m, 0, sizeof(m));
281 if (r_lightningbeam_qmbtexture.integer)
282 m.tex[0] = R_GetTexture(r_lightningbeamqmbtexture);
284 m.tex[0] = R_GetTexture(r_lightningbeamtexture);
285 m.pointer_texcoord[0] = varray_texcoord2f[0];
286 m.pointer_vertex = varray_vertex3f;
288 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
291 if (r_lightningbeam_qmbtexture.integer && r_lightningbeamqmbtexture == NULL)
292 r_lightningbeams_setupqmbtexture();
293 if (!r_lightningbeam_qmbtexture.integer && r_lightningbeamtexture == NULL)
294 r_lightningbeams_setuptexture();
296 // polygon 1, verts 0-3
297 VectorScale(right, r_lightningbeam_thickness.value, offset);
298 R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 0, b->start, b->end, offset);
299 // polygon 2, verts 4-7
300 VectorAdd(right, up, offset);
301 VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
302 R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 12, b->start, b->end, offset);
303 // polygon 3, verts 8-11
304 VectorSubtract(right, up, offset);
305 VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
306 R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 24, b->start, b->end, offset);
307 R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 0, t1, t2);
308 R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 8, t1 + 0.33, t2 + 0.33);
309 R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 16, t1 + 0.66, t2 + 0.66);
313 // per vertex colors if fog is used
314 m.pointer_color = varray_color4f;
315 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);
319 // solid color if fog is not used
320 GL_Color(r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
324 // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
325 GL_LockArrays(0, 12);
326 R_Mesh_Draw(0, 12, 6, r_lightningbeamelements);
330 void R_DrawLightningBeams(void)
336 if (!cl_beams_polygons.integer)
339 beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
340 for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
342 if (b->model && b->endtime >= r_refdef.time && b->lightning)
344 VectorAdd(b->start, b->end, org);
345 VectorScale(org, 0.5f, org);
346 R_MeshQueue_AddTransparent(org, R_DrawLightningBeam_TransparentCallback, NULL, i, NULL);