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 mempool_t *r_shadow_mempool;
19 int maxshadowelements;
21 int maxtrianglefacinglight;
22 qbyte *trianglefacinglight;
23 int *trianglefacinglightlist;
29 rtexturepool_t *r_shadow_texturepool;
30 rtexture_t *r_shadow_normalcubetexture;
31 rtexture_t *r_shadow_attenuation2dtexture;
32 rtexture_t *r_shadow_attenuation3dtexture;
33 rtexture_t *r_shadow_blankbumptexture;
34 rtexture_t *r_shadow_blankglosstexture;
35 rtexture_t *r_shadow_blankwhitetexture;
37 cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5"};
38 cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1"};
39 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
40 cvar_t r_shadow_realtime_world = {0, "r_shadow_realtime_world", "0"};
41 cvar_t r_shadow_realtime_dlight = {0, "r_shadow_realtime_dlight", "0"};
42 cvar_t r_shadow_visiblevolumes = {0, "r_shadow_visiblevolumes", "0"};
43 cvar_t r_shadow_gloss = {0, "r_shadow_gloss", "1"};
44 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
45 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
46 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
47 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0"};
48 cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "-1"};
49 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1"};
50 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "100000"};
51 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1"};
53 int c_rt_lights, c_rt_clears, c_rt_scissored;
54 int c_rt_shadowmeshes, c_rt_shadowtris, c_rt_lightmeshes, c_rt_lighttris;
55 int c_rtcached_shadowmeshes, c_rtcached_shadowtris;
57 void R_Shadow_ClearWorldLights(void);
58 void R_Shadow_SaveWorldLights(void);
59 void R_Shadow_LoadWorldLights(void);
60 void R_Shadow_LoadLightsFile(void);
61 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void);
63 void r_shadow_start(void)
65 // allocate vertex processing arrays
66 r_shadow_mempool = Mem_AllocPool("R_Shadow");
67 maxshadowelements = 0;
68 shadowelements = NULL;
72 maxtrianglefacinglight = 0;
73 trianglefacinglight = NULL;
74 trianglefacinglightlist = NULL;
75 r_shadow_normalcubetexture = NULL;
76 r_shadow_attenuation2dtexture = NULL;
77 r_shadow_attenuation3dtexture = NULL;
78 r_shadow_blankbumptexture = NULL;
79 r_shadow_blankglosstexture = NULL;
80 r_shadow_blankwhitetexture = NULL;
81 r_shadow_texturepool = NULL;
82 R_Shadow_ClearWorldLights();
83 r_shadow_reloadlights = true;
86 void r_shadow_shutdown(void)
88 R_Shadow_ClearWorldLights();
89 r_shadow_reloadlights = true;
90 r_shadow_normalcubetexture = NULL;
91 r_shadow_attenuation2dtexture = NULL;
92 r_shadow_attenuation3dtexture = NULL;
93 r_shadow_blankbumptexture = NULL;
94 r_shadow_blankglosstexture = NULL;
95 r_shadow_blankwhitetexture = NULL;
96 R_FreeTexturePool(&r_shadow_texturepool);
97 maxshadowelements = 0;
98 shadowelements = NULL;
102 maxtrianglefacinglight = 0;
103 trianglefacinglight = NULL;
104 trianglefacinglightlist = NULL;
105 Mem_FreePool(&r_shadow_mempool);
108 void r_shadow_newmap(void)
110 R_Shadow_ClearWorldLights();
111 r_shadow_reloadlights = true;
114 void R_Shadow_Init(void)
116 Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
117 Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
118 Cvar_RegisterVariable(&r_shadow_lightintensityscale);
119 Cvar_RegisterVariable(&r_shadow_realtime_world);
120 Cvar_RegisterVariable(&r_shadow_realtime_dlight);
121 Cvar_RegisterVariable(&r_shadow_visiblevolumes);
122 Cvar_RegisterVariable(&r_shadow_gloss);
123 Cvar_RegisterVariable(&r_shadow_debuglight);
124 Cvar_RegisterVariable(&r_shadow_scissor);
125 Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
126 Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
127 Cvar_RegisterVariable(&r_shadow_polygonoffset);
128 Cvar_RegisterVariable(&r_shadow_portallight);
129 Cvar_RegisterVariable(&r_shadow_projectdistance);
130 Cvar_RegisterVariable(&r_shadow_texture3d);
131 R_Shadow_EditLights_Init();
132 R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
135 int R_Shadow_MakeTriangleShadowFlags_Vertex3f(const int *elements, const float *vertex, int numtris, qbyte *facing, int *list, const float *relativelightorigin)
138 const float *v0, *v1, *v2;
139 for (i = 0;i < numtris;i++, elements += 3)
141 // calculate triangle facing flag
142 v0 = vertex + elements[0] * 3;
143 v1 = vertex + elements[1] * 3;
144 v2 = vertex + elements[2] * 3;
145 if(PointInfrontOfTriangle(relativelightorigin, v0, v1, v2))
156 int R_Shadow_BuildShadowVolume(const int *elements, const int *neighbors, int numverts, const qbyte *facing, const int *facinglist, int numfacing, int *out, float *vertices, const float *relativelightorigin, float projectdistance)
158 int i, j, tris, vertexpointeradjust = numverts * 3;
162 if (maxvertexupdate < numverts)
164 maxvertexupdate = numverts;
166 Mem_Free(vertexupdate);
167 vertexupdate = Mem_Alloc(r_shadow_mempool, maxvertexupdate * sizeof(int));
171 // check each frontface for bordering backfaces,
172 // and cast shadow polygons from those edges,
173 // also create front and back caps for shadow volume
174 tris = numfacing * 2;
176 for (i = 0;i < numfacing;i++)
178 e = elements + facinglist[i] * 3;
185 for (i = 0;i < numfacing;i++)
187 e = elements + facinglist[i] * 3;
188 // generate vertices if needed
189 for (j = 0;j < 3;j++)
191 if (vertexupdate[e[j]] != vertexupdatenum)
193 vertexupdate[e[j]] = vertexupdatenum;
194 vin = vertices + e[j] * 3;
195 vout = vin + vertexpointeradjust;
196 vout[0] = relativelightorigin[0] + projectdistance * (vin[0] - relativelightorigin[0]);
197 vout[1] = relativelightorigin[1] + projectdistance * (vin[1] - relativelightorigin[1]);
198 vout[2] = relativelightorigin[2] + projectdistance * (vin[2] - relativelightorigin[2]);
201 out[0] = e[2] + numverts;
202 out[1] = e[1] + numverts;
203 out[2] = e[0] + numverts;
206 // output sides around frontfaces
207 for (i = 0;i < numfacing;i++)
209 n = neighbors + facinglist[i] * 3;
211 if (n[0] < 0 || !facing[n[0]])
213 e = elements + facinglist[i] * 3;
216 out[2] = e[0] + numverts;
218 out[4] = e[0] + numverts;
219 out[5] = e[1] + numverts;
223 if (n[1] < 0 || !facing[n[1]])
225 e = elements + facinglist[i] * 3;
228 out[2] = e[1] + numverts;
230 out[4] = e[1] + numverts;
231 out[5] = e[2] + numverts;
235 if (n[2] < 0 || !facing[n[2]])
237 e = elements + facinglist[i] * 3;
240 out[2] = e[2] + numverts;
242 out[4] = e[2] + numverts;
243 out[5] = e[0] + numverts;
251 void R_Shadow_ResizeTriangleFacingLight(int numtris)
253 // make sure trianglefacinglight is big enough for this volume
254 // ameks ru ertaignelaficgnilhg tsib gie ongu hof rhtsiv lomu e
255 // m4k3 5ur3 7r14ng13f4c1n5115h7 15 b15 3n0u5h f0r 7h15 v01um3
256 if (maxtrianglefacinglight < numtris)
258 maxtrianglefacinglight = numtris;
259 if (trianglefacinglight)
260 Mem_Free(trianglefacinglight);
261 if (trianglefacinglightlist)
262 Mem_Free(trianglefacinglightlist);
263 trianglefacinglight = Mem_Alloc(r_shadow_mempool, maxtrianglefacinglight);
264 trianglefacinglightlist = Mem_Alloc(r_shadow_mempool, sizeof(int) * maxtrianglefacinglight);
268 int *R_Shadow_ResizeShadowElements(int numtris)
270 // make sure shadowelements is big enough for this volume
271 if (maxshadowelements < numtris * 24)
273 maxshadowelements = numtris * 24;
275 Mem_Free(shadowelements);
276 shadowelements = Mem_Alloc(r_shadow_mempool, maxshadowelements * sizeof(int));
278 return shadowelements;
281 void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance)
284 if (projectdistance < 0.1)
286 Con_Printf("R_Shadow_Volume: projectdistance %f\n");
294 // a triangle facing the light source
297 // a triangle not facing the light source
300 // an extrusion of the frontfaces, beginning at the original geometry and
301 // ending further from the light source than the original geometry
302 // (presumably at least as far as the light's radius, if the light has a
303 // radius at all), capped at both front and back to avoid any problems
306 // draws the shadow volumes of the model.
308 // vertex locations must already be in varray_vertex3f before use.
309 // varray_vertex3f must have capacity for numverts * 2.
311 // make sure trianglefacinglight is big enough for this volume
312 if (maxtrianglefacinglight < numtris)
313 R_Shadow_ResizeTriangleFacingLight(numtris);
315 // make sure shadowelements is big enough for this volume
316 if (maxshadowelements < numtris * 24)
317 R_Shadow_ResizeShadowElements(numtris);
319 // check which triangles are facing the light
320 tris = R_Shadow_MakeTriangleShadowFlags_Vertex3f(elements, varray_vertex3f, numtris, trianglefacinglight, trianglefacinglightlist, relativelightorigin);
324 // by clever use of elements we can construct the whole shadow from
325 // the unprojected vertices and the projected vertices
327 // output triangle elements and vertices
328 tris = R_Shadow_BuildShadowVolume(elements, neighbors, numverts, trianglefacinglight, trianglefacinglightlist, tris, shadowelements, varray_vertex3f, relativelightorigin, projectdistance);
332 if (r_shadowstage == SHADOWSTAGE_STENCIL)
334 // increment stencil if backface is behind depthbuffer
336 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
337 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
338 R_Mesh_Draw_NoBatching(numverts * 2, tris, shadowelements);
340 c_rt_shadowtris += numtris;
341 // decrement stencil if frontface is behind depthbuffer
343 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
344 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
346 R_Mesh_Draw_NoBatching(numverts * 2, tris, shadowelements);
348 c_rt_shadowtris += numtris;
351 void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
354 if (r_shadowstage == SHADOWSTAGE_STENCIL)
356 // increment stencil if backface is behind depthbuffer
358 qglCullFace(GL_BACK); // quake is backwards, this culls front faces
359 qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
360 for (mesh = firstmesh;mesh;mesh = mesh->next)
362 R_Mesh_GetSpace(mesh->numverts);
363 R_Mesh_CopyVertex3f(mesh->vertex3f, mesh->numverts);
364 R_Mesh_Draw_NoBatching(mesh->numverts, mesh->numtriangles, mesh->element3i);
365 c_rtcached_shadowmeshes++;
366 c_rtcached_shadowtris += mesh->numtriangles;
368 // decrement stencil if frontface is behind depthbuffer
370 qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
371 qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
373 for (mesh = firstmesh;mesh;mesh = mesh->next)
375 R_Mesh_GetSpace(mesh->numverts);
376 R_Mesh_CopyVertex3f(mesh->vertex3f, mesh->numverts);
377 R_Mesh_Draw_NoBatching(mesh->numverts, mesh->numtriangles, mesh->element3i);
378 c_rtcached_shadowmeshes++;
379 c_rtcached_shadowtris += mesh->numtriangles;
383 float r_shadow_attenpower, r_shadow_attenscale;
384 static void R_Shadow_MakeTextures(void)
386 int x, y, z, d, side;
387 float v[3], s, t, intensity;
389 R_FreeTexturePool(&r_shadow_texturepool);
390 r_shadow_texturepool = R_AllocTexturePool();
391 r_shadow_attenpower = r_shadow_lightattenuationpower.value;
392 r_shadow_attenscale = r_shadow_lightattenuationscale.value;
394 #define ATTEN2DSIZE 64
395 #define ATTEN3DSIZE 32
396 data = Mem_Alloc(tempmempool, max(6*NORMSIZE*NORMSIZE*4, max(ATTEN3DSIZE*ATTEN3DSIZE*ATTEN3DSIZE*4, ATTEN2DSIZE*ATTEN2DSIZE*4)));
401 r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
406 r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
411 r_shadow_blankwhitetexture = R_LoadTexture2D(r_shadow_texturepool, "blankwhite", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
412 if (gl_texturecubemap)
414 for (side = 0;side < 6;side++)
416 for (y = 0;y < NORMSIZE;y++)
418 for (x = 0;x < NORMSIZE;x++)
420 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
421 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
455 intensity = 127.0f / sqrt(DotProduct(v, v));
456 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+0] = 128.0f + intensity * v[0];
457 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+1] = 128.0f + intensity * v[1];
458 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+2] = 128.0f + intensity * v[2];
459 data[((side*NORMSIZE+y)*NORMSIZE+x)*4+3] = 255;
463 r_shadow_normalcubetexture = R_LoadTextureCubeMap(r_shadow_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
466 r_shadow_normalcubetexture = NULL;
467 for (y = 0;y < ATTEN2DSIZE;y++)
469 for (x = 0;x < ATTEN2DSIZE;x++)
471 v[0] = ((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
472 v[1] = ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375);
474 intensity = 1.0f - sqrt(DotProduct(v, v));
476 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
477 d = bound(0, intensity, 255);
478 data[(y*ATTEN2DSIZE+x)*4+0] = d;
479 data[(y*ATTEN2DSIZE+x)*4+1] = d;
480 data[(y*ATTEN2DSIZE+x)*4+2] = d;
481 data[(y*ATTEN2DSIZE+x)*4+3] = d;
484 r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
485 if (r_shadow_texture3d.integer)
487 for (z = 0;z < ATTEN3DSIZE;z++)
489 for (y = 0;y < ATTEN3DSIZE;y++)
491 for (x = 0;x < ATTEN3DSIZE;x++)
493 v[0] = ((x + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
494 v[1] = ((y + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
495 v[2] = ((z + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375);
496 intensity = 1.0f - sqrt(DotProduct(v, v));
498 intensity = pow(intensity, r_shadow_attenpower) * r_shadow_attenscale * 256.0f;
499 d = bound(0, intensity, 255);
500 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+0] = d;
501 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+1] = d;
502 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+2] = d;
503 data[((z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x)*4+3] = d;
507 r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, data, TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL);
512 void R_Shadow_Stage_Begin(void)
516 if (r_shadow_texture3d.integer && !gl_texture3d)
517 Cvar_SetValueQuick(&r_shadow_texture3d, 0);
519 //cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
520 if (!r_shadow_attenuation2dtexture
521 || (!r_shadow_attenuation3dtexture && r_shadow_texture3d.integer)
522 || r_shadow_lightattenuationpower.value != r_shadow_attenpower
523 || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
524 R_Shadow_MakeTextures();
527 memset(&m, 0, sizeof(m));
528 m.blendfunc1 = GL_ONE;
529 m.blendfunc2 = GL_ZERO;
531 GL_Color(0, 0, 0, 1);
532 qglDisable(GL_SCISSOR_TEST);
533 r_shadowstage = SHADOWSTAGE_NONE;
535 c_rt_lights = c_rt_clears = c_rt_scissored = 0;
536 c_rt_shadowmeshes = c_rt_shadowtris = c_rt_lightmeshes = c_rt_lighttris = 0;
537 c_rtcached_shadowmeshes = c_rtcached_shadowtris = 0;
540 void R_Shadow_LoadWorldLightsIfNeeded(void)
542 if (r_shadow_reloadlights && cl.worldmodel)
544 R_Shadow_ClearWorldLights();
545 r_shadow_reloadlights = false;
546 R_Shadow_LoadWorldLights();
547 if (r_shadow_worldlightchain == NULL)
549 R_Shadow_LoadLightsFile();
550 if (r_shadow_worldlightchain == NULL)
551 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
556 void R_Shadow_Stage_ShadowVolumes(void)
560 memset(&m, 0, sizeof(m));
561 R_Mesh_TextureState(&m);
562 GL_Color(1, 1, 1, 1);
563 qglColorMask(0, 0, 0, 0);
564 qglDisable(GL_BLEND);
566 qglDepthFunc(GL_LESS);
567 qglEnable(GL_STENCIL_TEST);
568 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
569 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
570 qglEnable(GL_CULL_FACE);
571 qglEnable(GL_DEPTH_TEST);
572 r_shadowstage = SHADOWSTAGE_STENCIL;
573 qglClear(GL_STENCIL_BUFFER_BIT);
575 // LordHavoc note: many shadow volumes reside entirely inside the world
576 // (that is to say they are entirely bounded by their lit surfaces),
577 // which can be optimized by handling things as an inverted light volume,
578 // with the shadow boundaries of the world being simulated by an altered
579 // (129) bias to stencil clearing on such lights
580 // FIXME: generate inverted light volumes for use as shadow volumes and
581 // optimize for them as noted above
584 void R_Shadow_Stage_LightWithoutShadows(void)
588 memset(&m, 0, sizeof(m));
589 R_Mesh_TextureState(&m);
590 qglActiveTexture(GL_TEXTURE0_ARB);
593 qglBlendFunc(GL_ONE, GL_ONE);
594 GL_Color(1, 1, 1, 1);
595 qglColorMask(1, 1, 1, 1);
597 qglDepthFunc(GL_EQUAL);
598 qglDisable(GL_STENCIL_TEST);
599 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
600 qglStencilFunc(GL_EQUAL, 128, 0xFF);
601 qglEnable(GL_CULL_FACE);
602 qglEnable(GL_DEPTH_TEST);
603 r_shadowstage = SHADOWSTAGE_LIGHT;
607 void R_Shadow_Stage_LightWithShadows(void)
611 memset(&m, 0, sizeof(m));
612 R_Mesh_TextureState(&m);
613 qglActiveTexture(GL_TEXTURE0_ARB);
616 qglBlendFunc(GL_ONE, GL_ONE);
617 GL_Color(1, 1, 1, 1);
618 qglColorMask(1, 1, 1, 1);
620 qglDepthFunc(GL_EQUAL);
621 qglEnable(GL_STENCIL_TEST);
622 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
623 // only draw light where this geometry was already rendered AND the
624 // stencil is 128 (values other than this mean shadow)
625 qglStencilFunc(GL_EQUAL, 128, 0xFF);
626 qglEnable(GL_CULL_FACE);
627 qglEnable(GL_DEPTH_TEST);
628 r_shadowstage = SHADOWSTAGE_LIGHT;
632 void R_Shadow_Stage_End(void)
636 // attempt to restore state to what Mesh_State thinks it is
637 qglDisable(GL_BLEND);
638 qglBlendFunc(GL_ONE, GL_ZERO);
640 // now restore the rest of the state to normal
641 GL_Color(1, 1, 1, 1);
642 qglColorMask(1, 1, 1, 1);
643 qglDisable(GL_SCISSOR_TEST);
644 qglDepthFunc(GL_LEQUAL);
645 qglDisable(GL_STENCIL_TEST);
646 qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
647 qglStencilFunc(GL_ALWAYS, 128, 0xFF);
648 qglEnable(GL_CULL_FACE);
649 qglEnable(GL_DEPTH_TEST);
650 // force mesh state to reset by using various combinations of features
651 memset(&m, 0, sizeof(m));
652 m.blendfunc1 = GL_SRC_ALPHA;
653 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
655 m.blendfunc1 = GL_ONE;
656 m.blendfunc2 = GL_ZERO;
658 r_shadowstage = SHADOWSTAGE_NONE;
662 int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const float *origin, float radius)
664 int i, ix1, iy1, ix2, iy2;
665 float x1, y1, x2, y2, x, y;
668 if (!r_shadow_scissor.integer)
670 // if view is inside the box, just say yes it's visible
671 if (r_origin[0] >= mins[0] && r_origin[0] <= maxs[0]
672 && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1]
673 && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2])
676 qglDisable(GL_SCISSOR_TEST);
679 VectorSubtract(r_origin, origin, v);
680 if (DotProduct(v, v) < radius * radius)
683 qglDisable(GL_SCISSOR_TEST);
686 // create viewspace bbox
687 for (i = 0;i < 8;i++)
689 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
690 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
691 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
692 v2[0] = DotProduct(v, vright);
693 v2[1] = DotProduct(v, vup);
694 v2[2] = DotProduct(v, vpn);
697 if (smins[0] > v2[0]) smins[0] = v2[0];
698 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
699 if (smins[1] > v2[1]) smins[1] = v2[1];
700 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
701 if (smins[2] > v2[2]) smins[2] = v2[2];
702 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
706 smins[0] = smaxs[0] = v2[0];
707 smins[1] = smaxs[1] = v2[1];
708 smins[2] = smaxs[2] = v2[2];
711 // now we have a bbox in viewspace
712 // clip it to the viewspace version of the sphere
713 v[0] = origin[0] - r_origin[0];
714 v[1] = origin[1] - r_origin[1];
715 v[2] = origin[2] - r_origin[2];
716 v2[0] = DotProduct(v, vright);
717 v2[1] = DotProduct(v, vup);
718 v2[2] = DotProduct(v, vpn);
719 if (smins[0] < v2[0] - radius) smins[0] = v2[0] - radius;
720 if (smaxs[0] < v2[0] - radius) smaxs[0] = v2[0] + radius;
721 if (smins[1] < v2[1] - radius) smins[1] = v2[1] - radius;
722 if (smaxs[1] < v2[1] - radius) smaxs[1] = v2[1] + radius;
723 if (smins[2] < v2[2] - radius) smins[2] = v2[2] - radius;
724 if (smaxs[2] < v2[2] - radius) smaxs[2] = v2[2] + radius;
725 // clip it to the view plane
728 // return true if that culled the box
729 if (smins[2] >= smaxs[2])
731 // ok some of it is infront of the view, transform each corner back to
732 // worldspace and then to screenspace and make screen rect
733 // initialize these variables just to avoid compiler warnings
734 x1 = y1 = x2 = y2 = 0;
735 for (i = 0;i < 8;i++)
737 v2[0] = (i & 1) ? smins[0] : smaxs[0];
738 v2[1] = (i & 2) ? smins[1] : smaxs[1];
739 v2[2] = (i & 4) ? smins[2] : smaxs[2];
740 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
741 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
742 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
744 GL_TransformToScreen(v, v2);
745 //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]);
762 // this code doesn't handle boxes with any points behind view properly
763 x1 = 1000;x2 = -1000;
764 y1 = 1000;y2 = -1000;
765 for (i = 0;i < 8;i++)
767 v[0] = (i & 1) ? mins[0] : maxs[0];
768 v[1] = (i & 2) ? mins[1] : maxs[1];
769 v[2] = (i & 4) ? mins[2] : maxs[2];
771 GL_TransformToScreen(v, v2);
772 //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]);
789 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
790 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
791 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
792 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
793 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
794 if (ix2 <= ix1 || iy2 <= iy1)
796 // set up the scissor rectangle
798 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
799 qglEnable(GL_SCISSOR_TEST);
805 int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
807 int i, ix1, iy1, ix2, iy2;
808 float x1, y1, x2, y2, x, y, f;
811 if (!r_shadow_scissor.integer)
813 // if view is inside the box, just say yes it's visible
814 // LordHavoc: for some odd reason scissor seems broken without stencil
815 // (?!? seems like a driver bug) so abort if gl_stencil is false
816 if (!gl_stencil || BoxesOverlap(r_origin, r_origin, mins, maxs))
819 qglDisable(GL_SCISSOR_TEST);
822 for (i = 0;i < 3;i++)
835 f = DotProduct(vpn, r_origin) + 1;
836 if (DotProduct(vpn, v2) <= f)
838 // entirely behind nearclip plane
841 if (DotProduct(vpn, v) >= f)
843 // entirely infront of nearclip plane
844 x1 = y1 = x2 = y2 = 0;
845 for (i = 0;i < 8;i++)
847 v[0] = (i & 1) ? mins[0] : maxs[0];
848 v[1] = (i & 2) ? mins[1] : maxs[1];
849 v[2] = (i & 4) ? mins[2] : maxs[2];
851 GL_TransformToScreen(v, v2);
852 //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]);
871 // clipped by nearclip plane
872 // this is nasty and crude...
873 // create viewspace bbox
874 for (i = 0;i < 8;i++)
876 v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
877 v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
878 v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
879 v2[0] = DotProduct(v, vright);
880 v2[1] = DotProduct(v, vup);
881 v2[2] = DotProduct(v, vpn);
884 if (smins[0] > v2[0]) smins[0] = v2[0];
885 if (smaxs[0] < v2[0]) smaxs[0] = v2[0];
886 if (smins[1] > v2[1]) smins[1] = v2[1];
887 if (smaxs[1] < v2[1]) smaxs[1] = v2[1];
888 if (smins[2] > v2[2]) smins[2] = v2[2];
889 if (smaxs[2] < v2[2]) smaxs[2] = v2[2];
893 smins[0] = smaxs[0] = v2[0];
894 smins[1] = smaxs[1] = v2[1];
895 smins[2] = smaxs[2] = v2[2];
898 // now we have a bbox in viewspace
899 // clip it to the view plane
902 // return true if that culled the box
903 if (smins[2] >= smaxs[2])
905 // ok some of it is infront of the view, transform each corner back to
906 // worldspace and then to screenspace and make screen rect
907 // initialize these variables just to avoid compiler warnings
908 x1 = y1 = x2 = y2 = 0;
909 for (i = 0;i < 8;i++)
911 v2[0] = (i & 1) ? smins[0] : smaxs[0];
912 v2[1] = (i & 2) ? smins[1] : smaxs[1];
913 v2[2] = (i & 4) ? smins[2] : smaxs[2];
914 v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
915 v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
916 v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
918 GL_TransformToScreen(v, v2);
919 //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]);
936 // this code doesn't handle boxes with any points behind view properly
937 x1 = 1000;x2 = -1000;
938 y1 = 1000;y2 = -1000;
939 for (i = 0;i < 8;i++)
941 v[0] = (i & 1) ? mins[0] : maxs[0];
942 v[1] = (i & 2) ? mins[1] : maxs[1];
943 v[2] = (i & 4) ? mins[2] : maxs[2];
945 GL_TransformToScreen(v, v2);
946 //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]);
964 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
965 if (ix1 < r_refdef.x) ix1 = r_refdef.x;
966 if (iy1 < r_refdef.y) iy1 = r_refdef.y;
967 if (ix2 > r_refdef.x + r_refdef.width) ix2 = r_refdef.x + r_refdef.width;
968 if (iy2 > r_refdef.y + r_refdef.height) iy2 = r_refdef.y + r_refdef.height;
969 if (ix2 <= ix1 || iy2 <= iy1)
971 // set up the scissor rectangle
973 qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
974 qglEnable(GL_SCISSOR_TEST);
979 void R_Shadow_VertexLighting(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const matrix4x4_t *m)
981 float *color4f = varray_color4f;
982 float dist, dot, intensity, v[3], n[3];
983 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
985 Matrix4x4_Transform(m, vertex3f, v);
986 if ((dist = DotProduct(v, v)) < 1)
988 Matrix4x4_Transform3x3(m, normal3f, n);
989 if ((dot = DotProduct(n, v)) > 0)
992 intensity = pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale * dot / sqrt(DotProduct(n,n));
993 VectorScale(lightcolor, intensity, color4f);
998 VectorClear(color4f);
1004 VectorClear(color4f);
1010 void R_Shadow_VertexLightingWithXYAttenuationTexture(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const matrix4x4_t *m)
1012 float *color4f = varray_color4f;
1013 float dist, dot, intensity, v[3], n[3];
1014 for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
1016 Matrix4x4_Transform(m, vertex3f, v);
1017 if ((dist = fabs(v[2])) < 1)
1019 Matrix4x4_Transform3x3(m, normal3f, n);
1020 if ((dot = DotProduct(n, v)) > 0)
1022 intensity = pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale * dot / sqrt(DotProduct(n,n));
1023 VectorScale(lightcolor, intensity, color4f);
1028 VectorClear(color4f);
1034 VectorClear(color4f);
1040 // FIXME: this should be done in a vertex program when possible
1041 // FIXME: if vertex program not available, this would really benefit from 3DNow! or SSE
1042 void R_Shadow_Transform_Vertex3f_TexCoord3f(float *tc3f, int numverts, const float *vertex3f, const matrix4x4_t *matrix)
1046 tc3f[0] = vertex3f[0] * matrix->m[0][0] + vertex3f[1] * matrix->m[0][1] + vertex3f[2] * matrix->m[0][2] + matrix->m[0][3];
1047 tc3f[1] = vertex3f[0] * matrix->m[1][0] + vertex3f[1] * matrix->m[1][1] + vertex3f[2] * matrix->m[1][2] + matrix->m[1][3];
1048 tc3f[2] = vertex3f[0] * matrix->m[2][0] + vertex3f[1] * matrix->m[2][1] + vertex3f[2] * matrix->m[2][2] + matrix->m[2][3];
1055 void R_Shadow_Transform_Vertex3f_TexCoord2f(float *tc2f, int numverts, const float *vertex3f, const matrix4x4_t *matrix)
1059 tc2f[0] = vertex3f[0] * matrix->m[0][0] + vertex3f[1] * matrix->m[0][1] + vertex3f[2] * matrix->m[0][2] + matrix->m[0][3];
1060 tc2f[1] = vertex3f[0] * matrix->m[1][0] + vertex3f[1] * matrix->m[1][1] + vertex3f[2] * matrix->m[1][2] + matrix->m[1][3];
1067 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)
1071 for (i = 0;i < numverts;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1073 VectorSubtract(vertex3f, relativelightorigin, lightdir);
1074 // the cubemap normalizes this for us
1075 out3f[0] = DotProduct(svector3f, lightdir);
1076 out3f[1] = DotProduct(tvector3f, lightdir);
1077 out3f[2] = DotProduct(normal3f, lightdir);
1081 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)
1084 float lightdir[3], eyedir[3], halfdir[3];
1085 for (i = 0;i < numverts;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
1087 VectorSubtract(vertex3f, relativelightorigin, lightdir);
1088 VectorNormalizeFast(lightdir);
1089 VectorSubtract(vertex3f, relativeeyeorigin, eyedir);
1090 VectorNormalizeFast(eyedir);
1091 VectorAdd(lightdir, eyedir, halfdir);
1092 // the cubemap normalizes this for us
1093 out3f[0] = DotProduct(svector3f, halfdir);
1094 out3f[1] = DotProduct(tvector3f, halfdir);
1095 out3f[2] = DotProduct(normal3f, halfdir);
1099 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)
1102 float color[3], color2[3];
1104 memset(&m, 0, sizeof(m));
1105 if (gl_dot3arb && gl_texturecubemap && gl_combine.integer && gl_stencil)
1108 bumptexture = r_shadow_blankbumptexture;
1109 // colorscale accounts for how much we multiply the brightness during combine
1110 // mult is how many times the final pass of the lighting will be
1111 // performed to get more brightness than otherwise possible
1112 // limit mult to 64 for sanity sake
1113 if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
1115 // 3/2 3D combine path (Geforce3, Radeon 8500)
1116 //R_Mesh_EndBatch();
1117 m.tex[0] = R_GetTexture(bumptexture);
1118 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1119 m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
1120 m.texcombinergb[0] = GL_REPLACE;
1121 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1122 R_Mesh_TextureState(&m);
1123 qglColorMask(0,0,0,1);
1124 qglDisable(GL_BLEND);
1126 R_Mesh_GetSpace(numverts);
1127 R_Mesh_CopyVertex3f(vertex3f, numverts);
1128 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1129 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1130 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[2], numverts, vertex3f, matrix_modeltoattenuationxyz);
1131 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1133 c_rt_lighttris += numtriangles;
1135 //R_Mesh_EndBatch();
1136 m.tex[0] = R_GetTexture(basetexture);
1138 m.texcubemap[1] = R_GetTexture(lightcubemap);
1140 m.texcombinergb[0] = GL_MODULATE;
1141 m.texcombinergb[1] = GL_MODULATE;
1142 R_Mesh_TextureState(&m);
1143 qglColorMask(1,1,1,0);
1144 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1145 qglEnable(GL_BLEND);
1146 R_Mesh_GetSpace(numverts);
1147 R_Mesh_CopyVertex3f(vertex3f, numverts);
1148 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1150 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1151 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1152 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1154 color[0] = bound(0, color2[0], 1);
1155 color[1] = bound(0, color2[1], 1);
1156 color[2] = bound(0, color2[2], 1);
1157 GL_Color(color[0], color[1], color[2], 1);
1158 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1160 c_rt_lighttris += numtriangles;
1163 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap)
1165 // 1/2/2 3D combine path (original Radeon)
1166 //R_Mesh_EndBatch();
1167 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1168 R_Mesh_TextureState(&m);
1169 qglColorMask(0,0,0,1);
1170 qglDisable(GL_BLEND);
1172 R_Mesh_GetSpace(numverts);
1173 R_Mesh_CopyVertex3f(vertex3f, numverts);
1174 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1175 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1177 c_rt_lighttris += numtriangles;
1179 //R_Mesh_EndBatch();
1180 m.tex[0] = R_GetTexture(bumptexture);
1182 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1183 m.texcombinergb[0] = GL_REPLACE;
1184 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1185 R_Mesh_TextureState(&m);
1186 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1187 qglEnable(GL_BLEND);
1188 R_Mesh_GetSpace(numverts);
1189 R_Mesh_CopyVertex3f(vertex3f, numverts);
1190 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1191 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1192 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1194 c_rt_lighttris += numtriangles;
1196 //R_Mesh_EndBatch();
1197 m.tex[0] = R_GetTexture(basetexture);
1198 m.texcubemap[1] = R_GetTexture(lightcubemap);
1199 m.texcombinergb[0] = GL_MODULATE;
1200 m.texcombinergb[1] = GL_MODULATE;
1201 R_Mesh_TextureState(&m);
1202 qglColorMask(1,1,1,0);
1203 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1204 R_Mesh_GetSpace(numverts);
1205 R_Mesh_CopyVertex3f(vertex3f, numverts);
1206 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1208 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1209 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1210 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1212 color[0] = bound(0, color2[0], 1);
1213 color[1] = bound(0, color2[1], 1);
1214 color[2] = bound(0, color2[2], 1);
1215 GL_Color(color[0], color[1], color[2], 1);
1216 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1218 c_rt_lighttris += numtriangles;
1221 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap)
1223 // 2/2 3D combine path (original Radeon)
1224 //R_Mesh_EndBatch();
1225 m.tex[0] = R_GetTexture(bumptexture);
1226 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1227 m.texcombinergb[0] = GL_REPLACE;
1228 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1229 R_Mesh_TextureState(&m);
1231 qglColorMask(0,0,0,1);
1232 qglDisable(GL_BLEND);
1233 R_Mesh_GetSpace(numverts);
1234 R_Mesh_CopyVertex3f(vertex3f, numverts);
1235 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1236 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1237 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1239 c_rt_lighttris += numtriangles;
1241 //R_Mesh_EndBatch();
1242 m.tex[0] = R_GetTexture(basetexture);
1243 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
1244 m.texcubemap[1] = 0;
1245 m.texcombinergb[0] = GL_MODULATE;
1246 m.texcombinergb[1] = GL_MODULATE;
1247 R_Mesh_TextureState(&m);
1248 R_Mesh_GetSpace(numverts);
1249 qglColorMask(1,1,1,0);
1250 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1251 qglEnable(GL_BLEND);
1252 R_Mesh_GetSpace(numverts);
1253 R_Mesh_CopyVertex3f(vertex3f, numverts);
1254 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1255 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
1256 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1257 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1259 color[0] = bound(0, color2[0], 1);
1260 color[1] = bound(0, color2[1], 1);
1261 color[2] = bound(0, color2[2], 1);
1262 GL_Color(color[0], color[1], color[2], 1);
1263 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1265 c_rt_lighttris += numtriangles;
1268 else if (r_textureunits.integer >= 4)
1270 // 4/2 2D combine path (Geforce3, Radeon 8500)
1271 //R_Mesh_EndBatch();
1272 m.tex[0] = R_GetTexture(bumptexture);
1273 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1274 m.texcombinergb[0] = GL_REPLACE;
1275 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1276 m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
1277 m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
1278 R_Mesh_TextureState(&m);
1279 qglColorMask(0,0,0,1);
1280 qglDisable(GL_BLEND);
1282 R_Mesh_GetSpace(numverts);
1283 R_Mesh_CopyVertex3f(vertex3f, numverts);
1284 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1285 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1286 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[2], numverts, vertex3f, matrix_modeltoattenuationxyz);
1287 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[3], numverts, vertex3f, matrix_modeltoattenuationz);
1288 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1290 c_rt_lighttris += numtriangles;
1292 //R_Mesh_EndBatch();
1293 m.tex[0] = R_GetTexture(basetexture);
1294 m.texcubemap[1] = R_GetTexture(lightcubemap);
1295 m.texcombinergb[0] = GL_MODULATE;
1296 m.texcombinergb[1] = GL_MODULATE;
1299 R_Mesh_TextureState(&m);
1300 qglColorMask(1,1,1,0);
1301 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1302 qglEnable(GL_BLEND);
1303 R_Mesh_GetSpace(numverts);
1304 R_Mesh_CopyVertex3f(vertex3f, numverts);
1305 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1307 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1308 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1309 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1311 color[0] = bound(0, color2[0], 1);
1312 color[1] = bound(0, color2[1], 1);
1313 color[2] = bound(0, color2[2], 1);
1314 GL_Color(color[0], color[1], color[2], 1);
1315 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1317 c_rt_lighttris += numtriangles;
1322 // 2/2/2 2D combine path (any dot3 card)
1323 //R_Mesh_EndBatch();
1324 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1325 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1326 R_Mesh_TextureState(&m);
1327 qglColorMask(0,0,0,1);
1328 qglDisable(GL_BLEND);
1330 R_Mesh_GetSpace(numverts);
1331 R_Mesh_CopyVertex3f(vertex3f, numverts);
1332 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1333 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
1334 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1336 c_rt_lighttris += numtriangles;
1338 //R_Mesh_EndBatch();
1339 m.tex[0] = R_GetTexture(bumptexture);
1341 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1342 m.texcombinergb[0] = GL_REPLACE;
1343 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1344 R_Mesh_TextureState(&m);
1345 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1346 qglEnable(GL_BLEND);
1347 R_Mesh_GetSpace(numverts);
1348 R_Mesh_CopyVertex3f(vertex3f, numverts);
1349 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1350 R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
1351 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1353 c_rt_lighttris += numtriangles;
1355 //R_Mesh_EndBatch();
1356 m.tex[0] = R_GetTexture(basetexture);
1357 m.texcubemap[1] = R_GetTexture(lightcubemap);
1358 m.texcombinergb[0] = GL_MODULATE;
1359 m.texcombinergb[1] = GL_MODULATE;
1360 R_Mesh_TextureState(&m);
1361 qglColorMask(1,1,1,0);
1362 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1363 R_Mesh_GetSpace(numverts);
1364 R_Mesh_CopyVertex3f(vertex3f, numverts);
1365 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1367 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1368 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1369 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1371 color[0] = bound(0, color2[0], 1);
1372 color[1] = bound(0, color2[1], 1);
1373 color[2] = bound(0, color2[2], 1);
1374 GL_Color(color[0], color[1], color[2], 1);
1375 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1377 c_rt_lighttris += numtriangles;
1383 if (r_textureunits.integer >= 2)
1386 //R_Mesh_EndBatch();
1388 m.tex[0] = R_GetTexture(basetexture);
1389 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1390 R_Mesh_TextureState(&m);
1391 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
1392 qglEnable(GL_BLEND);
1394 m.tex[0] = R_GetTexture(basetexture);
1395 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1396 m.blendfunc1 = GL_SRC_ALPHA;
1397 m.blendfunc2 = GL_ONE;
1400 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1401 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1403 color[0] = bound(0, color2[0], 1);
1404 color[1] = bound(0, color2[1], 1);
1405 color[2] = bound(0, color2[2], 1);
1407 R_Mesh_GetSpace(numverts);
1408 R_Mesh_CopyVertex3f(vertex3f, numverts);
1409 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1410 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
1411 R_Shadow_VertexLightingWithXYAttenuationTexture(numverts, vertex3f, normal3f, color, matrix_modeltofilter);
1412 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1414 c_rt_lighttris += numtriangles;
1420 //R_Mesh_EndBatch();
1422 m.tex[0] = R_GetTexture(basetexture);
1423 R_Mesh_TextureState(&m);
1424 qglBlendFunc(GL_SRC_ALPHA, GL_ONE);
1425 qglEnable(GL_BLEND);
1427 m.tex[0] = R_GetTexture(basetexture);
1428 m.blendfunc1 = GL_SRC_ALPHA;
1429 m.blendfunc2 = GL_ONE;
1432 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
1433 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1435 color[0] = bound(0, color2[0], 1);
1436 color[1] = bound(0, color2[1], 1);
1437 color[2] = bound(0, color2[2], 1);
1439 R_Mesh_GetSpace(numverts);
1440 R_Mesh_CopyVertex3f(vertex3f, numverts);
1441 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1442 R_Shadow_VertexLighting(numverts, vertex3f, normal3f, color, matrix_modeltofilter);
1443 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1445 c_rt_lighttris += numtriangles;
1451 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)
1454 float color[3], color2[3];
1456 if (!gl_dot3arb || !gl_texturecubemap || !gl_combine.integer || !gl_stencil)
1458 memset(&m, 0, sizeof(m));
1460 bumptexture = r_shadow_blankbumptexture;
1462 glosstexture = r_shadow_blankglosstexture;
1463 if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
1465 if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1467 // 2/0/0/1/2 3D combine blendsquare path
1468 //R_Mesh_EndBatch();
1469 m.tex[0] = R_GetTexture(bumptexture);
1470 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1471 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1472 R_Mesh_TextureState(&m);
1473 qglColorMask(0,0,0,1);
1474 // this squares the result
1475 qglEnable(GL_BLEND);
1476 qglBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1478 R_Mesh_GetSpace(numverts);
1479 R_Mesh_CopyVertex3f(vertex3f, numverts);
1480 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1481 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
1482 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1484 c_rt_lighttris += numtriangles;
1486 //R_Mesh_EndBatch();
1488 m.texcubemap[1] = 0;
1489 m.texcombinergb[1] = GL_MODULATE;
1490 R_Mesh_TextureState(&m);
1491 // square alpha in framebuffer a few times to make it shiny
1492 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1493 // these comments are a test run through this math for intensity 0.5
1494 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
1495 // 0.25 * 0.25 = 0.0625 (this is another pass)
1496 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
1497 R_Mesh_GetSpace(numverts);
1498 R_Mesh_CopyVertex3f(vertex3f, numverts);
1499 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1501 c_rt_lighttris += numtriangles;
1502 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1504 c_rt_lighttris += numtriangles;
1506 //R_Mesh_EndBatch();
1507 m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
1508 R_Mesh_TextureState(&m);
1509 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1510 R_Mesh_GetSpace(numverts);
1511 R_Mesh_CopyVertex3f(vertex3f, numverts);
1512 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1513 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1515 c_rt_lighttris += numtriangles;
1517 //R_Mesh_EndBatch();
1519 m.tex[0] = R_GetTexture(glosstexture);
1520 m.texcubemap[1] = R_GetTexture(lightcubemap);
1521 R_Mesh_TextureState(&m);
1522 qglColorMask(1,1,1,0);
1523 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1524 R_Mesh_GetSpace(numverts);
1525 R_Mesh_CopyVertex3f(vertex3f, numverts);
1526 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1528 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1529 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
1530 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1532 color[0] = bound(0, color2[0], 1);
1533 color[1] = bound(0, color2[1], 1);
1534 color[2] = bound(0, color2[2], 1);
1535 GL_Color(color[0], color[1], color[2], 1);
1536 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1538 c_rt_lighttris += numtriangles;
1541 else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1543 // 2/0/0/2 3D combine blendsquare path
1544 //R_Mesh_EndBatch();
1545 m.tex[0] = R_GetTexture(bumptexture);
1546 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1547 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1548 R_Mesh_TextureState(&m);
1549 qglColorMask(0,0,0,1);
1550 // this squares the result
1551 qglEnable(GL_BLEND);
1552 qglBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1554 R_Mesh_GetSpace(numverts);
1555 R_Mesh_CopyVertex3f(vertex3f, numverts);
1556 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1557 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
1558 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1560 c_rt_lighttris += numtriangles;
1562 //R_Mesh_EndBatch();
1564 m.texcubemap[1] = 0;
1565 m.texcombinergb[1] = GL_MODULATE;
1566 R_Mesh_TextureState(&m);
1567 // square alpha in framebuffer a few times to make it shiny
1568 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1569 // these comments are a test run through this math for intensity 0.5
1570 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
1571 // 0.25 * 0.25 = 0.0625 (this is another pass)
1572 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
1573 R_Mesh_GetSpace(numverts);
1574 R_Mesh_CopyVertex3f(vertex3f, numverts);
1575 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1577 c_rt_lighttris += numtriangles;
1578 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1580 c_rt_lighttris += numtriangles;
1582 //R_Mesh_EndBatch();
1583 R_Mesh_GetSpace(numverts);
1584 R_Mesh_CopyVertex3f(vertex3f, numverts);
1585 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1586 m.tex[0] = R_GetTexture(glosstexture);
1587 m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
1588 R_Mesh_TextureState(&m);
1589 qglColorMask(1,1,1,0);
1590 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1591 R_Mesh_GetSpace(numverts);
1592 R_Mesh_CopyVertex3f(vertex3f, numverts);
1593 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1594 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
1595 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
1596 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1598 color[0] = bound(0, color2[0], 1);
1599 color[1] = bound(0, color2[1], 1);
1600 color[2] = bound(0, color2[2], 1);
1601 GL_Color(color[0], color[1], color[2], 1);
1602 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1604 c_rt_lighttris += numtriangles;
1607 else if (r_textureunits.integer >= 2 /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
1609 // 2/0/0/2/2 2D combine blendsquare path
1610 //R_Mesh_EndBatch();
1611 m.tex[0] = R_GetTexture(bumptexture);
1612 m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
1613 m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
1614 R_Mesh_TextureState(&m);
1615 qglColorMask(0,0,0,1);
1616 // this squares the result
1617 qglEnable(GL_BLEND);
1618 qglBlendFunc(GL_SRC_ALPHA, GL_ZERO);
1620 R_Mesh_GetSpace(numverts);
1621 R_Mesh_CopyVertex3f(vertex3f, numverts);
1622 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1623 R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
1624 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1626 c_rt_lighttris += numtriangles;
1628 //R_Mesh_EndBatch();
1630 m.texcubemap[1] = 0;
1631 m.texcombinergb[1] = GL_MODULATE;
1632 R_Mesh_TextureState(&m);
1633 // square alpha in framebuffer a few times to make it shiny
1634 qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
1635 // these comments are a test run through this math for intensity 0.5
1636 // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
1637 // 0.25 * 0.25 = 0.0625 (this is another pass)
1638 // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
1639 R_Mesh_GetSpace(numverts);
1640 R_Mesh_CopyVertex3f(vertex3f, numverts);
1641 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1643 c_rt_lighttris += numtriangles;
1644 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1646 c_rt_lighttris += numtriangles;
1648 //R_Mesh_EndBatch();
1649 m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
1650 m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
1651 R_Mesh_TextureState(&m);
1652 qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
1653 R_Mesh_GetSpace(numverts);
1654 R_Mesh_CopyVertex3f(vertex3f, numverts);
1655 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
1656 R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
1657 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1659 c_rt_lighttris += numtriangles;
1661 //R_Mesh_EndBatch();
1662 m.tex[0] = R_GetTexture(glosstexture);
1663 m.texcubemap[1] = R_GetTexture(lightcubemap);
1664 R_Mesh_TextureState(&m);
1665 qglColorMask(1,1,1,0);
1666 qglBlendFunc(GL_DST_ALPHA, GL_ONE);
1667 R_Mesh_GetSpace(numverts);
1668 R_Mesh_CopyVertex3f(vertex3f, numverts);
1669 R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
1671 R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
1672 VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
1673 for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
1675 color[0] = bound(0, color2[0], 1);
1676 color[1] = bound(0, color2[1], 1);
1677 color[2] = bound(0, color2[2], 1);
1678 GL_Color(color[0], color[1], color[2], 1);
1679 R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
1681 c_rt_lighttris += numtriangles;
1687 void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
1689 R_Mesh_Matrix(matrix);
1690 R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
1693 cvar_t r_editlights = {0, "r_editlights", "0"};
1694 cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
1695 cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
1696 cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
1697 cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
1698 cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
1699 cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
1700 cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
1701 worldlight_t *r_shadow_worldlightchain;
1702 worldlight_t *r_shadow_selectedlight;
1703 vec3_t r_editlights_cursorlocation;
1705 static int castshadowcount = 1;
1706 void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname, int castshadow)
1708 int i, j, k, l, maxverts = 256, *mark, tris;
1709 float *vertex3f = NULL;
1711 shadowmesh_t *mesh, *castmesh;
1715 surfmesh_t *surfmesh;
1717 if (radius < 15 || DotProduct(color, color) < 0.03)
1719 Con_Printf("R_Shadow_NewWorldLight: refusing to create a light too small/dim\n");
1723 e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
1724 VectorCopy(origin, e->origin);
1725 VectorCopy(color, e->light);
1726 e->lightradius = radius;
1728 if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
1730 Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
1733 e->castshadows = castshadow;
1735 e->cullradius = e->lightradius;
1736 for (k = 0;k < 3;k++)
1738 e->mins[k] = e->origin[k] - e->lightradius;
1739 e->maxs[k] = e->origin[k] + e->lightradius;
1742 e->next = r_shadow_worldlightchain;
1743 r_shadow_worldlightchain = e;
1744 if (cubemapname && cubemapname[0])
1746 e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
1747 strcpy(e->cubemapname, cubemapname);
1748 // FIXME: add cubemap loading (and don't load a cubemap twice)
1753 i = Mod_PointContents(e->origin, cl.worldmodel);
1754 if (r_shadow_portallight.integer && i != CONTENTS_SOLID && i != CONTENTS_SKY)
1757 qbyte *bytesurfacepvs;
1759 byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->numleafs + 1);
1760 bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->numsurfaces);
1762 Portal_Visibility(cl.worldmodel, e->origin, byteleafpvs, bytesurfacepvs, NULL, 0, true, RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin));
1764 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1765 if (byteleafpvs[i+1] && BoxesOverlap(leaf->mins, leaf->maxs, e->mins, e->maxs))
1766 leaf->worldnodeframe = castshadowcount;
1768 for (i = 0, surf = cl.worldmodel->surfaces;i < cl.worldmodel->numsurfaces;i++, surf++)
1769 if (bytesurfacepvs[i] && BoxesOverlap(surf->poly_mins, surf->poly_maxs, e->mins, e->maxs))
1770 surf->castshadow = castshadowcount;
1772 Mem_Free(byteleafpvs);
1773 Mem_Free(bytesurfacepvs);
1777 leaf = Mod_PointInLeaf(origin, cl.worldmodel);
1778 pvs = Mod_LeafPVS(leaf, cl.worldmodel);
1779 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1781 if (pvs[i >> 3] & (1 << (i & 7)) && BoxesOverlap(leaf->mins, leaf->maxs, e->mins, e->maxs))
1783 leaf->worldnodeframe = castshadowcount;
1784 for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
1786 surf = cl.worldmodel->surfaces + *mark;
1787 if (surf->castshadow != castshadowcount && BoxesOverlap(surf->poly_mins, surf->poly_maxs, e->mins, e->maxs))
1788 surf->castshadow = castshadowcount;
1795 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1796 if (leaf->worldnodeframe == castshadowcount)
1799 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1800 if (surf->castshadow == castshadowcount)
1804 e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
1806 e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
1808 for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
1809 if (leaf->worldnodeframe == castshadowcount)
1810 e->leafs[e->numleafs++] = leaf;
1812 for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
1813 if (surf->castshadow == castshadowcount)
1814 e->surfaces[e->numsurfaces++] = surf;
1816 // find bounding box of lit leafs
1817 VectorCopy(e->origin, e->mins);
1818 VectorCopy(e->origin, e->maxs);
1819 for (j = 0;j < e->numleafs;j++)
1822 for (k = 0;k < 3;k++)
1824 if (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
1825 if (e->maxs[k] < leaf->maxs[k]) e->maxs[k] = leaf->maxs[k];
1829 for (k = 0;k < 3;k++)
1831 if (e->mins[k] < e->origin[k] - e->lightradius) e->mins[k] = e->origin[k] - e->lightradius;
1832 if (e->maxs[k] > e->origin[k] + e->lightradius) e->maxs[k] = e->origin[k] + e->lightradius;
1834 e->cullradius = RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin);
1839 for (j = 0;j < e->numsurfaces;j++)
1841 surf = e->surfaces[j];
1842 if (surf->flags & SURF_SHADOWCAST)
1844 surf->castshadow = castshadowcount;
1845 if (maxverts < surf->poly_numverts)
1846 maxverts = surf->poly_numverts;
1849 e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1850 // make a mesh to cast a shadow volume from
1851 castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
1852 for (j = 0;j < e->numsurfaces;j++)
1853 if (e->surfaces[j]->castshadow == castshadowcount)
1854 for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
1855 Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->vertex3f, surfmesh->numtriangles, surfmesh->element3i);
1856 castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
1858 // cast shadow volume from castmesh
1859 for (mesh = castmesh;mesh;mesh = mesh->next)
1861 R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
1862 R_Shadow_ResizeShadowElements(castmesh->numtriangles);
1864 if (maxverts < castmesh->numverts * 2)
1866 maxverts = castmesh->numverts * 2;
1871 if (vertex3f == NULL && maxverts > 0)
1872 vertex3f = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[3]));
1874 // now that we have the buffers big enough, construct shadow volume mesh
1875 memcpy(vertex3f, castmesh->vertex3f, castmesh->numverts * sizeof(float[3]));
1876 tris = R_Shadow_MakeTriangleShadowFlags_Vertex3f(castmesh->element3i, vertex3f, castmesh->numtriangles, trianglefacinglight, trianglefacinglightlist, e->origin);
1877 tris = R_Shadow_BuildShadowVolume(castmesh->element3i, castmesh->neighbor3i, castmesh->numverts, trianglefacinglight, trianglefacinglightlist, tris, shadowelements, vertex3f, e->origin, r_shadow_projectdistance.value);
1878 // add the constructed shadow volume mesh
1879 Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, vertex3f, tris, shadowelements);
1884 // we're done with castmesh now
1885 Mod_ShadowMesh_Free(castmesh);
1886 e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
1887 for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
1888 l += mesh->numtriangles;
1889 Con_Printf("static shadow volume built containing %i triangles\n", l);
1892 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);
1895 void R_Shadow_FreeWorldLight(worldlight_t *light)
1897 worldlight_t **lightpointer;
1898 for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
1899 if (*lightpointer != light)
1900 Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
1901 *lightpointer = light->next;
1902 if (light->cubemapname)
1903 Mem_Free(light->cubemapname);
1904 if (light->shadowvolume)
1905 Mod_ShadowMesh_Free(light->shadowvolume);
1906 if (light->surfaces)
1907 Mem_Free(light->surfaces);
1909 Mem_Free(light->leafs);
1913 void R_Shadow_ClearWorldLights(void)
1915 while (r_shadow_worldlightchain)
1916 R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
1917 r_shadow_selectedlight = NULL;
1920 void R_Shadow_SelectLight(worldlight_t *light)
1922 if (r_shadow_selectedlight)
1923 r_shadow_selectedlight->selected = false;
1924 r_shadow_selectedlight = light;
1925 if (r_shadow_selectedlight)
1926 r_shadow_selectedlight->selected = true;
1930 void R_DrawLightSprite(int texnum, const vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
1937 VectorSubtract(origin, r_origin, diff);
1938 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
1941 memset(&m, 0, sizeof(m));
1942 m.blendfunc1 = GL_SRC_ALPHA;
1943 m.blendfunc2 = GL_ONE;
1945 R_Mesh_Matrix(&r_identitymatrix);
1948 GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
1949 R_DrawSpriteMesh(origin, vright, vup, scale, -scale, -scale, scale);
1952 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
1955 pic = Draw_CachePic("gfx/crosshair1.tga");
1957 R_DrawLightSprite(R_GetTexture(pic->tex), r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 0.5);
1960 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
1963 const worldlight_t *light;
1966 if (light->selected)
1967 intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
1968 if (light->shadowvolume)
1969 R_DrawLightSprite(calldata2, light->origin, 8, intensity, intensity, intensity, 0.5);
1971 R_DrawLightSprite(calldata2, light->origin, 8, intensity * 0.5, intensity * 0.5, intensity * 0.5, 0.5);
1974 void R_Shadow_DrawLightSprites(void)
1978 worldlight_t *light;
1980 for (i = 0;i < 5;i++)
1982 pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1));
1984 texnums[i] = R_GetTexture(pic->tex);
1989 for (light = r_shadow_worldlightchain;light;light = light->next)
1990 R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, texnums[((int) light) % 5]);
1991 R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
1994 void R_Shadow_SelectLightInView(void)
1996 float bestrating, rating, temp[3];
1997 worldlight_t *best, *light;
2000 for (light = r_shadow_worldlightchain;light;light = light->next)
2002 VectorSubtract(light->origin, r_refdef.vieworg, temp);
2003 rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
2006 rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
2007 if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
2009 bestrating = rating;
2014 R_Shadow_SelectLight(best);
2017 void R_Shadow_LoadWorldLights(void)
2019 int n, a, style, shadow;
2020 char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
2021 float origin[3], radius, color[3];
2022 if (cl.worldmodel == NULL)
2024 Con_Printf("No map loaded.\n");
2027 FS_StripExtension(cl.worldmodel->name, name);
2028 strcat(name, ".rtlights");
2029 lightsstring = FS_LoadFile(name, false);
2037 while (*s && *s != '\n')
2043 // check for modifier flags
2049 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);
2055 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);
2058 VectorScale(color, r_editlights_rtlightscolorscale.value, color);
2059 radius *= r_editlights_rtlightssizescale.value;
2060 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadow);
2065 Con_Printf("invalid rtlights file \"%s\"\n", name);
2066 Mem_Free(lightsstring);
2070 void R_Shadow_SaveWorldLights(void)
2072 worldlight_t *light;
2073 int bufchars, bufmaxchars;
2075 char name[MAX_QPATH];
2077 if (!r_shadow_worldlightchain)
2079 if (cl.worldmodel == NULL)
2081 Con_Printf("No map loaded.\n");
2084 FS_StripExtension(cl.worldmodel->name, name);
2085 strcat(name, ".rtlights");
2086 bufchars = bufmaxchars = 0;
2088 for (light = r_shadow_worldlightchain;light;light = light->next)
2090 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 : "");
2091 if (bufchars + strlen(line) > bufmaxchars)
2093 bufmaxchars = bufchars + strlen(line) + 2048;
2095 buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
2099 memcpy(buf, oldbuf, bufchars);
2105 memcpy(buf + bufchars, line, strlen(line));
2106 bufchars += strlen(line);
2110 FS_WriteFile(name, buf, bufchars);
2115 void R_Shadow_LoadLightsFile(void)
2118 char name[MAX_QPATH], *lightsstring, *s, *t;
2119 float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
2120 if (cl.worldmodel == NULL)
2122 Con_Printf("No map loaded.\n");
2125 FS_StripExtension(cl.worldmodel->name, name);
2126 strcat(name, ".lights");
2127 lightsstring = FS_LoadFile(name, false);
2135 while (*s && *s != '\n')
2140 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);
2144 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);
2147 radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
2148 radius = bound(15, radius, 4096);
2149 VectorScale(color, (2.0f / (8388608.0f)), color);
2150 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
2155 Con_Printf("invalid lights file \"%s\"\n", name);
2156 Mem_Free(lightsstring);
2160 void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
2162 int entnum, style, islight;
2163 char key[256], value[1024];
2164 float origin[3], radius, color[3], light, scale, originhack[3], overridecolor[3];
2167 if (cl.worldmodel == NULL)
2169 Con_Printf("No map loaded.\n");
2172 data = cl.worldmodel->entities;
2175 for (entnum = 0;COM_ParseToken(&data) && com_token[0] == '{';entnum++)
2178 origin[0] = origin[1] = origin[2] = 0;
2179 originhack[0] = originhack[1] = originhack[2] = 0;
2180 color[0] = color[1] = color[2] = 1;
2181 overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
2187 if (!COM_ParseToken(&data))
2189 if (com_token[0] == '}')
2190 break; // end of entity
2191 if (com_token[0] == '_')
2192 strcpy(key, com_token + 1);
2194 strcpy(key, com_token);
2195 while (key[strlen(key)-1] == ' ') // remove trailing spaces
2196 key[strlen(key)-1] = 0;
2197 if (!COM_ParseToken(&data))
2199 strcpy(value, com_token);
2201 // now that we have the key pair worked out...
2202 if (!strcmp("light", key))
2203 light = atof(value);
2204 else if (!strcmp("origin", key))
2205 sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
2206 else if (!strcmp("color", key))
2207 sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
2208 else if (!strcmp("wait", key))
2209 scale = atof(value);
2210 else if (!strcmp("classname", key))
2212 if (!strncmp(value, "light", 5))
2215 if (!strcmp(value, "light_fluoro"))
2220 overridecolor[0] = 1;
2221 overridecolor[1] = 1;
2222 overridecolor[2] = 1;
2224 if (!strcmp(value, "light_fluorospark"))
2229 overridecolor[0] = 1;
2230 overridecolor[1] = 1;
2231 overridecolor[2] = 1;
2233 if (!strcmp(value, "light_globe"))
2238 overridecolor[0] = 1;
2239 overridecolor[1] = 0.8;
2240 overridecolor[2] = 0.4;
2242 if (!strcmp(value, "light_flame_large_yellow"))
2247 overridecolor[0] = 1;
2248 overridecolor[1] = 0.5;
2249 overridecolor[2] = 0.1;
2251 if (!strcmp(value, "light_flame_small_yellow"))
2256 overridecolor[0] = 1;
2257 overridecolor[1] = 0.5;
2258 overridecolor[2] = 0.1;
2260 if (!strcmp(value, "light_torch_small_white"))
2265 overridecolor[0] = 1;
2266 overridecolor[1] = 0.5;
2267 overridecolor[2] = 0.1;
2269 if (!strcmp(value, "light_torch_small_walltorch"))
2274 overridecolor[0] = 1;
2275 overridecolor[1] = 0.5;
2276 overridecolor[2] = 0.1;
2280 else if (!strcmp("style", key))
2281 style = atoi(value);
2283 if (light <= 0 && islight)
2285 radius = min(light * r_editlights_quakelightsizescale.value / scale, 1048576);
2286 light = sqrt(bound(0, light, 1048576)) * (1.0f / 16.0f);
2287 if (color[0] == 1 && color[1] == 1 && color[2] == 1)
2288 VectorCopy(overridecolor, color);
2289 VectorScale(color, light, color);
2290 VectorAdd(origin, originhack, origin);
2292 R_Shadow_NewWorldLight(origin, radius, color, style, NULL, true);
2297 void R_Shadow_SetCursorLocationForView(void)
2299 vec_t dist, push, frac;
2300 vec3_t dest, endpos, normal;
2301 VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
2302 frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
2305 dist = frac * r_editlights_cursordistance.value;
2306 push = r_editlights_cursorpushback.value;
2310 VectorMA(endpos, push, vpn, endpos);
2311 VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
2313 r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2314 r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2315 r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
2318 void R_Shadow_UpdateWorldLightSelection(void)
2320 R_Shadow_SetCursorLocationForView();
2321 if (r_editlights.integer)
2323 R_Shadow_SelectLightInView();
2324 R_Shadow_DrawLightSprites();
2327 R_Shadow_SelectLight(NULL);
2330 void R_Shadow_EditLights_Clear_f(void)
2332 R_Shadow_ClearWorldLights();
2335 void R_Shadow_EditLights_Reload_f(void)
2337 r_shadow_reloadlights = true;
2340 void R_Shadow_EditLights_Save_f(void)
2343 R_Shadow_SaveWorldLights();
2346 void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
2348 R_Shadow_ClearWorldLights();
2349 R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
2352 void R_Shadow_EditLights_ImportLightsFile_f(void)
2354 R_Shadow_ClearWorldLights();
2355 R_Shadow_LoadLightsFile();
2358 void R_Shadow_EditLights_Spawn_f(void)
2361 if (!r_editlights.integer)
2363 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
2366 if (Cmd_Argc() != 1)
2368 Con_Printf("r_editlights_spawn does not take parameters\n");
2371 color[0] = color[1] = color[2] = 1;
2372 R_Shadow_NewWorldLight(r_editlights_cursorlocation, 200, color, 0, NULL, true);
2375 void R_Shadow_EditLights_Edit_f(void)
2377 vec3_t origin, color;
2380 char cubemapname[1024];
2381 if (!r_editlights.integer)
2383 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
2386 if (!r_shadow_selectedlight)
2388 Con_Printf("No selected light.\n");
2391 VectorCopy(r_shadow_selectedlight->origin, origin);
2392 radius = r_shadow_selectedlight->lightradius;
2393 VectorCopy(r_shadow_selectedlight->light, color);
2394 style = r_shadow_selectedlight->style;
2395 if (r_shadow_selectedlight->cubemapname)
2396 strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
2399 shadows = r_shadow_selectedlight->castshadows;
2400 if (!strcmp(Cmd_Argv(1), "origin"))
2402 if (Cmd_Argc() != 5)
2404 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
2407 origin[0] = atof(Cmd_Argv(2));
2408 origin[1] = atof(Cmd_Argv(3));
2409 origin[2] = atof(Cmd_Argv(4));
2411 else if (!strcmp(Cmd_Argv(1), "originx"))
2413 if (Cmd_Argc() != 3)
2415 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2418 origin[0] = atof(Cmd_Argv(2));
2420 else if (!strcmp(Cmd_Argv(1), "originy"))
2422 if (Cmd_Argc() != 3)
2424 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2427 origin[1] = atof(Cmd_Argv(2));
2429 else if (!strcmp(Cmd_Argv(1), "originz"))
2431 if (Cmd_Argc() != 3)
2433 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2436 origin[2] = atof(Cmd_Argv(2));
2438 else if (!strcmp(Cmd_Argv(1), "move"))
2440 if (Cmd_Argc() != 5)
2442 Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(0));
2445 origin[0] += atof(Cmd_Argv(2));
2446 origin[1] += atof(Cmd_Argv(3));
2447 origin[2] += atof(Cmd_Argv(4));
2449 else if (!strcmp(Cmd_Argv(1), "movex"))
2451 if (Cmd_Argc() != 3)
2453 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2456 origin[0] += atof(Cmd_Argv(2));
2458 else if (!strcmp(Cmd_Argv(1), "movey"))
2460 if (Cmd_Argc() != 3)
2462 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2465 origin[1] += atof(Cmd_Argv(2));
2467 else if (!strcmp(Cmd_Argv(1), "movez"))
2469 if (Cmd_Argc() != 3)
2471 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2474 origin[2] += atof(Cmd_Argv(2));
2476 else if (!strcmp(Cmd_Argv(1), "color"))
2478 if (Cmd_Argc() != 5)
2480 Con_Printf("usage: r_editlights_edit %s red green blue\n", Cmd_Argv(0));
2483 color[0] = atof(Cmd_Argv(2));
2484 color[1] = atof(Cmd_Argv(3));
2485 color[2] = atof(Cmd_Argv(4));
2487 else if (!strcmp(Cmd_Argv(1), "radius"))
2489 if (Cmd_Argc() != 3)
2491 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2494 radius = atof(Cmd_Argv(2));
2496 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "style"))
2498 if (Cmd_Argc() != 3)
2500 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2503 style = atoi(Cmd_Argv(2));
2505 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "cubemap"))
2509 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2512 if (Cmd_Argc() == 3)
2513 strcpy(cubemapname, Cmd_Argv(2));
2517 else if (Cmd_Argc() == 3 && !strcmp(Cmd_Argv(1), "shadows"))
2519 if (Cmd_Argc() != 3)
2521 Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(0));
2524 shadows = Cmd_Argv(2)[0] == 'y' || Cmd_Argv(2)[0] == 'Y' || Cmd_Argv(2)[0] == 't' || atoi(Cmd_Argv(2));
2528 Con_Printf("usage: r_editlights_edit [property] [value]\n");
2529 Con_Printf("Selected light's properties:\n");
2530 Con_Printf("Origin: %f %f %f\n", r_shadow_selectedlight->origin[0], r_shadow_selectedlight->origin[1], r_shadow_selectedlight->origin[2]);
2531 Con_Printf("Radius: %f\n", r_shadow_selectedlight->lightradius);
2532 Con_Printf("Color: %f %f %f\n", r_shadow_selectedlight->light[0], r_shadow_selectedlight->light[1], r_shadow_selectedlight->light[2]);
2533 Con_Printf("Style: %i\n", r_shadow_selectedlight->style);
2534 Con_Printf("Cubemap: %s\n", r_shadow_selectedlight->cubemapname);
2535 Con_Printf("Shadows: %s\n", r_shadow_selectedlight->castshadows ? "yes" : "no");
2538 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2539 r_shadow_selectedlight = NULL;
2540 R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname, shadows);
2543 extern int con_vislines;
2544 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
2548 if (r_shadow_selectedlight == NULL)
2552 sprintf(temp, "Light properties");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2553 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;
2554 sprintf(temp, "Radius %f", r_shadow_selectedlight->lightradius);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2555 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;
2556 sprintf(temp, "Style %i", r_shadow_selectedlight->style);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2557 sprintf(temp, "Cubemap %s", r_shadow_selectedlight->cubemapname);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0);y += 8;
2558 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;
2561 void R_Shadow_EditLights_ToggleShadow_f(void)
2563 if (!r_editlights.integer)
2565 Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
2568 if (!r_shadow_selectedlight)
2570 Con_Printf("No selected light.\n");
2573 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);
2574 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2575 r_shadow_selectedlight = NULL;
2578 void R_Shadow_EditLights_Remove_f(void)
2580 if (!r_editlights.integer)
2582 Con_Printf("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
2585 if (!r_shadow_selectedlight)
2587 Con_Printf("No selected light.\n");
2590 R_Shadow_FreeWorldLight(r_shadow_selectedlight);
2591 r_shadow_selectedlight = NULL;
2594 void R_Shadow_EditLights_Init(void)
2596 Cvar_RegisterVariable(&r_editlights);
2597 Cvar_RegisterVariable(&r_editlights_cursordistance);
2598 Cvar_RegisterVariable(&r_editlights_cursorpushback);
2599 Cvar_RegisterVariable(&r_editlights_cursorpushoff);
2600 Cvar_RegisterVariable(&r_editlights_cursorgrid);
2601 Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
2602 Cvar_RegisterVariable(&r_editlights_rtlightssizescale);
2603 Cvar_RegisterVariable(&r_editlights_rtlightscolorscale);
2604 Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
2605 Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
2606 Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
2607 Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
2608 Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
2609 Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
2610 Cmd_AddCommand("r_editlights_toggleshadow", R_Shadow_EditLights_ToggleShadow_f);
2611 Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
2612 Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);