[Interest] Shadow Mapping using QOpenGLFramebufferObject
Prashanth Udupa
prashanth.udupa at gmail.com
Tue Feb 2 06:49:58 CET 2016
Hello,
I figured out a solution to this. I thought I should post it here, because
it may be of some use to others.
Since using QOpenGLFrameBufferObject was out of question, I had to create
the buffer by myself using gl function calls as follows.
// Refer http://learnopengl.com/#!Advanced-Lighting/Shadows/Shadow-Mapping
if(m_shadowMapFBO != 0)
return;
// Create a texture for storing the depth map
glGenTextures(1, &m_shadowMapTex);
glBindTexture(GL_TEXTURE_2D, m_shadowMapTex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT,
NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
GLfloat borderColor[] = { 1.0, 1.0, 1.0, 1.0 };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
// Create a frame-buffer and associate the texture with it.
glGenFramebuffers(1, &m_shadowMapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_shadowMapFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
m_shadowMapTex, 0);
// Let OpenGL know that we are not interested in colors for this buffer
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
// Cleanup for now.
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
Then I rendered the scene by binding the shadow texture. The fragment
shader code had to be updated a bit to determine whether a fragment lies
within a shadow or outside of it.
const float qt_ZNear=0.1;
const float qt_ZFar=1000.0;
float linearizeDepth(float depth)
{
float z = depth * 2.0 - 1.0; // Back to NDC
return (2.0 * qt_ZNear * qt_ZFar) / (qt_ZFar + qt_ZNear - z * (qt_ZFar
- qt_ZNear));
}
float evaluateShadow(in vec4 shadowPos)
{
vec3 shadowCoords = shadowPos.xyz / shadowPos.w;
shadowCoords = shadowCoords * c_half + c_half;
if(shadowCoords.z > c_one)
return c_one;
float closestDepth = linearizeDepth( texture2D(qt_ShadowMap,
shadowCoords.xy).r );
float currentDepth = shadowPos.z;
float shadow = (currentDepth < closestDepth) ? c_one : c_half;
return shadow;
}
With that done, I was now able to render the bike with shadows.
http://i.stack.imgur.com/Rek7c.png
The complete code can be downloaded from here. https://goo.gl/Cf1B3i
Thanks once again Guiseppe!
Best Regards,
Prashanth
On Mon, 1 Feb 2016 at 23:49 Prashanth Udupa <prashanth.udupa at gmail.com>
wrote:
> Hi Guiseppe,
>
> The call "m_shadowFBO->texture();" will not return the depth texture
>> that you need for shadow mapping, but the (useless) color texture that
>> your QOpenGLFramebufferObject contains. There's currently no accessor
>> for the depth texture, nor a way to create a QOGLFBO without a color
>> texture. For this kind of usages it would be better if you roll out your
>> own FBO class :\
>>
>
> Ok. Got it. Thanks :-)
>
> Best Regards,
> Prashanth
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160202/a648694e/attachment.html>
More information about the Interest
mailing list