问题描述
我正在尝试替换OpenGL ES 2.0中缺少的OpenGL gl_FragDepth
功能.
I'm trying to replace OpenGL's gl_FragDepth
feature which is missing in OpenGL ES 2.0.
我需要一种在片段着色器中设置深度的方法,因为在顶点着色器中设置深度不足以达到我的目的. AFAIK唯一的方法是通过具有渲染到纹理的帧缓冲区,并在该缓冲区上完成第一次渲染.此深度纹理存储屏幕上每个像素的深度值.然后,将深度纹理附加到最终渲染过程中,以便最终渲染器知道每个像素的深度.
I need a way to set the depth in the fragment shader, because setting it in the vertex shader is not accurate enough for my purpose. AFAIK the only way to do that is by having a render-to-texture framebuffer on which a first rendering pass is done. This depth texture stores the depth values for each pixel on the screen. Then, the depth texture is attached in the final rendering pass, so the final renderer knows the depth at each pixel.
由于iOS> = 4.1支持GL_OES_depth_texture
,因此我尝试使用GL_DEPTH_COMPONENT24
或GL_DEPTH_COMPONENT16
作为深度纹理.我正在使用以下调用来创建纹理:
Since iOS >= 4.1 supports GL_OES_depth_texture
, I'm trying to use GL_DEPTH_COMPONENT24
or GL_DEPTH_COMPONENT16
for the depth texture. I'm using the following calls to create the texture:
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, textureId, 0);
帧缓冲区创建成功,但是我不知道如何进行.我对附加到帧缓冲区的深度纹理缺乏基本的了解.
The framebuffer creation succeeds, but I don't know how to proceed. I'm lacking some fundamental understanding of depth textures attached to framebuffers.
- 我应该在片段着色器中输出什么值?我的意思是,即使纹理是深度纹理,
gl_FragColor
仍然是RGBA值.我无法在片段着色器中设置深度,因为OpenGL ES 2.0中缺少gl_FragDepth
- 如何在最终渲染过程中从深度纹理中读取深度纹理(深度纹理以
sampler2D
的形式附加)? - 如果将
glTexImage2D
的第三个参数设置为GL_DEPTH_COMPONENT16
,GL_DEPTH_COMPONENT16_OES
或GL_DEPTH_COMPONENT24_OES
,为什么会得到不完整的帧缓冲区? - 将纹理附加到
GL_DEPTH_ATTACHMENT
是否正确?如果将其更改为GL_COLOR_ATTACHMENT0
,则表示帧缓冲区不完整.
- What values should I output in the fragment shader? I mean,
gl_FragColor
is still an RGBA value, even though the texture is a depth texture. I cannot set the depth in the fragment shader, sincegl_FragDepth
is missing in OpenGL ES 2.0 - How can I read from the depth texture in the final rendering pass, where the depth texture is attached as a
sampler2D
? - Why do I get an incomplete framebuffer if I set the third argument of
glTexImage2D
toGL_DEPTH_COMPONENT16
,GL_DEPTH_COMPONENT16_OES
orGL_DEPTH_COMPONENT24_OES
? - Is it right to attach the texture to the
GL_DEPTH_ATTACHMENT
? If I'm changing that toGL_COLOR_ATTACHMENT0
, I'm getting an incomplete framebuffer.
推荐答案
深度纹理不会影响片段着色器的输出.渲染到深度纹理中时,该值最终将是固定功能深度值.
Depth textures do not affect the output of the fragment shader. The value that ends up in the depth texture when you're rendering to it will be the fixed-function depth value.
因此,如果没有gl_FragDepth,您将无法真正在片段着色器中设置深度".但是,您可以按照自己的描述进行操作,例如,一次通过渲染纹理的深度,然后在以后通过读取访问该值.
So without gl_FragDepth, you can't really "set the depth in the fragment shader". You can, however, do what you describe, i.e., render depth to a texture in one pass and then read access that value in a later pass.
您可以使用内置的texture2D函数从深度纹理中读取内容,就像常规颜色纹理一样.您返回的值将是(d,d,d,1.0).
You can read from a depth texture using the texture2D built-in function just like for regular color textures. The value you get back will be (d, d, d, 1.0).
根据深度纹理扩展规范,GL_DEPTH_COMPONENT16_OES和不支持将GL_DEPTH_COMPONENT24_OES作为深度纹理的内部格式.我希望这会产生错误.您得到的不完整的帧缓冲区状态可能与此有关.
According to the depth texture extension specification, GL_DEPTH_COMPONENT16_OES and GL_DEPTH_COMPONENT24_OES are not supported as internal formats for depth textures. I'd expect this to generate an error. The incomplete framebuffer status you get is probably related to this.
将纹理附加到GL_DEPTH_ATTACHMENT是正确的.
It is correct to attach the texture to the GL_DEPTH_ATTACHMENT.
这篇关于渲染到深度纹理-有关GL_OES_depth_texture使用的不确定性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!