enhanced texture class, added PCF to shadowmapping

This commit is contained in:
Anselme FRANÇOIS 2016-06-11 13:50:58 +02:00
parent bca6c8c13f
commit cbefe4f86e
4 changed files with 20 additions and 11 deletions

View File

@ -38,7 +38,7 @@ in vec3 varNormal;
#endif #endif
#ifdef SHADOWMAP #ifdef SHADOWMAP
uniform sampler2D shadowMap; uniform sampler2DShadow shadowMap;
in vec4 posInLightSpace; in vec4 posInLightSpace;
#endif #endif
@ -99,7 +99,8 @@ void main(void) {
#endif #endif
#ifdef SHADOWMAP #ifdef SHADOWMAP
float shadow = computeShadow(shadowMap, posInLightSpace.xyz/posInLightSpace.w); posInLightSpace.z = posInLightSpace.z - 0.001;
float shadow = texture(shadowMap, posInLightSpace.xyz/posInLightSpace.w);
#else #else
float shadow = 1; float shadow = 1;
#endif #endif

View File

@ -83,7 +83,7 @@ void Light::initShadowMap(int resWidth, int resHeight, glm::vec3 dim)
{ {
shadowMapWidth = resWidth; shadowMapWidth = resWidth;
shadowMapHeight = resHeight; shadowMapHeight = resHeight;
viewMatrix = glm::lookAt(position, position+direction, glm::vec3(0, 1, 0)); viewMatrix = glm::lookAt(position, position-direction, glm::vec3(0, 1, 0));
if(type == DIRECTIONNAL) if(type == DIRECTIONNAL)
projectionMatrix = glm::ortho(-dim.x/2, dim.x/2, -dim.y/2, dim.y/2, -dim.z/2, dim.z/2); projectionMatrix = glm::ortho(-dim.x/2, dim.x/2, -dim.y/2, dim.y/2, -dim.z/2, dim.z/2);
else else
@ -95,6 +95,8 @@ void Light::initShadowMap(int resWidth, int resHeight, glm::vec3 dim)
Texture* tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, resWidth, resHeight, GL_FLOAT); Texture* tex = new Texture(GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT, resWidth, resHeight, GL_FLOAT);
tex->setFiltering(GL_LINEAR); tex->setFiltering(GL_LINEAR);
tex->setWrap(GL_CLAMP_TO_EDGE); tex->setWrap(GL_CLAMP_TO_EDGE);
tex->setParameter(GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
tex->setParameter(GL_TEXTURE_COMPARE_FUNC, GL_LESS);
shadowMap->addTexture(tex, GL_DEPTH_ATTACHMENT); shadowMap->addTexture(tex, GL_DEPTH_ATTACHMENT);
shadowMap->initColorAttachments(); shadowMap->initColorAttachments();
@ -116,7 +118,7 @@ void Light::generateShadowMap(Scene* scene)
glClear(GL_DEPTH_BUFFER_BIT); glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
glCullFace(GL_FRONT); //glCullFace(GL_FRONT);
for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry(); for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
geometryIt->isValid(); geometryIt->next()) geometryIt->isValid(); geometryIt->next())
{ {
@ -131,7 +133,7 @@ void Light::generateShadowMap(Scene* scene)
node->mesh->draw(shaders[hasAlpha], false, hasAlpha, false); node->mesh->draw(shaders[hasAlpha], false, hasAlpha, false);
} }
} }
glCullFace(GL_BACK); //glCullFace(GL_BACK);
} }
Texture* Light::getShadowMap() Texture* Light::getShadowMap()

View File

@ -122,22 +122,27 @@ void Texture::initPixels(Image* myImage, GLenum target)
void Texture::setWrap(GLint wrap) void Texture::setWrap(GLint wrap)
{ {
glTexParameteri(m_target, GL_TEXTURE_WRAP_S, wrap); setParameter(GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(m_target, GL_TEXTURE_WRAP_T, wrap); setParameter(GL_TEXTURE_WRAP_T, wrap);
glTexParameteri(m_target, GL_TEXTURE_WRAP_R, wrap); setParameter(GL_TEXTURE_WRAP_R, wrap);
} }
void Texture::setFiltering(GLint filter) void Texture::setFiltering(GLint filter)
{ {
glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, filter); setParameter(GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, filter); setParameter(GL_TEXTURE_MAG_FILTER, filter);
}
void Texture::setParameter(GLenum parameter, GLenum value)
{
glTextureParameteri(m_texId, parameter, value);
} }
void Texture::createMipMaps() void Texture::createMipMaps()
{ {
m_hasMipMaps = true; m_hasMipMaps = true;
glGenerateMipmap(m_target); glGenerateMipmap(m_target);
glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); setParameter(GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
} }
void Texture::bind(int slot) void Texture::bind(int slot)

View File

@ -45,6 +45,7 @@ public:
GLenum getTarget() {return m_target;} GLenum getTarget() {return m_target;}
void setWrap(GLint wrap); void setWrap(GLint wrap);
void setFiltering(GLint filter); void setFiltering(GLint filter);
void setParameter(GLenum parameter, GLenum value);
void createMipMaps(); void createMipMaps();
bool isCubeMap() {return m_target == GL_TEXTURE_CUBE_MAP;} bool isCubeMap() {return m_target == GL_TEXTURE_CUBE_MAP;}
bool hasMipMaps() {return m_hasMipMaps;} bool hasMipMaps() {return m_hasMipMaps;}