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