optional portal based determination of lit surfaces (good speed gain, enough to make...
[divverent/darkplaces.git] / r_shadow.c
1
2 #include "quakedef.h"
3 #include "r_shadow.h"
4 #include "cl_collision.h"
5 #include "portals.h"
6
7 extern void R_Shadow_EditLights_Init(void);
8
9 #define SHADOWSTAGE_NONE 0
10 #define SHADOWSTAGE_STENCIL 1
11 #define SHADOWSTAGE_LIGHT 2
12 #define SHADOWSTAGE_ERASESTENCIL 3
13
14 int r_shadowstage = SHADOWSTAGE_NONE;
15 int r_shadow_reloadlights = false;
16
17 int r_shadow_lightingmode = 0;
18
19 mempool_t *r_shadow_mempool;
20
21 int maxshadowelements;
22 int *shadowelements;
23 int maxtrianglefacinglight;
24 qbyte *trianglefacinglight;
25
26 rtexturepool_t *r_shadow_texturepool;
27 rtexture_t *r_shadow_normalscubetexture;
28 rtexture_t *r_shadow_attenuation2dtexture;
29 rtexture_t *r_shadow_blankbumptexture;
30 rtexture_t *r_shadow_blankglosstexture;
31 rtexture_t *r_shadow_blankwhitetexture;
32
33 cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5"};
34 cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1"};
35 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
36 cvar_t r_shadow_realtime = {0, "r_shadow_realtime", "0"};
37 cvar_t r_shadow_gloss = {0, "r_shadow_gloss", "1"};
38 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
39 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
40 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
41 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0"};
42 cvar_t r_shadow_shadownudge = {0, "r_shadow_shadownudge", "1"};
43 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1"};
44
45 int c_rt_lights, c_rt_clears, c_rt_scissored;
46 int c_rt_shadowmeshes, c_rt_shadowtris, c_rt_lightmeshes, c_rt_lighttris;
47 int c_rtcached_shadowmeshes, c_rtcached_shadowtris;
48
49 void R_Shadow_ClearWorldLights(void);
50 void R_Shadow_SaveWorldLights(void);
51 void R_Shadow_LoadWorldLights(void);
52 void R_Shadow_LoadLightsFile(void);
53 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
54
55 void r_shadow_start(void)
56 {
57         // allocate vertex processing arrays
58         r_shadow_mempool = Mem_AllocPool("R_Shadow");
59         maxshadowelements = 0;
60         shadowelements = NULL;
61         maxtrianglefacinglight = 0;
62         trianglefacinglight = NULL;
63         r_shadow_normalscubetexture = NULL;
64         r_shadow_attenuation2dtexture = NULL;
65         r_shadow_blankbumptexture = NULL;
66         r_shadow_blankglosstexture = NULL;
67         r_shadow_blankwhitetexture = NULL;
68         r_shadow_texturepool = NULL;
69         R_Shadow_ClearWorldLights();
70         r_shadow_reloadlights = true;
71 }
72
73 void r_shadow_shutdown(void)
74 {
75         R_Shadow_ClearWorldLights();
76         r_shadow_reloadlights = true;
77         r_shadow_normalscubetexture = NULL;
78         r_shadow_attenuation2dtexture = NULL;
79         r_shadow_blankbumptexture = NULL;
80         r_shadow_blankglosstexture = NULL;
81         r_shadow_blankwhitetexture = NULL;
82         R_FreeTexturePool(&r_shadow_texturepool);
83         maxshadowelements = 0;
84         shadowelements = NULL;
85         maxtrianglefacinglight = 0;
86         trianglefacinglight = NULL;
87         Mem_FreePool(&r_shadow_mempool);
88 }
89
90 void r_shadow_newmap(void)
91 {
92         R_Shadow_ClearWorldLights();
93         r_shadow_reloadlights = true;
94 }
95
96 void R_Shadow_Init(void)
97 {
98         Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
99         Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
100         Cvar_RegisterVariable(&r_shadow_lightintensityscale);
101         Cvar_RegisterVariable(&r_shadow_realtime);
102         Cvar_RegisterVariable(&r_shadow_gloss);
103         Cvar_RegisterVariable(&r_shadow_debuglight);
104         Cvar_RegisterVariable(&r_shadow_scissor);
105         Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
106         Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
107         Cvar_RegisterVariable(&r_shadow_shadownudge);
108         Cvar_RegisterVariable(&r_shadow_portallight);
109         R_Shadow_EditLights_Init();
110         R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
111 }
112
113 void R_Shadow_ProjectVertices(float *verts, int numverts, const float *relativelightorigin, float projectdistance)
114 {
115         int i;
116         float *in, *out, diff[4];
117         in = verts;
118         out = verts + numverts * 4;
119         for (i = 0;i < numverts;i++, in += 4, out += 4)
120         {
121                 VectorSubtract(in, relativelightorigin, diff);
122                 VectorNormalizeFast(diff);
123                 VectorMA(in, projectdistance, diff, out);
124                 VectorMA(in, r_shadow_shadownudge.value, diff, in);
125         }
126 }
127
128 void R_Shadow_MakeTriangleShadowFlags(const int *elements, const float *vertex, int numtris, qbyte *trianglefacinglight, const float *relativelightorigin, float lightradius)
129 {
130         int i;
131         const float *v0, *v1, *v2;
132         for (i = 0;i < numtris;i++, elements += 3)
133         {
134                 // calculate triangle facing flag
135                 v0 = vertex + elements[0] * 4;
136                 v1 = vertex + elements[1] * 4;
137                 v2 = vertex + elements[2] * 4;
138                 // we do not need to normalize the surface normal because both sides
139                 // of the comparison use it, therefore they are both multiplied the
140                 // same amount...  furthermore the subtract can be done on the
141                 // vectors, saving a little bit of math in the dotproducts
142 #if 1
143                 // fast version
144                 // subtracts v1 from v0 and v2, combined into a crossproduct,
145                 // combined with a dotproduct of the light location relative to the
146                 // first point of the triangle (any point works, since the triangle
147                 // is obviously flat), and finally a comparison to determine if the
148                 // light is infront of the triangle (the goal of this statement)
149                 trianglefacinglight[i] =
150                    (relativelightorigin[0] - v0[0]) * ((v0[1] - v1[1]) * (v2[2] - v1[2]) - (v0[2] - v1[2]) * (v2[1] - v1[1]))
151                  + (relativelightorigin[1] - v0[1]) * ((v0[2] - v1[2]) * (v2[0] - v1[0]) - (v0[0] - v1[0]) * (v2[2] - v1[2]))
152                  + (relativelightorigin[2] - v0[2]) * ((v0[0] - v1[0]) * (v2[1] - v1[1]) - (v0[1] - v1[1]) * (v2[0] - v1[0])) > 0;
153 #else
154                 // readable version
155                 {
156                 float dir0[3], dir1[3], temp[3];
157
158                 // calculate two mostly perpendicular edge directions
159                 VectorSubtract(v0, v1, dir0);
160                 VectorSubtract(v2, v1, dir1);
161
162                 // we have two edge directions, we can calculate a third vector from
163                 // them, which is the direction of the surface normal (it's magnitude
164                 // is not 1 however)
165                 CrossProduct(dir0, dir1, temp);
166
167                 // this is entirely unnecessary, but kept for clarity
168                 //VectorNormalize(temp);
169
170                 // compare distance of light along normal, with distance of any point
171                 // of the triangle along the same normal (the triangle is planar,
172                 // I.E. flat, so all points give the same answer)
173                 // the normal is not normalized because it is used on both sides of
174                 // the comparison, so it's magnitude does not matter
175                 trianglefacinglight[i] = DotProduct(relativelightorigin, temp) >= DotProduct(v0, temp);
176                 }
177 #endif
178         }
179 }
180
181 int R_Shadow_BuildShadowVolumeTriangles(const int *elements, const int *neighbors, int numtris, int numverts, const qbyte *trianglefacinglight, int *out)
182 {
183         int i, tris;
184         // check each frontface for bordering backfaces,
185         // and cast shadow polygons from those edges,
186         // also create front and back caps for shadow volume
187         tris = 0;
188         for (i = 0;i < numtris;i++, elements += 3, neighbors += 3)
189         {
190                 if (trianglefacinglight[i])
191                 {
192                         // triangle is frontface and therefore casts shadow,
193                         // output front and back caps for shadow volume
194                         // front cap
195                         out[0] = elements[0];
196                         out[1] = elements[1];
197                         out[2] = elements[2];
198                         // rear cap (with flipped winding order)
199                         out[3] = elements[0] + numverts;
200                         out[4] = elements[2] + numverts;
201                         out[5] = elements[1] + numverts;
202                         out += 6;
203                         tris += 2;
204                         // check the edges
205                         if (neighbors[0] < 0 || !trianglefacinglight[neighbors[0]])
206                         {
207                                 out[0] = elements[1];
208                                 out[1] = elements[0];
209                                 out[2] = elements[0] + numverts;
210                                 out[3] = elements[1];
211                                 out[4] = elements[0] + numverts;
212                                 out[5] = elements[1] + numverts;
213                                 out += 6;
214                                 tris += 2;
215                         }
216                         if (neighbors[1] < 0 || !trianglefacinglight[neighbors[1]])
217                         {
218                                 out[0] = elements[2];
219                                 out[1] = elements[1];
220                                 out[2] = elements[1] + numverts;
221                                 out[3] = elements[2];
222                                 out[4] = elements[1] + numverts;
223                                 out[5] = elements[2] + numverts;
224                                 out += 6;
225                                 tris += 2;
226                         }
227                         if (neighbors[2] < 0 || !trianglefacinglight[neighbors[2]])
228                         {
229                                 out[0] = elements[0];
230                                 out[1] = elements[2];
231                                 out[2] = elements[2] + numverts;
232                                 out[3] = elements[0];
233                                 out[4] = elements[2] + numverts;
234                                 out[5] = elements[0] + numverts;
235                                 out += 6;
236                                 tris += 2;
237                         }
238                 }
239         }
240         return tris;
241 }
242
243 void R_Shadow_ResizeTriangleFacingLight(int numtris)
244 {
245         // make sure trianglefacinglight is big enough for this volume
246         if (maxtrianglefacinglight < numtris)
247         {
248                 maxtrianglefacinglight = numtris;
249                 if (trianglefacinglight)
250                         Mem_Free(trianglefacinglight);
251                 trianglefacinglight = Mem_Alloc(r_shadow_mempool, maxtrianglefacinglight);
252         }
253 }
254
255 void R_Shadow_ResizeShadowElements(int numtris)
256 {
257         // make sure shadowelements is big enough for this volume
258         if (maxshadowelements < numtris * 24)
259         {
260                 maxshadowelements = numtris * 24;
261                 if (shadowelements)
262                         Mem_Free(shadowelements);
263                 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
264         }
265 }
266
267 void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance)
268 {
269         int tris;
270         if (projectdistance < 0.1)
271         {
272                 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
273                 return;
274         }
275 // terminology:
276 //
277 // frontface:
278 // a triangle facing the light source
279 //
280 // backface:
281 // a triangle not facing the light source
282 //
283 // shadow volume:
284 // an extrusion of the frontfaces, beginning at the original geometry and
285 // ending further from the light source than the original geometry
286 // (presumably at least as far as the light's radius, if the light has a
287 // radius at all), capped at both front and back to avoid any problems
288 //
289 // description:
290 // draws the shadow volumes of the model.
291 // requirements:
292 // vertex locations must already be in varray_vertex before use.
293 // varray_vertex must have capacity for numverts * 2.
294
295         // make sure trianglefacinglight is big enough for this volume
296         if (maxtrianglefacinglight < numtris)
297                 R_Shadow_ResizeTriangleFacingLight(numtris);
298
299         // make sure shadowelements is big enough for this volume
300         if (maxshadowelements < numtris * 24)
301                 R_Shadow_ResizeShadowElements(numtris);
302
303         // check which triangles are facing the light
304         R_Shadow_MakeTriangleShadowFlags(elements, varray_vertex, numtris, trianglefacinglight, relativelightorigin, lightradius);
305
306         // generate projected vertices
307         // by clever use of elements we'll construct the whole shadow from
308         // the unprojected vertices and these projected vertices
309         R_Shadow_ProjectVertices(varray_vertex, numverts, relativelightorigin, projectdistance);
310
311         // output triangle elements
312         tris = R_Shadow_BuildShadowVolumeTriangles(elements, neighbors, numtris, numverts, trianglefacinglight, shadowelements);
313         R_Shadow_RenderVolume(numverts * 2, tris, shadowelements);
314 }
315
316 void R_Shadow_RenderVolume(int numverts, int numtris, int *elements)
317 {
318         if (!numverts || !numtris)
319                 return;
320         if (r_shadowstage == SHADOWSTAGE_STENCIL)
321         {
322                 // increment stencil if backface is behind depthbuffer
323                 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
324                 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
325                 R_Mesh_Draw(numverts, numtris, elements);
326                 c_rt_shadowmeshes++;
327                 c_rt_shadowtris += numtris;
328                 // decrement stencil if frontface is behind depthbuffer
329                 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
330                 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
331         }
332         R_Mesh_Draw(numverts, numtris, elements);
333         c_rt_shadowmeshes++;
334         c_rt_shadowtris += numtris;
335 }
336
337 void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
338 {
339         shadowmesh_t *mesh;
340         if (r_shadowstage == SHADOWSTAGE_STENCIL)
341         {
342                 // increment stencil if backface is behind depthbuffer
343                 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
344                 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
345                 for (mesh = firstmesh;mesh;mesh = mesh->next)
346                 {
347                         R_Mesh_ResizeCheck(mesh->numverts);
348                         memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
349                         R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
350                         c_rtcached_shadowmeshes++;
351                         c_rtcached_shadowtris += mesh->numtriangles;
352                 }
353                 // decrement stencil if frontface is behind depthbuffer
354                 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
355                 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
356         }
357         for (mesh = firstmesh;mesh;mesh = mesh->next)
358         {
359                 R_Mesh_ResizeCheck(mesh->numverts);
360                 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
361                 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
362                 c_rtcached_shadowmeshes++;
363                 c_rtcached_shadowtris += mesh->numtriangles;
364         }
365 }
366
367 float r_shadow_attenpower, r_shadow_attenscale;
368 static void R_Shadow_MakeTextures(void)
369 {
370         int x, y, d, side;
371         float v[3], s, t, intensity;
372         qbyte *data;
373         R_FreeTexturePool(&r_shadow_texturepool);
374         r_shadow_texturepool = R_AllocTexturePool();
375         r_shadow_attenpower = r_shadow_lightattenuationpower.value;
376         r_shadow_attenscale = r_shadow_lightattenuationscale.value;
377         data = Mem_Alloc(tempmempool, 6*128*128*4);
378         data[0] = 128;
379         data[1] = 128;
380         data[2] = 255;
381         data[3] = 255;
382         r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
383         data[0] = 64;
384         data[1] = 64;
385         data[2] = 64;
386         data[3] = 255;
387         r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
388         data[0] = 255;
389         data[1] = 255;
390         data[2] = 255;
391         data[3] = 255;
392         r_shadow_blankwhitetexture = R_LoadTexture2D(r_shadow_texturepool, "blankwhite", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
393         for (side = 0;side < 6;side++)
394         {
395                 for (y = 0;y < 128;y++)
396                 {
397                         for (x = 0;x < 128;x++)
398                         {
399                                 s = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
400                                 t = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
401                                 switch(side)
402                                 {
403                                 case 0:
404                                         v[0] = 1;
405                                         v[1] = -t;
406                                         v[2] = -s;
407                                         break;
408                                 case 1:
409                                         v[0] = -1;
410                                         v[1] = -t;
411                                         v[2] = s;
412                                         break;
413                                 case 2:
414                                         v[0] = s;
415                                         v[1] = 1;
416                                         v[2] = t;
417                                         break;
418                                 case 3:
419                                         v[0] = s;
420                                         v[1] = -1;
421                                         v[2] = -t;
422                                         break;
423                                 case 4:
424                                         v[0] = s;
425                                         v[1] = -t;
426                                         v[2] = 1;
427                                         break;
428                                 case 5:
429                                         v[0] = -s;
430                                         v[1] = -t;
431                                         v[2] = -1;
432                                         break;
433                                 }
434                                 intensity = 127.0f / sqrt(DotProduct(v, v));
435                                 data[((side*128+y)*128+x)*4+0] = 128.0f + intensity * v[0];
436                                 data[((side*128+y)*128+x)*4+1] = 128.0f + intensity * v[1];
437                                 data[((side*128+y)*128+x)*4+2] = 128.0f + intensity * v[2];
438                                 data[((side*128+y)*128+x)*4+3] = 255;
439                         }
440                 }
441         }
442         r_shadow_normalscubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalscube", 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
443         for (y = 0;y < 128;y++)
444         {
445                 for (x = 0;x < 128;x++)
446                 {
447                         v[0] = (x + 0.5f) * (2.0f / (128.0f - 8.0f)) - 1.0f;
448                         v[1] = (y + 0.5f) * (2.0f / (128.0f - 8.0f)) - 1.0f;
449                         v[2] = 0;
450                         intensity = 1.0f - sqrt(DotProduct(v, v));
451                         if (intensity > 0)
452                                 intensity = pow(intensity, r_shadow_attenpower);
453                         intensity = bound(0, intensity * r_shadow_attenscale * 256.0f, 255.0f);
454                         d = bound(0, intensity, 255);
455                         data[((0*128+y)*128+x)*4+0] = d;
456                         data[((0*128+y)*128+x)*4+1] = d;
457                         data[((0*128+y)*128+x)*4+2] = d;
458                         data[((0*128+y)*128+x)*4+3] = d;
459                 }
460         }
461         r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", 128, 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
462         Mem_Free(data);
463 }
464
465 void R_Shadow_Stage_Begin(void)
466 {
467         rmeshstate_t m;
468
469         //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
470         if (!r_shadow_attenuation2dtexture
471          || r_shadow_lightattenuationpower.value != r_shadow_attenpower
472          || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
473                 R_Shadow_MakeTextures();
474         if (r_shadow_reloadlights && cl.worldmodel)
475         {
476                 R_Shadow_ClearWorldLights();
477                 r_shadow_reloadlights = false;
478                 R_Shadow_LoadWorldLights();
479                 if (r_shadow_worldlightchain == NULL)
480                 {
481                         R_Shadow_LoadLightsFile();
482                         if (r_shadow_worldlightchain == NULL)
483                                 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
484                 }
485         }
486
487         memset(&m, 0, sizeof(m));
488         m.blendfunc1 = GL_ONE;
489         m.blendfunc2 = GL_ZERO;
490         R_Mesh_State(&m);
491         GL_Color(0, 0, 0, 1);
492         r_shadowstage = SHADOWSTAGE_NONE;
493
494         c_rt_lights = c_rt_clears = c_rt_scissored = 0;
495         c_rt_shadowmeshes = c_rt_shadowtris = c_rt_lightmeshes = c_rt_lighttris = 0;
496         c_rtcached_shadowmeshes = c_rtcached_shadowtris = 0;
497 }
498
499 void R_Shadow_Stage_ShadowVolumes(void)
500 {
501         rmeshstate_t m;
502         memset(&m, 0, sizeof(m));
503         R_Mesh_TextureState(&m);
504         GL_Color(1, 1, 1, 1);
505         qglColorMask(0, 0, 0, 0);
506         qglDisable(GL_BLEND);
507         qglDepthMask(0);
508         qglDepthFunc(GL_LESS);
509         qglEnable(GL_STENCIL_TEST);
510         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
511         qglStencilFunc(GL_ALWAYS, 128, 0xFF);
512         qglEnable(GL_CULL_FACE);
513         qglEnable(GL_DEPTH_TEST);
514         r_shadowstage = SHADOWSTAGE_STENCIL;
515         qglClear(GL_STENCIL_BUFFER_BIT);
516         c_rt_clears++;
517         // LordHavoc note: many shadow volumes reside entirely inside the world
518         // (that is to say they are entirely bounded by their lit surfaces),
519         // which can be optimized by handling things as an inverted light volume,
520         // with the shadow boundaries of the world being simulated by an altered
521         // (129) bias to stencil clearing on such lights
522         // FIXME: generate inverted light volumes for use as shadow volumes and
523         // optimize for them as noted above
524 }
525
526 void R_Shadow_Stage_LightWithoutShadows(void)
527 {
528         rmeshstate_t m;
529         memset(&m, 0, sizeof(m));
530         R_Mesh_TextureState(&m);
531         qglActiveTexture(GL_TEXTURE0_ARB);
532
533         qglEnable(GL_BLEND);
534         qglBlendFunc(GL_ONE, GL_ONE);
535         GL_Color(1, 1, 1, 1);
536         qglColorMask(1, 1, 1, 1);
537         qglDepthMask(0);
538         qglDepthFunc(GL_EQUAL);
539         qglDisable(GL_STENCIL_TEST);
540         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
541         qglStencilFunc(GL_EQUAL, 128, 0xFF);
542         qglEnable(GL_CULL_FACE);
543         qglEnable(GL_DEPTH_TEST);
544         r_shadowstage = SHADOWSTAGE_LIGHT;
545         c_rt_lights++;
546 }
547
548 void R_Shadow_Stage_LightWithShadows(void)
549 {
550         rmeshstate_t m;
551         memset(&m, 0, sizeof(m));
552         R_Mesh_TextureState(&m);
553         qglActiveTexture(GL_TEXTURE0_ARB);
554
555         qglEnable(GL_BLEND);
556         qglBlendFunc(GL_ONE, GL_ONE);
557         GL_Color(1, 1, 1, 1);
558         qglColorMask(1, 1, 1, 1);
559         qglDepthMask(0);
560         qglDepthFunc(GL_EQUAL);
561         qglEnable(GL_STENCIL_TEST);
562         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
563         // only draw light where this geometry was already rendered AND the
564         // stencil is 128 (values other than this mean shadow)
565         qglStencilFunc(GL_EQUAL, 128, 0xFF);
566         qglEnable(GL_CULL_FACE);
567         qglEnable(GL_DEPTH_TEST);
568         r_shadowstage = SHADOWSTAGE_LIGHT;
569         c_rt_lights++;
570 }
571
572 void R_Shadow_Stage_End(void)
573 {
574         rmeshstate_t m;
575         // attempt to restore state to what Mesh_State thinks it is
576         qglDisable(GL_BLEND);
577         qglBlendFunc(GL_ONE, GL_ZERO);
578         qglDepthMask(1);
579         // now restore the rest of the state to normal
580         GL_Color(1, 1, 1, 1);
581         qglColorMask(1, 1, 1, 1);
582         qglDisable(GL_SCISSOR_TEST);
583         qglDepthFunc(GL_LEQUAL);
584         qglDisable(GL_STENCIL_TEST);
585         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
586         qglStencilFunc(GL_ALWAYS, 128, 0xFF);
587         qglEnable(GL_CULL_FACE);
588         qglEnable(GL_DEPTH_TEST);
589         // force mesh state to reset by using various combinations of features
590         memset(&m, 0, sizeof(m));
591         m.blendfunc1 = GL_SRC_ALPHA;
592         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
593         R_Mesh_State(&m);
594         m.blendfunc1 = GL_ONE;
595         m.blendfunc2 = GL_ZERO;
596         R_Mesh_State(&m);
597         r_shadowstage = SHADOWSTAGE_NONE;
598 }
599
600 int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const float *origin, float radius)
601 {
602         int i, ix1, iy1, ix2, iy2;
603         float x1, y1, x2, y2, x, y;
604         vec3_t smins, smaxs;
605         vec4_t v, v2;
606         if (!r_shadow_scissor.integer)
607                 return false;
608         // if view is inside the box, just say yes it's visible
609         if (r_origin[0] >= mins[0] && r_origin[0] <= maxs[0]
610          && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1]
611          && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2])
612         {
613                 qglDisable(GL_SCISSOR_TEST);
614                 return false;
615         }
616         VectorSubtract(r_origin, origin, v);
617         if (DotProduct(v, v) < radius * radius)
618         {
619                 qglDisable(GL_SCISSOR_TEST);
620                 return false;
621         }
622         // create viewspace bbox
623         for (i = 0;i < 8;i++)
624         {
625                 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
626                 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
627                 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
628                 v2[0] = DotProduct(v, vright);
629                 v2[1] = DotProduct(v, vup);
630                 v2[2] = DotProduct(v, vpn);
631                 if (i)
632                 {
633                         if (smins[0] > v2[0]) smins[0] = v2[0];
634                         if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
635                         if (smins[1] > v2[1]) smins[1] = v2[1];
636                         if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
637                         if (smins[2] > v2[2]) smins[2] = v2[2];
638                         if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
639                 }
640                 else
641                 {
642                         smins[0] = smaxs[0] = v2[0];
643                         smins[1] = smaxs[1] = v2[1];
644                         smins[2] = smaxs[2] = v2[2];
645                 }
646         }
647         // now we have a bbox in viewspace
648         // clip it to the viewspace version of the sphere
649         v[0] = origin[0] - r_origin[0];
650         v[1] = origin[1] - r_origin[1];
651         v[2] = origin[2] - r_origin[2];
652         v2[0] = DotProduct(v, vright);
653         v2[1] = DotProduct(v, vup);
654         v2[2] = DotProduct(v, vpn);
655         if (smins[0] < v2[0] - radius) smins[0] = v2[0] - radius;
656         if (smaxs[0] < v2[0] - radius) smaxs[0] = v2[0] + radius;
657         if (smins[1] < v2[1] - radius) smins[1] = v2[1] - radius;
658         if (smaxs[1] < v2[1] - radius) smaxs[1] = v2[1] + radius;
659         if (smins[2] < v2[2] - radius) smins[2] = v2[2] - radius;
660         if (smaxs[2] < v2[2] - radius) smaxs[2] = v2[2] + radius;
661         // clip it to the view plane
662         if (smins[2] < 1)
663                 smins[2] = 1;
664         // return true if that culled the box
665         if (smins[2] >= smaxs[2])
666                 return true;
667         // ok some of it is infront of the view, transform each corner back to
668         // worldspace and then to screenspace and make screen rect
669         // initialize these variables just to avoid compiler warnings
670         x1 = y1 = x2 = y2 = 0;
671         for (i = 0;i < 8;i++)
672         {
673                 v2[0] = (i & 1) ? smins[0] : smaxs[0];
674                 v2[1] = (i & 2) ? smins[1] : smaxs[1];
675                 v2[2] = (i & 4) ? smins[2] : smaxs[2];
676                 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
677                 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
678                 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
679                 v[3] = 1.0f;
680                 GL_TransformToScreen(v, v2);
681                 //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
682                 x = v2[0];
683                 y = v2[1];
684                 if (i)
685                 {
686                         if (x1 > x) x1 = x;
687                         if (x2 < x) x2 = x;
688                         if (y1 > y) y1 = y;
689                         if (y2 < y) y2 = y;
690                 }
691                 else
692                 {
693                         x1 = x2 = x;
694                         y1 = y2 = y;
695                 }
696         }
697         /*
698         // this code doesn't handle boxes with any points behind view properly
699         x1 = 1000;x2 = -1000;
700         y1 = 1000;y2 = -1000;
701         for (i = 0;i < 8;i++)
702         {
703                 v[0] = (i & 1) ? mins[0] : maxs[0];
704                 v[1] = (i & 2) ? mins[1] : maxs[1];
705                 v[2] = (i & 4) ? mins[2] : maxs[2];
706                 v[3] = 1.0f;
707                 GL_TransformToScreen(v, v2);
708                 //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
709                 if (v2[2] > 0)
710                 {
711                         x = v2[0];
712                         y = v2[1];
713
714                         if (x1 > x) x1 = x;
715                         if (x2 < x) x2 = x;
716                         if (y1 > y) y1 = y;
717                         if (y2 < y) y2 = y;
718                 }
719         }
720         */
721         ix1 = x1 - 1.0f;
722         iy1 = y1 - 1.0f;
723         ix2 = x2 + 1.0f;
724         iy2 = y2 + 1.0f;
725         //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
726         if (ix1 < r_refdef.x) ix1 = r_refdef.x;
727         if (iy1 < r_refdef.y) iy1 = r_refdef.y;
728         if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
729         if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
730         if (ix2 <= ix1 || iy2 <= iy1)
731                 return true;
732         // set up the scissor rectangle
733         qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
734         qglEnable(GL_SCISSOR_TEST);
735         c_rt_scissored++;
736         return false;
737 }
738
739 void R_Shadow_GenTexCoords_Attenuation2D1D(float *out2d, float *out1d, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, float lightradius)
740 {
741         int i;
742         float lightvec[3], iradius;
743         iradius = 0.5f / lightradius;
744         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out2d += 4, out1d += 4)
745         {
746                 VectorSubtract(vertex, relativelightorigin, lightvec);
747                 out2d[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
748                 out2d[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
749                 out2d[2] = 0;
750                 out1d[0] = 0.5f + DotProduct(normals, lightvec) * iradius;
751                 out1d[1] = 0.5f;
752                 out1d[2] = 0;
753         }
754 }
755
756 void R_Shadow_GenTexCoords_Diffuse_Attenuation3D(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, float lightradius)
757 {
758         int i;
759         float lightvec[3], iradius;
760         iradius = 0.5f / lightradius;
761         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
762         {
763                 VectorSubtract(vertex, relativelightorigin, lightvec);
764                 out[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
765                 out[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
766                 out[2] = 0.5f + DotProduct(normals, lightvec) * iradius;
767         }
768 }
769
770 void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin)
771 {
772         int i;
773         float lightdir[3];
774         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
775         {
776                 VectorSubtract(vertex, relativelightorigin, lightdir);
777                 // the cubemap normalizes this for us
778                 out[0] = DotProduct(svectors, lightdir);
779                 out[1] = DotProduct(tvectors, lightdir);
780                 out[2] = DotProduct(normals, lightdir);
781         }
782 }
783
784 void R_Shadow_GenTexCoords_Specular_Attenuation3D(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, const vec3_t relativeeyeorigin, float lightradius)
785 {
786         int i;
787         float lightdir[3], eyedir[3], halfdir[3], lightdirlen, iradius;
788         iradius = 0.5f / lightradius;
789         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
790         {
791                 VectorSubtract(vertex, relativelightorigin, lightdir);
792                 // this is used later to make the attenuation correct
793                 lightdirlen = sqrt(DotProduct(lightdir, lightdir)) * iradius;
794                 VectorNormalizeFast(lightdir);
795                 VectorSubtract(vertex, relativeeyeorigin, eyedir);
796                 VectorNormalizeFast(eyedir);
797                 VectorAdd(lightdir, eyedir, halfdir);
798                 VectorNormalizeFast(halfdir);
799                 out[0] = 0.5f + DotProduct(svectors, halfdir) * lightdirlen;
800                 out[1] = 0.5f + DotProduct(tvectors, halfdir) * lightdirlen;
801                 out[2] = 0.5f + DotProduct(normals, halfdir) * lightdirlen;
802         }
803 }
804
805 void R_Shadow_GenTexCoords_Specular_NormalCubeMap(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, const vec3_t relativeeyeorigin)
806 {
807         int i;
808         float lightdir[3], eyedir[3], halfdir[3];
809         for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
810         {
811                 VectorSubtract(vertex, relativelightorigin, lightdir);
812                 VectorNormalizeFast(lightdir);
813                 VectorSubtract(vertex, relativeeyeorigin, eyedir);
814                 VectorNormalizeFast(eyedir);
815                 VectorAdd(lightdir, eyedir, halfdir);
816                 // the cubemap normalizes this for us
817                 out[0] = DotProduct(svectors, halfdir);
818                 out[1] = DotProduct(tvectors, halfdir);
819                 out[2] = DotProduct(normals, halfdir);
820         }
821 }
822
823 void R_Shadow_GenTexCoords_LightCubeMap(float *out, int numverts, const float *vertex, const vec3_t relativelightorigin)
824 {
825         int i;
826         // FIXME: this needs to be written
827         // this code assumes the vertices are in worldspace (a false assumption)
828         for (i = 0;i < numverts;i++, vertex += 4, out += 4)
829                 VectorSubtract(vertex, relativelightorigin, out);
830 }
831
832 void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *elements, const float *svectors, const float *tvectors, const float *normals, const float *texcoords, const float *relativelightorigin, float lightradius, const float *lightcolor, rtexture_t *basetexture, rtexture_t *bumptexture, rtexture_t *lightcubemap)
833 {
834         int renders;
835         float color[3];
836         rmeshstate_t m;
837         memset(&m, 0, sizeof(m));
838         if (!bumptexture)
839                 bumptexture = r_shadow_blankbumptexture;
840         // colorscale accounts for how much we multiply the brightness during combine
841         // mult is how many times the final pass of the lighting will be
842         // performed to get more brightness than otherwise possible
843         // limit mult to 64 for sanity sake
844         if (r_textureunits.integer >= 4)
845         {
846                 // 4 texture no3D combine path, two pass
847                 m.tex[0] = R_GetTexture(bumptexture);
848                 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
849                 m.texcombinergb[0] = GL_REPLACE;
850                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
851                 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
852                 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
853                 R_Mesh_TextureState(&m);
854                 qglColorMask(0,0,0,1);
855                 qglDisable(GL_BLEND);
856                 GL_Color(1,1,1,1);
857                 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
858                 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
859                 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[2], varray_texcoord[3], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
860                 R_Mesh_Draw(numverts, numtriangles, elements);
861                 c_rt_lightmeshes++;
862                 c_rt_lighttris += numtriangles;
863
864                 m.tex[0] = R_GetTexture(basetexture);
865                 m.texcubemap[1] = R_GetTexture(lightcubemap);
866                 m.texcombinergb[0] = GL_MODULATE;
867                 m.texcombinergb[1] = GL_MODULATE;
868                 m.tex[2] = 0;
869                 m.tex[3] = 0;
870                 R_Mesh_TextureState(&m);
871                 qglColorMask(1,1,1,0);
872                 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
873                 qglEnable(GL_BLEND);
874                 if (lightcubemap)
875                         R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
876
877                 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
878                 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
879                 {
880                         GL_Color(color[0], color[1], color[2], 1);
881                         R_Mesh_Draw(numverts, numtriangles, elements);
882                         c_rt_lightmeshes++;
883                         c_rt_lighttris += numtriangles;
884                 }
885         }
886         else
887         {
888                 // 2 texture no3D combine path, three pass
889                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
890                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
891                 R_Mesh_TextureState(&m);
892                 qglColorMask(0,0,0,1);
893                 qglDisable(GL_BLEND);
894                 GL_Color(1,1,1,1);
895                 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
896                 R_Mesh_Draw(numverts, numtriangles, elements);
897                 c_rt_lightmeshes++;
898                 c_rt_lighttris += numtriangles;
899
900                 m.tex[0] = R_GetTexture(bumptexture);
901                 m.tex[1] = 0;
902                 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
903                 m.texcombinergb[0] = GL_REPLACE;
904                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
905                 R_Mesh_TextureState(&m);
906                 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
907                 qglEnable(GL_BLEND);
908                 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
909                 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
910                 R_Mesh_Draw(numverts, numtriangles, elements);
911                 c_rt_lightmeshes++;
912                 c_rt_lighttris += numtriangles;
913
914                 m.tex[0] = R_GetTexture(basetexture);
915                 m.texcubemap[1] = R_GetTexture(lightcubemap);
916                 m.texcombinergb[0] = GL_MODULATE;
917                 m.texcombinergb[1] = GL_MODULATE;
918                 R_Mesh_TextureState(&m);
919                 qglColorMask(1,1,1,0);
920                 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
921                 if (lightcubemap)
922                         R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
923
924                 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
925                 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
926                 {
927                         GL_Color(color[0], color[1], color[2], 1);
928                         R_Mesh_Draw(numverts, numtriangles, elements);
929                         c_rt_lightmeshes++;
930                         c_rt_lighttris += numtriangles;
931                 }
932         }
933 }
934
935 void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elements, const float *svectors, const float *tvectors, const float *normals, const float *texcoords, const float *relativelightorigin, const float *relativeeyeorigin, float lightradius, const float *lightcolor, rtexture_t *glosstexture, rtexture_t *bumptexture, rtexture_t *lightcubemap)
936 {
937         int renders;
938         float color[3];
939         rmeshstate_t m;
940         memset(&m, 0, sizeof(m));
941         if (!bumptexture)
942                 bumptexture = r_shadow_blankbumptexture;
943         if (!glosstexture)
944                 glosstexture = r_shadow_blankglosstexture;
945         if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
946         {
947                 // 2 texture no3D combine path, five pass
948                 memset(&m, 0, sizeof(m));
949
950                 m.tex[0] = R_GetTexture(bumptexture);
951                 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
952                 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
953                 R_Mesh_TextureState(&m);
954                 qglColorMask(0,0,0,1);
955                 qglDisable(GL_BLEND);
956                 GL_Color(1,1,1,1);
957                 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
958                 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin);
959                 R_Mesh_Draw(numverts, numtriangles, elements);
960                 c_rt_lightmeshes++;
961                 c_rt_lighttris += numtriangles;
962
963                 m.tex[0] = 0;
964                 m.texcubemap[1] = 0;
965                 m.texcombinergb[1] = GL_MODULATE;
966                 R_Mesh_TextureState(&m);
967                 // square alpha in framebuffer a few times to make it shiny
968                 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
969                 qglEnable(GL_BLEND);
970                 // these comments are a test run through this math for intensity 0.5
971                 // 0.5 * 0.5 = 0.25
972                 R_Mesh_Draw(numverts, numtriangles, elements);
973                 c_rt_lightmeshes++;
974                 c_rt_lighttris += numtriangles;
975                 // 0.25 * 0.25 = 0.0625
976                 R_Mesh_Draw(numverts, numtriangles, elements);
977                 c_rt_lightmeshes++;
978                 c_rt_lighttris += numtriangles;
979                 // 0.0625 * 0.0625 = 0.00390625
980                 R_Mesh_Draw(numverts, numtriangles, elements);
981                 c_rt_lightmeshes++;
982                 c_rt_lighttris += numtriangles;
983
984                 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
985                 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
986                 R_Mesh_TextureState(&m);
987                 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
988                 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
989                 R_Mesh_Draw(numverts, numtriangles, elements);
990                 c_rt_lightmeshes++;
991                 c_rt_lighttris += numtriangles;
992
993                 m.tex[0] = R_GetTexture(glosstexture);
994                 m.texcubemap[1] = R_GetTexture(lightcubemap);
995                 R_Mesh_TextureState(&m);
996                 qglColorMask(1,1,1,0);
997                 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
998                 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
999                 if (lightcubemap)
1000                         R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
1001
1002                 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1003                 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
1004                 {
1005                         GL_Color(color[0], color[1], color[2], 1);
1006                         R_Mesh_Draw(numverts, numtriangles, elements);
1007                         c_rt_lightmeshes++;
1008                         c_rt_lighttris += numtriangles;
1009                 }
1010         }
1011 }
1012
1013 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
1014 {
1015         R_Mesh_Matrix(matrix);
1016         R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
1017 }
1018
1019 cvar_t r_editlights = {0, "r_editlights", "0"};
1020 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
1021 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
1022 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
1023 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
1024 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
1025 cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
1026 cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
1027 worldlight_t *r_shadow_worldlightchain;
1028 worldlight_t *r_shadow_selectedlight;
1029 vec3_t r_editlights_cursorlocation;
1030
1031 static int castshadowcount = 1;
1032 void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname, int castshadow)
1033 {
1034         int i, j, k, l, maxverts, *mark, tris;
1035         float *verts, *v, f, temp[3], radius2;
1036         //float projectdistance, *v0, *v1, temp2[3], temp3[3];
1037         worldlight_t *e;
1038         shadowmesh_t *mesh, *castmesh;
1039         mleaf_t *leaf;
1040         msurface_t *surf;
1041         qbyte *pvs;
1042         surfmesh_t *surfmesh;
1043
1044         if (radius < 15 || DotProduct(color, color) < 0.03)
1045         {
1046                 Con_Printf("R_Shadow_NewWorldLight: refusing to create a light too small/dim\n");
1047                 return;
1048         }
1049
1050         e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
1051         VectorCopy(origin, e->origin);
1052         VectorCopy(color, e->light);
1053         e->lightradius = radius;
1054         e->style = style;
1055         if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
1056         {
1057                 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
1058                 e->style = 0;
1059         }
1060         e->castshadows = castshadow;
1061
1062         e->cullradius = e->lightradius;
1063         e->mins[0] = e->origin[0] - e->lightradius;
1064         e->maxs[0] = e->origin[0] + e->lightradius;
1065         e->mins[1] = e->origin[1] - e->lightradius;
1066         e->maxs[1] = e->origin[1] + e->lightradius;
1067         e->mins[2] = e->origin[2] - e->lightradius;
1068         e->maxs[2] = e->origin[2] + e->lightradius;
1069
1070         e->next = r_shadow_worldlightchain;
1071         r_shadow_worldlightchain = e;
1072         if (cubemapname && cubemapname[0])
1073         {
1074                 e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
1075                 strcpy(e->cubemapname, cubemapname);
1076                 // FIXME: add cubemap loading (and don't load a cubemap twice)
1077         }
1078         if (cl.worldmodel)
1079         {
1080                 castshadowcount++;
1081                 if (r_shadow_portallight.integer)
1082                 {
1083                         qbyte *byteleafpvs;
1084                         qbyte *bytesurfacepvs;
1085                         byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->numleafs + 1);
1086                         bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->numsurfaces);
1087                         Portal_Visibility(cl.worldmodel, e->origin, byteleafpvs, bytesurfacepvs, NULL, 0, true, e->lightradius);
1088
1089                         for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1090                         {
1091                                 if (byteleafpvs[i+1])
1092                                 {
1093                                         temp[0] = bound(leaf->mins[0], e->origin[0], leaf->maxs[0]) - e->origin[0];
1094                                         temp[1] = bound(leaf->mins[1], e->origin[1], leaf->maxs[1]) - e->origin[1];
1095                                         temp[2] = bound(leaf->mins[2], e->origin[2], leaf->maxs[2]) - e->origin[2];
1096                                         if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1097                                                 leaf->worldnodeframe = castshadowcount;
1098                                 }
1099                         }
1100
1101                         for (i = 0, surf = cl.worldmodel->surfaces;i < cl.worldmodel->numsurfaces;i++, surf++)
1102                         {
1103                                 if (bytesurfacepvs[i])
1104                                 {
1105                                         f = DotProduct(e->origin, surf->plane->normal) - surf->plane->dist;
1106                                         if (surf->flags & SURF_PLANEBACK)
1107                                                 f = -f;
1108                                         if (f > 0 && f < e->lightradius)
1109                                         {
1110                                                 temp[0] = bound(surf->poly_mins[0], e->origin[0], surf->poly_maxs[0]) - e->origin[0];
1111                                                 temp[1] = bound(surf->poly_mins[1], e->origin[1], surf->poly_maxs[1]) - e->origin[1];
1112                                                 temp[2] = bound(surf->poly_mins[2], e->origin[2], surf->poly_maxs[2]) - e->origin[2];
1113                                                 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1114                                                         surf->castshadow = castshadowcount;
1115                                         }
1116                                 }
1117                         }
1118
1119                         Mem_Free(byteleafpvs);
1120                         Mem_Free(bytesurfacepvs);
1121                 }
1122                 else
1123                 {
1124                         leaf = Mod_PointInLeaf(origin, cl.worldmodel);
1125                         pvs = Mod_LeafPVS(leaf, cl.worldmodel);
1126                         for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1127                         {
1128                                 if (pvs[i >> 3] & (1 << (i & 7)))
1129                                 {
1130                                         VectorCopy(origin, temp);
1131                                         if (temp[0] < leaf->mins[0]) temp[0] = leaf->mins[0];
1132                                         if (temp[0] > leaf->maxs[0]) temp[0] = leaf->maxs[0];
1133                                         if (temp[1] < leaf->mins[1]) temp[1] = leaf->mins[1];
1134                                         if (temp[1] > leaf->maxs[1]) temp[1] = leaf->maxs[1];
1135                                         if (temp[2] < leaf->mins[2]) temp[2] = leaf->mins[2];
1136                                         if (temp[2] > leaf->maxs[2]) temp[2] = leaf->maxs[2];
1137                                         VectorSubtract(temp, origin, temp);
1138                                         if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1139                                         {
1140                                                 leaf->worldnodeframe = castshadowcount;
1141                                                 for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
1142                                                 {
1143                                                         surf = cl.worldmodel->surfaces + *mark;
1144                                                         if (surf->castshadow != castshadowcount)
1145                                                         {
1146                                                                 f = DotProduct(e->origin, surf->plane->normal) - surf->plane->dist;
1147                                                                 if (surf->flags & SURF_PLANEBACK)
1148                                                                         f = -f;
1149                                                                 if (f > 0 && f < e->lightradius)
1150                                                                 {
1151                                                                         temp[0] = bound(surf->poly_mins[0], e->origin[0], surf->poly_maxs[0]) - e->origin[0];
1152                                                                         temp[1] = bound(surf->poly_mins[1], e->origin[1], surf->poly_maxs[1]) - e->origin[1];
1153                                                                         temp[2] = bound(surf->poly_mins[2], e->origin[2], surf->poly_maxs[2]) - e->origin[2];
1154                                                                         if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1155                                                                                 surf->castshadow = castshadowcount;
1156                                                                 }
1157                                                         }
1158                                                 }
1159                                         }
1160                                 }
1161                         }
1162                 }
1163
1164                 e->numleafs = 0;
1165                 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1166                         if (leaf->worldnodeframe == castshadowcount)
1167                                 e->numleafs++;
1168                 e->numsurfaces = 0;
1169                 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1170                         if (surf->castshadow == castshadowcount)
1171                                 e->numsurfaces++;
1172
1173                 if (e->numleafs)
1174                         e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
1175                 if (e->numsurfaces)
1176                         e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
1177                 e->numleafs = 0;
1178                 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1179                         if (leaf->worldnodeframe == castshadowcount)
1180                                 e->leafs[e->numleafs++] = leaf;
1181                 e->numsurfaces = 0;
1182                 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1183                         if (surf->castshadow == castshadowcount)
1184                                 e->surfaces[e->numsurfaces++] = surf;
1185                 // find bounding box and sphere of lit surfaces
1186                 // (these will be used for creating a shape to clip the light)
1187                 radius2 = 0;
1188                 VectorCopy(e->origin, e->mins);
1189                 VectorCopy(e->origin, e->maxs);
1190                 for (j = 0;j < e->numsurfaces;j++)
1191                 {
1192                         surf = e->surfaces[j];
1193                         for (k = 0, v = surf->poly_verts;k < surf->poly_numverts;k++, v += 3)
1194                         {
1195                                 if (e->mins[0] > v[0]) e->mins[0] = v[0];if (e->maxs[0] < v[0]) e->maxs[0] = v[0];
1196                                 if (e->mins[1] > v[1]) e->mins[1] = v[1];if (e->maxs[1] < v[1]) e->maxs[1] = v[1];
1197                                 if (e->mins[2] > v[2]) e->mins[2] = v[2];if (e->maxs[2] < v[2]) e->maxs[2] = v[2];
1198                                 VectorSubtract(v, e->origin, temp);
1199                                 f = DotProduct(temp, temp);
1200                                 if (radius2 < f)
1201                                         radius2 = f;
1202                         }
1203                 }
1204                 e->cullradius = sqrt(radius2);
1205                 if (e->cullradius > e->lightradius)
1206                         e->cullradius = e->lightradius;
1207                 if (e->mins[0] < e->origin[0] - e->lightradius) e->mins[0] = e->origin[0] - e->lightradius;
1208                 if (e->maxs[0] > e->origin[0] + e->lightradius) e->maxs[0] = e->origin[0] + e->lightradius;
1209                 if (e->mins[1] < e->origin[1] - e->lightradius) e->mins[1] = e->origin[1] - e->lightradius;
1210                 if (e->maxs[1] > e->origin[1] + e->lightradius) e->maxs[1] = e->origin[1] + e->lightradius;
1211                 if (e->mins[2] < e->origin[2] - e->lightradius) e->mins[2] = e->origin[2] - e->lightradius;
1212                 if (e->maxs[2] > e->origin[2] + e->lightradius) e->maxs[2] = e->origin[2] + e->lightradius;
1213
1214                 if (e->castshadows)
1215                 {
1216                         maxverts = 256;
1217                         verts = NULL;
1218                         castshadowcount++;
1219                         for (j = 0;j < e->numsurfaces;j++)
1220                         {
1221                                 surf = e->surfaces[j];
1222                                 if (surf->flags & SURF_SHADOWCAST)
1223                                 {
1224                                         surf->castshadow = castshadowcount;
1225                                         if (maxverts < surf->poly_numverts)
1226                                                 maxverts = surf->poly_numverts;
1227                                 }
1228                         }
1229                         e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1230                         // make a mesh to cast a shadow volume from
1231                         castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1232                         for (j = 0;j < e->numsurfaces;j++)
1233                                 if (e->surfaces[j]->castshadow == castshadowcount)
1234                                         for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
1235                                                 Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->verts, surfmesh->numtriangles, surfmesh->index);
1236                         castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
1237
1238                         // cast shadow volume from castmesh
1239                         for (mesh = castmesh;mesh;mesh = mesh->next)
1240                         {
1241                                 R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
1242                                 R_Shadow_ResizeShadowElements(castmesh->numtriangles);
1243
1244                                 if (maxverts < castmesh->numverts * 2)
1245                                 {
1246                                         maxverts = castmesh->numverts * 2;
1247                                         if (verts)
1248                                                 Mem_Free(verts);
1249                                         verts = NULL;
1250                                 }
1251                                 if (verts == NULL && maxverts > 0)
1252                                         verts = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[4]));
1253
1254                                 // now that we have the buffers big enough, construct shadow volume mesh
1255                                 memcpy(verts, castmesh->verts, castmesh->numverts * sizeof(float[4]));
1256                                 R_Shadow_ProjectVertices(verts, castmesh->numverts, e->origin, 10000000.0f);//, e->lightradius);
1257                                 R_Shadow_MakeTriangleShadowFlags(castmesh->elements, verts, castmesh->numtriangles, trianglefacinglight, e->origin, e->lightradius);
1258                                 tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->elements, castmesh->neighbors, castmesh->numtriangles, castmesh->numverts, trianglefacinglight, shadowelements);
1259                                 // add the constructed shadow volume mesh
1260                                 Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, verts, tris, shadowelements);
1261                         }
1262                         // we're done with castmesh now
1263                         Mod_ShadowMesh_Free(castmesh);
1264                         e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
1265                         for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
1266                                 l += mesh->numtriangles;
1267                         Con_Printf("static shadow volume built containing %i triangles\n", l);
1268                 }
1269         }
1270         Con_Printf("%f %f %f, %f %f %f, %f, %f, %d, %d\n", e->mins[0], e->mins[1], e->mins[2], e->maxs[0], e->maxs[1], e->maxs[2], e->cullradius, e->lightradius, e->numleafs, e->numsurfaces);
1271 }
1272
1273 void R_Shadow_FreeWorldLight(worldlight_t *light)
1274 {
1275         worldlight_t **lightpointer;
1276         for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
1277         if (*lightpointer != light)
1278                 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
1279         *lightpointer = light->next;
1280         if (light->cubemapname)
1281                 Mem_Free(light->cubemapname);
1282         if (light->shadowvolume)
1283                 Mod_ShadowMesh_Free(light->shadowvolume);
1284         if (light->surfaces)
1285                 Mem_Free(light->surfaces);
1286         if (light->leafs)
1287                 Mem_Free(light->leafs);
1288         Mem_Free(light);
1289 }
1290
1291 void R_Shadow_ClearWorldLights(void)
1292 {
1293         while (r_shadow_worldlightchain)
1294                 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
1295         r_shadow_selectedlight = NULL;
1296 }
1297
1298 void R_Shadow_SelectLight(worldlight_t *light)
1299 {
1300         if (r_shadow_selectedlight)
1301                 r_shadow_selectedlight->selected = false;
1302         r_shadow_selectedlight = light;
1303         if (r_shadow_selectedlight)
1304                 r_shadow_selectedlight->selected = true;
1305 }
1306
1307
1308 void R_DrawLightSprite(int texnum, const vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
1309 {
1310         rmeshstate_t m;
1311         float diff[3];
1312
1313         if (fogenabled)
1314         {
1315                 VectorSubtract(origin, r_origin, diff);
1316                 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
1317         }
1318
1319         memset(&m, 0, sizeof(m));
1320         m.blendfunc1 = GL_SRC_ALPHA;
1321         m.blendfunc2 = GL_ONE;
1322         m.tex[0] = texnum;
1323         R_Mesh_Matrix(&r_identitymatrix);
1324         R_Mesh_State(&m);
1325
1326         GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1327         varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
1328         varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
1329         varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
1330         varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
1331         varray_vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
1332         varray_vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
1333         varray_vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
1334         varray_vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
1335         varray_vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
1336         varray_vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
1337         varray_vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
1338         varray_vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
1339         varray_vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
1340         varray_vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
1341         varray_vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
1342         varray_vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
1343         R_Mesh_Draw(4, 2, polygonelements);
1344 }
1345
1346 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
1347 {
1348         cachepic_t *pic;
1349         pic = Draw_CachePic("gfx/crosshair1.tga");
1350         if (pic)
1351                 R_DrawLightSprite(R_GetTexture(pic->tex), r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 0.5);
1352 }
1353
1354 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
1355 {
1356         float intensity;
1357         const worldlight_t *light;
1358         light = calldata1;
1359         intensity = 0.5;
1360         if (light->selected)
1361                 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
1362         if (light->shadowvolume)
1363                 R_DrawLightSprite(calldata2, light->origin, 8, intensity, intensity, intensity, 0.5);
1364         else
1365                 R_DrawLightSprite(calldata2, light->origin, 8, intensity * 0.5, intensity * 0.5, intensity * 0.5, 0.5);
1366 }
1367
1368 void R_Shadow_DrawLightSprites(void)
1369 {
1370         int i, texnums[5];
1371         cachepic_t *pic;
1372         worldlight_t *light;
1373
1374         for (i = 0;i < 5;i++)
1375         {
1376                 pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1));
1377                 if (pic)
1378                         texnums[i] = R_GetTexture(pic->tex);
1379                 else
1380                         texnums[i] = 0;
1381         }
1382
1383         for (light = r_shadow_worldlightchain;light;light = light->next)
1384                 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, texnums[((int) light) % 5]);
1385         R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
1386 }
1387
1388 void R_Shadow_SelectLightInView(void)
1389 {
1390         float bestrating, rating, temp[3];
1391         worldlight_t *best, *light;
1392         best = NULL;
1393         bestrating = 0;
1394         for (light = r_shadow_worldlightchain;light;light = light->next)
1395         {
1396                 VectorSubtract(light->origin, r_refdef.vieworg, temp);
1397                 rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
1398                 if (rating >= 0.95)
1399                 {
1400                         rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
1401                         if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
1402                         {
1403                                 bestrating = rating;
1404                                 best = light;
1405                         }
1406                 }
1407         }
1408         R_Shadow_SelectLight(best);
1409 }
1410
1411 void R_Shadow_LoadWorldLights(void)
1412 {
1413         int n, a, style, shadow;
1414         char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
1415         float origin[3], radius, color[3];
1416         if (cl.worldmodel == NULL)
1417         {
1418                 Con_Printf("No map loaded.\n");
1419                 return;
1420         }
1421         COM_StripExtension(cl.worldmodel->name, name);
1422         strcat(name, ".rtlights");
1423         lightsstring = COM_LoadFile(name, false);
1424         if (lightsstring)
1425         {
1426                 s = lightsstring;
1427                 n = 0;
1428                 while (*s)
1429                 {
1430                         t = s;
1431                         while (*s && *s != '\n')
1432                                 s++;
1433                         if (!*s)
1434                                 break;
1435                         *s = 0;
1436                         shadow = true;
1437                         // check for modifier flags
1438                         if (*t == '!')
1439                         {
1440                                 shadow = false;
1441                                 t++;
1442                         }
1443                         a = sscanf(t, "%f %f %f %f %f %f %f %d %s", &origin[0], &origin[1], &origin[2], &radius, &color[0], &color[1], &color[2], &style, cubemapname);
1444                         if (a < 9)
1445                                 cubemapname[0] = 0;
1446                         *s = '\n';
1447                         if (a < 8)
1448                         {
1449                                 Con_Printf("found %d parameters on line %i, should be 8 or 9 parameters (origin[0] origin[1] origin[2] radius color[0] color[1] color[2] style cubemapname)\n", a, n + 1);
1450                                 break;
1451                         }
1452                         VectorScale(color, r_editlights_rtlightscolorscale.value, color);
1453                         radius *= r_editlights_rtlightssizescale.value;
1454                         R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadow);
1455                         s++;
1456                         n++;
1457                 }
1458                 if (*s)
1459                         Con_Printf("invalid rtlights file \"%s\"\n", name);
1460                 Mem_Free(lightsstring);
1461         }
1462 }
1463
1464 void R_Shadow_SaveWorldLights(void)
1465 {
1466         worldlight_t *light;
1467         int bufchars, bufmaxchars;
1468         char *buf, *oldbuf;
1469         char name[MAX_QPATH];
1470         char line[1024];
1471         if (!r_shadow_worldlightchain)
1472                 return;
1473         if (cl.worldmodel == NULL)
1474         {
1475                 Con_Printf("No map loaded.\n");
1476                 return;
1477         }
1478         COM_StripExtension(cl.worldmodel->name, name);
1479         strcat(name, ".rtlights");
1480         bufchars = bufmaxchars = 0;
1481         buf = NULL;
1482         for (light = r_shadow_worldlightchain;light;light = light->next)
1483         {
1484                 sprintf(line, "%s%g %g %g %g %g %g %g %d %s\n", light->castshadows ? "" : "!", light->origin[0], light->origin[1], light->origin[2], light->lightradius / r_editlights_rtlightssizescale.value, light->light[0] / r_editlights_rtlightscolorscale.value, light->light[1] / r_editlights_rtlightscolorscale.value, light->light[2] / r_editlights_rtlightscolorscale.value, light->style, light->cubemapname ? light->cubemapname : "");
1485                 if (bufchars + strlen(line) > bufmaxchars)
1486                 {
1487                         bufmaxchars = bufchars + strlen(line) + 2048;
1488                         oldbuf = buf;
1489                         buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
1490                         if (oldbuf)
1491                         {
1492                                 if (bufchars)
1493                                         memcpy(buf, oldbuf, bufchars);
1494                                 Mem_Free(oldbuf);
1495                         }
1496                 }
1497                 if (strlen(line))
1498                 {
1499                         memcpy(buf + bufchars, line, strlen(line));
1500                         bufchars += strlen(line);
1501                 }
1502         }
1503         if (bufchars)
1504                 COM_WriteFile(name, buf, bufchars);
1505         if (buf)
1506                 Mem_Free(buf);
1507 }
1508
1509 void R_Shadow_LoadLightsFile(void)
1510 {
1511         int n, a, style;
1512         char name[MAX_QPATH], *lightsstring, *s, *t;
1513         float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
1514         if (cl.worldmodel == NULL)
1515         {
1516                 Con_Printf("No map loaded.\n");
1517                 return;
1518         }
1519         COM_StripExtension(cl.worldmodel->name, name);
1520         strcat(name, ".lights");
1521         lightsstring = COM_LoadFile(name, false);
1522         if (lightsstring)
1523         {
1524                 s = lightsstring;
1525                 n = 0;
1526                 while (*s)
1527                 {
1528                         t = s;
1529                         while (*s && *s != '\n')
1530                                 s++;
1531                         if (!*s)
1532                                 break;
1533                         *s = 0;
1534                         a = sscanf(t, "%f %f %f %f %f %f %f %f %f %f %f %f %f %d", &origin[0], &origin[1], &origin[2], &falloff, &color[0], &color[1], &color[2], &subtract, &spotdir[0], &spotdir[1], &spotdir[2], &spotcone, &distbias, &style);
1535                         *s = '\n';
1536                         if (a < 14)
1537                         {
1538                                 Con_Printf("invalid lights file, found %d parameters on line %i, should be 14 parameters (origin[0] origin[1] origin[2] falloff light[0] light[1] light[2] subtract spotdir[0] spotdir[1] spotdir[2] spotcone distancebias style)\n", a, n + 1);
1539                                 break;
1540                         }
1541                         radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
1542                         radius = bound(15, radius, 4096);
1543                         VectorScale(color, (2.0f / (8388608.0f)), color);
1544                         R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
1545                         s++;
1546                         n++;
1547                 }
1548                 if (*s)
1549                         Con_Printf("invalid lights file \"%s\"\n", name);
1550                 Mem_Free(lightsstring);
1551         }
1552 }
1553
1554 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
1555 {
1556         int entnum, style, islight;
1557         char key[256], value[1024];
1558         float origin[3], radius, color[3], light, scale, originhack[3], overridecolor[3];
1559         const char *data;
1560
1561         if (cl.worldmodel == NULL)
1562         {
1563                 Con_Printf("No map loaded.\n");
1564                 return;
1565         }
1566         data = cl.worldmodel->entities;
1567         if (!data)
1568                 return;
1569         for (entnum = 0;COM_ParseToken(&data) && com_token[0] == '{';entnum++)
1570         {
1571                 light = 0;
1572                 origin[0] = origin[1] = origin[2] = 0;
1573                 originhack[0] = originhack[1] = originhack[2] = 0;
1574                 color[0] = color[1] = color[2] = 1;
1575                 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
1576                 scale = 1;
1577                 style = 0;
1578                 islight = false;
1579                 while (1)
1580                 {
1581                         if (!COM_ParseToken(&data))
1582                                 break; // error
1583                         if (com_token[0] == '}')
1584                                 break; // end of entity
1585                         if (com_token[0] == '_')
1586                                 strcpy(key, com_token + 1);
1587                         else
1588                                 strcpy(key, com_token);
1589                         while (key[strlen(key)-1] == ' ') // remove trailing spaces
1590                                 key[strlen(key)-1] = 0;
1591                         if (!COM_ParseToken(&data))
1592                                 break; // error
1593                         strcpy(value, com_token);
1594
1595                         // now that we have the key pair worked out...
1596                         if (!strcmp("light", key))
1597                                 light = atof(value);
1598                         else if (!strcmp("origin", key))
1599                                 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
1600                         else if (!strcmp("color", key))
1601                                 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
1602                         else if (!strcmp("wait", key))
1603                                 scale = atof(value);
1604                         else if (!strcmp("classname", key))
1605                         {
1606                                 if (!strncmp(value, "light", 5))
1607                                 {
1608                                         islight = true;
1609                                         if (!strcmp(value, "light_fluoro"))
1610                                         {
1611                                                 originhack[0] = 0;
1612                                                 originhack[1] = 0;
1613                                                 originhack[2] = 0;
1614                                                 overridecolor[0] = 1;
1615                                                 overridecolor[1] = 1;
1616                                                 overridecolor[2] = 1;
1617                                         }
1618                                         if (!strcmp(value, "light_fluorospark"))
1619                                         {
1620                                                 originhack[0] = 0;
1621                                                 originhack[1] = 0;
1622                                                 originhack[2] = 0;
1623                                                 overridecolor[0] = 1;
1624                                                 overridecolor[1] = 1;
1625                                                 overridecolor[2] = 1;
1626                                         }
1627                                         if (!strcmp(value, "light_globe"))
1628                                         {
1629                                                 originhack[0] = 0;
1630                                                 originhack[1] = 0;
1631                                                 originhack[2] = 0;
1632                                                 overridecolor[0] = 1;
1633                                                 overridecolor[1] = 0.8;
1634                                                 overridecolor[2] = 0.4;
1635                                         }
1636                                         if (!strcmp(value, "light_flame_large_yellow"))
1637                                         {
1638                                                 originhack[0] = 0;
1639                                                 originhack[1] = 0;
1640                                                 originhack[2] = 48;
1641                                                 overridecolor[0] = 1;
1642                                                 overridecolor[1] = 0.5;
1643                                                 overridecolor[2] = 0.1;
1644                                         }
1645                                         if (!strcmp(value, "light_flame_small_yellow"))
1646                                         {
1647                                                 originhack[0] = 0;
1648                                                 originhack[1] = 0;
1649                                                 originhack[2] = 40;
1650                                                 overridecolor[0] = 1;
1651                                                 overridecolor[1] = 0.5;
1652                                                 overridecolor[2] = 0.1;
1653                                         }
1654                                         if (!strcmp(value, "light_torch_small_white"))
1655                                         {
1656                                                 originhack[0] = 0;
1657                                                 originhack[1] = 0;
1658                                                 originhack[2] = 40;
1659                                                 overridecolor[0] = 1;
1660                                                 overridecolor[1] = 0.5;
1661                                                 overridecolor[2] = 0.1;
1662                                         }
1663                                         if (!strcmp(value, "light_torch_small_walltorch"))
1664                                         {
1665                                                 originhack[0] = 0;
1666                                                 originhack[1] = 0;
1667                                                 originhack[2] = 40;
1668                                                 overridecolor[0] = 1;
1669                                                 overridecolor[1] = 0.5;
1670                                                 overridecolor[2] = 0.1;
1671                                         }
1672                                 }
1673                         }
1674                         else if (!strcmp("style", key))
1675                                 style = atoi(value);
1676                 }
1677                 if (light <= 0 && islight)
1678                         light = 300;
1679                 radius = bound(15, light * r_editlights_quakelightsizescale.value / scale, 1048576);
1680                 light = sqrt(bound(0, light, 1048576)) * (1.0f / 16.0f);
1681                 if (color[0] == 1 && color[1] == 1 && color[2] == 1)
1682                         VectorCopy(overridecolor, color);
1683                 VectorScale(color, light, color);
1684                 VectorAdd(origin, originhack, origin);
1685                 if (radius >= 15)
1686                         R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
1687         }
1688 }
1689
1690
1691 void R_Shadow_SetCursorLocationForView(void)
1692 {
1693         vec_t dist, push, frac;
1694         vec3_t dest, endpos, normal;
1695         VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
1696         frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
1697         if (frac < 1)
1698         {
1699                 dist = frac * r_editlights_cursordistance.value;
1700                 push = r_editlights_cursorpushback.value;
1701                 if (push > dist)
1702                         push = dist;
1703                 push = -push;
1704                 VectorMA(endpos, push, vpn, endpos);
1705                 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
1706         }
1707         r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1708         r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1709         r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1710 }
1711
1712 void R_Shadow_UpdateLightingMode(void)
1713 {
1714         r_shadow_lightingmode = 0;
1715         if (r_shadow_realtime.integer)
1716         {
1717                 if (r_shadow_worldlightchain)
1718                         r_shadow_lightingmode = 2;
1719                 else
1720                         r_shadow_lightingmode = 1;
1721         }
1722 }
1723
1724 void R_Shadow_UpdateWorldLightSelection(void)
1725 {
1726         R_Shadow_SetCursorLocationForView();
1727         if (r_editlights.integer)
1728         {
1729                 R_Shadow_SelectLightInView();
1730                 R_Shadow_DrawLightSprites();
1731         }
1732         else
1733                 R_Shadow_SelectLight(NULL);
1734 }
1735
1736 void R_Shadow_EditLights_Clear_f(void)
1737 {
1738         R_Shadow_ClearWorldLights();
1739 }
1740
1741 void R_Shadow_EditLights_Reload_f(void)
1742 {
1743         r_shadow_reloadlights = true;
1744 }
1745
1746 void R_Shadow_EditLights_Save_f(void)
1747 {
1748         if (cl.worldmodel)
1749                 R_Shadow_SaveWorldLights();
1750 }
1751
1752 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
1753 {
1754         R_Shadow_ClearWorldLights();
1755         R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
1756 }
1757
1758 void R_Shadow_EditLights_ImportLightsFile_f(void)
1759 {
1760         R_Shadow_ClearWorldLights();
1761         R_Shadow_LoadLightsFile();
1762 }
1763
1764 void R_Shadow_EditLights_Spawn_f(void)
1765 {
1766         vec3_t color;
1767         if (!r_editlights.integer)
1768         {
1769                 Con_Printf("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
1770                 return;
1771         }
1772         if (Cmd_Argc() != 1)
1773         {
1774                 Con_Printf("r_editlights_spawn does not take parameters\n");
1775                 return;
1776         }
1777         color[0] = color[1] = color[2] = 1;
1778         R_Shadow_NewWorldLight(r_editlights_cursorlocation, 200, color, 0, NULL, true);
1779 }
1780
1781 void R_Shadow_EditLights_Edit_f(void)
1782 {
1783         vec3_t origin, color;
1784         vec_t radius;
1785         int style, shadows;
1786         char cubemapname[1024];
1787         if (!r_editlights.integer)
1788         {
1789                 Con_Printf("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
1790                 return;
1791         }
1792         if (!r_shadow_selectedlight)
1793         {
1794                 Con_Printf("No selected light.\n");
1795                 return;
1796         }
1797         VectorCopy(r_shadow_selectedlight->origin, origin);
1798         radius = r_shadow_selectedlight->lightradius;
1799         VectorCopy(r_shadow_selectedlight->light, color);
1800         style = r_shadow_selectedlight->style;
1801         if (r_shadow_selectedlight->cubemapname)
1802                 strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
1803         else
1804                 cubemapname[0] = 0;
1805         shadows = r_shadow_selectedlight->castshadows;
1806         if (!strcmp(Cmd_Argv(1), "origin"))
1807         {
1808                 if (Cmd_Argc() != 5)
1809                 {
1810                         Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
1811                         return;
1812                 }
1813                 origin[0] = atof(Cmd_Argv(2));
1814                 origin[1] = atof(Cmd_Argv(3));
1815                 origin[2] = atof(Cmd_Argv(4));
1816         }
1817         else if (!strcmp(Cmd_Argv(1), "originx"))
1818         {
1819                 if (Cmd_Argc() != 3)
1820                 {
1821                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1822                         return;
1823                 }
1824                 origin[0] = atof(Cmd_Argv(2));
1825         }
1826         else if (!strcmp(Cmd_Argv(1), "originy"))
1827         {
1828                 if (Cmd_Argc() != 3)
1829                 {
1830                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1831                         return;
1832                 }
1833                 origin[1] = atof(Cmd_Argv(2));
1834         }
1835         else if (!strcmp(Cmd_Argv(1), "originz"))
1836         {
1837                 if (Cmd_Argc() != 3)
1838                 {
1839                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1840                         return;
1841                 }
1842                 origin[2] = atof(Cmd_Argv(2));
1843         }
1844         else if (!strcmp(Cmd_Argv(1), "move"))
1845         {
1846                 if (Cmd_Argc() != 5)
1847                 {
1848                         Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
1849                         return;
1850                 }
1851                 origin[0] += atof(Cmd_Argv(2));
1852                 origin[1] += atof(Cmd_Argv(3));
1853                 origin[2] += atof(Cmd_Argv(4));
1854         }
1855         else if (!strcmp(Cmd_Argv(1), "movex"))
1856         {
1857                 if (Cmd_Argc() != 3)
1858                 {
1859                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1860                         return;
1861                 }
1862                 origin[0] += atof(Cmd_Argv(2));
1863         }
1864         else if (!strcmp(Cmd_Argv(1), "movey"))
1865         {
1866                 if (Cmd_Argc() != 3)
1867                 {
1868                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1869                         return;
1870                 }
1871                 origin[1] += atof(Cmd_Argv(2));
1872         }
1873         else if (!strcmp(Cmd_Argv(1), "movez"))
1874         {
1875                 if (Cmd_Argc() != 3)
1876                 {
1877                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1878                         return;
1879                 }
1880                 origin[2] += atof(Cmd_Argv(2));
1881         }
1882         else if (!strcmp(Cmd_Argv(1), "color"))
1883         {
1884                 if (Cmd_Argc() != 5)
1885                 {
1886                         Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(0));
1887                         return;
1888                 }
1889                 color[0] = atof(Cmd_Argv(2));
1890                 color[1] = atof(Cmd_Argv(3));
1891                 color[2] = atof(Cmd_Argv(4));
1892         }
1893         else if (!strcmp(Cmd_Argv(1), "radius"))
1894         {
1895                 if (Cmd_Argc() != 3)
1896                 {
1897                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1898                         return;
1899                 }
1900                 radius = atof(Cmd_Argv(2));
1901         }
1902         else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "style"))
1903         {
1904                 if (Cmd_Argc() != 3)
1905                 {
1906                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1907                         return;
1908                 }
1909                 style = atoi(Cmd_Argv(2));
1910         }
1911         else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "cubemap"))
1912         {
1913                 if (Cmd_Argc() > 3)
1914                 {
1915                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1916                         return;
1917                 }
1918                 if (Cmd_Argc() == 3)
1919                         strcpy(cubemapname, Cmd_Argv(2));
1920                 else
1921                         cubemapname[0] = 0;
1922         }
1923         else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "shadows"))
1924         {
1925                 if (Cmd_Argc() != 3)
1926                 {
1927                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1928                         return;
1929                 }
1930                 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
1931         }
1932         else
1933         {
1934                 Con_Printf("usage: r_editlights_edit [property] [value]\n");
1935                 Con_Printf("Selected light's properties:\n");
1936                 Con_Printf("Origin: %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
1937                 Con_Printf("Radius: %f\n", r_shadow_selectedlight->lightradius);
1938                 Con_Printf("Color: %f %f %f\n", r_shadow_selectedlight->light[0], r_shadow_selectedlight->light[1], r_shadow_selectedlight->light[2]);
1939                 Con_Printf("Style: %i\n", r_shadow_selectedlight->style);
1940                 Con_Printf("Cubemap: %s\n", r_shadow_selectedlight->cubemapname);
1941                 Con_Printf("Shadows: %s\n", r_shadow_selectedlight->castshadows ? "yes" : "no");
1942                 return;
1943         }
1944         R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1945         r_shadow_selectedlight = NULL;
1946         R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadows);
1947 }
1948
1949 extern int con_vislines;
1950 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
1951 {
1952         float x, y;
1953         char temp[256];
1954         if (r_shadow_selectedlight == NULL)
1955                 return;
1956         x = 0;
1957         y = con_vislines;
1958         sprintf(temp, "Light properties");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1959         sprintf(temp, "Origin %f %f %f", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1960         sprintf(temp, "Radius %f", r_shadow_selectedlight->lightradius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1961         sprintf(temp, "Color %f %f %f", r_shadow_selectedlight->light[0], r_shadow_selectedlight->light[1], r_shadow_selectedlight->light[2]);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1962         sprintf(temp, "Style %i", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1963         sprintf(temp, "Cubemap %s", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1964         sprintf(temp, "Shadows %s", r_shadow_selectedlight->castshadows ? "yes" : "no");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1965 }
1966
1967 void R_Shadow_EditLights_ToggleShadow_f(void)
1968 {
1969         if (!r_editlights.integer)
1970         {
1971                 Con_Printf("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
1972                 return;
1973         }
1974         if (!r_shadow_selectedlight)
1975         {
1976                 Con_Printf("No selected light.\n");
1977                 return;
1978         }
1979         R_Shadow_NewWorldLight(r_shadow_selectedlight->origin, r_shadow_selectedlight->lightradius, r_shadow_selectedlight->light, r_shadow_selectedlight->style, r_shadow_selectedlight->cubemapname, !r_shadow_selectedlight->castshadows);
1980         R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1981         r_shadow_selectedlight = NULL;
1982 }
1983
1984 void R_Shadow_EditLights_Remove_f(void)
1985 {
1986         if (!r_editlights.integer)
1987         {
1988                 Con_Printf("Cannot remove light when not in editing mode.  Set r_editlights to 1.\n");
1989                 return;
1990         }
1991         if (!r_shadow_selectedlight)
1992         {
1993                 Con_Printf("No selected light.\n");
1994                 return;
1995         }
1996         R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1997         r_shadow_selectedlight = NULL;
1998 }
1999
2000 void R_Shadow_EditLights_Init(void)
2001 {
2002         Cvar_RegisterVariable(&r_editlights);
2003         Cvar_RegisterVariable(&r_editlights_cursordistance);
2004         Cvar_RegisterVariable(&r_editlights_cursorpushback);
2005         Cvar_RegisterVariable(&r_editlights_cursorpushoff);
2006         Cvar_RegisterVariable(&r_editlights_cursorgrid);
2007         Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
2008         Cvar_RegisterVariable(&r_editlights_rtlightssizescale);
2009         Cvar_RegisterVariable(&r_editlights_rtlightscolorscale);
2010         Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
2011         Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
2012         Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
2013         Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
2014         Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
2015         Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
2016         Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f);
2017         Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
2018         Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);
2019 }