]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - r_shadow.c
varray_* rewritten to remove padding (varray_vertex3f, varray_texcoord2f, varray_texc...
[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/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                         qglDisable(GL_BLEND);
1422                         GL_Color(1,1,1,1);
1423                         R_Mesh_GetSpace(numverts);
1424                         R_Mesh_CopyVertex3f(vertex3f, numverts);
1425                         R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1426                         R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
1427                         R_Mesh_Draw(numverts, numtriangles, elements);
1428                         c_rt_lightmeshes++;
1429                         c_rt_lighttris += numtriangles;
1430
1431                         m.tex[0] = 0;
1432                         m.texcubemap[1] = 0;
1433                         m.texcombinergb[1] = GL_MODULATE;
1434                         R_Mesh_TextureState(&m);
1435                         // square alpha in framebuffer a few times to make it shiny
1436                         qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1437                         qglEnable(GL_BLEND);
1438                         // these comments are a test run through this math for intensity 0.5
1439                         // 0.5 * 0.5 = 0.25
1440                         // 0.25 * 0.25 = 0.0625
1441                         // 0.0625 * 0.0625 = 0.00390625
1442                         for (renders = 0;renders < 3;renders++)
1443                         {
1444                                 R_Mesh_GetSpace(numverts);
1445                                 R_Mesh_CopyVertex3f(vertex3f, numverts);
1446                                 R_Mesh_Draw(numverts, numtriangles, elements);
1447                         }
1448                         c_rt_lightmeshes += 3;
1449                         c_rt_lighttris += numtriangles * 3;
1450
1451                         m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1452                         R_Mesh_TextureState(&m);
1453                         qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1454                         R_Mesh_GetSpace(numverts);
1455                         R_Mesh_CopyVertex3f(vertex3f, numverts);
1456                         R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1457                         R_Mesh_Draw(numverts, numtriangles, elements);
1458                         c_rt_lightmeshes++;
1459                         c_rt_lighttris += numtriangles;
1460
1461                         m.tex3d[0] = 0;
1462                         m.tex[0] = R_GetTexture(glosstexture);
1463                         m.texcubemap[1] = R_GetTexture(lightcubemap);
1464                         R_Mesh_TextureState(&m);
1465                         qglColorMask(1,1,1,0);
1466                         qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1467
1468                         VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
1469                         for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1470                         {
1471                                 color[0] = bound(0, color2[0], 1);
1472                                 color[1] = bound(0, color2[1], 1);
1473                                 color[2] = bound(0, color2[2], 1);
1474                                 GL_Color(color[0], color[1], color[2], 1);
1475                                 R_Mesh_GetSpace(numverts);
1476                                 R_Mesh_CopyVertex3f(vertex3f, numverts);
1477                                 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1478                                 if (lightcubemap)
1479                                         R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1480                                 R_Mesh_Draw(numverts, numtriangles, elements);
1481                                 c_rt_lightmeshes++;
1482                                 c_rt_lighttris += numtriangles;
1483                         }
1484                 }
1485                 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1486                 {
1487                         // 2/0/0/0/2 3D combine blendsquare path
1488                         m.tex[0] = R_GetTexture(bumptexture);
1489                         m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1490                         m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1491                         R_Mesh_TextureState(&m);
1492                         qglColorMask(0,0,0,1);
1493                         qglDisable(GL_BLEND);
1494                         GL_Color(1,1,1,1);
1495                         R_Mesh_GetSpace(numverts);
1496                         R_Mesh_CopyVertex3f(vertex3f, numverts);
1497                         R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1498                         R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
1499                         R_Mesh_Draw(numverts, numtriangles, elements);
1500                         c_rt_lightmeshes++;
1501                         c_rt_lighttris += numtriangles;
1502
1503                         m.tex[0] = 0;
1504                         m.texcubemap[1] = 0;
1505                         m.texcombinergb[1] = GL_MODULATE;
1506                         R_Mesh_TextureState(&m);
1507                         // square alpha in framebuffer a few times to make it shiny
1508                         qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1509                         qglEnable(GL_BLEND);
1510                         // these comments are a test run through this math for intensity 0.5
1511                         // 0.5 * 0.5 = 0.25
1512                         // 0.25 * 0.25 = 0.0625
1513                         // 0.0625 * 0.0625 = 0.00390625
1514                         for (renders = 0;renders < 3;renders++)
1515                         {
1516                                 R_Mesh_GetSpace(numverts);
1517                                 R_Mesh_CopyVertex3f(vertex3f, numverts);
1518                                 R_Mesh_Draw(numverts, numtriangles, elements);
1519                         }
1520                         c_rt_lightmeshes += 3;
1521                         c_rt_lighttris += numtriangles * 3;
1522
1523                         R_Mesh_CopyVertex3f(vertex3f, numverts);
1524                         R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1525                         m.tex[0] = R_GetTexture(glosstexture);
1526                         m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
1527                         R_Mesh_TextureState(&m);
1528                         qglColorMask(1,1,1,0);
1529                         qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1530                         c_rt_lightmeshes++;
1531                         c_rt_lighttris += numtriangles;
1532
1533                         VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
1534                         for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1535                         {
1536                                 color[0] = bound(0, color2[0], 1);
1537                                 color[1] = bound(0, color2[1], 1);
1538                                 color[2] = bound(0, color2[2], 1);
1539                                 GL_Color(color[0], color[1], color[2], 1);
1540                                 R_Mesh_GetSpace(numverts);
1541                                 R_Mesh_CopyVertex3f(vertex3f, numverts);
1542                                 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1543                                 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
1544                                 R_Mesh_Draw(numverts, numtriangles, elements);
1545                                 c_rt_lightmeshes++;
1546                                 c_rt_lighttris += numtriangles;
1547                         }
1548                 }
1549                 else if (r_textureunits.integer >= 2 /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1550                 {
1551                         // 2/0/0/0/2/2 2D combine blendsquare path
1552                         m.tex[0] = R_GetTexture(bumptexture);
1553                         m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1554                         m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1555                         R_Mesh_TextureState(&m);
1556                         qglColorMask(0,0,0,1);
1557                         qglDisable(GL_BLEND);
1558                         GL_Color(1,1,1,1);
1559                         R_Mesh_GetSpace(numverts);
1560                         R_Mesh_CopyVertex3f(vertex3f, numverts);
1561                         R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1562                         R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
1563                         R_Mesh_Draw(numverts, numtriangles, elements);
1564                         c_rt_lightmeshes++;
1565                         c_rt_lighttris += numtriangles;
1566
1567                         m.tex[0] = 0;
1568                         m.texcubemap[1] = 0;
1569                         m.texcombinergb[1] = GL_MODULATE;
1570                         R_Mesh_TextureState(&m);
1571                         // square alpha in framebuffer a few times to make it shiny
1572                         qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1573                         qglEnable(GL_BLEND);
1574                         // these comments are a test run through this math for intensity 0.5
1575                         // 0.5 * 0.5 = 0.25
1576                         // 0.25 * 0.25 = 0.0625
1577                         // 0.0625 * 0.0625 = 0.00390625
1578                         for (renders = 0;renders < 3;renders++)
1579                         {
1580                                 R_Mesh_GetSpace(numverts);
1581                                 R_Mesh_CopyVertex3f(vertex3f, numverts);
1582                                 R_Mesh_Draw(numverts, numtriangles, elements);
1583                         }
1584                         c_rt_lightmeshes += 3;
1585                         c_rt_lighttris += numtriangles * 3;
1586
1587                         m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1588                         m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1589                         R_Mesh_TextureState(&m);
1590                         qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1591                         R_Mesh_GetSpace(numverts);
1592                         R_Mesh_CopyVertex3f(vertex3f, numverts);
1593                         R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1594                         R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
1595                         R_Mesh_Draw(numverts, numtriangles, elements);
1596                         c_rt_lightmeshes++;
1597                         c_rt_lighttris += numtriangles;
1598
1599                         m.tex[0] = R_GetTexture(glosstexture);
1600                         m.texcubemap[1] = R_GetTexture(lightcubemap);
1601                         R_Mesh_TextureState(&m);
1602                         qglColorMask(1,1,1,0);
1603                         qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1604
1605                         VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
1606                         for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1607                         {
1608                                 color[0] = bound(0, color2[0], 1);
1609                                 color[1] = bound(0, color2[1], 1);
1610                                 color[2] = bound(0, color2[2], 1);
1611                                 GL_Color(color[0], color[1], color[2], 1);
1612                                 R_Mesh_GetSpace(numverts);
1613                                 R_Mesh_CopyVertex3f(vertex3f, numverts);
1614                                 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1615                                 if (lightcubemap)
1616                                         R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1617                                 R_Mesh_Draw(numverts, numtriangles, elements);
1618                                 c_rt_lightmeshes++;
1619                                 c_rt_lighttris += numtriangles;
1620                         }
1621                 }
1622         }
1623 }
1624
1625 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
1626 {
1627         R_Mesh_Matrix(matrix);
1628         R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
1629 }
1630
1631 cvar_t r_editlights = {0, "r_editlights", "0"};
1632 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
1633 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
1634 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
1635 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
1636 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
1637 cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
1638 cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
1639 worldlight_t *r_shadow_worldlightchain;
1640 worldlight_t *r_shadow_selectedlight;
1641 vec3_t r_editlights_cursorlocation;
1642
1643 static int castshadowcount = 1;
1644 void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname, int castshadow)
1645 {
1646         int i, j, k, l, maxverts = 256, *mark, tris;
1647         float *vertex3f = NULL;
1648         worldlight_t *e;
1649         shadowmesh_t *mesh, *castmesh;
1650         mleaf_t *leaf;
1651         msurface_t *surf;
1652         qbyte *pvs;
1653         surfmesh_t *surfmesh;
1654
1655         if (radius < 15 || DotProduct(color, color) < 0.03)
1656         {
1657                 Con_Printf("R_Shadow_NewWorldLight: refusing to create a light too small/dim\n");
1658                 return;
1659         }
1660
1661         e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
1662         VectorCopy(origin, e->origin);
1663         VectorCopy(color, e->light);
1664         e->lightradius = radius;
1665         e->style = style;
1666         if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
1667         {
1668                 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
1669                 e->style = 0;
1670         }
1671         e->castshadows = castshadow;
1672
1673         e->cullradius = e->lightradius;
1674         for (k = 0;k < 3;k++)
1675         {
1676                 e->mins[k] = e->origin[k] - e->lightradius;
1677                 e->maxs[k] = e->origin[k] + e->lightradius;
1678         }
1679
1680         e->next = r_shadow_worldlightchain;
1681         r_shadow_worldlightchain = e;
1682         if (cubemapname && cubemapname[0])
1683         {
1684                 e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
1685                 strcpy(e->cubemapname, cubemapname);
1686                 // FIXME: add cubemap loading (and don't load a cubemap twice)
1687         }
1688         if (cl.worldmodel)
1689         {
1690                 castshadowcount++;
1691                 i = Mod_PointContents(e->origin, cl.worldmodel);
1692                 if (r_shadow_portallight.integer && i != CONTENTS_SOLID && i != CONTENTS_SKY)
1693                 {
1694                         qbyte *byteleafpvs;
1695                         qbyte *bytesurfacepvs;
1696
1697                         byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->numleafs + 1);
1698                         bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->numsurfaces);
1699
1700                         Portal_Visibility(cl.worldmodel, e->origin, byteleafpvs, bytesurfacepvs, NULL, 0, true, RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin));
1701
1702                         for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1703                                 if (byteleafpvs[i+1] && BoxesOverlap(leaf->mins, leaf->maxs, e->mins, e->maxs))
1704                                         leaf->worldnodeframe = castshadowcount;
1705
1706                         for (i = 0, surf = cl.worldmodel->surfaces;i < cl.worldmodel->numsurfaces;i++, surf++)
1707                                 if (bytesurfacepvs[i] && BoxesOverlap(surf->poly_mins, surf->poly_maxs, e->mins, e->maxs))
1708                                         surf->castshadow = castshadowcount;
1709
1710                         Mem_Free(byteleafpvs);
1711                         Mem_Free(bytesurfacepvs);
1712                 }
1713                 else
1714                 {
1715                         leaf = Mod_PointInLeaf(origin, cl.worldmodel);
1716                         pvs = Mod_LeafPVS(leaf, cl.worldmodel);
1717                         for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1718                         {
1719                                 if (pvs[i >> 3] & (1 << (i & 7)) && BoxesOverlap(leaf->mins, leaf->maxs, e->mins, e->maxs))
1720                                 {
1721                                         leaf->worldnodeframe = castshadowcount;
1722                                         for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
1723                                         {
1724                                                 surf = cl.worldmodel->surfaces + *mark;
1725                                                 if (surf->castshadow != castshadowcount && BoxesOverlap(surf->poly_mins, surf->poly_maxs, e->mins, e->maxs))
1726                                                         surf->castshadow = castshadowcount;
1727                                         }
1728                                 }
1729                         }
1730                 }
1731
1732                 e->numleafs = 0;
1733                 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1734                         if (leaf->worldnodeframe == castshadowcount)
1735                                 e->numleafs++;
1736                 e->numsurfaces = 0;
1737                 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1738                         if (surf->castshadow == castshadowcount)
1739                                 e->numsurfaces++;
1740
1741                 if (e->numleafs)
1742                         e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
1743                 if (e->numsurfaces)
1744                         e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
1745                 e->numleafs = 0;
1746                 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1747                         if (leaf->worldnodeframe == castshadowcount)
1748                                 e->leafs[e->numleafs++] = leaf;
1749                 e->numsurfaces = 0;
1750                 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1751                         if (surf->castshadow == castshadowcount)
1752                                 e->surfaces[e->numsurfaces++] = surf;
1753
1754                 // find bounding box of lit leafs
1755                 VectorCopy(e->origin, e->mins);
1756                 VectorCopy(e->origin, e->maxs);
1757                 for (j = 0;j < e->numleafs;j++)
1758                 {
1759                         leaf = e->leafs[j];
1760                         for (k = 0;k < 3;k++)
1761                         {
1762                                 if (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
1763                                 if (e->maxs[k] < leaf->maxs[k]) e->maxs[k] = leaf->maxs[k];
1764                         }
1765                 }
1766
1767                 for (k = 0;k < 3;k++)
1768                 {
1769                         if (e->mins[k] < e->origin[k] - e->lightradius) e->mins[k] = e->origin[k] - e->lightradius;
1770                         if (e->maxs[k] > e->origin[k] + e->lightradius) e->maxs[k] = e->origin[k] + e->lightradius;
1771                 }
1772                 e->cullradius = RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin);
1773
1774                 if (e->castshadows)
1775                 {
1776                         castshadowcount++;
1777                         for (j = 0;j < e->numsurfaces;j++)
1778                         {
1779                                 surf = e->surfaces[j];
1780                                 if (surf->flags & SURF_SHADOWCAST)
1781                                 {
1782                                         surf->castshadow = castshadowcount;
1783                                         if (maxverts < surf->poly_numverts)
1784                                                 maxverts = surf->poly_numverts;
1785                                 }
1786                         }
1787                         e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1788                         // make a mesh to cast a shadow volume from
1789                         castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1790                         for (j = 0;j < e->numsurfaces;j++)
1791                                 if (e->surfaces[j]->castshadow == castshadowcount)
1792                                         for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
1793                                                 Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->vertex3f, surfmesh->numtriangles, surfmesh->element3i);
1794                         castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
1795
1796                         // cast shadow volume from castmesh
1797                         for (mesh = castmesh;mesh;mesh = mesh->next)
1798                         {
1799                                 R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
1800                                 R_Shadow_ResizeShadowElements(castmesh->numtriangles);
1801
1802                                 if (maxverts < castmesh->numverts * 2)
1803                                 {
1804                                         maxverts = castmesh->numverts * 2;
1805                                         if (vertex3f)
1806                                                 Mem_Free(vertex3f);
1807                                         vertex3f = NULL;
1808                                 }
1809                                 if (vertex3f == NULL && maxverts > 0)
1810                                         vertex3f = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[3]));
1811
1812                                 // now that we have the buffers big enough, construct shadow volume mesh
1813                                 memcpy(vertex3f, castmesh->vertex3f, castmesh->numverts * sizeof(float[3]));
1814                                 R_Shadow_ProjectVertex3f(vertex3f, castmesh->numverts, e->origin, r_shadow_projectdistance.value);//, e->lightradius);
1815                                 tris = R_Shadow_MakeTriangleShadowFlags_Vertex3f(castmesh->element3i, vertex3f, castmesh->numtriangles, trianglefacinglight, trianglefacinglightlist, e->origin);
1816                                 tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->element3i, castmesh->neighbor3i, castmesh->numverts, trianglefacinglight, trianglefacinglightlist, tris, shadowelements);
1817                                 // add the constructed shadow volume mesh
1818                                 Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, vertex3f, tris, shadowelements);
1819                         }
1820                         if (vertex3f)
1821                                 Mem_Free(vertex3f);
1822                         vertex3f = NULL;
1823                         // we're done with castmesh now
1824                         Mod_ShadowMesh_Free(castmesh);
1825                         e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
1826                         for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
1827                                 l += mesh->numtriangles;
1828                         Con_Printf("static shadow volume built containing %i triangles\n", l);
1829                 }
1830         }
1831         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);
1832 }
1833
1834 void R_Shadow_FreeWorldLight(worldlight_t *light)
1835 {
1836         worldlight_t **lightpointer;
1837         for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
1838         if (*lightpointer != light)
1839                 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
1840         *lightpointer = light->next;
1841         if (light->cubemapname)
1842                 Mem_Free(light->cubemapname);
1843         if (light->shadowvolume)
1844                 Mod_ShadowMesh_Free(light->shadowvolume);
1845         if (light->surfaces)
1846                 Mem_Free(light->surfaces);
1847         if (light->leafs)
1848                 Mem_Free(light->leafs);
1849         Mem_Free(light);
1850 }
1851
1852 void R_Shadow_ClearWorldLights(void)
1853 {
1854         while (r_shadow_worldlightchain)
1855                 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
1856         r_shadow_selectedlight = NULL;
1857 }
1858
1859 void R_Shadow_SelectLight(worldlight_t *light)
1860 {
1861         if (r_shadow_selectedlight)
1862                 r_shadow_selectedlight->selected = false;
1863         r_shadow_selectedlight = light;
1864         if (r_shadow_selectedlight)
1865                 r_shadow_selectedlight->selected = true;
1866 }
1867
1868
1869 void R_DrawLightSprite(int texnum, const vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
1870 {
1871         rmeshstate_t m;
1872         float diff[3];
1873
1874         if (fogenabled)
1875         {
1876                 VectorSubtract(origin, r_origin, diff);
1877                 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
1878         }
1879
1880         memset(&m, 0, sizeof(m));
1881         m.blendfunc1 = GL_SRC_ALPHA;
1882         m.blendfunc2 = GL_ONE;
1883         m.tex[0] = texnum;
1884         R_Mesh_Matrix(&r_identitymatrix);
1885         R_Mesh_State(&m);
1886
1887         GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1888         R_DrawSpriteMesh(origin, vright, vup, scale, -scale, -scale, scale);
1889 }
1890
1891 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
1892 {
1893         cachepic_t *pic;
1894         pic = Draw_CachePic("gfx/crosshair1.tga");
1895         if (pic)
1896                 R_DrawLightSprite(R_GetTexture(pic->tex), r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 0.5);
1897 }
1898
1899 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
1900 {
1901         float intensity;
1902         const worldlight_t *light;
1903         light = calldata1;
1904         intensity = 0.5;
1905         if (light->selected)
1906                 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
1907         if (light->shadowvolume)
1908                 R_DrawLightSprite(calldata2, light->origin, 8, intensity, intensity, intensity, 0.5);
1909         else
1910                 R_DrawLightSprite(calldata2, light->origin, 8, intensity * 0.5, intensity * 0.5, intensity * 0.5, 0.5);
1911 }
1912
1913 void R_Shadow_DrawLightSprites(void)
1914 {
1915         int i, texnums[5];
1916         cachepic_t *pic;
1917         worldlight_t *light;
1918
1919         for (i = 0;i < 5;i++)
1920         {
1921                 pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1));
1922                 if (pic)
1923                         texnums[i] = R_GetTexture(pic->tex);
1924                 else
1925                         texnums[i] = 0;
1926         }
1927
1928         for (light = r_shadow_worldlightchain;light;light = light->next)
1929                 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, texnums[((int) light) % 5]);
1930         R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
1931 }
1932
1933 void R_Shadow_SelectLightInView(void)
1934 {
1935         float bestrating, rating, temp[3];
1936         worldlight_t *best, *light;
1937         best = NULL;
1938         bestrating = 0;
1939         for (light = r_shadow_worldlightchain;light;light = light->next)
1940         {
1941                 VectorSubtract(light->origin, r_refdef.vieworg, temp);
1942                 rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
1943                 if (rating >= 0.95)
1944                 {
1945                         rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
1946                         if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
1947                         {
1948                                 bestrating = rating;
1949                                 best = light;
1950                         }
1951                 }
1952         }
1953         R_Shadow_SelectLight(best);
1954 }
1955
1956 void R_Shadow_LoadWorldLights(void)
1957 {
1958         int n, a, style, shadow;
1959         char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
1960         float origin[3], radius, color[3];
1961         if (cl.worldmodel == NULL)
1962         {
1963                 Con_Printf("No map loaded.\n");
1964                 return;
1965         }
1966         COM_StripExtension(cl.worldmodel->name, name);
1967         strcat(name, ".rtlights");
1968         lightsstring = COM_LoadFile(name, false);
1969         if (lightsstring)
1970         {
1971                 s = lightsstring;
1972                 n = 0;
1973                 while (*s)
1974                 {
1975                         t = s;
1976                         while (*s && *s != '\n')
1977                                 s++;
1978                         if (!*s)
1979                                 break;
1980                         *s = 0;
1981                         shadow = true;
1982                         // check for modifier flags
1983                         if (*t == '!')
1984                         {
1985                                 shadow = false;
1986                                 t++;
1987                         }
1988                         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);
1989                         if (a < 9)
1990                                 cubemapname[0] = 0;
1991                         *s = '\n';
1992                         if (a < 8)
1993                         {
1994                                 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);
1995                                 break;
1996                         }
1997                         VectorScale(color, r_editlights_rtlightscolorscale.value, color);
1998                         radius *= r_editlights_rtlightssizescale.value;
1999                         R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadow);
2000                         s++;
2001                         n++;
2002                 }
2003                 if (*s)
2004                         Con_Printf("invalid rtlights file \"%s\"\n", name);
2005                 Mem_Free(lightsstring);
2006         }
2007 }
2008
2009 void R_Shadow_SaveWorldLights(void)
2010 {
2011         worldlight_t *light;
2012         int bufchars, bufmaxchars;
2013         char *buf, *oldbuf;
2014         char name[MAX_QPATH];
2015         char line[1024];
2016         if (!r_shadow_worldlightchain)
2017                 return;
2018         if (cl.worldmodel == NULL)
2019         {
2020                 Con_Printf("No map loaded.\n");
2021                 return;
2022         }
2023         COM_StripExtension(cl.worldmodel->name, name);
2024         strcat(name, ".rtlights");
2025         bufchars = bufmaxchars = 0;
2026         buf = NULL;
2027         for (light = r_shadow_worldlightchain;light;light = light->next)
2028         {
2029                 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 : "");
2030                 if (bufchars + strlen(line) > bufmaxchars)
2031                 {
2032                         bufmaxchars = bufchars + strlen(line) + 2048;
2033                         oldbuf = buf;
2034                         buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
2035                         if (oldbuf)
2036                         {
2037                                 if (bufchars)
2038                                         memcpy(buf, oldbuf, bufchars);
2039                                 Mem_Free(oldbuf);
2040                         }
2041                 }
2042                 if (strlen(line))
2043                 {
2044                         memcpy(buf + bufchars, line, strlen(line));
2045                         bufchars += strlen(line);
2046                 }
2047         }
2048         if (bufchars)
2049                 COM_WriteFile(name, buf, bufchars);
2050         if (buf)
2051                 Mem_Free(buf);
2052 }
2053
2054 void R_Shadow_LoadLightsFile(void)
2055 {
2056         int n, a, style;
2057         char name[MAX_QPATH], *lightsstring, *s, *t;
2058         float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
2059         if (cl.worldmodel == NULL)
2060         {
2061                 Con_Printf("No map loaded.\n");
2062                 return;
2063         }
2064         COM_StripExtension(cl.worldmodel->name, name);
2065         strcat(name, ".lights");
2066         lightsstring = COM_LoadFile(name, false);
2067         if (lightsstring)
2068         {
2069                 s = lightsstring;
2070                 n = 0;
2071                 while (*s)
2072                 {
2073                         t = s;
2074                         while (*s && *s != '\n')
2075                                 s++;
2076                         if (!*s)
2077                                 break;
2078                         *s = 0;
2079                         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);
2080                         *s = '\n';
2081                         if (a < 14)
2082                         {
2083                                 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);
2084                                 break;
2085                         }
2086                         radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
2087                         radius = bound(15, radius, 4096);
2088                         VectorScale(color, (2.0f / (8388608.0f)), color);
2089                         R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
2090                         s++;
2091                         n++;
2092                 }
2093                 if (*s)
2094                         Con_Printf("invalid lights file \"%s\"\n", name);
2095                 Mem_Free(lightsstring);
2096         }
2097 }
2098
2099 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
2100 {
2101         int entnum, style, islight;
2102         char key[256], value[1024];
2103         float origin[3], radius, color[3], light, scale, originhack[3], overridecolor[3];
2104         const char *data;
2105
2106         if (cl.worldmodel == NULL)
2107         {
2108                 Con_Printf("No map loaded.\n");
2109                 return;
2110         }
2111         data = cl.worldmodel->entities;
2112         if (!data)
2113                 return;
2114         for (entnum = 0;COM_ParseToken(&data) && com_token[0] == '{';entnum++)
2115         {
2116                 light = 0;
2117                 origin[0] = origin[1] = origin[2] = 0;
2118                 originhack[0] = originhack[1] = originhack[2] = 0;
2119                 color[0] = color[1] = color[2] = 1;
2120                 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
2121                 scale = 1;
2122                 style = 0;
2123                 islight = false;
2124                 while (1)
2125                 {
2126                         if (!COM_ParseToken(&data))
2127                                 break; // error
2128                         if (com_token[0] == '}')
2129                                 break; // end of entity
2130                         if (com_token[0] == '_')
2131                                 strcpy(key, com_token + 1);
2132                         else
2133                                 strcpy(key, com_token);
2134                         while (key[strlen(key)-1] == ' ') // remove trailing spaces
2135                                 key[strlen(key)-1] = 0;
2136                         if (!COM_ParseToken(&data))
2137                                 break; // error
2138                         strcpy(value, com_token);
2139
2140                         // now that we have the key pair worked out...
2141                         if (!strcmp("light", key))
2142                                 light = atof(value);
2143                         else if (!strcmp("origin", key))
2144                                 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
2145                         else if (!strcmp("color", key))
2146                                 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
2147                         else if (!strcmp("wait", key))
2148                                 scale = atof(value);
2149                         else if (!strcmp("classname", key))
2150                         {
2151                                 if (!strncmp(value, "light", 5))
2152                                 {
2153                                         islight = true;
2154                                         if (!strcmp(value, "light_fluoro"))
2155                                         {
2156                                                 originhack[0] = 0;
2157                                                 originhack[1] = 0;
2158                                                 originhack[2] = 0;
2159                                                 overridecolor[0] = 1;
2160                                                 overridecolor[1] = 1;
2161                                                 overridecolor[2] = 1;
2162                                         }
2163                                         if (!strcmp(value, "light_fluorospark"))
2164                                         {
2165                                                 originhack[0] = 0;
2166                                                 originhack[1] = 0;
2167                                                 originhack[2] = 0;
2168                                                 overridecolor[0] = 1;
2169                                                 overridecolor[1] = 1;
2170                                                 overridecolor[2] = 1;
2171                                         }
2172                                         if (!strcmp(value, "light_globe"))
2173                                         {
2174                                                 originhack[0] = 0;
2175                                                 originhack[1] = 0;
2176                                                 originhack[2] = 0;
2177                                                 overridecolor[0] = 1;
2178                                                 overridecolor[1] = 0.8;
2179                                                 overridecolor[2] = 0.4;
2180                                         }
2181                                         if (!strcmp(value, "light_flame_large_yellow"))
2182                                         {
2183                                                 originhack[0] = 0;
2184                                                 originhack[1] = 0;
2185                                                 originhack[2] = 48;
2186                                                 overridecolor[0] = 1;
2187                                                 overridecolor[1] = 0.5;
2188                                                 overridecolor[2] = 0.1;
2189                                         }
2190                                         if (!strcmp(value, "light_flame_small_yellow"))
2191                                         {
2192                                                 originhack[0] = 0;
2193                                                 originhack[1] = 0;
2194                                                 originhack[2] = 40;
2195                                                 overridecolor[0] = 1;
2196                                                 overridecolor[1] = 0.5;
2197                                                 overridecolor[2] = 0.1;
2198                                         }
2199                                         if (!strcmp(value, "light_torch_small_white"))
2200                                         {
2201                                                 originhack[0] = 0;
2202                                                 originhack[1] = 0;
2203                                                 originhack[2] = 40;
2204                                                 overridecolor[0] = 1;
2205                                                 overridecolor[1] = 0.5;
2206                                                 overridecolor[2] = 0.1;
2207                                         }
2208                                         if (!strcmp(value, "light_torch_small_walltorch"))
2209                                         {
2210                                                 originhack[0] = 0;
2211                                                 originhack[1] = 0;
2212                                                 originhack[2] = 40;
2213                                                 overridecolor[0] = 1;
2214                                                 overridecolor[1] = 0.5;
2215                                                 overridecolor[2] = 0.1;
2216                                         }
2217                                 }
2218                         }
2219                         else if (!strcmp("style", key))
2220                                 style = atoi(value);
2221                 }
2222                 if (light <= 0 && islight)
2223                         light = 300;
2224                 radius = min(light * r_editlights_quakelightsizescale.value / scale, 1048576);
2225                 light = sqrt(bound(0, light, 1048576)) * (1.0f / 16.0f);
2226                 if (color[0] == 1 && color[1] == 1 && color[2] == 1)
2227                         VectorCopy(overridecolor, color);
2228                 VectorScale(color, light, color);
2229                 VectorAdd(origin, originhack, origin);
2230                 if (radius >= 15)
2231                         R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
2232         }
2233 }
2234
2235
2236 void R_Shadow_SetCursorLocationForView(void)
2237 {
2238         vec_t dist, push, frac;
2239         vec3_t dest, endpos, normal;
2240         VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
2241         frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
2242         if (frac < 1)
2243         {
2244                 dist = frac * r_editlights_cursordistance.value;
2245                 push = r_editlights_cursorpushback.value;
2246                 if (push > dist)
2247                         push = dist;
2248                 push = -push;
2249                 VectorMA(endpos, push, vpn, endpos);
2250                 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
2251         }
2252         r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2253         r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2254         r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2255 }
2256
2257 void R_Shadow_UpdateLightingMode(void)
2258 {
2259         r_shadow_lightingmode = 0;
2260         if (r_shadow_realtime.integer)
2261         {
2262                 if (r_shadow_worldlightchain)
2263                         r_shadow_lightingmode = 2;
2264                 else
2265                         r_shadow_lightingmode = 1;
2266         }
2267 }
2268
2269 void R_Shadow_UpdateWorldLightSelection(void)
2270 {
2271         R_Shadow_SetCursorLocationForView();
2272         if (r_editlights.integer)
2273         {
2274                 R_Shadow_SelectLightInView();
2275                 R_Shadow_DrawLightSprites();
2276         }
2277         else
2278                 R_Shadow_SelectLight(NULL);
2279 }
2280
2281 void R_Shadow_EditLights_Clear_f(void)
2282 {
2283         R_Shadow_ClearWorldLights();
2284 }
2285
2286 void R_Shadow_EditLights_Reload_f(void)
2287 {
2288         r_shadow_reloadlights = true;
2289 }
2290
2291 void R_Shadow_EditLights_Save_f(void)
2292 {
2293         if (cl.worldmodel)
2294                 R_Shadow_SaveWorldLights();
2295 }
2296
2297 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
2298 {
2299         R_Shadow_ClearWorldLights();
2300         R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
2301 }
2302
2303 void R_Shadow_EditLights_ImportLightsFile_f(void)
2304 {
2305         R_Shadow_ClearWorldLights();
2306         R_Shadow_LoadLightsFile();
2307 }
2308
2309 void R_Shadow_EditLights_Spawn_f(void)
2310 {
2311         vec3_t color;
2312         if (!r_editlights.integer)
2313         {
2314                 Con_Printf("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
2315                 return;
2316         }
2317         if (Cmd_Argc() != 1)
2318         {
2319                 Con_Printf("r_editlights_spawn does not take parameters\n");
2320                 return;
2321         }
2322         color[0] = color[1] = color[2] = 1;
2323         R_Shadow_NewWorldLight(r_editlights_cursorlocation, 200, color, 0, NULL, true);
2324 }
2325
2326 void R_Shadow_EditLights_Edit_f(void)
2327 {
2328         vec3_t origin, color;
2329         vec_t radius;
2330         int style, shadows;
2331         char cubemapname[1024];
2332         if (!r_editlights.integer)
2333         {
2334                 Con_Printf("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
2335                 return;
2336         }
2337         if (!r_shadow_selectedlight)
2338         {
2339                 Con_Printf("No selected light.\n");
2340                 return;
2341         }
2342         VectorCopy(r_shadow_selectedlight->origin, origin);
2343         radius = r_shadow_selectedlight->lightradius;
2344         VectorCopy(r_shadow_selectedlight->light, color);
2345         style = r_shadow_selectedlight->style;
2346         if (r_shadow_selectedlight->cubemapname)
2347                 strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
2348         else
2349                 cubemapname[0] = 0;
2350         shadows = r_shadow_selectedlight->castshadows;
2351         if (!strcmp(Cmd_Argv(1), "origin"))
2352         {
2353                 if (Cmd_Argc() != 5)
2354                 {
2355                         Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
2356                         return;
2357                 }
2358                 origin[0] = atof(Cmd_Argv(2));
2359                 origin[1] = atof(Cmd_Argv(3));
2360                 origin[2] = atof(Cmd_Argv(4));
2361         }
2362         else if (!strcmp(Cmd_Argv(1), "originx"))
2363         {
2364                 if (Cmd_Argc() != 3)
2365                 {
2366                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2367                         return;
2368                 }
2369                 origin[0] = atof(Cmd_Argv(2));
2370         }
2371         else if (!strcmp(Cmd_Argv(1), "originy"))
2372         {
2373                 if (Cmd_Argc() != 3)
2374                 {
2375                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2376                         return;
2377                 }
2378                 origin[1] = atof(Cmd_Argv(2));
2379         }
2380         else if (!strcmp(Cmd_Argv(1), "originz"))
2381         {
2382                 if (Cmd_Argc() != 3)
2383                 {
2384                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2385                         return;
2386                 }
2387                 origin[2] = atof(Cmd_Argv(2));
2388         }
2389         else if (!strcmp(Cmd_Argv(1), "move"))
2390         {
2391                 if (Cmd_Argc() != 5)
2392                 {
2393                         Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
2394                         return;
2395                 }
2396                 origin[0] += atof(Cmd_Argv(2));
2397                 origin[1] += atof(Cmd_Argv(3));
2398                 origin[2] += atof(Cmd_Argv(4));
2399         }
2400         else if (!strcmp(Cmd_Argv(1), "movex"))
2401         {
2402                 if (Cmd_Argc() != 3)
2403                 {
2404                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2405                         return;
2406                 }
2407                 origin[0] += atof(Cmd_Argv(2));
2408         }
2409         else if (!strcmp(Cmd_Argv(1), "movey"))
2410         {
2411                 if (Cmd_Argc() != 3)
2412                 {
2413                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2414                         return;
2415                 }
2416                 origin[1] += atof(Cmd_Argv(2));
2417         }
2418         else if (!strcmp(Cmd_Argv(1), "movez"))
2419         {
2420                 if (Cmd_Argc() != 3)
2421                 {
2422                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2423                         return;
2424                 }
2425                 origin[2] += atof(Cmd_Argv(2));
2426         }
2427         else if (!strcmp(Cmd_Argv(1), "color"))
2428         {
2429                 if (Cmd_Argc() != 5)
2430                 {
2431                         Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(0));
2432                         return;
2433                 }
2434                 color[0] = atof(Cmd_Argv(2));
2435                 color[1] = atof(Cmd_Argv(3));
2436                 color[2] = atof(Cmd_Argv(4));
2437         }
2438         else if (!strcmp(Cmd_Argv(1), "radius"))
2439         {
2440                 if (Cmd_Argc() != 3)
2441                 {
2442                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2443                         return;
2444                 }
2445                 radius = atof(Cmd_Argv(2));
2446         }
2447         else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "style"))
2448         {
2449                 if (Cmd_Argc() != 3)
2450                 {
2451                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2452                         return;
2453                 }
2454                 style = atoi(Cmd_Argv(2));
2455         }
2456         else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "cubemap"))
2457         {
2458                 if (Cmd_Argc() > 3)
2459                 {
2460                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2461                         return;
2462                 }
2463                 if (Cmd_Argc() == 3)
2464                         strcpy(cubemapname, Cmd_Argv(2));
2465                 else
2466                         cubemapname[0] = 0;
2467         }
2468         else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "shadows"))
2469         {
2470                 if (Cmd_Argc() != 3)
2471                 {
2472                         Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2473                         return;
2474                 }
2475                 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
2476         }
2477         else
2478         {
2479                 Con_Printf("usage: r_editlights_edit [property] [value]\n");
2480                 Con_Printf("Selected light's properties:\n");
2481                 Con_Printf("Origin: %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
2482                 Con_Printf("Radius: %f\n", r_shadow_selectedlight->lightradius);
2483                 Con_Printf("Color: %f %f %f\n", r_shadow_selectedlight->light[0], r_shadow_selectedlight->light[1], r_shadow_selectedlight->light[2]);
2484                 Con_Printf("Style: %i\n", r_shadow_selectedlight->style);
2485                 Con_Printf("Cubemap: %s\n", r_shadow_selectedlight->cubemapname);
2486                 Con_Printf("Shadows: %s\n", r_shadow_selectedlight->castshadows ? "yes" : "no");
2487                 return;
2488         }
2489         R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2490         r_shadow_selectedlight = NULL;
2491         R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadows);
2492 }
2493
2494 extern int con_vislines;
2495 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
2496 {
2497         float x, y;
2498         char temp[256];
2499         if (r_shadow_selectedlight == NULL)
2500                 return;
2501         x = 0;
2502         y = con_vislines;
2503         sprintf(temp, "Light properties");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2504         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;
2505         sprintf(temp, "Radius %f", r_shadow_selectedlight->lightradius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2506         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;
2507         sprintf(temp, "Style %i", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2508         sprintf(temp, "Cubemap %s", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2509         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;
2510 }
2511
2512 void R_Shadow_EditLights_ToggleShadow_f(void)
2513 {
2514         if (!r_editlights.integer)
2515         {
2516                 Con_Printf("Cannot spawn light when not in editing mode.  Set r_editlights to 1.\n");
2517                 return;
2518         }
2519         if (!r_shadow_selectedlight)
2520         {
2521                 Con_Printf("No selected light.\n");
2522                 return;
2523         }
2524         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);
2525         R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2526         r_shadow_selectedlight = NULL;
2527 }
2528
2529 void R_Shadow_EditLights_Remove_f(void)
2530 {
2531         if (!r_editlights.integer)
2532         {
2533                 Con_Printf("Cannot remove light when not in editing mode.  Set r_editlights to 1.\n");
2534                 return;
2535         }
2536         if (!r_shadow_selectedlight)
2537         {
2538                 Con_Printf("No selected light.\n");
2539                 return;
2540         }
2541         R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2542         r_shadow_selectedlight = NULL;
2543 }
2544
2545 void R_Shadow_EditLights_Init(void)
2546 {
2547         Cvar_RegisterVariable(&r_editlights);
2548         Cvar_RegisterVariable(&r_editlights_cursordistance);
2549         Cvar_RegisterVariable(&r_editlights_cursorpushback);
2550         Cvar_RegisterVariable(&r_editlights_cursorpushoff);
2551         Cvar_RegisterVariable(&r_editlights_cursorgrid);
2552         Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
2553         Cvar_RegisterVariable(&r_editlights_rtlightssizescale);
2554         Cvar_RegisterVariable(&r_editlights_rtlightscolorscale);
2555         Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
2556         Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
2557         Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
2558         Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
2559         Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
2560         Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
2561         Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f);
2562         Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
2563         Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);
2564 }