简体   繁体   中英

OpenGL: Rendering to colour texture attached to FBO results in white texture

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM