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_normalsattenuationtexture;
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_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "2"};
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_erasebydrawing = {0, "r_shadow_erasebydrawing", "0"};
37 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "0"};
38 cvar_t r_shadow_gloss = {0, "r_shadow_gloss", "1"};
39 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
40 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
41 cvar_t r_shadow_bumpscale = {0, "r_shadow_bumpscale", "4"};
43 void R_Shadow_ClearWorldLights(void);
44 void R_Shadow_SaveWorldLights(void);
45 void R_Shadow_LoadWorldLights(void);
46 void R_Shadow_LoadLightsFile(void);
47 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
49 void r_shadow_start(void)
51 // allocate vertex processing arrays
52 r_shadow_mempool = Mem_AllocPool("R_Shadow");
53 maxshadowelements = 0;
54 shadowelements = NULL;
55 maxtrianglefacinglight = 0;
56 trianglefacinglight = NULL;
57 r_shadow_normalsattenuationtexture = NULL;
58 r_shadow_normalscubetexture = NULL;
59 r_shadow_attenuation2dtexture = NULL;
60 r_shadow_blankbumptexture = NULL;
61 r_shadow_blankglosstexture = NULL;
62 r_shadow_blankwhitetexture = NULL;
63 r_shadow_texturepool = NULL;
64 R_Shadow_ClearWorldLights();
65 r_shadow_reloadlights = true;
68 void r_shadow_shutdown(void)
70 R_Shadow_ClearWorldLights();
71 r_shadow_reloadlights = true;
72 r_shadow_normalsattenuationtexture = NULL;
73 r_shadow_normalscubetexture = NULL;
74 r_shadow_attenuation2dtexture = NULL;
75 r_shadow_blankbumptexture = NULL;
76 r_shadow_blankglosstexture = NULL;
77 r_shadow_blankwhitetexture = NULL;
78 R_FreeTexturePool(&r_shadow_texturepool);
79 maxshadowelements = 0;
80 shadowelements = NULL;
81 maxtrianglefacinglight = 0;
82 trianglefacinglight = NULL;
83 Mem_FreePool(&r_shadow_mempool);
86 void r_shadow_newmap(void)
88 R_Shadow_ClearWorldLights();
89 r_shadow_reloadlights = true;
92 void R_Shadow_Init(void)
94 Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
95 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
96 Cvar_RegisterVariable(&r_shadow_realtime);
97 Cvar_RegisterVariable(&r_shadow_texture3d);
98 Cvar_RegisterVariable(&r_shadow_gloss);
99 Cvar_RegisterVariable(&r_shadow_debuglight);
100 Cvar_RegisterVariable(&r_shadow_erasebydrawing);
101 Cvar_RegisterVariable(&r_shadow_scissor);
102 Cvar_RegisterVariable(&r_shadow_bumpscale);
103 R_Shadow_EditLights_Init();
104 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
107 void R_Shadow_ProjectVertices(const float *in, float *out, int numverts, const float *relativelightorigin, float projectdistance)
110 for (i = 0;i < numverts;i++, in += 4, out += 4)
113 out[0] = in[0] + 1000000.0f * (in[0] - relativelightorigin[0]);
114 out[1] = in[1] + 1000000.0f * (in[1] - relativelightorigin[1]);
115 out[2] = in[2] + 1000000.0f * (in[2] - relativelightorigin[2]);
117 VectorSubtract(in, relativelightorigin, temp);
118 f = lightradius / sqrt(DotProduct(temp,temp));
121 VectorMA(relativelightorigin, f, temp, out);
123 VectorSubtract(in, relativelightorigin, temp);
124 f = projectdistance / sqrt(DotProduct(temp,temp));
125 VectorMA(in, f, temp, out);
130 void R_Shadow_MakeTriangleShadowFlags(const int *elements, const float *vertex, int numtris, qbyte *trianglefacinglight, const float *relativelightorigin, float lightradius)
133 const float *v0, *v1, *v2;
134 for (i = 0;i < numtris;i++, elements += 3)
136 // calculate triangle facing flag
137 v0 = vertex + elements[0] * 4;
138 v1 = vertex + elements[1] * 4;
139 v2 = vertex + elements[2] * 4;
140 // we do not need to normalize the surface normal because both sides
141 // of the comparison use it, therefore they are both multiplied the
142 // same amount... furthermore the subtract can be done on the
143 // vectors, saving a little bit of math in the dotproducts
146 // subtracts v1 from v0 and v2, combined into a crossproduct,
147 // combined with a dotproduct of the light location relative to the
148 // first point of the triangle (any point works, since the triangle
149 // is obviously flat), and finally a comparison to determine if the
150 // light is infront of the triangle (the goal of this statement)
151 trianglefacinglight[i] =
152 (relativelightorigin[0] - v0[0]) * ((v0[1] - v1[1]) * (v2[2] - v1[2]) - (v0[2] - v1[2]) * (v2[1] - v1[1]))
153 + (relativelightorigin[1] - v0[1]) * ((v0[2] - v1[2]) * (v2[0] - v1[0]) - (v0[0] - v1[0]) * (v2[2] - v1[2]))
154 + (relativelightorigin[2] - v0[2]) * ((v0[0] - v1[0]) * (v2[1] - v1[1]) - (v0[1] - v1[1]) * (v2[0] - v1[0])) > 0;
158 float dir0[3], dir1[3], temp[3], f;
160 // calculate two mostly perpendicular edge directions
161 VectorSubtract(v0, v1, dir0);
162 VectorSubtract(v2, v1, dir1);
164 // we have two edge directions, we can calculate a third vector from
165 // them, which is the direction of the surface normal (it's magnitude
167 CrossProduct(dir0, dir1, temp);
169 // this is entirely unnecessary, but kept for clarity
170 //VectorNormalize(temp);
172 // compare distance of light along normal, with distance of any point
173 // of the triangle along the same normal (the triangle is planar,
174 // I.E. flat, so all points give the same answer)
175 // the normal is not normalized because it is used on both sides of
176 // the comparison, so it's magnitude does not matter
177 //trianglefacinglight[i] = DotProduct(relativelightorigin, temp) >= DotProduct(v0, temp);
178 f = DotProduct(relativelightorigin, temp) - DotProduct(v0, temp);
179 trianglefacinglight[i] = f > 0 && f < lightradius * sqrt(DotProduct(temp, temp));
185 int R_Shadow_BuildShadowVolumeTriangles(const int *elements, const int *neighbors, int numtris, int numverts, const qbyte *trianglefacinglight, int *out)
188 // check each frontface for bordering backfaces,
189 // and cast shadow polygons from those edges,
190 // also create front and back caps for shadow volume
192 for (i = 0;i < numtris;i++, elements += 3, neighbors += 3)
194 if (trianglefacinglight[i])
196 // triangle is frontface and therefore casts shadow,
197 // output front and back caps for shadow volume
199 out[0] = elements[0];
200 out[1] = elements[1];
201 out[2] = elements[2];
202 // rear cap (with flipped winding order)
203 out[3] = elements[0] + numverts;
204 out[4] = elements[2] + numverts;
205 out[5] = elements[1] + numverts;
209 if (neighbors[0] < 0 || !trianglefacinglight[neighbors[0]])
211 out[0] = elements[1];
212 out[1] = elements[0];
213 out[2] = elements[0] + numverts;
214 out[3] = elements[1];
215 out[4] = elements[0] + numverts;
216 out[5] = elements[1] + numverts;
220 if (neighbors[1] < 0 || !trianglefacinglight[neighbors[1]])
222 out[0] = elements[2];
223 out[1] = elements[1];
224 out[2] = elements[1] + numverts;
225 out[3] = elements[2];
226 out[4] = elements[1] + numverts;
227 out[5] = elements[2] + numverts;
231 if (neighbors[2] < 0 || !trianglefacinglight[neighbors[2]])
233 out[0] = elements[0];
234 out[1] = elements[2];
235 out[2] = elements[2] + numverts;
236 out[3] = elements[0];
237 out[4] = elements[2] + numverts;
238 out[5] = elements[0] + numverts;
247 void R_Shadow_ResizeTriangleFacingLight(int numtris)
249 // make sure trianglefacinglight is big enough for this volume
250 if (maxtrianglefacinglight < numtris)
252 maxtrianglefacinglight = numtris;
253 if (trianglefacinglight)
254 Mem_Free(trianglefacinglight);
255 trianglefacinglight = Mem_Alloc(r_shadow_mempool, maxtrianglefacinglight);
259 void R_Shadow_ResizeShadowElements(int numtris)
261 // make sure shadowelements is big enough for this volume
262 if (maxshadowelements < numtris * 24)
264 maxshadowelements = numtris * 24;
266 Mem_Free(shadowelements);
267 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
271 void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance)
274 if (projectdistance < 0.1)
276 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
282 // a triangle facing the light source
285 // a triangle not facing the light source
288 // an extrusion of the frontfaces, beginning at the original geometry and
289 // ending further from the light source than the original geometry
290 // (presumably at least as far as the light's radius, if the light has a
291 // radius at all), capped at both front and back to avoid any problems
294 // draws the shadow volumes of the model.
296 // vertex locations must already be in varray_vertex before use.
297 // varray_vertex must have capacity for numverts * 2.
299 // make sure trianglefacinglight is big enough for this volume
300 if (maxtrianglefacinglight < numtris)
301 R_Shadow_ResizeTriangleFacingLight(numtris);
303 // make sure shadowelements is big enough for this volume
304 if (maxshadowelements < numtris * 24)
305 R_Shadow_ResizeShadowElements(numtris);
307 // generate projected vertices
308 // by clever use of elements we'll construct the whole shadow from
309 // the unprojected vertices and these projected vertices
310 R_Shadow_ProjectVertices(varray_vertex, varray_vertex + numverts * 4, numverts, relativelightorigin, projectdistance);
312 // check which triangles are facing the light
313 R_Shadow_MakeTriangleShadowFlags(elements, varray_vertex, numtris, trianglefacinglight, relativelightorigin, lightradius);
315 // output triangle elements
316 tris = R_Shadow_BuildShadowVolumeTriangles(elements, neighbors, numtris, numverts, trianglefacinglight, shadowelements);
317 R_Shadow_RenderVolume(numverts * 2, tris, shadowelements);
320 void R_Shadow_RenderVolume(int numverts, int numtris, int *elements)
322 if (!numverts || !numtris)
324 if (r_shadowstage == SHADOWSTAGE_STENCIL)
326 // increment stencil if backface is behind depthbuffer
327 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
328 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
329 R_Mesh_Draw(numverts, numtris, elements);
330 // decrement stencil if frontface is behind depthbuffer
331 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
332 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
334 R_Mesh_Draw(numverts, numtris, elements);
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);
351 // decrement stencil if frontface is behind depthbuffer
352 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
353 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
355 for (mesh = firstmesh;mesh;mesh = mesh->next)
357 R_Mesh_ResizeCheck(mesh->numverts);
358 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
359 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->elements);
363 float r_shadow_atten1;
364 #define ATTEN3DSIZE 64
365 static void R_Shadow_Make3DTextures(void)
368 float v[3], intensity, ilen, bordercolor[4];
370 if (r_shadow_texture3d.integer != 1 || !gl_texture3d)
372 data = Mem_Alloc(tempmempool, ATTEN3DSIZE * ATTEN3DSIZE * ATTEN3DSIZE * 4);
373 for (z = 0;z < ATTEN3DSIZE;z++)
375 for (y = 0;y < ATTEN3DSIZE;y++)
377 for (x = 0;x < ATTEN3DSIZE;x++)
379 v[0] = (x + 0.5f) * (2.0f / (float) ATTEN3DSIZE) - 1.0f;
380 v[1] = (y + 0.5f) * (2.0f / (float) ATTEN3DSIZE) - 1.0f;
381 v[2] = (z + 0.5f) * (2.0f / (float) ATTEN3DSIZE) - 1.0f;
382 intensity = 1.0f - sqrt(DotProduct(v, v));
384 intensity *= intensity;
385 ilen = 127.0f * bound(0, intensity * r_shadow_atten1, 1) / sqrt(DotProduct(v, v));
386 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+0] = 128.0f + ilen * v[0];
387 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+1] = 128.0f + ilen * v[1];
388 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+2] = 128.0f + ilen * v[2];
389 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+3] = 255;
393 r_shadow_normalsattenuationtexture = R_LoadTexture3D(r_shadow_texturepool, "normalsattenuation", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
394 bordercolor[0] = 0.5f;
395 bordercolor[1] = 0.5f;
396 bordercolor[2] = 0.5f;
397 bordercolor[3] = 1.0f;
398 qglTexParameterfv(GL_TEXTURE_3D, GL_TEXTURE_BORDER_COLOR, bordercolor);
402 static void R_Shadow_MakeTextures(void)
405 float v[3], s, t, intensity;
407 data = Mem_Alloc(tempmempool, 6*128*128*4);
408 R_FreeTexturePool(&r_shadow_texturepool);
409 r_shadow_texturepool = R_AllocTexturePool();
410 r_shadow_atten1 = r_shadow_lightattenuationscale.value;
415 r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
420 r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
425 r_shadow_blankwhitetexture = R_LoadTexture2D(r_shadow_texturepool, "blankwhite", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
426 for (side = 0;side < 6;side++)
428 for (y = 0;y < 128;y++)
430 for (x = 0;x < 128;x++)
432 s = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
433 t = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
467 intensity = 127.0f / sqrt(DotProduct(v, v));
468 data[((side*128+y)*128+x)*4+0] = 128.0f + intensity * v[0];
469 data[((side*128+y)*128+x)*4+1] = 128.0f + intensity * v[1];
470 data[((side*128+y)*128+x)*4+2] = 128.0f + intensity * v[2];
471 data[((side*128+y)*128+x)*4+3] = 255;
475 r_shadow_normalscubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalscube", 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
476 for (y = 0;y < 128;y++)
478 for (x = 0;x < 128;x++)
480 v[0] = (x + 0.5f) * (2.0f / 128.0f) - 1.0f;
481 v[1] = (y + 0.5f) * (2.0f / 128.0f) - 1.0f;
483 intensity = 1.0f - sqrt(DotProduct(v, v));
485 intensity *= intensity;
486 intensity = bound(0, intensity * r_shadow_atten1 * 256.0f, 255.0f);
487 d = bound(0, intensity, 255);
488 data[((0*128+y)*128+x)*4+0] = d;
489 data[((0*128+y)*128+x)*4+1] = d;
490 data[((0*128+y)*128+x)*4+2] = d;
491 data[((0*128+y)*128+x)*4+3] = d;
494 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", 128, 128, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_MIPMAP, NULL);
496 R_Shadow_Make3DTextures();
499 void R_Shadow_Stage_Begin(void)
503 if (r_shadow_texture3d.integer == 1 && !gl_texture3d)
505 Con_Printf("3D texture support not detected, falling back on slower 2D + 1D + normalization lighting\n");
506 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
508 //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
509 if (!r_shadow_attenuation2dtexture
510 || (r_shadow_texture3d.integer == 1 && !r_shadow_normalsattenuationtexture)
511 || r_shadow_lightattenuationscale.value != r_shadow_atten1)
512 R_Shadow_MakeTextures();
513 if (r_shadow_reloadlights && cl.worldmodel)
515 r_shadow_reloadlights = false;
516 R_Shadow_LoadWorldLights();
517 if (r_shadow_worldlightchain == NULL)
519 R_Shadow_LoadLightsFile();
520 if (r_shadow_worldlightchain == NULL)
521 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
525 memset(&m, 0, sizeof(m));
526 m.blendfunc1 = GL_ONE;
527 m.blendfunc2 = GL_ZERO;
529 GL_Color(0, 0, 0, 1);
530 r_shadowstage = SHADOWSTAGE_NONE;
533 void R_Shadow_Stage_ShadowVolumes(void)
536 memset(&m, 0, sizeof(m));
537 R_Mesh_TextureState(&m);
538 GL_Color(1, 1, 1, 1);
539 qglColorMask(0, 0, 0, 0);
540 qglDisable(GL_BLEND);
542 qglDepthFunc(GL_LESS);
543 qglEnable(GL_STENCIL_TEST);
544 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
545 qglStencilFunc(GL_ALWAYS, 0, 0xFF);
546 qglEnable(GL_CULL_FACE);
547 qglEnable(GL_DEPTH_TEST);
548 r_shadowstage = SHADOWSTAGE_STENCIL;
549 if (!r_shadow_erasebydrawing.integer)
550 qglClear(GL_STENCIL_BUFFER_BIT);
553 void R_Shadow_Stage_Light(void)
556 memset(&m, 0, sizeof(m));
557 R_Mesh_TextureState(&m);
558 qglActiveTexture(GL_TEXTURE0_ARB);
561 qglBlendFunc(GL_ONE, GL_ONE);
562 GL_Color(1, 1, 1, 1);
563 qglColorMask(1, 1, 1, 1);
565 qglDepthFunc(GL_EQUAL);
566 qglEnable(GL_STENCIL_TEST);
567 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
568 // only draw light where this geometry was already rendered AND the
569 // stencil is 0 (non-zero means shadow)
570 qglStencilFunc(GL_EQUAL, 0, 0xFF);
571 qglEnable(GL_CULL_FACE);
572 qglEnable(GL_DEPTH_TEST);
573 r_shadowstage = SHADOWSTAGE_LIGHT;
576 int R_Shadow_Stage_EraseShadowVolumes(void)
578 if (r_shadow_erasebydrawing.integer)
581 memset(&m, 0, sizeof(m));
582 R_Mesh_TextureState(&m);
583 GL_Color(1, 1, 1, 1);
584 qglColorMask(0, 0, 0, 0);
585 qglDisable(GL_BLEND);
587 qglDepthFunc(GL_LESS);
588 qglEnable(GL_STENCIL_TEST);
589 qglStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
590 qglStencilFunc(GL_ALWAYS, 0, 0xFF);
591 qglDisable(GL_CULL_FACE);
592 qglDisable(GL_DEPTH_TEST);
593 r_shadowstage = SHADOWSTAGE_ERASESTENCIL;
600 void R_Shadow_Stage_End(void)
603 // attempt to restore state to what Mesh_State thinks it is
604 qglDisable(GL_BLEND);
605 qglBlendFunc(GL_ONE, GL_ZERO);
607 // now restore the rest of the state to normal
608 GL_Color(1, 1, 1, 1);
609 qglColorMask(1, 1, 1, 1);
610 qglDisable(GL_SCISSOR_TEST);
611 qglDepthFunc(GL_LEQUAL);
612 qglDisable(GL_STENCIL_TEST);
613 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
614 qglStencilFunc(GL_ALWAYS, 0, 0xFF);
615 qglEnable(GL_CULL_FACE);
616 qglEnable(GL_DEPTH_TEST);
617 // force mesh state to reset by using various combinations of features
618 memset(&m, 0, sizeof(m));
619 m.blendfunc1 = GL_SRC_ALPHA;
620 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
622 m.blendfunc1 = GL_ONE;
623 m.blendfunc2 = GL_ZERO;
625 r_shadowstage = SHADOWSTAGE_NONE;
628 int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const float *origin, float radius)
630 int i, ix1, iy1, ix2, iy2;
631 float x1, y1, x2, y2, x, y;
634 if (!r_shadow_scissor.integer)
636 // if view is inside the box, just say yes it's visible
637 if (r_origin[0] >= mins[0] && r_origin[0] <= maxs[0]
638 && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1]
639 && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2])
641 qglDisable(GL_SCISSOR_TEST);
644 VectorSubtract(r_origin, origin, v);
645 if (DotProduct(v, v) < radius * radius)
647 qglDisable(GL_SCISSOR_TEST);
650 // create viewspace bbox
651 for (i = 0;i < 8;i++)
653 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
654 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
655 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
656 v2[0] = DotProduct(v, vright);
657 v2[1] = DotProduct(v, vup);
658 v2[2] = DotProduct(v, vpn);
661 if (smins[0] > v2[0]) smins[0] = v2[0];
662 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
663 if (smins[1] > v2[1]) smins[1] = v2[1];
664 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
665 if (smins[2] > v2[2]) smins[2] = v2[2];
666 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
670 smins[0] = smaxs[0] = v2[0];
671 smins[1] = smaxs[1] = v2[1];
672 smins[2] = smaxs[2] = v2[2];
675 // now we have a bbox in viewspace
676 // clip it to the viewspace version of the sphere
677 v[0] = origin[0] - r_origin[0];
678 v[1] = origin[1] - r_origin[1];
679 v[2] = origin[2] - r_origin[2];
680 v2[0] = DotProduct(v, vright);
681 v2[1] = DotProduct(v, vup);
682 v2[2] = DotProduct(v, vpn);
683 if (smins[0] < v2[0] - radius) smins[0] = v2[0] - radius;
684 if (smaxs[0] < v2[0] - radius) smaxs[0] = v2[0] + radius;
685 if (smins[1] < v2[1] - radius) smins[1] = v2[1] - radius;
686 if (smaxs[1] < v2[1] - radius) smaxs[1] = v2[1] + radius;
687 if (smins[2] < v2[2] - radius) smins[2] = v2[2] - radius;
688 if (smaxs[2] < v2[2] - radius) smaxs[2] = v2[2] + radius;
689 // clip it to the view plane
692 // return true if that culled the box
693 if (smins[2] >= smaxs[2])
695 // ok some of it is infront of the view, transform each corner back to
696 // worldspace and then to screenspace and make screen rect
697 for (i = 0;i < 8;i++)
699 v2[0] = (i & 1) ? smins[0] : smaxs[0];
700 v2[1] = (i & 2) ? smins[1] : smaxs[1];
701 v2[2] = (i & 4) ? smins[2] : smaxs[2];
702 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
703 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
704 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
706 GL_TransformToScreen(v, v2);
707 //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]);
724 // this code doesn't handle boxes with any points behind view properly
725 x1 = 1000;x2 = -1000;
726 y1 = 1000;y2 = -1000;
727 for (i = 0;i < 8;i++)
729 v[0] = (i & 1) ? mins[0] : maxs[0];
730 v[1] = (i & 2) ? mins[1] : maxs[1];
731 v[2] = (i & 4) ? mins[2] : maxs[2];
733 GL_TransformToScreen(v, v2);
734 //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]);
751 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
752 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
753 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
754 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
755 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
756 if (ix2 <= ix1 || iy2 <= iy1)
758 // set up the scissor rectangle
759 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
760 qglEnable(GL_SCISSOR_TEST);
764 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)
767 float lightvec[3], iradius;
768 iradius = 0.5f / lightradius;
769 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out2d += 4, out1d += 4)
771 VectorSubtract(vertex, relativelightorigin, lightvec);
772 out2d[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
773 out2d[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
775 out1d[0] = 0.5f + DotProduct(normals, lightvec) * iradius;
781 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)
784 float lightvec[3], 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, lightvec);
789 out[0] = 0.5f + DotProduct(svectors, lightvec) * iradius;
790 out[1] = 0.5f + DotProduct(tvectors, lightvec) * iradius;
791 out[2] = 0.5f + DotProduct(normals, lightvec) * iradius;
795 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)
799 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
801 VectorSubtract(vertex, relativelightorigin, lightdir);
802 // the cubemap normalizes this for us
803 out[0] = DotProduct(svectors, lightdir);
804 out[1] = DotProduct(tvectors, lightdir);
805 out[2] = DotProduct(normals, lightdir);
809 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)
812 float lightdir[3], eyedir[3], halfdir[3], lightdirlen, iradius;
813 iradius = 0.5f / lightradius;
814 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
816 VectorSubtract(vertex, relativelightorigin, lightdir);
817 // this is used later to make the attenuation correct
818 lightdirlen = sqrt(DotProduct(lightdir, lightdir)) * iradius;
819 VectorNormalizeFast(lightdir);
820 VectorSubtract(vertex, relativeeyeorigin, eyedir);
821 VectorNormalizeFast(eyedir);
822 VectorAdd(lightdir, eyedir, halfdir);
823 VectorNormalizeFast(halfdir);
824 out[0] = 0.5f + DotProduct(svectors, halfdir) * lightdirlen;
825 out[1] = 0.5f + DotProduct(tvectors, halfdir) * lightdirlen;
826 out[2] = 0.5f + DotProduct(normals, halfdir) * lightdirlen;
830 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)
833 float lightdir[3], eyedir[3], halfdir[3];
834 for (i = 0;i < numverts;i++, vertex += 4, svectors += 4, tvectors += 4, normals += 4, out += 4)
836 VectorSubtract(vertex, relativelightorigin, lightdir);
837 VectorNormalizeFast(lightdir);
838 VectorSubtract(vertex, relativeeyeorigin, eyedir);
839 VectorNormalizeFast(eyedir);
840 VectorAdd(lightdir, eyedir, halfdir);
841 // the cubemap normalizes this for us
842 out[0] = DotProduct(svectors, halfdir);
843 out[1] = DotProduct(tvectors, halfdir);
844 out[2] = DotProduct(normals, halfdir);
848 void R_Shadow_GenTexCoords_LightCubeMap(float *out, int numverts, const float *vertex, const vec3_t relativelightorigin)
851 // FIXME: this needs to be written
852 // this code assumes the vertices are in worldspace (a false assumption)
853 for (i = 0;i < numverts;i++, vertex += 4, out += 4)
854 VectorSubtract(vertex, relativelightorigin, out);
857 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)
860 float scale, colorscale;
862 memset(&m, 0, sizeof(m));
864 bumptexture = r_shadow_blankbumptexture;
865 // colorscale accounts for how much we multiply the brightness during combine
866 // mult is how many times the final pass of the lighting will be
867 // performed to get more brightness than otherwise possible
868 // limit mult to 64 for sanity sake
869 if (r_shadow_texture3d.integer)
871 if (r_textureunits.integer >= 4 && !lightcubemap)
873 // 4 texture 3D combine path, one pass, no light cubemap support
874 m.tex[0] = R_GetTexture(bumptexture);
875 m.tex3d[1] = R_GetTexture(r_shadow_normalsattenuationtexture);
876 m.tex[2] = R_GetTexture(basetexture);
877 m.tex[3] = R_GetTexture(r_shadow_blankwhitetexture);
878 m.texcombinergb[0] = GL_REPLACE;
879 m.texcombinergb[1] = GL_DOT3_RGB_ARB;
880 m.texcombinergb[2] = GL_MODULATE;
881 m.texcombinergb[3] = GL_MODULATE;
882 m.texrgbscale[1] = 1;
883 m.texrgbscale[3] = 4;
884 R_Mesh_TextureState(&m);
885 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
886 memcpy(varray_texcoord[2], texcoords, numverts * sizeof(float[4]));
887 R_Shadow_GenTexCoords_Diffuse_Attenuation3D(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
888 qglActiveTexture(GL_TEXTURE3_ARB);
889 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PRIMARY_COLOR_ARB);
890 colorscale = r_colorscale * 0.25f * r_shadow_lightintensityscale.value;
891 for (mult = 1, scale = ixtable[mult];mult < 64 && (lightcolor[0] * scale * colorscale > 1 || lightcolor[1] * scale * colorscale > 1 || lightcolor[2] * scale * colorscale > 1);mult++, scale = ixtable[mult]);
893 GL_Color(lightcolor[0] * colorscale, lightcolor[1] * colorscale, lightcolor[2] * colorscale, 1);
894 for (renders = 0;renders < mult;renders++)
895 R_Mesh_Draw(numverts, numtriangles, elements);
896 qglActiveTexture(GL_TEXTURE3_ARB);
897 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
901 // 2 texture no3D combine path, two pass
902 m.tex[0] = R_GetTexture(bumptexture);
903 m.tex3d[1] = R_GetTexture(r_shadow_normalsattenuationtexture);
904 m.texcombinergb[0] = GL_REPLACE;
905 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
906 m.texalphascale[1] = 1;
907 R_Mesh_TextureState(&m);
908 qglColorMask(0,0,0,1);
909 qglDisable(GL_BLEND);
911 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
912 R_Shadow_GenTexCoords_Diffuse_Attenuation3D(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
913 R_Mesh_Draw(numverts, numtriangles, elements);
915 m.tex[0] = R_GetTexture(basetexture);
917 m.texcubemap[1] = R_GetTexture(lightcubemap);
918 m.texcombinergb[0] = GL_MODULATE;
919 m.texcombinergb[1] = GL_MODULATE;
920 m.texrgbscale[1] = 1;
921 m.texalphascale[1] = 1;
922 R_Mesh_TextureState(&m);
923 qglColorMask(1,1,1,1);
924 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
927 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
929 colorscale = r_colorscale * 1.0f * r_shadow_lightintensityscale.value;
930 for (mult = 1, scale = ixtable[mult];mult < 64 && (lightcolor[0] * scale * colorscale > 1 || lightcolor[1] * scale * colorscale > 1 || lightcolor[2] * scale * colorscale > 1);mult++, scale = ixtable[mult]);
932 GL_Color(lightcolor[0] * colorscale, lightcolor[1] * colorscale, lightcolor[2] * colorscale, 1);
933 for (renders = 0;renders < mult;renders++)
934 R_Mesh_Draw(numverts, numtriangles, elements);
937 else if (r_textureunits.integer >= 4)
939 // 4 texture no3D combine path, two pass
940 m.tex[0] = R_GetTexture(bumptexture);
941 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
942 m.texcombinergb[0] = GL_REPLACE;
943 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
944 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
945 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
946 R_Mesh_TextureState(&m);
947 qglColorMask(0,0,0,1);
948 qglDisable(GL_BLEND);
950 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
951 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
952 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[2], varray_texcoord[3], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
953 R_Mesh_Draw(numverts, numtriangles, elements);
955 m.tex[0] = R_GetTexture(basetexture);
956 m.texcubemap[1] = R_GetTexture(lightcubemap);
957 m.texcombinergb[0] = GL_MODULATE;
958 m.texcombinergb[1] = GL_MODULATE;
961 R_Mesh_TextureState(&m);
962 qglColorMask(1,1,1,1);
963 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
966 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
968 colorscale = r_colorscale * 1.0f * r_shadow_lightintensityscale.value;
969 for (mult = 1, scale = ixtable[mult];mult < 64 && (lightcolor[0] * scale * colorscale > 1 || lightcolor[1] * scale * colorscale > 1 || lightcolor[2] * scale * colorscale > 1);mult++, scale = ixtable[mult]);
971 GL_Color(lightcolor[0] * colorscale, lightcolor[1] * colorscale, lightcolor[2] * colorscale, 1);
972 for (renders = 0;renders < mult;renders++)
973 R_Mesh_Draw(numverts, numtriangles, elements);
977 // 2 texture no3D combine path, three pass
978 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
979 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
980 R_Mesh_TextureState(&m);
981 qglColorMask(0,0,0,1);
982 qglDisable(GL_BLEND);
984 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
985 R_Mesh_Draw(numverts, numtriangles, elements);
987 m.tex[0] = R_GetTexture(bumptexture);
989 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
990 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
991 R_Mesh_TextureState(&m);
992 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
994 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
995 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin);
996 R_Mesh_Draw(numverts, numtriangles, elements);
998 m.tex[0] = R_GetTexture(basetexture);
999 m.texcubemap[1] = R_GetTexture(lightcubemap);
1000 m.texcombinergb[1] = GL_MODULATE;
1001 R_Mesh_TextureState(&m);
1002 qglColorMask(1,1,1,1);
1003 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1005 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
1007 colorscale = r_colorscale * 1.0f * r_shadow_lightintensityscale.value;
1008 for (mult = 1, scale = ixtable[mult];mult < 64 && (lightcolor[0] * scale * colorscale > 1 || lightcolor[1] * scale * colorscale > 1 || lightcolor[2] * scale * colorscale > 1);mult++, scale = ixtable[mult]);
1009 colorscale *= scale;
1010 GL_Color(lightcolor[0] * colorscale, lightcolor[1] * colorscale, lightcolor[2] * colorscale, 1);
1011 for (renders = 0;renders < mult;renders++)
1012 R_Mesh_Draw(numverts, numtriangles, elements);
1016 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)
1019 float scale, colorscale;
1021 memset(&m, 0, sizeof(m));
1023 bumptexture = r_shadow_blankbumptexture;
1025 glosstexture = r_shadow_blankglosstexture;
1026 if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
1028 // 2 texture no3D combine path, five pass
1029 memset(&m, 0, sizeof(m));
1031 m.tex[0] = R_GetTexture(bumptexture);
1032 m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
1033 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1034 R_Mesh_TextureState(&m);
1035 qglColorMask(0,0,0,1);
1036 qglDisable(GL_BLEND);
1038 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1039 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin);
1040 R_Mesh_Draw(numverts, numtriangles, elements);
1043 m.texcubemap[1] = 0;
1044 m.texcombinergb[1] = GL_MODULATE;
1045 R_Mesh_TextureState(&m);
1046 // square alpha in framebuffer a few times to make it shiny
1047 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1048 qglEnable(GL_BLEND);
1049 // these comments are a test run through this math for intensity 0.5
1051 R_Mesh_Draw(numverts, numtriangles, elements);
1052 // 0.25 * 0.25 = 0.0625
1053 R_Mesh_Draw(numverts, numtriangles, elements);
1054 // 0.0625 * 0.0625 = 0.00390625
1055 R_Mesh_Draw(numverts, numtriangles, elements);
1057 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1058 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1059 R_Mesh_TextureState(&m);
1060 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1061 R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
1062 R_Mesh_Draw(numverts, numtriangles, elements);
1064 m.tex[0] = R_GetTexture(glosstexture);
1065 m.texcubemap[1] = R_GetTexture(lightcubemap);
1066 R_Mesh_TextureState(&m);
1067 qglColorMask(1,1,1,1);
1068 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1069 memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
1071 R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
1073 // the 0.25f makes specular lighting much dimmer than diffuse (intentionally)
1074 colorscale = r_colorscale * 0.25f * r_shadow_lightintensityscale.value;
1075 for (mult = 1, scale = ixtable[mult];mult < 64 && (lightcolor[0] * scale * colorscale > 1 || lightcolor[1] * scale * colorscale > 1 || lightcolor[2] * scale * colorscale > 1);mult++, scale = ixtable[mult]);
1076 colorscale *= scale;
1077 GL_Color(lightcolor[0] * colorscale, lightcolor[1] * colorscale, lightcolor[2] * colorscale, 1);
1078 for (renders = 0;renders < mult;renders++)
1079 R_Mesh_Draw(numverts, numtriangles, elements);
1083 #define PRECOMPUTEDSHADOWVOLUMES 1
1084 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
1086 #if PRECOMPUTEDSHADOWVOLUMES
1087 R_Mesh_Matrix(matrix);
1088 R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
1091 R_Mesh_Matrix(matrix);
1092 for (mesh = light->shadowvolume;mesh;mesh = mesh->next)
1094 R_Mesh_ResizeCheck(mesh->numverts * 2);
1095 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1096 R_Shadow_Volume(mesh->numverts, mesh->numtriangles, varray_vertex, mesh->elements, mesh->neighbors, light->origin, light->lightradius, light->lightradius);
1101 cvar_t r_editlights = {0, "r_editlights", "0"};
1102 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
1103 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
1104 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
1105 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
1106 worldlight_t *r_shadow_worldlightchain;
1107 worldlight_t *r_shadow_selectedlight;
1108 vec3_t r_editlights_cursorlocation;
1110 static int castshadowcount = 1;
1111 void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname)
1113 int i, j, k, l, maxverts, *mark;
1114 float *verts, *v, *v0, *v1, f, projectdistance, temp[3], temp2[3], temp3[3], radius2;
1121 e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
1122 VectorCopy(origin, e->origin);
1123 VectorCopy(color, e->light);
1124 e->lightradius = radius;
1125 VectorCopy(origin, e->mins);
1126 VectorCopy(origin, e->maxs);
1129 e->next = r_shadow_worldlightchain;
1130 r_shadow_worldlightchain = e;
1133 e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
1134 strcpy(e->cubemapname, cubemapname);
1135 // FIXME: add cubemap loading (and don't load a cubemap twice)
1140 leaf = Mod_PointInLeaf(origin, cl.worldmodel);
1141 pvs = Mod_LeafPVS(leaf, cl.worldmodel);
1142 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1144 if (pvs[i >> 3] & (1 << (i & 7)))
1146 VectorCopy(origin, temp);
1147 if (temp[0] < leaf->mins[0]) temp[0] = leaf->mins[0];
1148 if (temp[0] > leaf->maxs[0]) temp[0] = leaf->maxs[0];
1149 if (temp[1] < leaf->mins[1]) temp[1] = leaf->mins[1];
1150 if (temp[1] > leaf->maxs[1]) temp[1] = leaf->maxs[1];
1151 if (temp[2] < leaf->mins[2]) temp[2] = leaf->mins[2];
1152 if (temp[2] > leaf->maxs[2]) temp[2] = leaf->maxs[2];
1153 VectorSubtract(temp, origin, temp);
1154 if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
1156 leaf->worldnodeframe = castshadowcount;
1157 for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
1159 surf = cl.worldmodel->surfaces + *mark;
1160 if (surf->castshadow != castshadowcount)
1162 f = DotProduct(e->origin, surf->plane->normal) - surf->plane->dist;
1163 if (surf->flags & SURF_PLANEBACK)
1165 if (f > 0 && f < e->lightradius)
1167 VectorSubtract(e->origin, surf->poly_center, temp);
1168 if (DotProduct(temp, temp) - surf->poly_radius2 < e->lightradius * e->lightradius)
1169 surf->castshadow = castshadowcount;
1178 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1179 if (leaf->worldnodeframe == castshadowcount)
1182 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1183 if (surf->castshadow == castshadowcount)
1187 e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
1189 e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
1191 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1192 if (leaf->worldnodeframe == castshadowcount)
1193 e->leafs[e->numleafs++] = leaf;
1195 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1196 if (surf->castshadow == castshadowcount)
1197 e->surfaces[e->numsurfaces++] = surf;
1198 // find bounding box and sphere of lit surfaces
1199 // (these will be used for creating a shape to clip the light)
1201 VectorCopy(e->origin, e->mins);
1202 VectorCopy(e->origin, e->maxs);
1203 for (j = 0;j < e->numsurfaces;j++)
1205 surf = e->surfaces[j];
1206 for (k = 0, v = surf->poly_verts;k < surf->poly_numverts;k++, v += 3)
1208 if (e->mins[0] > v[0]) e->mins[0] = v[0];if (e->maxs[0] < v[0]) e->maxs[0] = v[0];
1209 if (e->mins[1] > v[1]) e->mins[1] = v[1];if (e->maxs[1] < v[1]) e->maxs[1] = v[1];
1210 if (e->mins[2] > v[2]) e->mins[2] = v[2];if (e->maxs[2] < v[2]) e->maxs[2] = v[2];
1211 VectorSubtract(v, e->origin, temp);
1212 f = DotProduct(temp, temp);
1217 e->cullradius = sqrt(radius2);
1218 if (e->cullradius > e->lightradius)
1219 e->cullradius = e->lightradius;
1220 if (e->mins[0] < e->origin[0] - e->lightradius) e->mins[0] = e->origin[0] - e->lightradius;
1221 if (e->maxs[0] > e->origin[0] + e->lightradius) e->maxs[0] = e->origin[0] + e->lightradius;
1222 if (e->mins[1] < e->origin[1] - e->lightradius) e->mins[1] = e->origin[1] - e->lightradius;
1223 if (e->maxs[1] > e->origin[1] + e->lightradius) e->maxs[1] = e->origin[1] + e->lightradius;
1224 if (e->mins[2] < e->origin[2] - e->lightradius) e->mins[2] = e->origin[2] - e->lightradius;
1225 if (e->maxs[2] > e->origin[2] + e->lightradius) e->maxs[2] = e->origin[2] + e->lightradius;
1226 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);
1227 // clip shadow volumes against eachother to remove unnecessary
1228 // polygons (and sections of polygons)
1232 for (j = 0;j < e->numsurfaces;j++)
1234 surf = e->surfaces[j];
1235 if (surf->flags & SURF_SHADOWCAST)
1237 surf->castshadow = castshadowcount;
1238 if (maxverts < surf->poly_numverts)
1239 maxverts = surf->poly_numverts;
1242 e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1243 #if !PRECOMPUTEDSHADOWVOLUMES
1244 // make a mesh to cast a shadow volume from
1245 for (j = 0;j < e->numsurfaces;j++)
1246 if (e->surfaces[j]->castshadow == castshadowcount)
1247 Mod_ShadowMesh_AddPolygon(r_shadow_mempool, e->shadowvolume, e->surfaces[j]->poly_numverts, e->surfaces[j]->poly_verts);
1252 shadowmesh_t *castmesh, *mesh;
1253 surfmesh_t *surfmesh;
1254 // make a mesh to cast a shadow volume from
1255 castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1256 for (j = 0;j < e->numsurfaces;j++)
1257 if (e->surfaces[j]->castshadow == castshadowcount)
1258 for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
1259 Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->verts, surfmesh->numtriangles, surfmesh->index);
1260 castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
1262 // cast shadow volume from castmesh
1263 for (mesh = castmesh;mesh;mesh = mesh->next)
1265 R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
1266 R_Shadow_ResizeShadowElements(castmesh->numtriangles);
1268 if (maxverts < castmesh->numverts * 2)
1270 maxverts = castmesh->numverts * 2;
1275 if (verts == NULL && maxverts > 0)
1276 verts = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[4]));
1278 // now that we have the buffers big enough, construct shadow volume mesh
1279 memcpy(verts, castmesh->verts, castmesh->numverts * sizeof(float[4]));
1280 R_Shadow_ProjectVertices(verts, verts + castmesh->numverts * 4, castmesh->numverts, e->origin, e->lightradius);
1281 R_Shadow_MakeTriangleShadowFlags(castmesh->elements, verts, castmesh->numtriangles, trianglefacinglight, e->origin, e->lightradius);
1282 tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->elements, castmesh->neighbors, castmesh->numtriangles, castmesh->numverts, trianglefacinglight, shadowelements);
1283 // add the constructed shadow volume mesh
1284 Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, verts, tris, shadowelements);
1286 // we're done with castmesh now
1287 Mod_ShadowMesh_Free(castmesh);
1290 // make a shadow volume mesh
1291 if (verts == NULL && maxverts > 0)
1292 verts = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[4]));
1293 for (j = 0;j < e->numsurfaces;j++)
1295 surf = e->surfaces[j];
1296 if (surf->castshadow != castshadowcount)
1298 projectdistance = 1000000.0f;//e->lightradius;
1299 // copy the original polygon, for the front cap of the volume
1300 for (k = 0, v0 = surf->poly_verts, v1 = verts;k < surf->poly_numverts;k++, v0 += 3, v1 += 3)
1302 Mod_ShadowMesh_AddPolygon(r_shadow_mempool, e->shadowvolume, surf->poly_numverts, verts);
1303 // project the original polygon, reversed, for the back cap of the volume
1304 for (k = 0, v0 = surf->poly_verts + (surf->poly_numverts - 1) * 3, v1 = verts;k < surf->poly_numverts;k++, v0 -= 3, v1 += 3)
1306 VectorSubtract(v0, e->origin, temp);
1307 //VectorNormalize(temp);
1308 VectorMA(v0, projectdistance, temp, v1);
1310 Mod_ShadowMesh_AddPolygon(r_shadow_mempool, e->shadowvolume, surf->poly_numverts, verts);
1311 // project the shadow volume sides
1312 for (l = surf->poly_numverts - 1, k = 0, v0 = surf->poly_verts + (surf->poly_numverts - 1) * 3, v1 = surf->poly_verts;k < surf->poly_numverts;l = k, k++, v0 = v1, v1 += 3)
1314 if (surf->neighborsurfaces == NULL || surf->neighborsurfaces[l] == NULL || surf->neighborsurfaces[l]->castshadow != castshadowcount)
1316 VectorCopy(v1, &verts[0]);
1317 VectorCopy(v0, &verts[3]);
1318 VectorCopy(v0, &verts[6]);
1319 VectorCopy(v1, &verts[9]);
1320 VectorSubtract(&verts[6], e->origin, temp);
1321 //VectorNormalize(temp);
1322 VectorMA(&verts[6], projectdistance, temp, &verts[6]);
1323 VectorSubtract(&verts[9], e->origin, temp);
1324 //VectorNormalize(temp);
1325 VectorMA(&verts[9], projectdistance, temp, &verts[9]);
1328 VectorSubtract(&verts[0], &verts[3], temp);
1329 VectorSubtract(&verts[6], &verts[3], temp2);
1330 CrossProduct(temp, temp2, temp3);
1331 VectorNormalize(temp3);
1332 if (DotProduct(surf->poly_center, temp3) > DotProduct(&verts[0], temp3))
1334 VectorCopy(v0, &verts[0]);
1335 VectorCopy(v1, &verts[3]);
1336 VectorCopy(v1, &verts[6]);
1337 VectorCopy(v0, &verts[9]);
1338 VectorSubtract(&verts[6], e->origin, temp);
1339 //VectorNormalize(temp);
1340 VectorMA(&verts[6], projectdistance, temp, &verts[6]);
1341 VectorSubtract(&verts[9], e->origin, temp);
1342 //VectorNormalize(temp);
1343 VectorMA(&verts[9], projectdistance, temp, &verts[9]);
1344 Con_Printf("flipped shadow volume edge %8p %i\n", surf, l);
1348 Mod_ShadowMesh_AddPolygon(r_shadow_mempool, e->shadowvolume, 4, verts);
1354 e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
1355 for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
1356 l += mesh->numtriangles;
1357 Con_Printf("static shadow volume built containing %i triangles\n", l);
1361 void R_Shadow_FreeWorldLight(worldlight_t *light)
1363 worldlight_t **lightpointer;
1364 for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
1365 if (*lightpointer != light)
1366 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
1367 *lightpointer = light->next;
1368 if (light->cubemapname)
1369 Mem_Free(light->cubemapname);
1370 if (light->shadowvolume)
1371 Mod_ShadowMesh_Free(light->shadowvolume);
1372 if (light->surfaces)
1373 Mem_Free(light->surfaces);
1375 Mem_Free(light->leafs);
1379 void R_Shadow_ClearWorldLights(void)
1381 while (r_shadow_worldlightchain)
1382 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
1383 r_shadow_selectedlight = NULL;
1386 void R_Shadow_SelectLight(worldlight_t *light)
1388 if (r_shadow_selectedlight)
1389 r_shadow_selectedlight->selected = false;
1390 r_shadow_selectedlight = light;
1391 if (r_shadow_selectedlight)
1392 r_shadow_selectedlight->selected = true;
1395 void R_Shadow_FreeSelectedWorldLight(void)
1397 if (r_shadow_selectedlight)
1399 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1400 r_shadow_selectedlight = NULL;
1404 void R_Shadow_SelectLightInView(void)
1406 float bestrating, rating, temp[3], dist;
1407 worldlight_t *best, *light;
1410 for (light = r_shadow_worldlightchain;light;light = light->next)
1412 VectorSubtract(light->origin, r_refdef.vieworg, temp);
1413 dist = sqrt(DotProduct(temp, temp));
1414 if (DotProduct(temp, vpn) >= 0.97 * dist && bestrating > dist && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
1420 R_Shadow_SelectLight(best);
1423 void R_Shadow_LoadWorldLights(void)
1426 char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
1427 float origin[3], radius, color[3];
1428 COM_StripExtension(cl.worldmodel->name, name);
1429 strcat(name, ".rtlights");
1430 lightsstring = COM_LoadFile(name, false);
1438 while (*s && *s != '\n')
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 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname);
1457 Con_Printf("invalid rtlights file \"%s\"\n", name);
1458 Mem_Free(lightsstring);
1462 void R_Shadow_SaveWorldLights(void)
1464 worldlight_t *light;
1465 int bufchars, bufmaxchars;
1467 char name[MAX_QPATH];
1469 if (!r_shadow_worldlightchain)
1471 COM_StripExtension(cl.worldmodel->name, name);
1472 strcat(name, ".rtlights");
1473 bufchars = bufmaxchars = 0;
1475 for (light = r_shadow_worldlightchain;light;light = light->next)
1477 sprintf(line, "%g %g %g %g %g %g %g %d %s\n", light->origin[0], light->origin[1], light->origin[2], light->lightradius, light->light[0], light->light[1], light->light[2], light->style, light->cubemapname ? light->cubemapname : "");
1478 if (bufchars + strlen(line) > bufmaxchars)
1480 bufmaxchars = bufchars + strlen(line) + 2048;
1482 buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
1486 memcpy(buf, oldbuf, bufchars);
1492 memcpy(buf + bufchars, line, strlen(line));
1493 bufchars += strlen(line);
1497 COM_WriteFile(name, buf, bufchars);
1502 void R_Shadow_LoadLightsFile(void)
1505 char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
1506 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
1507 COM_StripExtension(cl.worldmodel->name, name);
1508 strcat(name, ".lights");
1509 lightsstring = COM_LoadFile(name, false);
1517 while (*s && *s != '\n')
1522 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);
1526 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);
1529 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
1530 radius = bound(15, radius, 4096);
1531 VectorScale(color, (1.0f / (8388608.0f)), color);
1532 R_Shadow_NewWorldLight(origin, radius, color, style, NULL);
1537 Con_Printf("invalid lights file \"%s\"\n", name);
1538 Mem_Free(lightsstring);
1542 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
1544 int entnum, style, islight;
1545 char key[256], value[1024];
1546 float origin[3], radius, color[3], light, scale, originhack[3];
1549 data = cl.worldmodel->entities;
1552 for (entnum = 0;COM_ParseToken(&data) && com_token[0] == '{';entnum++)
1555 origin[0] = origin[1] = origin[2] = 0;
1556 originhack[0] = originhack[1] = originhack[2] = 0;
1557 color[0] = color[1] = color[2] = 1;
1563 if (!COM_ParseToken(&data))
1565 if (com_token[0] == '}')
1566 break; // end of entity
1567 if (com_token[0] == '_')
1568 strcpy(key, com_token + 1);
1570 strcpy(key, com_token);
1571 while (key[strlen(key)-1] == ' ') // remove trailing spaces
1572 key[strlen(key)-1] = 0;
1573 if (!COM_ParseToken(&data))
1575 strcpy(value, com_token);
1577 // now that we have the key pair worked out...
1578 if (!strcmp("light", key))
1579 light = atof(value);
1580 else if (!strcmp("origin", key))
1581 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
1582 else if (!strcmp("color", key))
1583 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
1584 else if (!strcmp("wait", key))
1585 scale = atof(value);
1586 else if (!strcmp("classname", key))
1588 if (!strncmp(value, "light", 5))
1591 if (!strcmp(value, "light_flame_large_yellow"))
1597 if (!strcmp(value, "light_flame_small_yellow"))
1603 if (!strcmp(value, "light_torch_small_white"))
1609 if (!strcmp(value, "light_torch_small_walltorch"))
1617 else if (!strcmp("style", key))
1618 style = atoi(value);
1620 if (light <= 0 && islight)
1622 radius = bound(0, light / scale, 1048576) + 15.0f;
1623 light = bound(0, light, 1048576) * (1.0f / 256.0f);
1624 VectorScale(color, light, color);
1625 VectorAdd(origin, originhack, origin);
1627 R_Shadow_NewWorldLight(origin, radius, color, style, NULL);
1632 void R_Shadow_SetCursorLocationForView(void)
1634 vec_t dist, push, frac;
1635 vec3_t dest, endpos, normal;
1636 VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
1637 frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
1640 dist = frac * r_editlights_cursordistance.value;
1641 push = r_editlights_cursorpushback.value;
1645 VectorMA(endpos, push, vpn, endpos);
1646 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
1648 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1649 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1650 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
1653 extern void R_DrawCrosshairSprite(rtexture_t *texture, vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca);
1654 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
1657 pic = Draw_CachePic("gfx/crosshair1.tga");
1659 R_DrawCrosshairSprite(pic->tex, r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 1);
1662 void R_Shadow_DrawCursor(void)
1664 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
1667 void R_Shadow_UpdateLightingMode(void)
1669 r_shadow_lightingmode = 0;
1670 if (r_shadow_realtime.integer)
1672 if (r_shadow_worldlightchain)
1673 r_shadow_lightingmode = 2;
1675 r_shadow_lightingmode = 1;
1679 void R_Shadow_UpdateWorldLightSelection(void)
1681 if (r_editlights.integer)
1683 R_Shadow_SelectLightInView();
1684 R_Shadow_SetCursorLocationForView();
1685 R_Shadow_DrawCursor();
1688 R_Shadow_SelectLight(NULL);
1691 void R_Shadow_EditLights_Clear_f(void)
1693 R_Shadow_ClearWorldLights();
1696 void R_Shadow_EditLights_Reload_f(void)
1700 R_Shadow_ClearWorldLights();
1701 R_Shadow_LoadWorldLights();
1705 void R_Shadow_EditLights_Save_f(void)
1708 R_Shadow_SaveWorldLights();
1711 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
1713 R_Shadow_ClearWorldLights();
1714 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
1717 void R_Shadow_EditLights_ImportLightsFile_f(void)
1719 R_Shadow_ClearWorldLights();
1720 R_Shadow_LoadLightsFile();
1723 void R_Shadow_EditLights_Spawn_f(void)
1725 vec3_t origin, color;
1728 const char *cubemapname;
1729 if (!r_editlights.integer)
1731 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1734 if (Cmd_Argc() <= 7)
1737 color[0] = color[1] = color[2] = 1;
1740 if (Cmd_Argc() >= 2)
1742 radius = atof(Cmd_Argv(1));
1743 if (Cmd_Argc() >= 3)
1745 color[0] = atof(Cmd_Argv(2));
1746 color[1] = color[0];
1747 color[2] = color[0];
1748 if (Cmd_Argc() >= 5)
1750 color[1] = atof(Cmd_Argv(3));
1751 color[2] = atof(Cmd_Argv(4));
1752 if (Cmd_Argc() >= 6)
1754 style = atoi(Cmd_Argv(5));
1755 if (Cmd_Argc() >= 7)
1756 cubemapname = Cmd_Argv(6);
1761 if (cubemapname && !cubemapname[0])
1763 if (radius >= 16 && color[0] >= 0 && color[1] >= 0 && color[2] >= 0 && style >= 0 && style < 256 && (color[0] >= 0.1 || color[1] >= 0.1 || color[2] >= 0.1))
1765 VectorCopy(r_editlights_cursorlocation, origin);
1766 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname);
1770 Con_Printf("usage: r_editlights_spawn radius red green blue [style [cubemap]]\n");
1773 void R_Shadow_EditLights_Edit_f(void)
1775 vec3_t origin, color;
1778 const char *cubemapname;
1779 if (!r_editlights.integer)
1781 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
1784 if (!r_shadow_selectedlight)
1786 Con_Printf("No selected light.\n");
1789 if (Cmd_Argc() <= 7)
1792 color[0] = color[1] = color[2] = 1;
1795 if (Cmd_Argc() >= 2)
1797 radius = atof(Cmd_Argv(1));
1798 if (Cmd_Argc() >= 3)
1800 color[0] = atof(Cmd_Argv(2));
1801 color[1] = color[0];
1802 color[2] = color[0];
1803 if (Cmd_Argc() >= 5)
1805 color[1] = atof(Cmd_Argv(3));
1806 color[2] = atof(Cmd_Argv(4));
1807 if (Cmd_Argc() >= 6)
1809 style = atoi(Cmd_Argv(5));
1810 if (Cmd_Argc() >= 7)
1811 cubemapname = Cmd_Argv(6);
1816 if (cubemapname && !cubemapname[0])
1818 if (radius >= 16 && color[0] >= 0 && color[1] >= 0 && color[2] >= 0 && style >= 0 && style < 256 && (color[0] >= 0.1 || color[1] >= 0.1 || color[2] >= 0.1))
1820 VectorCopy(r_shadow_selectedlight->origin, origin);
1821 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
1822 r_shadow_selectedlight = NULL;
1823 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname);
1827 Con_Printf("usage: r_editlights_edit radius red green blue [style [cubemap]]\n");
1830 void R_Shadow_EditLights_Remove_f(void)
1832 if (!r_editlights.integer)
1834 Con_Printf("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
1837 if (!r_shadow_selectedlight)
1839 Con_Printf("No selected light.\n");
1842 R_Shadow_FreeSelectedWorldLight();
1845 void R_Shadow_EditLights_Init(void)
1847 Cvar_RegisterVariable(&r_editlights);
1848 Cvar_RegisterVariable(&r_editlights_cursordistance);
1849 Cvar_RegisterVariable(&r_editlights_cursorpushback);
1850 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
1851 Cvar_RegisterVariable(&r_editlights_cursorgrid);
1852 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
1853 Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
1854 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
1855 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
1856 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
1857 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
1858 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
1859 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);