From 687c76a6f8ae0aff1cf1554a9d929cee01109035 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 23 Feb 2015 13:12:29 -0800 Subject: [PATCH] trying to ge the shadow to render correctly --- interface/src/Application.cpp | 6 +++++- libraries/octree/src/ViewFrustum.cpp | 16 ++++++++++------ libraries/render-utils/src/Shadow.slh | 2 ++ .../src/directional_ambient_light_shadow_map.slf | 2 ++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 1211df3727..f533765644 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2564,7 +2564,9 @@ void Application::updateShadowMap() { glm::ortho(minima.x, maxima.x, minima.y, maxima.y, -maxima.z, -minima.z) * glm::mat4_cast(inverseRotation)); // update the shadow view frustum - _shadowViewFrustum.setPosition(rotation * ((minima + maxima) * 0.5f)); + // glm::vec3 shadowFrustumCenter = glm::vec3((minima.x + maxima.x) * 0.5f, (minima.y + maxima.y) * 0.5f, (minima.z + maxima.z) * 0.5f); + glm::vec3 shadowFrustumCenter = rotation * ((minima + maxima) * 0.5f); + _shadowViewFrustum.setPosition(shadowFrustumCenter); _shadowViewFrustum.setOrientation(rotation); _shadowViewFrustum.setOrthographic(true); _shadowViewFrustum.setWidth(maxima.x - minima.x); @@ -2594,8 +2596,10 @@ void Application::updateShadowMap() { // this is what is used for rendering the Entities and avatars Transform viewTransform; viewTransform.setRotation(rotation); + // viewTransform.postTranslate(shadowFrustumCenter); setViewTransform(viewTransform); + glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(1.1f, 4.0f); // magic numbers courtesy http://www.eecs.berkeley.edu/~ravir/6160/papers/shadowmaps.ppt diff --git a/libraries/octree/src/ViewFrustum.cpp b/libraries/octree/src/ViewFrustum.cpp index f92ba0a742..fac017218d 100644 --- a/libraries/octree/src/ViewFrustum.cpp +++ b/libraries/octree/src/ViewFrustum.cpp @@ -577,6 +577,10 @@ void ViewFrustum::computeOffAxisFrustum(float& left, float& right, float& bottom // compute our dimensions the usual way float hheight = _nearClip * tanf(_fieldOfView * 0.5f * RADIANS_PER_DEGREE); float hwidth = _aspectRatio * hheight; + if (isOrthographic()) { + hheight = getHeight(); + hwidth = getWidth(); + } // get our frustum corners in view space glm::mat4 eyeMatrix = glm::mat4_cast(glm::inverse(_eyeOffsetOrientation)) * glm::translate(-_eyeOffsetPosition); @@ -868,18 +872,18 @@ float ViewFrustum::distanceToCamera(const glm::vec3& point) const { void ViewFrustum::evalProjectionMatrix(glm::mat4& proj) const { if (isOrthographic()) { - float left, right, bottom, top, near, far; - glm::vec4 clip0, clip1; - computeOffAxisFrustum(left, right, bottom, top, near, far, clip0, clip1); + glm::vec3 frustumCenter = glm::inverse( _orientation) * _position; - proj = glm::ortho(left, right, bottom, top); - proj = glm::ortho(-0.5f * getWidth(), +0.5f * getWidth(), -0.5f * getWidth(), +0.5f * getWidth()); + proj = glm::ortho(frustumCenter.x -0.5f * getWidth(), + frustumCenter.x +0.5f * getWidth(), + frustumCenter.y -0.5f * getHeight(), + frustumCenter.y +0.5f * getHeight(), + -getFarClip(), -getNearClip()); } else { float left, right, bottom, top, near, far; glm::vec4 clip0, clip1; computeOffAxisFrustum(left, right, bottom, top, near, far, clip0, clip1); proj = glm::perspective(glm::radians(getFieldOfView()), getAspectRatio(), getNearClip(), getFarClip()); - // proj = glm::perspective(getFieldOfView() * 0.5f, getAspectRatio(), getNearClip(), getFarClip()); } } diff --git a/libraries/render-utils/src/Shadow.slh b/libraries/render-utils/src/Shadow.slh index a8d0ce457a..d4c19915ff 100755 --- a/libraries/render-utils/src/Shadow.slh +++ b/libraries/render-utils/src/Shadow.slh @@ -31,6 +31,8 @@ uniform vec3 shadowDistances; // the inverse of the size of the shadow map uniform float shadowScale; +uniform mat4 shadowMatrices[4]; + vec2 samples[8] = vec2[8]( vec2(-2.0, -2.0), vec2(2.0, -2.0), diff --git a/libraries/render-utils/src/directional_ambient_light_shadow_map.slf b/libraries/render-utils/src/directional_ambient_light_shadow_map.slf index 7f098eac2e..3407cb03e3 100755 --- a/libraries/render-utils/src/directional_ambient_light_shadow_map.slf +++ b/libraries/render-utils/src/directional_ambient_light_shadow_map.slf @@ -46,4 +46,6 @@ void main(void) { gl_FragColor = vec4(color, frag.normalVal.a); } + + gl_FragColor = vec4(vec3(shadowAttenuation), 1.0); }