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