diff --git a/interface/resources/shaders/shadow_map.frag b/interface/resources/shaders/shadow_map.frag index f53c66b095..f2246fff55 100644 --- a/interface/resources/shaders/shadow_map.frag +++ b/interface/resources/shaders/shadow_map.frag @@ -13,11 +13,17 @@ uniform sampler2DShadow shadowMap; +uniform vec3 shadowDistances; + +// the color in shadow varying vec4 shadowColor; +// the interpolated position +varying vec4 position; + void main(void) { - gl_FragColor = mix(shadowColor, gl_Color, shadow2D(shadowMap, gl_TexCoord[0].stp).r * - shadow2D(shadowMap, gl_TexCoord[1].stp).r * - shadow2D(shadowMap, gl_TexCoord[2].stp).r * - shadow2D(shadowMap, gl_TexCoord[3].stp).r); + int shadowIndex = int(dot(step(vec3(position.z), shadowDistances), vec3(1.0, 1.0, 1.0))); + vec3 texCoord = vec3(dot(gl_EyePlaneS[shadowIndex], position), dot(gl_EyePlaneT[shadowIndex], position), + dot(gl_EyePlaneR[shadowIndex], position)); + gl_FragColor = mix(shadowColor, gl_Color, shadow2D(shadowMap, texCoord).r); } diff --git a/interface/resources/shaders/shadow_map.vert b/interface/resources/shaders/shadow_map.vert index e7efcd7c0f..dcc5324927 100644 --- a/interface/resources/shaders/shadow_map.vert +++ b/interface/resources/shaders/shadow_map.vert @@ -11,8 +11,12 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +// the color in shadow varying vec4 shadowColor; +// the interpolated position +varying vec4 position; + void main(void) { // the shadow color includes only the ambient terms shadowColor = gl_Color * (gl_LightModel.ambient + gl_LightSource[0].ambient); @@ -22,15 +26,7 @@ void main(void) { gl_FrontColor = shadowColor + gl_Color * (gl_LightSource[0].diffuse * max(0.0, dot(normal, gl_LightSource[0].position))); // generate the shadow texture coordinates using the eye position - vec4 eyePosition = gl_ModelViewMatrix * gl_Vertex; - gl_TexCoord[0] = vec4(dot(gl_EyePlaneS[0], eyePosition), dot(gl_EyePlaneT[0], eyePosition), - dot(gl_EyePlaneR[0], eyePosition), 1.0); - gl_TexCoord[1] = vec4(dot(gl_EyePlaneS[1], eyePosition), dot(gl_EyePlaneT[1], eyePosition), - dot(gl_EyePlaneR[1], eyePosition), 1.0); - gl_TexCoord[2] = vec4(dot(gl_EyePlaneS[2], eyePosition), dot(gl_EyePlaneT[2], eyePosition), - dot(gl_EyePlaneR[2], eyePosition), 1.0); - gl_TexCoord[3] = vec4(dot(gl_EyePlaneS[3], eyePosition), dot(gl_EyePlaneT[3], eyePosition), - dot(gl_EyePlaneR[3], eyePosition), 1.0); + position = gl_ModelViewMatrix * gl_Vertex; // use the fixed function transform gl_Position = ftransform(); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0cae0f3e75..a2892eb003 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2260,7 +2260,6 @@ void Application::updateShadowMap() { glm::quat rotation = rotationBetween(IDENTITY_FRONT, lightDirection); glm::quat inverseRotation = glm::inverse(rotation); - const float MAP_DISTANCES[] = { 0.0f, 2.0f, 6.0f, 14.0f, 30.0f }; const glm::vec2 MAP_COORDS[] = { glm::vec2(0.0f, 0.0f), glm::vec2(0.5f, 0.0f), glm::vec2(0.0f, 0.5f), glm::vec2(0.5f, 0.5f) }; @@ -2272,8 +2271,8 @@ void Application::updateShadowMap() { const glm::vec2& coord = MAP_COORDS[i]; glViewport(coord.s * fbo->width(), coord.t * fbo->height(), halfSize, halfSize); - float nearScale = MAP_DISTANCES[i] * frustumScale; - float farScale = MAP_DISTANCES[i + 1] * frustumScale; + float nearScale = SHADOW_MATRIX_DISTANCES[i] * frustumScale; + float farScale = SHADOW_MATRIX_DISTANCES[i + 1] * frustumScale; glm::vec3 points[] = { glm::mix(_viewFrustum.getNearTopLeft(), _viewFrustum.getFarTopLeft(), nearScale), glm::mix(_viewFrustum.getNearTopRight(), _viewFrustum.getFarTopRight(), nearScale), diff --git a/interface/src/Application.h b/interface/src/Application.h index 5d29a9057a..59f9ad4e15 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -127,6 +127,7 @@ static const float MIRROR_REARVIEW_BODY_DISTANCE = 2.3f; static const float MIRROR_FIELD_OF_VIEW = 30.0f; static const int SHADOW_MATRIX_COUNT = 4; +static const float SHADOW_MATRIX_DISTANCES[] = { 0.0f, 2.0f, 6.0f, 14.0f, 30.0f }; class Application : public QApplication { Q_OBJECT diff --git a/interface/src/voxels/VoxelSystem.cpp b/interface/src/voxels/VoxelSystem.cpp index 4f1b322455..11bd47b7e4 100644 --- a/interface/src/voxels/VoxelSystem.cpp +++ b/interface/src/voxels/VoxelSystem.cpp @@ -518,6 +518,8 @@ void VoxelSystem::initVoxelMemory() { _shadowMapProgram.bind(); _shadowMapProgram.setUniformValue("shadowMap", 0); + _shadowMapProgram.setUniformValue("shadowDistances", -SHADOW_MATRIX_DISTANCES[1], + -SHADOW_MATRIX_DISTANCES[2], -SHADOW_MATRIX_DISTANCES[3]); _shadowMapProgram.release(); } }