在得知我必须使用FBO从着色器中读取浮点数据后,我尝试了这个但没有运气:
glGenTextures( 1, &renderTex );
glBindTexture( GL_TEXTURE_2D, renderTex );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexImage2D( GL_TEXTURE_2D, 0, GL_R32F, size, size, 0, GL_RED, GL_FLOAT, NULL );
glBindTexture( GL_TEXTURE_2D, 0 );
glGenFramebuffers( 1, &fbo );
glBindFramebuffer( GL_FRAMEBUFFER, fbo );
glGenRenderbuffers( 1, &depth );
glBindRenderbuffer( GL_RENDERBUFFER, depth );
glRenderbufferStorage( GL_RENDERBUFFER, GL_DEPTH_COMPONENT, size, size );
glBindRenderbuffer( GL_RENDERBUFFER, 0 );
glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, renderTex, 0 );
glFramebufferRenderbuffer( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth );
if ( !FBOStatusOK() ) return;
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer( GL_READ_FRAMEBUFFER , m_FBO );
glClampColor( GL_CLAMP_READ_COLOR, GL_FALSE );
GLfloat* pixels = new GLfloat[size * size];
//glReadPixels also returns 0s
//glReadPixels( 0, 0, size, size, GL_RED, GL_FLOAT, pixels );
glGetTexImage( GL_TEXTURE_2D, 0, GL_RED, GL_FLOAT, pixels );
pixelDataVector.resize( size * size );
for ( int i = 0; i < size * size; i++ )
{
pixelDataVector[i] = (float) pixels[i];
}
我的着色器代码:
out float data;
void main()
{
data = 0.02;
}
这将打印所有 0。当我尝试使用 GL_RGBA 或 GL_BRGA 而不是 GL_RED 时,我得到 (0, 0, 0, 1; ...)。
难道我做错了什么?