Search code examples
openglrenderingfbo

OpenGL Rendering to Multiple Textures, results are white


I've begun switching my rendering code to support shaders, and that all works fine when rendering to the back buffer. So now I'm working towards rendering to FBOs, but all I get are white textures for both the color and normals.

Here is my FBO creation code:

void RenderTarget_GL::CreateFBO (void)
{
  // if the machine supports the GL FBO extension
  if (s_supportfbo)
  {
    // Create FBO
    glGenFramebuffersEXT(1, &m_fbo);
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);

    // Create default texture buffer
    char *buffer = new char [static_cast<int>(g_window->GetWidth() * m_screenWidth) * static_cast<int>(g_window->GetHeight() * m_screenHeight) * 4];
    std::memset(buffer, 0, static_cast<int>(g_window->GetWidth() * m_screenWidth) * static_cast<int>(g_window->GetHeight() * m_screenHeight) * 4);

    // Create Render Texture
    glGenTextures(1, &m_rendertexture);
    glBindTexture(GL_TEXTURE_2D, m_rendertexture);
    glTexImage2D(GL_TEXTURE_2D, 0, 4, static_cast<int>(g_window->GetWidth() * m_screenWidth), static_cast<int>(g_window->GetHeight() * m_screenHeight), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

    // Bind Render Texture to FBO
    glBindTexture(GL_TEXTURE_2D, 0);
    glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_rendertexture, 0);

    // Create Normal Texture if this FBO will be rendering normals
    if (m_hasnormal)
    {
      glGenTextures(1, &m_normaltexture);
      glTexImage2D(GL_TEXTURE_2D, 0, 4, static_cast<int>(g_window->GetWidth() * m_screenWidth), static_cast<int>(g_window->GetHeight() * m_screenHeight), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

      // Bind Normal Texture to FBO
      glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, m_normaltexture, 0);
    }

    // UnBind FBO and cleanup default buffer
    delete [] buffer;
    Clear();
  }
}

And the code I use to set the current render target:

void RenderTarget_GL::Set (void)
{
  if (s_supportfbo && g_glgraphics->GetShaderEnabled())
  {
    static const GLenum buffer1[] = {GL_COLOR_ATTACHMENT0_EXT};
    static const GLenum buffer2[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT};

    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_fbo);
    if (m_hasnormal)
      glDrawBuffers(2, buffer2);
    else
      glDrawBuffers(1, buffer1);
  }
}

And finally, my actual drawing code:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Setup the camera transformation
glMatrixMode(GL_MODELVIEW);
glPushMatrix();

if (m_camera)
  m_camera->GLMatrix();
else
  m_defaultCam.GLMatrix();

// Setup Render Target
if (m_shaderenabled)
{
  glPushAttrib(GL_VIEWPORT_BIT);
  glViewport(0,0,g_window->GetWidth(),g_window->GetHeight());
  m_initialpass->Set();
}

// Draw All Objects with their per-object shaders

// Clear render target and shader bindings
if (m_shaderenabled)
{
  glPopAttrib();
  RenderTarget_GL::Clear();
  Shader_GL::ClearShaderBinding();
}

glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

// Draw Scene
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_initialpass->GetColorTexture());
glBegin(GL_QUADS);
  glTexCoord2f(0.0f, 0.0f);
  glVertex3f(-1.0f, -1.0f, 0.0f);
  glTexCoord2f(1.0f, 0.0f);
  glVertex3f(1.0f, -1.0f, 0.0f);
  glTexCoord2f(1.0f, 1.0f);
  glVertex3f(1.0f, 1.0f, 0.0f);
  glTexCoord2f(0.0f, 1.0f);
  glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();

Texture_GL::ClearTextureBinding();
glPopMatrix();

// Swap Buffers

Solution

  • GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR by default. Supply mipmaps or switch to GL_LINEAR or GL_NEAREST.

    The OpenGL Wiki has more.