4 #include "cl_collision.h"
6 extern void R_Shadow_EditLights_Init(void);
8 #define SHADOWSTAGE_NONE 0
9 #define SHADOWSTAGE_STENCIL 1
10 #define SHADOWSTAGE_LIGHT 2
11 #define SHADOWSTAGE_ERASESTENCIL 3
13 int r_shadowstage = SHADOWSTAGE_NONE;
14 int r_shadow_reloadlights = false;
16 int r_shadow_lightingmode = 0;
18 mempool_t *r_shadow_mempool;
20 int maxshadowelements;
22 int maxtrianglefacinglight;
23 qbyte *trianglefacinglight;
25 rtexturepool_t *r_shadow_texturepool;
26 rtexture_t *r_shadow_normalscubetexture;
27 rtexture_t *r_shadow_attenuation2dtexture;
28 rtexture_t *r_shadow_blankbumptexture;
29 rtexture_t *r_shadow_blankglosstexture;
30 rtexture_t *r_shadow_blankwhitetexture;
32 cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5"};
33 cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1"};
34 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
35 cvar_t r_shadow_realtime = {0, "r_shadow_realtime", "0"};
36 cvar_t r_shadow_gloss = {0, "r_shadow_gloss", "1"};
37 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
38 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
39 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
40 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0"};
41 cvar_t r_shadow_shadownudge = {0, "r_shadow_shadownudge", "1"};
43 int c_rt_lights, c_rt_clears, c_rt_scissored;
44 int c_rt_shadowmeshes, c_rt_shadowtris, c_rt_lightmeshes, c_rt_lighttris;
45 int c_rtcached_shadowmeshes, c_rtcached_shadowtris;
47 void R_Shadow_ClearWorldLights(void);
48 void R_Shadow_SaveWorldLights(void);
49 void R_Shadow_LoadWorldLights(void);
50 void R_Shadow_LoadLightsFile(void);
51 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
53 void r_shadow_start(void)
55 // allocate vertex processing arrays
56 r_shadow_mempool = Mem_AllocPool("R_Shadow");
57 maxshadowelements = 0;
58 shadowelements = NULL;
59 maxtrianglefacinglight = 0;
60 trianglefacinglight = NULL;
61 r_shadow_normalscubetexture = NULL;
62 r_shadow_attenuation2dtexture = NULL;
63 r_shadow_blankbumptexture = NULL;
64 r_shadow_blankglosstexture = NULL;
65 r_shadow_blankwhitetexture = NULL;
66 r_shadow_texturepool = NULL;
67 R_Shadow_ClearWorldLights();
68 r_shadow_reloadlights = true;
71 void r_shadow_shutdown(void)
73 R_Shadow_ClearWorldLights();
74 r_shadow_reloadlights = true;
75 r_shadow_normalscubetexture = NULL;
76 r_shadow_attenuation2dtexture = NULL;
77 r_shadow_blankbumptexture = NULL;
78 r_shadow_blankglosstexture = NULL;
79 r_shadow_blankwhitetexture = NULL;
80 R_FreeTexturePool(&r_shadow_texturepool);
81 maxshadowelements = 0;
82 shadowelements = NULL;
83 maxtrianglefacinglight = 0;
84 trianglefacinglight = NULL;
85 Mem_FreePool(&r_shadow_mempool);
88 void r_shadow_newmap(void)
90 R_Shadow_ClearWorldLights();
91 r_shadow_reloadlights = true;
94 void R_Shadow_Init(void)
96 Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
97 Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
98 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
99 Cvar_RegisterVariable(&r_shadow_realtime);
100 Cvar_RegisterVariable(&r_shadow_gloss);
101 Cvar_RegisterVariable(&r_shadow_debuglight);
102 Cvar_RegisterVariable(&r_shadow_scissor);
103 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
104 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
105 Cvar_RegisterVariable(&r_shadow_shadownudge);
106 R_Shadow_EditLights_Init();
107 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
110 void R_Shadow_ProjectVertices(float *verts, int numverts, const float *relativelightorigin, float projectdistance)
113 float *in, *out, diff[4];
115 out = verts + numverts * 4;
116 for (i = 0;i < numverts;i++, in += 4, out += 4)
118 VectorSubtract(in, relativelightorigin, diff);
119 VectorNormalizeFast(diff);
120 VectorMA(in, projectdistance, diff, out);
121 VectorMA(in, r_shadow_shadownudge.value, diff, in);
125 void R_Shadow_MakeTriangleShadowFlags(const int *elements, const float *vertex, int numtris, qbyte *trianglefacinglight, const float *relativelightorigin, float lightradius)
128 const float *v0, *v1, *v2;
129 for (i = 0;i < numtris;i++, elements += 3)
131 // calculate triangle facing flag
132 v0 = vertex + elements[0] * 4;
133 v1 = vertex + elements[1] * 4;
134 v2 = vertex + elements[2] * 4;
135 // we do not need to normalize the surface normal because both sides
136 // of the comparison use it, therefore they are both multiplied the
137 // same amount... furthermore the subtract can be done on the
138 // vectors, saving a little bit of math in the dotproducts
141 // subtracts v1 from v0 and v2, combined into a crossproduct,
142 // combined with a dotproduct of the light location relative to the
143 // first point of the triangle (any point works, since the triangle
144 // is obviously flat), and finally a comparison to determine if the
145 // light is infront of the triangle (the goal of this statement)
146 trianglefacinglight[i] =
147 (relativelightorigin[0] - v0[0]) * ((v0[1] - v1[1]) * (v2[2] - v1[2]) - (v0[2] - v1[2]) * (v2[1] - v1[1]))
148 + (relativelightorigin[1] - v0[1]) * ((v0[2] - v1[2]) * (v2[0] - v1[0]) - (v0[0] - v1[0]) * (v2[2] - v1[2]))
149 + (relativelightorigin[2] - v0[2]) * ((v0[0] - v1[0]) * (v2[1] - v1[1]) - (v0[1] - v1[1]) * (v2[0] - v1[0])) > 0;
153 float dir0[3], dir1[3], temp[3];
155 // calculate two mostly perpendicular edge directions
156 VectorSubtract(v0, v1, dir0);
157 VectorSubtract(v2, v1, dir1);
159 // we have two edge directions, we can calculate a third vector from
160 // them, which is the direction of the surface normal (it's magnitude
162 CrossProduct(dir0, dir1, temp);
164 // this is entirely unnecessary, but kept for clarity
165 //VectorNormalize(temp);
167 // compare distance of light along normal, with distance of any point
168 // of the triangle along the same normal (the triangle is planar,
169 // I.E. flat, so all points give the same answer)
170 // the normal is not normalized because it is used on both sides of
171 // the comparison, so it's magnitude does not matter
172 trianglefacinglight[i] = DotProduct(relativelightorigin, temp) >= DotProduct(v0, temp);
178 int R_Shadow_BuildShadowVolumeTriangles(const int *elements, const int *neighbors, int numtris, int numverts, const qbyte *trianglefacinglight, int *out)
181 // check each frontface for bordering backfaces,
182 // and cast shadow polygons from those edges,
183 // also create front and back caps for shadow volume
185 for (i = 0;i < numtris;i++, elements += 3, neighbors += 3)
187 if (trianglefacinglight[i])
189 // triangle is frontface and therefore casts shadow,
190 // output front and back caps for shadow volume
192 out[0] = elements[0];
193 out[1] = elements[1];
194 out[2] = elements[2];
195 // rear cap (with flipped winding order)
196 out[3] = elements[0] + numverts;
197 out[4] = elements[2] + numverts;
198 out[5] = elements[1] + numverts;
202 if (neighbors[0] < 0 || !trianglefacinglight[neighbors[0]])
204 out[0] = elements[1];
205 out[1] = elements[0];
206 out[2] = elements[0] + numverts;
207 out[3] = elements[1];
208 out[4] = elements[0] + numverts;
209 out[5] = elements[1] + numverts;
213 if (neighbors[1] < 0 || !trianglefacinglight[neighbors[1]])
215 out[0] = elements[2];
216 out[1] = elements[1];
217 out[2] = elements[1] + numverts;
218 out[3] = elements[2];
219 out[4] = elements[1] + numverts;
220 out[5] = elements[2] + numverts;
224 if (neighbors[2] < 0 || !trianglefacinglight[neighbors[2]])
226 out[0] = elements[0];
227 out[1] = elements[2];
228 out[2] = elements[2] + numverts;
229 out[3] = elements[0];
230 out[4] = elements[2] + numverts;
231 out[5] = elements[0] + numverts;
240 void R_Shadow_ResizeTriangleFacingLight(int numtris)
242 // make sure trianglefacinglight is big enough for this volume
243 if (maxtrianglefacinglight < numtris)
245 maxtrianglefacinglight = numtris;
246 if (trianglefacinglight)
247 Mem_Free(trianglefacinglight);
248 trianglefacinglight = Mem_Alloc(r_shadow_mempool, maxtrianglefacinglight);
252 void R_Shadow_ResizeShadowElements(int numtris)
254 // make sure shadowelements is big enough for this volume
255 if (maxshadowelements < numtris * 24)
257 maxshadowelements = numtris * 24;
259 Mem_Free(shadowelements);
260 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
264 void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance)
267 if (projectdistance < 0.1)
269 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
275 // a triangle facing the light source
278 // a triangle not facing the light source
281 // an extrusion of the frontfaces, beginning at the original geometry and
282 // ending further from the light source than the original geometry
283 // (presumably at least as far as the light's radius, if the light has a
284 // radius at all), capped at both front and back to avoid any problems
287 // draws the shadow volumes of the model.
289 // vertex locations must already be in varray_vertex before use.
290 // varray_vertex must have capacity for numverts * 2.
292 // make sure trianglefacinglight is big enough for this volume
293 if (maxtrianglefacinglight < numtris)
294 R_Shadow_ResizeTriangleFacingLight(numtris);
296 // make sure shadowelements is big enough for this volume
297 if (maxshadowelements < numtris * 24)
298 R_Shadow_ResizeShadowElements(numtris);
300 // check which triangles are facing the light
301 R_Shadow_MakeTriangleShadowFlags(elements, varray_vertex, numtris, trianglefacinglight, relativelightorigin, lightradius);
303 // generate projected vertices
304 // by clever use of elements we'll construct the whole shadow from
305 // the unprojected vertices and these projected vertices
306 R_Shadow_ProjectVertices(varray_vertex, numverts, relativelightorigin, projectdistance);
308 // output triangle elements
309 tris = R_Shadow_BuildShadowVolumeTriangles(elements, neighbors, numtris, numverts, trianglefacinglight, shadowelements);
310 R_Shadow_RenderVolume(numverts * 2, tris, shadowelements);
313 void R_Shadow_RenderVolume(int numverts, int numtris, int *elements)
315 if (!numverts || !numtris)
317 if (r_shadowstage == SHADOWSTAGE_STENCIL)
319 // increment stencil if backface is behind depthbuffer
320 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
321 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
322 R_Mesh_Draw(numverts, numtris, elements);
324 c_rt_shadowtris += numtris;
325 // decrement stencil if frontface is behind depthbuffer
326 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
327 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
329 R_Mesh_Draw(numverts, numtris, elements);
331 c_rt_shadowtris += numtris;
334 void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
337 if (r_shadowstage == SHADOWSTAGE_STENCIL)
339 // increment stencil if backface is behind depthbuffer
340 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
341 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
342 for (mesh = firstmesh;mesh;mesh = mesh->next)
344 R_Mesh_ResizeCheck(mesh->numverts);
345 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
346 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
347 c_rtcached_shadowmeshes++;
348 c_rtcached_shadowtris += mesh->numtriangles;
350 // decrement stencil if frontface is behind depthbuffer
351 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
352 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
354 for (mesh = firstmesh;mesh;mesh = mesh->next)
356 R_Mesh_ResizeCheck(mesh->numverts);
357 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
358 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
359 c_rtcached_shadowmeshes++;
360 c_rtcached_shadowtris += mesh->numtriangles;
364 float r_shadow_attenpower, r_shadow_attenscale;
365 static void R_Shadow_MakeTextures(void)
368 float v[3], s, t, intensity;
370 R_FreeTexturePool(&r_shadow_texturepool);
371 r_shadow_texturepool = R_AllocTexturePool();
372 r_shadow_attenpower = r_shadow_lightattenuationpower.value;
373 r_shadow_attenscale = r_shadow_lightattenuationscale.value;
374 data = Mem_Alloc(tempmempool, 6*128*128*4);
379 r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
384 r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
389 r_shadow_blankwhitetexture = R_LoadTexture2D(r_shadow_texturepool, "blankwhite", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
390 for (side = 0;side < 6;side++)
392 for (y = 0;y < 128;y++)
394 for (x = 0;x < 128;x++)
396 s = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
397 t = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
431 intensity = 127.0f / sqrt(DotProduct(v, v));
432 data[((side*128+y)*128+x)*4+0] = 128.0f + intensity * v[0];
433 data[((side*128+y)*128+x)*4+1] = 128.0f + intensity * v[1];
434 data[((side*128+y)*128+x)*4+2] = 128.0f + intensity * v[2];
435 data[((side*128+y)*128+x)*4+3] = 255;
439 r_shadow_normalscubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalscube", 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
440 for (y = 0;y < 128;y++)
442 for (x = 0;x < 128;x++)
444 v[0] = (x + 0.5f) * (2.0f / (128.0f - 8.0f)) - 1.0f;
445 v[1] = (y + 0.5f) * (2.0f / (128.0f - 8.0f)) - 1.0f;
447 intensity = 1.0f - sqrt(DotProduct(v, v));
449 intensity = pow(intensity, r_shadow_attenpower);
450 intensity = bound(0, intensity * r_shadow_attenscale * 256.0f, 255.0f);
451 d = bound(0, intensity, 255);
452 data[((0*128+y)*128+x)*4+0] = d;
453 data[((0*128+y)*128+x)*4+1] = d;
454 data[((0*128+y)*128+x)*4+2] = d;
455 data[((0*128+y)*128+x)*4+3] = d;
458 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", 128, 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
462 void R_Shadow_Stage_Begin(void)
466 //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
467 if (!r_shadow_attenuation2dtexture
468 || r_shadow_lightattenuationpower.value != r_shadow_attenpower
469 || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
470 R_Shadow_MakeTextures();
471 if (r_shadow_reloadlights && cl.worldmodel)
473 R_Shadow_ClearWorldLights();
474 r_shadow_reloadlights = false;
475 R_Shadow_LoadWorldLights();
476 if (r_shadow_worldlightchain == NULL)
478 R_Shadow_LoadLightsFile();
479 if (r_shadow_worldlightchain == NULL)
480 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
484 memset(&m, 0, sizeof(m));
485 m.blendfunc1 = GL_ONE;
486 m.blendfunc2 = GL_ZERO;
488 GL_Color(0, 0, 0, 1);
489 r_shadowstage = SHADOWSTAGE_NONE;
491 c_rt_lights = c_rt_clears = c_rt_scissored = 0;
492 c_rt_shadowmeshes = c_rt_shadowtris = c_rt_lightmeshes = c_rt_lighttris = 0;
493 c_rtcached_shadowmeshes = c_rtcached_shadowtris = 0;
496 void R_Shadow_Stage_ShadowVolumes(void)
499 memset(&m, 0, sizeof(m));
500 R_Mesh_TextureState(&m);
501 GL_Color(1, 1, 1, 1);
502 qglColorMask(0, 0, 0, 0);
503 qglDisable(GL_BLEND);
505 qglDepthFunc(GL_LESS);
506 qglEnable(GL_STENCIL_TEST);
507 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
508 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
509 qglEnable(GL_CULL_FACE);
510 qglEnable(GL_DEPTH_TEST);
511 r_shadowstage = SHADOWSTAGE_STENCIL;
512 qglClear(GL_STENCIL_BUFFER_BIT);
514 // LordHavoc note: many shadow volumes reside entirely inside the world
515 // (that is to say they are entirely bounded by their lit surfaces),
516 // which can be optimized by handling things as an inverted light volume,
517 // with the shadow boundaries of the world being simulated by an altered
518 // (129) bias to stencil clearing on such lights
519 // FIXME: generate inverted light volumes for use as shadow volumes and
520 // optimize for them as noted above
523 void R_Shadow_Stage_LightWithoutShadows(void)
526 memset(&m, 0, sizeof(m));
527 R_Mesh_TextureState(&m);
528 qglActiveTexture(GL_TEXTURE0_ARB);
531 qglBlendFunc(GL_ONE, GL_ONE);
532 GL_Color(1, 1, 1, 1);
533 qglColorMask(1, 1, 1, 1);
535 qglDepthFunc(GL_EQUAL);
536 qglDisable(GL_STENCIL_TEST);
537 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
538 qglStencilFunc(GL_EQUAL, 128, 0xFF);
539 qglEnable(GL_CULL_FACE);
540 qglEnable(GL_DEPTH_TEST);
541 r_shadowstage = SHADOWSTAGE_LIGHT;
545 void R_Shadow_Stage_LightWithShadows(void)
548 memset(&m, 0, sizeof(m));
549 R_Mesh_TextureState(&m);
550 qglActiveTexture(GL_TEXTURE0_ARB);
553 qglBlendFunc(GL_ONE, GL_ONE);
554 GL_Color(1, 1, 1, 1);
555 qglColorMask(1, 1, 1, 1);
557 qglDepthFunc(GL_EQUAL);
558 qglEnable(GL_STENCIL_TEST);
559 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
560 // only draw light where this geometry was already rendered AND the
561 // stencil is 128 (values other than this mean shadow)
562 qglStencilFunc(GL_EQUAL, 128, 0xFF);
563 qglEnable(GL_CULL_FACE);
564 qglEnable(GL_DEPTH_TEST);
565 r_shadowstage = SHADOWSTAGE_LIGHT;
569 void R_Shadow_Stage_End(void)
572 // attempt to restore state to what Mesh_State thinks it is
573 qglDisable(GL_BLEND);
574 qglBlendFunc(GL_ONE, GL_ZERO);
576 // now restore the rest of the state to normal
577 GL_Color(1, 1, 1, 1);
578 qglColorMask(1, 1, 1, 1);
579 qglDisable(GL_SCISSOR_TEST);
580 qglDepthFunc(GL_LEQUAL);
581 qglDisable(GL_STENCIL_TEST);
582 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
583 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
584 qglEnable(GL_CULL_FACE);
585 qglEnable(GL_DEPTH_TEST);
586 // force mesh state to reset by using various combinations of features
587 memset(&m, 0, sizeof(m));
588 m.blendfunc1 = GL_SRC_ALPHA;
589 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
591 m.blendfunc1 = GL_ONE;
592 m.blendfunc2 = GL_ZERO;
594 r_shadowstage = SHADOWSTAGE_NONE;
597 int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const float *origin, float radius)
599 int i, ix1, iy1, ix2, iy2;
600 float x1, y1, x2, y2, x, y;
603 if (!r_shadow_scissor.integer)
605 // if view is inside the box, just say yes it's visible
606 if (r_origin[0] >= mins[0] && r_origin[0] <= maxs[0]
607 && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1]
608 && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2])
610 qglDisable(GL_SCISSOR_TEST);
613 VectorSubtract(r_origin, origin, v);
614 if (DotProduct(v, v) < radius * radius)
616 qglDisable(GL_SCISSOR_TEST);
619 // create viewspace bbox
620 for (i = 0;i < 8;i++)
622 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
623 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
624 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
625 v2[0] = DotProduct(v, vright);
626 v2[1] = DotProduct(v, vup);
627 v2[2] = DotProduct(v, vpn);
630 if (smins[0] > v2[0]) smins[0] = v2[0];
631 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
632 if (smins[1] > v2[1]) smins[1] = v2[1];
633 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
634 if (smins[2] > v2[2]) smins[2] = v2[2];
635 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
639 smins[0] = smaxs[0] = v2[0];
640 smins[1] = smaxs[1] = v2[1];
641 smins[2] = smaxs[2] = v2[2];
644 // now we have a bbox in viewspace
645 // clip it to the viewspace version of the sphere
646 v[0] = origin[0] - r_origin[0];
647 v[1] = origin[1] - r_origin[1];
648 v[2] = origin[2] - r_origin[2];
649 v2[0] = DotProduct(v, vright);
650 v2[1] = DotProduct(v, vup);
651 v2[2] = DotProduct(v, vpn);
652 if (smins[0] < v2[0] - radius) smins[0] = v2[0] - radius;
653 if (smaxs[0] < v2[0] - radius) smaxs[0] = v2[0] + radius;
654 if (smins[1] < v2[1] - radius) smins[1] = v2[1] - radius;
655 if (smaxs[1] < v2[1] - radius) smaxs[1] = v2[1] + radius;
656 if (smins[2] < v2[2] - radius) smins[2] = v2[2] - radius;
657 if (smaxs[2] < v2[2] - radius) smaxs[2] = v2[2] + radius;
658 // clip it to the view plane
661 // return true if that culled the box
662 if (smins[2] >= smaxs[2])
664 // ok some of it is infront of the view, transform each corner back to
665 // worldspace and then to screenspace and make screen rect
666 // initialize these variables just to avoid compiler warnings
667 x1 = y1 = x2 = y2 = 0;
668 for (i = 0;i < 8;i++)
670 v2[0] = (i & 1) ? smins[0] : smaxs[0];
671 v2[1] = (i & 2) ? smins[1] : smaxs[1];
672 v2[2] = (i & 4) ? smins[2] : smaxs[2];
673 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
674 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
675 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
677 GL_TransformToScreen(v, v2);
678 //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]);
695 // this code doesn't handle boxes with any points behind view properly
696 x1 = 1000;x2 = -1000;
697 y1 = 1000;y2 = -1000;
698 for (i = 0;i < 8;i++)
700 v[0] = (i & 1) ? mins[0] : maxs[0];
701 v[1] = (i & 2) ? mins[1] : maxs[1];
702 v[2] = (i & 4) ? mins[2] : maxs[2];
704 GL_TransformToScreen(v, v2);
705 //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]);
722 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
723 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
724 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
725 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
726 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
727 if (ix2 <= ix1 || iy2 <= iy1)
729 // set up the scissor rectangle
730 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
731 qglEnable(GL_SCISSOR_TEST);
736 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)
739 float lightvec[3], iradius;
740 iradius = 0.5f / lightradius;
741 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out2d += 4, out1d += 4)
743 VectorSubtract(vertex, relativelightorigin, lightvec);
744 out2d[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
745 out2d[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
747 out1d[0] = 0.5f + DotProduct(normals, lightvec) * iradius;
753 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)
756 float lightvec[3], iradius;
757 iradius = 0.5f / lightradius;
758 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
760 VectorSubtract(vertex, relativelightorigin, lightvec);
761 out[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
762 out[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
763 out[2] = 0.5f + DotProduct(normals, lightvec) * iradius;
767 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)
771 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
773 VectorSubtract(vertex, relativelightorigin, lightdir);
774 // the cubemap normalizes this for us
775 out[0] = DotProduct(svectors, lightdir);
776 out[1] = DotProduct(tvectors, lightdir);
777 out[2] = DotProduct(normals, lightdir);
781 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)
784 float lightdir[3], eyedir[3], halfdir[3], lightdirlen, iradius;
785 iradius = 0.5f / lightradius;
786 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
788 VectorSubtract(vertex, relativelightorigin, lightdir);
789 // this is used later to make the attenuation correct
790 lightdirlen = sqrt(DotProduct(lightdir, lightdir)) * iradius;
791 VectorNormalizeFast(lightdir);
792 VectorSubtract(vertex, relativeeyeorigin, eyedir);
793 VectorNormalizeFast(eyedir);
794 VectorAdd(lightdir, eyedir, halfdir);
795 VectorNormalizeFast(halfdir);
796 out[0] = 0.5f + DotProduct(svectors, halfdir) * lightdirlen;
797 out[1] = 0.5f + DotProduct(tvectors, halfdir) * lightdirlen;
798 out[2] = 0.5f + DotProduct(normals, halfdir) * lightdirlen;
802 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)
805 float lightdir[3], eyedir[3], halfdir[3];
806 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
808 VectorSubtract(vertex, relativelightorigin, lightdir);
809 VectorNormalizeFast(lightdir);
810 VectorSubtract(vertex, relativeeyeorigin, eyedir);
811 VectorNormalizeFast(eyedir);
812 VectorAdd(lightdir, eyedir, halfdir);
813 // the cubemap normalizes this for us
814 out[0] = DotProduct(svectors, halfdir);
815 out[1] = DotProduct(tvectors, halfdir);
816 out[2] = DotProduct(normals, halfdir);
820 void R_Shadow_GenTexCoords_LightCubeMap(float *out, int numverts, const float *vertex, const vec3_t relativelightorigin)
823 // FIXME: this needs to be written
824 // this code assumes the vertices are in worldspace (a false assumption)
825 for (i = 0;i < numverts;i++, vertex += 4, out += 4)
826 VectorSubtract(vertex, relativelightorigin, out);
829 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)
834 memset(&m, 0, sizeof(m));
836 bumptexture = r_shadow_blankbumptexture;
837 // colorscale accounts for how much we multiply the brightness during combine
838 // mult is how many times the final pass of the lighting will be
839 // performed to get more brightness than otherwise possible
840 // limit mult to 64 for sanity sake
841 if (r_textureunits.integer >= 4)
843 // 4 texture no3D combine path, two pass
844 m.tex[0] = R_GetTexture(bumptexture);
845 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
846 m.texcombinergb[0] = GL_REPLACE;
847 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
848 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
849 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
850 R_Mesh_TextureState(&m);
851 qglColorMask(0,0,0,1);
852 qglDisable(GL_BLEND);
854 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
855 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
856 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[2], varray_texcoord[3], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
857 R_Mesh_Draw(numverts, numtriangles, elements);
859 c_rt_lighttris += numtriangles;
861 m.tex[0] = R_GetTexture(basetexture);
862 m.texcubemap[1] = R_GetTexture(lightcubemap);
863 m.texcombinergb[0] = GL_MODULATE;
864 m.texcombinergb[1] = GL_MODULATE;
867 R_Mesh_TextureState(&m);
868 qglColorMask(1,1,1,0);
869 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
872 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
874 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
875 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))
877 GL_Color(color[0], color[1], color[2], 1);
878 R_Mesh_Draw(numverts, numtriangles, elements);
880 c_rt_lighttris += numtriangles;
885 // 2 texture no3D combine path, three pass
886 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
887 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
888 R_Mesh_TextureState(&m);
889 qglColorMask(0,0,0,1);
890 qglDisable(GL_BLEND);
892 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
893 R_Mesh_Draw(numverts, numtriangles, elements);
895 c_rt_lighttris += numtriangles;
897 m.tex[0] = R_GetTexture(bumptexture);
899 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
900 m.texcombinergb[0] = GL_REPLACE;
901 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
902 R_Mesh_TextureState(&m);
903 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
905 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
906 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
907 R_Mesh_Draw(numverts, numtriangles, elements);
909 c_rt_lighttris += numtriangles;
911 m.tex[0] = R_GetTexture(basetexture);
912 m.texcubemap[1] = R_GetTexture(lightcubemap);
913 m.texcombinergb[0] = GL_MODULATE;
914 m.texcombinergb[1] = GL_MODULATE;
915 R_Mesh_TextureState(&m);
916 qglColorMask(1,1,1,0);
917 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
919 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
921 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
922 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))
924 GL_Color(color[0], color[1], color[2], 1);
925 R_Mesh_Draw(numverts, numtriangles, elements);
927 c_rt_lighttris += numtriangles;
932 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)
937 memset(&m, 0, sizeof(m));
939 bumptexture = r_shadow_blankbumptexture;
941 glosstexture = r_shadow_blankglosstexture;
942 if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
944 // 2 texture no3D combine path, five pass
945 memset(&m, 0, sizeof(m));
947 m.tex[0] = R_GetTexture(bumptexture);
948 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
949 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
950 R_Mesh_TextureState(&m);
951 qglColorMask(0,0,0,1);
952 qglDisable(GL_BLEND);
954 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
955 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin);
956 R_Mesh_Draw(numverts, numtriangles, elements);
958 c_rt_lighttris += numtriangles;
962 m.texcombinergb[1] = GL_MODULATE;
963 R_Mesh_TextureState(&m);
964 // square alpha in framebuffer a few times to make it shiny
965 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
967 // these comments are a test run through this math for intensity 0.5
969 R_Mesh_Draw(numverts, numtriangles, elements);
971 c_rt_lighttris += numtriangles;
972 // 0.25 * 0.25 = 0.0625
973 R_Mesh_Draw(numverts, numtriangles, elements);
975 c_rt_lighttris += numtriangles;
976 // 0.0625 * 0.0625 = 0.00390625
977 R_Mesh_Draw(numverts, numtriangles, elements);
979 c_rt_lighttris += numtriangles;
981 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
982 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
983 R_Mesh_TextureState(&m);
984 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
985 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
986 R_Mesh_Draw(numverts, numtriangles, elements);
988 c_rt_lighttris += numtriangles;
990 m.tex[0] = R_GetTexture(glosstexture);
991 m.texcubemap[1] = R_GetTexture(lightcubemap);
992 R_Mesh_TextureState(&m);
993 qglColorMask(1,1,1,0);
994 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
995 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
997 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
999 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1000 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))
1002 GL_Color(color[0], color[1], color[2], 1);
1003 R_Mesh_Draw(numverts, numtriangles, elements);
1005 c_rt_lighttris += numtriangles;
1010 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
1012 R_Mesh_Matrix(matrix);
1013 R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
1016 cvar_t r_editlights = {0, "r_editlights", "0"};
1017 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
1018 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
1019 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
1020 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
1021 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
1022 cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
1023 cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
1024 worldlight_t *r_shadow_worldlightchain;
1025 worldlight_t *r_shadow_selectedlight;
1026 vec3_t r_editlights_cursorlocation;
1028 static int castshadowcount = 1;
1029 void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname, int castshadow)
1031 int i, j, k, l, maxverts, *mark, tris;
1032 float *verts, *v, f, temp[3], radius2;
1033 //float projectdistance, *v0, *v1, temp2[3], temp3[3];
1035 shadowmesh_t *mesh, *castmesh;
1039 surfmesh_t *surfmesh;
1041 if (radius < 15 || DotProduct(color, color) < 0.03)
1043 Con_Printf("R_Shadow_NewWorldLight: refusing to create a light too small/dim\n");
1047 e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
1048 VectorCopy(origin, e->origin);
1049 VectorCopy(color, e->light);
1050 e->lightradius = radius;
1052 if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
1054 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be > = 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
1057 e->castshadows = castshadow;
1059 e->cullradius = e->lightradius;
1060 e->mins[0] = e->origin[0] - e->lightradius;
1061 e->maxs[0] = e->origin[0] + e->lightradius;
1062 e->mins[1] = e->origin[1] - e->lightradius;
1063 e->maxs[1] = e->origin[1] + e->lightradius;
1064 e->mins[2] = e->origin[2] - e->lightradius;
1065 e->maxs[2] = e->origin[2] + e->lightradius;
1067 e->next = r_shadow_worldlightchain;
1068 r_shadow_worldlightchain = e;
1069 if (cubemapname && cubemapname[0])
1071 e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
1072 strcpy(e->cubemapname, cubemapname);
1073 // FIXME: add cubemap loading (and don't load a cubemap twice)
1078 leaf = Mod_PointInLeaf(origin, cl.worldmodel);
1079 pvs = Mod_LeafPVS(leaf, cl.worldmodel);
1080 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1082 if (pvs[i >> 3] & (1 << (i & 7)))
1084 VectorCopy(origin, temp);
1085 if (temp[0] < leaf->mins[0]) temp[0] = leaf->mins[0];
1086 if (temp[0] > leaf->maxs[0]) temp[0] = leaf->maxs[0];
1087 if (temp[1] < leaf->mins[1]) temp[1] = leaf->mins[1];
1088 if (temp[1] > leaf->maxs[1]) temp[1] = leaf->maxs[1];
1089 if (temp[2] < leaf->mins[2]) temp[2] = leaf->mins[2];
1090 if (temp[2] > leaf->maxs[2]) temp[2] = leaf->maxs[2];
1091 VectorSubtract(temp, origin, temp);
1092 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1094 leaf->worldnodeframe = castshadowcount;
1095 for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
1097 surf = cl.worldmodel->surfaces + *mark;
1098 if (surf->castshadow != castshadowcount)
1100 f = DotProduct(e->origin, surf->plane->normal) - surf->plane->dist;
1101 if (surf->flags & SURF_PLANEBACK)
1103 if (f > 0 && f < e->lightradius)
1105 temp[0] = bound(surf->poly_mins[0], e->origin[0], surf->poly_maxs[0]) - e->origin[0];
1106 temp[1] = bound(surf->poly_mins[1], e->origin[1], surf->poly_maxs[1]) - e->origin[1];
1107 temp[2] = bound(surf->poly_mins[2], e->origin[2], surf->poly_maxs[2]) - e->origin[2];
1108 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1109 surf->castshadow = castshadowcount;
1118 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1119 if (leaf->worldnodeframe == castshadowcount)
1122 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1123 if (surf->castshadow == castshadowcount)
1127 e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
1129 e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
1131 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1132 if (leaf->worldnodeframe == castshadowcount)
1133 e->leafs[e->numleafs++] = leaf;
1135 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1136 if (surf->castshadow == castshadowcount)
1137 e->surfaces[e->numsurfaces++] = surf;
1138 // find bounding box and sphere of lit surfaces
1139 // (these will be used for creating a shape to clip the light)
1141 VectorCopy(e->origin, e->mins);
1142 VectorCopy(e->origin, e->maxs);
1143 for (j = 0;j < e->numsurfaces;j++)
1145 surf = e->surfaces[j];
1146 for (k = 0, v = surf->poly_verts;k < surf->poly_numverts;k++, v += 3)
1148 if (e->mins[0] > v[0]) e->mins[0] = v[0];if (e->maxs[0] < v[0]) e->maxs[0] = v[0];
1149 if (e->mins[1] > v[1]) e->mins[1] = v[1];if (e->maxs[1] < v[1]) e->maxs[1] = v[1];
1150 if (e->mins[2] > v[2]) e->mins[2] = v[2];if (e->maxs[2] < v[2]) e->maxs[2] = v[2];
1151 VectorSubtract(v, e->origin, temp);
1152 f = DotProduct(temp, temp);
1157 e->cullradius = sqrt(radius2);
1158 if (e->cullradius > e->lightradius)
1159 e->cullradius = e->lightradius;
1160 if (e->mins[0] < e->origin[0] - e->lightradius) e->mins[0] = e->origin[0] - e->lightradius;
1161 if (e->maxs[0] > e->origin[0] + e->lightradius) e->maxs[0] = e->origin[0] + e->lightradius;
1162 if (e->mins[1] < e->origin[1] - e->lightradius) e->mins[1] = e->origin[1] - e->lightradius;
1163 if (e->maxs[1] > e->origin[1] + e->lightradius) e->maxs[1] = e->origin[1] + e->lightradius;
1164 if (e->mins[2] < e->origin[2] - e->lightradius) e->mins[2] = e->origin[2] - e->lightradius;
1165 if (e->maxs[2] > e->origin[2] + e->lightradius) e->maxs[2] = e->origin[2] + e->lightradius;
1172 for (j = 0;j < e->numsurfaces;j++)
1174 surf = e->surfaces[j];
1175 if (surf->flags & SURF_SHADOWCAST)
1177 surf->castshadow = castshadowcount;
1178 if (maxverts < surf->poly_numverts)
1179 maxverts = surf->poly_numverts;
1182 e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1183 // make a mesh to cast a shadow volume from
1184 castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1185 for (j = 0;j < e->numsurfaces;j++)
1186 if (e->surfaces[j]->castshadow == castshadowcount)
1187 for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
1188 Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->verts, surfmesh->numtriangles, surfmesh->index);
1189 castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
1191 // cast shadow volume from castmesh
1192 for (mesh = castmesh;mesh;mesh = mesh->next)
1194 R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
1195 R_Shadow_ResizeShadowElements(castmesh->numtriangles);
1197 if (maxverts < castmesh->numverts * 2)
1199 maxverts = castmesh->numverts * 2;
1204 if (verts == NULL && maxverts > 0)
1205 verts = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[4]));
1207 // now that we have the buffers big enough, construct shadow volume mesh
1208 memcpy(verts, castmesh->verts, castmesh->numverts * sizeof(float[4]));
1209 R_Shadow_ProjectVertices(verts, castmesh->numverts, e->origin, 10000000.0f);//, e->lightradius);
1210 R_Shadow_MakeTriangleShadowFlags(castmesh->elements, verts, castmesh->numtriangles, trianglefacinglight, e->origin, e->lightradius);
1211 tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->elements, castmesh->neighbors, castmesh->numtriangles, castmesh->numverts, trianglefacinglight, shadowelements);
1212 // add the constructed shadow volume mesh
1213 Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, verts, tris, shadowelements);
1215 // we're done with castmesh now
1216 Mod_ShadowMesh_Free(castmesh);
1217 e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
1218 for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
1219 l += mesh->numtriangles;
1220 Con_Printf("static shadow volume built containing %i triangles\n", l);
1223 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);
1226 void R_Shadow_FreeWorldLight(worldlight_t *light)
1228 worldlight_t **lightpointer;
1229 for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
1230 if (*lightpointer != light)
1231 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
1232 *lightpointer = light->next;
1233 if (light->cubemapname)
1234 Mem_Free(light->cubemapname);
1235 if (light->shadowvolume)
1236 Mod_ShadowMesh_Free(light->shadowvolume);
1237 if (light->surfaces)
1238 Mem_Free(light->surfaces);
1240 Mem_Free(light->leafs);
1244 void R_Shadow_ClearWorldLights(void)
1246 while (r_shadow_worldlightchain)
1247 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
1248 r_shadow_selectedlight = NULL;
1251 void R_Shadow_SelectLight(worldlight_t *light)
1253 if (r_shadow_selectedlight)
1254 r_shadow_selectedlight->selected = false;
1255 r_shadow_selectedlight = light;
1256 if (r_shadow_selectedlight)
1257 r_shadow_selectedlight->selected = true;
1261 void R_DrawLightSprite(int texnum, const vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
1268 VectorSubtract(origin, r_origin, diff);
1269 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
1272 memset(&m, 0, sizeof(m));
1273 m.blendfunc1 = GL_SRC_ALPHA;
1274 m.blendfunc2 = GL_ONE;
1276 R_Mesh_Matrix(&r_identitymatrix);
1279 GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1280 varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
1281 varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
1282 varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
1283 varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
1284 varray_vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
1285 varray_vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
1286 varray_vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
1287 varray_vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
1288 varray_vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
1289 varray_vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
1290 varray_vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
1291 varray_vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
1292 varray_vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
1293 varray_vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
1294 varray_vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
1295 varray_vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
1296 R_Mesh_Draw(4, 2, polygonelements);
1299 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
1302 pic = Draw_CachePic("gfx/crosshair1.tga");
1304 R_DrawLightSprite(R_GetTexture(pic->tex), r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 0.5);
1307 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
1310 const worldlight_t *light;
1313 if (light->selected)
1314 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
1315 if (light->shadowvolume)
1316 R_DrawLightSprite(calldata2, light->origin, 8, intensity, intensity, intensity, 0.5);
1318 R_DrawLightSprite(calldata2, light->origin, 8, intensity * 0.5, intensity * 0.5, intensity * 0.5, 0.5);
1321 void R_Shadow_DrawLightSprites(void)
1325 worldlight_t *light;
1327 for (i = 0;i < 5;i++)
1329 pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1));
1331 texnums[i] = R_GetTexture(pic->tex);
1336 for (light = r_shadow_worldlightchain;light;light = light->next)
1337 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, texnums[((int) light) % 5]);
1338 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
1341 void R_Shadow_SelectLightInView(void)
1343 float bestrating, rating, temp[3];
1344 worldlight_t *best, *light;
1347 for (light = r_shadow_worldlightchain;light;light = light->next)
1349 VectorSubtract(light->origin, r_refdef.vieworg, temp);
1350 rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
1353 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
1354 if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
1356 bestrating = rating;
1361 R_Shadow_SelectLight(best);
1364 void R_Shadow_LoadWorldLights(void)
1366 int n, a, style, shadow;
1367 char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
1368 float origin[3], radius, color[3];
1369 if (cl.worldmodel == NULL)
1371 Con_Printf("No map loaded.\n");
1374 COM_StripExtension(cl.worldmodel->name, name);
1375 strcat(name, ".rtlights");
1376 lightsstring = COM_LoadFile(name, false);
1384 while (*s && *s != '\n')
1390 // check for modifier flags
1396 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);
1402 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);
1405 VectorScale(color, r_editlights_rtlightscolorscale.value, color);
1406 radius *= r_editlights_rtlightssizescale.value;
1407 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadow);
1412 Con_Printf("invalid rtlights file \"%s\"\n", name);
1413 Mem_Free(lightsstring);
1417 void R_Shadow_SaveWorldLights(void)
1419 worldlight_t *light;
1420 int bufchars, bufmaxchars;
1422 char name[MAX_QPATH];
1424 if (!r_shadow_worldlightchain)
1426 if (cl.worldmodel == NULL)
1428 Con_Printf("No map loaded.\n");
1431 COM_StripExtension(cl.worldmodel->name, name);
1432 strcat(name, ".rtlights");
1433 bufchars = bufmaxchars = 0;
1435 for (light = r_shadow_worldlightchain;light;light = light->next)
1437 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 : "");
1438 if (bufchars + strlen(line) > bufmaxchars)
1440 bufmaxchars = bufchars + strlen(line) + 2048;
1442 buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
1446 memcpy(buf, oldbuf, bufchars);
1452 memcpy(buf + bufchars, line, strlen(line));
1453 bufchars += strlen(line);
1457 COM_WriteFile(name, buf, bufchars);
1462 void R_Shadow_LoadLightsFile(void)
1465 char name[MAX_QPATH], *lightsstring, *s, *t;
1466 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
1467 if (cl.worldmodel == NULL)
1469 Con_Printf("No map loaded.\n");
1472 COM_StripExtension(cl.worldmodel->name, name);
1473 strcat(name, ".lights");
1474 lightsstring = COM_LoadFile(name, false);
1482 while (*s && *s != '\n')
1487 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);
1491 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);
1494 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
1495 radius = bound(15, radius, 4096);
1496 VectorScale(color, (2.0f / (8388608.0f)), color);
1497 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
1502 Con_Printf("invalid lights file \"%s\"\n", name);
1503 Mem_Free(lightsstring);
1507 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
1509 int entnum, style, islight;
1510 char key[256], value[1024];
1511 float origin[3], radius, color[3], light, scale, originhack[3], overridecolor[3];
1514 if (cl.worldmodel == NULL)
1516 Con_Printf("No map loaded.\n");
1519 data = cl.worldmodel->entities;
1522 for (entnum = 0;COM_ParseToken(&data) && com_token[0] == '{';entnum++)
1525 origin[0] = origin[1] = origin[2] = 0;
1526 originhack[0] = originhack[1] = originhack[2] = 0;
1527 color[0] = color[1] = color[2] = 1;
1528 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
1534 if (!COM_ParseToken(&data))
1536 if (com_token[0] == '}')
1537 break; // end of entity
1538 if (com_token[0] == '_')
1539 strcpy(key, com_token + 1);
1541 strcpy(key, com_token);
1542 while (key[strlen(key)-1] == ' ') // remove trailing spaces
1543 key[strlen(key)-1] = 0;
1544 if (!COM_ParseToken(&data))
1546 strcpy(value, com_token);
1548 // now that we have the key pair worked out...
1549 if (!strcmp("light", key))
1550 light = atof(value);
1551 else if (!strcmp("origin", key))
1552 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
1553 else if (!strcmp("color", key))
1554 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
1555 else if (!strcmp("wait", key))
1556 scale = atof(value);
1557 else if (!strcmp("classname", key))
1559 if (!strncmp(value, "light", 5))
1562 if (!strcmp(value, "light_fluoro"))
1567 overridecolor[0] = 1;
1568 overridecolor[1] = 1;
1569 overridecolor[2] = 1;
1571 if (!strcmp(value, "light_fluorospark"))
1576 overridecolor[0] = 1;
1577 overridecolor[1] = 1;
1578 overridecolor[2] = 1;
1580 if (!strcmp(value, "light_globe"))
1585 overridecolor[0] = 1;
1586 overridecolor[1] = 0.8;
1587 overridecolor[2] = 0.4;
1589 if (!strcmp(value, "light_flame_large_yellow"))
1594 overridecolor[0] = 1;
1595 overridecolor[1] = 0.5;
1596 overridecolor[2] = 0.1;
1598 if (!strcmp(value, "light_flame_small_yellow"))
1603 overridecolor[0] = 1;
1604 overridecolor[1] = 0.5;
1605 overridecolor[2] = 0.1;
1607 if (!strcmp(value, "light_torch_small_white"))
1612 overridecolor[0] = 1;
1613 overridecolor[1] = 0.5;
1614 overridecolor[2] = 0.1;
1616 if (!strcmp(value, "light_torch_small_walltorch"))
1621 overridecolor[0] = 1;
1622 overridecolor[1] = 0.5;
1623 overridecolor[2] = 0.1;
1627 else if (!strcmp("style", key))
1628 style = atoi(value);
1630 if (light <= 0 && islight)
1632 radius = bound(15, light * r_editlights_quakelightsizescale.value / scale, 1048576);
1633 light = sqrt(bound(0, light, 1048576)) * (1.0f / 16.0f);
1634 if (color[0] == 1 && color[1] == 1 && color[2] == 1)
1635 VectorCopy(overridecolor, color);
1636 VectorScale(color, light, color);
1637 VectorAdd(origin, originhack, origin);
1639 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
1644 void R_Shadow_SetCursorLocationForView(void)
1646 vec_t dist, push, frac;
1647 vec3_t dest, endpos, normal;
1648 VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
1649 frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
1652 dist = frac * r_editlights_cursordistance.value;
1653 push = r_editlights_cursorpushback.value;
1657 VectorMA(endpos, push, vpn, endpos);
1658 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
1660 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1661 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1662 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1665 void R_Shadow_UpdateLightingMode(void)
1667 r_shadow_lightingmode = 0;
1668 if (r_shadow_realtime.integer)
1670 if (r_shadow_worldlightchain)
1671 r_shadow_lightingmode = 2;
1673 r_shadow_lightingmode = 1;
1677 void R_Shadow_UpdateWorldLightSelection(void)
1679 R_Shadow_SetCursorLocationForView();
1680 if (r_editlights.integer)
1682 R_Shadow_SelectLightInView();
1683 R_Shadow_DrawLightSprites();
1686 R_Shadow_SelectLight(NULL);
1689 void R_Shadow_EditLights_Clear_f(void)
1691 R_Shadow_ClearWorldLights();
1694 void R_Shadow_EditLights_Reload_f(void)
1696 r_shadow_reloadlights = true;
1699 void R_Shadow_EditLights_Save_f(void)
1702 R_Shadow_SaveWorldLights();
1705 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
1707 R_Shadow_ClearWorldLights();
1708 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
1711 void R_Shadow_EditLights_ImportLightsFile_f(void)
1713 R_Shadow_ClearWorldLights();
1714 R_Shadow_LoadLightsFile();
1717 void R_Shadow_EditLights_Spawn_f(void)
1720 if (!r_editlights.integer)
1722 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1725 if (Cmd_Argc() != 1)
1727 Con_Printf("r_editlights_spawn does not take parameters\n");
1730 color[0] = color[1] = color[2] = 1;
1731 R_Shadow_NewWorldLight(r_editlights_cursorlocation, 200, color, 0, NULL, true);
1734 void R_Shadow_EditLights_Edit_f(void)
1736 vec3_t origin, color;
1739 char cubemapname[1024];
1740 if (!r_editlights.integer)
1742 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1745 if (!r_shadow_selectedlight)
1747 Con_Printf("No selected light.\n");
1750 VectorCopy(r_shadow_selectedlight->origin, origin);
1751 radius = r_shadow_selectedlight->lightradius;
1752 VectorCopy(r_shadow_selectedlight->light, color);
1753 style = r_shadow_selectedlight->style;
1754 if (r_shadow_selectedlight->cubemapname)
1755 strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
1758 shadows = r_shadow_selectedlight->castshadows;
1759 if (!strcmp(Cmd_Argv(1), "origin"))
1761 if (Cmd_Argc() != 5)
1763 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
1766 origin[0] = atof(Cmd_Argv(2));
1767 origin[1] = atof(Cmd_Argv(3));
1768 origin[2] = atof(Cmd_Argv(4));
1770 else if (!strcmp(Cmd_Argv(1), "originx"))
1772 if (Cmd_Argc() != 3)
1774 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1777 origin[0] = atof(Cmd_Argv(2));
1779 else if (!strcmp(Cmd_Argv(1), "originy"))
1781 if (Cmd_Argc() != 3)
1783 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1786 origin[1] = atof(Cmd_Argv(2));
1788 else if (!strcmp(Cmd_Argv(1), "originz"))
1790 if (Cmd_Argc() != 3)
1792 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1795 origin[2] = atof(Cmd_Argv(2));
1797 else if (!strcmp(Cmd_Argv(1), "move"))
1799 if (Cmd_Argc() != 5)
1801 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
1804 origin[0] += atof(Cmd_Argv(2));
1805 origin[1] += atof(Cmd_Argv(3));
1806 origin[2] += atof(Cmd_Argv(4));
1808 else if (!strcmp(Cmd_Argv(1), "movex"))
1810 if (Cmd_Argc() != 3)
1812 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1815 origin[0] += atof(Cmd_Argv(2));
1817 else if (!strcmp(Cmd_Argv(1), "movey"))
1819 if (Cmd_Argc() != 3)
1821 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1824 origin[1] += atof(Cmd_Argv(2));
1826 else if (!strcmp(Cmd_Argv(1), "movez"))
1828 if (Cmd_Argc() != 3)
1830 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1833 origin[2] += atof(Cmd_Argv(2));
1835 else if (!strcmp(Cmd_Argv(1), "color"))
1837 if (Cmd_Argc() != 5)
1839 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(0));
1842 color[0] = atof(Cmd_Argv(2));
1843 color[1] = atof(Cmd_Argv(3));
1844 color[2] = atof(Cmd_Argv(4));
1846 else if (!strcmp(Cmd_Argv(1), "radius"))
1848 if (Cmd_Argc() != 3)
1850 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1853 radius = atof(Cmd_Argv(2));
1855 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "style"))
1857 if (Cmd_Argc() != 3)
1859 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1862 style = atoi(Cmd_Argv(2));
1864 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "cubemap"))
1868 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1871 if (Cmd_Argc() == 3)
1872 strcpy(cubemapname, Cmd_Argv(2));
1876 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "shadows"))
1878 if (Cmd_Argc() != 3)
1880 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1883 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
1887 Con_Printf("usage: r_editlights_edit [property] [value]\n");
1888 Con_Printf("Selected light's properties:\n");
1889 Con_Printf("Origin: %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
1890 Con_Printf("Radius: %f\n", r_shadow_selectedlight->lightradius);
1891 Con_Printf("Color: %f %f %f\n", r_shadow_selectedlight->light[0], r_shadow_selectedlight->light[1], r_shadow_selectedlight->light[2]);
1892 Con_Printf("Style: %i\n", r_shadow_selectedlight->style);
1893 Con_Printf("Cubemap: %s\n", r_shadow_selectedlight->cubemapname);
1894 Con_Printf("Shadows: %s\n", r_shadow_selectedlight->castshadows ? "yes" : "no");
1897 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1898 r_shadow_selectedlight = NULL;
1899 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadows);
1902 extern int con_vislines;
1903 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
1907 if (r_shadow_selectedlight == NULL)
1911 sprintf(temp, "Light properties");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1912 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;
1913 sprintf(temp, "Radius %f", r_shadow_selectedlight->lightradius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1914 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;
1915 sprintf(temp, "Style %i", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1916 sprintf(temp, "Cubemap %s", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1917 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;
1920 void R_Shadow_EditLights_ToggleShadow_f(void)
1922 if (!r_editlights.integer)
1924 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1927 if (!r_shadow_selectedlight)
1929 Con_Printf("No selected light.\n");
1932 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);
1933 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1934 r_shadow_selectedlight = NULL;
1937 void R_Shadow_EditLights_Remove_f(void)
1939 if (!r_editlights.integer)
1941 Con_Printf("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
1944 if (!r_shadow_selectedlight)
1946 Con_Printf("No selected light.\n");
1949 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1950 r_shadow_selectedlight = NULL;
1953 void R_Shadow_EditLights_Init(void)
1955 Cvar_RegisterVariable(&r_editlights);
1956 Cvar_RegisterVariable(&r_editlights_cursordistance);
1957 Cvar_RegisterVariable(&r_editlights_cursorpushback);
1958 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
1959 Cvar_RegisterVariable(&r_editlights_cursorgrid);
1960 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
1961 Cvar_RegisterVariable(&r_editlights_rtlightssizescale);
1962 Cvar_RegisterVariable(&r_editlights_rtlightscolorscale);
1963 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
1964 Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
1965 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
1966 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
1967 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
1968 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
1969 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f);
1970 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
1971 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);