#include "portals.h"
#include "image.h"
+#define R_SHADOW_SHADOWMAP_NUMCUBEMAPS 8
+
extern void R_Shadow_EditLights_Init(void);
typedef enum r_shadow_rendermode_e
R_SHADOW_RENDERMODE_LIGHT_GLSL,
R_SHADOW_RENDERMODE_VISIBLEVOLUMES,
R_SHADOW_RENDERMODE_VISIBLELIGHTING,
+ R_SHADOW_RENDERMODE_SHADOWMAP2D,
+ R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE,
+ R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE,
}
r_shadow_rendermode_t;
+typedef enum r_shadow_shadowmode_e
+{
+ R_SHADOW_SHADOWMODE_STENCIL,
+ R_SHADOW_SHADOWMODE_SHADOWMAP2D,
+ R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE,
+ R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE
+}
+r_shadow_shadowmode_t;
+
r_shadow_rendermode_t r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
r_shadow_rendermode_t r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_NONE;
r_shadow_rendermode_t r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_NONE;
r_shadow_rendermode_t r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_NONE;
+qboolean r_shadow_usingshadowmaprect;
+qboolean r_shadow_usingshadowmap2d;
+qboolean r_shadow_usingshadowmapcube;
+int r_shadow_shadowmapside;
+float r_shadow_shadowmap_texturescale[2];
+float r_shadow_shadowmap_parameters[4];
+#if 0
+int r_shadow_drawbuffer;
+int r_shadow_readbuffer;
+#endif
+int r_shadow_cullface_front, r_shadow_cullface_back;
+GLuint r_shadow_fborectangle;
+GLuint r_shadow_fbocubeside[R_SHADOW_SHADOWMAP_NUMCUBEMAPS];
+GLuint r_shadow_fbo2d;
+r_shadow_shadowmode_t r_shadow_shadowmode;
+int r_shadow_shadowmapfilterquality;
+int r_shadow_shadowmaptexturetype;
+int r_shadow_shadowmapdepthbits;
+int r_shadow_shadowmapmaxsize;
+qboolean r_shadow_shadowmapvsdct;
+qboolean r_shadow_shadowmapsampler;
+int r_shadow_shadowmappcf;
+int r_shadow_shadowmapborder;
+int r_shadow_lightscissor[4];
int maxshadowtriangles;
int *shadowelements;
int *shadowmarklist;
int shadowmarkcount;
+int maxshadowsides;
+int numshadowsides;
+unsigned char *shadowsides;
+int *shadowsideslist;
+
int maxvertexupdate;
int *vertexupdate;
int *vertexremap;
int r_shadow_buffer_numsurfacepvsbytes;
unsigned char *r_shadow_buffer_surfacepvs;
int *r_shadow_buffer_surfacelist;
+unsigned char *r_shadow_buffer_surfacesides;
int r_shadow_buffer_numshadowtrispvsbytes;
unsigned char *r_shadow_buffer_shadowtrispvs;
rtexture_t *r_shadow_attenuation2dtexture;
rtexture_t *r_shadow_attenuation3dtexture;
rtexture_t *r_shadow_lightcorona;
+rtexture_t *r_shadow_shadowmaprectangletexture;
+rtexture_t *r_shadow_shadowmap2dtexture;
+rtexture_t *r_shadow_shadowmapcubetexture[R_SHADOW_SHADOWMAP_NUMCUBEMAPS];
+rtexture_t *r_shadow_shadowmapvsdcttexture;
+int r_shadow_shadowmapsize; // changes for each light based on distance
+int r_shadow_shadowmaplod; // changes for each light based on distance
// lights are reloaded when this changes
char r_shadow_mapname[MAX_QPATH];
cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0", "generate fake bumpmaps from diffuse textures at this bumpyness, try 4 to match tenebrae, higher values increase depth, requires r_restart to take effect"};
cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4", "what magnitude to interpret _bump.tga textures as, higher values increase depth, requires r_restart to take effect"};
cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1", "renders only one light, for level design purposes or debugging"};
+cvar_t r_shadow_dot3 = {CVAR_SAVE, "r_shadow_dot3", "0", "enables use of (slow) per pixel lighting on GL1.3 hardware"};
cvar_t r_shadow_usenormalmap = {CVAR_SAVE, "r_shadow_usenormalmap", "1", "enables use of directional shading on lights"};
cvar_t r_shadow_gloss = {CVAR_SAVE, "r_shadow_gloss", "1", "0 disables gloss (specularity) rendering, 1 uses gloss if textures are found, 2 forces a flat metallic specular effect on everything without textures (similar to tenebrae)"};
cvar_t r_shadow_gloss2intensity = {0, "r_shadow_gloss2intensity", "0.125", "how bright the forced flat gloss should look if r_shadow_gloss is 2"};
cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1", "how bright textured glossmaps should look if r_shadow_gloss is 1 or 2"};
cvar_t r_shadow_glossexponent = {0, "r_shadow_glossexponent", "32", "how 'sharp' the gloss should appear (specular power)"};
+cvar_t r_shadow_gloss2exponent = {0, "r_shadow_gloss2exponent", "32", "same as r_shadow_glossexponent but for forced gloss (gloss 2) surfaces"};
cvar_t r_shadow_glossexact = {0, "r_shadow_glossexact", "0", "use exact reflection math for gloss (slightly slower, but should look a tad better)"};
cvar_t r_shadow_lightattenuationdividebias = {0, "r_shadow_lightattenuationdividebias", "1", "changes attenuation texture generation"};
cvar_t r_shadow_lightattenuationlinearscale = {0, "r_shadow_lightattenuationlinearscale", "2", "changes attenuation texture generation"};
cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation"};
cvar_t r_shadow_realtime_world_compileportalculling = {0, "r_shadow_realtime_world_compileportalculling", "1", "enables portal-based culling optimization during compilation"};
cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1", "use scissor optimization of light rendering (restricts rendering to the portion of the screen affected by the light)"};
+cvar_t r_shadow_shadowmapping = {CVAR_SAVE, "r_shadow_shadowmapping", "0", "enables use of shadowmapping (depth texture sampling) instead of stencil shadow volumes, requires gl_fbo 1"};
+cvar_t r_shadow_shadowmapping_texturetype = {CVAR_SAVE, "r_shadow_shadowmapping_texturetype", "-1", "shadowmap texture types: -1 = auto-select, 0 = 2D, 1 = rectangle, 2 = cubemap"};
+cvar_t r_shadow_shadowmapping_filterquality = {CVAR_SAVE, "r_shadow_shadowmapping_filterquality", "-1", "shadowmap filter modes: -1 = auto-select, 0 = no filtering, 1 = bilinear, 2 = bilinear 2x2 blur (fast), 3 = 3x3 blur (moderate), 4 = 4x4 blur (slow)"};
+cvar_t r_shadow_shadowmapping_depthbits = {CVAR_SAVE, "r_shadow_shadowmapping_depthbits", "24", "requested minimum shadowmap texture depth bits"};
+cvar_t r_shadow_shadowmapping_vsdct = {CVAR_SAVE, "r_shadow_shadowmapping_vsdct", "1", "enables use of virtual shadow depth cube texture"};
+cvar_t r_shadow_shadowmapping_minsize = {CVAR_SAVE, "r_shadow_shadowmapping_minsize", "32", "shadowmap size limit"};
+cvar_t r_shadow_shadowmapping_maxsize = {CVAR_SAVE, "r_shadow_shadowmapping_maxsize", "512", "shadowmap size limit"};
+cvar_t r_shadow_shadowmapping_precision = {CVAR_SAVE, "r_shadow_shadowmapping_precision", "1", "makes shadowmaps have a maximum resolution of this number of pixels per light source radius unit such that, for example, at precision 0.5 a light with radius 200 will have a maximum resolution of 100 pixels"};
+//cvar_t r_shadow_shadowmapping_lod_bias = {CVAR_SAVE, "r_shadow_shadowmapping_lod_bias", "16", "shadowmap size bias"};
+//cvar_t r_shadow_shadowmapping_lod_scale = {CVAR_SAVE, "r_shadow_shadowmapping_lod_scale", "128", "shadowmap size scaling parameter"};
+cvar_t r_shadow_shadowmapping_bordersize = {CVAR_SAVE, "r_shadow_shadowmapping_bordersize", "4", "shadowmap size bias for filtering"};
+cvar_t r_shadow_shadowmapping_nearclip = {CVAR_SAVE, "r_shadow_shadowmapping_nearclip", "1", "shadowmap nearclip in world units"};
+cvar_t r_shadow_shadowmapping_bias = {CVAR_SAVE, "r_shadow_shadowmapping_bias", "0.03", "shadowmap bias parameter (this is multiplied by nearclip * 1024 / lodsize)"};
+cvar_t r_shadow_shadowmapping_polygonfactor = {CVAR_SAVE, "r_shadow_shadowmapping_polygonfactor", "2", "slope-dependent shadowmapping bias"};
+cvar_t r_shadow_shadowmapping_polygonoffset = {CVAR_SAVE, "r_shadow_shadowmapping_polygonoffset", "0", "constant shadowmapping bias"};
cvar_t r_shadow_culltriangles = {0, "r_shadow_culltriangles", "1", "performs more expensive tests to remove unnecessary triangles of lit surfaces"};
cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "1", "how much to push shadow volumes into the distance when rendering, to reduce chances of zfighting artifacts (should not be less than 0)"};
cachepic_t *r_editlights_sprcubemaplight;
cachepic_t *r_editlights_sprcubemapnoshadowlight;
cachepic_t *r_editlights_sprselection;
+extern cvar_t gl_max_size;
+
+void R_Shadow_SetShadowMode(void)
+{
+ r_shadow_shadowmapmaxsize = bound(1, r_shadow_shadowmapping_maxsize.integer, gl_max_size.integer / 4);
+ r_shadow_shadowmapvsdct = r_shadow_shadowmapping_vsdct.integer != 0;
+ r_shadow_shadowmapfilterquality = r_shadow_shadowmapping_filterquality.integer;
+ r_shadow_shadowmaptexturetype = r_shadow_shadowmapping_texturetype.integer;
+ r_shadow_shadowmapdepthbits = r_shadow_shadowmapping_depthbits.integer;
+ r_shadow_shadowmapborder = bound(0, r_shadow_shadowmapping_bordersize.integer, 16);
+ r_shadow_shadowmaplod = -1;
+ r_shadow_shadowmapsize = 0;
+ r_shadow_shadowmapsampler = false;
+ r_shadow_shadowmappcf = 0;
+ r_shadow_shadowmode = R_SHADOW_SHADOWMODE_STENCIL;
+ if(r_shadow_shadowmapping.integer && r_glsl.integer && gl_support_fragment_shader && gl_support_ext_framebuffer_object)
+ {
+ if(r_shadow_shadowmapfilterquality < 0)
+ {
+ if(strstr(gl_vendor, "NVIDIA"))
+ {
+ r_shadow_shadowmapsampler = gl_support_arb_shadow;
+ r_shadow_shadowmappcf = 1;
+ }
+ else if(gl_support_amd_texture_texture4 || gl_support_arb_texture_gather)
+ r_shadow_shadowmappcf = 1;
+ else if(strstr(gl_vendor, "ATI"))
+ r_shadow_shadowmappcf = 1;
+ else
+ r_shadow_shadowmapsampler = gl_support_arb_shadow;
+ }
+ else
+ {
+ switch (r_shadow_shadowmapfilterquality)
+ {
+ case 1:
+ r_shadow_shadowmapsampler = gl_support_arb_shadow;
+ break;
+ case 2:
+ r_shadow_shadowmapsampler = gl_support_arb_shadow;
+ r_shadow_shadowmappcf = 1;
+ break;
+ case 3:
+ r_shadow_shadowmappcf = 1;
+ break;
+ case 4:
+ r_shadow_shadowmappcf = 2;
+ break;
+ }
+ }
+ switch (r_shadow_shadowmaptexturetype)
+ {
+ case 0:
+ r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
+ break;
+ case 1:
+ r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE;
+ break;
+ case 2:
+ r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE;
+ break;
+ default:
+ if((gl_support_amd_texture_texture4 || gl_support_arb_texture_gather) && r_shadow_shadowmappcf && !r_shadow_shadowmapsampler)
+ r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
+ else if(gl_texturerectangle)
+ r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE;
+ else
+ r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
+ break;
+ }
+ }
+}
+
+void R_Shadow_FreeShadowMaps(void)
+{
+ int i;
+
+ R_Shadow_SetShadowMode();
+
+ if (r_shadow_fborectangle)
+ qglDeleteFramebuffersEXT(1, &r_shadow_fborectangle);
+ r_shadow_fborectangle = 0;
+ CHECKGLERROR
+
+ if (r_shadow_fbo2d)
+ qglDeleteFramebuffersEXT(1, &r_shadow_fbo2d);
+ r_shadow_fbo2d = 0;
+ CHECKGLERROR
+ for (i = 0;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
+ if (r_shadow_fbocubeside[i])
+ qglDeleteFramebuffersEXT(1, &r_shadow_fbocubeside[i]);
+ memset(r_shadow_fbocubeside, 0, sizeof(r_shadow_fbocubeside));
+ CHECKGLERROR
+
+ if (r_shadow_shadowmaprectangletexture)
+ R_FreeTexture(r_shadow_shadowmaprectangletexture);
+ r_shadow_shadowmaprectangletexture = NULL;
+
+ if (r_shadow_shadowmap2dtexture)
+ R_FreeTexture(r_shadow_shadowmap2dtexture);
+ r_shadow_shadowmap2dtexture = NULL;
+
+ for (i = 0;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
+ if (r_shadow_shadowmapcubetexture[i])
+ R_FreeTexture(r_shadow_shadowmapcubetexture[i]);
+ memset(r_shadow_shadowmapcubetexture, 0, sizeof(r_shadow_shadowmapcubetexture));
+
+ if (r_shadow_shadowmapvsdcttexture)
+ R_FreeTexture(r_shadow_shadowmapvsdcttexture);
+ r_shadow_shadowmapvsdcttexture = NULL;
+
+ CHECKGLERROR
+}
void r_shadow_start(void)
{
r_shadow_attenuationgradienttexture = NULL;
r_shadow_attenuation2dtexture = NULL;
r_shadow_attenuation3dtexture = NULL;
+ r_shadow_shadowmode = R_SHADOW_SHADOWMODE_STENCIL;
+ r_shadow_shadowmaprectangletexture = NULL;
+ r_shadow_shadowmap2dtexture = NULL;
+ memset(r_shadow_shadowmapcubetexture, 0, sizeof(r_shadow_shadowmapcubetexture));
+ r_shadow_shadowmapvsdcttexture = NULL;
+ r_shadow_shadowmapmaxsize = 0;
+ r_shadow_shadowmapsize = 0;
+ r_shadow_shadowmaplod = 0;
+ r_shadow_shadowmapfilterquality = -1;
+ r_shadow_shadowmaptexturetype = -1;
+ r_shadow_shadowmapdepthbits = 0;
+ r_shadow_shadowmapvsdct = false;
+ r_shadow_shadowmapsampler = false;
+ r_shadow_shadowmappcf = 0;
+ r_shadow_fborectangle = 0;
+ r_shadow_fbo2d = 0;
+ memset(r_shadow_fbocubeside, 0, sizeof(r_shadow_fbocubeside));
+
+ R_Shadow_FreeShadowMaps();
+
r_shadow_texturepool = NULL;
r_shadow_filters_texturepool = NULL;
R_Shadow_ValidateCvars();
shadowmark = NULL;
shadowmarklist = NULL;
shadowmarkcount = 0;
+ maxshadowsides = 0;
+ numshadowsides = 0;
+ shadowsides = NULL;
+ shadowsideslist = NULL;
r_shadow_buffer_numleafpvsbytes = 0;
r_shadow_buffer_visitingleafpvs = NULL;
r_shadow_buffer_leafpvs = NULL;
r_shadow_buffer_numsurfacepvsbytes = 0;
r_shadow_buffer_surfacepvs = NULL;
r_shadow_buffer_surfacelist = NULL;
+ r_shadow_buffer_surfacesides = NULL;
r_shadow_buffer_numshadowtrispvsbytes = 0;
r_shadow_buffer_shadowtrispvs = NULL;
r_shadow_buffer_numlighttrispvsbytes = 0;
void r_shadow_shutdown(void)
{
+ CHECKGLERROR
R_Shadow_UncompileWorldLights();
+
+ R_Shadow_FreeShadowMaps();
+
+ CHECKGLERROR
numcubemaps = 0;
r_shadow_attenuationgradienttexture = NULL;
r_shadow_attenuation2dtexture = NULL;
Mem_Free(shadowmarklist);
shadowmarklist = NULL;
shadowmarkcount = 0;
+ maxshadowsides = 0;
+ numshadowsides = 0;
+ if (shadowsides)
+ Mem_Free(shadowsides);
+ shadowsides = NULL;
+ if (shadowsideslist)
+ Mem_Free(shadowsideslist);
+ shadowsideslist = NULL;
r_shadow_buffer_numleafpvsbytes = 0;
if (r_shadow_buffer_visitingleafpvs)
Mem_Free(r_shadow_buffer_visitingleafpvs);
if (r_shadow_buffer_surfacelist)
Mem_Free(r_shadow_buffer_surfacelist);
r_shadow_buffer_surfacelist = NULL;
+ if (r_shadow_buffer_surfacesides)
+ Mem_Free(r_shadow_buffer_surfacesides);
+ r_shadow_buffer_surfacesides = NULL;
r_shadow_buffer_numshadowtrispvsbytes = 0;
if (r_shadow_buffer_shadowtrispvs)
Mem_Free(r_shadow_buffer_shadowtrispvs);
{
Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
+ Cvar_RegisterVariable(&r_shadow_dot3);
Cvar_RegisterVariable(&r_shadow_usenormalmap);
Cvar_RegisterVariable(&r_shadow_debuglight);
Cvar_RegisterVariable(&r_shadow_gloss);
Cvar_RegisterVariable(&r_shadow_gloss2intensity);
Cvar_RegisterVariable(&r_shadow_glossintensity);
Cvar_RegisterVariable(&r_shadow_glossexponent);
+ Cvar_RegisterVariable(&r_shadow_gloss2exponent);
Cvar_RegisterVariable(&r_shadow_glossexact);
Cvar_RegisterVariable(&r_shadow_lightattenuationdividebias);
Cvar_RegisterVariable(&r_shadow_lightattenuationlinearscale);
Cvar_RegisterVariable(&r_shadow_realtime_world_compilesvbsp);
Cvar_RegisterVariable(&r_shadow_realtime_world_compileportalculling);
Cvar_RegisterVariable(&r_shadow_scissor);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_vsdct);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_texturetype);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_filterquality);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_depthbits);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_precision);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_maxsize);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_minsize);
+// Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_bias);
+// Cvar_RegisterVariable(&r_shadow_shadowmapping_lod_scale);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_bordersize);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_nearclip);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_bias);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_polygonfactor);
+ Cvar_RegisterVariable(&r_shadow_shadowmapping_polygonoffset);
Cvar_RegisterVariable(&r_shadow_culltriangles);
Cvar_RegisterVariable(&r_shadow_polygonfactor);
Cvar_RegisterVariable(&r_shadow_polygonoffset);
shadowmark = NULL;
shadowmarklist = NULL;
shadowmarkcount = 0;
+ maxshadowsides = 0;
+ numshadowsides = 0;
+ shadowsides = NULL;
+ shadowsideslist = NULL;
r_shadow_buffer_numleafpvsbytes = 0;
r_shadow_buffer_visitingleafpvs = NULL;
r_shadow_buffer_leafpvs = NULL;
r_shadow_buffer_numsurfacepvsbytes = 0;
r_shadow_buffer_surfacepvs = NULL;
r_shadow_buffer_surfacelist = NULL;
+ r_shadow_buffer_surfacesides = NULL;
r_shadow_buffer_shadowtrispvs = NULL;
r_shadow_buffer_lighttrispvs = NULL;
R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
}
};
-void R_Shadow_ResizeShadowArrays(int numvertices, int numtriangles)
+void R_Shadow_ResizeShadowArrays(int numvertices, int numtriangles, int vertscale, int triscale)
{
+ numvertices = ((numvertices + 255) & ~255) * vertscale;
+ numtriangles = ((numtriangles + 255) & ~255) * triscale;
// make sure shadowelements is big enough for this volume
if (maxshadowtriangles < numtriangles)
{
maxshadowtriangles = numtriangles;
if (shadowelements)
Mem_Free(shadowelements);
- shadowelements = (int *)Mem_Alloc(r_main_mempool, maxshadowtriangles * sizeof(int[24]));
+ shadowelements = (int *)Mem_Alloc(r_main_mempool, maxshadowtriangles * sizeof(int[3]));
}
// make sure shadowvertex3f is big enough for this volume
if (maxshadowvertices < numvertices)
maxshadowvertices = numvertices;
if (shadowvertex3f)
Mem_Free(shadowvertex3f);
- shadowvertex3f = (float *)Mem_Alloc(r_main_mempool, maxshadowvertices * sizeof(float[6]));
+ shadowvertex3f = (float *)Mem_Alloc(r_main_mempool, maxshadowvertices * sizeof(float[3]));
}
}
Mem_Free(r_shadow_buffer_surfacepvs);
if (r_shadow_buffer_surfacelist)
Mem_Free(r_shadow_buffer_surfacelist);
+ if (r_shadow_buffer_surfacesides)
+ Mem_Free(r_shadow_buffer_surfacesides);
r_shadow_buffer_numsurfacepvsbytes = numsurfacepvsbytes;
r_shadow_buffer_surfacepvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes);
r_shadow_buffer_surfacelist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
+ r_shadow_buffer_surfacesides = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numsurfacepvsbytes * 8 * sizeof(*r_shadow_buffer_surfacelist));
}
if (r_shadow_buffer_numshadowtrispvsbytes < numshadowtrispvsbytes)
{
numshadowmark = 0;
}
+void R_Shadow_PrepareShadowSides(int numtris)
+{
+ if (maxshadowsides < numtris)
+ {
+ maxshadowsides = numtris;
+ if (shadowsides)
+ Mem_Free(shadowsides);
+ if (shadowsideslist)
+ Mem_Free(shadowsideslist);
+ shadowsides = (unsigned char *)Mem_Alloc(r_main_mempool, maxshadowsides * sizeof(*shadowsides));
+ shadowsideslist = (int *)Mem_Alloc(r_main_mempool, maxshadowsides * sizeof(*shadowsideslist));
+ }
+ numshadowsides = 0;
+}
+
static int R_Shadow_ConstructShadowVolume_ZFail(int innumvertices, int innumtris, const int *inelement3i, const int *inneighbor3i, const float *invertex3f, int *outnumvertices, int *outelement3i, float *outvertex3f, const float *projectorigin, const float *projectdirection, float projectdistance, int numshadowmarktris, const int *shadowmarktris)
{
int i, j;
if (!numverts || !nummarktris)
return;
// make sure shadowelements is big enough for this volume
- if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts)
- R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255);
+ if (maxshadowtriangles < nummarktris*8 || maxshadowvertices < numverts*2)
+ R_Shadow_ResizeShadowArrays(numverts, nummarktris, 2, 8);
if (maxvertexupdate < numverts)
{
}
}
+int R_Shadow_CalcTriangleSideMask(const vec3_t p1, const vec3_t p2, const vec3_t p3, float bias)
+{
+ // p1, p2, p3 are in the cubemap's local coordinate system
+ // bias = border/(size - border)
+ int mask = 0x3F;
+
+ float dp1 = p1[0] + p1[1], dn1 = p1[0] - p1[1], ap1 = fabs(dp1), an1 = fabs(dn1),
+ dp2 = p2[0] + p2[1], dn2 = p2[0] - p2[1], ap2 = fabs(dp2), an2 = fabs(dn2),
+ dp3 = p3[0] + p3[1], dn3 = p3[0] - p3[1], ap3 = fabs(dp3), an3 = fabs(dn3);
+ if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
+ mask &= (3<<4)
+ | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
+ | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
+ | (dp3 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
+ if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
+ mask &= (3<<4)
+ | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
+ | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
+ | (dn3 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
+
+ dp1 = p1[1] + p1[2], dn1 = p1[1] - p1[2], ap1 = fabs(dp1), an1 = fabs(dn1),
+ dp2 = p2[1] + p2[2], dn2 = p2[1] - p2[2], ap2 = fabs(dp2), an2 = fabs(dn2),
+ dp3 = p3[1] + p3[2], dn3 = p3[1] - p3[2], ap3 = fabs(dp3), an3 = fabs(dn3);
+ if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
+ mask &= (3<<0)
+ | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
+ | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
+ | (dp3 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
+ if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
+ mask &= (3<<0)
+ | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
+ | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
+ | (dn3 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
+
+ dp1 = p1[2] + p1[0], dn1 = p1[2] - p1[0], ap1 = fabs(dp1), an1 = fabs(dn1),
+ dp2 = p2[2] + p2[0], dn2 = p2[2] - p2[0], ap2 = fabs(dp2), an2 = fabs(dn2),
+ dp3 = p3[2] + p3[0], dn3 = p3[2] - p3[0], ap3 = fabs(dp3), an3 = fabs(dn3);
+ if(ap1 > bias*an1 && ap2 > bias*an2 && ap3 > bias*an3)
+ mask &= (3<<2)
+ | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
+ | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
+ | (dp3 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
+ if(an1 > bias*ap1 && an2 > bias*ap2 && an3 > bias*ap3)
+ mask &= (3<<2)
+ | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
+ | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
+ | (dn3 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
+
+ return mask;
+}
+
+int R_Shadow_CalcBBoxSideMask(const vec3_t mins, const vec3_t maxs, const matrix4x4_t *worldtolight, const matrix4x4_t *radiustolight, float bias)
+{
+ vec3_t center, radius, lightcenter, lightradius, pmin, pmax;
+ float dp1, dn1, ap1, an1, dp2, dn2, ap2, an2;
+ int mask = 0x3F;
+
+ VectorSubtract(maxs, mins, radius);
+ VectorScale(radius, 0.5f, radius);
+ VectorAdd(mins, radius, center);
+ Matrix4x4_Transform(worldtolight, center, lightcenter);
+ Matrix4x4_Transform3x3(radiustolight, radius, lightradius);
+ VectorSubtract(lightcenter, lightradius, pmin);
+ VectorAdd(lightcenter, lightradius, pmax);
+
+ dp1 = pmax[0] + pmax[1], dn1 = pmax[0] - pmin[1], ap1 = fabs(dp1), an1 = fabs(dn1),
+ dp2 = pmin[0] + pmin[1], dn2 = pmin[0] - pmax[1], ap2 = fabs(dp2), an2 = fabs(dn2);
+ if(ap1 > bias*an1 && ap2 > bias*an2)
+ mask &= (3<<4)
+ | (dp1 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2))
+ | (dp2 >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
+ if(an1 > bias*ap1 && an2 > bias*ap2)
+ mask &= (3<<4)
+ | (dn1 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2))
+ | (dn2 >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
+
+ dp1 = pmax[1] + pmax[2], dn1 = pmax[1] - pmin[2], ap1 = fabs(dp1), an1 = fabs(dn1),
+ dp2 = pmin[1] + pmin[2], dn2 = pmin[1] - pmax[2], ap2 = fabs(dp2), an2 = fabs(dn2);
+ if(ap1 > bias*an1 && ap2 > bias*an2)
+ mask &= (3<<0)
+ | (dp1 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4))
+ | (dp2 >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
+ if(an1 > bias*ap1 && an2 > bias*ap2)
+ mask &= (3<<0)
+ | (dn1 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4))
+ | (dn2 >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
+
+ dp1 = pmax[2] + pmax[0], dn1 = pmax[2] - pmin[0], ap1 = fabs(dp1), an1 = fabs(dn1),
+ dp2 = pmin[2] + pmin[0], dn2 = pmin[2] - pmax[0], ap2 = fabs(dp2), an2 = fabs(dn2);
+ if(ap1 > bias*an1 && ap2 > bias*an2)
+ mask &= (3<<2)
+ | (dp1 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0))
+ | (dp2 >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
+ if(an1 > bias*ap1 && an2 > bias*ap2)
+ mask &= (3<<2)
+ | (dn1 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0))
+ | (dn2 >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
+
+ return mask;
+}
+
+#define R_Shadow_CalcEntitySideMask(ent, worldtolight, radiustolight, bias) R_Shadow_CalcBBoxSideMask((ent)->mins, (ent)->maxs, worldtolight, radiustolight, bias)
+
+int R_Shadow_CalcSphereSideMask(const vec3_t p, float radius, float bias)
+{
+ // p is in the cubemap's local coordinate system
+ // bias = border/(size - border)
+ float dxyp = p[0] + p[1], dxyn = p[0] - p[1], axyp = fabs(dxyp), axyn = fabs(dxyn);
+ float dyzp = p[1] + p[2], dyzn = p[1] - p[2], ayzp = fabs(dyzp), ayzn = fabs(dyzn);
+ float dzxp = p[2] + p[0], dzxn = p[2] - p[0], azxp = fabs(dzxp), azxn = fabs(dzxn);
+ int mask = 0x3F;
+ if(axyp > bias*axyn + radius) mask &= dxyp < 0 ? ~((1<<0)|(1<<2)) : ~((2<<0)|(2<<2));
+ if(axyn > bias*axyp + radius) mask &= dxyn < 0 ? ~((1<<0)|(2<<2)) : ~((2<<0)|(1<<2));
+ if(ayzp > bias*ayzn + radius) mask &= dyzp < 0 ? ~((1<<2)|(1<<4)) : ~((2<<2)|(2<<4));
+ if(ayzn > bias*ayzp + radius) mask &= dyzn < 0 ? ~((1<<2)|(2<<4)) : ~((2<<2)|(1<<4));
+ if(azxp > bias*azxn + radius) mask &= dzxp < 0 ? ~((1<<4)|(1<<0)) : ~((2<<4)|(2<<0));
+ if(azxn > bias*azxp + radius) mask &= dzxn < 0 ? ~((1<<4)|(2<<0)) : ~((2<<4)|(1<<0));
+ return mask;
+}
+
+int R_Shadow_CullFrustumSides(rtlight_t *rtlight, float size, float border)
+{
+ int i;
+ vec3_t p, n;
+ int sides = 0x3F, masks[6] = { 3<<4, 3<<4, 3<<0, 3<<0, 3<<2, 3<<2 };
+ float scale = (size - 2*border)/size, len;
+ float bias = border / (float)(size - border), dp, dn, ap, an;
+ // check if cone enclosing side would cross frustum plane
+ scale = 2 / (scale*scale + 2);
+ for (i = 0;i < 5;i++)
+ {
+ if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) > -0.03125)
+ continue;
+ Matrix4x4_Transform3x3(&rtlight->matrix_worldtolight, r_refdef.view.frustum[i].normal, n);
+ len = scale*VectorLength2(n);
+ if(n[0]*n[0] > len) sides &= n[0] < 0 ? ~(1<<0) : ~(2 << 0);
+ if(n[1]*n[1] > len) sides &= n[1] < 0 ? ~(1<<2) : ~(2 << 2);
+ if(n[2]*n[2] > len) sides &= n[2] < 0 ? ~(1<<4) : ~(2 << 4);
+ }
+ if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[4]) >= r_refdef.farclip - r_refdef.nearclip + 0.03125)
+ {
+ Matrix4x4_Transform3x3(&rtlight->matrix_worldtolight, r_refdef.view.frustum[4].normal, n);
+ len = scale*VectorLength(n);
+ if(n[0]*n[0] > len) sides &= n[0] >= 0 ? ~(1<<0) : ~(2 << 0);
+ if(n[1]*n[1] > len) sides &= n[1] >= 0 ? ~(1<<2) : ~(2 << 2);
+ if(n[2]*n[2] > len) sides &= n[2] >= 0 ? ~(1<<4) : ~(2 << 4);
+ }
+ // this next test usually clips off more sides than the former, but occasionally clips fewer/different ones, so do both and combine results
+ // check if frustum corners/origin cross plane sides
+ for (i = 0;i < 5;i++)
+ {
+ Matrix4x4_Transform(&rtlight->matrix_worldtolight, !i ? r_refdef.view.origin : r_refdef.view.frustumcorner[i-1], p);
+ dp = p[0] + p[1], dn = p[0] - p[1], ap = fabs(dp), an = fabs(dn),
+ masks[0] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<0)|(1<<2) : (2<<0)|(2<<2));
+ masks[1] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<0)|(2<<2) : (2<<0)|(1<<2));
+ dp = p[1] + p[2], dn = p[1] - p[2], ap = fabs(dp), an = fabs(dn),
+ masks[2] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<2)|(1<<4) : (2<<2)|(2<<4));
+ masks[3] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<2)|(2<<4) : (2<<2)|(1<<4));
+ dp = p[2] + p[0], dn = p[2] - p[0], ap = fabs(dp), an = fabs(dn),
+ masks[4] |= ap <= bias*an ? 0x3F : (dp >= 0 ? (1<<4)|(1<<0) : (2<<4)|(2<<0));
+ masks[5] |= an <= bias*ap ? 0x3F : (dn >= 0 ? (1<<4)|(2<<0) : (2<<4)|(1<<0));
+ }
+ return sides & masks[0] & masks[1] & masks[2] & masks[3] & masks[4] & masks[5];
+}
+
+int R_Shadow_ChooseSidesFromBox(int firsttriangle, int numtris, const float *invertex3f, const int *elements, const matrix4x4_t *worldtolight, const vec3_t projectorigin, const vec3_t projectdirection, const vec3_t lightmins, const vec3_t lightmaxs, const vec3_t surfacemins, const vec3_t surfacemaxs, int *totals)
+{
+ int t, tend;
+ const int *e;
+ const float *v[3];
+ float normal[3];
+ vec3_t p[3];
+ float bias;
+ int mask, surfacemask = 0;
+ if (!BoxesOverlap(lightmins, lightmaxs, surfacemins, surfacemaxs))
+ return 0;
+ bias = r_shadow_shadowmapborder / (float)(r_shadow_shadowmapmaxsize - r_shadow_shadowmapborder);
+ tend = firsttriangle + numtris;
+ if (BoxInsideBox(surfacemins, surfacemaxs, lightmins, lightmaxs))
+ {
+ // surface box entirely inside light box, no box cull
+ if (projectdirection)
+ {
+ for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
+ {
+ v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
+ TriangleNormal(v[0], v[1], v[2], normal);
+ if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0))
+ {
+ Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
+ mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
+ surfacemask |= mask;
+ if(totals)
+ {
+ totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
+ shadowsides[numshadowsides] = mask;
+ shadowsideslist[numshadowsides++] = t;
+ }
+ }
+ }
+ }
+ else
+ {
+ for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
+ {
+ v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
+ if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2]))
+ {
+ Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
+ mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
+ surfacemask |= mask;
+ if(totals)
+ {
+ totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
+ shadowsides[numshadowsides] = mask;
+ shadowsideslist[numshadowsides++] = t;
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ // surface box not entirely inside light box, cull each triangle
+ if (projectdirection)
+ {
+ for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
+ {
+ v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
+ TriangleNormal(v[0], v[1], v[2], normal);
+ if (r_shadow_frontsidecasting.integer == (DotProduct(normal, projectdirection) < 0)
+ && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
+ {
+ Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
+ mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
+ surfacemask |= mask;
+ if(totals)
+ {
+ totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
+ shadowsides[numshadowsides] = mask;
+ shadowsideslist[numshadowsides++] = t;
+ }
+ }
+ }
+ }
+ else
+ {
+ for (t = firsttriangle, e = elements + t * 3;t < tend;t++, e += 3)
+ {
+ v[0] = invertex3f + e[0] * 3, v[1] = invertex3f + e[1] * 3, v[2] = invertex3f + e[2] * 3;
+ if (r_shadow_frontsidecasting.integer == PointInfrontOfTriangle(projectorigin, v[0], v[1], v[2])
+ && TriangleOverlapsBox(v[0], v[1], v[2], lightmins, lightmaxs))
+ {
+ Matrix4x4_Transform(worldtolight, v[0], p[0]), Matrix4x4_Transform(worldtolight, v[1], p[1]), Matrix4x4_Transform(worldtolight, v[2], p[2]);
+ mask = R_Shadow_CalcTriangleSideMask(p[0], p[1], p[2], bias);
+ surfacemask |= mask;
+ if(totals)
+ {
+ totals[0] += mask&1, totals[1] += (mask>>1)&1, totals[2] += (mask>>2)&1, totals[3] += (mask>>3)&1, totals[4] += (mask>>4)&1, totals[5] += mask>>5;
+ shadowsides[numshadowsides] = mask;
+ shadowsideslist[numshadowsides++] = t;
+ }
+ }
+ }
+ }
+ }
+ return surfacemask;
+}
+
+void R_Shadow_ShadowMapFromList(int numverts, int numtris, const float *vertex3f, const int *elements, int numsidetris, const int *sidetotals, const unsigned char *sides, const int *sidetris)
+{
+ int i, j, outtriangles = 0;
+ int *outelement3i[6];
+ if (!numverts || !numsidetris || !r_shadow_compilingrtlight)
+ return;
+ outtriangles = sidetotals[0] + sidetotals[1] + sidetotals[2] + sidetotals[3] + sidetotals[4] + sidetotals[5];
+ // make sure shadowelements is big enough for this mesh
+ if (maxshadowtriangles < outtriangles)
+ R_Shadow_ResizeShadowArrays(0, outtriangles, 0, 1);
+
+ // compute the offset and size of the separate index lists for each cubemap side
+ outtriangles = 0;
+ for (i = 0;i < 6;i++)
+ {
+ outelement3i[i] = shadowelements + outtriangles * 3;
+ r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap->sideoffsets[i] = outtriangles;
+ r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap->sidetotals[i] = sidetotals[i];
+ outtriangles += sidetotals[i];
+ }
+
+ // gather up the (sparse) triangles into separate index lists for each cubemap side
+ for (i = 0;i < numsidetris;i++)
+ {
+ const int *element = elements + sidetris[i] * 3;
+ for (j = 0;j < 6;j++)
+ {
+ if (sides[i] & (1 << j))
+ {
+ outelement3i[j][0] = element[0];
+ outelement3i[j][1] = element[1];
+ outelement3i[j][2] = element[2];
+ outelement3i[j] += 3;
+ }
+ }
+ }
+
+ Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap, NULL, NULL, NULL, vertex3f, NULL, NULL, NULL, NULL, outtriangles, shadowelements);
+}
+
static void R_Shadow_MakeTextures_MakeCorona(void)
{
float dx, dy;
int x, y, z;
float intensity, dist;
unsigned int *data;
+ R_Shadow_FreeShadowMaps();
R_FreeTexturePool(&r_shadow_texturepool);
r_shadow_texturepool = R_AllocTexturePool();
r_shadow_attenlinearscale = r_shadow_lightattenuationlinearscale.value;
void R_Shadow_RenderMode_Begin(void)
{
+#if 0
+ GLint drawbuffer;
+ GLint readbuffer;
+#endif
R_Shadow_ValidateCvars();
if (!r_shadow_attenuation2dtexture
GL_DepthTest(true);
GL_DepthMask(false);
GL_Color(0, 0, 0, 1);
- GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
+ GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
if (r_glsl.integer && gl_support_fragment_shader)
r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL;
- else if (gl_dot3arb && gl_texturecubemap && r_textureunits.integer >= 2 && gl_combine.integer && gl_stencil)
+ else if (gl_dot3arb && gl_texturecubemap && r_shadow_dot3.integer && gl_stencil)
r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_DOT3;
else
r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
+
+ CHECKGLERROR
+#if 0
+ qglGetIntegerv(GL_DRAW_BUFFER, &drawbuffer);CHECKGLERROR
+ qglGetIntegerv(GL_READ_BUFFER, &readbuffer);CHECKGLERROR
+ r_shadow_drawbuffer = drawbuffer;
+ r_shadow_readbuffer = readbuffer;
+#endif
+ r_shadow_cullface_front = r_refdef.view.cullface_front;
+ r_shadow_cullface_back = r_refdef.view.cullface_back;
}
void R_Shadow_RenderMode_ActiveLight(const rtlight_t *rtlight)
{
qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
}
+ if (gl_support_ext_framebuffer_object)
+ {
+ qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);CHECKGLERROR
+ }
+#if 0
+ qglDrawBuffer(r_shadow_drawbuffer);CHECKGLERROR
+ qglReadBuffer(r_shadow_readbuffer);CHECKGLERROR
+#endif
+ R_SetViewport(&r_refdef.view.viewport);
+ GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
R_Mesh_ColorPointer(NULL, 0, 0);
R_Mesh_ResetTextureState();
GL_DepthRange(0, 1);
qglStencilMask(~0);CHECKGLERROR
qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
+ r_refdef.view.cullface_front = r_shadow_cullface_front;
+ r_refdef.view.cullface_back = r_shadow_cullface_back;
GL_CullFace(r_refdef.view.cullface_back);
GL_Color(1, 1, 1, 1);
GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
GL_BlendFunc(GL_ONE, GL_ZERO);
R_SetupGenericShader(false);
+ r_shadow_usingshadowmaprect = false;
+ r_shadow_usingshadowmapcube = false;
+ r_shadow_usingshadowmap2d = false;
+ CHECKGLERROR
}
void R_Shadow_ClearStencil(void)
}
}
-void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent)
+static void R_Shadow_MakeVSDCT(void)
{
+ // maps to a 2x3 texture rectangle with normalized coordinates
+ // +-
+ // XX
+ // YY
+ // ZZ
+ // stores abs(dir.xy), offset.xy/2.5
+ unsigned char data[4*6] =
+ {
+ 255, 0, 0x33, 0x33, // +X: <1, 0>, <0.5, 0.5>
+ 255, 0, 0x99, 0x33, // -X: <1, 0>, <1.5, 0.5>
+ 0, 255, 0x33, 0x99, // +Y: <0, 1>, <0.5, 1.5>
+ 0, 255, 0x99, 0x99, // -Y: <0, 1>, <1.5, 1.5>
+ 0, 0, 0x33, 0xFF, // +Z: <0, 0>, <0.5, 2.5>
+ 0, 0, 0x99, 0xFF, // -Z: <0, 0>, <1.5, 2.5>
+ };
+ r_shadow_shadowmapvsdcttexture = R_LoadTextureCubeMap(r_shadow_texturepool, "shadowmapvsdct", 1, data, TEXTYPE_RGBA, TEXF_ALWAYSPRECACHE | TEXF_FORCENEAREST | TEXF_CLAMP | TEXF_ALPHA, NULL);
+}
+
+void R_Shadow_RenderMode_ShadowMap(int side, qboolean clear, int size)
+{
+ int status;
+ int maxsize;
+ float nearclip, farclip, bias;
+ r_viewport_t viewport;
+ GLuint fbo = 0;
+ CHECKGLERROR
+ maxsize = r_shadow_shadowmapmaxsize;
+ nearclip = r_shadow_shadowmapping_nearclip.value / rsurface.rtlight->radius;
+ farclip = 1.0f;
+ bias = r_shadow_shadowmapping_bias.value * nearclip * (1024.0f / size);// * rsurface.rtlight->radius;
+ r_shadow_shadowmap_parameters[2] = 0.5f + 0.5f * (farclip + nearclip) / (farclip - nearclip);
+ r_shadow_shadowmap_parameters[3] = -nearclip * farclip / (farclip - nearclip) - 0.5f * bias;
+ r_shadow_shadowmapside = side;
+ r_shadow_shadowmapsize = size;
+ if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAP2D)
+ {
+ r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder);
+ r_shadow_shadowmap_parameters[1] = r_shadow_shadowmapvsdct ? 2.5f*size : size;
+ R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL);
+ if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D) goto init_done;
+
+ // complex unrolled cube approach (more flexible)
+ if (r_shadow_shadowmapvsdct && !r_shadow_shadowmapvsdcttexture)
+ R_Shadow_MakeVSDCT();
+ if (!r_shadow_shadowmap2dtexture)
+ {
+#if 1
+ int w = maxsize*2, h = gl_support_arb_texture_non_power_of_two ? maxsize*3 : maxsize*4;
+ r_shadow_shadowmap2dtexture = R_LoadTextureShadowMap2D(r_shadow_texturepool, "shadowmap", w, h, r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
+ qglGenFramebuffersEXT(1, &r_shadow_fbo2d);CHECKGLERROR
+ qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbo2d);CHECKGLERROR
+ qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, R_GetTexture(r_shadow_shadowmap2dtexture), 0);CHECKGLERROR
+ // render depth into the fbo, do not render color at all
+ qglDrawBuffer(GL_NONE);CHECKGLERROR
+ qglReadBuffer(GL_NONE);CHECKGLERROR
+ status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
+ if (status != GL_FRAMEBUFFER_COMPLETE_EXT && r_shadow_shadowmapping.integer)
+ {
+ Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
+ Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
+ }
+#endif
+ }
+ CHECKGLERROR
+ if (r_shadow_shadowmap2dtexture) fbo = r_shadow_fbo2d;
+ r_shadow_shadowmap_texturescale[0] = 1.0f / R_TextureWidth(r_shadow_shadowmap2dtexture);
+ r_shadow_shadowmap_texturescale[1] = 1.0f / R_TextureHeight(r_shadow_shadowmap2dtexture);
+ r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAP2D;
+ }
+ else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE)
+ {
+ r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder);
+ r_shadow_shadowmap_parameters[1] = r_shadow_shadowmapvsdct ? 2.5f*size : size;
+ R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL);
+ if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE) goto init_done;
+
+ // complex unrolled cube approach (more flexible)
+ if (r_shadow_shadowmapvsdct && !r_shadow_shadowmapvsdcttexture)
+ R_Shadow_MakeVSDCT();
+ if (!r_shadow_shadowmaprectangletexture)
+ {
+#if 1
+ r_shadow_shadowmaprectangletexture = R_LoadTextureShadowMapRectangle(r_shadow_texturepool, "shadowmap", maxsize*2, maxsize*3, r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
+ qglGenFramebuffersEXT(1, &r_shadow_fborectangle);CHECKGLERROR
+ qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fborectangle);CHECKGLERROR
+ qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_RECTANGLE_ARB, R_GetTexture(r_shadow_shadowmaprectangletexture), 0);CHECKGLERROR
+ // render depth into the fbo, do not render color at all
+ qglDrawBuffer(GL_NONE);CHECKGLERROR
+ qglReadBuffer(GL_NONE);CHECKGLERROR
+ status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
+ if (status != GL_FRAMEBUFFER_COMPLETE_EXT && r_shadow_shadowmapping.integer)
+ {
+ Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
+ Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
+ }
+#endif
+ }
+ CHECKGLERROR
+ if(r_shadow_shadowmaprectangletexture) fbo = r_shadow_fborectangle;
+ r_shadow_shadowmap_texturescale[0] = 1.0f;
+ r_shadow_shadowmap_texturescale[1] = 1.0f;
+ r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE;
+ }
+ else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE)
+ {
+ r_shadow_shadowmap_parameters[0] = 1.0f;
+ r_shadow_shadowmap_parameters[1] = 1.0f;
+ R_Viewport_InitCubeSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, nearclip, farclip, NULL);
+ if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE) goto init_done;
+
+ // simple cube approach
+ if (!r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod])
+ {
+ #if 1
+ r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod] = R_LoadTextureShadowMapCube(r_shadow_texturepool, "shadowmapcube", size, r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
+ qglGenFramebuffersEXT(1, &r_shadow_fbocubeside[r_shadow_shadowmaplod]);CHECKGLERROR
+ qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbocubeside[r_shadow_shadowmaplod]);CHECKGLERROR
+ qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + side, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]), 0);CHECKGLERROR
+ // render depth into the fbo, do not render color at all
+ qglDrawBuffer(GL_NONE);CHECKGLERROR
+ qglReadBuffer(GL_NONE);CHECKGLERROR
+ status = qglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);CHECKGLERROR
+ if (status != GL_FRAMEBUFFER_COMPLETE_EXT && r_shadow_shadowmapping.integer)
+ {
+ Con_Printf("R_Shadow_RenderMode_ShadowMap: glCheckFramebufferStatusEXT returned %i\n", status);
+ Cvar_SetValueQuick(&r_shadow_shadowmapping, 0);
+ }
+ #endif
+ }
+ CHECKGLERROR
+ if (r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]) fbo = r_shadow_fbocubeside[r_shadow_shadowmaplod];
+ r_shadow_shadowmap_texturescale[0] = 0.0f;
+ r_shadow_shadowmap_texturescale[1] = 0.0f;
+ r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE;
+ }
+
+ R_Shadow_RenderMode_Reset();
+ if (fbo)
+ {
+ qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);CHECKGLERROR
+ R_SetupDepthOrShadowShader();
+ }
+ else
+ {
+ R_SetupShowDepthShader();
+ qglClearColor(1,1,1,1);CHECKGLERROR
+ }
+ CHECKGLERROR
+ GL_PolygonOffset(r_shadow_shadowmapping_polygonfactor.value, r_shadow_shadowmapping_polygonoffset.value);
+ GL_DepthMask(true);
+ GL_DepthTest(true);
+ qglClearDepth(1);
+ CHECKGLERROR
+
+init_done:
+ R_SetViewport(&viewport);
+ GL_Scissor(viewport.x, viewport.y, viewport.width, viewport.height);
+ if(r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE)
+ {
+ int flipped = (side&1)^(side>>2);
+ r_refdef.view.cullface_front = flipped ? r_shadow_cullface_back : r_shadow_cullface_front;
+ r_refdef.view.cullface_back = flipped ? r_shadow_cullface_front : r_shadow_cullface_back;
+ GL_CullFace(r_refdef.view.cullface_back);
+ }
+ else if(r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE)
+ {
+ qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + side, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]), 0);CHECKGLERROR
+ }
+ if (clear)
+ qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+ CHECKGLERROR
+}
+
+void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent, qboolean shadowmapping)
+{
+ if (transparent)
+ {
+ r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
+ r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
+ r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
+ r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
+ }
CHECKGLERROR
R_Shadow_RenderMode_Reset();
GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
{
R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap)); // light filter
GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
+ CHECKGLERROR
+ if (shadowmapping)
+ {
+ if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAP2D)
+ {
+ r_shadow_usingshadowmap2d = true;
+ R_Mesh_TexBind(GL20TU_SHADOWMAP2D, R_GetTexture(r_shadow_shadowmap2dtexture));
+ CHECKGLERROR
+ }
+ else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE)
+ {
+ r_shadow_usingshadowmaprect = true;
+ R_Mesh_TexBindRectangle(GL20TU_SHADOWMAPRECT, R_GetTexture(r_shadow_shadowmaprectangletexture));
+ CHECKGLERROR
+ }
+ else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE)
+ {
+ r_shadow_usingshadowmapcube = true;
+ R_Mesh_TexBindCubeMap(GL20TU_SHADOWMAPCUBE, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]));
+ CHECKGLERROR
+ }
+
+ if (r_shadow_shadowmapvsdct && (r_shadow_usingshadowmap2d || r_shadow_usingshadowmaprect))
+ {
+ R_Mesh_TexBindCubeMap(GL20TU_CUBEPROJECTION, R_GetTexture(r_shadow_shadowmapvsdcttexture));
+ CHECKGLERROR
+ }
+ }
}
else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_VERTEX)
R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+ CHECKGLERROR
}
void R_Shadow_RenderMode_VisibleShadowVolumes(void)
R_Shadow_RenderMode_Reset();
R_Shadow_RenderMode_ActiveLight(NULL);
GL_DepthMask(true);
- GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
+ GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
}
int sign[8];
float f;
+ r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
+ r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
+ r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
+ r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
+
if (!r_shadow_scissor.integer)
return false;
// if view is inside the light box, just say yes it's visible
if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
- {
- GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
return false;
- }
x1 = y1 = x2 = y2 = 0;
for (i = 0;i < numvertices;i++)
{
VectorCopy(vertex[i], v);
- GL_TransformToScreen(v, v2);
+ R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
//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]);
if (i)
{
// now convert the scissor rectangle to integer screen coordinates
ix1 = (int)(x1 - 1.0f);
- iy1 = (int)(y1 - 1.0f);
+ iy1 = vid.height - (int)(y2 - 1.0f);
ix2 = (int)(x2 + 1.0f);
- iy2 = (int)(y2 + 1.0f);
+ iy2 = vid.height - (int)(y1 + 1.0f);
//Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
// clamp it to the screen
- if (ix1 < r_refdef.view.x) ix1 = r_refdef.view.x;
- if (iy1 < r_refdef.view.y) iy1 = r_refdef.view.y;
- if (ix2 > r_refdef.view.x + r_refdef.view.width) ix2 = r_refdef.view.x + r_refdef.view.width;
- if (iy2 > r_refdef.view.y + r_refdef.view.height) iy2 = r_refdef.view.y + r_refdef.view.height;
+ if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
+ if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
+ if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
+ if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
// if it is inside out, it's not visible
if (ix2 <= ix1 || iy2 <= iy1)
return true;
// the light area is visible, set up the scissor rectangle
- GL_Scissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
- //qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);CHECKGLERROR
- //qglEnable(GL_SCISSOR_TEST);CHECKGLERROR
+ r_shadow_lightscissor[0] = ix1;
+ r_shadow_lightscissor[1] = iy1;
+ r_shadow_lightscissor[2] = ix2 - ix1;
+ r_shadow_lightscissor[3] = iy2 - iy1;
+
r_refdef.stats.lights_scissored++;
return false;
}
m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
if (r_textureunits.integer >= 3)
{
- // Voodoo4 or Kyro (or Geforce3/Radeon with gl_combine off)
+ // Voodoo4 or Kyro (or Geforce3/Radeon with r_shadow_dot3 off)
m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
m.texmatrix[2] = rsurface.entitytoattenuationz;
m.pointer_texcoord3f[2] = rsurface.vertex3f;
}
}
-void R_RTLight_Update(rtlight_t *rtlight, int isstatic, matrix4x4_t *matrix, vec3_t color, int style, const char *cubemapname, qboolean shadow, vec_t corona, vec_t coronasizescale, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int flags)
+void R_RTLight_Update(rtlight_t *rtlight, int isstatic, matrix4x4_t *matrix, vec3_t color, int style, const char *cubemapname, int shadow, vec_t corona, vec_t coronasizescale, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int flags)
{
matrix4x4_t tempmatrix = *matrix;
Matrix4x4_Scale(&tempmatrix, r_shadow_lightradiusscale.value, 1);
// compile the light
rtlight->compiled = true;
+ rtlight->shadowmode = rtlight->shadow ? (int)r_shadow_shadowmode : -1;
rtlight->static_numleafs = 0;
rtlight->static_numleafpvsbytes = 0;
rtlight->static_leaflist = NULL;
rtlight->static_leafpvs = NULL;
rtlight->static_numsurfaces = 0;
rtlight->static_surfacelist = NULL;
+ rtlight->static_shadowmap_receivers = 0x3F;
+ rtlight->static_shadowmap_casters = 0x3F;
rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
if (model && model->GetLightInfo)
{
- // this variable must be set for the CompileShadowVolume code
+ // this variable must be set for the CompileShadowVolume/CompileShadowMap code
r_shadow_compilingrtlight = rtlight;
R_Shadow_EnlargeLeafSurfaceTrisBuffer(model->brush.num_leafs, model->num_surfaces, model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles, model->surfmesh.num_triangles);
model->GetLightInfo(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs);
memcpy(rtlight->static_shadowtrispvs, r_shadow_buffer_shadowtrispvs, rtlight->static_numshadowtrispvsbytes);
if (rtlight->static_numlighttrispvsbytes)
memcpy(rtlight->static_lighttrispvs, r_shadow_buffer_lighttrispvs, rtlight->static_numlighttrispvsbytes);
- if (model->CompileShadowVolume && rtlight->shadow)
- model->CompileShadowVolume(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
+ switch (rtlight->shadowmode)
+ {
+ case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
+ case R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE:
+ case R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE:
+ if (model->CompileShadowMap && rtlight->shadow)
+ model->CompileShadowMap(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
+ break;
+ default:
+ if (model->CompileShadowVolume && rtlight->shadow)
+ model->CompileShadowVolume(ent, rtlight->shadoworigin, NULL, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
+ break;
+ }
// now we're done compiling the rtlight
r_shadow_compilingrtlight = NULL;
}
if (rtlight->static_meshchain_shadow_zfail)
Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zfail);
rtlight->static_meshchain_shadow_zfail = NULL;
+ if (rtlight->static_meshchain_shadow_shadowmap)
+ Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_shadowmap);
+ rtlight->static_meshchain_shadow_shadowmap = NULL;
// these allocations are grouped
if (rtlight->static_surfacelist)
Mem_Free(rtlight->static_surfacelist);
#endif
}
-void R_Shadow_DrawWorldShadow(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
+void R_Shadow_DrawWorldShadow_ShadowMap(int numsurfaces, int *surfacelist, const unsigned char *trispvs, const unsigned char *surfacesides)
+{
+ shadowmesh_t *mesh;
+
+ RSurf_ActiveWorldEntity();
+
+ if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
+ {
+ CHECKGLERROR
+ GL_CullFace(GL_NONE);
+ mesh = rsurface.rtlight->static_meshchain_shadow_shadowmap;
+ for (;mesh;mesh = mesh->next)
+ {
+ if (!mesh->sidetotals[r_shadow_shadowmapside])
+ continue;
+ r_refdef.stats.lights_shadowtriangles += mesh->sidetotals[r_shadow_shadowmapside];
+ R_Mesh_VertexPointer(mesh->vertex3f, mesh->vbo, mesh->vbooffset_vertex3f);
+ R_Mesh_Draw(0, mesh->numverts, mesh->sideoffsets[r_shadow_shadowmapside], mesh->sidetotals[r_shadow_shadowmapside], mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s);
+ }
+ CHECKGLERROR
+ }
+ else if (r_refdef.scene.worldentity->model)
+ r_refdef.scene.worldmodel->DrawShadowMap(r_shadow_shadowmapside, r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, surfacesides, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs);
+
+ rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
+}
+
+void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
{
qboolean zpass;
shadowmesh_t *mesh;
msurface_t *surface;
RSurf_ActiveWorldEntity();
+
if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
{
CHECKGLERROR
relativeshadowmaxs[0] = relativeshadoworigin[0] + relativeshadowradius;
relativeshadowmaxs[1] = relativeshadoworigin[1] + relativeshadowradius;
relativeshadowmaxs[2] = relativeshadoworigin[2] + relativeshadowradius;
- ent->model->DrawShadowVolume(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
+ if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAPCUBESIDE || r_shadow_rendermode == R_SHADOW_RENDERMODE_SHADOWMAP2D)
+ {
+ ent->model->DrawShadowMap(r_shadow_shadowmapside, ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, NULL, relativeshadowmins, relativeshadowmaxs);
+ }
+ else
+ ent->model->DrawShadowVolume(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
}
float f;
int numleafs, numsurfaces;
int *leaflist, *surfacelist;
- unsigned char *leafpvs, *shadowtrispvs, *lighttrispvs;
+ unsigned char *leafpvs, *shadowtrispvs, *lighttrispvs, *surfacesides;
int numlightentities;
int numlightentities_noselfshadow;
int numshadowentities;
int numshadowentities_noselfshadow;
static entity_render_t *lightentities[MAX_EDICTS];
- static entity_render_t *lightentities_noselfshadow[MAX_EDICTS];
static entity_render_t *shadowentities[MAX_EDICTS];
- static entity_render_t *shadowentities_noselfshadow[MAX_EDICTS];
+ static unsigned char entitysides[MAX_EDICTS];
+ int lightentities_noselfshadow;
+ int shadowentities_noselfshadow;
+ vec3_t nearestpoint;
+ vec_t distance;
+ qboolean castshadows;
+ int lodlinear;
// skip lights that don't light because of ambientscale+diffusescale+specularscale being 0 (corona only lights)
// skip lights that are basically invisible (color 0 0 0)
// all at once at the start of a level, not when it stalls gameplay.
// (especially important to benchmarks)
// compile light
- if (rtlight->isstatic && !rtlight->compiled && r_shadow_realtime_world_compile.integer)
+ if (rtlight->isstatic && (!rtlight->compiled || (rtlight->shadow && rtlight->shadowmode != (int)r_shadow_shadowmode)) && r_shadow_realtime_world_compile.integer)
+ {
+ if (rtlight->compiled)
+ R_RTLight_Uncompile(rtlight);
R_RTLight_Compile(rtlight);
+ }
+
// load cubemap
rtlight->currentcubemap = rtlight->cubemapname[0] ? R_Shadow_Cubemap(rtlight->cubemapname) : r_texture_whitecube;
leafpvs = rtlight->static_leafpvs;
numsurfaces = rtlight->static_numsurfaces;
surfacelist = rtlight->static_surfacelist;
+ surfacesides = NULL;
shadowtrispvs = rtlight->static_shadowtrispvs;
lighttrispvs = rtlight->static_lighttrispvs;
}
leaflist = r_shadow_buffer_leaflist;
leafpvs = r_shadow_buffer_leafpvs;
surfacelist = r_shadow_buffer_surfacelist;
+ surfacesides = r_shadow_buffer_surfacesides;
shadowtrispvs = r_shadow_buffer_shadowtrispvs;
lighttrispvs = r_shadow_buffer_lighttrispvs;
// if the reduced leaf bounds are offscreen, skip it
leafpvs = NULL;
numsurfaces = 0;
surfacelist = NULL;
+ surfacesides = NULL;
shadowtrispvs = NULL;
lighttrispvs = NULL;
}
// make a list of lit entities and shadow casting entities
numlightentities = 0;
numlightentities_noselfshadow = 0;
+ lightentities_noselfshadow = sizeof(lightentities)/sizeof(lightentities[0]) - 1;
numshadowentities = 0;
numshadowentities_noselfshadow = 0;
+ shadowentities_noselfshadow = sizeof(shadowentities)/sizeof(shadowentities[0]) - 1;
+
// add dynamic entities that are lit by the light
if (r_drawentities.integer)
{
if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingLeafPVS && !r_refdef.scene.worldmodel->brush.BoxTouchingLeafPVS(r_refdef.scene.worldmodel, leafpvs, ent->mins, ent->maxs))
continue;
if (ent->flags & RENDER_NOSELFSHADOW)
- lightentities_noselfshadow[numlightentities_noselfshadow++] = ent;
+ lightentities[lightentities_noselfshadow - numlightentities_noselfshadow++] = ent;
else
lightentities[numlightentities++] = ent;
// since it is lit, it probably also casts a shadow...
// RENDER_NOSELFSHADOW entities such as the gun
// (very weird, but keeps the player shadow off the gun)
if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
- shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
+ shadowentities[shadowentities_noselfshadow - numshadowentities_noselfshadow++] = ent;
else
shadowentities[numshadowentities++] = ent;
}
if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
{
if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
- shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
+ shadowentities[shadowentities_noselfshadow - numshadowentities_noselfshadow++] = ent;
else
shadowentities[numshadowentities++] = ent;
}
// for performance analysis by level designers
R_Shadow_RenderMode_VisibleShadowVolumes();
if (numsurfaces)
- R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
+ R_Shadow_DrawWorldShadow_ShadowVolume(numsurfaces, surfacelist, shadowtrispvs);
for (i = 0;i < numshadowentities;i++)
R_Shadow_DrawEntityShadow(shadowentities[i]);
for (i = 0;i < numshadowentities_noselfshadow;i++)
- R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
+ R_Shadow_DrawEntityShadow(shadowentities[shadowentities_noselfshadow - i]);
+ }
+
+ if (r_showlighting.integer && r_refdef.view.showdebug && numsurfaces + numlightentities + numlightentities_noselfshadow)
+ {
+ // optionally draw the illuminated areas
+ // for performance analysis by level designers
+ R_Shadow_RenderMode_VisibleLighting(false, false);
+ if (numsurfaces)
+ R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
+ for (i = 0;i < numlightentities;i++)
+ R_Shadow_DrawEntityLight(lightentities[i]);
+ for (i = 0;i < numlightentities_noselfshadow;i++)
+ R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
}
- if (gl_stencil && numsurfaces + numshadowentities + numshadowentities_noselfshadow && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows))
+ castshadows = numsurfaces + numshadowentities + numshadowentities_noselfshadow > 0 && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows);
+
+ nearestpoint[0] = bound(rtlight->cullmins[0], r_refdef.view.origin[0], rtlight->cullmaxs[0]);
+ nearestpoint[1] = bound(rtlight->cullmins[1], r_refdef.view.origin[1], rtlight->cullmaxs[1]);
+ nearestpoint[2] = bound(rtlight->cullmins[2], r_refdef.view.origin[2], rtlight->cullmaxs[2]);
+ distance = VectorDistance(nearestpoint, r_refdef.view.origin);
+
+ lodlinear = (rtlight->radius * r_shadow_shadowmapping_precision.value) / sqrt(max(1.0f, distance/rtlight->radius));
+ //lodlinear = (int)(r_shadow_shadowmapping_lod_bias.value + r_shadow_shadowmapping_lod_scale.value * rtlight->radius / max(1.0f, distance));
+ lodlinear = bound(r_shadow_shadowmapping_minsize.integer, lodlinear, r_shadow_shadowmapmaxsize);
+
+ if (castshadows && (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAP2D || r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE || r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE))
+ {
+ float borderbias;
+ int side;
+ int size;
+ int castermask = 0;
+ int receivermask = 0;
+ matrix4x4_t radiustolight = rtlight->matrix_worldtolight;
+ Matrix4x4_Abs(&radiustolight);
+
+ r_shadow_shadowmaplod = 0;
+ for (i = 1;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
+ if ((r_shadow_shadowmapmaxsize >> i) > lodlinear)
+ r_shadow_shadowmaplod = i;
+
+ size = r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE ? r_shadow_shadowmapmaxsize >> r_shadow_shadowmaplod : lodlinear;
+ size = bound(1, size, r_shadow_shadowmapmaxsize);
+ borderbias = r_shadow_shadowmapborder / (float)(size - r_shadow_shadowmapborder);
+
+ if (numsurfaces)
+ {
+ if (rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
+ {
+ castermask = rtlight->static_shadowmap_casters;
+ receivermask = rtlight->static_shadowmap_receivers;
+ }
+ else
+ {
+ for(i = 0;i < numsurfaces;i++)
+ {
+ msurface_t *surface = r_refdef.scene.worldmodel->data_surfaces + surfacelist[i];
+ surfacesides[i] = R_Shadow_CalcBBoxSideMask(surface->mins, surface->maxs, &rtlight->matrix_worldtolight, &radiustolight, borderbias);
+ castermask |= surfacesides[i];
+ receivermask |= surfacesides[i];
+ }
+ }
+ }
+ if (receivermask < 0x3F)
+ {
+ for (i = 0;i < numlightentities;i++)
+ receivermask |= R_Shadow_CalcEntitySideMask(lightentities[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias);
+ if (receivermask < 0x3F)
+ for(i = 0; i < numlightentities_noselfshadow;i++)
+ receivermask |= R_Shadow_CalcEntitySideMask(lightentities[lightentities_noselfshadow - i], &rtlight->matrix_worldtolight, &radiustolight, borderbias);
+ }
+
+ receivermask &= R_Shadow_CullFrustumSides(rtlight, size, r_shadow_shadowmapborder);
+
+ if (receivermask)
+ {
+ for (i = 0;i < numshadowentities;i++)
+ castermask |= (entitysides[i] = R_Shadow_CalcEntitySideMask(shadowentities[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias));
+ for (i = 0;i < numshadowentities_noselfshadow;i++)
+ castermask |= (entitysides[shadowentities_noselfshadow - i] = R_Shadow_CalcEntitySideMask(shadowentities[shadowentities_noselfshadow - i], &rtlight->matrix_worldtolight, &radiustolight, borderbias));
+ }
+
+ //Con_Printf("distance %f lodlinear %i (lod %i) size %i\n", distance, lodlinear, r_shadow_shadowmaplod, size);
+
+ // render shadow casters into 6 sided depth texture
+ for (side = 0;side < 6;side++) if (receivermask & (1 << side))
+ {
+ R_Shadow_RenderMode_ShadowMap(side, true, size);
+ if (! (castermask & (1 << side))) continue;
+ if (numsurfaces)
+ R_Shadow_DrawWorldShadow_ShadowMap(numsurfaces, surfacelist, shadowtrispvs, surfacesides);
+ for (i = 0;i < numshadowentities;i++) if (entitysides[i] & (1 << side))
+ R_Shadow_DrawEntityShadow(shadowentities[i]);
+ }
+
+ if (numlightentities_noselfshadow)
+ {
+ // render lighting using the depth texture as shadowmap
+ // draw lighting in the unmasked areas
+ R_Shadow_RenderMode_Lighting(false, false, true);
+ for (i = 0;i < numlightentities_noselfshadow;i++)
+ R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
+ }
+
+ // render shadow casters into 6 sided depth texture
+ if (numshadowentities_noselfshadow)
+ {
+ for (side = 0;side < 6;side++) if ((receivermask & castermask) & (1 << side))
+ {
+ R_Shadow_RenderMode_ShadowMap(side, false, size);
+ for (i = 0;i < numshadowentities_noselfshadow;i++) if (entitysides[shadowentities_noselfshadow - i] & (1 << side))
+ R_Shadow_DrawEntityShadow(shadowentities[shadowentities_noselfshadow - i]);
+ }
+ }
+
+ // render lighting using the depth texture as shadowmap
+ // draw lighting in the unmasked areas
+ R_Shadow_RenderMode_Lighting(false, false, true);
+ // draw lighting in the unmasked areas
+ if (numsurfaces)
+ R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
+ for (i = 0;i < numlightentities;i++)
+ R_Shadow_DrawEntityLight(lightentities[i]);
+ }
+ else if (castshadows && gl_stencil)
{
// draw stencil shadow volumes to mask off pixels that are in shadow
// so that they won't receive lighting
+ GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
R_Shadow_ClearStencil();
if (numsurfaces)
- R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
+ R_Shadow_DrawWorldShadow_ShadowVolume(numsurfaces, surfacelist, shadowtrispvs);
for (i = 0;i < numshadowentities;i++)
R_Shadow_DrawEntityShadow(shadowentities[i]);
if (numlightentities_noselfshadow)
{
// draw lighting in the unmasked areas
- R_Shadow_RenderMode_Lighting(true, false);
+ R_Shadow_RenderMode_Lighting(true, false, false);
for (i = 0;i < numlightentities_noselfshadow;i++)
- R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
+ R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
// optionally draw the illuminated areas
// for performance analysis by level designers
{
R_Shadow_RenderMode_VisibleLighting(!r_showdisabledepthtest.integer, false);
for (i = 0;i < numlightentities_noselfshadow;i++)
- R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
+ R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
}
}
for (i = 0;i < numshadowentities_noselfshadow;i++)
- R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
+ R_Shadow_DrawEntityShadow(shadowentities[shadowentities_noselfshadow - i]);
if (numsurfaces + numlightentities)
{
// draw lighting in the unmasked areas
- R_Shadow_RenderMode_Lighting(true, false);
+ R_Shadow_RenderMode_Lighting(true, false, false);
if (numsurfaces)
R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
for (i = 0;i < numlightentities;i++)
R_Shadow_DrawEntityLight(lightentities[i]);
-
- // optionally draw the illuminated areas
- // for performance analysis by level designers
- if (r_showlighting.integer && r_refdef.view.showdebug)
- {
- R_Shadow_RenderMode_VisibleLighting(!r_showdisabledepthtest.integer, false);
- if (numsurfaces)
- R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
- for (i = 0;i < numlightentities;i++)
- R_Shadow_DrawEntityLight(lightentities[i]);
- }
}
}
else
if (numsurfaces + numlightentities)
{
// draw lighting in the unmasked areas
- R_Shadow_RenderMode_Lighting(false, false);
+ R_Shadow_RenderMode_Lighting(false, false, false);
if (numsurfaces)
R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
for (i = 0;i < numlightentities;i++)
R_Shadow_DrawEntityLight(lightentities[i]);
for (i = 0;i < numlightentities_noselfshadow;i++)
- R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
-
- // optionally draw the illuminated areas
- // for performance analysis by level designers
- if (r_showlighting.integer && r_refdef.view.showdebug)
- {
- R_Shadow_RenderMode_VisibleLighting(false, false);
- if (numsurfaces)
- R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
- for (i = 0;i < numlightentities;i++)
- R_Shadow_DrawEntityLight(lightentities[i]);
- for (i = 0;i < numlightentities_noselfshadow;i++)
- R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
- }
+ R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
}
}
}
dlight_t *light;
size_t range;
+ if (r_shadow_shadowmapmaxsize != bound(1, r_shadow_shadowmapping_maxsize.integer, gl_max_size.integer / 4) ||
+ (r_shadow_shadowmode != R_SHADOW_SHADOWMODE_STENCIL) != (r_shadow_shadowmapping.integer && r_glsl.integer && gl_support_fragment_shader && gl_support_ext_framebuffer_object) ||
+ r_shadow_shadowmapvsdct != (r_shadow_shadowmapping_vsdct.integer != 0) ||
+ r_shadow_shadowmaptexturetype != r_shadow_shadowmapping_texturetype.integer ||
+ r_shadow_shadowmapfilterquality != r_shadow_shadowmapping_filterquality.integer ||
+ r_shadow_shadowmapdepthbits != r_shadow_shadowmapping_depthbits.integer ||
+ r_shadow_shadowmapborder != bound(0, r_shadow_shadowmapping_bordersize.integer, 16))
+ R_Shadow_FreeShadowMaps();
+
if (r_editlights.integer)
R_Shadow_DrawLightSprites();
R_Shadow_RenderMode_End();
}
+extern const float r_screenvertex3f[12];
extern void R_SetupView(qboolean allowwaterclippingplane);
+extern void R_ResetViewRendering3D(void);
+extern void R_ResetViewRendering2D(void);
extern cvar_t r_shadows;
+extern cvar_t r_shadows_darken;
+extern cvar_t r_shadows_drawafterrtlighting;
+extern cvar_t r_shadows_castfrombmodels;
extern cvar_t r_shadows_throwdistance;
+extern cvar_t r_shadows_throwdirection;
void R_DrawModelShadows(void)
{
int i;
vec3_t relativelightorigin;
vec3_t relativelightdirection;
vec3_t relativeshadowmins, relativeshadowmaxs;
- vec3_t tmp;
- float vertex3f[12];
+ vec3_t tmp, shadowdir;
if (!r_drawentities.integer || !gl_stencil)
return;
CHECKGLERROR
- GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
-
- r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
+ R_ResetViewRendering3D();
+ //GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
+ //GL_Scissor(r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
+ R_Shadow_RenderMode_Begin();
+ R_Shadow_RenderMode_ActiveLight(NULL);
+ r_shadow_lightscissor[0] = r_refdef.view.x;
+ r_shadow_lightscissor[1] = vid.height - r_refdef.view.y - r_refdef.view.height;
+ r_shadow_lightscissor[2] = r_refdef.view.width;
+ r_shadow_lightscissor[3] = r_refdef.view.height;
+ R_Shadow_RenderMode_StencilShadowVolumes(false);
- if (gl_ext_separatestencil.integer)
- {
- r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL;
- r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL;
- }
- else if (gl_ext_stenciltwoside.integer)
+ // get shadow dir
+ if (r_shadows.integer == 2)
{
- r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE;
- r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE;
- }
- else
- {
- r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCIL;
- r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL;
+ Math_atov(r_shadows_throwdirection.string, shadowdir);
+ VectorNormalize(shadowdir);
}
R_Shadow_ClearStencil();
for (i = 0;i < r_refdef.scene.numentities;i++)
{
ent = r_refdef.scene.entities[i];
- // cast shadows from anything that is not a submodel of the map
- if (ent->model && ent->model->DrawShadowVolume != NULL && !ent->model->brush.submodel && (ent->flags & RENDER_SHADOW))
+
+ // cast shadows from anything of the map (submodels are optional)
+ if (ent->model && ent->model->DrawShadowVolume != NULL && (!ent->model->brush.submodel || r_shadows_castfrombmodels.integer) && (ent->flags & RENDER_SHADOW))
{
relativethrowdistance = r_shadows_throwdistance.value * Matrix4x4_ScaleFromMatrix(&ent->inversematrix);
VectorSet(relativeshadowmins, -relativethrowdistance, -relativethrowdistance, -relativethrowdistance);
VectorSet(relativeshadowmaxs, relativethrowdistance, relativethrowdistance, relativethrowdistance);
-
- if(r_shadows.integer == 2)
- {
- // 2: simpler mode, throw shadows always DOWN
- VectorSet(tmp, 0, 0, -1);
- Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
- }
+ if (r_shadows.integer == 2) // 2: simpler mode, throw shadows always in same direction
+ Matrix4x4_Transform3x3(&ent->inversematrix, shadowdir, relativelightdirection);
else
{
if(ent->entitynumber != 0)
}
// not really the right mode, but this will disable any silly stencil features
- R_Shadow_RenderMode_VisibleLighting(true, true);
-
- // vertex coordinates for a quad that covers the screen exactly
- vertex3f[0] = 0;vertex3f[1] = 0;vertex3f[2] = 0;
- vertex3f[3] = 1;vertex3f[4] = 0;vertex3f[5] = 0;
- vertex3f[6] = 1;vertex3f[7] = 1;vertex3f[8] = 0;
- vertex3f[9] = 0;vertex3f[10] = 1;vertex3f[11] = 0;
+ R_Shadow_RenderMode_End();
// set up ortho view for rendering this pass
- GL_SetupView_Mode_Ortho(0, 0, 1, 1, -10, 100);
- GL_Scissor(r_refdef.view.x, r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
- GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
- GL_ScissorTest(true);
- R_Mesh_Matrix(&identitymatrix);
- R_Mesh_ResetTextureState();
- R_Mesh_VertexPointer(vertex3f, 0, 0);
+ //GL_Scissor(r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
+ //GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
+ //GL_ScissorTest(true);
+ //R_Mesh_Matrix(&identitymatrix);
+ //R_Mesh_ResetTextureState();
+ R_ResetViewRendering2D();
+ R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
R_Mesh_ColorPointer(NULL, 0, 0);
+ R_SetupGenericShader(false);
- // set up a 50% darkening blend on shadowed areas
+ // set up a darkening blend on shadowed areas
GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- GL_DepthRange(0, 1);
- GL_DepthTest(false);
- GL_DepthMask(false);
- GL_PolygonOffset(0, 0);CHECKGLERROR
- GL_Color(0, 0, 0, 0.5);
- GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
- qglDepthFunc(GL_ALWAYS);CHECKGLERROR
+ //GL_DepthRange(0, 1);
+ //GL_DepthTest(false);
+ //GL_DepthMask(false);
+ //GL_PolygonOffset(0, 0);CHECKGLERROR
+ GL_Color(0, 0, 0, r_shadows_darken.value);
+ //GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
+ //qglDepthFunc(GL_ALWAYS);CHECKGLERROR
qglEnable(GL_STENCIL_TEST);CHECKGLERROR
qglStencilMask(~0);CHECKGLERROR
qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
// apply the blend to the shadowed areas
R_Mesh_Draw(0, 4, 0, 2, NULL, polygonelements, 0, 0);
- // restoring the perspective view is done by R_RenderScene
- //R_SetupView(true);
+ // restore the viewport
+ R_SetViewport(&r_refdef.view.viewport);
// restore other state to normal
- R_Shadow_RenderMode_End();
+ //R_Shadow_RenderMode_End();
}
void R_BeginCoronaQuery(rtlight_t *rtlight, float scale, qboolean usequery)
float zdist;
vec3_t centerorigin;
// if it's too close, skip it
- if (VectorLength(rtlight->color) < (1.0f / 256.0f))
+ if (VectorLength(rtlight->currentcolor) < (1.0f / 256.0f))
return;
zdist = (DotProduct(rtlight->shadoworigin, r_refdef.view.forward) - DotProduct(r_refdef.view.origin, r_refdef.view.forward));
if (zdist < 32)
else
{
// FIXME: these traces should scan all render entities instead of cl.world
- if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
+ if (CL_TraceLine(r_refdef.view.origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
return;
}
- VectorScale(rtlight->color, cscale, color);
+ VectorScale(rtlight->currentcolor, cscale, color);
if (VectorLength(color) > (1.0f / 256.0f))
R_DrawSprite(GL_ONE, GL_ONE, r_shadow_lightcorona, NULL, true, false, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, color[0], color[1], color[2], 1);
}
if (rating >= 0.95)
{
rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
- if (bestrating < rating && CL_Move(light->origin, vec3_origin, vec3_origin, r_refdef.view.origin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction == 1.0f)
+ if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.view.origin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction == 1.0f)
{
bestrating = rating;
best = light;
vec3_t dest, endpos;
trace_t trace;
VectorMA(r_refdef.view.origin, r_editlights_cursordistance.value, r_refdef.view.forward, dest);
- trace = CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, dest, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false);
+ trace = CL_TraceLine(r_refdef.view.origin, dest, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false);
if (trace.fraction < 1)
{
dist = trace.fraction * r_editlights_cursordistance.value;
light = r_refdef.scene.lights[i];
Matrix4x4_Transform(&light->matrix_worldtolight, p, v);
f = 1 - VectorLength2(v);
- if (f > 0 && CL_Move(p, vec3_origin, vec3_origin, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction == 1)
+ if (f > 0 && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction == 1)
VectorMA(ambientcolor, f, light->currentcolor, ambientcolor);
}
}