5 cvar_t r_lightningbeam_thickness = {CVAR_SAVE, "r_lightningbeam_thickness", "8", "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"};
13 static texture_t cl_beams_externaltexture;
14 static texture_t cl_beams_builtintexture;
16 static void r_lightningbeams_start(void)
18 memset(&cl_beams_externaltexture, 0, sizeof(cl_beams_externaltexture));
19 memset(&cl_beams_builtintexture, 0, sizeof(cl_beams_builtintexture));
22 static void CL_Beams_SetupExternalTexture(void)
24 if (Mod_LoadTextureFromQ3Shader(&cl_beams_externaltexture, "textures/particles/lightning", false, false, TEXF_ALPHA | TEXF_FORCELINEAR))
25 cl_beams_externaltexture.basematerialflags = cl_beams_externaltexture.currentmaterialflags = MATERIALFLAG_WALL | MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOCULLFACE;
27 Cvar_SetValueQuick(&r_lightningbeam_qmbtexture, false);
30 static void CL_Beams_SetupBuiltinTexture(void)
32 // beam direction is horizontal in the lightning texture
35 float r, g, b, intensity, thickness = texheight * 0.25f, border = thickness + 2.0f, ithickness = 1.0f / thickness, center, n;
38 skinframe_t *skinframe;
39 float centersamples[17][2];
41 // make a repeating noise pattern for the beam path
42 for (x = 0; x < 16; x++)
44 centersamples[x][0] = lhrandom(border, texheight - border);
45 centersamples[x][1] = lhrandom(0.2f, 1.00f);
47 centersamples[16][0] = centersamples[0][0];
48 centersamples[16][1] = centersamples[0][1];
50 data = (unsigned char *)Mem_Alloc(tempmempool, texwidth * texheight * 4);
52 // iterate by columns and draw the entire column of pixels
53 for (x = 0; x < texwidth; x++)
55 r = x * 16.0f / texwidth;
58 center = centersamples[y][0] * (1.0f - g) + centersamples[y+1][0] * g;
59 n = centersamples[y][1] * (1.0f - g) + centersamples[y + 1][1] * g;
60 for (y = 0; y < texheight; y++)
62 intensity = 1.0f - fabs((y - center) * ithickness);
65 intensity = pow(intensity * n, 2);
66 r = intensity * 1.000f * 255.0f;
67 g = intensity * 2.000f * 255.0f;
68 b = intensity * 4.000f * 255.0f;
69 data[(y * texwidth + x) * 4 + 2] = (unsigned char)(bound(0, r, 255));
70 data[(y * texwidth + x) * 4 + 1] = (unsigned char)(bound(0, g, 255));
71 data[(y * texwidth + x) * 4 + 0] = (unsigned char)(bound(0, b, 255));
75 data[(y * texwidth + x) * 4 + 3] = (unsigned char)255;
79 skinframe = R_SkinFrame_LoadInternalBGRA("lightningbeam", TEXF_FORCELINEAR, data, texwidth, texheight, false);
80 Mod_LoadCustomMaterial(&cl_beams_builtintexture, "cl_beams_builtintexture", 0, MATERIALFLAG_WALL | MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOCULLFACE, skinframe);
84 static void r_lightningbeams_shutdown(void)
86 memset(&cl_beams_externaltexture, 0, sizeof(cl_beams_externaltexture));
87 memset(&cl_beams_builtintexture, 0, sizeof(cl_beams_builtintexture));
90 static void r_lightningbeams_newmap(void)
92 if (cl_beams_externaltexture.currentskinframe)
93 R_SkinFrame_MarkUsed(cl_beams_externaltexture.currentskinframe);
94 if (cl_beams_builtintexture.currentskinframe)
95 R_SkinFrame_MarkUsed(cl_beams_builtintexture.currentskinframe);
98 void R_LightningBeams_Init(void)
100 Cvar_RegisterVariable(&r_lightningbeam_thickness);
101 Cvar_RegisterVariable(&r_lightningbeam_scroll);
102 Cvar_RegisterVariable(&r_lightningbeam_repeatdistance);
103 Cvar_RegisterVariable(&r_lightningbeam_color_red);
104 Cvar_RegisterVariable(&r_lightningbeam_color_green);
105 Cvar_RegisterVariable(&r_lightningbeam_color_blue);
106 Cvar_RegisterVariable(&r_lightningbeam_qmbtexture);
107 R_RegisterModule("R_LightningBeams", r_lightningbeams_start, r_lightningbeams_shutdown, r_lightningbeams_newmap, NULL, NULL);
110 static void CL_Beam_AddQuad(dp_model_t *mod, msurface_t *surf, const vec3_t start, const vec3_t end, const vec3_t offset, float t1, float t2)
117 Vector4Set(c, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1.0f);
119 VectorSubtract(end, start, dir);
120 CrossProduct(dir, offset, n);
123 e0 = Mod_Mesh_IndexForVertex(mod, surf, start[0] + offset[0], start[1] + offset[1], start[2] + offset[2], n[0], n[1], n[2], t1, 0, 0, 0, c[0], c[1], c[2], c[3]);
124 e1 = Mod_Mesh_IndexForVertex(mod, surf, start[0] - offset[0], start[1] - offset[1], start[2] - offset[2], n[0], n[1], n[2], t1, 1, 0, 0, c[0], c[1], c[2], c[3]);
125 e2 = Mod_Mesh_IndexForVertex(mod, surf, end[0] - offset[0], end[1] - offset[1], end[2] - offset[2], n[0], n[1], n[2], t2, 1, 0, 0, c[0], c[1], c[2], c[3]);
126 e3 = Mod_Mesh_IndexForVertex(mod, surf, end[0] + offset[0], end[1] + offset[1], end[2] + offset[2], n[0], n[1], n[2], t2, 0, 0, 0, c[0], c[1], c[2], c[3]);
127 Mod_Mesh_AddTriangle(mod, surf, e0, e1, e2);
128 Mod_Mesh_AddTriangle(mod, surf, e0, e2, e3);
131 void CL_Beam_AddPolygons(const beam_t *b)
133 vec3_t beamdir, right, up, offset, start, end;
134 vec_t beamscroll = r_refdef.scene.time * -r_lightningbeam_scroll.value;
135 vec_t beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
136 float length, t1, t2;
140 if (r_lightningbeam_qmbtexture.integer && cl_beams_externaltexture.currentskinframe == NULL)
141 CL_Beams_SetupExternalTexture();
142 if (!r_lightningbeam_qmbtexture.integer && cl_beams_builtintexture.currentskinframe == NULL)
143 CL_Beams_SetupBuiltinTexture();
145 // calculate beam direction (beamdir) vector and beam length
146 // get difference vector
147 CL_Beam_CalculatePositions(b, start, end);
148 VectorSubtract(end, start, beamdir);
149 // find length of difference vector
150 length = sqrt(DotProduct(beamdir, beamdir));
151 // calculate scale to make beamdir a unit vector (normalized)
153 // scale beamdir so it is now normalized
154 VectorScale(beamdir, t1, beamdir);
156 // calculate up vector such that it points toward viewer, and rotates around the beamdir
157 // get direction from start of beam to viewer
158 VectorSubtract(r_refdef.view.origin, start, up);
159 // remove the portion of the vector that moves along the beam
160 // (this leaves only a vector pointing directly away from the beam)
161 t1 = -DotProduct(up, beamdir);
162 VectorMA(up, t1, beamdir, up);
163 // generate right vector from forward and up, the result is unnormalized
164 CrossProduct(beamdir, up, right);
165 // now normalize the right vector and up vector
166 VectorNormalize(right);
169 // calculate T coordinate scrolling (start and end texcoord along the beam)
172 t2 = t1 + beamrepeatscale * length;
174 // the beam is 3 polygons in this configuration:
180 // they are showing different portions of the beam texture, creating an
181 // illusion of a beam that appears to curl around in 3D space
182 // (and realize that the whole polygon assembly orients itself to face
185 mod = &cl_meshentitymodels[MESH_PARTICLES];
186 surf = Mod_Mesh_AddSurface(mod, r_lightningbeam_qmbtexture.integer ? &cl_beams_externaltexture : &cl_beams_builtintexture);
188 VectorM(r_lightningbeam_thickness.value, right, offset);
189 CL_Beam_AddQuad(mod, surf, start, end, offset, t1, t2);
191 VectorMAM(r_lightningbeam_thickness.value * 0.70710681f, right, r_lightningbeam_thickness.value * 0.70710681f, up, offset);
192 CL_Beam_AddQuad(mod, surf, start, end, offset, t1 + 0.33f, t2 + 0.33f);
194 VectorMAM(r_lightningbeam_thickness.value * 0.70710681f, right, r_lightningbeam_thickness.value * -0.70710681f, up, offset);
195 CL_Beam_AddQuad(mod, surf, start, end, offset, t1 + 0.66f, t2 + 0.66f);