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