4 #include "cl_collision.h"
7 extern void R_Shadow_EditLights_Init(void);
9 #define SHADOWSTAGE_NONE 0
10 #define SHADOWSTAGE_STENCIL 1
11 #define SHADOWSTAGE_LIGHT 2
12 #define SHADOWSTAGE_ERASESTENCIL 3
14 int r_shadowstage = SHADOWSTAGE_NONE;
15 int r_shadow_reloadlights = false;
17 int r_shadow_lightingmode = 0;
19 mempool_t *r_shadow_mempool;
21 int maxshadowelements;
23 int maxtrianglefacinglight;
24 qbyte *trianglefacinglight;
26 rtexturepool_t *r_shadow_texturepool;
27 rtexture_t *r_shadow_normalscubetexture;
28 rtexture_t *r_shadow_attenuation2dtexture;
29 rtexture_t *r_shadow_attenuation3dtexture;
30 rtexture_t *r_shadow_blankbumptexture;
31 rtexture_t *r_shadow_blankglosstexture;
32 rtexture_t *r_shadow_blankwhitetexture;
34 cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5"};
35 cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1"};
36 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
37 cvar_t r_shadow_realtime = {0, "r_shadow_realtime", "0"};
38 cvar_t r_shadow_gloss = {0, "r_shadow_gloss", "1"};
39 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
40 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
41 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
42 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0"};
43 cvar_t r_shadow_shadownudge = {0, "r_shadow_shadownudge", "1"};
44 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1"};
45 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "100000"};
46 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1"};
48 int c_rt_lights, c_rt_clears, c_rt_scissored;
49 int c_rt_shadowmeshes, c_rt_shadowtris, c_rt_lightmeshes, c_rt_lighttris;
50 int c_rtcached_shadowmeshes, c_rtcached_shadowtris;
52 void R_Shadow_ClearWorldLights(void);
53 void R_Shadow_SaveWorldLights(void);
54 void R_Shadow_LoadWorldLights(void);
55 void R_Shadow_LoadLightsFile(void);
56 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
58 void r_shadow_start(void)
60 // allocate vertex processing arrays
61 r_shadow_mempool = Mem_AllocPool("R_Shadow");
62 maxshadowelements = 0;
63 shadowelements = NULL;
64 maxtrianglefacinglight = 0;
65 trianglefacinglight = NULL;
66 r_shadow_normalscubetexture = NULL;
67 r_shadow_attenuation2dtexture = NULL;
68 r_shadow_attenuation3dtexture = NULL;
69 r_shadow_blankbumptexture = NULL;
70 r_shadow_blankglosstexture = NULL;
71 r_shadow_blankwhitetexture = NULL;
72 r_shadow_texturepool = NULL;
73 R_Shadow_ClearWorldLights();
74 r_shadow_reloadlights = true;
77 void r_shadow_shutdown(void)
79 R_Shadow_ClearWorldLights();
80 r_shadow_reloadlights = true;
81 r_shadow_normalscubetexture = NULL;
82 r_shadow_attenuation2dtexture = NULL;
83 r_shadow_attenuation3dtexture = NULL;
84 r_shadow_blankbumptexture = NULL;
85 r_shadow_blankglosstexture = NULL;
86 r_shadow_blankwhitetexture = NULL;
87 R_FreeTexturePool(&r_shadow_texturepool);
88 maxshadowelements = 0;
89 shadowelements = NULL;
90 maxtrianglefacinglight = 0;
91 trianglefacinglight = NULL;
92 Mem_FreePool(&r_shadow_mempool);
95 void r_shadow_newmap(void)
97 R_Shadow_ClearWorldLights();
98 r_shadow_reloadlights = true;
101 void R_Shadow_Init(void)
103 Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
104 Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
105 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
106 Cvar_RegisterVariable(&r_shadow_realtime);
107 Cvar_RegisterVariable(&r_shadow_gloss);
108 Cvar_RegisterVariable(&r_shadow_debuglight);
109 Cvar_RegisterVariable(&r_shadow_scissor);
110 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
111 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
112 Cvar_RegisterVariable(&r_shadow_shadownudge);
113 Cvar_RegisterVariable(&r_shadow_portallight);
114 Cvar_RegisterVariable(&r_shadow_projectdistance);
115 Cvar_RegisterVariable(&r_shadow_texture3d);
116 R_Shadow_EditLights_Init();
117 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
120 void R_Shadow_ProjectVertices(float *verts, int numverts, const float *relativelightorigin, float projectdistance)
123 float *in, *out, diff[4];
125 out = verts + numverts * 4;
126 for (i = 0;i < numverts;i++, in += 4, out += 4)
128 VectorSubtract(in, relativelightorigin, diff);
129 VectorNormalizeFast(diff);
130 VectorMA(in, projectdistance, diff, out);
131 VectorMA(in, r_shadow_shadownudge.value, diff, in);
135 void R_Shadow_MakeTriangleShadowFlags(const int *elements, const float *vertex, int numtris, qbyte *trianglefacinglight, const float *relativelightorigin, float lightradius)
138 const float *v0, *v1, *v2;
139 for (i = 0;i < numtris;i++, elements += 3)
141 // calculate triangle facing flag
142 v0 = vertex + elements[0] * 4;
143 v1 = vertex + elements[1] * 4;
144 v2 = vertex + elements[2] * 4;
145 // we do not need to normalize the surface normal because both sides
146 // of the comparison use it, therefore they are both multiplied the
147 // same amount... furthermore the subtract can be done on the
148 // vectors, saving a little bit of math in the dotproducts
151 // subtracts v1 from v0 and v2, combined into a crossproduct,
152 // combined with a dotproduct of the light location relative to the
153 // first point of the triangle (any point works, since the triangle
154 // is obviously flat), and finally a comparison to determine if the
155 // light is infront of the triangle (the goal of this statement)
156 trianglefacinglight[i] =
157 (relativelightorigin[0] - v0[0]) * ((v0[1] - v1[1]) * (v2[2] - v1[2]) - (v0[2] - v1[2]) * (v2[1] - v1[1]))
158 + (relativelightorigin[1] - v0[1]) * ((v0[2] - v1[2]) * (v2[0] - v1[0]) - (v0[0] - v1[0]) * (v2[2] - v1[2]))
159 + (relativelightorigin[2] - v0[2]) * ((v0[0] - v1[0]) * (v2[1] - v1[1]) - (v0[1] - v1[1]) * (v2[0] - v1[0])) > 0;
163 float dir0[3], dir1[3], temp[3];
165 // calculate two mostly perpendicular edge directions
166 VectorSubtract(v0, v1, dir0);
167 VectorSubtract(v2, v1, dir1);
169 // we have two edge directions, we can calculate a third vector from
170 // them, which is the direction of the surface normal (it's magnitude
172 CrossProduct(dir0, dir1, temp);
174 // this is entirely unnecessary, but kept for clarity
175 //VectorNormalize(temp);
177 // compare distance of light along normal, with distance of any point
178 // of the triangle along the same normal (the triangle is planar,
179 // I.E. flat, so all points give the same answer)
180 // the normal is not normalized because it is used on both sides of
181 // the comparison, so it's magnitude does not matter
182 trianglefacinglight[i] = DotProduct(relativelightorigin, temp) >= DotProduct(v0, temp);
188 int R_Shadow_BuildShadowVolumeTriangles(const int *elements, const int *neighbors, int numtris, int numverts, const qbyte *trianglefacinglight, int *out)
191 // check each frontface for bordering backfaces,
192 // and cast shadow polygons from those edges,
193 // also create front and back caps for shadow volume
195 for (i = 0;i < numtris;i++, elements += 3, neighbors += 3)
197 if (trianglefacinglight[i])
199 // triangle is frontface and therefore casts shadow,
200 // output front and back caps for shadow volume
202 out[0] = elements[0];
203 out[1] = elements[1];
204 out[2] = elements[2];
205 // rear cap (with flipped winding order)
206 out[3] = elements[0] + numverts;
207 out[4] = elements[2] + numverts;
208 out[5] = elements[1] + numverts;
212 if (neighbors[0] < 0 || !trianglefacinglight[neighbors[0]])
214 out[0] = elements[1];
215 out[1] = elements[0];
216 out[2] = elements[0] + numverts;
217 out[3] = elements[1];
218 out[4] = elements[0] + numverts;
219 out[5] = elements[1] + numverts;
223 if (neighbors[1] < 0 || !trianglefacinglight[neighbors[1]])
225 out[0] = elements[2];
226 out[1] = elements[1];
227 out[2] = elements[1] + numverts;
228 out[3] = elements[2];
229 out[4] = elements[1] + numverts;
230 out[5] = elements[2] + numverts;
234 if (neighbors[2] < 0 || !trianglefacinglight[neighbors[2]])
236 out[0] = elements[0];
237 out[1] = elements[2];
238 out[2] = elements[2] + numverts;
239 out[3] = elements[0];
240 out[4] = elements[2] + numverts;
241 out[5] = elements[0] + numverts;
250 void R_Shadow_ResizeTriangleFacingLight(int numtris)
252 // make sure trianglefacinglight is big enough for this volume
253 if (maxtrianglefacinglight < numtris)
255 maxtrianglefacinglight = numtris;
256 if (trianglefacinglight)
257 Mem_Free(trianglefacinglight);
258 trianglefacinglight = Mem_Alloc(r_shadow_mempool, maxtrianglefacinglight);
262 void R_Shadow_ResizeShadowElements(int numtris)
264 // make sure shadowelements is big enough for this volume
265 if (maxshadowelements < numtris * 24)
267 maxshadowelements = numtris * 24;
269 Mem_Free(shadowelements);
270 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
274 void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance)
277 if (projectdistance < 0.1)
279 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
285 // a triangle facing the light source
288 // a triangle not facing the light source
291 // an extrusion of the frontfaces, beginning at the original geometry and
292 // ending further from the light source than the original geometry
293 // (presumably at least as far as the light's radius, if the light has a
294 // radius at all), capped at both front and back to avoid any problems
297 // draws the shadow volumes of the model.
299 // vertex locations must already be in varray_vertex before use.
300 // varray_vertex must have capacity for numverts * 2.
302 // make sure trianglefacinglight is big enough for this volume
303 if (maxtrianglefacinglight < numtris)
304 R_Shadow_ResizeTriangleFacingLight(numtris);
306 // make sure shadowelements is big enough for this volume
307 if (maxshadowelements < numtris * 24)
308 R_Shadow_ResizeShadowElements(numtris);
310 // check which triangles are facing the light
311 R_Shadow_MakeTriangleShadowFlags(elements, varray_vertex, numtris, trianglefacinglight, relativelightorigin, lightradius);
313 // generate projected vertices
314 // by clever use of elements we'll construct the whole shadow from
315 // the unprojected vertices and these projected vertices
316 R_Shadow_ProjectVertices(varray_vertex, numverts, relativelightorigin, projectdistance);
318 // output triangle elements
319 tris = R_Shadow_BuildShadowVolumeTriangles(elements, neighbors, numtris, numverts, trianglefacinglight, shadowelements);
320 R_Shadow_RenderVolume(numverts * 2, tris, shadowelements);
323 void R_Shadow_RenderVolume(int numverts, int numtris, int *elements)
325 if (!numverts || !numtris)
327 if (r_shadowstage == SHADOWSTAGE_STENCIL)
329 // increment stencil if backface is behind depthbuffer
330 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
331 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
332 R_Mesh_Draw(numverts, numtris, elements);
334 c_rt_shadowtris += numtris;
335 // decrement stencil if frontface is behind depthbuffer
336 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
337 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
339 R_Mesh_Draw(numverts, numtris, elements);
341 c_rt_shadowtris += numtris;
344 void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
347 if (r_shadowstage == SHADOWSTAGE_STENCIL)
349 // increment stencil if backface is behind depthbuffer
350 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
351 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
352 for (mesh = firstmesh;mesh;mesh = mesh->next)
354 R_Mesh_ResizeCheck(mesh->numverts);
355 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
356 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
357 c_rtcached_shadowmeshes++;
358 c_rtcached_shadowtris += mesh->numtriangles;
360 // decrement stencil if frontface is behind depthbuffer
361 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
362 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
364 for (mesh = firstmesh;mesh;mesh = mesh->next)
366 R_Mesh_ResizeCheck(mesh->numverts);
367 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
368 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
369 c_rtcached_shadowmeshes++;
370 c_rtcached_shadowtris += mesh->numtriangles;
374 float r_shadow_attenpower, r_shadow_attenscale;
375 static void R_Shadow_MakeTextures(void)
377 int x, y, z, d, side;
378 float v[3], s, t, intensity;
380 R_FreeTexturePool(&r_shadow_texturepool);
381 r_shadow_texturepool = R_AllocTexturePool();
382 r_shadow_attenpower = r_shadow_lightattenuationpower.value;
383 r_shadow_attenscale = r_shadow_lightattenuationscale.value;
385 #define ATTEN2DSIZE 64
386 #define ATTEN3DSIZE 32
387 data = Mem_Alloc(tempmempool, max(6*NORMSIZE*NORMSIZE*4, max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE*4, ATTEN2DSIZE*ATTEN2DSIZE*4)));
392 r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
397 r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
402 r_shadow_blankwhitetexture = R_LoadTexture2D(r_shadow_texturepool, "blankwhite", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
403 for (side = 0;side < 6;side++)
405 for (y = 0;y < NORMSIZE;y++)
407 for (x = 0;x < NORMSIZE;x++)
409 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
410 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
444 intensity = 127.0f / sqrt(DotProduct(v, v));
445 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+0] = 128.0f + intensity * v[0];
446 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+1] = 128.0f + intensity * v[1];
447 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+2] = 128.0f + intensity * v[2];
448 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+3] = 255;
452 r_shadow_normalscubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalscube", NORMSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
453 for (y = 0;y < ATTEN2DSIZE;y++)
455 for (x = 0;x < ATTEN2DSIZE;x++)
457 v[0] = ((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
458 v[1] = ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
460 intensity = 1.0f - sqrt(DotProduct(v, v));
462 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
463 d = bound(0, intensity, 255);
464 data[(y*ATTEN2DSIZE+x)*4+0] = d;
465 data[(y*ATTEN2DSIZE+x)*4+1] = d;
466 data[(y*ATTEN2DSIZE+x)*4+2] = d;
467 data[(y*ATTEN2DSIZE+x)*4+3] = d;
470 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
471 if (r_shadow_texture3d.integer)
473 for (z = 0;z < ATTEN3DSIZE;z++)
475 for (y = 0;y < ATTEN3DSIZE;y++)
477 for (x = 0;x < ATTEN3DSIZE;x++)
479 v[0] = ((x + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
480 v[1] = ((y + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
481 v[2] = ((z + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
482 intensity = 1.0f - sqrt(DotProduct(v, v));
484 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
485 d = bound(0, intensity, 255);
486 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+0] = d;
487 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+1] = d;
488 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+2] = d;
489 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+3] = d;
493 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
498 void R_Shadow_Stage_Begin(void)
502 if (r_shadow_texture3d.integer && !gl_texture3d)
503 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
505 //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
506 if (!r_shadow_attenuation2dtexture
507 || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
508 || r_shadow_lightattenuationpower.value != r_shadow_attenpower
509 || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
510 R_Shadow_MakeTextures();
511 if (r_shadow_reloadlights && cl.worldmodel)
513 R_Shadow_ClearWorldLights();
514 r_shadow_reloadlights = false;
515 R_Shadow_LoadWorldLights();
516 if (r_shadow_worldlightchain == NULL)
518 R_Shadow_LoadLightsFile();
519 if (r_shadow_worldlightchain == NULL)
520 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
524 memset(&m, 0, sizeof(m));
525 m.blendfunc1 = GL_ONE;
526 m.blendfunc2 = GL_ZERO;
528 GL_Color(0, 0, 0, 1);
529 r_shadowstage = SHADOWSTAGE_NONE;
531 c_rt_lights = c_rt_clears = c_rt_scissored = 0;
532 c_rt_shadowmeshes = c_rt_shadowtris = c_rt_lightmeshes = c_rt_lighttris = 0;
533 c_rtcached_shadowmeshes = c_rtcached_shadowtris = 0;
536 void R_Shadow_Stage_ShadowVolumes(void)
539 memset(&m, 0, sizeof(m));
540 R_Mesh_TextureState(&m);
541 GL_Color(1, 1, 1, 1);
542 qglColorMask(0, 0, 0, 0);
543 qglDisable(GL_BLEND);
545 qglDepthFunc(GL_LESS);
546 qglEnable(GL_STENCIL_TEST);
547 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
548 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
549 qglEnable(GL_CULL_FACE);
550 qglEnable(GL_DEPTH_TEST);
551 r_shadowstage = SHADOWSTAGE_STENCIL;
552 qglClear(GL_STENCIL_BUFFER_BIT);
554 // LordHavoc note: many shadow volumes reside entirely inside the world
555 // (that is to say they are entirely bounded by their lit surfaces),
556 // which can be optimized by handling things as an inverted light volume,
557 // with the shadow boundaries of the world being simulated by an altered
558 // (129) bias to stencil clearing on such lights
559 // FIXME: generate inverted light volumes for use as shadow volumes and
560 // optimize for them as noted above
563 void R_Shadow_Stage_LightWithoutShadows(void)
566 memset(&m, 0, sizeof(m));
567 R_Mesh_TextureState(&m);
568 qglActiveTexture(GL_TEXTURE0_ARB);
571 qglBlendFunc(GL_ONE, GL_ONE);
572 GL_Color(1, 1, 1, 1);
573 qglColorMask(1, 1, 1, 1);
575 qglDepthFunc(GL_EQUAL);
576 qglDisable(GL_STENCIL_TEST);
577 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
578 qglStencilFunc(GL_EQUAL, 128, 0xFF);
579 qglEnable(GL_CULL_FACE);
580 qglEnable(GL_DEPTH_TEST);
581 r_shadowstage = SHADOWSTAGE_LIGHT;
585 void R_Shadow_Stage_LightWithShadows(void)
588 memset(&m, 0, sizeof(m));
589 R_Mesh_TextureState(&m);
590 qglActiveTexture(GL_TEXTURE0_ARB);
593 qglBlendFunc(GL_ONE, GL_ONE);
594 GL_Color(1, 1, 1, 1);
595 qglColorMask(1, 1, 1, 1);
597 qglDepthFunc(GL_EQUAL);
598 qglEnable(GL_STENCIL_TEST);
599 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
600 // only draw light where this geometry was already rendered AND the
601 // stencil is 128 (values other than this mean shadow)
602 qglStencilFunc(GL_EQUAL, 128, 0xFF);
603 qglEnable(GL_CULL_FACE);
604 qglEnable(GL_DEPTH_TEST);
605 r_shadowstage = SHADOWSTAGE_LIGHT;
609 void R_Shadow_Stage_End(void)
612 // attempt to restore state to what Mesh_State thinks it is
613 qglDisable(GL_BLEND);
614 qglBlendFunc(GL_ONE, GL_ZERO);
616 // now restore the rest of the state to normal
617 GL_Color(1, 1, 1, 1);
618 qglColorMask(1, 1, 1, 1);
619 qglDisable(GL_SCISSOR_TEST);
620 qglDepthFunc(GL_LEQUAL);
621 qglDisable(GL_STENCIL_TEST);
622 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
623 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
624 qglEnable(GL_CULL_FACE);
625 qglEnable(GL_DEPTH_TEST);
626 // force mesh state to reset by using various combinations of features
627 memset(&m, 0, sizeof(m));
628 m.blendfunc1 = GL_SRC_ALPHA;
629 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
631 m.blendfunc1 = GL_ONE;
632 m.blendfunc2 = GL_ZERO;
634 r_shadowstage = SHADOWSTAGE_NONE;
638 int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const float *origin, float radius)
640 int i, ix1, iy1, ix2, iy2;
641 float x1, y1, x2, y2, x, y;
644 if (!r_shadow_scissor.integer)
646 // if view is inside the box, just say yes it's visible
647 if (r_origin[0] >= mins[0] && r_origin[0] <= maxs[0]
648 && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1]
649 && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2])
651 qglDisable(GL_SCISSOR_TEST);
654 VectorSubtract(r_origin, origin, v);
655 if (DotProduct(v, v) < radius * radius)
657 qglDisable(GL_SCISSOR_TEST);
660 // create viewspace bbox
661 for (i = 0;i < 8;i++)
663 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
664 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
665 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
666 v2[0] = DotProduct(v, vright);
667 v2[1] = DotProduct(v, vup);
668 v2[2] = DotProduct(v, vpn);
671 if (smins[0] > v2[0]) smins[0] = v2[0];
672 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
673 if (smins[1] > v2[1]) smins[1] = v2[1];
674 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
675 if (smins[2] > v2[2]) smins[2] = v2[2];
676 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
680 smins[0] = smaxs[0] = v2[0];
681 smins[1] = smaxs[1] = v2[1];
682 smins[2] = smaxs[2] = v2[2];
685 // now we have a bbox in viewspace
686 // clip it to the viewspace version of the sphere
687 v[0] = origin[0] - r_origin[0];
688 v[1] = origin[1] - r_origin[1];
689 v[2] = origin[2] - r_origin[2];
690 v2[0] = DotProduct(v, vright);
691 v2[1] = DotProduct(v, vup);
692 v2[2] = DotProduct(v, vpn);
693 if (smins[0] < v2[0] - radius) smins[0] = v2[0] - radius;
694 if (smaxs[0] < v2[0] - radius) smaxs[0] = v2[0] + radius;
695 if (smins[1] < v2[1] - radius) smins[1] = v2[1] - radius;
696 if (smaxs[1] < v2[1] - radius) smaxs[1] = v2[1] + radius;
697 if (smins[2] < v2[2] - radius) smins[2] = v2[2] - radius;
698 if (smaxs[2] < v2[2] - radius) smaxs[2] = v2[2] + radius;
699 // clip it to the view plane
702 // return true if that culled the box
703 if (smins[2] >= smaxs[2])
705 // ok some of it is infront of the view, transform each corner back to
706 // worldspace and then to screenspace and make screen rect
707 // initialize these variables just to avoid compiler warnings
708 x1 = y1 = x2 = y2 = 0;
709 for (i = 0;i < 8;i++)
711 v2[0] = (i & 1) ? smins[0] : smaxs[0];
712 v2[1] = (i & 2) ? smins[1] : smaxs[1];
713 v2[2] = (i & 4) ? smins[2] : smaxs[2];
714 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
715 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
716 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
718 GL_TransformToScreen(v, v2);
719 //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]);
736 // this code doesn't handle boxes with any points behind view properly
737 x1 = 1000;x2 = -1000;
738 y1 = 1000;y2 = -1000;
739 for (i = 0;i < 8;i++)
741 v[0] = (i & 1) ? mins[0] : maxs[0];
742 v[1] = (i & 2) ? mins[1] : maxs[1];
743 v[2] = (i & 4) ? mins[2] : maxs[2];
745 GL_TransformToScreen(v, v2);
746 //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]);
763 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
764 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
765 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
766 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
767 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
768 if (ix2 <= ix1 || iy2 <= iy1)
770 // set up the scissor rectangle
771 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
772 qglEnable(GL_SCISSOR_TEST);
778 int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
780 int i, ix1, iy1, ix2, iy2;
781 float x1, y1, x2, y2, x, y, f;
784 if (!r_shadow_scissor.integer)
786 // if view is inside the box, just say yes it's visible
787 if (BoxesOverlap(r_origin, r_origin, mins, maxs))
789 qglDisable(GL_SCISSOR_TEST);
792 for (i = 0;i < 3;i++)
805 f = DotProduct(vpn, r_origin) + 1;
806 if (DotProduct(vpn, v2) <= f)
808 // entirely behind nearclip plane
809 qglDisable(GL_SCISSOR_TEST);
812 if (DotProduct(vpn, v) >= f)
814 // entirely infront of nearclip plane
815 x1 = y1 = x2 = y2 = 0;
816 for (i = 0;i < 8;i++)
818 v[0] = (i & 1) ? mins[0] : maxs[0];
819 v[1] = (i & 2) ? mins[1] : maxs[1];
820 v[2] = (i & 4) ? mins[2] : maxs[2];
822 GL_TransformToScreen(v, v2);
823 //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]);
842 // clipped by nearclip plane
843 // this is nasty and crude...
844 // create viewspace bbox
845 for (i = 0;i < 8;i++)
847 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
848 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
849 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
850 v2[0] = DotProduct(v, vright);
851 v2[1] = DotProduct(v, vup);
852 v2[2] = DotProduct(v, vpn);
855 if (smins[0] > v2[0]) smins[0] = v2[0];
856 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
857 if (smins[1] > v2[1]) smins[1] = v2[1];
858 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
859 if (smins[2] > v2[2]) smins[2] = v2[2];
860 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
864 smins[0] = smaxs[0] = v2[0];
865 smins[1] = smaxs[1] = v2[1];
866 smins[2] = smaxs[2] = v2[2];
869 // now we have a bbox in viewspace
870 // clip it to the view plane
873 // return true if that culled the box
874 if (smins[2] >= smaxs[2])
876 // ok some of it is infront of the view, transform each corner back to
877 // worldspace and then to screenspace and make screen rect
878 // initialize these variables just to avoid compiler warnings
879 x1 = y1 = x2 = y2 = 0;
880 for (i = 0;i < 8;i++)
882 v2[0] = (i & 1) ? smins[0] : smaxs[0];
883 v2[1] = (i & 2) ? smins[1] : smaxs[1];
884 v2[2] = (i & 4) ? smins[2] : smaxs[2];
885 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
886 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
887 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
889 GL_TransformToScreen(v, v2);
890 //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]);
907 // this code doesn't handle boxes with any points behind view properly
908 x1 = 1000;x2 = -1000;
909 y1 = 1000;y2 = -1000;
910 for (i = 0;i < 8;i++)
912 v[0] = (i & 1) ? mins[0] : maxs[0];
913 v[1] = (i & 2) ? mins[1] : maxs[1];
914 v[2] = (i & 4) ? mins[2] : maxs[2];
916 GL_TransformToScreen(v, v2);
917 //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]);
935 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
936 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
937 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
938 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
939 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
940 if (ix2 <= ix1 || iy2 <= iy1)
942 // set up the scissor rectangle
943 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
944 qglEnable(GL_SCISSOR_TEST);
949 void R_Shadow_VertexLighting(float *color, int numverts, const float *vertex, const float *normal, const float *lightcolor, const float *relativelightorigin, float lightradius)
951 float dist, dot, intensity, iradius = 1.0f / lightradius, radius2 = lightradius * lightradius, v[3];
952 for (;numverts > 0;numverts--, vertex += 4, color += 4, normal += 4)
954 VectorSubtract(vertex, relativelightorigin, v);
955 if ((dot = DotProduct(normal, v)) > 0 && (dist = DotProduct(v, v)) < radius2)
958 intensity = pow(1 - (dist * iradius), r_shadow_attenpower) * r_shadow_attenscale * dot / dist;
959 VectorScale(lightcolor, intensity, color);
966 void R_Shadow_VertexLightingWithXYAttenuationTexture(float *color, int numverts, const float *vertex, const float *normal, const float *lightcolor, const float *relativelightorigin, float lightradius, const float *zdir)
968 float dist, dot, intensity, iradius = 1.0f / lightradius, v[3];
969 for (;numverts > 0;numverts--, vertex += 4, color += 4, normal += 4)
971 VectorSubtract(vertex, relativelightorigin, v);
972 if ((dot = DotProduct(normal, v)) > 0 && (dist = fabs(DotProduct(zdir, v))) < lightradius)
974 intensity = pow(1 - (dist * iradius), r_shadow_attenpower) * r_shadow_attenscale * dot / sqrt(DotProduct(v,v));
975 VectorScale(lightcolor, intensity, color);
982 // FIXME: this should be done in a vertex program when possible
983 // FIXME: if vertex program not available, this would really benefit from 3DNow! or SSE
984 void R_Shadow_TransformVertices(float *out, int numverts, const float *vertex, const matrix4x4_t *matrix)
988 Matrix4x4_Transform(matrix, vertex, out);
995 void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin)
999 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
1001 VectorSubtract(vertex, relativelightorigin, lightdir);
1002 // the cubemap normalizes this for us
1003 out[0] = DotProduct(svectors, lightdir);
1004 out[1] = DotProduct(tvectors, lightdir);
1005 out[2] = DotProduct(normals, lightdir);
1009 void R_Shadow_GenTexCoords_Specular_NormalCubeMap(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, const vec3_t relativeeyeorigin)
1012 float lightdir[3], eyedir[3], halfdir[3];
1013 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
1015 VectorSubtract(vertex, relativelightorigin, lightdir);
1016 VectorNormalizeFast(lightdir);
1017 VectorSubtract(vertex, relativeeyeorigin, eyedir);
1018 VectorNormalizeFast(eyedir);
1019 VectorAdd(lightdir, eyedir, halfdir);
1020 // the cubemap normalizes this for us
1021 out[0] = DotProduct(svectors, halfdir);
1022 out[1] = DotProduct(tvectors, halfdir);
1023 out[2] = DotProduct(normals, halfdir);
1027 void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *elements, const float *svectors, const float *tvectors, const float *normals, const float *texcoords, 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)
1032 memset(&m, 0, sizeof(m));
1036 bumptexture = r_shadow_blankbumptexture;
1037 // colorscale accounts for how much we multiply the brightness during combine
1038 // mult is how many times the final pass of the lighting will be
1039 // performed to get more brightness than otherwise possible
1040 // limit mult to 64 for sanity sake
1041 if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
1043 // 3/2 3D combine path
1044 m.tex[0] = R_GetTexture(bumptexture);
1045 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
1046 m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
1047 m.texcombinergb[0] = GL_REPLACE;
1048 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1049 R_Mesh_TextureState(&m);
1050 qglColorMask(0,0,0,1);
1051 qglDisable(GL_BLEND);
1053 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1054 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
1055 R_Shadow_TransformVertices(varray_texcoord[2], numverts, varray_vertex, matrix_modeltoattenuationxyz);
1056 R_Mesh_Draw(numverts, numtriangles, elements);
1058 c_rt_lighttris += numtriangles;
1060 m.tex[0] = R_GetTexture(basetexture);
1062 m.texcubemap[1] = R_GetTexture(lightcubemap);
1064 m.texcombinergb[0] = GL_MODULATE;
1065 m.texcombinergb[1] = GL_MODULATE;
1066 R_Mesh_TextureState(&m);
1067 qglColorMask(1,1,1,0);
1068 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1069 qglEnable(GL_BLEND);
1071 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltofilter);
1073 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1074 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
1076 GL_Color(color[0], color[1], color[2], 1);
1077 R_Mesh_Draw(numverts, numtriangles, elements);
1079 c_rt_lighttris += numtriangles;
1082 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap)
1084 // 1/2/2 3D combine path
1085 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1086 R_Mesh_TextureState(&m);
1087 qglColorMask(0,0,0,1);
1088 qglDisable(GL_BLEND);
1090 R_Shadow_TransformVertices(varray_texcoord[0], numverts, varray_vertex, matrix_modeltoattenuationxyz);
1091 R_Mesh_Draw(numverts, numtriangles, elements);
1093 c_rt_lighttris += numtriangles;
1095 m.tex[0] = R_GetTexture(bumptexture);
1097 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
1098 m.texcombinergb[0] = GL_REPLACE;
1099 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1100 R_Mesh_TextureState(&m);
1101 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1102 qglEnable(GL_BLEND);
1103 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1104 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
1105 R_Mesh_Draw(numverts, numtriangles, elements);
1107 c_rt_lighttris += numtriangles;
1109 m.tex[0] = R_GetTexture(basetexture);
1110 m.texcubemap[1] = R_GetTexture(lightcubemap);
1111 m.texcombinergb[0] = GL_MODULATE;
1112 m.texcombinergb[1] = GL_MODULATE;
1113 R_Mesh_TextureState(&m);
1114 qglColorMask(1,1,1,0);
1115 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1117 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltofilter);
1119 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1120 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
1122 GL_Color(color[0], color[1], color[2], 1);
1123 R_Mesh_Draw(numverts, numtriangles, elements);
1125 c_rt_lighttris += numtriangles;
1128 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap)
1130 // 2/2 3D combine path
1131 m.tex[0] = R_GetTexture(bumptexture);
1133 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
1134 m.texcombinergb[0] = GL_REPLACE;
1135 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1136 R_Mesh_TextureState(&m);
1138 qglColorMask(0,0,0,1);
1139 qglDisable(GL_BLEND);
1140 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1141 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
1142 R_Mesh_Draw(numverts, numtriangles, elements);
1144 c_rt_lighttris += numtriangles;
1146 m.tex[0] = R_GetTexture(basetexture);
1147 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
1148 m.texcombinergb[0] = GL_MODULATE;
1149 m.texcombinergb[1] = GL_MODULATE;
1150 R_Mesh_TextureState(&m);
1151 qglColorMask(1,1,1,0);
1152 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1153 qglEnable(GL_BLEND);
1154 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltoattenuationxyz);
1156 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1157 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
1159 GL_Color(color[0], color[1], color[2], 1);
1160 R_Mesh_Draw(numverts, numtriangles, elements);
1162 c_rt_lighttris += numtriangles;
1165 else if (r_textureunits.integer >= 4)
1167 // 4/2 2D combine path
1168 m.tex[0] = R_GetTexture(bumptexture);
1169 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
1170 m.texcombinergb[0] = GL_REPLACE;
1171 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1172 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
1173 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
1174 R_Mesh_TextureState(&m);
1175 qglColorMask(0,0,0,1);
1176 qglDisable(GL_BLEND);
1178 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1179 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
1180 R_Shadow_TransformVertices(varray_texcoord[2], numverts, varray_vertex, matrix_modeltoattenuationxyz);
1181 R_Shadow_TransformVertices(varray_texcoord[3], numverts, varray_vertex, matrix_modeltoattenuationz);
1182 R_Mesh_Draw(numverts, numtriangles, elements);
1184 c_rt_lighttris += numtriangles;
1186 m.tex[0] = R_GetTexture(basetexture);
1187 m.texcubemap[1] = R_GetTexture(lightcubemap);
1188 m.texcombinergb[0] = GL_MODULATE;
1189 m.texcombinergb[1] = GL_MODULATE;
1192 R_Mesh_TextureState(&m);
1193 qglColorMask(1,1,1,0);
1194 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1195 qglEnable(GL_BLEND);
1197 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltofilter);
1199 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1200 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
1202 GL_Color(color[0], color[1], color[2], 1);
1203 R_Mesh_Draw(numverts, numtriangles, elements);
1205 c_rt_lighttris += numtriangles;
1210 // 2/2/2 2D combine path
1211 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1212 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1213 R_Mesh_TextureState(&m);
1214 qglColorMask(0,0,0,1);
1215 qglDisable(GL_BLEND);
1217 R_Shadow_TransformVertices(varray_texcoord[0], numverts, varray_vertex, matrix_modeltoattenuationxyz);
1218 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltoattenuationz);
1219 R_Mesh_Draw(numverts, numtriangles, elements);
1221 c_rt_lighttris += numtriangles;
1223 m.tex[0] = R_GetTexture(bumptexture);
1225 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
1226 m.texcombinergb[0] = GL_REPLACE;
1227 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1228 R_Mesh_TextureState(&m);
1229 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1230 qglEnable(GL_BLEND);
1231 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1232 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
1233 R_Mesh_Draw(numverts, numtriangles, elements);
1235 c_rt_lighttris += numtriangles;
1237 m.tex[0] = R_GetTexture(basetexture);
1238 m.texcubemap[1] = R_GetTexture(lightcubemap);
1239 m.texcombinergb[0] = GL_MODULATE;
1240 m.texcombinergb[1] = GL_MODULATE;
1241 R_Mesh_TextureState(&m);
1242 qglColorMask(1,1,1,0);
1243 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1245 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltofilter);
1247 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1248 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
1250 GL_Color(color[0], color[1], color[2], 1);
1251 R_Mesh_Draw(numverts, numtriangles, elements);
1253 c_rt_lighttris += numtriangles;
1259 if (r_textureunits.integer >= 2)
1263 m.tex[0] = R_GetTexture(basetexture);
1264 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1265 R_Mesh_TextureState(&m);
1266 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
1267 qglEnable(GL_BLEND);
1269 m.tex[0] = R_GetTexture(basetexture);
1270 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1271 m.blendfunc1 = GL_SRC_ALPHA;
1272 m.blendfunc2 = GL_ONE;
1276 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1277 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltoattenuationxyz);
1278 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1279 R_Shadow_VertexLightingWithXYAttenuationTexture(varray_color, numverts, varray_vertex, normals, color, relativelightorigin, lightradius, matrix_modeltofilter->m[2]);
1280 R_Mesh_Draw(numverts, numtriangles, elements);
1286 m.tex[0] = R_GetTexture(basetexture);
1287 R_Mesh_TextureState(&m);
1288 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
1289 qglEnable(GL_BLEND);
1291 m.tex[0] = R_GetTexture(basetexture);
1292 m.blendfunc1 = GL_SRC_ALPHA;
1293 m.blendfunc2 = GL_ONE;
1297 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1298 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1299 R_Shadow_VertexLighting(varray_color, numverts, varray_vertex, normals, color, relativelightorigin, lightradius);
1300 R_Mesh_Draw(numverts, numtriangles, elements);
1305 void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elements, const float *svectors, const float *tvectors, const float *normals, const float *texcoords, 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)
1312 memset(&m, 0, sizeof(m));
1314 bumptexture = r_shadow_blankbumptexture;
1316 glosstexture = r_shadow_blankglosstexture;
1317 if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
1319 if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1321 // 2/0/0/0/1/2 3D combine blendsquare path
1322 m.tex[0] = R_GetTexture(bumptexture);
1323 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
1324 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1325 R_Mesh_TextureState(&m);
1326 qglColorMask(0,0,0,1);
1327 qglDisable(GL_BLEND);
1329 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1330 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin);
1331 R_Mesh_Draw(numverts, numtriangles, elements);
1333 c_rt_lighttris += numtriangles;
1336 m.texcubemap[1] = 0;
1337 m.texcombinergb[1] = GL_MODULATE;
1338 R_Mesh_TextureState(&m);
1339 // square alpha in framebuffer a few times to make it shiny
1340 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1341 qglEnable(GL_BLEND);
1342 // these comments are a test run through this math for intensity 0.5
1344 R_Mesh_Draw(numverts, numtriangles, elements);
1346 c_rt_lighttris += numtriangles;
1347 // 0.25 * 0.25 = 0.0625
1348 R_Mesh_Draw(numverts, numtriangles, elements);
1350 c_rt_lighttris += numtriangles;
1351 // 0.0625 * 0.0625 = 0.00390625
1352 R_Mesh_Draw(numverts, numtriangles, elements);
1354 c_rt_lighttris += numtriangles;
1356 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1357 R_Mesh_TextureState(&m);
1358 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1359 R_Shadow_TransformVertices(varray_texcoord[0], numverts, varray_vertex, matrix_modeltoattenuationxyz);
1360 R_Mesh_Draw(numverts, numtriangles, elements);
1362 c_rt_lighttris += numtriangles;
1365 m.tex[0] = R_GetTexture(glosstexture);
1366 m.texcubemap[1] = R_GetTexture(lightcubemap);
1367 R_Mesh_TextureState(&m);
1368 qglColorMask(1,1,1,0);
1369 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1370 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1372 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltofilter);
1374 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color);
1375 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
1377 GL_Color(color[0], color[1], color[2], 1);
1378 R_Mesh_Draw(numverts, numtriangles, elements);
1380 c_rt_lighttris += numtriangles;
1383 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1385 // 2/0/0/0/2 3D combine blendsquare path
1386 m.tex[0] = R_GetTexture(bumptexture);
1387 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
1388 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1389 R_Mesh_TextureState(&m);
1390 qglColorMask(0,0,0,1);
1391 qglDisable(GL_BLEND);
1393 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1394 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin);
1395 R_Mesh_Draw(numverts, numtriangles, elements);
1397 c_rt_lighttris += numtriangles;
1400 m.texcubemap[1] = 0;
1401 m.texcombinergb[1] = GL_MODULATE;
1402 R_Mesh_TextureState(&m);
1403 // square alpha in framebuffer a few times to make it shiny
1404 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1405 qglEnable(GL_BLEND);
1406 // these comments are a test run through this math for intensity 0.5
1408 R_Mesh_Draw(numverts, numtriangles, elements);
1410 c_rt_lighttris += numtriangles;
1411 // 0.25 * 0.25 = 0.0625
1412 R_Mesh_Draw(numverts, numtriangles, elements);
1414 c_rt_lighttris += numtriangles;
1415 // 0.0625 * 0.0625 = 0.00390625
1416 R_Mesh_Draw(numverts, numtriangles, elements);
1418 c_rt_lighttris += numtriangles;
1420 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1421 m.tex[1] = R_GetTexture(glosstexture);
1422 R_Mesh_TextureState(&m);
1423 R_Shadow_TransformVertices(varray_texcoord[0], numverts, varray_vertex, matrix_modeltoattenuationxyz);
1424 memcpy(varray_texcoord[1], texcoords, numverts * sizeof(float[4]));
1425 qglColorMask(1,1,1,0);
1426 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1427 R_Mesh_Draw(numverts, numtriangles, elements);
1429 c_rt_lighttris += numtriangles;
1431 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color);
1432 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
1434 GL_Color(color[0], color[1], color[2], 1);
1435 R_Mesh_Draw(numverts, numtriangles, elements);
1437 c_rt_lighttris += numtriangles;
1440 else if (r_textureunits.integer >= 2 /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1442 // 2/0/0/0/2/2 2D combine blendsquare path
1443 m.tex[0] = R_GetTexture(bumptexture);
1444 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
1445 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1446 R_Mesh_TextureState(&m);
1447 qglColorMask(0,0,0,1);
1448 qglDisable(GL_BLEND);
1450 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1451 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin);
1452 R_Mesh_Draw(numverts, numtriangles, elements);
1454 c_rt_lighttris += numtriangles;
1457 m.texcubemap[1] = 0;
1458 m.texcombinergb[1] = GL_MODULATE;
1459 R_Mesh_TextureState(&m);
1460 // square alpha in framebuffer a few times to make it shiny
1461 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1462 qglEnable(GL_BLEND);
1463 // these comments are a test run through this math for intensity 0.5
1465 R_Mesh_Draw(numverts, numtriangles, elements);
1467 c_rt_lighttris += numtriangles;
1468 // 0.25 * 0.25 = 0.0625
1469 R_Mesh_Draw(numverts, numtriangles, elements);
1471 c_rt_lighttris += numtriangles;
1472 // 0.0625 * 0.0625 = 0.00390625
1473 R_Mesh_Draw(numverts, numtriangles, elements);
1475 c_rt_lighttris += numtriangles;
1477 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1478 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1479 R_Mesh_TextureState(&m);
1480 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1481 R_Shadow_TransformVertices(varray_texcoord[0], numverts, varray_vertex, matrix_modeltoattenuationxyz);
1482 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltoattenuationz);
1483 R_Mesh_Draw(numverts, numtriangles, elements);
1485 c_rt_lighttris += numtriangles;
1487 m.tex[0] = R_GetTexture(glosstexture);
1488 m.texcubemap[1] = R_GetTexture(lightcubemap);
1489 R_Mesh_TextureState(&m);
1490 qglColorMask(1,1,1,0);
1491 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1492 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1494 R_Shadow_TransformVertices(varray_texcoord[1], numverts, varray_vertex, matrix_modeltofilter);
1496 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color);
1497 for (renders = 0;renders < 64 && (color[0] > 0 || color[1] > 0 || color[2] > 0);renders++, color[0] = max(0, color[0] - 1.0f), color[1] = max(0, color[1] - 1.0f), color[2] = max(0, color[2] - 1.0f))
1499 GL_Color(color[0], color[1], color[2], 1);
1500 R_Mesh_Draw(numverts, numtriangles, elements);
1502 c_rt_lighttris += numtriangles;
1508 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
1510 R_Mesh_Matrix(matrix);
1511 R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
1514 cvar_t r_editlights = {0, "r_editlights", "0"};
1515 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
1516 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
1517 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
1518 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
1519 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
1520 cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
1521 cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
1522 worldlight_t *r_shadow_worldlightchain;
1523 worldlight_t *r_shadow_selectedlight;
1524 vec3_t r_editlights_cursorlocation;
1526 static int castshadowcount = 1;
1527 void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname, int castshadow)
1529 int i, j, k, l, maxverts, *mark, tris;
1532 shadowmesh_t *mesh, *castmesh;
1536 surfmesh_t *surfmesh;
1538 if (radius < 15 || DotProduct(color, color) < 0.03)
1540 Con_Printf("R_Shadow_NewWorldLight: refusing to create a light too small/dim\n");
1544 e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
1545 VectorCopy(origin, e->origin);
1546 VectorCopy(color, e->light);
1547 e->lightradius = radius;
1549 if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
1551 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
1554 e->castshadows = castshadow;
1556 e->cullradius = e->lightradius;
1557 for (k = 0;k < 3;k++)
1559 e->mins[k] = e->origin[k] - e->lightradius;
1560 e->maxs[k] = e->origin[k] + e->lightradius;
1563 e->next = r_shadow_worldlightchain;
1564 r_shadow_worldlightchain = e;
1565 if (cubemapname && cubemapname[0])
1567 e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
1568 strcpy(e->cubemapname, cubemapname);
1569 // FIXME: add cubemap loading (and don't load a cubemap twice)
1574 i = Mod_PointContents(e->origin, cl.worldmodel);
1575 if (r_shadow_portallight.integer && i != CONTENTS_SOLID && i != CONTENTS_SKY)
1578 qbyte *bytesurfacepvs;
1580 byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->numleafs + 1);
1581 bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->numsurfaces);
1583 Portal_Visibility(cl.worldmodel, e->origin, byteleafpvs, bytesurfacepvs, NULL, 0, true, RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin));
1585 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1586 if (byteleafpvs[i+1] && BoxesOverlap(leaf->mins, leaf->maxs, e->mins, e->maxs))
1587 leaf->worldnodeframe = castshadowcount;
1589 for (i = 0, surf = cl.worldmodel->surfaces;i < cl.worldmodel->numsurfaces;i++, surf++)
1590 if (bytesurfacepvs[i] && BoxesOverlap(surf->poly_mins, surf->poly_maxs, e->mins, e->maxs))
1591 surf->castshadow = castshadowcount;
1593 Mem_Free(byteleafpvs);
1594 Mem_Free(bytesurfacepvs);
1598 leaf = Mod_PointInLeaf(origin, cl.worldmodel);
1599 pvs = Mod_LeafPVS(leaf, cl.worldmodel);
1600 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1602 if (pvs[i >> 3] & (1 << (i & 7)) && BoxesOverlap(leaf->mins, leaf->maxs, e->mins, e->maxs))
1604 leaf->worldnodeframe = castshadowcount;
1605 for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
1607 surf = cl.worldmodel->surfaces + *mark;
1608 if (surf->castshadow != castshadowcount && BoxesOverlap(surf->poly_mins, surf->poly_maxs, e->mins, e->maxs))
1609 surf->castshadow = castshadowcount;
1616 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1617 if (leaf->worldnodeframe == castshadowcount)
1620 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1621 if (surf->castshadow == castshadowcount)
1625 e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
1627 e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
1629 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1630 if (leaf->worldnodeframe == castshadowcount)
1631 e->leafs[e->numleafs++] = leaf;
1633 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1634 if (surf->castshadow == castshadowcount)
1635 e->surfaces[e->numsurfaces++] = surf;
1637 // find bounding box of lit leafs
1638 VectorCopy(e->origin, e->mins);
1639 VectorCopy(e->origin, e->maxs);
1640 for (j = 0;j < e->numleafs;j++)
1643 for (k = 0;k < 3;k++)
1645 if (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
1646 if (e->maxs[k] < leaf->maxs[k]) e->maxs[k] = leaf->maxs[k];
1650 for (k = 0;k < 3;k++)
1652 if (e->mins[k] < e->origin[k] - e->lightradius) e->mins[k] = e->origin[k] - e->lightradius;
1653 if (e->maxs[k] > e->origin[k] + e->lightradius) e->maxs[k] = e->origin[k] + e->lightradius;
1655 e->cullradius = RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin);
1662 for (j = 0;j < e->numsurfaces;j++)
1664 surf = e->surfaces[j];
1665 if (surf->flags & SURF_SHADOWCAST)
1667 surf->castshadow = castshadowcount;
1668 if (maxverts < surf->poly_numverts)
1669 maxverts = surf->poly_numverts;
1672 e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1673 // make a mesh to cast a shadow volume from
1674 castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1675 for (j = 0;j < e->numsurfaces;j++)
1676 if (e->surfaces[j]->castshadow == castshadowcount)
1677 for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
1678 Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->verts, surfmesh->numtriangles, surfmesh->index);
1679 castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
1681 // cast shadow volume from castmesh
1682 for (mesh = castmesh;mesh;mesh = mesh->next)
1684 R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
1685 R_Shadow_ResizeShadowElements(castmesh->numtriangles);
1687 if (maxverts < castmesh->numverts * 2)
1689 maxverts = castmesh->numverts * 2;
1694 if (verts == NULL && maxverts > 0)
1695 verts = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[4]));
1697 // now that we have the buffers big enough, construct shadow volume mesh
1698 memcpy(verts, castmesh->verts, castmesh->numverts * sizeof(float[4]));
1699 R_Shadow_ProjectVertices(verts, castmesh->numverts, e->origin, r_shadow_projectdistance.value);//, e->lightradius);
1700 R_Shadow_MakeTriangleShadowFlags(castmesh->elements, verts, castmesh->numtriangles, trianglefacinglight, e->origin, e->lightradius);
1701 tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->elements, castmesh->neighbors, castmesh->numtriangles, castmesh->numverts, trianglefacinglight, shadowelements);
1702 // add the constructed shadow volume mesh
1703 Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, verts, tris, shadowelements);
1705 // we're done with castmesh now
1706 Mod_ShadowMesh_Free(castmesh);
1707 e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
1708 for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
1709 l += mesh->numtriangles;
1710 Con_Printf("static shadow volume built containing %i triangles\n", l);
1713 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);
1716 void R_Shadow_FreeWorldLight(worldlight_t *light)
1718 worldlight_t **lightpointer;
1719 for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
1720 if (*lightpointer != light)
1721 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
1722 *lightpointer = light->next;
1723 if (light->cubemapname)
1724 Mem_Free(light->cubemapname);
1725 if (light->shadowvolume)
1726 Mod_ShadowMesh_Free(light->shadowvolume);
1727 if (light->surfaces)
1728 Mem_Free(light->surfaces);
1730 Mem_Free(light->leafs);
1734 void R_Shadow_ClearWorldLights(void)
1736 while (r_shadow_worldlightchain)
1737 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
1738 r_shadow_selectedlight = NULL;
1741 void R_Shadow_SelectLight(worldlight_t *light)
1743 if (r_shadow_selectedlight)
1744 r_shadow_selectedlight->selected = false;
1745 r_shadow_selectedlight = light;
1746 if (r_shadow_selectedlight)
1747 r_shadow_selectedlight->selected = true;
1751 void R_DrawLightSprite(int texnum, const vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
1758 VectorSubtract(origin, r_origin, diff);
1759 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
1762 memset(&m, 0, sizeof(m));
1763 m.blendfunc1 = GL_SRC_ALPHA;
1764 m.blendfunc2 = GL_ONE;
1766 R_Mesh_Matrix(&r_identitymatrix);
1769 GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1770 varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
1771 varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
1772 varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
1773 varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
1774 varray_vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
1775 varray_vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
1776 varray_vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
1777 varray_vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
1778 varray_vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
1779 varray_vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
1780 varray_vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
1781 varray_vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
1782 varray_vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
1783 varray_vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
1784 varray_vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
1785 varray_vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
1786 R_Mesh_Draw(4, 2, polygonelements);
1789 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
1792 pic = Draw_CachePic("gfx/crosshair1.tga");
1794 R_DrawLightSprite(R_GetTexture(pic->tex), r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 0.5);
1797 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
1800 const worldlight_t *light;
1803 if (light->selected)
1804 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
1805 if (light->shadowvolume)
1806 R_DrawLightSprite(calldata2, light->origin, 8, intensity, intensity, intensity, 0.5);
1808 R_DrawLightSprite(calldata2, light->origin, 8, intensity * 0.5, intensity * 0.5, intensity * 0.5, 0.5);
1811 void R_Shadow_DrawLightSprites(void)
1815 worldlight_t *light;
1817 for (i = 0;i < 5;i++)
1819 pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1));
1821 texnums[i] = R_GetTexture(pic->tex);
1826 for (light = r_shadow_worldlightchain;light;light = light->next)
1827 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, texnums[((int) light) % 5]);
1828 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
1831 void R_Shadow_SelectLightInView(void)
1833 float bestrating, rating, temp[3];
1834 worldlight_t *best, *light;
1837 for (light = r_shadow_worldlightchain;light;light = light->next)
1839 VectorSubtract(light->origin, r_refdef.vieworg, temp);
1840 rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
1843 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
1844 if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
1846 bestrating = rating;
1851 R_Shadow_SelectLight(best);
1854 void R_Shadow_LoadWorldLights(void)
1856 int n, a, style, shadow;
1857 char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
1858 float origin[3], radius, color[3];
1859 if (cl.worldmodel == NULL)
1861 Con_Printf("No map loaded.\n");
1864 COM_StripExtension(cl.worldmodel->name, name);
1865 strcat(name, ".rtlights");
1866 lightsstring = COM_LoadFile(name, false);
1874 while (*s && *s != '\n')
1880 // check for modifier flags
1886 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);
1892 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);
1895 VectorScale(color, r_editlights_rtlightscolorscale.value, color);
1896 radius *= r_editlights_rtlightssizescale.value;
1897 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadow);
1902 Con_Printf("invalid rtlights file \"%s\"\n", name);
1903 Mem_Free(lightsstring);
1907 void R_Shadow_SaveWorldLights(void)
1909 worldlight_t *light;
1910 int bufchars, bufmaxchars;
1912 char name[MAX_QPATH];
1914 if (!r_shadow_worldlightchain)
1916 if (cl.worldmodel == NULL)
1918 Con_Printf("No map loaded.\n");
1921 COM_StripExtension(cl.worldmodel->name, name);
1922 strcat(name, ".rtlights");
1923 bufchars = bufmaxchars = 0;
1925 for (light = r_shadow_worldlightchain;light;light = light->next)
1927 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 : "");
1928 if (bufchars + strlen(line) > bufmaxchars)
1930 bufmaxchars = bufchars + strlen(line) + 2048;
1932 buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
1936 memcpy(buf, oldbuf, bufchars);
1942 memcpy(buf + bufchars, line, strlen(line));
1943 bufchars += strlen(line);
1947 COM_WriteFile(name, buf, bufchars);
1952 void R_Shadow_LoadLightsFile(void)
1955 char name[MAX_QPATH], *lightsstring, *s, *t;
1956 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
1957 if (cl.worldmodel == NULL)
1959 Con_Printf("No map loaded.\n");
1962 COM_StripExtension(cl.worldmodel->name, name);
1963 strcat(name, ".lights");
1964 lightsstring = COM_LoadFile(name, false);
1972 while (*s && *s != '\n')
1977 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);
1981 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);
1984 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
1985 radius = bound(15, radius, 4096);
1986 VectorScale(color, (2.0f / (8388608.0f)), color);
1987 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
1992 Con_Printf("invalid lights file \"%s\"\n", name);
1993 Mem_Free(lightsstring);
1997 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
1999 int entnum, style, islight;
2000 char key[256], value[1024];
2001 float origin[3], radius, color[3], light, scale, originhack[3], overridecolor[3];
2004 if (cl.worldmodel == NULL)
2006 Con_Printf("No map loaded.\n");
2009 data = cl.worldmodel->entities;
2012 for (entnum = 0;COM_ParseToken(&data) && com_token[0] == '{';entnum++)
2015 origin[0] = origin[1] = origin[2] = 0;
2016 originhack[0] = originhack[1] = originhack[2] = 0;
2017 color[0] = color[1] = color[2] = 1;
2018 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
2024 if (!COM_ParseToken(&data))
2026 if (com_token[0] == '}')
2027 break; // end of entity
2028 if (com_token[0] == '_')
2029 strcpy(key, com_token + 1);
2031 strcpy(key, com_token);
2032 while (key[strlen(key)-1] == ' ') // remove trailing spaces
2033 key[strlen(key)-1] = 0;
2034 if (!COM_ParseToken(&data))
2036 strcpy(value, com_token);
2038 // now that we have the key pair worked out...
2039 if (!strcmp("light", key))
2040 light = atof(value);
2041 else if (!strcmp("origin", key))
2042 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
2043 else if (!strcmp("color", key))
2044 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
2045 else if (!strcmp("wait", key))
2046 scale = atof(value);
2047 else if (!strcmp("classname", key))
2049 if (!strncmp(value, "light", 5))
2052 if (!strcmp(value, "light_fluoro"))
2057 overridecolor[0] = 1;
2058 overridecolor[1] = 1;
2059 overridecolor[2] = 1;
2061 if (!strcmp(value, "light_fluorospark"))
2066 overridecolor[0] = 1;
2067 overridecolor[1] = 1;
2068 overridecolor[2] = 1;
2070 if (!strcmp(value, "light_globe"))
2075 overridecolor[0] = 1;
2076 overridecolor[1] = 0.8;
2077 overridecolor[2] = 0.4;
2079 if (!strcmp(value, "light_flame_large_yellow"))
2084 overridecolor[0] = 1;
2085 overridecolor[1] = 0.5;
2086 overridecolor[2] = 0.1;
2088 if (!strcmp(value, "light_flame_small_yellow"))
2093 overridecolor[0] = 1;
2094 overridecolor[1] = 0.5;
2095 overridecolor[2] = 0.1;
2097 if (!strcmp(value, "light_torch_small_white"))
2102 overridecolor[0] = 1;
2103 overridecolor[1] = 0.5;
2104 overridecolor[2] = 0.1;
2106 if (!strcmp(value, "light_torch_small_walltorch"))
2111 overridecolor[0] = 1;
2112 overridecolor[1] = 0.5;
2113 overridecolor[2] = 0.1;
2117 else if (!strcmp("style", key))
2118 style = atoi(value);
2120 if (light <= 0 && islight)
2122 radius = min(light * r_editlights_quakelightsizescale.value / scale, 1048576);
2123 light = sqrt(bound(0, light, 1048576)) * (1.0f / 16.0f);
2124 if (color[0] == 1 && color[1] == 1 && color[2] == 1)
2125 VectorCopy(overridecolor, color);
2126 VectorScale(color, light, color);
2127 VectorAdd(origin, originhack, origin);
2129 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
2134 void R_Shadow_SetCursorLocationForView(void)
2136 vec_t dist, push, frac;
2137 vec3_t dest, endpos, normal;
2138 VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
2139 frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
2142 dist = frac * r_editlights_cursordistance.value;
2143 push = r_editlights_cursorpushback.value;
2147 VectorMA(endpos, push, vpn, endpos);
2148 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
2150 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2151 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2152 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2155 void R_Shadow_UpdateLightingMode(void)
2157 r_shadow_lightingmode = 0;
2158 if (r_shadow_realtime.integer)
2160 if (r_shadow_worldlightchain)
2161 r_shadow_lightingmode = 2;
2163 r_shadow_lightingmode = 1;
2167 void R_Shadow_UpdateWorldLightSelection(void)
2169 R_Shadow_SetCursorLocationForView();
2170 if (r_editlights.integer)
2172 R_Shadow_SelectLightInView();
2173 R_Shadow_DrawLightSprites();
2176 R_Shadow_SelectLight(NULL);
2179 void R_Shadow_EditLights_Clear_f(void)
2181 R_Shadow_ClearWorldLights();
2184 void R_Shadow_EditLights_Reload_f(void)
2186 r_shadow_reloadlights = true;
2189 void R_Shadow_EditLights_Save_f(void)
2192 R_Shadow_SaveWorldLights();
2195 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
2197 R_Shadow_ClearWorldLights();
2198 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
2201 void R_Shadow_EditLights_ImportLightsFile_f(void)
2203 R_Shadow_ClearWorldLights();
2204 R_Shadow_LoadLightsFile();
2207 void R_Shadow_EditLights_Spawn_f(void)
2210 if (!r_editlights.integer)
2212 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
2215 if (Cmd_Argc() != 1)
2217 Con_Printf("r_editlights_spawn does not take parameters\n");
2220 color[0] = color[1] = color[2] = 1;
2221 R_Shadow_NewWorldLight(r_editlights_cursorlocation, 200, color, 0, NULL, true);
2224 void R_Shadow_EditLights_Edit_f(void)
2226 vec3_t origin, color;
2229 char cubemapname[1024];
2230 if (!r_editlights.integer)
2232 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
2235 if (!r_shadow_selectedlight)
2237 Con_Printf("No selected light.\n");
2240 VectorCopy(r_shadow_selectedlight->origin, origin);
2241 radius = r_shadow_selectedlight->lightradius;
2242 VectorCopy(r_shadow_selectedlight->light, color);
2243 style = r_shadow_selectedlight->style;
2244 if (r_shadow_selectedlight->cubemapname)
2245 strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
2248 shadows = r_shadow_selectedlight->castshadows;
2249 if (!strcmp(Cmd_Argv(1), "origin"))
2251 if (Cmd_Argc() != 5)
2253 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
2256 origin[0] = atof(Cmd_Argv(2));
2257 origin[1] = atof(Cmd_Argv(3));
2258 origin[2] = atof(Cmd_Argv(4));
2260 else if (!strcmp(Cmd_Argv(1), "originx"))
2262 if (Cmd_Argc() != 3)
2264 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2267 origin[0] = atof(Cmd_Argv(2));
2269 else if (!strcmp(Cmd_Argv(1), "originy"))
2271 if (Cmd_Argc() != 3)
2273 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2276 origin[1] = atof(Cmd_Argv(2));
2278 else if (!strcmp(Cmd_Argv(1), "originz"))
2280 if (Cmd_Argc() != 3)
2282 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2285 origin[2] = atof(Cmd_Argv(2));
2287 else if (!strcmp(Cmd_Argv(1), "move"))
2289 if (Cmd_Argc() != 5)
2291 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
2294 origin[0] += atof(Cmd_Argv(2));
2295 origin[1] += atof(Cmd_Argv(3));
2296 origin[2] += atof(Cmd_Argv(4));
2298 else if (!strcmp(Cmd_Argv(1), "movex"))
2300 if (Cmd_Argc() != 3)
2302 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2305 origin[0] += atof(Cmd_Argv(2));
2307 else if (!strcmp(Cmd_Argv(1), "movey"))
2309 if (Cmd_Argc() != 3)
2311 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2314 origin[1] += atof(Cmd_Argv(2));
2316 else if (!strcmp(Cmd_Argv(1), "movez"))
2318 if (Cmd_Argc() != 3)
2320 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2323 origin[2] += atof(Cmd_Argv(2));
2325 else if (!strcmp(Cmd_Argv(1), "color"))
2327 if (Cmd_Argc() != 5)
2329 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(0));
2332 color[0] = atof(Cmd_Argv(2));
2333 color[1] = atof(Cmd_Argv(3));
2334 color[2] = atof(Cmd_Argv(4));
2336 else if (!strcmp(Cmd_Argv(1), "radius"))
2338 if (Cmd_Argc() != 3)
2340 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2343 radius = atof(Cmd_Argv(2));
2345 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "style"))
2347 if (Cmd_Argc() != 3)
2349 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2352 style = atoi(Cmd_Argv(2));
2354 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "cubemap"))
2358 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2361 if (Cmd_Argc() == 3)
2362 strcpy(cubemapname, Cmd_Argv(2));
2366 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "shadows"))
2368 if (Cmd_Argc() != 3)
2370 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2373 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
2377 Con_Printf("usage: r_editlights_edit [property] [value]\n");
2378 Con_Printf("Selected light's properties:\n");
2379 Con_Printf("Origin: %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
2380 Con_Printf("Radius: %f\n", r_shadow_selectedlight->lightradius);
2381 Con_Printf("Color: %f %f %f\n", r_shadow_selectedlight->light[0], r_shadow_selectedlight->light[1], r_shadow_selectedlight->light[2]);
2382 Con_Printf("Style: %i\n", r_shadow_selectedlight->style);
2383 Con_Printf("Cubemap: %s\n", r_shadow_selectedlight->cubemapname);
2384 Con_Printf("Shadows: %s\n", r_shadow_selectedlight->castshadows ? "yes" : "no");
2387 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2388 r_shadow_selectedlight = NULL;
2389 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadows);
2392 extern int con_vislines;
2393 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
2397 if (r_shadow_selectedlight == NULL)
2401 sprintf(temp, "Light properties");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2402 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;
2403 sprintf(temp, "Radius %f", r_shadow_selectedlight->lightradius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2404 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;
2405 sprintf(temp, "Style %i", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2406 sprintf(temp, "Cubemap %s", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2407 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;
2410 void R_Shadow_EditLights_ToggleShadow_f(void)
2412 if (!r_editlights.integer)
2414 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
2417 if (!r_shadow_selectedlight)
2419 Con_Printf("No selected light.\n");
2422 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);
2423 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2424 r_shadow_selectedlight = NULL;
2427 void R_Shadow_EditLights_Remove_f(void)
2429 if (!r_editlights.integer)
2431 Con_Printf("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
2434 if (!r_shadow_selectedlight)
2436 Con_Printf("No selected light.\n");
2439 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2440 r_shadow_selectedlight = NULL;
2443 void R_Shadow_EditLights_Init(void)
2445 Cvar_RegisterVariable(&r_editlights);
2446 Cvar_RegisterVariable(&r_editlights_cursordistance);
2447 Cvar_RegisterVariable(&r_editlights_cursorpushback);
2448 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
2449 Cvar_RegisterVariable(&r_editlights_cursorgrid);
2450 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
2451 Cvar_RegisterVariable(&r_editlights_rtlightssizescale);
2452 Cvar_RegisterVariable(&r_editlights_rtlightscolorscale);
2453 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
2454 Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
2455 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
2456 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
2457 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
2458 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
2459 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f);
2460 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
2461 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);