如何使用 QOpenGLFrameBufferObject 进行阴影映射?
Posted
技术标签:
【中文标题】如何使用 QOpenGLFrameBufferObject 进行阴影映射?【英文标题】:How to use QOpenGLFrameBufferObject for shadow mapping? 【发布时间】:2016-02-01 15:30:06 【问题描述】:我正在尝试使用 Qt 的 OpenGL 类渲染带有阴影的场景(如下所示平台上的自行车)。
Screenshot of bike model without shadows
我意识到我们需要为此进行两次传递。
从灯光的角度渲染深度图的第一遍 第二个用于从相机的角度渲染场景,其中 片段着色器使用深度图作为纹理来确定是否 碎片是否在阴影内。
为了捕捉深度图,我从灯光渲染场景 视点成帧缓冲如下
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();
每个模型都使用
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();
带顶点着色器
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);
和片段着色器
void main(void)
gl_FragDepth = gl_FragCoord.z;
然后为了在第二遍中使用阴影纹理,我在 第二遍的片段着色器。
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;
但以上所有似乎都没有区别。我看不到任何 第二遍中的阴影。
谁能指出我哪里出错了?
【问题讨论】:
【参考方案1】:由于使用 QOpenGLFrameBufferObject 没有问题,我必须自己使用 gl 函数调用来创建缓冲区,如下所示。
// 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);
然后我通过绑定阴影纹理来渲染场景。片段着色器代码必须稍作更新,以确定片段是位于阴影内还是阴影外。
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
【讨论】:
【参考方案2】:编辑:显然还不能为这个用例使用 QOpenGLFrameBufferObject (http://lists.qt-project.org/pipermail/interest/2016-February/020786.html)
【讨论】:
以上是关于如何使用 QOpenGLFrameBufferObject 进行阴影映射?的主要内容,如果未能解决你的问题,请参考以下文章
如何在自动布局中使用约束标识符以及如何使用标识符更改约束? [迅速]
如何使用 AngularJS 的 ng-model 创建一个数组以及如何使用 jquery 提交?