5 cvar_t r_lightningbeam_thickness = {CF_CLIENT | CF_ARCHIVE, "r_lightningbeam_thickness", "8", "thickness of the lightning beam effect"};
6 cvar_t r_lightningbeam_scroll = {CF_CLIENT | CF_ARCHIVE, "r_lightningbeam_scroll", "5", "speed of texture scrolling on the lightning beam effect"};
7 cvar_t r_lightningbeam_repeatdistance = {CF_CLIENT | CF_ARCHIVE, "r_lightningbeam_repeatdistance", "128", "how far to stretch the texture along the lightning beam effect"};
8 cvar_t r_lightningbeam_color_red = {CF_CLIENT | CF_ARCHIVE, "r_lightningbeam_color_red", "1", "color of the lightning beam effect"};
9 cvar_t r_lightningbeam_color_green = {CF_CLIENT | CF_ARCHIVE, "r_lightningbeam_color_green", "1", "color of the lightning beam effect"};
10 cvar_t r_lightningbeam_color_blue = {CF_CLIENT | CF_ARCHIVE, "r_lightningbeam_color_blue", "1", "color of the lightning beam effect"};
11 cvar_t r_lightningbeam_qmbtexture = {CF_CLIENT | CF_ARCHIVE, "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(r_main_mempool, "r_lightning.c", &cl_beams_externaltexture, "textures/particles/lightning", false, false, TEXF_ALPHA | TEXF_FORCELINEAR, MATERIALFLAG_WALL | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_VERTEXCOLOR | MATERIALFLAG_ALPHAGEN_VERTEX | MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW))
25 Cvar_SetValueQuick(&r_lightningbeam_qmbtexture, false);
28 static void CL_Beams_SetupBuiltinTexture(void)
30 // beam direction is horizontal in the lightning texture
33 float r, g, b, intensity, thickness = texheight * 0.25f, border = thickness + 2.0f, ithickness = 1.0f / thickness, center, n;
36 skinframe_t *skinframe;
37 float centersamples[17][2];
39 // make a repeating noise pattern for the beam path
40 for (x = 0; x < 16; x++)
42 centersamples[x][0] = lhrandom(border, texheight - border);
43 centersamples[x][1] = lhrandom(0.2f, 1.00f);
45 centersamples[16][0] = centersamples[0][0];
46 centersamples[16][1] = centersamples[0][1];
48 data = (unsigned char *)Mem_Alloc(tempmempool, texwidth * texheight * 4);
50 // iterate by columns and draw the entire column of pixels
51 for (x = 0; x < texwidth; x++)
53 r = x * 16.0f / texwidth;
56 center = centersamples[y][0] * (1.0f - g) + centersamples[y+1][0] * g;
57 n = centersamples[y][1] * (1.0f - g) + centersamples[y + 1][1] * g;
58 for (y = 0; y < texheight; y++)
60 intensity = 1.0f - fabs((y - center) * ithickness);
63 intensity = pow(intensity * n, 2);
64 r = intensity * 1.000f * 255.0f;
65 g = intensity * 2.000f * 255.0f;
66 b = intensity * 4.000f * 255.0f;
67 data[(y * texwidth + x) * 4 + 2] = (unsigned char)(bound(0, r, 255));
68 data[(y * texwidth + x) * 4 + 1] = (unsigned char)(bound(0, g, 255));
69 data[(y * texwidth + x) * 4 + 0] = (unsigned char)(bound(0, b, 255));
73 data[(y * texwidth + x) * 4 + 3] = (unsigned char)255;
77 skinframe = R_SkinFrame_LoadInternalBGRA("lightningbeam", TEXF_FORCELINEAR, data, texwidth, texheight, 0, 0, 0, false);
78 Mod_LoadCustomMaterial(r_main_mempool, &cl_beams_builtintexture, "cl_beams_builtintexture", 0, MATERIALFLAG_WALL | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_VERTEXCOLOR | MATERIALFLAG_ALPHAGEN_VERTEX | MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW, skinframe);
82 static void r_lightningbeams_shutdown(void)
84 memset(&cl_beams_externaltexture, 0, sizeof(cl_beams_externaltexture));
85 memset(&cl_beams_builtintexture, 0, sizeof(cl_beams_builtintexture));
88 static void r_lightningbeams_newmap(void)
90 if (cl_beams_externaltexture.currentskinframe)
91 R_SkinFrame_MarkUsed(cl_beams_externaltexture.currentskinframe);
92 if (cl_beams_builtintexture.currentskinframe)
93 R_SkinFrame_MarkUsed(cl_beams_builtintexture.currentskinframe);
96 void R_LightningBeams_Init(void)
98 Cvar_RegisterVariable(&r_lightningbeam_thickness);
99 Cvar_RegisterVariable(&r_lightningbeam_scroll);
100 Cvar_RegisterVariable(&r_lightningbeam_repeatdistance);
101 Cvar_RegisterVariable(&r_lightningbeam_color_red);
102 Cvar_RegisterVariable(&r_lightningbeam_color_green);
103 Cvar_RegisterVariable(&r_lightningbeam_color_blue);
104 Cvar_RegisterVariable(&r_lightningbeam_qmbtexture);
105 R_RegisterModule("R_LightningBeams", r_lightningbeams_start, r_lightningbeams_shutdown, r_lightningbeams_newmap, NULL, NULL);
108 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)
115 Vector4Set(c, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1.0f);
117 VectorSubtract(end, start, dir);
118 CrossProduct(dir, offset, n);
121 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]);
122 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]);
123 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]);
124 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]);
125 Mod_Mesh_AddTriangle(mod, surf, e0, e1, e2);
126 Mod_Mesh_AddTriangle(mod, surf, e0, e2, e3);
129 void CL_Beam_AddPolygons(const beam_t *b)
131 vec3_t beamdir, right, up, offset, start, end;
132 vec_t beamscroll = r_refdef.scene.time * -r_lightningbeam_scroll.value;
133 vec_t beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
134 float length, t1, t2;
138 if (r_lightningbeam_qmbtexture.integer && cl_beams_externaltexture.currentskinframe == NULL)
139 CL_Beams_SetupExternalTexture();
140 if (!r_lightningbeam_qmbtexture.integer && cl_beams_builtintexture.currentskinframe == NULL)
141 CL_Beams_SetupBuiltinTexture();
143 // calculate beam direction (beamdir) vector and beam length
144 // get difference vector
145 CL_Beam_CalculatePositions(b, start, end);
146 VectorSubtract(end, start, beamdir);
147 // find length of difference vector
148 length = sqrt(DotProduct(beamdir, beamdir));
149 // calculate scale to make beamdir a unit vector (normalized)
151 // scale beamdir so it is now normalized
152 VectorScale(beamdir, t1, beamdir);
154 // calculate up vector such that it points toward viewer, and rotates around the beamdir
155 // get direction from start of beam to viewer
156 VectorSubtract(r_refdef.view.origin, start, up);
157 // remove the portion of the vector that moves along the beam
158 // (this leaves only a vector pointing directly away from the beam)
159 t1 = -DotProduct(up, beamdir);
160 VectorMA(up, t1, beamdir, up);
161 // generate right vector from forward and up, the result is unnormalized
162 CrossProduct(beamdir, up, right);
163 // now normalize the right vector and up vector
164 VectorNormalize(right);
167 // calculate T coordinate scrolling (start and end texcoord along the beam)
170 t2 = t1 + beamrepeatscale * length;
172 // the beam is 3 polygons in this configuration:
178 // they are showing different portions of the beam texture, creating an
179 // illusion of a beam that appears to curl around in 3D space
180 // (and realize that the whole polygon assembly orients itself to face
183 mod = CL_Mesh_Scene();
184 surf = Mod_Mesh_AddSurface(mod, r_lightningbeam_qmbtexture.integer ? &cl_beams_externaltexture : &cl_beams_builtintexture, false);
186 VectorM(r_lightningbeam_thickness.value, right, offset);
187 CL_Beam_AddQuad(mod, surf, start, end, offset, t1, t2);
189 VectorMAM(r_lightningbeam_thickness.value * 0.70710681f, right, r_lightningbeam_thickness.value * 0.70710681f, up, offset);
190 CL_Beam_AddQuad(mod, surf, start, end, offset, t1 + 0.33f, t2 + 0.33f);
192 VectorMAM(r_lightningbeam_thickness.value * 0.70710681f, right, r_lightningbeam_thickness.value * -0.70710681f, up, offset);
193 CL_Beam_AddQuad(mod, surf, start, end, offset, t1 + 0.66f, t2 + 0.66f);