Fix issue with shadow mapping

This commit is contained in:
Daniel Chappuis 2015-07-26 22:30:45 +00:00
parent b5f1557bd6
commit 6884adf0c2
4 changed files with 70 additions and 7 deletions

View File

@ -27,7 +27,7 @@
#define MATRIX4_H
// Libraries
#include <math.h>
#include <cmath>
#include <assert.h>
#include <iostream>
#include "Vector3.h"
@ -376,6 +376,15 @@ class Matrix4 {
// Return a 4x4 rotation matrix
static Matrix4 rotationMatrix(const Vector3& axis, float angle);
// Return a 4x4 perspective projection matrix
static Matrix4 perspectiveProjectionMatrix(float near, float far,
int width, int height,
float fieldOfView);
// Return a 4x4 orthographic projection matrix
static Matrix4 orthoProjectionMatrix(float near, float far, int width,
int height);
};
// * operator
@ -422,6 +431,53 @@ inline Matrix4 Matrix4::rotationMatrix(const Vector3& axis, float angle) {
return rotationMatrix;
}
// Return a 4x4 perspective projection matrix
inline Matrix4 Matrix4::perspectiveProjectionMatrix(float near, float far, int width, int height,
float fieldOfView) {
// Compute the aspect ratio
float aspect = float(width) / float(height);
float top = near * tan((fieldOfView / 2.0f) * (float(M_PI) / 180.0f));
float bottom = -top;
float left = bottom * aspect;
float right = top * aspect;
float fx = 2.0f * near / (right - left);
float fy = 2.0f * near / (top - bottom);
float fz = -(far + near) / (far - near);
float fw = -2.0f * far * near / (far - near);
// Compute the projection matrix
return Matrix4(fx, 0, 0, 0,
0, fy, 0, 0,
0, 0, fz, fw,
0, 0, -1, 0);
}
// Return a 4x4 orthographic projection matrix
inline Matrix4 Matrix4::orthoProjectionMatrix(float near, float far, int width, int height) {
// Compute the aspect ratio
float aspect = float(width) / float(height);
float top = height * 0.5f;
float bottom = -top;
float left = bottom * aspect;
float right = top * aspect;
float fx = 2.0f / (right - left);
float fy = 2.0f / (top - bottom);
float fz = -2.0f / (far - near);
float fw = -(far + near) / (far - near);
// Compute the projection matrix
return Matrix4(fx, 0, 0, 0,
0, fy, 0, 0,
0, 0, fz, fw,
0, 0, 0, 1);
}
}
#endif //_MATRIX4_H

View File

@ -71,9 +71,8 @@ void main() {
// Compute shadow factor
vec4 shadowMapCoordsOverW = shadowMapCoords / shadowMapCoords.w ;
shadowMapCoordsOverW += 0.0005;
vec2 shadowMapCoordsWithBias = (shadowMapCoordsOverW.xy + vec2(1, 1)) * 0.5;
float distanceInShadowMap = texture(shadowMapSampler, shadowMapCoordsWithBias).r;
//shadowMapCoordsOverW += 0.0005;
float distanceInShadowMap = texture(shadowMapSampler, shadowMapCoordsOverW.xy).r;
float shadow = 0.0;
if (shadowMapCoords.w > 0) {
shadow = distanceInShadowMap < shadowMapCoordsOverW.z ? 0.0 : 1.0;
@ -81,5 +80,4 @@ void main() {
// Compute the final color
color = vec4(ambient + shadow * vertexColor.rgb, 1.0);
//color = vec4(distanceInShadowMap, distanceInShadowMap, distanceInShadowMap, 1.0);
}

View File

@ -47,6 +47,12 @@ SceneDemo::SceneDemo(const std::string& name, float sceneRadius) : Scene(name),
mShadowMapLightCamera.setSceneRadius(200);
//mShadowMapLightCamera.setZoom(1.0);
mShadowMapBiasMatrix.setAllValues(0.5, 0.0, 0.0, 0.5,
0.0, 0.5, 0.0, 0.5,
0.0, 0.0, 0.5, 0.5,
0.0, 0.0, 0.0, 1.0);
// Create the Shadow map FBO and texture
createShadowMapFBOAndTexture();
@ -115,7 +121,7 @@ void SceneDemo::render() {
// Set the variables of the shader
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
mPhongShader.setMatrix4x4Uniform("shadowMapProjectionMatrix", shadowMapProjMatrix);
mPhongShader.setMatrix4x4Uniform("shadowMapProjectionMatrix", mShadowMapBiasMatrix * shadowMapProjMatrix);
mPhongShader.setMatrix4x4Uniform("worldToLight0CameraMatrix", worldToLightCameraMatrix);
mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
@ -148,7 +154,7 @@ void SceneDemo::render() {
void SceneDemo::createShadowMapFBOAndTexture() {
// Create the texture for the depth values
mShadowMapTexture.create(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT,
mShadowMapTexture.create(SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT,
GL_UNSIGNED_BYTE, GL_NEAREST, GL_NEAREST, GL_CLAMP, GL_CLAMP, NULL);
mShadowMapTexture.setLayer(1);

View File

@ -51,6 +51,9 @@ class SceneDemo : public Scene {
/// Shadow map texture
openglframework::Texture2D mShadowMapTexture;
/// Shadow map bias matrix
openglframework::Matrix4 mShadowMapBiasMatrix;
/// Camera at light0 position for the shadow map
openglframework::Camera mShadowMapLightCamera;