OpenGL:渲染到附着在FBO上的颜色纹理会产生白色纹理

时间:2011-12-04 12:24:58

标签: c++ opengl textures fbo

我的问题是,在设置了一个帧缓冲区对象,其中单个颜色纹理附加到GL_COLOR_ATTACHMENT0点并将多个对象渲染到此纹理,然后我将其绘制到屏幕上,我得到一个完全白色的纹理

现在我知道绘图代码是正确的,因为我可以很好地绘制到后台缓冲区,只是当帧缓冲区涉及问题发生时。绘图代码使用一个非常基本的着色器来构造四边形。

我的代码如下:

// Create the FBO
glGenFramebuffers(1, &m_gbufferFBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_gbufferFBO);

// Create a colour texture for use in the fbo
glGenTextures(1, &m_colourBuffer);
glBindTexture(GL_TEXTURE_2D, m_colourBuffer);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_colourBuffer, 0);

// check if the frame buffer was successfully created
CheckFrameBufferErrors();
CheckGLErrors();


// Begin rendering with the frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, m_gbufferFBO);
glPushAttrib(GL_VIEWPORT_BIT | GL_COLOR_BUFFER_BIT);

// Set the viewport to match the width and height of our FBO
glViewport(0, 0, m_width, m_height);

glDrawBuffer(GL_COLOR_ATTACHMENT0);

// Clear buffer to whatever the clear colour is set to
glClearColor(m_clearColour.GetR(), m_clearColour.GetG(), m_clearColour.GetB(), m_clearColour.GetA());
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );

Game::GetInstance().GetCamera()->ApplyViewTransform();


// RENDERING OF OBJECTS


glPopAttrib();
glBindFramebuffer(GL_FRAMEBUFFER, 0);

然后使用固定功能管道将缓冲区的颜色纹理渲染到屏幕上。

glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);

glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);

// Render the colour buffer to screen
glBindTexture(GL_TEXTURE_2D, m_colourBuffer);

glMatrixMode(GL_MODELVIEW); 
glLoadIdentity();

glBegin(GL_QUADS);

glTexCoord2f(0.f, 0.f); glVertex3f(0.f, 0.f, 0.f);
glTexCoord2f(1.f, 0.f); glVertex3f(m_width, 0.f, 0.f);
glTexCoord2f(1.f, 1.f); glVertex3f(m_width, m_height, 0.f);
glTexCoord2f(0.f, 1.f); glVertex3f(0.f, m_height, 0.f);

glEnd();

关于我在这里做错了什么的想法?

1 个答案:

答案 0 :(得分:7)

绑定FBO时,不得绑定FBO附加纹理。纹理永远不会成为数据源并同时下沉。

对于纹理绑定到的所有纹理单元和目标,在将FBO绑定为渲染目标之前,必须绑定另一个或没有纹理。