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