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;
25 int *trianglefacinglightlist;
27 int maxshadowvertices;
28 float *shadowvertex3f;
30 rtexturepool_t *r_shadow_texturepool;
31 rtexture_t *r_shadow_normalcubetexture;
32 rtexture_t *r_shadow_attenuation2dtexture;
33 rtexture_t *r_shadow_attenuation3dtexture;
34 rtexture_t *r_shadow_blankbumptexture;
35 rtexture_t *r_shadow_blankglosstexture;
36 rtexture_t *r_shadow_blankwhitetexture;
38 cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5"};
39 cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1"};
40 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
41 cvar_t r_shadow_realtime = {0, "r_shadow_realtime", "0"};
42 cvar_t r_shadow_gloss = {0, "r_shadow_gloss", "1"};
43 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
44 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
45 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
46 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0"};
47 cvar_t r_shadow_shadownudge = {0, "r_shadow_shadownudge", "1"};
48 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1"};
49 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "100000"};
50 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1"};
52 int c_rt_lights, c_rt_clears, c_rt_scissored;
53 int c_rt_shadowmeshes, c_rt_shadowtris, c_rt_lightmeshes, c_rt_lighttris;
54 int c_rtcached_shadowmeshes, c_rtcached_shadowtris;
56 void R_Shadow_ClearWorldLights(void);
57 void R_Shadow_SaveWorldLights(void);
58 void R_Shadow_LoadWorldLights(void);
59 void R_Shadow_LoadLightsFile(void);
60 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
62 void r_shadow_start(void)
64 // allocate vertex processing arrays
65 r_shadow_mempool = Mem_AllocPool("R_Shadow");
66 maxshadowelements = 0;
67 shadowelements = NULL;
68 maxshadowvertices = 0;
69 shadowvertex3f = NULL;
70 maxtrianglefacinglight = 0;
71 trianglefacinglight = NULL;
72 trianglefacinglightlist = NULL;
73 r_shadow_normalcubetexture = NULL;
74 r_shadow_attenuation2dtexture = NULL;
75 r_shadow_attenuation3dtexture = NULL;
76 r_shadow_blankbumptexture = NULL;
77 r_shadow_blankglosstexture = NULL;
78 r_shadow_blankwhitetexture = NULL;
79 r_shadow_texturepool = NULL;
80 R_Shadow_ClearWorldLights();
81 r_shadow_reloadlights = true;
84 void r_shadow_shutdown(void)
86 R_Shadow_ClearWorldLights();
87 r_shadow_reloadlights = true;
88 r_shadow_normalcubetexture = NULL;
89 r_shadow_attenuation2dtexture = NULL;
90 r_shadow_attenuation3dtexture = NULL;
91 r_shadow_blankbumptexture = NULL;
92 r_shadow_blankglosstexture = NULL;
93 r_shadow_blankwhitetexture = NULL;
94 R_FreeTexturePool(&r_shadow_texturepool);
95 maxshadowelements = 0;
96 shadowelements = NULL;
97 maxshadowvertices = 0;
98 shadowvertex3f = NULL;
99 maxtrianglefacinglight = 0;
100 trianglefacinglight = NULL;
101 trianglefacinglightlist = NULL;
102 Mem_FreePool(&r_shadow_mempool);
105 void r_shadow_newmap(void)
107 R_Shadow_ClearWorldLights();
108 r_shadow_reloadlights = true;
111 void R_Shadow_Init(void)
113 Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
114 Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
115 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
116 Cvar_RegisterVariable(&r_shadow_realtime);
117 Cvar_RegisterVariable(&r_shadow_gloss);
118 Cvar_RegisterVariable(&r_shadow_debuglight);
119 Cvar_RegisterVariable(&r_shadow_scissor);
120 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
121 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
122 Cvar_RegisterVariable(&r_shadow_shadownudge);
123 Cvar_RegisterVariable(&r_shadow_portallight);
124 Cvar_RegisterVariable(&r_shadow_projectdistance);
125 Cvar_RegisterVariable(&r_shadow_texture3d);
126 R_Shadow_EditLights_Init();
127 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
130 void R_Shadow_ProjectVertex3f(float *verts, int numverts, const float *relativelightorigin, float projectdistance)
133 float *in, *out, diff[3];
135 out = verts + numverts * 3;
136 for (i = 0;i < numverts;i++, in += 3, out += 3)
138 VectorSubtract(in, relativelightorigin, diff);
139 VectorNormalizeFast(diff);
140 VectorMA(in, projectdistance, diff, out);
141 VectorMA(in, r_shadow_shadownudge.value, diff, in);
145 int R_Shadow_MakeTriangleShadowFlags_Vertex3f(const int *elements, const float *vertex, int numtris, qbyte *facing, int *list, const float *relativelightorigin)
148 const float *v0, *v1, *v2;
149 for (i = 0;i < numtris;i++, elements += 3)
151 // calculate triangle facing flag
152 v0 = vertex + elements[0] * 3;
153 v1 = vertex + elements[1] * 3;
154 v2 = vertex + elements[2] * 3;
155 if(PointInfrontOfTriangle(relativelightorigin, v0, v1, v2))
166 int R_Shadow_BuildShadowVolumeTriangles(const int *elements, const int *neighbors, int numverts, const qbyte *facing, const int *facinglist, int numfacing, int *out)
170 // check each frontface for bordering backfaces,
171 // and cast shadow polygons from those edges,
172 // also create front and back caps for shadow volume
173 tris = numfacing * 2;
175 for (i = 0;i < numfacing;i++)
177 e = elements + facinglist[i] * 3;
184 for (i = 0;i < numfacing;i++)
186 e = elements + facinglist[i] * 3;
187 out[0] = e[2] + numverts;
188 out[1] = e[1] + numverts;
189 out[2] = e[0] + numverts;
192 // output sides around frontfaces
193 for (i = 0;i < numfacing;i++)
195 n = neighbors + facinglist[i] * 3;
197 if (n[0] < 0 || !facing[n[0]])
199 e = elements + facinglist[i] * 3;
202 out[2] = e[0] + numverts;
204 out[4] = e[0] + numverts;
205 out[5] = e[1] + numverts;
209 if (n[1] < 0 || !facing[n[1]])
211 e = elements + facinglist[i] * 3;
214 out[2] = e[1] + numverts;
216 out[4] = e[1] + numverts;
217 out[5] = e[2] + numverts;
221 if (n[2] < 0 || !facing[n[2]])
223 e = elements + facinglist[i] * 3;
226 out[2] = e[2] + numverts;
228 out[4] = e[2] + numverts;
229 out[5] = e[0] + numverts;
237 void R_Shadow_ResizeTriangleFacingLight(int numtris)
239 // make sure trianglefacinglight is big enough for this volume
240 // ameks ru ertaignelaficgnilhg tsib gie ongu hof rhtsiv lomu e
241 // m4k3 5ur3 7r14ng13f4c1n5115h7 15 b15 3n0u5h f0r 7h15 v01um3
242 if (maxtrianglefacinglight < numtris)
244 maxtrianglefacinglight = numtris;
245 if (trianglefacinglight)
246 Mem_Free(trianglefacinglight);
247 if (trianglefacinglightlist)
248 Mem_Free(trianglefacinglightlist);
249 trianglefacinglight = Mem_Alloc(r_shadow_mempool, maxtrianglefacinglight);
250 trianglefacinglightlist = Mem_Alloc(r_shadow_mempool, sizeof(int) * maxtrianglefacinglight);
254 int *R_Shadow_ResizeShadowElements(int numtris)
256 // make sure shadowelements is big enough for this volume
257 if (maxshadowelements < numtris * 24)
259 maxshadowelements = numtris * 24;
261 Mem_Free(shadowelements);
262 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
264 return shadowelements;
267 float *R_Shadow_VertexBuffer(int numvertices)
269 if (maxshadowvertices < numvertices)
271 maxshadowvertices = numvertices;
273 Mem_Free(shadowvertex3f);
274 shadowvertex3f = Mem_Alloc(r_shadow_mempool, maxshadowvertices * sizeof(float[3]));
276 return shadowvertex3f;
279 void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance)
282 if (projectdistance < 0.1)
284 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
292 // a triangle facing the light source
295 // a triangle not facing the light source
298 // an extrusion of the frontfaces, beginning at the original geometry and
299 // ending further from the light source than the original geometry
300 // (presumably at least as far as the light's radius, if the light has a
301 // radius at all), capped at both front and back to avoid any problems
304 // draws the shadow volumes of the model.
306 // vertex locations must already be in varray_vertex3f before use.
307 // varray_vertex3f must have capacity for numverts * 2.
309 // make sure trianglefacinglight is big enough for this volume
310 if (maxtrianglefacinglight < numtris)
311 R_Shadow_ResizeTriangleFacingLight(numtris);
313 // make sure shadowelements is big enough for this volume
314 if (maxshadowelements < numtris * 24)
315 R_Shadow_ResizeShadowElements(numtris);
317 // check which triangles are facing the light
318 tris = R_Shadow_MakeTriangleShadowFlags_Vertex3f(elements, varray_vertex3f, numtris, trianglefacinglight, trianglefacinglightlist, relativelightorigin);
322 // output triangle elements
323 tris = R_Shadow_BuildShadowVolumeTriangles(elements, neighbors, numverts, trianglefacinglight, trianglefacinglightlist, tris, shadowelements);
327 // by clever use of elements we can construct the whole shadow from
328 // the unprojected vertices and the projected vertices
329 R_Shadow_ProjectVertex3f(varray_vertex3f, numverts, relativelightorigin, projectdistance);
331 if (r_shadowstage == SHADOWSTAGE_STENCIL)
333 // increment stencil if backface is behind depthbuffer
334 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
335 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
336 R_Mesh_Draw(numverts * 2, tris, shadowelements);
338 c_rt_shadowtris += numtris;
339 // decrement stencil if frontface is behind depthbuffer
340 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
341 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
343 R_Mesh_Draw(numverts * 2, tris, shadowelements);
345 c_rt_shadowtris += numtris;
348 void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
351 if (r_shadowstage == SHADOWSTAGE_STENCIL)
353 // increment stencil if backface is behind depthbuffer
354 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
355 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
356 for (mesh = firstmesh;mesh;mesh = mesh->next)
358 R_Mesh_GetSpace(mesh->numverts);
359 R_Mesh_CopyVertex3f(mesh->vertex3f, mesh->numverts);
360 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
361 c_rtcached_shadowmeshes++;
362 c_rtcached_shadowtris += mesh->numtriangles;
364 // decrement stencil if frontface is behind depthbuffer
365 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
366 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
368 for (mesh = firstmesh;mesh;mesh = mesh->next)
370 R_Mesh_GetSpace(mesh->numverts);
371 R_Mesh_CopyVertex3f(mesh->vertex3f, mesh->numverts);
372 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
373 c_rtcached_shadowmeshes++;
374 c_rtcached_shadowtris += mesh->numtriangles;
378 float r_shadow_attenpower, r_shadow_attenscale;
379 static void R_Shadow_MakeTextures(void)
381 int x, y, z, d, side;
382 float v[3], s, t, intensity;
384 R_FreeTexturePool(&r_shadow_texturepool);
385 r_shadow_texturepool = R_AllocTexturePool();
386 r_shadow_attenpower = r_shadow_lightattenuationpower.value;
387 r_shadow_attenscale = r_shadow_lightattenuationscale.value;
389 #define ATTEN2DSIZE 64
390 #define ATTEN3DSIZE 32
391 data = Mem_Alloc(tempmempool, max(6*NORMSIZE*NORMSIZE*4, max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE*4, ATTEN2DSIZE*ATTEN2DSIZE*4)));
396 r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
401 r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
406 r_shadow_blankwhitetexture = R_LoadTexture2D(r_shadow_texturepool, "blankwhite", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
407 if (gl_texturecubemap)
409 for (side = 0;side < 6;side++)
411 for (y = 0;y < NORMSIZE;y++)
413 for (x = 0;x < NORMSIZE;x++)
415 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
416 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
450 intensity = 127.0f / sqrt(DotProduct(v, v));
451 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+0] = 128.0f + intensity * v[0];
452 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+1] = 128.0f + intensity * v[1];
453 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+2] = 128.0f + intensity * v[2];
454 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+3] = 255;
458 r_shadow_normalcubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
461 r_shadow_normalcubetexture = NULL;
462 for (y = 0;y < ATTEN2DSIZE;y++)
464 for (x = 0;x < ATTEN2DSIZE;x++)
466 v[0] = ((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
467 v[1] = ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
469 intensity = 1.0f - sqrt(DotProduct(v, v));
471 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
472 d = bound(0, intensity, 255);
473 data[(y*ATTEN2DSIZE+x)*4+0] = d;
474 data[(y*ATTEN2DSIZE+x)*4+1] = d;
475 data[(y*ATTEN2DSIZE+x)*4+2] = d;
476 data[(y*ATTEN2DSIZE+x)*4+3] = d;
479 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
480 if (r_shadow_texture3d.integer)
482 for (z = 0;z < ATTEN3DSIZE;z++)
484 for (y = 0;y < ATTEN3DSIZE;y++)
486 for (x = 0;x < ATTEN3DSIZE;x++)
488 v[0] = ((x + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
489 v[1] = ((y + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
490 v[2] = ((z + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
491 intensity = 1.0f - sqrt(DotProduct(v, v));
493 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
494 d = bound(0, intensity, 255);
495 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+0] = d;
496 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+1] = d;
497 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+2] = d;
498 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+3] = d;
502 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
507 void R_Shadow_Stage_Begin(void)
511 if (r_shadow_texture3d.integer && !gl_texture3d)
512 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
514 //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
515 if (!r_shadow_attenuation2dtexture
516 || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
517 || r_shadow_lightattenuationpower.value != r_shadow_attenpower
518 || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
519 R_Shadow_MakeTextures();
520 if (r_shadow_reloadlights && cl.worldmodel)
522 R_Shadow_ClearWorldLights();
523 r_shadow_reloadlights = false;
524 R_Shadow_LoadWorldLights();
525 if (r_shadow_worldlightchain == NULL)
527 R_Shadow_LoadLightsFile();
528 if (r_shadow_worldlightchain == NULL)
529 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
533 memset(&m, 0, sizeof(m));
534 m.blendfunc1 = GL_ONE;
535 m.blendfunc2 = GL_ZERO;
537 GL_Color(0, 0, 0, 1);
538 r_shadowstage = SHADOWSTAGE_NONE;
540 c_rt_lights = c_rt_clears = c_rt_scissored = 0;
541 c_rt_shadowmeshes = c_rt_shadowtris = c_rt_lightmeshes = c_rt_lighttris = 0;
542 c_rtcached_shadowmeshes = c_rtcached_shadowtris = 0;
545 void R_Shadow_Stage_ShadowVolumes(void)
548 memset(&m, 0, sizeof(m));
549 R_Mesh_TextureState(&m);
550 GL_Color(1, 1, 1, 1);
551 qglColorMask(0, 0, 0, 0);
552 qglDisable(GL_BLEND);
554 qglDepthFunc(GL_LESS);
555 qglEnable(GL_STENCIL_TEST);
556 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
557 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
558 qglEnable(GL_CULL_FACE);
559 qglEnable(GL_DEPTH_TEST);
560 r_shadowstage = SHADOWSTAGE_STENCIL;
561 qglClear(GL_STENCIL_BUFFER_BIT);
563 // LordHavoc note: many shadow volumes reside entirely inside the world
564 // (that is to say they are entirely bounded by their lit surfaces),
565 // which can be optimized by handling things as an inverted light volume,
566 // with the shadow boundaries of the world being simulated by an altered
567 // (129) bias to stencil clearing on such lights
568 // FIXME: generate inverted light volumes for use as shadow volumes and
569 // optimize for them as noted above
572 void R_Shadow_Stage_LightWithoutShadows(void)
575 memset(&m, 0, sizeof(m));
576 R_Mesh_TextureState(&m);
577 qglActiveTexture(GL_TEXTURE0_ARB);
580 qglBlendFunc(GL_ONE, GL_ONE);
581 GL_Color(1, 1, 1, 1);
582 qglColorMask(1, 1, 1, 1);
584 qglDepthFunc(GL_EQUAL);
585 qglDisable(GL_STENCIL_TEST);
586 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
587 qglStencilFunc(GL_EQUAL, 128, 0xFF);
588 qglEnable(GL_CULL_FACE);
589 qglEnable(GL_DEPTH_TEST);
590 r_shadowstage = SHADOWSTAGE_LIGHT;
594 void R_Shadow_Stage_LightWithShadows(void)
597 memset(&m, 0, sizeof(m));
598 R_Mesh_TextureState(&m);
599 qglActiveTexture(GL_TEXTURE0_ARB);
602 qglBlendFunc(GL_ONE, GL_ONE);
603 GL_Color(1, 1, 1, 1);
604 qglColorMask(1, 1, 1, 1);
606 qglDepthFunc(GL_EQUAL);
607 qglEnable(GL_STENCIL_TEST);
608 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
609 // only draw light where this geometry was already rendered AND the
610 // stencil is 128 (values other than this mean shadow)
611 qglStencilFunc(GL_EQUAL, 128, 0xFF);
612 qglEnable(GL_CULL_FACE);
613 qglEnable(GL_DEPTH_TEST);
614 r_shadowstage = SHADOWSTAGE_LIGHT;
618 void R_Shadow_Stage_End(void)
621 // attempt to restore state to what Mesh_State thinks it is
622 qglDisable(GL_BLEND);
623 qglBlendFunc(GL_ONE, GL_ZERO);
625 // now restore the rest of the state to normal
626 GL_Color(1, 1, 1, 1);
627 qglColorMask(1, 1, 1, 1);
628 qglDisable(GL_SCISSOR_TEST);
629 qglDepthFunc(GL_LEQUAL);
630 qglDisable(GL_STENCIL_TEST);
631 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
632 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
633 qglEnable(GL_CULL_FACE);
634 qglEnable(GL_DEPTH_TEST);
635 // force mesh state to reset by using various combinations of features
636 memset(&m, 0, sizeof(m));
637 m.blendfunc1 = GL_SRC_ALPHA;
638 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
640 m.blendfunc1 = GL_ONE;
641 m.blendfunc2 = GL_ZERO;
643 r_shadowstage = SHADOWSTAGE_NONE;
647 int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const float *origin, float radius)
649 int i, ix1, iy1, ix2, iy2;
650 float x1, y1, x2, y2, x, y;
653 if (!r_shadow_scissor.integer)
655 // if view is inside the box, just say yes it's visible
656 if (r_origin[0] >= mins[0] && r_origin[0] <= maxs[0]
657 && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1]
658 && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2])
660 qglDisable(GL_SCISSOR_TEST);
663 VectorSubtract(r_origin, origin, v);
664 if (DotProduct(v, v) < radius * radius)
666 qglDisable(GL_SCISSOR_TEST);
669 // create viewspace bbox
670 for (i = 0;i < 8;i++)
672 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
673 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
674 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
675 v2[0] = DotProduct(v, vright);
676 v2[1] = DotProduct(v, vup);
677 v2[2] = DotProduct(v, vpn);
680 if (smins[0] > v2[0]) smins[0] = v2[0];
681 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
682 if (smins[1] > v2[1]) smins[1] = v2[1];
683 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
684 if (smins[2] > v2[2]) smins[2] = v2[2];
685 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
689 smins[0] = smaxs[0] = v2[0];
690 smins[1] = smaxs[1] = v2[1];
691 smins[2] = smaxs[2] = v2[2];
694 // now we have a bbox in viewspace
695 // clip it to the viewspace version of the sphere
696 v[0] = origin[0] - r_origin[0];
697 v[1] = origin[1] - r_origin[1];
698 v[2] = origin[2] - r_origin[2];
699 v2[0] = DotProduct(v, vright);
700 v2[1] = DotProduct(v, vup);
701 v2[2] = DotProduct(v, vpn);
702 if (smins[0] < v2[0] - radius) smins[0] = v2[0] - radius;
703 if (smaxs[0] < v2[0] - radius) smaxs[0] = v2[0] + radius;
704 if (smins[1] < v2[1] - radius) smins[1] = v2[1] - radius;
705 if (smaxs[1] < v2[1] - radius) smaxs[1] = v2[1] + radius;
706 if (smins[2] < v2[2] - radius) smins[2] = v2[2] - radius;
707 if (smaxs[2] < v2[2] - radius) smaxs[2] = v2[2] + radius;
708 // clip it to the view plane
711 // return true if that culled the box
712 if (smins[2] >= smaxs[2])
714 // ok some of it is infront of the view, transform each corner back to
715 // worldspace and then to screenspace and make screen rect
716 // initialize these variables just to avoid compiler warnings
717 x1 = y1 = x2 = y2 = 0;
718 for (i = 0;i < 8;i++)
720 v2[0] = (i & 1) ? smins[0] : smaxs[0];
721 v2[1] = (i & 2) ? smins[1] : smaxs[1];
722 v2[2] = (i & 4) ? smins[2] : smaxs[2];
723 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
724 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
725 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
727 GL_TransformToScreen(v, v2);
728 //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]);
745 // this code doesn't handle boxes with any points behind view properly
746 x1 = 1000;x2 = -1000;
747 y1 = 1000;y2 = -1000;
748 for (i = 0;i < 8;i++)
750 v[0] = (i & 1) ? mins[0] : maxs[0];
751 v[1] = (i & 2) ? mins[1] : maxs[1];
752 v[2] = (i & 4) ? mins[2] : maxs[2];
754 GL_TransformToScreen(v, v2);
755 //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]);
772 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
773 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
774 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
775 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
776 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
777 if (ix2 <= ix1 || iy2 <= iy1)
779 // set up the scissor rectangle
780 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
781 qglEnable(GL_SCISSOR_TEST);
787 int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
789 int i, ix1, iy1, ix2, iy2;
790 float x1, y1, x2, y2, x, y, f;
793 if (!r_shadow_scissor.integer)
795 // if view is inside the box, just say yes it's visible
796 if (BoxesOverlap(r_origin, r_origin, mins, maxs))
798 qglDisable(GL_SCISSOR_TEST);
801 for (i = 0;i < 3;i++)
814 f = DotProduct(vpn, r_origin) + 1;
815 if (DotProduct(vpn, v2) <= f)
817 // entirely behind nearclip plane
818 qglDisable(GL_SCISSOR_TEST);
821 if (DotProduct(vpn, v) >= f)
823 // entirely infront of nearclip plane
824 x1 = y1 = x2 = y2 = 0;
825 for (i = 0;i < 8;i++)
827 v[0] = (i & 1) ? mins[0] : maxs[0];
828 v[1] = (i & 2) ? mins[1] : maxs[1];
829 v[2] = (i & 4) ? mins[2] : maxs[2];
831 GL_TransformToScreen(v, v2);
832 //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]);
851 // clipped by nearclip plane
852 // this is nasty and crude...
853 // create viewspace bbox
854 for (i = 0;i < 8;i++)
856 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
857 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
858 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
859 v2[0] = DotProduct(v, vright);
860 v2[1] = DotProduct(v, vup);
861 v2[2] = DotProduct(v, vpn);
864 if (smins[0] > v2[0]) smins[0] = v2[0];
865 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
866 if (smins[1] > v2[1]) smins[1] = v2[1];
867 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
868 if (smins[2] > v2[2]) smins[2] = v2[2];
869 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
873 smins[0] = smaxs[0] = v2[0];
874 smins[1] = smaxs[1] = v2[1];
875 smins[2] = smaxs[2] = v2[2];
878 // now we have a bbox in viewspace
879 // clip it to the view plane
882 // return true if that culled the box
883 if (smins[2] >= smaxs[2])
885 // ok some of it is infront of the view, transform each corner back to
886 // worldspace and then to screenspace and make screen rect
887 // initialize these variables just to avoid compiler warnings
888 x1 = y1 = x2 = y2 = 0;
889 for (i = 0;i < 8;i++)
891 v2[0] = (i & 1) ? smins[0] : smaxs[0];
892 v2[1] = (i & 2) ? smins[1] : smaxs[1];
893 v2[2] = (i & 4) ? smins[2] : smaxs[2];
894 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
895 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
896 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
898 GL_TransformToScreen(v, v2);
899 //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]);
916 // this code doesn't handle boxes with any points behind view properly
917 x1 = 1000;x2 = -1000;
918 y1 = 1000;y2 = -1000;
919 for (i = 0;i < 8;i++)
921 v[0] = (i & 1) ? mins[0] : maxs[0];
922 v[1] = (i & 2) ? mins[1] : maxs[1];
923 v[2] = (i & 4) ? mins[2] : maxs[2];
925 GL_TransformToScreen(v, v2);
926 //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]);
944 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
945 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
946 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
947 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
948 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
949 if (ix2 <= ix1 || iy2 <= iy1)
951 // set up the scissor rectangle
952 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
953 qglEnable(GL_SCISSOR_TEST);
958 void R_Shadow_VertexLighting(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const float *relativelightorigin, float lightradius)
960 float *color4f = varray_color4f;
961 float dist, dot, intensity, iradius = 1.0f / lightradius, radius2 = lightradius * lightradius, v[3];
962 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
964 VectorSubtract(vertex3f, relativelightorigin, v);
965 if ((dot = DotProduct(normal3f, v)) > 0 && (dist = DotProduct(v, v)) < radius2)
968 intensity = pow(1 - (dist * iradius), r_shadow_attenpower) * r_shadow_attenscale * dot / dist;
969 VectorScale(lightcolor, intensity, color4f);
974 VectorClear(color4f);
980 void R_Shadow_VertexLightingWithXYAttenuationTexture(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const float *relativelightorigin, float lightradius, const float *zdir)
982 float *color4f = varray_color4f;
983 float dist, dot, intensity, iradius = 1.0f / lightradius, v[3];
984 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
986 VectorSubtract(vertex3f, relativelightorigin, v);
987 if ((dot = DotProduct(normal3f, v)) > 0 && (dist = fabs(DotProduct(zdir, v))) < lightradius)
989 intensity = pow(1 - (dist * iradius), r_shadow_attenpower) * r_shadow_attenscale * dot / sqrt(DotProduct(v,v));
990 VectorScale(lightcolor, intensity, color4f);
995 VectorClear(color4f);
1001 // FIXME: this should be done in a vertex program when possible
1002 // FIXME: if vertex program not available, this would really benefit from 3DNow! or SSE
1003 void R_Shadow_Transform_Vertex3f_TexCoord3f(float *tc3f, int numverts, const float *vertex3f, const matrix4x4_t *matrix)
1007 tc3f[0] = vertex3f[0] * matrix->m[0][0] + vertex3f[1] * matrix->m[0][1] + vertex3f[2] * matrix->m[0][2] + matrix->m[0][3];
1008 tc3f[1] = vertex3f[0] * matrix->m[1][0] + vertex3f[1] * matrix->m[1][1] + vertex3f[2] * matrix->m[1][2] + matrix->m[1][3];
1009 tc3f[2] = vertex3f[0] * matrix->m[2][0] + vertex3f[1] * matrix->m[2][1] + vertex3f[2] * matrix->m[2][2] + matrix->m[2][3];
1016 void R_Shadow_Transform_Vertex3f_TexCoord2f(float *tc2f, int numverts, const float *vertex3f, const matrix4x4_t *matrix)
1020 tc2f[0] = vertex3f[0] * matrix->m[0][0] + vertex3f[1] * matrix->m[0][1] + vertex3f[2] * matrix->m[0][2] + matrix->m[0][3];
1021 tc2f[1] = vertex3f[0] * matrix->m[1][0] + vertex3f[1] * matrix->m[1][1] + vertex3f[2] * matrix->m[1][2] + matrix->m[1][3];
1028 void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(float *out3f, int numverts, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const vec3_t relativelightorigin)
1032 for (i = 0;i < numverts;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1034 VectorSubtract(vertex3f, relativelightorigin, lightdir);
1035 // the cubemap normalizes this for us
1036 out3f[0] = DotProduct(svector3f, lightdir);
1037 out3f[1] = DotProduct(tvector3f, lightdir);
1038 out3f[2] = DotProduct(normal3f, lightdir);
1042 void R_Shadow_GenTexCoords_Specular_NormalCubeMap(float *out3f, int numverts, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const vec3_t relativelightorigin, const vec3_t relativeeyeorigin)
1045 float lightdir[3], eyedir[3], halfdir[3];
1046 for (i = 0;i < numverts;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1048 VectorSubtract(vertex3f, relativelightorigin, lightdir);
1049 VectorNormalizeFast(lightdir);
1050 VectorSubtract(vertex3f, relativeeyeorigin, eyedir);
1051 VectorNormalizeFast(eyedir);
1052 VectorAdd(lightdir, eyedir, halfdir);
1053 // the cubemap normalizes this for us
1054 out3f[0] = DotProduct(svector3f, halfdir);
1055 out3f[1] = DotProduct(tvector3f, halfdir);
1056 out3f[2] = DotProduct(normal3f, halfdir);
1060 void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *elements, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *texcoord2f, const float *relativelightorigin, float lightradius, const float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *basetexture, rtexture_t *bumptexture, rtexture_t *lightcubemap)
1063 float color[3], color2[3];
1065 memset(&m, 0, sizeof(m));
1066 if (gl_dot3arb && gl_texturecubemap && gl_combine.integer && gl_stencil)
1069 bumptexture = r_shadow_blankbumptexture;
1070 // colorscale accounts for how much we multiply the brightness during combine
1071 // mult is how many times the final pass of the lighting will be
1072 // performed to get more brightness than otherwise possible
1073 // limit mult to 64 for sanity sake
1074 if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
1076 // 3/2 3D combine path (Geforce3, Radeon 8500)
1077 m.tex[0] = R_GetTexture(bumptexture);
1078 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1079 m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
1080 m.texcombinergb[0] = GL_REPLACE;
1081 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1082 R_Mesh_TextureState(&m);
1083 qglColorMask(0,0,0,1);
1084 qglDisable(GL_BLEND);
1086 R_Mesh_GetSpace(numverts);
1087 R_Mesh_CopyVertex3f(vertex3f, numverts);
1088 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1089 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1090 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[2], numverts, vertex3f, matrix_modeltoattenuationxyz);
1091 R_Mesh_Draw(numverts, numtriangles, elements);
1093 c_rt_lighttris += numtriangles;
1095 m.tex[0] = R_GetTexture(basetexture);
1097 m.texcubemap[1] = R_GetTexture(lightcubemap);
1099 m.texcombinergb[0] = GL_MODULATE;
1100 m.texcombinergb[1] = GL_MODULATE;
1101 R_Mesh_TextureState(&m);
1102 qglColorMask(1,1,1,0);
1103 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1104 qglEnable(GL_BLEND);
1106 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1107 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1109 color[0] = bound(0, color2[0], 1);
1110 color[1] = bound(0, color2[1], 1);
1111 color[2] = bound(0, color2[2], 1);
1112 GL_Color(color[0], color[1], color[2], 1);
1113 R_Mesh_GetSpace(numverts);
1114 R_Mesh_CopyVertex3f(vertex3f, numverts);
1115 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1117 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1118 R_Mesh_Draw(numverts, numtriangles, elements);
1120 c_rt_lighttris += numtriangles;
1123 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap)
1125 // 1/2/2 3D combine path (original Radeon)
1126 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1127 R_Mesh_TextureState(&m);
1128 qglColorMask(0,0,0,1);
1129 qglDisable(GL_BLEND);
1131 R_Mesh_GetSpace(numverts);
1132 R_Mesh_CopyVertex3f(vertex3f, numverts);
1133 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1134 R_Mesh_Draw(numverts, numtriangles, elements);
1136 c_rt_lighttris += numtriangles;
1138 m.tex[0] = R_GetTexture(bumptexture);
1140 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1141 m.texcombinergb[0] = GL_REPLACE;
1142 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1143 R_Mesh_TextureState(&m);
1144 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1145 qglEnable(GL_BLEND);
1146 R_Mesh_GetSpace(numverts);
1147 R_Mesh_CopyVertex3f(vertex3f, numverts);
1148 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1149 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1150 R_Mesh_Draw(numverts, numtriangles, elements);
1152 c_rt_lighttris += numtriangles;
1154 m.tex[0] = R_GetTexture(basetexture);
1155 m.texcubemap[1] = R_GetTexture(lightcubemap);
1156 m.texcombinergb[0] = GL_MODULATE;
1157 m.texcombinergb[1] = GL_MODULATE;
1158 R_Mesh_TextureState(&m);
1159 qglColorMask(1,1,1,0);
1160 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1162 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1163 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1165 color[0] = bound(0, color2[0], 1);
1166 color[1] = bound(0, color2[1], 1);
1167 color[2] = bound(0, color2[2], 1);
1168 GL_Color(color[0], color[1], color[2], 1);
1169 R_Mesh_GetSpace(numverts);
1170 R_Mesh_CopyVertex3f(vertex3f, numverts);
1171 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1173 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1174 R_Mesh_Draw(numverts, numtriangles, elements);
1176 c_rt_lighttris += numtriangles;
1179 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap)
1181 // 2/2 3D combine path (original Radeon)
1182 m.tex[0] = R_GetTexture(bumptexture);
1183 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1184 m.texcombinergb[0] = GL_REPLACE;
1185 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1186 R_Mesh_TextureState(&m);
1188 qglColorMask(0,0,0,1);
1189 qglDisable(GL_BLEND);
1190 R_Mesh_GetSpace(numverts);
1191 R_Mesh_CopyVertex3f(vertex3f, numverts);
1192 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1193 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1194 R_Mesh_Draw(numverts, numtriangles, elements);
1196 c_rt_lighttris += numtriangles;
1198 m.tex[0] = R_GetTexture(basetexture);
1199 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
1200 m.texcubemap[1] = 0;
1201 m.texcombinergb[0] = GL_MODULATE;
1202 m.texcombinergb[1] = GL_MODULATE;
1203 R_Mesh_TextureState(&m);
1204 qglColorMask(1,1,1,0);
1205 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1206 qglEnable(GL_BLEND);
1208 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1209 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1211 color[0] = bound(0, color2[0], 1);
1212 color[1] = bound(0, color2[1], 1);
1213 color[2] = bound(0, color2[2], 1);
1214 GL_Color(color[0], color[1], color[2], 1);
1215 R_Mesh_GetSpace(numverts);
1216 R_Mesh_CopyVertex3f(vertex3f, numverts);
1217 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1218 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
1219 R_Mesh_Draw(numverts, numtriangles, elements);
1221 c_rt_lighttris += numtriangles;
1224 else if (r_textureunits.integer >= 4)
1226 // 4/2 2D combine path (Geforce3, Radeon 8500)
1227 m.tex[0] = R_GetTexture(bumptexture);
1228 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1229 m.texcombinergb[0] = GL_REPLACE;
1230 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1231 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
1232 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
1233 R_Mesh_TextureState(&m);
1234 qglColorMask(0,0,0,1);
1235 qglDisable(GL_BLEND);
1237 R_Mesh_GetSpace(numverts);
1238 R_Mesh_CopyVertex3f(vertex3f, numverts);
1239 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1240 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1241 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[2], numverts, vertex3f, matrix_modeltoattenuationxyz);
1242 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[3], numverts, vertex3f, matrix_modeltoattenuationz);
1243 R_Mesh_Draw(numverts, numtriangles, elements);
1245 c_rt_lighttris += numtriangles;
1247 m.tex[0] = R_GetTexture(basetexture);
1248 m.texcubemap[1] = R_GetTexture(lightcubemap);
1249 m.texcombinergb[0] = GL_MODULATE;
1250 m.texcombinergb[1] = GL_MODULATE;
1253 R_Mesh_TextureState(&m);
1254 qglColorMask(1,1,1,0);
1255 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1256 qglEnable(GL_BLEND);
1258 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1259 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1261 color[0] = bound(0, color2[0], 1);
1262 color[1] = bound(0, color2[1], 1);
1263 color[2] = bound(0, color2[2], 1);
1264 GL_Color(color[0], color[1], color[2], 1);
1265 R_Mesh_GetSpace(numverts);
1266 R_Mesh_CopyVertex3f(vertex3f, numverts);
1267 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1269 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1270 R_Mesh_Draw(numverts, numtriangles, elements);
1272 c_rt_lighttris += numtriangles;
1277 // 2/2/2 2D combine path (any dot3 card)
1278 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1279 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1280 R_Mesh_TextureState(&m);
1281 qglColorMask(0,0,0,1);
1282 qglDisable(GL_BLEND);
1284 R_Mesh_GetSpace(numverts);
1285 R_Mesh_CopyVertex3f(vertex3f, numverts);
1286 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1287 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
1288 R_Mesh_Draw(numverts, numtriangles, elements);
1290 c_rt_lighttris += numtriangles;
1292 m.tex[0] = R_GetTexture(bumptexture);
1294 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1295 m.texcombinergb[0] = GL_REPLACE;
1296 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1297 R_Mesh_TextureState(&m);
1298 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1299 qglEnable(GL_BLEND);
1300 R_Mesh_GetSpace(numverts);
1301 R_Mesh_CopyVertex3f(vertex3f, numverts);
1302 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1303 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1304 R_Mesh_Draw(numverts, numtriangles, elements);
1306 c_rt_lighttris += numtriangles;
1308 m.tex[0] = R_GetTexture(basetexture);
1309 m.texcubemap[1] = R_GetTexture(lightcubemap);
1310 m.texcombinergb[0] = GL_MODULATE;
1311 m.texcombinergb[1] = GL_MODULATE;
1312 R_Mesh_TextureState(&m);
1313 qglColorMask(1,1,1,0);
1314 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1316 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1317 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1319 color[0] = bound(0, color2[0], 1);
1320 color[1] = bound(0, color2[1], 1);
1321 color[2] = bound(0, color2[2], 1);
1322 GL_Color(color[0], color[1], color[2], 1);
1323 R_Mesh_GetSpace(numverts);
1324 R_Mesh_CopyVertex3f(vertex3f, numverts);
1325 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1327 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1328 R_Mesh_Draw(numverts, numtriangles, elements);
1330 c_rt_lighttris += numtriangles;
1336 if (r_textureunits.integer >= 2)
1340 m.tex[0] = R_GetTexture(basetexture);
1341 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1342 R_Mesh_TextureState(&m);
1343 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
1344 qglEnable(GL_BLEND);
1346 m.tex[0] = R_GetTexture(basetexture);
1347 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1348 m.blendfunc1 = GL_SRC_ALPHA;
1349 m.blendfunc2 = GL_ONE;
1352 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1353 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1355 color[0] = bound(0, color2[0], 1);
1356 color[1] = bound(0, color2[1], 1);
1357 color[2] = bound(0, color2[2], 1);
1359 R_Mesh_GetSpace(numverts);
1360 R_Mesh_CopyVertex3f(vertex3f, numverts);
1361 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1362 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
1363 R_Shadow_VertexLightingWithXYAttenuationTexture(numverts, vertex3f, normal3f, color2, relativelightorigin, lightradius, matrix_modeltofilter->m[2]);
1364 R_Mesh_Draw(numverts, numtriangles, elements);
1371 m.tex[0] = R_GetTexture(basetexture);
1372 R_Mesh_TextureState(&m);
1373 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
1374 qglEnable(GL_BLEND);
1376 m.tex[0] = R_GetTexture(basetexture);
1377 m.blendfunc1 = GL_SRC_ALPHA;
1378 m.blendfunc2 = GL_ONE;
1381 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1382 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1384 color[0] = bound(0, color2[0], 1);
1385 color[1] = bound(0, color2[1], 1);
1386 color[2] = bound(0, color2[2], 1);
1388 R_Mesh_GetSpace(numverts);
1389 R_Mesh_CopyVertex3f(vertex3f, numverts);
1390 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1391 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
1392 R_Shadow_VertexLighting(numverts, vertex3f, normal3f, color, relativelightorigin, lightradius);
1393 R_Mesh_Draw(numverts, numtriangles, elements);
1399 void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elements, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *texcoord2f, const float *relativelightorigin, const float *relativeeyeorigin, float lightradius, const float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *glosstexture, rtexture_t *bumptexture, rtexture_t *lightcubemap)
1402 float color[3], color2[3];
1404 if (!gl_dot3arb || !gl_texturecubemap || !gl_combine.integer || !gl_stencil)
1406 memset(&m, 0, sizeof(m));
1408 bumptexture = r_shadow_blankbumptexture;
1410 glosstexture = r_shadow_blankglosstexture;
1411 if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
1413 if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1415 // 2/0/0/1/2 3D combine blendsquare path
1416 m.tex[0] = R_GetTexture(bumptexture);
1417 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1418 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1419 R_Mesh_TextureState(&m);
1420 qglColorMask(0,0,0,1);
1421 // this squares the result
1422 qglEnable(GL_BLEND);
1423 qglBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1425 R_Mesh_GetSpace(numverts);
1426 R_Mesh_CopyVertex3f(vertex3f, numverts);
1427 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1428 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
1429 R_Mesh_Draw(numverts, numtriangles, elements);
1431 c_rt_lighttris += numtriangles;
1434 m.texcubemap[1] = 0;
1435 m.texcombinergb[1] = GL_MODULATE;
1436 R_Mesh_TextureState(&m);
1437 // square alpha in framebuffer a few times to make it shiny
1438 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1439 // these comments are a test run through this math for intensity 0.5
1440 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
1441 // 0.25 * 0.25 = 0.0625 (this is another pass)
1442 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
1443 for (renders = 0;renders < 2;renders++)
1445 R_Mesh_GetSpace(numverts);
1446 R_Mesh_CopyVertex3f(vertex3f, numverts);
1447 R_Mesh_Draw(numverts, numtriangles, elements);
1449 c_rt_lightmeshes += 3;
1450 c_rt_lighttris += numtriangles * 3;
1452 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1453 R_Mesh_TextureState(&m);
1454 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1455 R_Mesh_GetSpace(numverts);
1456 R_Mesh_CopyVertex3f(vertex3f, numverts);
1457 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1458 R_Mesh_Draw(numverts, numtriangles, elements);
1460 c_rt_lighttris += numtriangles;
1463 m.tex[0] = R_GetTexture(glosstexture);
1464 m.texcubemap[1] = R_GetTexture(lightcubemap);
1465 R_Mesh_TextureState(&m);
1466 qglColorMask(1,1,1,0);
1467 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1469 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
1470 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1472 color[0] = bound(0, color2[0], 1);
1473 color[1] = bound(0, color2[1], 1);
1474 color[2] = bound(0, color2[2], 1);
1475 GL_Color(color[0], color[1], color[2], 1);
1476 R_Mesh_GetSpace(numverts);
1477 R_Mesh_CopyVertex3f(vertex3f, numverts);
1478 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1480 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1481 R_Mesh_Draw(numverts, numtriangles, elements);
1483 c_rt_lighttris += numtriangles;
1486 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1488 // 2/0/0/2 3D combine blendsquare path
1489 m.tex[0] = R_GetTexture(bumptexture);
1490 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1491 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1492 R_Mesh_TextureState(&m);
1493 qglColorMask(0,0,0,1);
1494 // this squares the result
1495 qglEnable(GL_BLEND);
1496 qglBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1498 R_Mesh_GetSpace(numverts);
1499 R_Mesh_CopyVertex3f(vertex3f, numverts);
1500 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1501 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
1502 R_Mesh_Draw(numverts, numtriangles, elements);
1504 c_rt_lighttris += numtriangles;
1507 m.texcubemap[1] = 0;
1508 m.texcombinergb[1] = GL_MODULATE;
1509 R_Mesh_TextureState(&m);
1510 // square alpha in framebuffer a few times to make it shiny
1511 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1512 // these comments are a test run through this math for intensity 0.5
1513 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
1514 // 0.25 * 0.25 = 0.0625 (this is another pass)
1515 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
1516 for (renders = 0;renders < 2;renders++)
1518 R_Mesh_GetSpace(numverts);
1519 R_Mesh_CopyVertex3f(vertex3f, numverts);
1520 R_Mesh_Draw(numverts, numtriangles, elements);
1522 c_rt_lightmeshes += 3;
1523 c_rt_lighttris += numtriangles * 3;
1525 R_Mesh_CopyVertex3f(vertex3f, numverts);
1526 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1527 m.tex[0] = R_GetTexture(glosstexture);
1528 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
1529 R_Mesh_TextureState(&m);
1530 qglColorMask(1,1,1,0);
1531 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1533 c_rt_lighttris += numtriangles;
1535 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
1536 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1538 color[0] = bound(0, color2[0], 1);
1539 color[1] = bound(0, color2[1], 1);
1540 color[2] = bound(0, color2[2], 1);
1541 GL_Color(color[0], color[1], color[2], 1);
1542 R_Mesh_GetSpace(numverts);
1543 R_Mesh_CopyVertex3f(vertex3f, numverts);
1544 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1545 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
1546 R_Mesh_Draw(numverts, numtriangles, elements);
1548 c_rt_lighttris += numtriangles;
1551 else if (r_textureunits.integer >= 2 /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1553 // 2/0/0/2/2 2D combine blendsquare path
1554 m.tex[0] = R_GetTexture(bumptexture);
1555 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1556 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1557 R_Mesh_TextureState(&m);
1558 qglColorMask(0,0,0,1);
1559 // this squares the result
1560 qglEnable(GL_BLEND);
1561 qglBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1563 R_Mesh_GetSpace(numverts);
1564 R_Mesh_CopyVertex3f(vertex3f, numverts);
1565 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1566 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
1567 R_Mesh_Draw(numverts, numtriangles, elements);
1569 c_rt_lighttris += numtriangles;
1572 m.texcubemap[1] = 0;
1573 m.texcombinergb[1] = GL_MODULATE;
1574 R_Mesh_TextureState(&m);
1575 // square alpha in framebuffer a few times to make it shiny
1576 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1577 // these comments are a test run through this math for intensity 0.5
1578 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
1579 // 0.25 * 0.25 = 0.0625 (this is another pass)
1580 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
1581 for (renders = 0;renders < 2;renders++)
1583 R_Mesh_GetSpace(numverts);
1584 R_Mesh_CopyVertex3f(vertex3f, numverts);
1585 R_Mesh_Draw(numverts, numtriangles, elements);
1587 c_rt_lightmeshes += 3;
1588 c_rt_lighttris += numtriangles * 3;
1590 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1591 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1592 R_Mesh_TextureState(&m);
1593 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1594 R_Mesh_GetSpace(numverts);
1595 R_Mesh_CopyVertex3f(vertex3f, numverts);
1596 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1597 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
1598 R_Mesh_Draw(numverts, numtriangles, elements);
1600 c_rt_lighttris += numtriangles;
1602 m.tex[0] = R_GetTexture(glosstexture);
1603 m.texcubemap[1] = R_GetTexture(lightcubemap);
1604 R_Mesh_TextureState(&m);
1605 qglColorMask(1,1,1,0);
1606 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1608 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
1609 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1611 color[0] = bound(0, color2[0], 1);
1612 color[1] = bound(0, color2[1], 1);
1613 color[2] = bound(0, color2[2], 1);
1614 GL_Color(color[0], color[1], color[2], 1);
1615 R_Mesh_GetSpace(numverts);
1616 R_Mesh_CopyVertex3f(vertex3f, numverts);
1617 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1619 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1620 R_Mesh_Draw(numverts, numtriangles, elements);
1622 c_rt_lighttris += numtriangles;
1628 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
1630 R_Mesh_Matrix(matrix);
1631 R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
1634 cvar_t r_editlights = {0, "r_editlights", "0"};
1635 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
1636 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
1637 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
1638 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
1639 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
1640 cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
1641 cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
1642 worldlight_t *r_shadow_worldlightchain;
1643 worldlight_t *r_shadow_selectedlight;
1644 vec3_t r_editlights_cursorlocation;
1646 static int castshadowcount = 1;
1647 void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname, int castshadow)
1649 int i, j, k, l, maxverts = 256, *mark, tris;
1650 float *vertex3f = NULL;
1652 shadowmesh_t *mesh, *castmesh;
1656 surfmesh_t *surfmesh;
1658 if (radius < 15 || DotProduct(color, color) < 0.03)
1660 Con_Printf("R_Shadow_NewWorldLight: refusing to create a light too small/dim\n");
1664 e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
1665 VectorCopy(origin, e->origin);
1666 VectorCopy(color, e->light);
1667 e->lightradius = radius;
1669 if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
1671 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
1674 e->castshadows = castshadow;
1676 e->cullradius = e->lightradius;
1677 for (k = 0;k < 3;k++)
1679 e->mins[k] = e->origin[k] - e->lightradius;
1680 e->maxs[k] = e->origin[k] + e->lightradius;
1683 e->next = r_shadow_worldlightchain;
1684 r_shadow_worldlightchain = e;
1685 if (cubemapname && cubemapname[0])
1687 e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
1688 strcpy(e->cubemapname, cubemapname);
1689 // FIXME: add cubemap loading (and don't load a cubemap twice)
1694 i = Mod_PointContents(e->origin, cl.worldmodel);
1695 if (r_shadow_portallight.integer && i != CONTENTS_SOLID && i != CONTENTS_SKY)
1698 qbyte *bytesurfacepvs;
1700 byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->numleafs + 1);
1701 bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->numsurfaces);
1703 Portal_Visibility(cl.worldmodel, e->origin, byteleafpvs, bytesurfacepvs, NULL, 0, true, RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin));
1705 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1706 if (byteleafpvs[i+1] && BoxesOverlap(leaf->mins, leaf->maxs, e->mins, e->maxs))
1707 leaf->worldnodeframe = castshadowcount;
1709 for (i = 0, surf = cl.worldmodel->surfaces;i < cl.worldmodel->numsurfaces;i++, surf++)
1710 if (bytesurfacepvs[i] && BoxesOverlap(surf->poly_mins, surf->poly_maxs, e->mins, e->maxs))
1711 surf->castshadow = castshadowcount;
1713 Mem_Free(byteleafpvs);
1714 Mem_Free(bytesurfacepvs);
1718 leaf = Mod_PointInLeaf(origin, cl.worldmodel);
1719 pvs = Mod_LeafPVS(leaf, cl.worldmodel);
1720 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1722 if (pvs[i >> 3] & (1 << (i & 7)) && BoxesOverlap(leaf->mins, leaf->maxs, e->mins, e->maxs))
1724 leaf->worldnodeframe = castshadowcount;
1725 for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
1727 surf = cl.worldmodel->surfaces + *mark;
1728 if (surf->castshadow != castshadowcount && BoxesOverlap(surf->poly_mins, surf->poly_maxs, e->mins, e->maxs))
1729 surf->castshadow = castshadowcount;
1736 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1737 if (leaf->worldnodeframe == castshadowcount)
1740 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1741 if (surf->castshadow == castshadowcount)
1745 e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
1747 e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
1749 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1750 if (leaf->worldnodeframe == castshadowcount)
1751 e->leafs[e->numleafs++] = leaf;
1753 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1754 if (surf->castshadow == castshadowcount)
1755 e->surfaces[e->numsurfaces++] = surf;
1757 // find bounding box of lit leafs
1758 VectorCopy(e->origin, e->mins);
1759 VectorCopy(e->origin, e->maxs);
1760 for (j = 0;j < e->numleafs;j++)
1763 for (k = 0;k < 3;k++)
1765 if (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
1766 if (e->maxs[k] < leaf->maxs[k]) e->maxs[k] = leaf->maxs[k];
1770 for (k = 0;k < 3;k++)
1772 if (e->mins[k] < e->origin[k] - e->lightradius) e->mins[k] = e->origin[k] - e->lightradius;
1773 if (e->maxs[k] > e->origin[k] + e->lightradius) e->maxs[k] = e->origin[k] + e->lightradius;
1775 e->cullradius = RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin);
1780 for (j = 0;j < e->numsurfaces;j++)
1782 surf = e->surfaces[j];
1783 if (surf->flags & SURF_SHADOWCAST)
1785 surf->castshadow = castshadowcount;
1786 if (maxverts < surf->poly_numverts)
1787 maxverts = surf->poly_numverts;
1790 e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1791 // make a mesh to cast a shadow volume from
1792 castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1793 for (j = 0;j < e->numsurfaces;j++)
1794 if (e->surfaces[j]->castshadow == castshadowcount)
1795 for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
1796 Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->vertex3f, surfmesh->numtriangles, surfmesh->element3i);
1797 castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
1799 // cast shadow volume from castmesh
1800 for (mesh = castmesh;mesh;mesh = mesh->next)
1802 R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
1803 R_Shadow_ResizeShadowElements(castmesh->numtriangles);
1805 if (maxverts < castmesh->numverts * 2)
1807 maxverts = castmesh->numverts * 2;
1812 if (vertex3f == NULL && maxverts > 0)
1813 vertex3f = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[3]));
1815 // now that we have the buffers big enough, construct shadow volume mesh
1816 memcpy(vertex3f, castmesh->vertex3f, castmesh->numverts * sizeof(float[3]));
1817 R_Shadow_ProjectVertex3f(vertex3f, castmesh->numverts, e->origin, r_shadow_projectdistance.value);//, e->lightradius);
1818 tris = R_Shadow_MakeTriangleShadowFlags_Vertex3f(castmesh->element3i, vertex3f, castmesh->numtriangles, trianglefacinglight, trianglefacinglightlist, e->origin);
1819 tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->element3i, castmesh->neighbor3i, castmesh->numverts, trianglefacinglight, trianglefacinglightlist, tris, shadowelements);
1820 // add the constructed shadow volume mesh
1821 Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, vertex3f, tris, shadowelements);
1826 // we're done with castmesh now
1827 Mod_ShadowMesh_Free(castmesh);
1828 e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
1829 for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
1830 l += mesh->numtriangles;
1831 Con_Printf("static shadow volume built containing %i triangles\n", l);
1834 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);
1837 void R_Shadow_FreeWorldLight(worldlight_t *light)
1839 worldlight_t **lightpointer;
1840 for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
1841 if (*lightpointer != light)
1842 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
1843 *lightpointer = light->next;
1844 if (light->cubemapname)
1845 Mem_Free(light->cubemapname);
1846 if (light->shadowvolume)
1847 Mod_ShadowMesh_Free(light->shadowvolume);
1848 if (light->surfaces)
1849 Mem_Free(light->surfaces);
1851 Mem_Free(light->leafs);
1855 void R_Shadow_ClearWorldLights(void)
1857 while (r_shadow_worldlightchain)
1858 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
1859 r_shadow_selectedlight = NULL;
1862 void R_Shadow_SelectLight(worldlight_t *light)
1864 if (r_shadow_selectedlight)
1865 r_shadow_selectedlight->selected = false;
1866 r_shadow_selectedlight = light;
1867 if (r_shadow_selectedlight)
1868 r_shadow_selectedlight->selected = true;
1872 void R_DrawLightSprite(int texnum, const vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
1879 VectorSubtract(origin, r_origin, diff);
1880 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
1883 memset(&m, 0, sizeof(m));
1884 m.blendfunc1 = GL_SRC_ALPHA;
1885 m.blendfunc2 = GL_ONE;
1887 R_Mesh_Matrix(&r_identitymatrix);
1890 GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1891 R_DrawSpriteMesh(origin, vright, vup, scale, -scale, -scale, scale);
1894 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
1897 pic = Draw_CachePic("gfx/crosshair1.tga");
1899 R_DrawLightSprite(R_GetTexture(pic->tex), r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 0.5);
1902 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
1905 const worldlight_t *light;
1908 if (light->selected)
1909 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
1910 if (light->shadowvolume)
1911 R_DrawLightSprite(calldata2, light->origin, 8, intensity, intensity, intensity, 0.5);
1913 R_DrawLightSprite(calldata2, light->origin, 8, intensity * 0.5, intensity * 0.5, intensity * 0.5, 0.5);
1916 void R_Shadow_DrawLightSprites(void)
1920 worldlight_t *light;
1922 for (i = 0;i < 5;i++)
1924 pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1));
1926 texnums[i] = R_GetTexture(pic->tex);
1931 for (light = r_shadow_worldlightchain;light;light = light->next)
1932 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, texnums[((int) light) % 5]);
1933 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
1936 void R_Shadow_SelectLightInView(void)
1938 float bestrating, rating, temp[3];
1939 worldlight_t *best, *light;
1942 for (light = r_shadow_worldlightchain;light;light = light->next)
1944 VectorSubtract(light->origin, r_refdef.vieworg, temp);
1945 rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
1948 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
1949 if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
1951 bestrating = rating;
1956 R_Shadow_SelectLight(best);
1959 void R_Shadow_LoadWorldLights(void)
1961 int n, a, style, shadow;
1962 char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
1963 float origin[3], radius, color[3];
1964 if (cl.worldmodel == NULL)
1966 Con_Printf("No map loaded.\n");
1969 FS_StripExtension(cl.worldmodel->name, name);
1970 strcat(name, ".rtlights");
1971 lightsstring = FS_LoadFile(name, false);
1979 while (*s && *s != '\n')
1985 // check for modifier flags
1991 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);
1997 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);
2000 VectorScale(color, r_editlights_rtlightscolorscale.value, color);
2001 radius *= r_editlights_rtlightssizescale.value;
2002 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadow);
2007 Con_Printf("invalid rtlights file \"%s\"\n", name);
2008 Mem_Free(lightsstring);
2012 void R_Shadow_SaveWorldLights(void)
2014 worldlight_t *light;
2015 int bufchars, bufmaxchars;
2017 char name[MAX_QPATH];
2019 if (!r_shadow_worldlightchain)
2021 if (cl.worldmodel == NULL)
2023 Con_Printf("No map loaded.\n");
2026 FS_StripExtension(cl.worldmodel->name, name);
2027 strcat(name, ".rtlights");
2028 bufchars = bufmaxchars = 0;
2030 for (light = r_shadow_worldlightchain;light;light = light->next)
2032 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 : "");
2033 if (bufchars + strlen(line) > bufmaxchars)
2035 bufmaxchars = bufchars + strlen(line) + 2048;
2037 buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
2041 memcpy(buf, oldbuf, bufchars);
2047 memcpy(buf + bufchars, line, strlen(line));
2048 bufchars += strlen(line);
2052 FS_WriteFile(name, buf, bufchars);
2057 void R_Shadow_LoadLightsFile(void)
2060 char name[MAX_QPATH], *lightsstring, *s, *t;
2061 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
2062 if (cl.worldmodel == NULL)
2064 Con_Printf("No map loaded.\n");
2067 FS_StripExtension(cl.worldmodel->name, name);
2068 strcat(name, ".lights");
2069 lightsstring = FS_LoadFile(name, false);
2077 while (*s && *s != '\n')
2082 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);
2086 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);
2089 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
2090 radius = bound(15, radius, 4096);
2091 VectorScale(color, (2.0f / (8388608.0f)), color);
2092 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
2097 Con_Printf("invalid lights file \"%s\"\n", name);
2098 Mem_Free(lightsstring);
2102 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
2104 int entnum, style, islight;
2105 char key[256], value[1024];
2106 float origin[3], radius, color[3], light, scale, originhack[3], overridecolor[3];
2109 if (cl.worldmodel == NULL)
2111 Con_Printf("No map loaded.\n");
2114 data = cl.worldmodel->entities;
2117 for (entnum = 0;COM_ParseToken(&data) && com_token[0] == '{';entnum++)
2120 origin[0] = origin[1] = origin[2] = 0;
2121 originhack[0] = originhack[1] = originhack[2] = 0;
2122 color[0] = color[1] = color[2] = 1;
2123 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
2129 if (!COM_ParseToken(&data))
2131 if (com_token[0] == '}')
2132 break; // end of entity
2133 if (com_token[0] == '_')
2134 strcpy(key, com_token + 1);
2136 strcpy(key, com_token);
2137 while (key[strlen(key)-1] == ' ') // remove trailing spaces
2138 key[strlen(key)-1] = 0;
2139 if (!COM_ParseToken(&data))
2141 strcpy(value, com_token);
2143 // now that we have the key pair worked out...
2144 if (!strcmp("light", key))
2145 light = atof(value);
2146 else if (!strcmp("origin", key))
2147 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
2148 else if (!strcmp("color", key))
2149 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
2150 else if (!strcmp("wait", key))
2151 scale = atof(value);
2152 else if (!strcmp("classname", key))
2154 if (!strncmp(value, "light", 5))
2157 if (!strcmp(value, "light_fluoro"))
2162 overridecolor[0] = 1;
2163 overridecolor[1] = 1;
2164 overridecolor[2] = 1;
2166 if (!strcmp(value, "light_fluorospark"))
2171 overridecolor[0] = 1;
2172 overridecolor[1] = 1;
2173 overridecolor[2] = 1;
2175 if (!strcmp(value, "light_globe"))
2180 overridecolor[0] = 1;
2181 overridecolor[1] = 0.8;
2182 overridecolor[2] = 0.4;
2184 if (!strcmp(value, "light_flame_large_yellow"))
2189 overridecolor[0] = 1;
2190 overridecolor[1] = 0.5;
2191 overridecolor[2] = 0.1;
2193 if (!strcmp(value, "light_flame_small_yellow"))
2198 overridecolor[0] = 1;
2199 overridecolor[1] = 0.5;
2200 overridecolor[2] = 0.1;
2202 if (!strcmp(value, "light_torch_small_white"))
2207 overridecolor[0] = 1;
2208 overridecolor[1] = 0.5;
2209 overridecolor[2] = 0.1;
2211 if (!strcmp(value, "light_torch_small_walltorch"))
2216 overridecolor[0] = 1;
2217 overridecolor[1] = 0.5;
2218 overridecolor[2] = 0.1;
2222 else if (!strcmp("style", key))
2223 style = atoi(value);
2225 if (light <= 0 && islight)
2227 radius = min(light * r_editlights_quakelightsizescale.value / scale, 1048576);
2228 light = sqrt(bound(0, light, 1048576)) * (1.0f / 16.0f);
2229 if (color[0] == 1 && color[1] == 1 && color[2] == 1)
2230 VectorCopy(overridecolor, color);
2231 VectorScale(color, light, color);
2232 VectorAdd(origin, originhack, origin);
2234 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
2239 void R_Shadow_SetCursorLocationForView(void)
2241 vec_t dist, push, frac;
2242 vec3_t dest, endpos, normal;
2243 VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
2244 frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
2247 dist = frac * r_editlights_cursordistance.value;
2248 push = r_editlights_cursorpushback.value;
2252 VectorMA(endpos, push, vpn, endpos);
2253 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
2255 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2256 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2257 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2260 void R_Shadow_UpdateLightingMode(void)
2262 r_shadow_lightingmode = 0;
2263 if (r_shadow_realtime.integer)
2265 if (r_shadow_worldlightchain)
2266 r_shadow_lightingmode = 2;
2268 r_shadow_lightingmode = 1;
2272 void R_Shadow_UpdateWorldLightSelection(void)
2274 R_Shadow_SetCursorLocationForView();
2275 if (r_editlights.integer)
2277 R_Shadow_SelectLightInView();
2278 R_Shadow_DrawLightSprites();
2281 R_Shadow_SelectLight(NULL);
2284 void R_Shadow_EditLights_Clear_f(void)
2286 R_Shadow_ClearWorldLights();
2289 void R_Shadow_EditLights_Reload_f(void)
2291 r_shadow_reloadlights = true;
2294 void R_Shadow_EditLights_Save_f(void)
2297 R_Shadow_SaveWorldLights();
2300 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
2302 R_Shadow_ClearWorldLights();
2303 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
2306 void R_Shadow_EditLights_ImportLightsFile_f(void)
2308 R_Shadow_ClearWorldLights();
2309 R_Shadow_LoadLightsFile();
2312 void R_Shadow_EditLights_Spawn_f(void)
2315 if (!r_editlights.integer)
2317 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
2320 if (Cmd_Argc() != 1)
2322 Con_Printf("r_editlights_spawn does not take parameters\n");
2325 color[0] = color[1] = color[2] = 1;
2326 R_Shadow_NewWorldLight(r_editlights_cursorlocation, 200, color, 0, NULL, true);
2329 void R_Shadow_EditLights_Edit_f(void)
2331 vec3_t origin, color;
2334 char cubemapname[1024];
2335 if (!r_editlights.integer)
2337 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
2340 if (!r_shadow_selectedlight)
2342 Con_Printf("No selected light.\n");
2345 VectorCopy(r_shadow_selectedlight->origin, origin);
2346 radius = r_shadow_selectedlight->lightradius;
2347 VectorCopy(r_shadow_selectedlight->light, color);
2348 style = r_shadow_selectedlight->style;
2349 if (r_shadow_selectedlight->cubemapname)
2350 strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
2353 shadows = r_shadow_selectedlight->castshadows;
2354 if (!strcmp(Cmd_Argv(1), "origin"))
2356 if (Cmd_Argc() != 5)
2358 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
2361 origin[0] = atof(Cmd_Argv(2));
2362 origin[1] = atof(Cmd_Argv(3));
2363 origin[2] = atof(Cmd_Argv(4));
2365 else if (!strcmp(Cmd_Argv(1), "originx"))
2367 if (Cmd_Argc() != 3)
2369 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2372 origin[0] = atof(Cmd_Argv(2));
2374 else if (!strcmp(Cmd_Argv(1), "originy"))
2376 if (Cmd_Argc() != 3)
2378 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2381 origin[1] = atof(Cmd_Argv(2));
2383 else if (!strcmp(Cmd_Argv(1), "originz"))
2385 if (Cmd_Argc() != 3)
2387 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2390 origin[2] = atof(Cmd_Argv(2));
2392 else if (!strcmp(Cmd_Argv(1), "move"))
2394 if (Cmd_Argc() != 5)
2396 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
2399 origin[0] += atof(Cmd_Argv(2));
2400 origin[1] += atof(Cmd_Argv(3));
2401 origin[2] += atof(Cmd_Argv(4));
2403 else if (!strcmp(Cmd_Argv(1), "movex"))
2405 if (Cmd_Argc() != 3)
2407 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2410 origin[0] += atof(Cmd_Argv(2));
2412 else if (!strcmp(Cmd_Argv(1), "movey"))
2414 if (Cmd_Argc() != 3)
2416 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2419 origin[1] += atof(Cmd_Argv(2));
2421 else if (!strcmp(Cmd_Argv(1), "movez"))
2423 if (Cmd_Argc() != 3)
2425 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2428 origin[2] += atof(Cmd_Argv(2));
2430 else if (!strcmp(Cmd_Argv(1), "color"))
2432 if (Cmd_Argc() != 5)
2434 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(0));
2437 color[0] = atof(Cmd_Argv(2));
2438 color[1] = atof(Cmd_Argv(3));
2439 color[2] = atof(Cmd_Argv(4));
2441 else if (!strcmp(Cmd_Argv(1), "radius"))
2443 if (Cmd_Argc() != 3)
2445 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2448 radius = atof(Cmd_Argv(2));
2450 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "style"))
2452 if (Cmd_Argc() != 3)
2454 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2457 style = atoi(Cmd_Argv(2));
2459 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "cubemap"))
2463 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2466 if (Cmd_Argc() == 3)
2467 strcpy(cubemapname, Cmd_Argv(2));
2471 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "shadows"))
2473 if (Cmd_Argc() != 3)
2475 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2478 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
2482 Con_Printf("usage: r_editlights_edit [property] [value]\n");
2483 Con_Printf("Selected light's properties:\n");
2484 Con_Printf("Origin: %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
2485 Con_Printf("Radius: %f\n", r_shadow_selectedlight->lightradius);
2486 Con_Printf("Color: %f %f %f\n", r_shadow_selectedlight->light[0], r_shadow_selectedlight->light[1], r_shadow_selectedlight->light[2]);
2487 Con_Printf("Style: %i\n", r_shadow_selectedlight->style);
2488 Con_Printf("Cubemap: %s\n", r_shadow_selectedlight->cubemapname);
2489 Con_Printf("Shadows: %s\n", r_shadow_selectedlight->castshadows ? "yes" : "no");
2492 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2493 r_shadow_selectedlight = NULL;
2494 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadows);
2497 extern int con_vislines;
2498 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
2502 if (r_shadow_selectedlight == NULL)
2506 sprintf(temp, "Light properties");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2507 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;
2508 sprintf(temp, "Radius %f", r_shadow_selectedlight->lightradius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2509 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;
2510 sprintf(temp, "Style %i", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2511 sprintf(temp, "Cubemap %s", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2512 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;
2515 void R_Shadow_EditLights_ToggleShadow_f(void)
2517 if (!r_editlights.integer)
2519 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
2522 if (!r_shadow_selectedlight)
2524 Con_Printf("No selected light.\n");
2527 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);
2528 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2529 r_shadow_selectedlight = NULL;
2532 void R_Shadow_EditLights_Remove_f(void)
2534 if (!r_editlights.integer)
2536 Con_Printf("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
2539 if (!r_shadow_selectedlight)
2541 Con_Printf("No selected light.\n");
2544 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2545 r_shadow_selectedlight = NULL;
2548 void R_Shadow_EditLights_Init(void)
2550 Cvar_RegisterVariable(&r_editlights);
2551 Cvar_RegisterVariable(&r_editlights_cursordistance);
2552 Cvar_RegisterVariable(&r_editlights_cursorpushback);
2553 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
2554 Cvar_RegisterVariable(&r_editlights_cursorgrid);
2555 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
2556 Cvar_RegisterVariable(&r_editlights_rtlightssizescale);
2557 Cvar_RegisterVariable(&r_editlights_rtlightscolorscale);
2558 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
2559 Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
2560 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
2561 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
2562 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
2563 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
2564 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f);
2565 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
2566 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);