Fix issue with shadow mapping
This commit is contained in:
parent
b5f1557bd6
commit
6884adf0c2
|
@ -27,7 +27,7 @@
|
||||||
#define MATRIX4_H
|
#define MATRIX4_H
|
||||||
|
|
||||||
// Libraries
|
// Libraries
|
||||||
#include <math.h>
|
#include <cmath>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Vector3.h"
|
#include "Vector3.h"
|
||||||
|
@ -376,6 +376,15 @@ class Matrix4 {
|
||||||
|
|
||||||
// Return a 4x4 rotation matrix
|
// Return a 4x4 rotation matrix
|
||||||
static Matrix4 rotationMatrix(const Vector3& axis, float angle);
|
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
|
// * operator
|
||||||
|
@ -422,6 +431,53 @@ inline Matrix4 Matrix4::rotationMatrix(const Vector3& axis, float angle) {
|
||||||
return rotationMatrix;
|
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
|
#endif //_MATRIX4_H
|
||||||
|
|
|
@ -71,9 +71,8 @@ void main() {
|
||||||
|
|
||||||
// Compute shadow factor
|
// Compute shadow factor
|
||||||
vec4 shadowMapCoordsOverW = shadowMapCoords / shadowMapCoords.w ;
|
vec4 shadowMapCoordsOverW = shadowMapCoords / shadowMapCoords.w ;
|
||||||
shadowMapCoordsOverW += 0.0005;
|
//shadowMapCoordsOverW += 0.0005;
|
||||||
vec2 shadowMapCoordsWithBias = (shadowMapCoordsOverW.xy + vec2(1, 1)) * 0.5;
|
float distanceInShadowMap = texture(shadowMapSampler, shadowMapCoordsOverW.xy).r;
|
||||||
float distanceInShadowMap = texture(shadowMapSampler, shadowMapCoordsWithBias).r;
|
|
||||||
float shadow = 0.0;
|
float shadow = 0.0;
|
||||||
if (shadowMapCoords.w > 0) {
|
if (shadowMapCoords.w > 0) {
|
||||||
shadow = distanceInShadowMap < shadowMapCoordsOverW.z ? 0.0 : 1.0;
|
shadow = distanceInShadowMap < shadowMapCoordsOverW.z ? 0.0 : 1.0;
|
||||||
|
@ -81,5 +80,4 @@ void main() {
|
||||||
|
|
||||||
// Compute the final color
|
// Compute the final color
|
||||||
color = vec4(ambient + shadow * vertexColor.rgb, 1.0);
|
color = vec4(ambient + shadow * vertexColor.rgb, 1.0);
|
||||||
//color = vec4(distanceInShadowMap, distanceInShadowMap, distanceInShadowMap, 1.0);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,6 +47,12 @@ SceneDemo::SceneDemo(const std::string& name, float sceneRadius) : Scene(name),
|
||||||
mShadowMapLightCamera.setSceneRadius(200);
|
mShadowMapLightCamera.setSceneRadius(200);
|
||||||
//mShadowMapLightCamera.setZoom(1.0);
|
//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
|
// Create the Shadow map FBO and texture
|
||||||
createShadowMapFBOAndTexture();
|
createShadowMapFBOAndTexture();
|
||||||
|
|
||||||
|
@ -115,7 +121,7 @@ void SceneDemo::render() {
|
||||||
|
|
||||||
// Set the variables of the shader
|
// Set the variables of the shader
|
||||||
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
|
mPhongShader.setMatrix4x4Uniform("projectionMatrix", mCamera.getProjectionMatrix());
|
||||||
mPhongShader.setMatrix4x4Uniform("shadowMapProjectionMatrix", shadowMapProjMatrix);
|
mPhongShader.setMatrix4x4Uniform("shadowMapProjectionMatrix", mShadowMapBiasMatrix * shadowMapProjMatrix);
|
||||||
mPhongShader.setMatrix4x4Uniform("worldToLight0CameraMatrix", worldToLightCameraMatrix);
|
mPhongShader.setMatrix4x4Uniform("worldToLight0CameraMatrix", worldToLightCameraMatrix);
|
||||||
mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
|
mPhongShader.setVector3Uniform("light0PosCameraSpace", worldToCameraMatrix * mLight0.getOrigin());
|
||||||
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
|
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
|
||||||
|
@ -148,7 +154,7 @@ void SceneDemo::render() {
|
||||||
void SceneDemo::createShadowMapFBOAndTexture() {
|
void SceneDemo::createShadowMapFBOAndTexture() {
|
||||||
|
|
||||||
// Create the texture for the depth values
|
// 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);
|
GL_UNSIGNED_BYTE, GL_NEAREST, GL_NEAREST, GL_CLAMP, GL_CLAMP, NULL);
|
||||||
mShadowMapTexture.setLayer(1);
|
mShadowMapTexture.setLayer(1);
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,9 @@ class SceneDemo : public Scene {
|
||||||
/// Shadow map texture
|
/// Shadow map texture
|
||||||
openglframework::Texture2D mShadowMapTexture;
|
openglframework::Texture2D mShadowMapTexture;
|
||||||
|
|
||||||
|
/// Shadow map bias matrix
|
||||||
|
openglframework::Matrix4 mShadowMapBiasMatrix;
|
||||||
|
|
||||||
/// Camera at light0 position for the shadow map
|
/// Camera at light0 position for the shadow map
|
||||||
openglframework::Camera mShadowMapLightCamera;
|
openglframework::Camera mShadowMapLightCamera;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user