Don't allow shortcuts with Ctrl+Alt because on Windows they can be used as the AltGr...
[xonotic/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", "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"};
12
13 static texture_t cl_beams_externaltexture;
14 static texture_t cl_beams_builtintexture;
15
16 static void r_lightningbeams_start(void)
17 {
18         memset(&cl_beams_externaltexture, 0, sizeof(cl_beams_externaltexture));
19         memset(&cl_beams_builtintexture, 0, sizeof(cl_beams_builtintexture));
20 }
21
22 static void CL_Beams_SetupExternalTexture(void)
23 {
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_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOCULLFACE))
25                 Cvar_SetValueQuick(&r_lightningbeam_qmbtexture, false);
26 }
27
28 static void CL_Beams_SetupBuiltinTexture(void)
29 {
30         // beam direction is horizontal in the lightning texture
31         int texwidth = 128;
32         int texheight = 64;
33         float r, g, b, intensity, thickness = texheight * 0.25f, border = thickness + 2.0f, ithickness = 1.0f / thickness, center, n;
34         int x, y;
35         unsigned char *data;
36         skinframe_t *skinframe;
37         float centersamples[17][2];
38
39         // make a repeating noise pattern for the beam path
40         for (x = 0; x < 16; x++)
41         {
42                 centersamples[x][0] = lhrandom(border, texheight - border);
43                 centersamples[x][1] = lhrandom(0.2f, 1.00f);
44         }
45         centersamples[16][0] = centersamples[0][0];
46         centersamples[16][1] = centersamples[0][1];
47
48         data = (unsigned char *)Mem_Alloc(tempmempool, texwidth * texheight * 4);
49
50         // iterate by columns and draw the entire column of pixels
51         for (x = 0; x < texwidth; x++)
52         {
53                 r = x * 16.0f / texwidth;
54                 y = (int)r;
55                 g = r - y;
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++)
59                 {
60                         intensity = 1.0f - fabs((y - center) * ithickness);
61                         if (intensity > 0)
62                         {
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));
70                         }
71                         else
72                                 intensity = 0.0f;
73                         data[(y * texwidth + x) * 4 + 3] = (unsigned char)255;
74                 }
75         }
76
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_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_VERTEXCOLOR | MATERIALFLAG_ALPHAGEN_VERTEX, skinframe);
79         Mem_Free(data);
80 }
81
82 static void r_lightningbeams_shutdown(void)
83 {
84         memset(&cl_beams_externaltexture, 0, sizeof(cl_beams_externaltexture));
85         memset(&cl_beams_builtintexture, 0, sizeof(cl_beams_builtintexture));
86 }
87
88 static void r_lightningbeams_newmap(void)
89 {
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);
94 }
95
96 void R_LightningBeams_Init(void)
97 {
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);
106 }
107
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)
109 {
110         int e0, e1, e2, e3;
111         vec3_t n;
112         vec3_t dir;
113         float c[4];
114
115         Vector4Set(c, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1.0f);
116
117         VectorSubtract(end, start, dir);
118         CrossProduct(dir, offset, n);
119         VectorNormalize(n);
120
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);
127 }
128
129 void CL_Beam_AddPolygons(const beam_t *b)
130 {
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;
135         dp_model_t *mod;
136         msurface_t *surf;
137
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();
142
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)
150         t1 = 1.0f / length;
151         // scale beamdir so it is now normalized
152         VectorScale(beamdir, t1, beamdir);
153
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);
165         VectorNormalize(up);
166
167         // calculate T coordinate scrolling (start and end texcoord along the beam)
168         t1 = beamscroll;
169         t1 = t1 - (int)t1;
170         t2 = t1 + beamrepeatscale * length;
171
172         // the beam is 3 polygons in this configuration:
173         //  *   2
174         //   * *
175         // 1*****
176         //   * *
177         //  *   3
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
181         //  the viewer)
182
183         mod = &cl_meshentitymodels[MESH_PARTICLES];
184         surf = Mod_Mesh_AddSurface(mod, r_lightningbeam_qmbtexture.integer ? &cl_beams_externaltexture : &cl_beams_builtintexture, false);
185         // polygon 1
186         VectorM(r_lightningbeam_thickness.value, right, offset);
187         CL_Beam_AddQuad(mod, surf, start, end, offset, t1, t2);
188         // polygon 2
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);
191         // polygon 3
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);
194 }