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
#ifdef SHADOWMAP
uniform sampler2D shadowMap;
uniform sampler2DShadow shadowMap;
in vec4 posInLightSpace;
#endif
@ -99,7 +99,8 @@ void main(void) {
#endif
#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
float shadow = 1;
#endif

View File

@ -83,7 +83,7 @@ void Light::initShadowMap(int resWidth, int resHeight, glm::vec3 dim)
{
shadowMapWidth = resWidth;
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)
projectionMatrix = glm::ortho(-dim.x/2, dim.x/2, -dim.y/2, dim.y/2, -dim.z/2, dim.z/2);
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);
tex->setFiltering(GL_LINEAR);
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->initColorAttachments();
@ -116,7 +118,7 @@ void Light::generateShadowMap(Scene* scene)
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glCullFace(GL_FRONT);
//glCullFace(GL_FRONT);
for(SceneIterator<GeometryNode*>* geometryIt = scene->getGeometry();
geometryIt->isValid(); geometryIt->next())
{
@ -131,7 +133,7 @@ void Light::generateShadowMap(Scene* scene)
node->mesh->draw(shaders[hasAlpha], false, hasAlpha, false);
}
}
glCullFace(GL_BACK);
//glCullFace(GL_BACK);
}
Texture* Light::getShadowMap()

View File

@ -122,22 +122,27 @@ void Texture::initPixels(Image* myImage, GLenum target)
void Texture::setWrap(GLint wrap)
{
glTexParameteri(m_target, GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(m_target, GL_TEXTURE_WRAP_T, wrap);
glTexParameteri(m_target, GL_TEXTURE_WRAP_R, wrap);
setParameter(GL_TEXTURE_WRAP_S, wrap);
setParameter(GL_TEXTURE_WRAP_T, wrap);
setParameter(GL_TEXTURE_WRAP_R, wrap);
}
void Texture::setFiltering(GLint filter)
{
glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, filter);
setParameter(GL_TEXTURE_MIN_FILTER, filter);
setParameter(GL_TEXTURE_MAG_FILTER, filter);
}
void Texture::setParameter(GLenum parameter, GLenum value)
{
glTextureParameteri(m_texId, parameter, value);
}
void Texture::createMipMaps()
{
m_hasMipMaps = true;
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)

View File

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