OpenGL:渲染到附加到 FBO 的彩色纹理会产生白色纹理

发布于 2024-12-19 13:55:29 字数 2267 浏览 0 评论 0原文

我的问题是,在设置了一个带有附加到 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();

关于我在这里可能做错了什么有什么想法吗?

My problem is that after having set up a frame buffer object with a single colour texture attached to the GL_COLOR_ATTACHMENT0 point and rendering a number of objects to this texture when I then go to draw this to the screen I get a completely white texture.

Now I know the drawing code is correct as I can draw to the back buffer just fine, it's simply when the frame buffer becomes involved that the problem occurs. The drawing code uses a very basic shader that textures a quad.

My code is below:

// 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);

Then the buffer's colour texture is rendered to the screen using the fixed function pipeline.

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();

Any ideas on what I could be doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东北女汉子 2024-12-26 13:55:29

绑定 FBO 时,不得绑定 FBO 附加纹理。纹理永远不能同时作为数据源和接收器。

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

The FBO attached texture must not be bound, when the FBO is bound. A texture can never be a data source and sink at the same time.

For all texturing units and targets to which the texture has been bound you must bind another or no texture before binding the FBO as render destination.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文