
[英]What might be the issue with my pipeline to rendering the depth buffer to a texture?
[英]Rendering depth buffer to texture with multiple color attachments
我试图将深度缓冲区写入纹理,以便可以在另一个渲染过程中使用它。 我的片段着色器有多个输出。
#version 330
layout (location=0) out float weight;
layout (location=1) out float mask;
in float faceArea;
in vec2 vTexCoords;
uniform sampler2D triangleArea;
void main()
{
weight = texture( triangleArea , vTexCoords ).x / faceArea;
mask = 1.0;
}
以下是纹理和帧缓冲区初始化代码:
GL_CHECK( glBindTexture( GL_TEXTURE_2D , mPerPixelWeightTextures[ 0 ] ) ) ;
GL_CHECK( glTexImage2D( GL_TEXTURE_2D , 0 , GL_R32F , mWidth , mHeight , 0 , GL_RED , GL_FLOAT , 0 ) ); //mask
GL_CHECK( glBindTexture( GL_TEXTURE_2D , mPerPixelWeightTextures[ 1 ] ) ) ;
GL_CHECK( glTexImage2D( GL_TEXTURE_2D , 0 , GL_R32F , mWidth , mHeight , 0 , GL_RED , GL_FLOAT , 0 ) ); //weight
GL_CHECK( glBindTexture( GL_TEXTURE_2D , mPerPixelWeightTextures[ 2 ] ) ) ;
GL_CHECK( glTexImage2D( GL_TEXTURE_2D , 0 , GL_DEPTH_COMPONENT , mWidth , mHeight , 0 , GL_DEPTH_COMPONENT , GL_FLOAT , 0 ) ); //depth buffer
..................
GL_CHECK( glBindFramebuffer( GL_DRAW_FRAMEBUFFER , mFBO[ 1 ] ) );
GL_CHECK( glFramebufferTexture2D( GL_DRAW_FRAMEBUFFER , GL_COLOR_ATTACHMENT0 , GL_TEXTURE_2D , mPerPixelWeightTextures[ 0 ] , 0 ) );
GL_CHECK( glFramebufferTexture2D( GL_DRAW_FRAMEBUFFER , GL_COLOR_ATTACHMENT1 , GL_TEXTURE_2D , mPerPixelWeightTextures[ 1 ] , 0 ) );
GL_CHECK( glFramebufferTexture2D( GL_DRAW_FRAMEBUFFER , GL_DEPTH_ATTACHMENT , GL_TEXTURE_2D , mPerPixelWeightTextures[ 2 ] , 0 ) );
以下是最终的渲染调用:
GLenum buffers[] = { GL_COLOR_ATTACHMENT0 , GL_COLOR_ATTACHMENT1 };
GL_CHECK( glDrawBuffers(2, buffers) );
GL_CHECK( glDrawElements( GL_TRIANGLES , mIndices.size() , GL_UNSIGNED_INT , 0 ) );
GL_CHECK( glBindTexture( GL_TEXTURE_2D , mPerPixelWeightTextures[ 2 ] ) );
GL_CHECK( glGetTexImage( GL_TEXTURE_2D, 0 , GL_DEPTH_COMPONENT , GL_FLOAT , mUsedPixelFlag.data ) );
cv::Mat depthMap( mHeight , mWidth , CV_8UC1 );
depthMap.setTo(cv::Scalar(255));
for( int rr = 0; rr < mHeight ; rr++ )
for( int cc = 0; cc < mWidth ; cc++ )
{
depthMap.at< uchar >(rr, cc) = ( 255 * mUsedPixelFlag.at< float >( rr , cc ) );
}
cv::imwrite( "depthmap.jpg" , mask);
当我访问对应于深度缓冲区的纹理的值时,它的值为0。(还要注意,片段着色器很好,我已经测试了颜色附加的值,它们也很好。)。
我在这里犯一些概念上的错误吗?...我希望深度缓冲区会自动写入GL_DEPTH_ATTACHMENT。 将深度缓冲区写入纹理的正确方法是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.