我试图通过从标准GL移植some code来在iOS上的OpenGL ES 2.0中使用一些阴影效果 . 部分示例涉及将深度缓冲区复制到纹理:
glBindTexture(GL_TEXTURE_2D, g_uiDepthBuffer);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, 800, 600, 0);
但是,ES上似乎不支持glCopyTexImage2D . 读一个related thread,似乎我可以使用帧缓冲区和片段着色器来提取深度数据 . 所以我试图将深度组件写入颜色缓冲区,然后复制它:
// clear everything
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// turn on depth rendering
glUseProgram(m_BaseShader.uiId);
// this is a switch to cause the fragment shader to just dump out the depth component
glUniform1i(uiBaseShaderRenderDepth, true);
// and for this, the color buffer needs to be on
glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
// and clear it to 1.0, like how the depth buffer starts
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// draw the scene
DrawScene();
// bind our texture
glBindTexture(GL_TEXTURE_2D, g_uiDepthBuffer);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
这是片段着色器:
uniform sampler2D sTexture;
uniform bool bRenderDepth;
varying lowp float LightIntensity;
varying mediump vec2 TexCoord;
void main()
{
if(bRenderDepth) {
gl_FragColor = vec4(vec3(gl_FragCoord.z), 1.0);
} else {
gl_FragColor = vec4(texture2D(sTexture, TexCoord).rgb * LightIntensity, 1.0);
}
}
我已经尝试过没有“bRenderDepth”分支,并且它没有显着加快速度 .
现在几乎只是以14fps执行此步骤,这显然是不可接受的 . 如果我以30fps以上的方式将副本拉出来 . 我在复制命令上从Xcode OpenGLES分析器得到两个建议:
-
file://localhost/Users/xxxx/Documents/Development/xxxx.mm:错误:验证错误:glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,0,0,960,640,0):高度<640>不是两个人的力量
-
file://localhost/Users/xxxx/Documents/Development/xxxx.mm:警告:GPU等待纹理:您的应用更新了当前用于渲染的纹理 . 这导致CPU等待GPU完成渲染 .
我将努力解决上述两个问题(也许它们是关键所在) . 在此期间,任何人都可以建议一种更有效的方法将深度数据拉入纹理吗?
提前致谢!
1 回答
iOS设备通常支持OES_depth_texture,因此在存在扩展名的设备上,您可以设置一个深度纹理作为唯一附件的帧缓冲对象:
然后,当您绘制场景时,您的纹理会接收写入深度缓冲区的所有值(您可以使用一个简单的片段着色器),您可以直接从它进行纹理而无需调用
glCopyTexImage2D
.