将DepthBuffer和TextureBuffer发送到同一片段OpenGL3

Send Depthbuffer and TextureBuffer to same Fragment OpenGL3

本文关键字:片段 OpenGL3 DepthBuffer TextureBuffer      更新时间:2023-10-16

这可能是愚蠢的,而且一个琐碎的问题着色器。
我的FBO使用1个纹理缓冲区和1个深度缓冲区生成

   glGenTextures(1, &texture_color);
   glBindTexture(GL_TEXTURE_2D, texture_color);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);    
   glGenTextures(1, &texture_depth);
   glBindTexture(GL_TEXTURE_2D, texture_depth);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);

我想在我的片段缓冲区中具有

uniform sampler2D colorTexture;
uniform sampler2D depthTexture;

要填充颜色和深度纹理...

我只能将一个或另一个带有glbindTexture(并且效果很好),但是我不成功地同时发送2次。有人知道该怎么做吗?

谢谢!

好吧,我可能不是巨人,因为解决方案很容易,我只是做错了。

我试图使用glactiveTexture(gl_depth),然后对我的纹理进行绑定:( glbindTexture(gl_texture_2d,vistibilityfbo.getDepthTexture());)认为使用GL_DEPTH。

但是我错了,它使用以下方式工作:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, visibilityFBO.getColorTexture()); 
glUniform1i(glGetUniformLocation(visibilityPassShader.Program, "positionTexture"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, visibilityFBO.getDepthTexture());
glUniform1i(glGetUniformLocation(visibilityPassShader.Program, "depthTexture"), 1);

与以前相同,但是深度被考虑为纹理,因为我使用glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);来产生深度纹理。因此,我只需要调用下一个纹理并将其绑定才能使用它。

在片段中仍然是:

uniform sampler2D positionTexture;
uniform sampler2D depthTexture;

按良好的顺序。

使用OpenGl> = 4,您可以在片段着色器

中使用
layout (binding = 0) uniform sampler2D positionTexture;
layout (binding = 1) uniform sampler2D depthTexture;

并原谅源代码中有关gluuniform1i的部分。

希望它可以帮助某人!