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