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"};
45 int c_rt_lights, c_rt_clears, c_rt_scissored;
46 int c_rt_shadowmeshes, c_rt_shadowtris, c_rt_lightmeshes, c_rt_lighttris;
47 int c_rtcached_shadowmeshes, c_rtcached_shadowtris;
49 void R_Shadow_ClearWorldLights(void);
50 void R_Shadow_SaveWorldLights(void);
51 void R_Shadow_LoadWorldLights(void);
52 void R_Shadow_LoadLightsFile(void);
53 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
55 void r_shadow_start(void)
57 // allocate vertex processing arrays
58 r_shadow_mempool = Mem_AllocPool("R_Shadow");
59 maxshadowelements = 0;
60 shadowelements = NULL;
61 maxtrianglefacinglight = 0;
62 trianglefacinglight = NULL;
63 r_shadow_normalscubetexture = NULL;
64 r_shadow_attenuation2dtexture = NULL;
65 r_shadow_blankbumptexture = NULL;
66 r_shadow_blankglosstexture = NULL;
67 r_shadow_blankwhitetexture = NULL;
68 r_shadow_texturepool = NULL;
69 R_Shadow_ClearWorldLights();
70 r_shadow_reloadlights = true;
73 void r_shadow_shutdown(void)
75 R_Shadow_ClearWorldLights();
76 r_shadow_reloadlights = true;
77 r_shadow_normalscubetexture = NULL;
78 r_shadow_attenuation2dtexture = NULL;
79 r_shadow_blankbumptexture = NULL;
80 r_shadow_blankglosstexture = NULL;
81 r_shadow_blankwhitetexture = NULL;
82 R_FreeTexturePool(&r_shadow_texturepool);
83 maxshadowelements = 0;
84 shadowelements = NULL;
85 maxtrianglefacinglight = 0;
86 trianglefacinglight = NULL;
87 Mem_FreePool(&r_shadow_mempool);
90 void r_shadow_newmap(void)
92 R_Shadow_ClearWorldLights();
93 r_shadow_reloadlights = true;
96 void R_Shadow_Init(void)
98 Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
99 Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
100 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
101 Cvar_RegisterVariable(&r_shadow_realtime);
102 Cvar_RegisterVariable(&r_shadow_gloss);
103 Cvar_RegisterVariable(&r_shadow_debuglight);
104 Cvar_RegisterVariable(&r_shadow_scissor);
105 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
106 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
107 Cvar_RegisterVariable(&r_shadow_shadownudge);
108 Cvar_RegisterVariable(&r_shadow_portallight);
109 R_Shadow_EditLights_Init();
110 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
113 void R_Shadow_ProjectVertices(float *verts, int numverts, const float *relativelightorigin, float projectdistance)
116 float *in, *out, diff[4];
118 out = verts + numverts * 4;
119 for (i = 0;i < numverts;i++, in += 4, out += 4)
121 VectorSubtract(in, relativelightorigin, diff);
122 VectorNormalizeFast(diff);
123 VectorMA(in, projectdistance, diff, out);
124 VectorMA(in, r_shadow_shadownudge.value, diff, in);
128 void R_Shadow_MakeTriangleShadowFlags(const int *elements, const float *vertex, int numtris, qbyte *trianglefacinglight, const float *relativelightorigin, float lightradius)
131 const float *v0, *v1, *v2;
132 for (i = 0;i < numtris;i++, elements += 3)
134 // calculate triangle facing flag
135 v0 = vertex + elements[0] * 4;
136 v1 = vertex + elements[1] * 4;
137 v2 = vertex + elements[2] * 4;
138 // we do not need to normalize the surface normal because both sides
139 // of the comparison use it, therefore they are both multiplied the
140 // same amount... furthermore the subtract can be done on the
141 // vectors, saving a little bit of math in the dotproducts
144 // subtracts v1 from v0 and v2, combined into a crossproduct,
145 // combined with a dotproduct of the light location relative to the
146 // first point of the triangle (any point works, since the triangle
147 // is obviously flat), and finally a comparison to determine if the
148 // light is infront of the triangle (the goal of this statement)
149 trianglefacinglight[i] =
150 (relativelightorigin[0] - v0[0]) * ((v0[1] - v1[1]) * (v2[2] - v1[2]) - (v0[2] - v1[2]) * (v2[1] - v1[1]))
151 + (relativelightorigin[1] - v0[1]) * ((v0[2] - v1[2]) * (v2[0] - v1[0]) - (v0[0] - v1[0]) * (v2[2] - v1[2]))
152 + (relativelightorigin[2] - v0[2]) * ((v0[0] - v1[0]) * (v2[1] - v1[1]) - (v0[1] - v1[1]) * (v2[0] - v1[0])) > 0;
156 float dir0[3], dir1[3], temp[3];
158 // calculate two mostly perpendicular edge directions
159 VectorSubtract(v0, v1, dir0);
160 VectorSubtract(v2, v1, dir1);
162 // we have two edge directions, we can calculate a third vector from
163 // them, which is the direction of the surface normal (it's magnitude
165 CrossProduct(dir0, dir1, temp);
167 // this is entirely unnecessary, but kept for clarity
168 //VectorNormalize(temp);
170 // compare distance of light along normal, with distance of any point
171 // of the triangle along the same normal (the triangle is planar,
172 // I.E. flat, so all points give the same answer)
173 // the normal is not normalized because it is used on both sides of
174 // the comparison, so it's magnitude does not matter
175 trianglefacinglight[i] = DotProduct(relativelightorigin, temp) >= DotProduct(v0, temp);
181 int R_Shadow_BuildShadowVolumeTriangles(const int *elements, const int *neighbors, int numtris, int numverts, const qbyte *trianglefacinglight, int *out)
184 // check each frontface for bordering backfaces,
185 // and cast shadow polygons from those edges,
186 // also create front and back caps for shadow volume
188 for (i = 0;i < numtris;i++, elements += 3, neighbors += 3)
190 if (trianglefacinglight[i])
192 // triangle is frontface and therefore casts shadow,
193 // output front and back caps for shadow volume
195 out[0] = elements[0];
196 out[1] = elements[1];
197 out[2] = elements[2];
198 // rear cap (with flipped winding order)
199 out[3] = elements[0] + numverts;
200 out[4] = elements[2] + numverts;
201 out[5] = elements[1] + numverts;
205 if (neighbors[0] < 0 || !trianglefacinglight[neighbors[0]])
207 out[0] = elements[1];
208 out[1] = elements[0];
209 out[2] = elements[0] + numverts;
210 out[3] = elements[1];
211 out[4] = elements[0] + numverts;
212 out[5] = elements[1] + numverts;
216 if (neighbors[1] < 0 || !trianglefacinglight[neighbors[1]])
218 out[0] = elements[2];
219 out[1] = elements[1];
220 out[2] = elements[1] + numverts;
221 out[3] = elements[2];
222 out[4] = elements[1] + numverts;
223 out[5] = elements[2] + numverts;
227 if (neighbors[2] < 0 || !trianglefacinglight[neighbors[2]])
229 out[0] = elements[0];
230 out[1] = elements[2];
231 out[2] = elements[2] + numverts;
232 out[3] = elements[0];
233 out[4] = elements[2] + numverts;
234 out[5] = elements[0] + numverts;
243 void R_Shadow_ResizeTriangleFacingLight(int numtris)
245 // make sure trianglefacinglight is big enough for this volume
246 if (maxtrianglefacinglight < numtris)
248 maxtrianglefacinglight = numtris;
249 if (trianglefacinglight)
250 Mem_Free(trianglefacinglight);
251 trianglefacinglight = Mem_Alloc(r_shadow_mempool, maxtrianglefacinglight);
255 void R_Shadow_ResizeShadowElements(int numtris)
257 // make sure shadowelements is big enough for this volume
258 if (maxshadowelements < numtris * 24)
260 maxshadowelements = numtris * 24;
262 Mem_Free(shadowelements);
263 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
267 void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance)
270 if (projectdistance < 0.1)
272 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
278 // a triangle facing the light source
281 // a triangle not facing the light source
284 // an extrusion of the frontfaces, beginning at the original geometry and
285 // ending further from the light source than the original geometry
286 // (presumably at least as far as the light's radius, if the light has a
287 // radius at all), capped at both front and back to avoid any problems
290 // draws the shadow volumes of the model.
292 // vertex locations must already be in varray_vertex before use.
293 // varray_vertex must have capacity for numverts * 2.
295 // make sure trianglefacinglight is big enough for this volume
296 if (maxtrianglefacinglight < numtris)
297 R_Shadow_ResizeTriangleFacingLight(numtris);
299 // make sure shadowelements is big enough for this volume
300 if (maxshadowelements < numtris * 24)
301 R_Shadow_ResizeShadowElements(numtris);
303 // check which triangles are facing the light
304 R_Shadow_MakeTriangleShadowFlags(elements, varray_vertex, numtris, trianglefacinglight, relativelightorigin, lightradius);
306 // generate projected vertices
307 // by clever use of elements we'll construct the whole shadow from
308 // the unprojected vertices and these projected vertices
309 R_Shadow_ProjectVertices(varray_vertex, numverts, relativelightorigin, projectdistance);
311 // output triangle elements
312 tris = R_Shadow_BuildShadowVolumeTriangles(elements, neighbors, numtris, numverts, trianglefacinglight, shadowelements);
313 R_Shadow_RenderVolume(numverts * 2, tris, shadowelements);
316 void R_Shadow_RenderVolume(int numverts, int numtris, int *elements)
318 if (!numverts || !numtris)
320 if (r_shadowstage == SHADOWSTAGE_STENCIL)
322 // increment stencil if backface is behind depthbuffer
323 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
324 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
325 R_Mesh_Draw(numverts, numtris, elements);
327 c_rt_shadowtris += numtris;
328 // decrement stencil if frontface is behind depthbuffer
329 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
330 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
332 R_Mesh_Draw(numverts, numtris, elements);
334 c_rt_shadowtris += numtris;
337 void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
340 if (r_shadowstage == SHADOWSTAGE_STENCIL)
342 // increment stencil if backface is behind depthbuffer
343 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
344 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
345 for (mesh = firstmesh;mesh;mesh = mesh->next)
347 R_Mesh_ResizeCheck(mesh->numverts);
348 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
349 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
350 c_rtcached_shadowmeshes++;
351 c_rtcached_shadowtris += mesh->numtriangles;
353 // decrement stencil if frontface is behind depthbuffer
354 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
355 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
357 for (mesh = firstmesh;mesh;mesh = mesh->next)
359 R_Mesh_ResizeCheck(mesh->numverts);
360 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
361 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
362 c_rtcached_shadowmeshes++;
363 c_rtcached_shadowtris += mesh->numtriangles;
367 float r_shadow_attenpower, r_shadow_attenscale;
368 static void R_Shadow_MakeTextures(void)
371 float v[3], s, t, intensity;
373 R_FreeTexturePool(&r_shadow_texturepool);
374 r_shadow_texturepool = R_AllocTexturePool();
375 r_shadow_attenpower = r_shadow_lightattenuationpower.value;
376 r_shadow_attenscale = r_shadow_lightattenuationscale.value;
377 data = Mem_Alloc(tempmempool, 6*128*128*4);
382 r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
387 r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
392 r_shadow_blankwhitetexture = R_LoadTexture2D(r_shadow_texturepool, "blankwhite", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
393 for (side = 0;side < 6;side++)
395 for (y = 0;y < 128;y++)
397 for (x = 0;x < 128;x++)
399 s = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
400 t = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
434 intensity = 127.0f / sqrt(DotProduct(v, v));
435 data[((side*128+y)*128+x)*4+0] = 128.0f + intensity * v[0];
436 data[((side*128+y)*128+x)*4+1] = 128.0f + intensity * v[1];
437 data[((side*128+y)*128+x)*4+2] = 128.0f + intensity * v[2];
438 data[((side*128+y)*128+x)*4+3] = 255;
442 r_shadow_normalscubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalscube", 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
443 for (y = 0;y < 128;y++)
445 for (x = 0;x < 128;x++)
447 v[0] = (x + 0.5f) * (2.0f / (128.0f - 8.0f)) - 1.0f;
448 v[1] = (y + 0.5f) * (2.0f / (128.0f - 8.0f)) - 1.0f;
450 intensity = 1.0f - sqrt(DotProduct(v, v));
452 intensity = pow(intensity, r_shadow_attenpower);
453 intensity = bound(0, intensity * r_shadow_attenscale * 256.0f, 255.0f);
454 d = bound(0, intensity, 255);
455 data[((0*128+y)*128+x)*4+0] = d;
456 data[((0*128+y)*128+x)*4+1] = d;
457 data[((0*128+y)*128+x)*4+2] = d;
458 data[((0*128+y)*128+x)*4+3] = d;
461 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", 128, 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
465 void R_Shadow_Stage_Begin(void)
469 //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
470 if (!r_shadow_attenuation2dtexture
471 || r_shadow_lightattenuationpower.value != r_shadow_attenpower
472 || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
473 R_Shadow_MakeTextures();
474 if (r_shadow_reloadlights && cl.worldmodel)
476 R_Shadow_ClearWorldLights();
477 r_shadow_reloadlights = false;
478 R_Shadow_LoadWorldLights();
479 if (r_shadow_worldlightchain == NULL)
481 R_Shadow_LoadLightsFile();
482 if (r_shadow_worldlightchain == NULL)
483 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
487 memset(&m, 0, sizeof(m));
488 m.blendfunc1 = GL_ONE;
489 m.blendfunc2 = GL_ZERO;
491 GL_Color(0, 0, 0, 1);
492 r_shadowstage = SHADOWSTAGE_NONE;
494 c_rt_lights = c_rt_clears = c_rt_scissored = 0;
495 c_rt_shadowmeshes = c_rt_shadowtris = c_rt_lightmeshes = c_rt_lighttris = 0;
496 c_rtcached_shadowmeshes = c_rtcached_shadowtris = 0;
499 void R_Shadow_Stage_ShadowVolumes(void)
502 memset(&m, 0, sizeof(m));
503 R_Mesh_TextureState(&m);
504 GL_Color(1, 1, 1, 1);
505 qglColorMask(0, 0, 0, 0);
506 qglDisable(GL_BLEND);
508 qglDepthFunc(GL_LESS);
509 qglEnable(GL_STENCIL_TEST);
510 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
511 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
512 qglEnable(GL_CULL_FACE);
513 qglEnable(GL_DEPTH_TEST);
514 r_shadowstage = SHADOWSTAGE_STENCIL;
515 qglClear(GL_STENCIL_BUFFER_BIT);
517 // LordHavoc note: many shadow volumes reside entirely inside the world
518 // (that is to say they are entirely bounded by their lit surfaces),
519 // which can be optimized by handling things as an inverted light volume,
520 // with the shadow boundaries of the world being simulated by an altered
521 // (129) bias to stencil clearing on such lights
522 // FIXME: generate inverted light volumes for use as shadow volumes and
523 // optimize for them as noted above
526 void R_Shadow_Stage_LightWithoutShadows(void)
529 memset(&m, 0, sizeof(m));
530 R_Mesh_TextureState(&m);
531 qglActiveTexture(GL_TEXTURE0_ARB);
534 qglBlendFunc(GL_ONE, GL_ONE);
535 GL_Color(1, 1, 1, 1);
536 qglColorMask(1, 1, 1, 1);
538 qglDepthFunc(GL_EQUAL);
539 qglDisable(GL_STENCIL_TEST);
540 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
541 qglStencilFunc(GL_EQUAL, 128, 0xFF);
542 qglEnable(GL_CULL_FACE);
543 qglEnable(GL_DEPTH_TEST);
544 r_shadowstage = SHADOWSTAGE_LIGHT;
548 void R_Shadow_Stage_LightWithShadows(void)
551 memset(&m, 0, sizeof(m));
552 R_Mesh_TextureState(&m);
553 qglActiveTexture(GL_TEXTURE0_ARB);
556 qglBlendFunc(GL_ONE, GL_ONE);
557 GL_Color(1, 1, 1, 1);
558 qglColorMask(1, 1, 1, 1);
560 qglDepthFunc(GL_EQUAL);
561 qglEnable(GL_STENCIL_TEST);
562 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
563 // only draw light where this geometry was already rendered AND the
564 // stencil is 128 (values other than this mean shadow)
565 qglStencilFunc(GL_EQUAL, 128, 0xFF);
566 qglEnable(GL_CULL_FACE);
567 qglEnable(GL_DEPTH_TEST);
568 r_shadowstage = SHADOWSTAGE_LIGHT;
572 void R_Shadow_Stage_End(void)
575 // attempt to restore state to what Mesh_State thinks it is
576 qglDisable(GL_BLEND);
577 qglBlendFunc(GL_ONE, GL_ZERO);
579 // now restore the rest of the state to normal
580 GL_Color(1, 1, 1, 1);
581 qglColorMask(1, 1, 1, 1);
582 qglDisable(GL_SCISSOR_TEST);
583 qglDepthFunc(GL_LEQUAL);
584 qglDisable(GL_STENCIL_TEST);
585 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
586 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
587 qglEnable(GL_CULL_FACE);
588 qglEnable(GL_DEPTH_TEST);
589 // force mesh state to reset by using various combinations of features
590 memset(&m, 0, sizeof(m));
591 m.blendfunc1 = GL_SRC_ALPHA;
592 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
594 m.blendfunc1 = GL_ONE;
595 m.blendfunc2 = GL_ZERO;
597 r_shadowstage = SHADOWSTAGE_NONE;
600 int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const float *origin, float radius)
602 int i, ix1, iy1, ix2, iy2;
603 float x1, y1, x2, y2, x, y;
606 if (!r_shadow_scissor.integer)
608 // if view is inside the box, just say yes it's visible
609 if (r_origin[0] >= mins[0] && r_origin[0] <= maxs[0]
610 && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1]
611 && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2])
613 qglDisable(GL_SCISSOR_TEST);
616 VectorSubtract(r_origin, origin, v);
617 if (DotProduct(v, v) < radius * radius)
619 qglDisable(GL_SCISSOR_TEST);
622 // create viewspace bbox
623 for (i = 0;i < 8;i++)
625 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
626 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
627 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
628 v2[0] = DotProduct(v, vright);
629 v2[1] = DotProduct(v, vup);
630 v2[2] = DotProduct(v, vpn);
633 if (smins[0] > v2[0]) smins[0] = v2[0];
634 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
635 if (smins[1] > v2[1]) smins[1] = v2[1];
636 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
637 if (smins[2] > v2[2]) smins[2] = v2[2];
638 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
642 smins[0] = smaxs[0] = v2[0];
643 smins[1] = smaxs[1] = v2[1];
644 smins[2] = smaxs[2] = v2[2];
647 // now we have a bbox in viewspace
648 // clip it to the viewspace version of the sphere
649 v[0] = origin[0] - r_origin[0];
650 v[1] = origin[1] - r_origin[1];
651 v[2] = origin[2] - r_origin[2];
652 v2[0] = DotProduct(v, vright);
653 v2[1] = DotProduct(v, vup);
654 v2[2] = DotProduct(v, vpn);
655 if (smins[0] < v2[0] - radius) smins[0] = v2[0] - radius;
656 if (smaxs[0] < v2[0] - radius) smaxs[0] = v2[0] + radius;
657 if (smins[1] < v2[1] - radius) smins[1] = v2[1] - radius;
658 if (smaxs[1] < v2[1] - radius) smaxs[1] = v2[1] + radius;
659 if (smins[2] < v2[2] - radius) smins[2] = v2[2] - radius;
660 if (smaxs[2] < v2[2] - radius) smaxs[2] = v2[2] + radius;
661 // clip it to the view plane
664 // return true if that culled the box
665 if (smins[2] >= smaxs[2])
667 // ok some of it is infront of the view, transform each corner back to
668 // worldspace and then to screenspace and make screen rect
669 // initialize these variables just to avoid compiler warnings
670 x1 = y1 = x2 = y2 = 0;
671 for (i = 0;i < 8;i++)
673 v2[0] = (i & 1) ? smins[0] : smaxs[0];
674 v2[1] = (i & 2) ? smins[1] : smaxs[1];
675 v2[2] = (i & 4) ? smins[2] : smaxs[2];
676 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
677 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
678 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
680 GL_TransformToScreen(v, v2);
681 //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]);
698 // this code doesn't handle boxes with any points behind view properly
699 x1 = 1000;x2 = -1000;
700 y1 = 1000;y2 = -1000;
701 for (i = 0;i < 8;i++)
703 v[0] = (i & 1) ? mins[0] : maxs[0];
704 v[1] = (i & 2) ? mins[1] : maxs[1];
705 v[2] = (i & 4) ? mins[2] : maxs[2];
707 GL_TransformToScreen(v, v2);
708 //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]);
725 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
726 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
727 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
728 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
729 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
730 if (ix2 <= ix1 || iy2 <= iy1)
732 // set up the scissor rectangle
733 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
734 qglEnable(GL_SCISSOR_TEST);
739 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)
742 float lightvec[3], iradius;
743 iradius = 0.5f / lightradius;
744 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out2d += 4, out1d += 4)
746 VectorSubtract(vertex, relativelightorigin, lightvec);
747 out2d[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
748 out2d[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
750 out1d[0] = 0.5f + DotProduct(normals, lightvec) * iradius;
756 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)
759 float lightvec[3], iradius;
760 iradius = 0.5f / lightradius;
761 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
763 VectorSubtract(vertex, relativelightorigin, lightvec);
764 out[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
765 out[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
766 out[2] = 0.5f + DotProduct(normals, lightvec) * iradius;
770 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)
774 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
776 VectorSubtract(vertex, relativelightorigin, lightdir);
777 // the cubemap normalizes this for us
778 out[0] = DotProduct(svectors, lightdir);
779 out[1] = DotProduct(tvectors, lightdir);
780 out[2] = DotProduct(normals, lightdir);
784 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)
787 float lightdir[3], eyedir[3], halfdir[3], lightdirlen, iradius;
788 iradius = 0.5f / lightradius;
789 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
791 VectorSubtract(vertex, relativelightorigin, lightdir);
792 // this is used later to make the attenuation correct
793 lightdirlen = sqrt(DotProduct(lightdir, lightdir)) * iradius;
794 VectorNormalizeFast(lightdir);
795 VectorSubtract(vertex, relativeeyeorigin, eyedir);
796 VectorNormalizeFast(eyedir);
797 VectorAdd(lightdir, eyedir, halfdir);
798 VectorNormalizeFast(halfdir);
799 out[0] = 0.5f + DotProduct(svectors, halfdir) * lightdirlen;
800 out[1] = 0.5f + DotProduct(tvectors, halfdir) * lightdirlen;
801 out[2] = 0.5f + DotProduct(normals, halfdir) * lightdirlen;
805 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)
808 float lightdir[3], eyedir[3], halfdir[3];
809 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
811 VectorSubtract(vertex, relativelightorigin, lightdir);
812 VectorNormalizeFast(lightdir);
813 VectorSubtract(vertex, relativeeyeorigin, eyedir);
814 VectorNormalizeFast(eyedir);
815 VectorAdd(lightdir, eyedir, halfdir);
816 // the cubemap normalizes this for us
817 out[0] = DotProduct(svectors, halfdir);
818 out[1] = DotProduct(tvectors, halfdir);
819 out[2] = DotProduct(normals, halfdir);
823 void R_Shadow_GenTexCoords_LightCubeMap(float *out, int numverts, const float *vertex, const vec3_t relativelightorigin)
826 // FIXME: this needs to be written
827 // this code assumes the vertices are in worldspace (a false assumption)
828 for (i = 0;i < numverts;i++, vertex += 4, out += 4)
829 VectorSubtract(vertex, relativelightorigin, out);
832 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)
837 memset(&m, 0, sizeof(m));
839 bumptexture = r_shadow_blankbumptexture;
840 // colorscale accounts for how much we multiply the brightness during combine
841 // mult is how many times the final pass of the lighting will be
842 // performed to get more brightness than otherwise possible
843 // limit mult to 64 for sanity sake
844 if (r_textureunits.integer >= 4)
846 // 4 texture no3D combine path, two pass
847 m.tex[0] = R_GetTexture(bumptexture);
848 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
849 m.texcombinergb[0] = GL_REPLACE;
850 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
851 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
852 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
853 R_Mesh_TextureState(&m);
854 qglColorMask(0,0,0,1);
855 qglDisable(GL_BLEND);
857 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
858 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
859 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[2], varray_texcoord[3], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
860 R_Mesh_Draw(numverts, numtriangles, elements);
862 c_rt_lighttris += numtriangles;
864 m.tex[0] = R_GetTexture(basetexture);
865 m.texcubemap[1] = R_GetTexture(lightcubemap);
866 m.texcombinergb[0] = GL_MODULATE;
867 m.texcombinergb[1] = GL_MODULATE;
870 R_Mesh_TextureState(&m);
871 qglColorMask(1,1,1,0);
872 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
875 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
877 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
878 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))
880 GL_Color(color[0], color[1], color[2], 1);
881 R_Mesh_Draw(numverts, numtriangles, elements);
883 c_rt_lighttris += numtriangles;
888 // 2 texture no3D combine path, three pass
889 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
890 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
891 R_Mesh_TextureState(&m);
892 qglColorMask(0,0,0,1);
893 qglDisable(GL_BLEND);
895 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
896 R_Mesh_Draw(numverts, numtriangles, elements);
898 c_rt_lighttris += numtriangles;
900 m.tex[0] = R_GetTexture(bumptexture);
902 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
903 m.texcombinergb[0] = GL_REPLACE;
904 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
905 R_Mesh_TextureState(&m);
906 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
908 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
909 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
910 R_Mesh_Draw(numverts, numtriangles, elements);
912 c_rt_lighttris += numtriangles;
914 m.tex[0] = R_GetTexture(basetexture);
915 m.texcubemap[1] = R_GetTexture(lightcubemap);
916 m.texcombinergb[0] = GL_MODULATE;
917 m.texcombinergb[1] = GL_MODULATE;
918 R_Mesh_TextureState(&m);
919 qglColorMask(1,1,1,0);
920 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
922 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
924 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
925 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))
927 GL_Color(color[0], color[1], color[2], 1);
928 R_Mesh_Draw(numverts, numtriangles, elements);
930 c_rt_lighttris += numtriangles;
935 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)
940 memset(&m, 0, sizeof(m));
942 bumptexture = r_shadow_blankbumptexture;
944 glosstexture = r_shadow_blankglosstexture;
945 if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
947 // 2 texture no3D combine path, five pass
948 memset(&m, 0, sizeof(m));
950 m.tex[0] = R_GetTexture(bumptexture);
951 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
952 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
953 R_Mesh_TextureState(&m);
954 qglColorMask(0,0,0,1);
955 qglDisable(GL_BLEND);
957 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
958 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin);
959 R_Mesh_Draw(numverts, numtriangles, elements);
961 c_rt_lighttris += numtriangles;
965 m.texcombinergb[1] = GL_MODULATE;
966 R_Mesh_TextureState(&m);
967 // square alpha in framebuffer a few times to make it shiny
968 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
970 // these comments are a test run through this math for intensity 0.5
972 R_Mesh_Draw(numverts, numtriangles, elements);
974 c_rt_lighttris += numtriangles;
975 // 0.25 * 0.25 = 0.0625
976 R_Mesh_Draw(numverts, numtriangles, elements);
978 c_rt_lighttris += numtriangles;
979 // 0.0625 * 0.0625 = 0.00390625
980 R_Mesh_Draw(numverts, numtriangles, elements);
982 c_rt_lighttris += numtriangles;
984 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
985 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
986 R_Mesh_TextureState(&m);
987 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
988 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
989 R_Mesh_Draw(numverts, numtriangles, elements);
991 c_rt_lighttris += numtriangles;
993 m.tex[0] = R_GetTexture(glosstexture);
994 m.texcubemap[1] = R_GetTexture(lightcubemap);
995 R_Mesh_TextureState(&m);
996 qglColorMask(1,1,1,0);
997 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
998 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1000 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
1002 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1003 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))
1005 GL_Color(color[0], color[1], color[2], 1);
1006 R_Mesh_Draw(numverts, numtriangles, elements);
1008 c_rt_lighttris += numtriangles;
1013 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
1015 R_Mesh_Matrix(matrix);
1016 R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
1019 cvar_t r_editlights = {0, "r_editlights", "0"};
1020 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
1021 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
1022 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
1023 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
1024 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
1025 cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
1026 cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
1027 worldlight_t *r_shadow_worldlightchain;
1028 worldlight_t *r_shadow_selectedlight;
1029 vec3_t r_editlights_cursorlocation;
1031 static int castshadowcount = 1;
1032 void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname, int castshadow)
1034 int i, j, k, l, maxverts, *mark, tris;
1035 float *verts, *v, f, temp[3], radius2;
1036 //float projectdistance, *v0, *v1, temp2[3], temp3[3];
1038 shadowmesh_t *mesh, *castmesh;
1042 surfmesh_t *surfmesh;
1044 if (radius < 15 || DotProduct(color, color) < 0.03)
1046 Con_Printf("R_Shadow_NewWorldLight: refusing to create a light too small/dim\n");
1050 e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
1051 VectorCopy(origin, e->origin);
1052 VectorCopy(color, e->light);
1053 e->lightradius = radius;
1055 if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
1057 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
1060 e->castshadows = castshadow;
1062 e->cullradius = e->lightradius;
1063 e->mins[0] = e->origin[0] - e->lightradius;
1064 e->maxs[0] = e->origin[0] + e->lightradius;
1065 e->mins[1] = e->origin[1] - e->lightradius;
1066 e->maxs[1] = e->origin[1] + e->lightradius;
1067 e->mins[2] = e->origin[2] - e->lightradius;
1068 e->maxs[2] = e->origin[2] + e->lightradius;
1070 e->next = r_shadow_worldlightchain;
1071 r_shadow_worldlightchain = e;
1072 if (cubemapname && cubemapname[0])
1074 e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
1075 strcpy(e->cubemapname, cubemapname);
1076 // FIXME: add cubemap loading (and don't load a cubemap twice)
1081 if (r_shadow_portallight.integer)
1084 qbyte *bytesurfacepvs;
1085 byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->numleafs + 1);
1086 bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->numsurfaces);
1087 Portal_Visibility(cl.worldmodel, e->origin, byteleafpvs, bytesurfacepvs, NULL, 0, true, e->lightradius);
1089 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1091 if (byteleafpvs[i+1])
1093 temp[0] = bound(leaf->mins[0], e->origin[0], leaf->maxs[0]) - e->origin[0];
1094 temp[1] = bound(leaf->mins[1], e->origin[1], leaf->maxs[1]) - e->origin[1];
1095 temp[2] = bound(leaf->mins[2], e->origin[2], leaf->maxs[2]) - e->origin[2];
1096 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1097 leaf->worldnodeframe = castshadowcount;
1101 for (i = 0, surf = cl.worldmodel->surfaces;i < cl.worldmodel->numsurfaces;i++, surf++)
1103 if (bytesurfacepvs[i])
1105 f = DotProduct(e->origin, surf->plane->normal) - surf->plane->dist;
1106 if (surf->flags & SURF_PLANEBACK)
1108 if (f > 0 && f < e->lightradius)
1110 temp[0] = bound(surf->poly_mins[0], e->origin[0], surf->poly_maxs[0]) - e->origin[0];
1111 temp[1] = bound(surf->poly_mins[1], e->origin[1], surf->poly_maxs[1]) - e->origin[1];
1112 temp[2] = bound(surf->poly_mins[2], e->origin[2], surf->poly_maxs[2]) - e->origin[2];
1113 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1114 surf->castshadow = castshadowcount;
1119 Mem_Free(byteleafpvs);
1120 Mem_Free(bytesurfacepvs);
1124 leaf = Mod_PointInLeaf(origin, cl.worldmodel);
1125 pvs = Mod_LeafPVS(leaf, cl.worldmodel);
1126 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1128 if (pvs[i >> 3] & (1 << (i & 7)))
1130 VectorCopy(origin, temp);
1131 if (temp[0] < leaf->mins[0]) temp[0] = leaf->mins[0];
1132 if (temp[0] > leaf->maxs[0]) temp[0] = leaf->maxs[0];
1133 if (temp[1] < leaf->mins[1]) temp[1] = leaf->mins[1];
1134 if (temp[1] > leaf->maxs[1]) temp[1] = leaf->maxs[1];
1135 if (temp[2] < leaf->mins[2]) temp[2] = leaf->mins[2];
1136 if (temp[2] > leaf->maxs[2]) temp[2] = leaf->maxs[2];
1137 VectorSubtract(temp, origin, temp);
1138 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1140 leaf->worldnodeframe = castshadowcount;
1141 for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
1143 surf = cl.worldmodel->surfaces + *mark;
1144 if (surf->castshadow != castshadowcount)
1146 f = DotProduct(e->origin, surf->plane->normal) - surf->plane->dist;
1147 if (surf->flags & SURF_PLANEBACK)
1149 if (f > 0 && f < e->lightradius)
1151 temp[0] = bound(surf->poly_mins[0], e->origin[0], surf->poly_maxs[0]) - e->origin[0];
1152 temp[1] = bound(surf->poly_mins[1], e->origin[1], surf->poly_maxs[1]) - e->origin[1];
1153 temp[2] = bound(surf->poly_mins[2], e->origin[2], surf->poly_maxs[2]) - e->origin[2];
1154 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1155 surf->castshadow = castshadowcount;
1165 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1166 if (leaf->worldnodeframe == castshadowcount)
1169 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1170 if (surf->castshadow == castshadowcount)
1174 e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
1176 e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
1178 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1179 if (leaf->worldnodeframe == castshadowcount)
1180 e->leafs[e->numleafs++] = leaf;
1182 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1183 if (surf->castshadow == castshadowcount)
1184 e->surfaces[e->numsurfaces++] = surf;
1185 // find bounding box and sphere of lit surfaces
1186 // (these will be used for creating a shape to clip the light)
1188 VectorCopy(e->origin, e->mins);
1189 VectorCopy(e->origin, e->maxs);
1190 for (j = 0;j < e->numsurfaces;j++)
1192 surf = e->surfaces[j];
1193 for (k = 0, v = surf->poly_verts;k < surf->poly_numverts;k++, v += 3)
1195 if (e->mins[0] > v[0]) e->mins[0] = v[0];if (e->maxs[0] < v[0]) e->maxs[0] = v[0];
1196 if (e->mins[1] > v[1]) e->mins[1] = v[1];if (e->maxs[1] < v[1]) e->maxs[1] = v[1];
1197 if (e->mins[2] > v[2]) e->mins[2] = v[2];if (e->maxs[2] < v[2]) e->maxs[2] = v[2];
1198 VectorSubtract(v, e->origin, temp);
1199 f = DotProduct(temp, temp);
1204 e->cullradius = sqrt(radius2);
1205 if (e->cullradius > e->lightradius)
1206 e->cullradius = e->lightradius;
1207 if (e->mins[0] < e->origin[0] - e->lightradius) e->mins[0] = e->origin[0] - e->lightradius;
1208 if (e->maxs[0] > e->origin[0] + e->lightradius) e->maxs[0] = e->origin[0] + e->lightradius;
1209 if (e->mins[1] < e->origin[1] - e->lightradius) e->mins[1] = e->origin[1] - e->lightradius;
1210 if (e->maxs[1] > e->origin[1] + e->lightradius) e->maxs[1] = e->origin[1] + e->lightradius;
1211 if (e->mins[2] < e->origin[2] - e->lightradius) e->mins[2] = e->origin[2] - e->lightradius;
1212 if (e->maxs[2] > e->origin[2] + e->lightradius) e->maxs[2] = e->origin[2] + e->lightradius;
1219 for (j = 0;j < e->numsurfaces;j++)
1221 surf = e->surfaces[j];
1222 if (surf->flags & SURF_SHADOWCAST)
1224 surf->castshadow = castshadowcount;
1225 if (maxverts < surf->poly_numverts)
1226 maxverts = surf->poly_numverts;
1229 e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1230 // make a mesh to cast a shadow volume from
1231 castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1232 for (j = 0;j < e->numsurfaces;j++)
1233 if (e->surfaces[j]->castshadow == castshadowcount)
1234 for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
1235 Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->verts, surfmesh->numtriangles, surfmesh->index);
1236 castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
1238 // cast shadow volume from castmesh
1239 for (mesh = castmesh;mesh;mesh = mesh->next)
1241 R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
1242 R_Shadow_ResizeShadowElements(castmesh->numtriangles);
1244 if (maxverts < castmesh->numverts * 2)
1246 maxverts = castmesh->numverts * 2;
1251 if (verts == NULL && maxverts > 0)
1252 verts = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[4]));
1254 // now that we have the buffers big enough, construct shadow volume mesh
1255 memcpy(verts, castmesh->verts, castmesh->numverts * sizeof(float[4]));
1256 R_Shadow_ProjectVertices(verts, castmesh->numverts, e->origin, 10000000.0f);//, e->lightradius);
1257 R_Shadow_MakeTriangleShadowFlags(castmesh->elements, verts, castmesh->numtriangles, trianglefacinglight, e->origin, e->lightradius);
1258 tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->elements, castmesh->neighbors, castmesh->numtriangles, castmesh->numverts, trianglefacinglight, shadowelements);
1259 // add the constructed shadow volume mesh
1260 Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, verts, tris, shadowelements);
1262 // we're done with castmesh now
1263 Mod_ShadowMesh_Free(castmesh);
1264 e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
1265 for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
1266 l += mesh->numtriangles;
1267 Con_Printf("static shadow volume built containing %i triangles\n", l);
1270 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);
1273 void R_Shadow_FreeWorldLight(worldlight_t *light)
1275 worldlight_t **lightpointer;
1276 for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
1277 if (*lightpointer != light)
1278 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
1279 *lightpointer = light->next;
1280 if (light->cubemapname)
1281 Mem_Free(light->cubemapname);
1282 if (light->shadowvolume)
1283 Mod_ShadowMesh_Free(light->shadowvolume);
1284 if (light->surfaces)
1285 Mem_Free(light->surfaces);
1287 Mem_Free(light->leafs);
1291 void R_Shadow_ClearWorldLights(void)
1293 while (r_shadow_worldlightchain)
1294 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
1295 r_shadow_selectedlight = NULL;
1298 void R_Shadow_SelectLight(worldlight_t *light)
1300 if (r_shadow_selectedlight)
1301 r_shadow_selectedlight->selected = false;
1302 r_shadow_selectedlight = light;
1303 if (r_shadow_selectedlight)
1304 r_shadow_selectedlight->selected = true;
1308 void R_DrawLightSprite(int texnum, const vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
1315 VectorSubtract(origin, r_origin, diff);
1316 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
1319 memset(&m, 0, sizeof(m));
1320 m.blendfunc1 = GL_SRC_ALPHA;
1321 m.blendfunc2 = GL_ONE;
1323 R_Mesh_Matrix(&r_identitymatrix);
1326 GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1327 varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
1328 varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
1329 varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
1330 varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
1331 varray_vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
1332 varray_vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
1333 varray_vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
1334 varray_vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
1335 varray_vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
1336 varray_vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
1337 varray_vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
1338 varray_vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
1339 varray_vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
1340 varray_vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
1341 varray_vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
1342 varray_vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
1343 R_Mesh_Draw(4, 2, polygonelements);
1346 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
1349 pic = Draw_CachePic("gfx/crosshair1.tga");
1351 R_DrawLightSprite(R_GetTexture(pic->tex), r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 0.5);
1354 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
1357 const worldlight_t *light;
1360 if (light->selected)
1361 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
1362 if (light->shadowvolume)
1363 R_DrawLightSprite(calldata2, light->origin, 8, intensity, intensity, intensity, 0.5);
1365 R_DrawLightSprite(calldata2, light->origin, 8, intensity * 0.5, intensity * 0.5, intensity * 0.5, 0.5);
1368 void R_Shadow_DrawLightSprites(void)
1372 worldlight_t *light;
1374 for (i = 0;i < 5;i++)
1376 pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1));
1378 texnums[i] = R_GetTexture(pic->tex);
1383 for (light = r_shadow_worldlightchain;light;light = light->next)
1384 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, texnums[((int) light) % 5]);
1385 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
1388 void R_Shadow_SelectLightInView(void)
1390 float bestrating, rating, temp[3];
1391 worldlight_t *best, *light;
1394 for (light = r_shadow_worldlightchain;light;light = light->next)
1396 VectorSubtract(light->origin, r_refdef.vieworg, temp);
1397 rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
1400 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
1401 if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
1403 bestrating = rating;
1408 R_Shadow_SelectLight(best);
1411 void R_Shadow_LoadWorldLights(void)
1413 int n, a, style, shadow;
1414 char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
1415 float origin[3], radius, color[3];
1416 if (cl.worldmodel == NULL)
1418 Con_Printf("No map loaded.\n");
1421 COM_StripExtension(cl.worldmodel->name, name);
1422 strcat(name, ".rtlights");
1423 lightsstring = COM_LoadFile(name, false);
1431 while (*s && *s != '\n')
1437 // check for modifier flags
1443 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);
1449 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);
1452 VectorScale(color, r_editlights_rtlightscolorscale.value, color);
1453 radius *= r_editlights_rtlightssizescale.value;
1454 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadow);
1459 Con_Printf("invalid rtlights file \"%s\"\n", name);
1460 Mem_Free(lightsstring);
1464 void R_Shadow_SaveWorldLights(void)
1466 worldlight_t *light;
1467 int bufchars, bufmaxchars;
1469 char name[MAX_QPATH];
1471 if (!r_shadow_worldlightchain)
1473 if (cl.worldmodel == NULL)
1475 Con_Printf("No map loaded.\n");
1478 COM_StripExtension(cl.worldmodel->name, name);
1479 strcat(name, ".rtlights");
1480 bufchars = bufmaxchars = 0;
1482 for (light = r_shadow_worldlightchain;light;light = light->next)
1484 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 : "");
1485 if (bufchars + strlen(line) > bufmaxchars)
1487 bufmaxchars = bufchars + strlen(line) + 2048;
1489 buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
1493 memcpy(buf, oldbuf, bufchars);
1499 memcpy(buf + bufchars, line, strlen(line));
1500 bufchars += strlen(line);
1504 COM_WriteFile(name, buf, bufchars);
1509 void R_Shadow_LoadLightsFile(void)
1512 char name[MAX_QPATH], *lightsstring, *s, *t;
1513 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
1514 if (cl.worldmodel == NULL)
1516 Con_Printf("No map loaded.\n");
1519 COM_StripExtension(cl.worldmodel->name, name);
1520 strcat(name, ".lights");
1521 lightsstring = COM_LoadFile(name, false);
1529 while (*s && *s != '\n')
1534 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);
1538 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);
1541 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
1542 radius = bound(15, radius, 4096);
1543 VectorScale(color, (2.0f / (8388608.0f)), color);
1544 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
1549 Con_Printf("invalid lights file \"%s\"\n", name);
1550 Mem_Free(lightsstring);
1554 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
1556 int entnum, style, islight;
1557 char key[256], value[1024];
1558 float origin[3], radius, color[3], light, scale, originhack[3], overridecolor[3];
1561 if (cl.worldmodel == NULL)
1563 Con_Printf("No map loaded.\n");
1566 data = cl.worldmodel->entities;
1569 for (entnum = 0;COM_ParseToken(&data) && com_token[0] == '{';entnum++)
1572 origin[0] = origin[1] = origin[2] = 0;
1573 originhack[0] = originhack[1] = originhack[2] = 0;
1574 color[0] = color[1] = color[2] = 1;
1575 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
1581 if (!COM_ParseToken(&data))
1583 if (com_token[0] == '}')
1584 break; // end of entity
1585 if (com_token[0] == '_')
1586 strcpy(key, com_token + 1);
1588 strcpy(key, com_token);
1589 while (key[strlen(key)-1] == ' ') // remove trailing spaces
1590 key[strlen(key)-1] = 0;
1591 if (!COM_ParseToken(&data))
1593 strcpy(value, com_token);
1595 // now that we have the key pair worked out...
1596 if (!strcmp("light", key))
1597 light = atof(value);
1598 else if (!strcmp("origin", key))
1599 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
1600 else if (!strcmp("color", key))
1601 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
1602 else if (!strcmp("wait", key))
1603 scale = atof(value);
1604 else if (!strcmp("classname", key))
1606 if (!strncmp(value, "light", 5))
1609 if (!strcmp(value, "light_fluoro"))
1614 overridecolor[0] = 1;
1615 overridecolor[1] = 1;
1616 overridecolor[2] = 1;
1618 if (!strcmp(value, "light_fluorospark"))
1623 overridecolor[0] = 1;
1624 overridecolor[1] = 1;
1625 overridecolor[2] = 1;
1627 if (!strcmp(value, "light_globe"))
1632 overridecolor[0] = 1;
1633 overridecolor[1] = 0.8;
1634 overridecolor[2] = 0.4;
1636 if (!strcmp(value, "light_flame_large_yellow"))
1641 overridecolor[0] = 1;
1642 overridecolor[1] = 0.5;
1643 overridecolor[2] = 0.1;
1645 if (!strcmp(value, "light_flame_small_yellow"))
1650 overridecolor[0] = 1;
1651 overridecolor[1] = 0.5;
1652 overridecolor[2] = 0.1;
1654 if (!strcmp(value, "light_torch_small_white"))
1659 overridecolor[0] = 1;
1660 overridecolor[1] = 0.5;
1661 overridecolor[2] = 0.1;
1663 if (!strcmp(value, "light_torch_small_walltorch"))
1668 overridecolor[0] = 1;
1669 overridecolor[1] = 0.5;
1670 overridecolor[2] = 0.1;
1674 else if (!strcmp("style", key))
1675 style = atoi(value);
1677 if (light <= 0 && islight)
1679 radius = bound(15, light * r_editlights_quakelightsizescale.value / scale, 1048576);
1680 light = sqrt(bound(0, light, 1048576)) * (1.0f / 16.0f);
1681 if (color[0] == 1 && color[1] == 1 && color[2] == 1)
1682 VectorCopy(overridecolor, color);
1683 VectorScale(color, light, color);
1684 VectorAdd(origin, originhack, origin);
1686 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
1691 void R_Shadow_SetCursorLocationForView(void)
1693 vec_t dist, push, frac;
1694 vec3_t dest, endpos, normal;
1695 VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
1696 frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
1699 dist = frac * r_editlights_cursordistance.value;
1700 push = r_editlights_cursorpushback.value;
1704 VectorMA(endpos, push, vpn, endpos);
1705 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
1707 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1708 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1709 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1712 void R_Shadow_UpdateLightingMode(void)
1714 r_shadow_lightingmode = 0;
1715 if (r_shadow_realtime.integer)
1717 if (r_shadow_worldlightchain)
1718 r_shadow_lightingmode = 2;
1720 r_shadow_lightingmode = 1;
1724 void R_Shadow_UpdateWorldLightSelection(void)
1726 R_Shadow_SetCursorLocationForView();
1727 if (r_editlights.integer)
1729 R_Shadow_SelectLightInView();
1730 R_Shadow_DrawLightSprites();
1733 R_Shadow_SelectLight(NULL);
1736 void R_Shadow_EditLights_Clear_f(void)
1738 R_Shadow_ClearWorldLights();
1741 void R_Shadow_EditLights_Reload_f(void)
1743 r_shadow_reloadlights = true;
1746 void R_Shadow_EditLights_Save_f(void)
1749 R_Shadow_SaveWorldLights();
1752 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
1754 R_Shadow_ClearWorldLights();
1755 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
1758 void R_Shadow_EditLights_ImportLightsFile_f(void)
1760 R_Shadow_ClearWorldLights();
1761 R_Shadow_LoadLightsFile();
1764 void R_Shadow_EditLights_Spawn_f(void)
1767 if (!r_editlights.integer)
1769 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1772 if (Cmd_Argc() != 1)
1774 Con_Printf("r_editlights_spawn does not take parameters\n");
1777 color[0] = color[1] = color[2] = 1;
1778 R_Shadow_NewWorldLight(r_editlights_cursorlocation, 200, color, 0, NULL, true);
1781 void R_Shadow_EditLights_Edit_f(void)
1783 vec3_t origin, color;
1786 char cubemapname[1024];
1787 if (!r_editlights.integer)
1789 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1792 if (!r_shadow_selectedlight)
1794 Con_Printf("No selected light.\n");
1797 VectorCopy(r_shadow_selectedlight->origin, origin);
1798 radius = r_shadow_selectedlight->lightradius;
1799 VectorCopy(r_shadow_selectedlight->light, color);
1800 style = r_shadow_selectedlight->style;
1801 if (r_shadow_selectedlight->cubemapname)
1802 strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
1805 shadows = r_shadow_selectedlight->castshadows;
1806 if (!strcmp(Cmd_Argv(1), "origin"))
1808 if (Cmd_Argc() != 5)
1810 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
1813 origin[0] = atof(Cmd_Argv(2));
1814 origin[1] = atof(Cmd_Argv(3));
1815 origin[2] = atof(Cmd_Argv(4));
1817 else if (!strcmp(Cmd_Argv(1), "originx"))
1819 if (Cmd_Argc() != 3)
1821 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1824 origin[0] = atof(Cmd_Argv(2));
1826 else if (!strcmp(Cmd_Argv(1), "originy"))
1828 if (Cmd_Argc() != 3)
1830 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1833 origin[1] = atof(Cmd_Argv(2));
1835 else if (!strcmp(Cmd_Argv(1), "originz"))
1837 if (Cmd_Argc() != 3)
1839 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1842 origin[2] = atof(Cmd_Argv(2));
1844 else if (!strcmp(Cmd_Argv(1), "move"))
1846 if (Cmd_Argc() != 5)
1848 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
1851 origin[0] += atof(Cmd_Argv(2));
1852 origin[1] += atof(Cmd_Argv(3));
1853 origin[2] += atof(Cmd_Argv(4));
1855 else if (!strcmp(Cmd_Argv(1), "movex"))
1857 if (Cmd_Argc() != 3)
1859 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1862 origin[0] += atof(Cmd_Argv(2));
1864 else if (!strcmp(Cmd_Argv(1), "movey"))
1866 if (Cmd_Argc() != 3)
1868 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1871 origin[1] += atof(Cmd_Argv(2));
1873 else if (!strcmp(Cmd_Argv(1), "movez"))
1875 if (Cmd_Argc() != 3)
1877 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1880 origin[2] += atof(Cmd_Argv(2));
1882 else if (!strcmp(Cmd_Argv(1), "color"))
1884 if (Cmd_Argc() != 5)
1886 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(0));
1889 color[0] = atof(Cmd_Argv(2));
1890 color[1] = atof(Cmd_Argv(3));
1891 color[2] = atof(Cmd_Argv(4));
1893 else if (!strcmp(Cmd_Argv(1), "radius"))
1895 if (Cmd_Argc() != 3)
1897 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1900 radius = atof(Cmd_Argv(2));
1902 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "style"))
1904 if (Cmd_Argc() != 3)
1906 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1909 style = atoi(Cmd_Argv(2));
1911 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "cubemap"))
1915 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1918 if (Cmd_Argc() == 3)
1919 strcpy(cubemapname, Cmd_Argv(2));
1923 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "shadows"))
1925 if (Cmd_Argc() != 3)
1927 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
1930 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
1934 Con_Printf("usage: r_editlights_edit [property] [value]\n");
1935 Con_Printf("Selected light's properties:\n");
1936 Con_Printf("Origin: %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
1937 Con_Printf("Radius: %f\n", r_shadow_selectedlight->lightradius);
1938 Con_Printf("Color: %f %f %f\n", r_shadow_selectedlight->light[0], r_shadow_selectedlight->light[1], r_shadow_selectedlight->light[2]);
1939 Con_Printf("Style: %i\n", r_shadow_selectedlight->style);
1940 Con_Printf("Cubemap: %s\n", r_shadow_selectedlight->cubemapname);
1941 Con_Printf("Shadows: %s\n", r_shadow_selectedlight->castshadows ? "yes" : "no");
1944 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1945 r_shadow_selectedlight = NULL;
1946 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadows);
1949 extern int con_vislines;
1950 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
1954 if (r_shadow_selectedlight == NULL)
1958 sprintf(temp, "Light properties");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1959 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;
1960 sprintf(temp, "Radius %f", r_shadow_selectedlight->lightradius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1961 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;
1962 sprintf(temp, "Style %i", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1963 sprintf(temp, "Cubemap %s", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
1964 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;
1967 void R_Shadow_EditLights_ToggleShadow_f(void)
1969 if (!r_editlights.integer)
1971 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1974 if (!r_shadow_selectedlight)
1976 Con_Printf("No selected light.\n");
1979 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);
1980 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1981 r_shadow_selectedlight = NULL;
1984 void R_Shadow_EditLights_Remove_f(void)
1986 if (!r_editlights.integer)
1988 Con_Printf("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
1991 if (!r_shadow_selectedlight)
1993 Con_Printf("No selected light.\n");
1996 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1997 r_shadow_selectedlight = NULL;
2000 void R_Shadow_EditLights_Init(void)
2002 Cvar_RegisterVariable(&r_editlights);
2003 Cvar_RegisterVariable(&r_editlights_cursordistance);
2004 Cvar_RegisterVariable(&r_editlights_cursorpushback);
2005 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
2006 Cvar_RegisterVariable(&r_editlights_cursorgrid);
2007 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
2008 Cvar_RegisterVariable(&r_editlights_rtlightssizescale);
2009 Cvar_RegisterVariable(&r_editlights_rtlightscolorscale);
2010 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
2011 Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
2012 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
2013 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
2014 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
2015 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
2016 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f);
2017 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
2018 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);