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_blankbumptexture;
30 rtexture_t *r_shadow_blankglosstexture;
31 rtexture_t *r_shadow_blankwhitetexture;
33 cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5"};
34 cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1"};
35 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
36 cvar_t r_shadow_realtime = {0, "r_shadow_realtime", "0"};
37 cvar_t r_shadow_gloss = {0, "r_shadow_gloss", "1"};
38 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
39 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
40 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
41 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0"};
42 cvar_t r_shadow_shadownudge = {0, "r_shadow_shadownudge", "1"};
43 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1"};
44 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "100000"};
46 int c_rt_lights, c_rt_clears, c_rt_scissored;
47 int c_rt_shadowmeshes, c_rt_shadowtris, c_rt_lightmeshes, c_rt_lighttris;
48 int c_rtcached_shadowmeshes, c_rtcached_shadowtris;
50 void R_Shadow_ClearWorldLights(void);
51 void R_Shadow_SaveWorldLights(void);
52 void R_Shadow_LoadWorldLights(void);
53 void R_Shadow_LoadLightsFile(void);
54 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
56 void r_shadow_start(void)
58 // allocate vertex processing arrays
59 r_shadow_mempool = Mem_AllocPool("R_Shadow");
60 maxshadowelements = 0;
61 shadowelements = NULL;
62 maxtrianglefacinglight = 0;
63 trianglefacinglight = NULL;
64 r_shadow_normalscubetexture = NULL;
65 r_shadow_attenuation2dtexture = NULL;
66 r_shadow_blankbumptexture = NULL;
67 r_shadow_blankglosstexture = NULL;
68 r_shadow_blankwhitetexture = NULL;
69 r_shadow_texturepool = NULL;
70 R_Shadow_ClearWorldLights();
71 r_shadow_reloadlights = true;
74 void r_shadow_shutdown(void)
76 R_Shadow_ClearWorldLights();
77 r_shadow_reloadlights = true;
78 r_shadow_normalscubetexture = NULL;
79 r_shadow_attenuation2dtexture = NULL;
80 r_shadow_blankbumptexture = NULL;
81 r_shadow_blankglosstexture = NULL;
82 r_shadow_blankwhitetexture = NULL;
83 R_FreeTexturePool(&r_shadow_texturepool);
84 maxshadowelements = 0;
85 shadowelements = NULL;
86 maxtrianglefacinglight = 0;
87 trianglefacinglight = NULL;
88 Mem_FreePool(&r_shadow_mempool);
91 void r_shadow_newmap(void)
93 R_Shadow_ClearWorldLights();
94 r_shadow_reloadlights = true;
97 void R_Shadow_Init(void)
99 Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
100 Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
101 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
102 Cvar_RegisterVariable(&r_shadow_realtime);
103 Cvar_RegisterVariable(&r_shadow_gloss);
104 Cvar_RegisterVariable(&r_shadow_debuglight);
105 Cvar_RegisterVariable(&r_shadow_scissor);
106 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
107 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
108 Cvar_RegisterVariable(&r_shadow_shadownudge);
109 Cvar_RegisterVariable(&r_shadow_portallight);
110 Cvar_RegisterVariable(&r_shadow_projectdistance);
111 R_Shadow_EditLights_Init();
112 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
115 void R_Shadow_ProjectVertices(float *verts, int numverts, const float *relativelightorigin, float projectdistance)
118 float *in, *out, diff[4];
120 out = verts + numverts * 4;
121 for (i = 0;i < numverts;i++, in += 4, out += 4)
123 VectorSubtract(in, relativelightorigin, diff);
124 VectorNormalizeFast(diff);
125 VectorMA(in, projectdistance, diff, out);
126 VectorMA(in, r_shadow_shadownudge.value, diff, in);
130 void R_Shadow_MakeTriangleShadowFlags(const int *elements, const float *vertex, int numtris, qbyte *trianglefacinglight, const float *relativelightorigin, float lightradius)
133 const float *v0, *v1, *v2;
134 for (i = 0;i < numtris;i++, elements += 3)
136 // calculate triangle facing flag
137 v0 = vertex + elements[0] * 4;
138 v1 = vertex + elements[1] * 4;
139 v2 = vertex + elements[2] * 4;
140 // we do not need to normalize the surface normal because both sides
141 // of the comparison use it, therefore they are both multiplied the
142 // same amount... furthermore the subtract can be done on the
143 // vectors, saving a little bit of math in the dotproducts
146 // subtracts v1 from v0 and v2, combined into a crossproduct,
147 // combined with a dotproduct of the light location relative to the
148 // first point of the triangle (any point works, since the triangle
149 // is obviously flat), and finally a comparison to determine if the
150 // light is infront of the triangle (the goal of this statement)
151 trianglefacinglight[i] =
152 (relativelightorigin[0] - v0[0]) * ((v0[1] - v1[1]) * (v2[2] - v1[2]) - (v0[2] - v1[2]) * (v2[1] - v1[1]))
153 + (relativelightorigin[1] - v0[1]) * ((v0[2] - v1[2]) * (v2[0] - v1[0]) - (v0[0] - v1[0]) * (v2[2] - v1[2]))
154 + (relativelightorigin[2] - v0[2]) * ((v0[0] - v1[0]) * (v2[1] - v1[1]) - (v0[1] - v1[1]) * (v2[0] - v1[0])) > 0;
158 float dir0[3], dir1[3], temp[3];
160 // calculate two mostly perpendicular edge directions
161 VectorSubtract(v0, v1, dir0);
162 VectorSubtract(v2, v1, dir1);
164 // we have two edge directions, we can calculate a third vector from
165 // them, which is the direction of the surface normal (it's magnitude
167 CrossProduct(dir0, dir1, temp);
169 // this is entirely unnecessary, but kept for clarity
170 //VectorNormalize(temp);
172 // compare distance of light along normal, with distance of any point
173 // of the triangle along the same normal (the triangle is planar,
174 // I.E. flat, so all points give the same answer)
175 // the normal is not normalized because it is used on both sides of
176 // the comparison, so it's magnitude does not matter
177 trianglefacinglight[i] = DotProduct(relativelightorigin, temp) >= DotProduct(v0, temp);
183 int R_Shadow_BuildShadowVolumeTriangles(const int *elements, const int *neighbors, int numtris, int numverts, const qbyte *trianglefacinglight, int *out)
186 // check each frontface for bordering backfaces,
187 // and cast shadow polygons from those edges,
188 // also create front and back caps for shadow volume
190 for (i = 0;i < numtris;i++, elements += 3, neighbors += 3)
192 if (trianglefacinglight[i])
194 // triangle is frontface and therefore casts shadow,
195 // output front and back caps for shadow volume
197 out[0] = elements[0];
198 out[1] = elements[1];
199 out[2] = elements[2];
200 // rear cap (with flipped winding order)
201 out[3] = elements[0] + numverts;
202 out[4] = elements[2] + numverts;
203 out[5] = elements[1] + numverts;
207 if (neighbors[0] < 0 || !trianglefacinglight[neighbors[0]])
209 out[0] = elements[1];
210 out[1] = elements[0];
211 out[2] = elements[0] + numverts;
212 out[3] = elements[1];
213 out[4] = elements[0] + numverts;
214 out[5] = elements[1] + numverts;
218 if (neighbors[1] < 0 || !trianglefacinglight[neighbors[1]])
220 out[0] = elements[2];
221 out[1] = elements[1];
222 out[2] = elements[1] + numverts;
223 out[3] = elements[2];
224 out[4] = elements[1] + numverts;
225 out[5] = elements[2] + numverts;
229 if (neighbors[2] < 0 || !trianglefacinglight[neighbors[2]])
231 out[0] = elements[0];
232 out[1] = elements[2];
233 out[2] = elements[2] + numverts;
234 out[3] = elements[0];
235 out[4] = elements[2] + numverts;
236 out[5] = elements[0] + numverts;
245 void R_Shadow_ResizeTriangleFacingLight(int numtris)
247 // make sure trianglefacinglight is big enough for this volume
248 if (maxtrianglefacinglight < numtris)
250 maxtrianglefacinglight = numtris;
251 if (trianglefacinglight)
252 Mem_Free(trianglefacinglight);
253 trianglefacinglight = Mem_Alloc(r_shadow_mempool, maxtrianglefacinglight);
257 void R_Shadow_ResizeShadowElements(int numtris)
259 // make sure shadowelements is big enough for this volume
260 if (maxshadowelements < numtris * 24)
262 maxshadowelements = numtris * 24;
264 Mem_Free(shadowelements);
265 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
269 void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance)
272 if (projectdistance < 0.1)
274 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
280 // a triangle facing the light source
283 // a triangle not facing the light source
286 // an extrusion of the frontfaces, beginning at the original geometry and
287 // ending further from the light source than the original geometry
288 // (presumably at least as far as the light's radius, if the light has a
289 // radius at all), capped at both front and back to avoid any problems
292 // draws the shadow volumes of the model.
294 // vertex locations must already be in varray_vertex before use.
295 // varray_vertex must have capacity for numverts * 2.
297 // make sure trianglefacinglight is big enough for this volume
298 if (maxtrianglefacinglight < numtris)
299 R_Shadow_ResizeTriangleFacingLight(numtris);
301 // make sure shadowelements is big enough for this volume
302 if (maxshadowelements < numtris * 24)
303 R_Shadow_ResizeShadowElements(numtris);
305 // check which triangles are facing the light
306 R_Shadow_MakeTriangleShadowFlags(elements, varray_vertex, numtris, trianglefacinglight, relativelightorigin, lightradius);
308 // generate projected vertices
309 // by clever use of elements we'll construct the whole shadow from
310 // the unprojected vertices and these projected vertices
311 R_Shadow_ProjectVertices(varray_vertex, numverts, relativelightorigin, projectdistance);
313 // output triangle elements
314 tris = R_Shadow_BuildShadowVolumeTriangles(elements, neighbors, numtris, numverts, trianglefacinglight, shadowelements);
315 R_Shadow_RenderVolume(numverts * 2, tris, shadowelements);
318 void R_Shadow_RenderVolume(int numverts, int numtris, int *elements)
320 if (!numverts || !numtris)
322 if (r_shadowstage == SHADOWSTAGE_STENCIL)
324 // increment stencil if backface is behind depthbuffer
325 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
326 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
327 R_Mesh_Draw(numverts, numtris, elements);
329 c_rt_shadowtris += numtris;
330 // decrement stencil if frontface is behind depthbuffer
331 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
332 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
334 R_Mesh_Draw(numverts, numtris, elements);
336 c_rt_shadowtris += numtris;
339 void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
342 if (r_shadowstage == SHADOWSTAGE_STENCIL)
344 // increment stencil if backface is behind depthbuffer
345 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
346 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
347 for (mesh = firstmesh;mesh;mesh = mesh->next)
349 R_Mesh_ResizeCheck(mesh->numverts);
350 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
351 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
352 c_rtcached_shadowmeshes++;
353 c_rtcached_shadowtris += mesh->numtriangles;
355 // decrement stencil if frontface is behind depthbuffer
356 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
357 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
359 for (mesh = firstmesh;mesh;mesh = mesh->next)
361 R_Mesh_ResizeCheck(mesh->numverts);
362 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
363 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
364 c_rtcached_shadowmeshes++;
365 c_rtcached_shadowtris += mesh->numtriangles;
369 float r_shadow_attenpower, r_shadow_attenscale;
370 static void R_Shadow_MakeTextures(void)
373 float v[3], s, t, intensity;
375 R_FreeTexturePool(&r_shadow_texturepool);
376 r_shadow_texturepool = R_AllocTexturePool();
377 r_shadow_attenpower = r_shadow_lightattenuationpower.value;
378 r_shadow_attenscale = r_shadow_lightattenuationscale.value;
379 data = Mem_Alloc(tempmempool, 6*128*128*4);
384 r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
389 r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
394 r_shadow_blankwhitetexture = R_LoadTexture2D(r_shadow_texturepool, "blankwhite", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
395 for (side = 0;side < 6;side++)
397 for (y = 0;y < 128;y++)
399 for (x = 0;x < 128;x++)
401 s = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
402 t = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
436 intensity = 127.0f / sqrt(DotProduct(v, v));
437 data[((side*128+y)*128+x)*4+0] = 128.0f + intensity * v[0];
438 data[((side*128+y)*128+x)*4+1] = 128.0f + intensity * v[1];
439 data[((side*128+y)*128+x)*4+2] = 128.0f + intensity * v[2];
440 data[((side*128+y)*128+x)*4+3] = 255;
444 r_shadow_normalscubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalscube", 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
445 for (y = 0;y < 128;y++)
447 for (x = 0;x < 128;x++)
449 v[0] = (x + 0.5f) * (2.0f / (128.0f - 8.0f)) - 1.0f;
450 v[1] = (y + 0.5f) * (2.0f / (128.0f - 8.0f)) - 1.0f;
452 intensity = 1.0f - sqrt(DotProduct(v, v));
454 intensity = pow(intensity, r_shadow_attenpower);
455 intensity = bound(0, intensity * r_shadow_attenscale * 256.0f, 255.0f);
456 d = bound(0, intensity, 255);
457 data[((0*128+y)*128+x)*4+0] = d;
458 data[((0*128+y)*128+x)*4+1] = d;
459 data[((0*128+y)*128+x)*4+2] = d;
460 data[((0*128+y)*128+x)*4+3] = d;
463 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", 128, 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
467 void R_Shadow_Stage_Begin(void)
471 //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
472 if (!r_shadow_attenuation2dtexture
473 || r_shadow_lightattenuationpower.value != r_shadow_attenpower
474 || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
475 R_Shadow_MakeTextures();
476 if (r_shadow_reloadlights && cl.worldmodel)
478 R_Shadow_ClearWorldLights();
479 r_shadow_reloadlights = false;
480 R_Shadow_LoadWorldLights();
481 if (r_shadow_worldlightchain == NULL)
483 R_Shadow_LoadLightsFile();
484 if (r_shadow_worldlightchain == NULL)
485 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
489 memset(&m, 0, sizeof(m));
490 m.blendfunc1 = GL_ONE;
491 m.blendfunc2 = GL_ZERO;
493 GL_Color(0, 0, 0, 1);
494 r_shadowstage = SHADOWSTAGE_NONE;
496 c_rt_lights = c_rt_clears = c_rt_scissored = 0;
497 c_rt_shadowmeshes = c_rt_shadowtris = c_rt_lightmeshes = c_rt_lighttris = 0;
498 c_rtcached_shadowmeshes = c_rtcached_shadowtris = 0;
501 void R_Shadow_Stage_ShadowVolumes(void)
504 memset(&m, 0, sizeof(m));
505 R_Mesh_TextureState(&m);
506 GL_Color(1, 1, 1, 1);
507 qglColorMask(0, 0, 0, 0);
508 qglDisable(GL_BLEND);
510 qglDepthFunc(GL_LESS);
511 qglEnable(GL_STENCIL_TEST);
512 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
513 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
514 qglEnable(GL_CULL_FACE);
515 qglEnable(GL_DEPTH_TEST);
516 r_shadowstage = SHADOWSTAGE_STENCIL;
517 qglClear(GL_STENCIL_BUFFER_BIT);
519 // LordHavoc note: many shadow volumes reside entirely inside the world
520 // (that is to say they are entirely bounded by their lit surfaces),
521 // which can be optimized by handling things as an inverted light volume,
522 // with the shadow boundaries of the world being simulated by an altered
523 // (129) bias to stencil clearing on such lights
524 // FIXME: generate inverted light volumes for use as shadow volumes and
525 // optimize for them as noted above
528 void R_Shadow_Stage_LightWithoutShadows(void)
531 memset(&m, 0, sizeof(m));
532 R_Mesh_TextureState(&m);
533 qglActiveTexture(GL_TEXTURE0_ARB);
536 qglBlendFunc(GL_ONE, GL_ONE);
537 GL_Color(1, 1, 1, 1);
538 qglColorMask(1, 1, 1, 1);
540 qglDepthFunc(GL_EQUAL);
541 qglDisable(GL_STENCIL_TEST);
542 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
543 qglStencilFunc(GL_EQUAL, 128, 0xFF);
544 qglEnable(GL_CULL_FACE);
545 qglEnable(GL_DEPTH_TEST);
546 r_shadowstage = SHADOWSTAGE_LIGHT;
550 void R_Shadow_Stage_LightWithShadows(void)
553 memset(&m, 0, sizeof(m));
554 R_Mesh_TextureState(&m);
555 qglActiveTexture(GL_TEXTURE0_ARB);
558 qglBlendFunc(GL_ONE, GL_ONE);
559 GL_Color(1, 1, 1, 1);
560 qglColorMask(1, 1, 1, 1);
562 qglDepthFunc(GL_EQUAL);
563 qglEnable(GL_STENCIL_TEST);
564 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
565 // only draw light where this geometry was already rendered AND the
566 // stencil is 128 (values other than this mean shadow)
567 qglStencilFunc(GL_EQUAL, 128, 0xFF);
568 qglEnable(GL_CULL_FACE);
569 qglEnable(GL_DEPTH_TEST);
570 r_shadowstage = SHADOWSTAGE_LIGHT;
574 void R_Shadow_Stage_End(void)
577 // attempt to restore state to what Mesh_State thinks it is
578 qglDisable(GL_BLEND);
579 qglBlendFunc(GL_ONE, GL_ZERO);
581 // now restore the rest of the state to normal
582 GL_Color(1, 1, 1, 1);
583 qglColorMask(1, 1, 1, 1);
584 qglDisable(GL_SCISSOR_TEST);
585 qglDepthFunc(GL_LEQUAL);
586 qglDisable(GL_STENCIL_TEST);
587 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
588 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
589 qglEnable(GL_CULL_FACE);
590 qglEnable(GL_DEPTH_TEST);
591 // force mesh state to reset by using various combinations of features
592 memset(&m, 0, sizeof(m));
593 m.blendfunc1 = GL_SRC_ALPHA;
594 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
596 m.blendfunc1 = GL_ONE;
597 m.blendfunc2 = GL_ZERO;
599 r_shadowstage = SHADOWSTAGE_NONE;
602 int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const float *origin, float radius)
604 int i, ix1, iy1, ix2, iy2;
605 float x1, y1, x2, y2, x, y;
608 if (!r_shadow_scissor.integer)
610 // if view is inside the box, just say yes it's visible
611 if (r_origin[0] >= mins[0] && r_origin[0] <= maxs[0]
612 && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1]
613 && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2])
615 qglDisable(GL_SCISSOR_TEST);
618 VectorSubtract(r_origin, origin, v);
619 if (DotProduct(v, v) < radius * radius)
621 qglDisable(GL_SCISSOR_TEST);
624 // create viewspace bbox
625 for (i = 0;i < 8;i++)
627 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
628 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
629 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
630 v2[0] = DotProduct(v, vright);
631 v2[1] = DotProduct(v, vup);
632 v2[2] = DotProduct(v, vpn);
635 if (smins[0] > v2[0]) smins[0] = v2[0];
636 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
637 if (smins[1] > v2[1]) smins[1] = v2[1];
638 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
639 if (smins[2] > v2[2]) smins[2] = v2[2];
640 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
644 smins[0] = smaxs[0] = v2[0];
645 smins[1] = smaxs[1] = v2[1];
646 smins[2] = smaxs[2] = v2[2];
649 // now we have a bbox in viewspace
650 // clip it to the viewspace version of the sphere
651 v[0] = origin[0] - r_origin[0];
652 v[1] = origin[1] - r_origin[1];
653 v[2] = origin[2] - r_origin[2];
654 v2[0] = DotProduct(v, vright);
655 v2[1] = DotProduct(v, vup);
656 v2[2] = DotProduct(v, vpn);
657 if (smins[0] < v2[0] - radius) smins[0] = v2[0] - radius;
658 if (smaxs[0] < v2[0] - radius) smaxs[0] = v2[0] + radius;
659 if (smins[1] < v2[1] - radius) smins[1] = v2[1] - radius;
660 if (smaxs[1] < v2[1] - radius) smaxs[1] = v2[1] + radius;
661 if (smins[2] < v2[2] - radius) smins[2] = v2[2] - radius;
662 if (smaxs[2] < v2[2] - radius) smaxs[2] = v2[2] + radius;
663 // clip it to the view plane
666 // return true if that culled the box
667 if (smins[2] >= smaxs[2])
669 // ok some of it is infront of the view, transform each corner back to
670 // worldspace and then to screenspace and make screen rect
671 // initialize these variables just to avoid compiler warnings
672 x1 = y1 = x2 = y2 = 0;
673 for (i = 0;i < 8;i++)
675 v2[0] = (i & 1) ? smins[0] : smaxs[0];
676 v2[1] = (i & 2) ? smins[1] : smaxs[1];
677 v2[2] = (i & 4) ? smins[2] : smaxs[2];
678 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
679 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
680 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
682 GL_TransformToScreen(v, v2);
683 //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]);
700 // this code doesn't handle boxes with any points behind view properly
701 x1 = 1000;x2 = -1000;
702 y1 = 1000;y2 = -1000;
703 for (i = 0;i < 8;i++)
705 v[0] = (i & 1) ? mins[0] : maxs[0];
706 v[1] = (i & 2) ? mins[1] : maxs[1];
707 v[2] = (i & 4) ? mins[2] : maxs[2];
709 GL_TransformToScreen(v, v2);
710 //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]);
727 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
728 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
729 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
730 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
731 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
732 if (ix2 <= ix1 || iy2 <= iy1)
734 // set up the scissor rectangle
735 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
736 qglEnable(GL_SCISSOR_TEST);
741 void R_Shadow_GenTexCoords_Attenuation2D1D(float *out2d, float *out1d, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, float lightradius)
744 float lightvec[3], iradius;
745 iradius = 0.5f / lightradius;
746 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out2d += 4, out1d += 4)
748 VectorSubtract(vertex, relativelightorigin, lightvec);
749 out2d[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
750 out2d[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
752 out1d[0] = 0.5f + DotProduct(normals, lightvec) * iradius;
758 void R_Shadow_GenTexCoords_Diffuse_Attenuation3D(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, float lightradius)
761 float lightvec[3], iradius;
762 iradius = 0.5f / lightradius;
763 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
765 VectorSubtract(vertex, relativelightorigin, lightvec);
766 out[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
767 out[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
768 out[2] = 0.5f + DotProduct(normals, lightvec) * iradius;
772 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)
776 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
778 VectorSubtract(vertex, relativelightorigin, lightdir);
779 // the cubemap normalizes this for us
780 out[0] = DotProduct(svectors, lightdir);
781 out[1] = DotProduct(tvectors, lightdir);
782 out[2] = DotProduct(normals, lightdir);
786 void R_Shadow_GenTexCoords_Specular_Attenuation3D(float *out, int numverts, const float *vertex, const float *svectors, const float *tvectors, const float *normals, const vec3_t relativelightorigin, const vec3_t relativeeyeorigin, float lightradius)
789 float lightdir[3], eyedir[3], halfdir[3], lightdirlen, iradius;
790 iradius = 0.5f / lightradius;
791 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
793 VectorSubtract(vertex, relativelightorigin, lightdir);
794 // this is used later to make the attenuation correct
795 lightdirlen = sqrt(DotProduct(lightdir, lightdir)) * iradius;
796 VectorNormalizeFast(lightdir);
797 VectorSubtract(vertex, relativeeyeorigin, eyedir);
798 VectorNormalizeFast(eyedir);
799 VectorAdd(lightdir, eyedir, halfdir);
800 VectorNormalizeFast(halfdir);
801 out[0] = 0.5f + DotProduct(svectors, halfdir) * lightdirlen;
802 out[1] = 0.5f + DotProduct(tvectors, halfdir) * lightdirlen;
803 out[2] = 0.5f + DotProduct(normals, halfdir) * lightdirlen;
807 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)
810 float lightdir[3], eyedir[3], halfdir[3];
811 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
813 VectorSubtract(vertex, relativelightorigin, lightdir);
814 VectorNormalizeFast(lightdir);
815 VectorSubtract(vertex, relativeeyeorigin, eyedir);
816 VectorNormalizeFast(eyedir);
817 VectorAdd(lightdir, eyedir, halfdir);
818 // the cubemap normalizes this for us
819 out[0] = DotProduct(svectors, halfdir);
820 out[1] = DotProduct(tvectors, halfdir);
821 out[2] = DotProduct(normals, halfdir);
825 void R_Shadow_GenTexCoords_LightCubeMap(float *out, int numverts, const float *vertex, const vec3_t relativelightorigin)
828 // FIXME: this needs to be written
829 // this code assumes the vertices are in worldspace (a false assumption)
830 for (i = 0;i < numverts;i++, vertex += 4, out += 4)
831 VectorSubtract(vertex, relativelightorigin, out);
834 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, rtexture_t *basetexture, rtexture_t *bumptexture, rtexture_t *lightcubemap)
839 memset(&m, 0, sizeof(m));
841 bumptexture = r_shadow_blankbumptexture;
842 // colorscale accounts for how much we multiply the brightness during combine
843 // mult is how many times the final pass of the lighting will be
844 // performed to get more brightness than otherwise possible
845 // limit mult to 64 for sanity sake
846 if (r_textureunits.integer >= 4)
848 // 4 texture no3D combine path, two pass
849 m.tex[0] = R_GetTexture(bumptexture);
850 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
851 m.texcombinergb[0] = GL_REPLACE;
852 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
853 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
854 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
855 R_Mesh_TextureState(&m);
856 qglColorMask(0,0,0,1);
857 qglDisable(GL_BLEND);
859 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
860 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
861 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[2], varray_texcoord[3], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
862 R_Mesh_Draw(numverts, numtriangles, elements);
864 c_rt_lighttris += numtriangles;
866 m.tex[0] = R_GetTexture(basetexture);
867 m.texcubemap[1] = R_GetTexture(lightcubemap);
868 m.texcombinergb[0] = GL_MODULATE;
869 m.texcombinergb[1] = GL_MODULATE;
872 R_Mesh_TextureState(&m);
873 qglColorMask(1,1,1,0);
874 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
877 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
879 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
880 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))
882 GL_Color(color[0], color[1], color[2], 1);
883 R_Mesh_Draw(numverts, numtriangles, elements);
885 c_rt_lighttris += numtriangles;
890 // 2 texture no3D combine path, three pass
891 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
892 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
893 R_Mesh_TextureState(&m);
894 qglColorMask(0,0,0,1);
895 qglDisable(GL_BLEND);
897 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
898 R_Mesh_Draw(numverts, numtriangles, elements);
900 c_rt_lighttris += numtriangles;
902 m.tex[0] = R_GetTexture(bumptexture);
904 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
905 m.texcombinergb[0] = GL_REPLACE;
906 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
907 R_Mesh_TextureState(&m);
908 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
910 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
911 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
912 R_Mesh_Draw(numverts, numtriangles, elements);
914 c_rt_lighttris += numtriangles;
916 m.tex[0] = R_GetTexture(basetexture);
917 m.texcubemap[1] = R_GetTexture(lightcubemap);
918 m.texcombinergb[0] = GL_MODULATE;
919 m.texcombinergb[1] = GL_MODULATE;
920 R_Mesh_TextureState(&m);
921 qglColorMask(1,1,1,0);
922 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
924 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
926 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
927 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))
929 GL_Color(color[0], color[1], color[2], 1);
930 R_Mesh_Draw(numverts, numtriangles, elements);
932 c_rt_lighttris += numtriangles;
937 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, rtexture_t *glosstexture, rtexture_t *bumptexture, rtexture_t *lightcubemap)
942 memset(&m, 0, sizeof(m));
944 bumptexture = r_shadow_blankbumptexture;
946 glosstexture = r_shadow_blankglosstexture;
947 if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
949 // 2 texture no3D combine path, five pass
950 memset(&m, 0, sizeof(m));
952 m.tex[0] = R_GetTexture(bumptexture);
953 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
954 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
955 R_Mesh_TextureState(&m);
956 qglColorMask(0,0,0,1);
957 qglDisable(GL_BLEND);
959 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
960 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin);
961 R_Mesh_Draw(numverts, numtriangles, elements);
963 c_rt_lighttris += numtriangles;
967 m.texcombinergb[1] = GL_MODULATE;
968 R_Mesh_TextureState(&m);
969 // square alpha in framebuffer a few times to make it shiny
970 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
972 // these comments are a test run through this math for intensity 0.5
974 R_Mesh_Draw(numverts, numtriangles, elements);
976 c_rt_lighttris += numtriangles;
977 // 0.25 * 0.25 = 0.0625
978 R_Mesh_Draw(numverts, numtriangles, elements);
980 c_rt_lighttris += numtriangles;
981 // 0.0625 * 0.0625 = 0.00390625
982 R_Mesh_Draw(numverts, numtriangles, elements);
984 c_rt_lighttris += numtriangles;
986 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
987 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
988 R_Mesh_TextureState(&m);
989 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
990 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
991 R_Mesh_Draw(numverts, numtriangles, elements);
993 c_rt_lighttris += numtriangles;
995 m.tex[0] = R_GetTexture(glosstexture);
996 m.texcubemap[1] = R_GetTexture(lightcubemap);
997 R_Mesh_TextureState(&m);
998 qglColorMask(1,1,1,0);
999 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1000 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1002 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
1004 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1005 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))
1007 GL_Color(color[0], color[1], color[2], 1);
1008 R_Mesh_Draw(numverts, numtriangles, elements);
1010 c_rt_lighttris += numtriangles;
1015 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
1017 R_Mesh_Matrix(matrix);
1018 R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
1021 cvar_t r_editlights = {0, "r_editlights", "0"};
1022 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
1023 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
1024 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
1025 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
1026 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
1027 cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
1028 cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
1029 worldlight_t *r_shadow_worldlightchain;
1030 worldlight_t *r_shadow_selectedlight;
1031 vec3_t r_editlights_cursorlocation;
1033 static int castshadowcount = 1;
1034 void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname, int castshadow)
1036 int i, j, k, l, maxverts, *mark, tris;
1037 float *verts, *v, f, temp[3], radius2;
1038 //float projectdistance, *v0, *v1, temp2[3], temp3[3];
1040 shadowmesh_t *mesh, *castmesh;
1044 surfmesh_t *surfmesh;
1046 if (radius < 15 || DotProduct(color, color) < 0.03)
1048 Con_Printf("R_Shadow_NewWorldLight: refusing to create a light too small/dim\n");
1052 e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
1053 VectorCopy(origin, e->origin);
1054 VectorCopy(color, e->light);
1055 e->lightradius = radius;
1057 if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
1059 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
1062 e->castshadows = castshadow;
1064 e->cullradius = e->lightradius;
1065 e->mins[0] = e->origin[0] - e->lightradius;
1066 e->maxs[0] = e->origin[0] + e->lightradius;
1067 e->mins[1] = e->origin[1] - e->lightradius;
1068 e->maxs[1] = e->origin[1] + e->lightradius;
1069 e->mins[2] = e->origin[2] - e->lightradius;
1070 e->maxs[2] = e->origin[2] + e->lightradius;
1072 e->next = r_shadow_worldlightchain;
1073 r_shadow_worldlightchain = e;
1074 if (cubemapname && cubemapname[0])
1076 e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
1077 strcpy(e->cubemapname, cubemapname);
1078 // FIXME: add cubemap loading (and don't load a cubemap twice)
1083 if (r_shadow_portallight.integer)
1086 qbyte *bytesurfacepvs;
1087 byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->numleafs + 1);
1088 bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->numsurfaces);
1089 Portal_Visibility(cl.worldmodel, e->origin, byteleafpvs, bytesurfacepvs, NULL, 0, true, e->lightradius);
1091 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1093 if (byteleafpvs[i+1])
1095 temp[0] = bound(leaf->mins[0], e->origin[0], leaf->maxs[0]) - e->origin[0];
1096 temp[1] = bound(leaf->mins[1], e->origin[1], leaf->maxs[1]) - e->origin[1];
1097 temp[2] = bound(leaf->mins[2], e->origin[2], leaf->maxs[2]) - e->origin[2];
1098 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1099 leaf->worldnodeframe = castshadowcount;
1103 for (i = 0, surf = cl.worldmodel->surfaces;i < cl.worldmodel->numsurfaces;i++, surf++)
1105 if (bytesurfacepvs[i])
1107 f = DotProduct(e->origin, surf->plane->normal) - surf->plane->dist;
1108 if (surf->flags & SURF_PLANEBACK)
1110 if (f > 0 && f < e->lightradius)
1112 temp[0] = bound(surf->poly_mins[0], e->origin[0], surf->poly_maxs[0]) - e->origin[0];
1113 temp[1] = bound(surf->poly_mins[1], e->origin[1], surf->poly_maxs[1]) - e->origin[1];
1114 temp[2] = bound(surf->poly_mins[2], e->origin[2], surf->poly_maxs[2]) - e->origin[2];
1115 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1116 surf->castshadow = castshadowcount;
1121 Mem_Free(byteleafpvs);
1122 Mem_Free(bytesurfacepvs);
1126 leaf = Mod_PointInLeaf(origin, cl.worldmodel);
1127 pvs = Mod_LeafPVS(leaf, cl.worldmodel);
1128 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1130 if (pvs[i >> 3] & (1 << (i & 7)))
1132 VectorCopy(origin, temp);
1133 if (temp[0] < leaf->mins[0]) temp[0] = leaf->mins[0];
1134 if (temp[0] > leaf->maxs[0]) temp[0] = leaf->maxs[0];
1135 if (temp[1] < leaf->mins[1]) temp[1] = leaf->mins[1];
1136 if (temp[1] > leaf->maxs[1]) temp[1] = leaf->maxs[1];
1137 if (temp[2] < leaf->mins[2]) temp[2] = leaf->mins[2];
1138 if (temp[2] > leaf->maxs[2]) temp[2] = leaf->maxs[2];
1139 VectorSubtract(temp, origin, temp);
1140 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1142 leaf->worldnodeframe = castshadowcount;
1143 for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
1145 surf = cl.worldmodel->surfaces + *mark;
1146 if (surf->castshadow != castshadowcount)
1148 f = DotProduct(e->origin, surf->plane->normal) - surf->plane->dist;
1149 if (surf->flags & SURF_PLANEBACK)
1151 if (f > 0 && f < e->lightradius)
1153 temp[0] = bound(surf->poly_mins[0], e->origin[0], surf->poly_maxs[0]) - e->origin[0];
1154 temp[1] = bound(surf->poly_mins[1], e->origin[1], surf->poly_maxs[1]) - e->origin[1];
1155 temp[2] = bound(surf->poly_mins[2], e->origin[2], surf->poly_maxs[2]) - e->origin[2];
1156 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1157 surf->castshadow = castshadowcount;
1167 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1168 if (leaf->worldnodeframe == castshadowcount)
1171 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1172 if (surf->castshadow == castshadowcount)
1176 e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
1178 e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
1180 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1181 if (leaf->worldnodeframe == castshadowcount)
1182 e->leafs[e->numleafs++] = leaf;
1184 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1185 if (surf->castshadow == castshadowcount)
1186 e->surfaces[e->numsurfaces++] = surf;
1187 // find bounding box and sphere of lit surfaces
1188 // (these will be used for creating a shape to clip the light)
1190 VectorCopy(e->origin, e->mins);
1191 VectorCopy(e->origin, e->maxs);
1192 for (j = 0;j < e->numsurfaces;j++)
1194 surf = e->surfaces[j];
1195 for (k = 0, v = surf->poly_verts;k < surf->poly_numverts;k++, v += 3)
1197 if (e->mins[0] > v[0]) e->mins[0] = v[0];if (e->maxs[0] < v[0]) e->maxs[0] = v[0];
1198 if (e->mins[1] > v[1]) e->mins[1] = v[1];if (e->maxs[1] < v[1]) e->maxs[1] = v[1];
1199 if (e->mins[2] > v[2]) e->mins[2] = v[2];if (e->maxs[2] < v[2]) e->maxs[2] = v[2];
1200 VectorSubtract(v, e->origin, temp);
1201 f = DotProduct(temp, temp);
1206 e->cullradius = sqrt(radius2);
1207 if (e->cullradius > e->lightradius)
1208 e->cullradius = e->lightradius;
1209 if (e->mins[0] < e->origin[0] - e->lightradius) e->mins[0] = e->origin[0] - e->lightradius;
1210 if (e->maxs[0] > e->origin[0] + e->lightradius) e->maxs[0] = e->origin[0] + e->lightradius;
1211 if (e->mins[1] < e->origin[1] - e->lightradius) e->mins[1] = e->origin[1] - e->lightradius;
1212 if (e->maxs[1] > e->origin[1] + e->lightradius) e->maxs[1] = e->origin[1] + e->lightradius;
1213 if (e->mins[2] < e->origin[2] - e->lightradius) e->mins[2] = e->origin[2] - e->lightradius;
1214 if (e->maxs[2] > e->origin[2] + e->lightradius) e->maxs[2] = e->origin[2] + e->lightradius;
1221 for (j = 0;j < e->numsurfaces;j++)
1223 surf = e->surfaces[j];
1224 if (surf->flags & SURF_SHADOWCAST)
1226 surf->castshadow = castshadowcount;
1227 if (maxverts < surf->poly_numverts)
1228 maxverts = surf->poly_numverts;
1231 e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1232 // make a mesh to cast a shadow volume from
1233 castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1234 for (j = 0;j < e->numsurfaces;j++)
1235 if (e->surfaces[j]->castshadow == castshadowcount)
1236 for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
1237 Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->verts, surfmesh->numtriangles, surfmesh->index);
1238 castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
1240 // cast shadow volume from castmesh
1241 for (mesh = castmesh;mesh;mesh = mesh->next)
1243 R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
1244 R_Shadow_ResizeShadowElements(castmesh->numtriangles);
1246 if (maxverts < castmesh->numverts * 2)
1248 maxverts = castmesh->numverts * 2;
1253 if (verts == NULL && maxverts > 0)
1254 verts = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[4]));
1256 // now that we have the buffers big enough, construct shadow volume mesh
1257 memcpy(verts, castmesh->verts, castmesh->numverts * sizeof(float[4]));
1258 R_Shadow_ProjectVertices(verts, castmesh->numverts, e->origin, r_shadow_projectdistance.value);//, e->lightradius);
1259 R_Shadow_MakeTriangleShadowFlags(castmesh->elements, verts, castmesh->numtriangles, trianglefacinglight, e->origin, e->lightradius);
1260 tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->elements, castmesh->neighbors, castmesh->numtriangles, castmesh->numverts, trianglefacinglight, shadowelements);
1261 // add the constructed shadow volume mesh
1262 Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, verts, tris, shadowelements);
1264 // we're done with castmesh now
1265 Mod_ShadowMesh_Free(castmesh);
1266 e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
1267 for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
1268 l += mesh->numtriangles;
1269 Con_Printf("static shadow volume built containing %i triangles\n", l);
1272 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);
1275 void R_Shadow_FreeWorldLight(worldlight_t *light)
1277 worldlight_t **lightpointer;
1278 for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
1279 if (*lightpointer != light)
1280 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
1281 *lightpointer = light->next;
1282 if (light->cubemapname)
1283 Mem_Free(light->cubemapname);
1284 if (light->shadowvolume)
1285 Mod_ShadowMesh_Free(light->shadowvolume);
1286 if (light->surfaces)
1287 Mem_Free(light->surfaces);
1289 Mem_Free(light->leafs);
1293 void R_Shadow_ClearWorldLights(void)
1295 while (r_shadow_worldlightchain)
1296 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
1297 r_shadow_selectedlight = NULL;
1300 void R_Shadow_SelectLight(worldlight_t *light)
1302 if (r_shadow_selectedlight)
1303 r_shadow_selectedlight->selected = false;
1304 r_shadow_selectedlight = light;
1305 if (r_shadow_selectedlight)
1306 r_shadow_selectedlight->selected = true;
1310 void R_DrawLightSprite(int texnum, const vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
1317 VectorSubtract(origin, r_origin, diff);
1318 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
1321 memset(&m, 0, sizeof(m));
1322 m.blendfunc1 = GL_SRC_ALPHA;
1323 m.blendfunc2 = GL_ONE;
1325 R_Mesh_Matrix(&r_identitymatrix);
1328 GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1329 varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
1330 varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
1331 varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
1332 varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
1333 varray_vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
1334 varray_vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
1335 varray_vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
1336 varray_vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
1337 varray_vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
1338 varray_vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
1339 varray_vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
1340 varray_vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
1341 varray_vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
1342 varray_vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
1343 varray_vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
1344 varray_vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
1345 R_Mesh_Draw(4, 2, polygonelements);
1348 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
1351 pic = Draw_CachePic("gfx/crosshair1.tga");
1353 R_DrawLightSprite(R_GetTexture(pic->tex), r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 0.5);
1356 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
1359 const worldlight_t *light;
1362 if (light->selected)
1363 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
1364 if (light->shadowvolume)
1365 R_DrawLightSprite(calldata2, light->origin, 8, intensity, intensity, intensity, 0.5);
1367 R_DrawLightSprite(calldata2, light->origin, 8, intensity * 0.5, intensity * 0.5, intensity * 0.5, 0.5);
1370 void R_Shadow_DrawLightSprites(void)
1374 worldlight_t *light;
1376 for (i = 0;i < 5;i++)
1378 pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1));
1380 texnums[i] = R_GetTexture(pic->tex);
1385 for (light = r_shadow_worldlightchain;light;light = light->next)
1386 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, texnums[((int) light) % 5]);
1387 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
1390 void R_Shadow_SelectLightInView(void)
1392 float bestrating, rating, temp[3];
1393 worldlight_t *best, *light;
1396 for (light = r_shadow_worldlightchain;light;light = light->next)
1398 VectorSubtract(light->origin, r_refdef.vieworg, temp);
1399 rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
1402 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
1403 if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
1405 bestrating = rating;
1410 R_Shadow_SelectLight(best);
1413 void R_Shadow_LoadWorldLights(void)
1415 int n, a, style, shadow;
1416 char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
1417 float origin[3], radius, color[3];
1418 if (cl.worldmodel == NULL)
1420 Con_Printf("No map loaded.\n");
1423 COM_StripExtension(cl.worldmodel->name, name);
1424 strcat(name, ".rtlights");
1425 lightsstring = COM_LoadFile(name, false);
1433 while (*s && *s != '\n')
1439 // check for modifier flags
1445 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);
1451 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);
1454 VectorScale(color, r_editlights_rtlightscolorscale.value, color);
1455 radius *= r_editlights_rtlightssizescale.value;
1456 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadow);
1461 Con_Printf("invalid rtlights file \"%s\"\n", name);
1462 Mem_Free(lightsstring);
1466 void R_Shadow_SaveWorldLights(void)
1468 worldlight_t *light;
1469 int bufchars, bufmaxchars;
1471 char name[MAX_QPATH];
1473 if (!r_shadow_worldlightchain)
1475 if (cl.worldmodel == NULL)
1477 Con_Printf("No map loaded.\n");
1480 COM_StripExtension(cl.worldmodel->name, name);
1481 strcat(name, ".rtlights");
1482 bufchars = bufmaxchars = 0;
1484 for (light = r_shadow_worldlightchain;light;light = light->next)
1486 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 : "");
1487 if (bufchars + strlen(line) > bufmaxchars)
1489 bufmaxchars = bufchars + strlen(line) + 2048;
1491 buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
1495 memcpy(buf, oldbuf, bufchars);
1501 memcpy(buf + bufchars, line, strlen(line));
1502 bufchars += strlen(line);
1506 COM_WriteFile(name, buf, bufchars);
1511 void R_Shadow_LoadLightsFile(void)
1514 char name[MAX_QPATH], *lightsstring, *s, *t;
1515 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
1516 if (cl.worldmodel == NULL)
1518 Con_Printf("No map loaded.\n");
1521 COM_StripExtension(cl.worldmodel->name, name);
1522 strcat(name, ".lights");
1523 lightsstring = COM_LoadFile(name, false);
1531 while (*s && *s != '\n')
1536 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);
1540 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);
1543 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
1544 radius = bound(15, radius, 4096);
1545 VectorScale(color, (2.0f / (8388608.0f)), color);
1546 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
1551 Con_Printf("invalid lights file \"%s\"\n", name);
1552 Mem_Free(lightsstring);
1556 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
1558 int entnum, style, islight;
1559 char key[256], value[1024];
1560 float origin[3], radius, color[3], light, scale, originhack[3], overridecolor[3];
1563 if (cl.worldmodel == NULL)
1565 Con_Printf("No map loaded.\n");
1568 data = cl.worldmodel->entities;
1571 for (entnum = 0;COM_ParseToken(&data) && com_token[0] == '{';entnum++)
1574 origin[0] = origin[1] = origin[2] = 0;
1575 originhack[0] = originhack[1] = originhack[2] = 0;
1576 color[0] = color[1] = color[2] = 1;
1577 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
1583 if (!COM_ParseToken(&data))
1585 if (com_token[0] == '}')
1586 break; // end of entity
1587 if (com_token[0] == '_')
1588 strcpy(key, com_token + 1);
1590 strcpy(key, com_token);
1591 while (key[strlen(key)-1] == ' ') // remove trailing spaces
1592 key[strlen(key)-1] = 0;
1593 if (!COM_ParseToken(&data))
1595 strcpy(value, com_token);
1597 // now that we have the key pair worked out...
1598 if (!strcmp("light", key))
1599 light = atof(value);
1600 else if (!strcmp("origin", key))
1601 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
1602 else if (!strcmp("color", key))
1603 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
1604 else if (!strcmp("wait", key))
1605 scale = atof(value);
1606 else if (!strcmp("classname", key))
1608 if (!strncmp(value, "light", 5))
1611 if (!strcmp(value, "light_fluoro"))
1616 overridecolor[0] = 1;
1617 overridecolor[1] = 1;
1618 overridecolor[2] = 1;
1620 if (!strcmp(value, "light_fluorospark"))
1625 overridecolor[0] = 1;
1626 overridecolor[1] = 1;
1627 overridecolor[2] = 1;
1629 if (!strcmp(value, "light_globe"))
1634 overridecolor[0] = 1;
1635 overridecolor[1] = 0.8;
1636 overridecolor[2] = 0.4;
1638 if (!strcmp(value, "light_flame_large_yellow"))
1643 overridecolor[0] = 1;
1644 overridecolor[1] = 0.5;
1645 overridecolor[2] = 0.1;
1647 if (!strcmp(value, "light_flame_small_yellow"))
1652 overridecolor[0] = 1;
1653 overridecolor[1] = 0.5;
1654 overridecolor[2] = 0.1;
1656 if (!strcmp(value, "light_torch_small_white"))
1661 overridecolor[0] = 1;
1662 overridecolor[1] = 0.5;
1663 overridecolor[2] = 0.1;
1665 if (!strcmp(value, "light_torch_small_walltorch"))
1670 overridecolor[0] = 1;
1671 overridecolor[1] = 0.5;
1672 overridecolor[2] = 0.1;
1676 else if (!strcmp("style", key))
1677 style = atoi(value);
1679 if (light <= 0 && islight)
1681 radius = bound(15, light * r_editlights_quakelightsizescale.value / scale, 1048576);
1682 light = sqrt(bound(0, light, 1048576)) * (1.0f / 16.0f);
1683 if (color[0] == 1 && color[1] == 1 && color[2] == 1)
1684 VectorCopy(overridecolor, color);
1685 VectorScale(color, light, color);
1686 VectorAdd(origin, originhack, origin);
1688 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
1693 void R_Shadow_SetCursorLocationForView(void)
1695 vec_t dist, push, frac;
1696 vec3_t dest, endpos, normal;
1697 VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
1698 frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
1701 dist = frac * r_editlights_cursordistance.value;
1702 push = r_editlights_cursorpushback.value;
1706 VectorMA(endpos, push, vpn, endpos);
1707 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
1709 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1710 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1711 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1714 void R_Shadow_UpdateLightingMode(void)
1716 r_shadow_lightingmode = 0;
1717 if (r_shadow_realtime.integer)
1719 if (r_shadow_worldlightchain)
1720 r_shadow_lightingmode = 2;
1722 r_shadow_lightingmode = 1;
1726 void R_Shadow_UpdateWorldLightSelection(void)
1728 R_Shadow_SetCursorLocationForView();
1729 if (r_editlights.integer)
1731 R_Shadow_SelectLightInView();
1732 R_Shadow_DrawLightSprites();
1735 R_Shadow_SelectLight(NULL);
1738 void R_Shadow_EditLights_Clear_f(void)
1740 R_Shadow_ClearWorldLights();
1743 void R_Shadow_EditLights_Reload_f(void)
1745 r_shadow_reloadlights = true;
1748 void R_Shadow_EditLights_Save_f(void)
1751 R_Shadow_SaveWorldLights();
1754 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
1756 R_Shadow_ClearWorldLights();
1757 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
1760 void R_Shadow_EditLights_ImportLightsFile_f(void)
1762 R_Shadow_ClearWorldLights();
1763 R_Shadow_LoadLightsFile();
1766 void R_Shadow_EditLights_Spawn_f(void)
1769 if (!r_editlights.integer)
1771 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1774 if (Cmd_Argc() != 1)
1776 Con_Printf("r_editlights_spawn does not take parameters\n");
1779 color[0] = color[1] = color[2] = 1;
1780 R_Shadow_NewWorldLight(r_editlights_cursorlocation, 200, color, 0, NULL, true);
1783 void R_Shadow_EditLights_Edit_f(void)
1785 vec3_t origin, color;
1788 char cubemapname[1024];
1789 if (!r_editlights.integer)
1791 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1794 if (!r_shadow_selectedlight)
1796 Con_Printf("No selected light.\n");
1799 VectorCopy(r_shadow_selectedlight->origin, origin);
1800 radius = r_shadow_selectedlight->lightradius;
1801 VectorCopy(r_shadow_selectedlight->light, color);
1802 style = r_shadow_selectedlight->style;
1803 if (r_shadow_selectedlight->cubemapname)
1804 strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
1807 shadows = r_shadow_selectedlight->castshadows;
1808 if (!strcmp(Cmd_Argv(1), "origin"))
1810 if (Cmd_Argc() != 5)
1812 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
1815 origin[0] = atof(Cmd_Argv(2));
1816 origin[1] = atof(Cmd_Argv(3));
1817 origin[2] = atof(Cmd_Argv(4));
1819 else if (!strcmp(Cmd_Argv(1), "originx"))
1821 if (Cmd_Argc() != 3)
1823 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1826 origin[0] = atof(Cmd_Argv(2));
1828 else if (!strcmp(Cmd_Argv(1), "originy"))
1830 if (Cmd_Argc() != 3)
1832 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1835 origin[1] = atof(Cmd_Argv(2));
1837 else if (!strcmp(Cmd_Argv(1), "originz"))
1839 if (Cmd_Argc() != 3)
1841 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1844 origin[2] = atof(Cmd_Argv(2));
1846 else if (!strcmp(Cmd_Argv(1), "move"))
1848 if (Cmd_Argc() != 5)
1850 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
1853 origin[0] += atof(Cmd_Argv(2));
1854 origin[1] += atof(Cmd_Argv(3));
1855 origin[2] += atof(Cmd_Argv(4));
1857 else if (!strcmp(Cmd_Argv(1), "movex"))
1859 if (Cmd_Argc() != 3)
1861 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1864 origin[0] += atof(Cmd_Argv(2));
1866 else if (!strcmp(Cmd_Argv(1), "movey"))
1868 if (Cmd_Argc() != 3)
1870 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1873 origin[1] += atof(Cmd_Argv(2));
1875 else if (!strcmp(Cmd_Argv(1), "movez"))
1877 if (Cmd_Argc() != 3)
1879 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1882 origin[2] += atof(Cmd_Argv(2));
1884 else if (!strcmp(Cmd_Argv(1), "color"))
1886 if (Cmd_Argc() != 5)
1888 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(0));
1891 color[0] = atof(Cmd_Argv(2));
1892 color[1] = atof(Cmd_Argv(3));
1893 color[2] = atof(Cmd_Argv(4));
1895 else if (!strcmp(Cmd_Argv(1), "radius"))
1897 if (Cmd_Argc() != 3)
1899 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1902 radius = atof(Cmd_Argv(2));
1904 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "style"))
1906 if (Cmd_Argc() != 3)
1908 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1911 style = atoi(Cmd_Argv(2));
1913 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "cubemap"))
1917 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1920 if (Cmd_Argc() == 3)
1921 strcpy(cubemapname, Cmd_Argv(2));
1925 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "shadows"))
1927 if (Cmd_Argc() != 3)
1929 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1932 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
1936 Con_Printf("usage: r_editlights_edit [property] [value]\n");
1937 Con_Printf("Selected light's properties:\n");
1938 Con_Printf("Origin: %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
1939 Con_Printf("Radius: %f\n", r_shadow_selectedlight->lightradius);
1940 Con_Printf("Color: %f %f %f\n", r_shadow_selectedlight->light[0], r_shadow_selectedlight->light[1], r_shadow_selectedlight->light[2]);
1941 Con_Printf("Style: %i\n", r_shadow_selectedlight->style);
1942 Con_Printf("Cubemap: %s\n", r_shadow_selectedlight->cubemapname);
1943 Con_Printf("Shadows: %s\n", r_shadow_selectedlight->castshadows ? "yes" : "no");
1946 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1947 r_shadow_selectedlight = NULL;
1948 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadows);
1951 extern int con_vislines;
1952 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
1956 if (r_shadow_selectedlight == NULL)
1960 sprintf(temp, "Light properties");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1961 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;
1962 sprintf(temp, "Radius %f", r_shadow_selectedlight->lightradius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1963 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;
1964 sprintf(temp, "Style %i", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1965 sprintf(temp, "Cubemap %s", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1966 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;
1969 void R_Shadow_EditLights_ToggleShadow_f(void)
1971 if (!r_editlights.integer)
1973 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1976 if (!r_shadow_selectedlight)
1978 Con_Printf("No selected light.\n");
1981 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);
1982 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1983 r_shadow_selectedlight = NULL;
1986 void R_Shadow_EditLights_Remove_f(void)
1988 if (!r_editlights.integer)
1990 Con_Printf("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
1993 if (!r_shadow_selectedlight)
1995 Con_Printf("No selected light.\n");
1998 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1999 r_shadow_selectedlight = NULL;
2002 void R_Shadow_EditLights_Init(void)
2004 Cvar_RegisterVariable(&r_editlights);
2005 Cvar_RegisterVariable(&r_editlights_cursordistance);
2006 Cvar_RegisterVariable(&r_editlights_cursorpushback);
2007 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
2008 Cvar_RegisterVariable(&r_editlights_cursorgrid);
2009 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
2010 Cvar_RegisterVariable(&r_editlights_rtlightssizescale);
2011 Cvar_RegisterVariable(&r_editlights_rtlightscolorscale);
2012 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
2013 Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
2014 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
2015 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
2016 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
2017 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
2018 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f);
2019 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
2020 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);