X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=r_shadow.c;h=f18154877700b8ccb0b9985f8c639502362a3c87;hb=509d0579232b21bce445380ea844e93b84839847;hp=5c2acfd700dc0f161a2394e4b19221367cf4e9e2;hpb=9cf7fb8ac0964e77ab862231b61a7b321f9f66bd;p=xonotic%2Fdarkplaces.git diff --git a/r_shadow.c b/r_shadow.c index 5c2acfd7..f1815487 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -145,9 +145,12 @@ extern void R_Shadow_EditLights_Init(void); typedef enum r_shadow_rendermode_e { R_SHADOW_RENDERMODE_NONE, - R_SHADOW_RENDERMODE_STENCIL, - R_SHADOW_RENDERMODE_SEPARATESTENCIL, - R_SHADOW_RENDERMODE_STENCILTWOSIDE, + R_SHADOW_RENDERMODE_ZPASS_STENCIL, + R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL, + R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE, + R_SHADOW_RENDERMODE_ZFAIL_STENCIL, + R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL, + R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE, R_SHADOW_RENDERMODE_LIGHT_VERTEX, R_SHADOW_RENDERMODE_LIGHT_DOT3, R_SHADOW_RENDERMODE_LIGHT_GLSL, @@ -158,7 +161,8 @@ r_shadow_rendermode_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 = 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; int maxshadowtriangles; int *shadowelements; @@ -178,6 +182,7 @@ int *vertexremap; int vertexupdatenum; int r_shadow_buffer_numleafpvsbytes; +unsigned char *r_shadow_buffer_visitingleafpvs; unsigned char *r_shadow_buffer_leafpvs; int *r_shadow_buffer_leaflist; @@ -210,6 +215,7 @@ cvar_t r_shadow_gloss = {CVAR_SAVE, "r_shadow_gloss", "1", "0 disables gloss (sp 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_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_lightintensityscale = {0, "r_shadow_lightintensityscale", "1", "renders all world lights brighter or darker"}; @@ -234,6 +240,8 @@ cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to 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)"}; cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect r_glsl lighting)"}; cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1", "brightness of corona flare effects around certain lights, 0 disables corona effects"}; +cvar_t r_coronas_occlusionsizescale = {CVAR_SAVE, "r_coronas_occlusionsizescale", "0.1", "size of light source for corona occlusion checksm the proportion of hidden pixels controls corona intensity"}; +cvar_t r_coronas_occlusionquery = {CVAR_SAVE, "r_coronas_occlusionquery", "1", "use GL_ARB_occlusion_query extension if supported (fades coronas according to visibility)"}; cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0", "render bright coronas for dynamic lights instead of actual lighting, fast but ugly"}; cvar_t gl_ext_separatestencil = {0, "gl_ext_separatestencil", "1", "make use of OpenGL 2.0 glStencilOpSeparate or GL_ATI_separate_stencil extension"}; cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"}; @@ -318,6 +326,7 @@ void r_shadow_start(void) shadowmarklist = NULL; shadowmarkcount = 0; r_shadow_buffer_numleafpvsbytes = 0; + r_shadow_buffer_visitingleafpvs = NULL; r_shadow_buffer_leafpvs = NULL; r_shadow_buffer_leaflist = NULL; r_shadow_buffer_numsurfacepvsbytes = 0; @@ -363,6 +372,9 @@ void r_shadow_shutdown(void) shadowmarklist = NULL; shadowmarkcount = 0; r_shadow_buffer_numleafpvsbytes = 0; + if (r_shadow_buffer_visitingleafpvs) + Mem_Free(r_shadow_buffer_visitingleafpvs); + r_shadow_buffer_visitingleafpvs = NULL; if (r_shadow_buffer_leafpvs) Mem_Free(r_shadow_buffer_leafpvs); r_shadow_buffer_leafpvs = NULL; @@ -435,6 +447,7 @@ void R_Shadow_Init(void) Cvar_RegisterVariable(&r_shadow_gloss2intensity); Cvar_RegisterVariable(&r_shadow_glossintensity); Cvar_RegisterVariable(&r_shadow_glossexponent); + Cvar_RegisterVariable(&r_shadow_glossexact); Cvar_RegisterVariable(&r_shadow_lightattenuationdividebias); Cvar_RegisterVariable(&r_shadow_lightattenuationlinearscale); Cvar_RegisterVariable(&r_shadow_lightintensityscale); @@ -459,6 +472,8 @@ void R_Shadow_Init(void) Cvar_RegisterVariable(&r_shadow_polygonoffset); Cvar_RegisterVariable(&r_shadow_texture3d); Cvar_RegisterVariable(&r_coronas); + Cvar_RegisterVariable(&r_coronas_occlusionsizescale); + Cvar_RegisterVariable(&r_coronas_occlusionquery); Cvar_RegisterVariable(&gl_flashblend); Cvar_RegisterVariable(&gl_ext_separatestencil); Cvar_RegisterVariable(&gl_ext_stenciltwoside); @@ -484,6 +499,7 @@ void R_Shadow_Init(void) shadowmarklist = NULL; shadowmarkcount = 0; r_shadow_buffer_numleafpvsbytes = 0; + r_shadow_buffer_visitingleafpvs = NULL; r_shadow_buffer_leafpvs = NULL; r_shadow_buffer_leaflist = NULL; r_shadow_buffer_numsurfacepvsbytes = 0; @@ -542,11 +558,14 @@ static void R_Shadow_EnlargeLeafSurfaceTrisBuffer(int numleafs, int numsurfaces, int numlighttrispvsbytes = (((numlighttriangles + 7) >> 3) + 255) & ~255; if (r_shadow_buffer_numleafpvsbytes < numleafpvsbytes) { + if (r_shadow_buffer_visitingleafpvs) + Mem_Free(r_shadow_buffer_visitingleafpvs); if (r_shadow_buffer_leafpvs) Mem_Free(r_shadow_buffer_leafpvs); if (r_shadow_buffer_leaflist) Mem_Free(r_shadow_buffer_leaflist); r_shadow_buffer_numleafpvsbytes = numleafpvsbytes; + r_shadow_buffer_visitingleafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes); r_shadow_buffer_leafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes); r_shadow_buffer_leaflist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes * 8 * sizeof(*r_shadow_buffer_leaflist)); } @@ -600,7 +619,7 @@ void R_Shadow_PrepareShadowMark(int numtris) numshadowmark = 0; } -int R_Shadow_ConstructShadowVolume(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) +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; int outtriangles = 0, outvertices = 0; @@ -613,28 +632,6 @@ int R_Shadow_ConstructShadowVolume(int innumvertices, int innumtris, const int * else VectorClear(projectvector); - if (maxvertexupdate < innumvertices) - { - maxvertexupdate = innumvertices; - if (vertexupdate) - Mem_Free(vertexupdate); - if (vertexremap) - Mem_Free(vertexremap); - vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int)); - vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int)); - vertexupdatenum = 0; - } - vertexupdatenum++; - if (vertexupdatenum == 0) - { - vertexupdatenum = 1; - memset(vertexupdate, 0, maxvertexupdate * sizeof(int)); - memset(vertexremap, 0, maxvertexupdate * sizeof(int)); - } - - for (i = 0;i < numshadowmarktris;i++) - shadowmark[shadowmarktris[i]] = shadowmarkcount; - // create the vertices if (projectdirection) { @@ -819,6 +816,116 @@ int R_Shadow_ConstructShadowVolume(int innumvertices, int innumtris, const int * return outtriangles; } +static int R_Shadow_ConstructShadowVolume_ZPass(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, k; + int outtriangles = 0, outvertices = 0; + const int *element; + const float *vertex; + float ratio, direction[3], projectvector[3]; + qboolean side[4]; + + if (projectdirection) + VectorScale(projectdirection, projectdistance, projectvector); + else + VectorClear(projectvector); + + for (i = 0;i < numshadowmarktris;i++) + { + int remappedelement[3]; + int markindex; + const int *neighbortriangle; + + markindex = shadowmarktris[i] * 3; + neighbortriangle = inneighbor3i + markindex; + side[0] = shadowmark[neighbortriangle[0]] == shadowmarkcount; + side[1] = shadowmark[neighbortriangle[1]] == shadowmarkcount; + side[2] = shadowmark[neighbortriangle[2]] == shadowmarkcount; + if (side[0] + side[1] + side[2] == 0) + continue; + + side[3] = side[0]; + element = inelement3i + markindex; + + // create the vertices + for (j = 0;j < 3;j++) + { + if (side[j] + side[j+1] == 0) + continue; + k = element[j]; + if (vertexupdate[k] != vertexupdatenum) + { + vertexupdate[k] = vertexupdatenum; + vertexremap[k] = outvertices; + vertex = invertex3f + k * 3; + VectorCopy(vertex, outvertex3f); + if (projectdirection) + { + // project one copy of the vertex according to projectvector + VectorAdd(vertex, projectvector, (outvertex3f + 3)); + } + else + { + // project one copy of the vertex to the sphere radius of the light + // (FIXME: would projecting it to the light box be better?) + VectorSubtract(vertex, projectorigin, direction); + ratio = projectdistance / VectorLength(direction); + VectorMA(projectorigin, ratio, direction, (outvertex3f + 3)); + } + outvertex3f += 6; + outvertices += 2; + } + } + + // output the sides (facing outward from this triangle) + if (!side[0]) + { + remappedelement[0] = vertexremap[element[0]]; + remappedelement[1] = vertexremap[element[1]]; + outelement3i[0] = remappedelement[1]; + outelement3i[1] = remappedelement[0]; + outelement3i[2] = remappedelement[0] + 1; + outelement3i[3] = remappedelement[1]; + outelement3i[4] = remappedelement[0] + 1; + outelement3i[5] = remappedelement[1] + 1; + + outelement3i += 6; + outtriangles += 2; + } + if (!side[1]) + { + remappedelement[1] = vertexremap[element[1]]; + remappedelement[2] = vertexremap[element[2]]; + outelement3i[0] = remappedelement[2]; + outelement3i[1] = remappedelement[1]; + outelement3i[2] = remappedelement[1] + 1; + outelement3i[3] = remappedelement[2]; + outelement3i[4] = remappedelement[1] + 1; + outelement3i[5] = remappedelement[2] + 1; + + outelement3i += 6; + outtriangles += 2; + } + if (!side[2]) + { + remappedelement[0] = vertexremap[element[0]]; + remappedelement[2] = vertexremap[element[2]]; + outelement3i[0] = remappedelement[0]; + outelement3i[1] = remappedelement[2]; + outelement3i[2] = remappedelement[2] + 1; + outelement3i[3] = remappedelement[0]; + outelement3i[4] = remappedelement[2] + 1; + outelement3i[5] = remappedelement[0] + 1; + + outelement3i += 6; + outtriangles += 2; + } + } + if (outnumvertices) + *outnumvertices = outvertices; + return outtriangles; +} + void R_Shadow_MarkVolumeFromBox(int firsttriangle, int numtris, const float *invertex3f, const int *elements, 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 t, tend; @@ -878,36 +985,27 @@ void R_Shadow_MarkVolumeFromBox(int firsttriangle, int numtris, const float *inv } } -static void R_Shadow_RenderVolume(int numvertices, int numtriangles, const float *vertex3f, const int *element3i) +qboolean R_Shadow_UseZPass(vec3_t mins, vec3_t maxs) { - if (r_shadow_compilingrtlight) - { - // if we're compiling an rtlight, capture the mesh - Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, NULL, NULL, NULL, vertex3f, NULL, NULL, NULL, NULL, numtriangles, element3i); - return; - } - r_refdef.stats.lights_shadowtriangles += numtriangles; - CHECKGLERROR - R_Mesh_VertexPointer(vertex3f, 0, 0); - GL_LockArrays(0, numvertices); - if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL) - { - // decrement stencil if backface is behind depthbuffer - GL_CullFace(r_refdef.view.cullface_front); - qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR - R_Mesh_Draw(0, numvertices, 0, numtriangles, element3i, NULL, 0, 0); - // increment stencil if frontface is behind depthbuffer - GL_CullFace(r_refdef.view.cullface_back); - qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR - } - R_Mesh_Draw(0, numvertices, 0, numtriangles, element3i, NULL, 0, 0); - GL_LockArrays(0, 0); - CHECKGLERROR +#if 1 + return false; +#else + if (r_shadow_compilingrtlight || !r_shadow_frontsidecasting.integer || !r_shadow_usezpassifpossible.integer) + return false; + // check if the shadow volume intersects the near plane + // + // a ray between the eye and light origin may intersect the caster, + // indicating that the shadow may touch the eye location, however we must + // test the near plane (a polygon), not merely the eye location, so it is + // easiest to enlarge the caster bounding shape slightly for this. + // TODO + return true; +#endif } -void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, const int *elements, const int *neighbors, const vec3_t projectorigin, const vec3_t projectdirection, float projectdistance, int nummarktris, const int *marktris) +void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, const int *elements, const int *neighbors, const vec3_t projectorigin, const vec3_t projectdirection, float projectdistance, int nummarktris, const int *marktris, vec3_t trismins, vec3_t trismaxs) { - int tris, outverts; + int i, tris, outverts; if (projectdistance < 0.1) { Con_Printf("R_Shadow_Volume: projectdistance %f\n", projectdistance); @@ -918,9 +1016,75 @@ void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, // make sure shadowelements is big enough for this volume if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts) R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255); - tris = R_Shadow_ConstructShadowVolume(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris); - r_refdef.stats.lights_dynamicshadowtriangles += tris; - R_Shadow_RenderVolume(outverts, tris, shadowvertex3f, shadowelements); + + if (maxvertexupdate < numverts) + { + maxvertexupdate = numverts; + if (vertexupdate) + Mem_Free(vertexupdate); + if (vertexremap) + Mem_Free(vertexremap); + vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int)); + vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int)); + vertexupdatenum = 0; + } + vertexupdatenum++; + if (vertexupdatenum == 0) + { + vertexupdatenum = 1; + memset(vertexupdate, 0, maxvertexupdate * sizeof(int)); + memset(vertexremap, 0, maxvertexupdate * sizeof(int)); + } + + for (i = 0;i < nummarktris;i++) + shadowmark[marktris[i]] = shadowmarkcount; + + if (r_shadow_compilingrtlight) + { + // if we're compiling an rtlight, capture the mesh + //tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris); + //Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zpass, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements); + tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris); + Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zfail, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements); + } + else + { + // decide which type of shadow to generate and set stencil mode + R_Shadow_RenderMode_StencilShadowVolumes(R_Shadow_UseZPass(trismins, trismaxs)); + // generate the sides or a solid volume, depending on type + if (r_shadow_rendermode >= R_SHADOW_RENDERMODE_ZPASS_STENCIL && r_shadow_rendermode <= R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE) + tris = R_Shadow_ConstructShadowVolume_ZPass(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris); + else + tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris); + r_refdef.stats.lights_dynamicshadowtriangles += tris; + r_refdef.stats.lights_shadowtriangles += tris; + CHECKGLERROR + R_Mesh_VertexPointer(shadowvertex3f, 0, 0); + GL_LockArrays(0, outverts); + if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL) + { + // increment stencil if frontface is infront of depthbuffer + GL_CullFace(r_refdef.view.cullface_front); + qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR + R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0); + // decrement stencil if backface is infront of depthbuffer + GL_CullFace(r_refdef.view.cullface_back); + qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR + } + else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL) + { + // decrement stencil if backface is behind depthbuffer + GL_CullFace(r_refdef.view.cullface_front); + qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR + R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0); + // increment stencil if frontface is behind depthbuffer + GL_CullFace(r_refdef.view.cullface_back); + qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR + } + R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0); + GL_LockArrays(0, 0); + CHECKGLERROR + } } static void R_Shadow_MakeTextures_MakeCorona(void) @@ -942,7 +1106,7 @@ static void R_Shadow_MakeTextures_MakeCorona(void) pixels[y][x][3] = 255; } } - r_shadow_lightcorona = R_LoadTexture2D(r_shadow_texturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE, NULL); + r_shadow_lightcorona = R_LoadTexture2D(r_shadow_texturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR, NULL); } static unsigned int R_Shadow_MakeTextures_SamplePoint(float x, float y, float z) @@ -973,12 +1137,12 @@ static void R_Shadow_MakeTextures(void) // 1D gradient texture for (x = 0;x < ATTEN1DSIZE;x++) data[x] = R_Shadow_MakeTextures_SamplePoint((x + 0.5f) * (1.0f / ATTEN1DSIZE) * (1.0f / 0.9375), 0, 0); - r_shadow_attenuationgradienttexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation1d", ATTEN1DSIZE, 1, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL); + r_shadow_attenuationgradienttexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation1d", ATTEN1DSIZE, 1, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL); // 2D circle texture for (y = 0;y < ATTEN2DSIZE;y++) for (x = 0;x < ATTEN2DSIZE;x++) data[y*ATTEN2DSIZE+x] = R_Shadow_MakeTextures_SamplePoint(((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), 0); - r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL); + r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL); // 3D sphere texture if (r_shadow_texture3d.integer && gl_texture3d) { @@ -986,7 +1150,7 @@ static void R_Shadow_MakeTextures(void) for (y = 0;y < ATTEN3DSIZE;y++) for (x = 0;x < ATTEN3DSIZE;x++) data[(z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x] = R_Shadow_MakeTextures_SamplePoint(((x + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375), ((y + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375), ((z + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375)); - r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA, NULL); + r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL); } else r_shadow_attenuation3dtexture = NULL; @@ -1037,11 +1201,20 @@ void R_Shadow_RenderMode_Begin(void) r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE; if (gl_ext_separatestencil.integer) - r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_SEPARATESTENCIL; + { + r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL; + r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL; + } else if (gl_ext_stenciltwoside.integer) - r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCILTWOSIDE; + { + r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE; + r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE; + } else - r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCIL; + { + r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCIL; + r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL; + } if (r_glsl.integer && gl_support_fragment_shader) r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL; @@ -1059,7 +1232,7 @@ void R_Shadow_RenderMode_ActiveLight(const rtlight_t *rtlight) void R_Shadow_RenderMode_Reset(void) { CHECKGLERROR - if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE) + if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE || r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE) { qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR } @@ -1081,8 +1254,18 @@ void R_Shadow_RenderMode_Reset(void) R_SetupGenericShader(false); } -void R_Shadow_RenderMode_StencilShadowVolumes(qboolean clearstencil) +void R_Shadow_ClearStencil(void) +{ + CHECKGLERROR + GL_Clear(GL_STENCIL_BUFFER_BIT); + r_refdef.stats.lights_clears++; +} + +void R_Shadow_RenderMode_StencilShadowVolumes(qboolean zpass) { + r_shadow_rendermode_t mode = zpass ? r_shadow_shadowingrendermode_zpass : r_shadow_shadowingrendermode_zfail; + if (r_shadow_rendermode == mode) + return; CHECKGLERROR R_Shadow_RenderMode_Reset(); GL_ColorMask(0, 0, 0, 0); @@ -1090,15 +1273,32 @@ void R_Shadow_RenderMode_StencilShadowVolumes(qboolean clearstencil) R_SetupDepthOrShadowShader(); qglDepthFunc(GL_LESS);CHECKGLERROR qglEnable(GL_STENCIL_TEST);CHECKGLERROR - r_shadow_rendermode = r_shadow_shadowingrendermode; - if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SEPARATESTENCIL) + r_shadow_rendermode = mode; + switch(mode) { + default: + break; + case R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL: + GL_CullFace(GL_NONE); + qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR + qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR + break; + case R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL: GL_CullFace(GL_NONE); qglStencilOpSeparate(r_refdef.view.cullface_front, GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR qglStencilOpSeparate(r_refdef.view.cullface_back, GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR - } - else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE) - { + break; + case R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE: + GL_CullFace(GL_NONE); + qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR + qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR + qglStencilMask(~0);CHECKGLERROR + qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR + qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR + qglStencilMask(~0);CHECKGLERROR + qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR + break; + case R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE: GL_CullFace(GL_NONE); qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR qglActiveStencilFaceEXT(r_refdef.view.cullface_front);CHECKGLERROR @@ -1107,10 +1307,8 @@ void R_Shadow_RenderMode_StencilShadowVolumes(qboolean clearstencil) qglActiveStencilFaceEXT(r_refdef.view.cullface_back);CHECKGLERROR qglStencilMask(~0);CHECKGLERROR qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR + break; } - if (clearstencil) - GL_Clear(GL_STENCIL_BUFFER_BIT); - r_refdef.stats.lights_clears++; } void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent) @@ -2443,10 +2641,11 @@ void R_RTLight_Compile(rtlight_t *rtlight) { int i; int numsurfaces, numleafs, numleafpvsbytes, numshadowtrispvsbytes, numlighttrispvsbytes; - int lighttris, shadowtris, shadowmeshes, shadowmeshtris; + int lighttris, shadowtris, shadowzpasstris, shadowzfailtris; entity_render_t *ent = r_refdef.scene.worldentity; dp_model_t *model = r_refdef.scene.worldmodel; unsigned char *data; + shadowmesh_t *mesh; // compile the light rtlight->compiled = true; @@ -2468,7 +2667,7 @@ void R_RTLight_Compile(rtlight_t *rtlight) // this variable must be set for the CompileShadowVolume 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); + 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); numleafpvsbytes = (model->brush.num_leafs + 7) >> 3; numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3; numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3; @@ -2504,17 +2703,15 @@ void R_RTLight_Compile(rtlight_t *rtlight) //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin); //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius); - shadowmeshes = 0; - shadowmeshtris = 0; - if (rtlight->static_meshchain_shadow) - { - shadowmesh_t *mesh; - for (mesh = rtlight->static_meshchain_shadow;mesh;mesh = mesh->next) - { - shadowmeshes++; - shadowmeshtris += mesh->numtriangles; - } - } + shadowzpasstris = 0; + if (rtlight->static_meshchain_shadow_zpass) + for (mesh = rtlight->static_meshchain_shadow_zpass;mesh;mesh = mesh->next) + shadowzpasstris += mesh->numtriangles; + + shadowzfailtris = 0; + if (rtlight->static_meshchain_shadow_zfail) + for (mesh = rtlight->static_meshchain_shadow_zfail;mesh;mesh = mesh->next) + shadowzfailtris += mesh->numtriangles; lighttris = 0; if (rtlight->static_numlighttrispvsbytes) @@ -2529,16 +2726,19 @@ void R_RTLight_Compile(rtlight_t *rtlight) shadowtris++; if (developer.integer >= 10) - Con_Printf("static light built: %f %f %f : %f %f %f box, %i light triangles, %i shadow triangles, %i compiled shadow volume triangles (in %i meshes)\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], lighttris, shadowtris, shadowmeshtris, shadowmeshes); + Con_Printf("static light built: %f %f %f : %f %f %f box, %i light triangles, %i shadow triangles, %i zpass/%i zfail compiled shadow volume triangles\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], lighttris, shadowtris, shadowzpasstris, shadowzfailtris); } void R_RTLight_Uncompile(rtlight_t *rtlight) { if (rtlight->compiled) { - if (rtlight->static_meshchain_shadow) - Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow); - rtlight->static_meshchain_shadow = NULL; + if (rtlight->static_meshchain_shadow_zpass) + Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zpass); + rtlight->static_meshchain_shadow_zpass = NULL; + if (rtlight->static_meshchain_shadow_zfail) + Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow_zfail); + rtlight->static_meshchain_shadow_zfail = NULL; // these allocations are grouped if (rtlight->static_surfacelist) Mem_Free(rtlight->static_surfacelist); @@ -2560,9 +2760,10 @@ void R_Shadow_UncompileWorldLights(void) { size_t lightindex; dlight_t *light; - for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++) + size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked + for (lightindex = 0;lightindex < range;lightindex++) { - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (!light) continue; R_RTLight_Uncompile(&light->rtlight); @@ -2737,17 +2938,35 @@ void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight) void R_Shadow_DrawWorldShadow(int numsurfaces, int *surfacelist, const unsigned char *trispvs) { + qboolean zpass; + shadowmesh_t *mesh; + int t, tend; + int surfacelistindex; + msurface_t *surface; + RSurf_ActiveWorldEntity(); if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer) { - shadowmesh_t *mesh; CHECKGLERROR - for (mesh = rsurface.rtlight->static_meshchain_shadow;mesh;mesh = mesh->next) + zpass = R_Shadow_UseZPass(r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs); + R_Shadow_RenderMode_StencilShadowVolumes(zpass); + mesh = zpass ? rsurface.rtlight->static_meshchain_shadow_zpass : rsurface.rtlight->static_meshchain_shadow_zfail; + for (;mesh;mesh = mesh->next) { r_refdef.stats.lights_shadowtriangles += mesh->numtriangles; R_Mesh_VertexPointer(mesh->vertex3f, mesh->vbo, mesh->vbooffset_vertex3f); GL_LockArrays(0, mesh->numverts); - if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL) + if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZPASS_STENCIL) + { + // increment stencil if frontface is infront of depthbuffer + GL_CullFace(r_refdef.view.cullface_back); + qglStencilOp(GL_KEEP, GL_KEEP, GL_INCR);CHECKGLERROR + R_Mesh_Draw(0, mesh->numverts, 0, mesh->numtriangles, mesh->element3i, mesh->element3s, mesh->ebo3i, mesh->ebo3s); + // decrement stencil if backface is infront of depthbuffer + GL_CullFace(r_refdef.view.cullface_front); + qglStencilOp(GL_KEEP, GL_KEEP, GL_DECR);CHECKGLERROR + } + else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_ZFAIL_STENCIL) { // decrement stencil if backface is behind depthbuffer GL_CullFace(r_refdef.view.cullface_front); @@ -2764,9 +2983,6 @@ void R_Shadow_DrawWorldShadow(int numsurfaces, int *surfacelist, const unsigned } else if (numsurfaces && r_refdef.scene.worldmodel->brush.shadowmesh && r_shadow_culltriangles.integer) { - int t, tend; - int surfacelistindex; - msurface_t *surface; R_Shadow_PrepareShadowMark(r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles); for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++) { @@ -2775,10 +2991,12 @@ void R_Shadow_DrawWorldShadow(int numsurfaces, int *surfacelist, const unsigned if (CHECKPVSBIT(trispvs, t)) shadowmarklist[numshadowmark++] = t; } - R_Shadow_VolumeFromList(r_refdef.scene.worldmodel->brush.shadowmesh->numverts, r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles, r_refdef.scene.worldmodel->brush.shadowmesh->vertex3f, r_refdef.scene.worldmodel->brush.shadowmesh->element3i, r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius + r_refdef.scene.worldmodel->radius*2 + r_shadow_projectdistance.value, numshadowmark, shadowmarklist); + R_Shadow_VolumeFromList(r_refdef.scene.worldmodel->brush.shadowmesh->numverts, r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles, r_refdef.scene.worldmodel->brush.shadowmesh->vertex3f, r_refdef.scene.worldmodel->brush.shadowmesh->element3i, r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius + r_refdef.scene.worldmodel->radius*2 + r_shadow_projectdistance.value, numshadowmark, shadowmarklist, r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs); } else if (numsurfaces) r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs); + + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity } void R_Shadow_DrawEntityShadow(entity_render_t *ent) @@ -2794,7 +3012,8 @@ void R_Shadow_DrawEntityShadow(entity_render_t *ent) 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->surfacelist, relativeshadowmins, relativeshadowmaxs); + 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 } void R_Shadow_SetupEntityLight(const entity_render_t *ent) @@ -2826,6 +3045,8 @@ void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const unsigned c R_Mesh_TexMatrix(3, &rsurface.entitytolight); r_refdef.scene.worldmodel->DrawLight(r_refdef.scene.worldentity, numsurfaces, surfacelist, trispvs); + + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity } void R_Shadow_DrawEntityLight(entity_render_t *ent) @@ -2836,7 +3057,9 @@ void R_Shadow_DrawEntityLight(entity_render_t *ent) R_Shadow_SetupEntityLight(ent); - model->DrawLight(ent, model->nummodelsurfaces, model->surfacelist, NULL); + model->DrawLight(ent, model->nummodelsurfaces, model->sortedmodelsurfaces, NULL); + + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity } void R_DrawRTLight(rtlight_t *rtlight, qboolean visible) @@ -2850,10 +3073,10 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible) int numlightentities_noselfshadow; int numshadowentities; int numshadowentities_noselfshadow; - entity_render_t *lightentities[MAX_EDICTS]; - entity_render_t *lightentities_noselfshadow[MAX_EDICTS]; - entity_render_t *shadowentities[MAX_EDICTS]; - entity_render_t *shadowentities_noselfshadow[MAX_EDICTS]; + 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]; // 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) @@ -2908,7 +3131,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible) // dynamic light, world available and can receive realtime lighting // calculate lit surfaces and leafs R_Shadow_EnlargeLeafSurfaceTrisBuffer(r_refdef.scene.worldmodel->brush.num_leafs, r_refdef.scene.worldmodel->num_surfaces, r_refdef.scene.worldmodel->brush.shadowmesh ? r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles : r_refdef.scene.worldmodel->surfmesh.num_triangles, r_refdef.scene.worldmodel->surfmesh.num_triangles); - r_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rsurface.rtlight_cullmins, rsurface.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_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rsurface.rtlight_cullmins, rsurface.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); leaflist = r_shadow_buffer_leaflist; leafpvs = r_shadow_buffer_leafpvs; surfacelist = r_shadow_buffer_surfacelist; @@ -3045,7 +3268,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible) { // draw stencil shadow volumes to mask off pixels that are in shadow // so that they won't receive lighting - R_Shadow_RenderMode_StencilShadowVolumes(true); + R_Shadow_ClearStencil(); if (numsurfaces) R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs); for (i = 0;i < numshadowentities;i++) @@ -3065,8 +3288,6 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible) for (i = 0;i < numlightentities_noselfshadow;i++) R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]); } - - R_Shadow_RenderMode_StencilShadowVolumes(false); } for (i = 0;i < numshadowentities_noselfshadow;i++) R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]); @@ -3128,6 +3349,7 @@ void R_ShadowVolumeLighting(qboolean visible) int lnum; size_t lightindex; dlight_t *light; + size_t range; if (r_editlights.integer) R_Shadow_DrawLightSprites(); @@ -3138,27 +3360,29 @@ void R_ShadowVolumeLighting(qboolean visible) if (r_shadow_debuglight.integer >= 0) { lightindex = r_shadow_debuglight.integer; - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (light && (light->flags & flag)) R_DrawRTLight(&light->rtlight, visible); } else { - for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++) + range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked + for (lightindex = 0;lightindex < range;lightindex++) { - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (light && (light->flags & flag)) R_DrawRTLight(&light->rtlight, visible); } } if (r_refdef.scene.rtdlight) for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++) - R_DrawRTLight(&r_refdef.scene.lights[lnum], visible); + R_DrawRTLight(r_refdef.scene.lights[lnum], visible); R_Shadow_RenderMode_End(); } extern void R_SetupView(qboolean allowwaterclippingplane); +extern cvar_t r_shadows; extern cvar_t r_shadows_throwdistance; void R_DrawModelShadows(void) { @@ -3168,6 +3392,7 @@ void R_DrawModelShadows(void) vec3_t relativelightorigin; vec3_t relativelightdirection; vec3_t relativeshadowmins, relativeshadowmaxs; + vec3_t tmp; float vertex3f[12]; if (!r_drawentities.integer || !gl_stencil) @@ -3179,13 +3404,22 @@ void R_DrawModelShadows(void) r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE; if (gl_ext_separatestencil.integer) - r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_SEPARATESTENCIL; + { + r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL; + r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL; + } else if (gl_ext_stenciltwoside.integer) - r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCILTWOSIDE; + { + r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE; + r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE; + } else - r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCIL; + { + r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCIL; + r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL; + } - R_Shadow_RenderMode_StencilShadowVolumes(true); + R_Shadow_ClearStencil(); for (i = 0;i < r_refdef.scene.numentities;i++) { @@ -3196,10 +3430,46 @@ void R_DrawModelShadows(void) relativethrowdistance = r_shadows_throwdistance.value * Matrix4x4_ScaleFromMatrix(&ent->inversematrix); VectorSet(relativeshadowmins, -relativethrowdistance, -relativethrowdistance, -relativethrowdistance); VectorSet(relativeshadowmaxs, relativethrowdistance, relativethrowdistance, relativethrowdistance); - VectorNegate(ent->modellight_lightdir, relativelightdirection); + + if(r_shadows.integer == 2) + { + // 2: simpler mode, throw shadows always DOWN + VectorSet(tmp, 0, 0, -1); + Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection); + } + else + { + if(ent->entitynumber != 0) + { + // networked entity - might be attached in some way (then we should use the parent's light direction, to not tear apart attached entities) + int entnum, entnum2, recursion; + entnum = entnum2 = ent->entitynumber; + for(recursion = 32; recursion > 0; --recursion) + { + entnum2 = cl.entities[entnum].state_current.tagentity; + if(entnum2 >= 1 && entnum2 < cl.num_entities && cl.entities_active[entnum2]) + entnum = entnum2; + else + break; + } + if(recursion && recursion != 32) // if we followed a valid non-empty attachment chain + { + VectorNegate(cl.entities[entnum].render.modellight_lightdir, relativelightdirection); + // transform into modelspace of OUR entity + Matrix4x4_Transform3x3(&cl.entities[entnum].render.matrix, relativelightdirection, tmp); + Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection); + } + else + VectorNegate(ent->modellight_lightdir, relativelightdirection); + } + else + VectorNegate(ent->modellight_lightdir, relativelightdirection); + } + VectorScale(relativelightdirection, -relativethrowdistance, relativelightorigin); RSurf_ActiveModelEntity(ent, false, false); - ent->model->DrawShadowVolume(ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->surfacelist, relativeshadowmins, relativeshadowmaxs); + ent->model->DrawShadowVolume(ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs); + rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity } } @@ -3246,62 +3516,153 @@ void R_DrawModelShadows(void) 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)) + return; + zdist = (DotProduct(rtlight->shadoworigin, r_refdef.view.forward) - DotProduct(r_refdef.view.origin, r_refdef.view.forward)); + if (zdist < 32) + return; + if (usequery && r_numqueries + 2 <= r_maxqueries) + { + rtlight->corona_queryindex_allpixels = r_queries[r_numqueries++]; + rtlight->corona_queryindex_visiblepixels = r_queries[r_numqueries++]; + VectorMA(r_refdef.view.origin, zdist, r_refdef.view.forward, centerorigin); + + CHECKGLERROR + // NOTE: we can't disable depth testing using R_DrawSprite's depthdisable argument, which calls GL_DepthTest, as that's broken in the ATI drivers + qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_allpixels); + qglDepthFunc(GL_ALWAYS); + R_DrawSprite(GL_ONE, GL_ZERO, r_shadow_lightcorona, NULL, false, false, centerorigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, 1, 1, 1, 1); + qglEndQueryARB(GL_SAMPLES_PASSED_ARB); + qglDepthFunc(GL_LEQUAL); + qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_visiblepixels); + R_DrawSprite(GL_ONE, GL_ZERO, r_shadow_lightcorona, NULL, false, false, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, 1, 1, 1, 1); + qglEndQueryARB(GL_SAMPLES_PASSED_ARB); + CHECKGLERROR + } + rtlight->corona_visibility = bound(0, (zdist - 32) / 32, 1); +} + +void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale) +{ + vec3_t color; + GLint allpixels = 0, visiblepixels = 0; + // now we have to check the query result + if (rtlight->corona_queryindex_visiblepixels) + { + CHECKGLERROR + qglGetQueryObjectivARB(rtlight->corona_queryindex_visiblepixels, GL_QUERY_RESULT_ARB, &visiblepixels); + qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, &allpixels); + CHECKGLERROR + //Con_Printf("%i of %i pixels\n", (int)visiblepixels, (int)allpixels); + if (visiblepixels < 1 || allpixels < 1) + return; + rtlight->corona_visibility *= bound(0, (float)visiblepixels / (float)allpixels, 1); + cscale *= rtlight->corona_visibility; + } + 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) + return; + } + VectorScale(rtlight->color, 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); +} + void R_DrawCoronas(void) { int i, flag; - float cscale, scale; + qboolean usequery; size_t lightindex; dlight_t *light; rtlight_t *rtlight; + size_t range; if (r_coronas.value < (1.0f / 256.0f) && !gl_flashblend.integer) return; - R_Mesh_Matrix(&identitymatrix); + if (r_waterstate.renderingscene) + return; flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE; - // FIXME: these traces should scan all render entities instead of cl.world - for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++) + R_Mesh_Matrix(&identitymatrix); + + range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked + + // check occlusion of coronas + // use GL_ARB_occlusion_query if available + // otherwise use raytraces + r_numqueries = 0; + usequery = gl_support_arb_occlusion_query && r_coronas_occlusionquery.integer; + if (usequery) + { + GL_ColorMask(0,0,0,0); + if (r_maxqueries < (range + r_refdef.scene.numlights) * 2) + if (r_maxqueries < R_MAX_OCCLUSION_QUERIES) + { + i = r_maxqueries; + r_maxqueries = (range + r_refdef.scene.numlights) * 4; + r_maxqueries = min(r_maxqueries, R_MAX_OCCLUSION_QUERIES); + CHECKGLERROR + qglGenQueriesARB(r_maxqueries - i, r_queries + i); + CHECKGLERROR + } + } + for (lightindex = 0;lightindex < range;lightindex++) { - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (!light) continue; rtlight = &light->rtlight; + rtlight->corona_visibility = 0; + rtlight->corona_queryindex_visiblepixels = 0; + rtlight->corona_queryindex_allpixels = 0; if (!(rtlight->flags & flag)) continue; - if (rtlight->corona * r_coronas.value <= 0) + if (rtlight->corona <= 0) continue; if (r_shadow_debuglight.integer >= 0 && r_shadow_debuglight.integer != (int)lightindex) continue; - cscale = rtlight->corona * r_coronas.value* 0.25f; - scale = rtlight->radius * rtlight->coronasizescale; - if (VectorDistance2(rtlight->shadoworigin, r_refdef.view.origin) < 16.0f * 16.0f) - continue; - if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1) - continue; - 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, rtlight->color[0] * cscale, rtlight->color[1] * cscale, rtlight->color[2] * cscale, 1); + R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery); } for (i = 0;i < r_refdef.scene.numlights;i++) { - rtlight = &r_refdef.scene.lights[i]; + rtlight = r_refdef.scene.lights[i]; + rtlight->corona_visibility = 0; + rtlight->corona_queryindex_visiblepixels = 0; + rtlight->corona_queryindex_allpixels = 0; if (!(rtlight->flags & flag)) continue; if (rtlight->corona <= 0) continue; - if (VectorDistance2(rtlight->shadoworigin, r_refdef.view.origin) < 32.0f * 32.0f) + R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery); + } + if (usequery) + GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1); + + // now draw the coronas using the query data for intensity info + for (lightindex = 0;lightindex < range;lightindex++) + { + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + if (!light) continue; - if (gl_flashblend.integer) - { - cscale = rtlight->corona * 1.0f; - scale = rtlight->radius * rtlight->coronasizescale * 2.0f; - } - else - { - cscale = rtlight->corona * r_coronas.value* 0.25f; - scale = rtlight->radius * rtlight->coronasizescale; - } - if (VectorLength(rtlight->color) * cscale < (1.0f / 256.0f)) + rtlight = &light->rtlight; + if (rtlight->corona_visibility <= 0) continue; - if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1) + R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale); + } + for (i = 0;i < r_refdef.scene.numlights;i++) + { + rtlight = r_refdef.scene.lights[i]; + if (rtlight->corona_visibility <= 0) continue; - 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, rtlight->color[0] * cscale, rtlight->color[1] * cscale, rtlight->color[2] * cscale, 1); + if (gl_flashblend.integer) + R_DrawCorona(rtlight, rtlight->corona, rtlight->radius * rtlight->coronasizescale * 2.0f); + else + R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale); } } @@ -3394,7 +3755,7 @@ rtexture_t *R_Shadow_LoadCubemap(const char *basename) if (!r_shadow_filters_texturepool) r_shadow_filters_texturepool = R_AllocTexturePool(); - cubemaptexture = R_LoadTextureCubeMap(r_shadow_filters_texturepool, basename, cubemapsize, cubemappixels, TEXTYPE_BGRA, TEXF_PRECACHE | (gl_texturecompression_lightcubemaps.integer ? TEXF_COMPRESS : 0), NULL); + cubemaptexture = R_LoadTextureCubeMap(r_shadow_filters_texturepool, basename, cubemapsize, cubemappixels, TEXTYPE_BGRA, TEXF_PRECACHE | (gl_texturecompression_lightcubemaps.integer ? TEXF_COMPRESS : 0) | TEXF_FORCELINEAR, NULL); Mem_Free(cubemappixels); } else @@ -3494,9 +3855,10 @@ void R_Shadow_ClearWorldLights(void) { size_t lightindex; dlight_t *light; - for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++) + size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked + for (lightindex = 0;lightindex < range;lightindex++) { - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (light) R_Shadow_FreeWorldLight(light); } @@ -3557,9 +3919,10 @@ void R_Shadow_DrawLightSprites(void) { size_t lightindex; dlight_t *light; - for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++) + size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked + for (lightindex = 0;lightindex < range;lightindex++) { - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (light) R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSprite_TransparentCallback, (entity_render_t *)light, 5, &light->rtlight); } @@ -3572,11 +3935,12 @@ void R_Shadow_SelectLightInView(void) dlight_t *best; size_t lightindex; dlight_t *light; + size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked best = NULL; bestrating = 0; - for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++) + for (lightindex = 0;lightindex < range;lightindex++) { - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (!light) continue; VectorSubtract(light->origin, r_refdef.view.origin, temp); @@ -3646,7 +4010,11 @@ void R_Shadow_LoadWorldLights(void) #if _MSC_VER >= 1400 #define sscanf sscanf_s #endif - a = sscanf(t, "%f %f %f %f %f %f %f %d %s %f %f %f %f %f %f %f %f %i", &origin[0], &origin[1], &origin[2], &radius, &color[0], &color[1], &color[2], &style, cubemapname + cubemapname[sizeof(cubemapname)-1] = 0; +#if MAX_QPATH != 128 +#error update this code if MAX_QPATH changes +#endif + a = sscanf(t, "%f %f %f %f %f %f %f %d %127s %f %f %f %f %f %f %f %f %i", &origin[0], &origin[1], &origin[2], &radius, &color[0], &color[1], &color[2], &style, cubemapname #if _MSC_VER >= 1400 , sizeof(cubemapname) #endif @@ -3702,7 +4070,9 @@ void R_Shadow_SaveWorldLights(void) char *buf, *oldbuf; char name[MAX_QPATH]; char line[MAX_INPUTLINE]; - if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray)) + size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked, assuming the dpsnprintf mess doesn't screw it up... + // I hate lines which are 3 times my screen size :( --blub + if (!range) return; if (cl.worldmodel == NULL) { @@ -3713,9 +4083,9 @@ void R_Shadow_SaveWorldLights(void) strlcat (name, ".rtlights", sizeof (name)); bufchars = bufmaxchars = 0; buf = NULL; - for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++) + for (lightindex = 0;lightindex < range;lightindex++) { - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (!light) continue; if (light->coronasizescale != 0.25f || light->ambientscale != 0 || light->diffusescale != 1 || light->specularscale != 1 || light->flags != LIGHTFLAG_REALTIMEMODE) @@ -4430,6 +4800,7 @@ void R_Shadow_EditLights_EditAll_f(void) { size_t lightindex; dlight_t *light; + size_t range; if (!r_editlights.integer) { @@ -4437,9 +4808,11 @@ void R_Shadow_EditLights_EditAll_f(void) return; } - for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++) + // EditLights doesn't seem to have a "remove" command or something so: + range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked + for (lightindex = 0;lightindex < range;lightindex++) { - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (!light) continue; R_Shadow_SelectLight(light); @@ -4450,7 +4823,7 @@ void R_Shadow_EditLights_EditAll_f(void) void R_Shadow_EditLights_DrawSelectedLightProperties(void) { int lightnumber, lightcount; - size_t lightindex; + size_t lightindex, range; dlight_t *light; float x, y; char temp[256]; @@ -4461,9 +4834,10 @@ void R_Shadow_EditLights_DrawSelectedLightProperties(void) DrawQ_Pic(x-5, y-5, NULL, 250, 155, 0, 0, 0, 0.75, 0); lightnumber = -1; lightcount = 0; - for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++) + range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked + for (lightindex = 0;lightindex < range;lightindex++) { - light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); + light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); if (!light) continue; if (light == r_shadow_selectedlight) @@ -4691,7 +5065,7 @@ void R_CompleteLightPoint(vec3_t ambientcolor, vec3_t diffusecolor, vec3_t diffu rtlight_t *light; for (i = 0;i < r_refdef.scene.numlights;i++) { - light = &r_refdef.scene.lights[i]; + 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)