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