How to use QOpenGLFrameBufferObject for shadow mapping?
<p>I am trying to render a scene (bike on a platform shown below) with shadow using Qt's OpenGL classes.</p>

<p><a href="http://i.stack.imgur.com/mfwhy.png" rel="nofollow">Screenshot of bike model without shadows</a></p>

<p>I realize that we would need to make two passes for this.</p>

<p>The first pass for rendering a depth-map from the light's point of view and
the second for rendering the scene from the camera's point of view where
the fragment shader uses the depth-map as a texture for determining whether
fragments are inside the shadow or not.</p>

<p>For capturing the depth map, I am rendering the scene from the light's
point of view into a framebuffer as follows</p>

<pre><code>uint ShadowRenderWindow::renderToShadowMap()
{
    if(!m_shadowFBO)
    {
        m_shadowFBO = new QOpenGLFramebufferObject(1024,
1024, QOpenGLFramebufferObject::Depth);
        m_shadowFBO-&gt;bind();
        glDrawBuffer(GL_NONE);
        glReadBuffer(GL_NONE);
        m_shadowFBO-&gt;release();
    }

    m_shadowFBO-&gt;bind();

    glViewport(0, 0, m_shadowFBO-&gt;width(), m_shadowFBO-&gt;height());
    glEnable(GL_DEPTH_TEST);
    glClear(GL_DEPTH_BUFFER_BIT);

    m_lightViewMatrix.setToIdentity();
    m_lightViewMatrix.lookAt(
      m_lightPositionMatrix.map( QVector3D(0,0,0) ),
      m_sceneBounds.center(),
      m_lightPositionMatrix.map( QVector3D(0,1,0) ).normalized() );

    for(int i=0; i&lt;m_models.size()-1; i++)
    {
        ObjModel *model = m_models.at(i);
        model-&gt;setRenderMode(ObjModel::ShadowMode);
        model-&gt;render(m_projectionMatrix, m_lightViewMatrix);
    }

    m_shadowFBO-&gt;release();

    return m_shadowFBO-&gt;texture();
}
</code></pre>

<p>Each model is rendered using</p>

<pre><code>m_shader-&gt;bind();
model-&gt;m_vertexBuffer-&gt;bind();
model-&gt;m_indexBuffer-&gt;bind();

const QMatrix4x4 lightViewProjectionMatrix = projectionMatrix *
lightViewMatrix * model-&gt;m_matrix;

m_shader-&gt;enableAttributeArray("qt_Vertex");
m_shader-&gt;setAttributeBuffer("qt_Vertex", GL_FLOAT, 0, 3, 0);

m_shader-&gt;setUniformValue("qt_LightViewProjectionMatrix",
lightViewProjectionMatrix);

Q_FOREACH(ObjModel::Part part, model-&gt;m_parts)
{
    const uint offset = part.start * sizeof(uint);
    glDrawElements(part.type, part.length,
         GL_UNSIGNED_INT, (void*)offset);
}

model-&gt;m_indexBuffer-&gt;release();
model-&gt;m_vertexBuffer-&gt;release();
m_shader-&gt;release();
</code></pre>

<p>with vertex shader</p>

<pre><code>attribute vec3 qt_Vertex;
uniform mat4 qt_LightViewProjectionMatrix;
const float c_one = 1.0;

void main(void)
{
    gl_Position = qt_LightViewProjectionMatrix * vec4(qt_Vertex, c_one);
}
</code></pre>

<p>and fragment shader</p>

<pre><code>void main(void)
{
    gl_FragDepth = gl_FragCoord.z;
}
</code></pre>

<p>And then for using the shadow texture in the second pass, I do this in the
fragment shader of the second pass.</p>

<pre><code>float evaluateShadow(in vec4 shadowPos)
{
    vec3 shadowCoords = shadowPos.xyz / shadowPos.w;
    shadowCoords = shadowCoords * 0.5 + 0.5;

    float closestDepth = texture2D(qt_ShadowMap, shadowCoords.xy).r;
    float currentDepth = shadowPos.z;
    float shadow = (currentDepth &gt; closestDepth) ? 1.0 : 0.5;

    return shadow;
}

void main(void)
{
    vec4 lmColor = evaluateLightMaterialColor(v_Normal);
    if(qt_ShadowEnabled == true)
    {
        float shadow = evaluateShadow(v_ShadowPosition);
        gl_FragColor = vec4(lmColor.xyz * shadow, qt_Material.opacity);
    }
    else
        gl_FragColor = lmColor;
}
</code></pre>

<p>But all of the above seems to make no difference. I am unable to see any
shadow in the second pass.</p>

<p>Can someone please point to me where I am going wrong please?</p>