1

I am trying to render a scene (bike on a platform shown below) with shadow using Qt's OpenGL classes.

Screenshot of bike model without shadows

I realize that we would need to make two passes for this.

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.

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

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

    m_shadowFBO->bind();

    glViewport(0, 0, m_shadowFBO->width(), m_shadowFBO->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<m_models.size()-1; i++)
    {
        ObjModel *model = m_models.at(i);
        model->setRenderMode(ObjModel::ShadowMode);
        model->render(m_projectionMatrix, m_lightViewMatrix);
    }

    m_shadowFBO->release();

    return m_shadowFBO->texture();
}

Each model is rendered using

m_shader->bind();
model->m_vertexBuffer->bind();
model->m_indexBuffer->bind();

const QMatrix4x4 lightViewProjectionMatrix = projectionMatrix *
lightViewMatrix * model->m_matrix;

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

m_shader->setUniformValue("qt_LightViewProjectionMatrix",
lightViewProjectionMatrix);

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

model->m_indexBuffer->release();
model->m_vertexBuffer->release();
m_shader->release();

with vertex shader

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);
}

and fragment shader

void main(void)
{
    gl_FragDepth = gl_FragCoord.z;
}

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

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 > 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;
}

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

Can someone please point to me where I am going wrong please?

1 Answer 1

1

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

The complete code can be downloaded from here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.