From 146e92be41a5c709e3aa273115c28c96afe1965d Mon Sep 17 00:00:00 2001 From: eihrul Date: Wed, 30 Sep 2009 15:26:55 +0000 Subject: [PATCH] shadowmap projection fixes git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@9264 d7cf8633-e32d-0410-b094-e92efae38249 --- gl_rmain.c | 35 ++++++++++++----------------------- gl_textures.c | 6 +++--- r_shadow.c | 18 +++++++++--------- r_textures.h | 2 +- 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/gl_rmain.c b/gl_rmain.c index 72f8c7d5..54159c25 100644 --- a/gl_rmain.c +++ b/gl_rmain.c @@ -912,11 +912,6 @@ static const char *builtinshaderstring = "{\n" " vec3 adir = abs(dir);\n" "# ifndef USESHADOWMAPVSDCT\n" -"# ifdef USESHADOWMAPRECT\n" -"# define cubedir(dx, dy, ox, oy) { tc = vec2(dx, dy); offset = vec2(ox, oy); }\n" -"# else\n" -"# define cubedir(dx, dy, ox, oy) { tc = vec2(dx, dy); offset = vec2(ox/2.0, oy/4.0); }\n" -"# endif\n" " vec2 tc;\n" " vec2 offset;\n" " float ma;\n" @@ -925,14 +920,14 @@ static const char *builtinshaderstring = " if (adir.x > adir.z)\n" " {\n" " ma = adir.x;\n" -" if (dir.x >= 0.0) cubedir(-dir.z, -dir.y, 0.5, 0.5) // +X\n" -" else cubedir( dir.z, -dir.y, 1.5, 0.5) // -X\n" +" if (dir.x >= 0.0) { tc = vec2(-dir.z, -dir.y); offset = vec2(0.5, 0.5); } // +X\n" +" else { tc = vec2( dir.z, -dir.y); offset = vec2(1.5, 0.5); } // -X\n" " }\n" " else\n" " {\n" " ma = adir.z;\n" -" if (dir.z >= 0.0) cubedir( dir.x, -dir.y, 0.5, 2.5) // +Z\n" -" else cubedir(-dir.x, -dir.y, 1.5, 2.5) // -Z\n" +" if (dir.z >= 0.0) { tc = vec2( dir.x, -dir.y); offset = vec2(0.5, 2.5); } // +Z\n" +" else { tc = vec2(-dir.x, -dir.y); offset = vec2(1.5, 2.5); } // -Z\n" " }\n" " }\n" " else\n" @@ -940,28 +935,22 @@ static const char *builtinshaderstring = " if (adir.y > adir.z)\n" " {\n" " ma = adir.y;\n" -" if (dir.y >= 0.0) cubedir( dir.x, dir.z, 0.5, 1.5) // +Y\n" -" else cubedir( dir.x, -dir.z, 1.5, 1.5) // -Y\n" +" if (dir.y >= 0.0) { tc = vec2( dir.x, dir.z); offset = vec2(0.5, 1.5); } // +Y\n" +" else { tc = vec2( dir.x, -dir.z); offset = vec2(1.5, 1.5); } // -Y\n" " }\n" " else\n" " {\n" " ma = adir.z;\n" -" if (dir.z >= 0.0) cubedir( dir.x, -dir.y, 0.5, 2.5) // +Z\n" -" else cubedir(-dir.x, -dir.y, 1.5, 2.5) // -Z\n" +" if (dir.z >= 0.0) { tc = vec2( dir.x, -dir.y); offset = vec2(0.5, 2.5); } // +Z\n" +" else { tc = vec2(-dir.x, -dir.y); offset = vec2(1.5, 2.5); } // -Z\n" " }\n" " }\n" "\n" -"# ifdef USESHADOWMAPRECT\n" -" return vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma + vec3(offset * ShadowMap_Parameters.y, ShadowMap_Parameters.z);\n" -"# else\n" -" return vec3(tc * ShadowMap_Parameters.xy, ShadowMap_Parameters.w) / ma + vec3(offset, ShadowMap_Parameters.z);\n" -"# endif\n" +" vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma + vec3(offset * ShadowMap_Parameters.y, ShadowMap_Parameters.z);\n" +" stc.xy *= ShadowMap_TextureScale.xy;\n" +" return stc;\n" "# else\n" -"# ifdef USESHADOWMAPRECT \n" -" return vec3(textureCube(Texture_CubeProjection, dir.xyz).ra * ShadowMap_TextureScale.xy, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z));\n" -"# else\n" -" return vec3(textureCube(Texture_CubeProjection, dir.xyz).ra, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z));\n" -"# endif\n" +" return vec3(textureCube(Texture_CubeProjection, dir.xyz).ra * ShadowMap_Parameters.xy, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z));\n" "# endif\n" "}\n" "#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n" diff --git a/gl_textures.c b/gl_textures.c index e5b084fe..3f9a3ba6 100644 --- a/gl_textures.c +++ b/gl_textures.c @@ -1147,7 +1147,7 @@ rtexture_t *R_LoadTextureShadowMapCube(rtexturepool_t *rtexturepool, const char return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, TEXF_ALWAYSPRECACHE | TEXF_CLAMP | (filter ? TEXF_FORCELINEAR | TEXF_COMPARE : TEXF_FORCENEAREST), TEXTYPE_SHADOWMAP, GLTEXTURETYPE_CUBEMAP, NULL, NULL); } -rtexture_t *R_LoadTextureCubeProjection(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int size, int border) +rtexture_t *R_LoadTextureCubeProjection(rtexturepool_t *rtexturepool, const char *identifier, int size, int border) { // maps to a 2x3 texture rectangle with normalized coordinates (must be scaled by size after lookup) // +- @@ -1171,8 +1171,8 @@ rtexture_t *R_LoadTextureCubeProjection(rtexturepool_t *rtexturepool, const char { for (k = 0;k < res;k++) { - *texel++ = (x + ((2*k + 1)<matrix_lighttoworld, side, size, r_shadow_shadowmapping_bordersize.integer, nearclip, farclip, NULL); 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_shadowmap_parameters[0] = (0.5f / 2) * (1.0f - r_shadow_shadowmapborder / (float)size); - r_shadow_shadowmap_parameters[1] = (0.5f / 4) * (1.0f - r_shadow_shadowmapborder / (float)size); + r_shadow_shadowmap_parameters[0] = r_shadow_shadowmapvsdct ? size / (float)maxsize : 0.5f * (size - r_shadow_shadowmapborder); + r_shadow_shadowmap_parameters[1] = r_shadow_shadowmapvsdct ? 0.75f * size / (float)maxsize : size; r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAP2D; } else if (r_shadow_shadowmode == 2) { // complex unrolled cube approach (more flexible) if (r_shadow_shadowmapvsdct && !r_shadow_shadowmapcubeprojectiontexture[r_shadow_shadowmaplod]) - r_shadow_shadowmapcubeprojectiontexture[r_shadow_shadowmaplod] = R_LoadTextureCubeProjection(r_shadow_texturepool, "shadowmapcubeprojection", 2, 3, size, r_shadow_shadowmapborder); + r_shadow_shadowmapcubeprojectiontexture[r_shadow_shadowmaplod] = R_LoadTextureCubeProjection(r_shadow_texturepool, "shadowmapcubeprojection", size, r_shadow_shadowmapborder); if (!r_shadow_shadowmaprectangletexture) { #if 1 @@ -1567,10 +1567,10 @@ void R_Shadow_RenderMode_ShadowMap(int side, qboolean clear, int size) qglClearColor(1,1,1,1);CHECKGLERROR } R_Viewport_InitRectSideView(&viewport, &rsurface.rtlight->matrix_lighttoworld, side, size, r_shadow_shadowmapborder, nearclip, farclip, NULL); - r_shadow_shadowmap_texturescale[0] = 2*size; - r_shadow_shadowmap_texturescale[1] = 3*size; - r_shadow_shadowmap_parameters[0] = 0.5f * (size - r_shadow_shadowmapborder); - r_shadow_shadowmap_parameters[1] = size; + r_shadow_shadowmap_texturescale[0] = 1.0f; + r_shadow_shadowmap_texturescale[1] = 1.0f; + r_shadow_shadowmap_parameters[0] = r_shadow_shadowmapvsdct ? 2*size : 0.5f * (size - r_shadow_shadowmapborder); + r_shadow_shadowmap_parameters[1] = r_shadow_shadowmapvsdct ? 3*size : size; r_shadow_rendermode = R_SHADOW_RENDERMODE_SHADOWMAPRECTANGLE; } else if (r_shadow_shadowmode == 3) @@ -3716,7 +3716,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible) if ((r_shadow_shadowmapping_maxsize.integer >> i) > lodlinear) r_shadow_shadowmaplod = i; - size = r_shadow_shadowmapvsdct || r_shadow_shadowmode ? r_shadow_shadowmapping_maxsize.integer >> r_shadow_shadowmaplod : lodlinear; + size = r_shadow_shadowmapvsdct || r_shadow_shadowmode == 3 ? r_shadow_shadowmapping_maxsize.integer >> r_shadow_shadowmaplod : lodlinear; size = bound(1, size, 2048); //Con_Printf("distance %f lodlinear %i (lod %i) size %i\n", distance, lodlinear, r_shadow_shadowmaplod, size); diff --git a/r_textures.h b/r_textures.h index 3cb7f838..55adebc5 100644 --- a/r_textures.h +++ b/r_textures.h @@ -86,7 +86,7 @@ rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *ident rtexture_t *R_LoadTextureShadowMapRectangle(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, qboolean filter); rtexture_t *R_LoadTextureShadowMap2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, qboolean filter); rtexture_t *R_LoadTextureShadowMapCube(rtexturepool_t *rtexturepool, const char *identifier, int width, qboolean filter); -rtexture_t *R_LoadTextureCubeProjection(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int size, int border); +rtexture_t *R_LoadTextureCubeProjection(rtexturepool_t *rtexturepool, const char *identifier, int size, int border); // free a texture void R_FreeTexture(rtexture_t *rt); -- 2.39.2