2

我已经开始切换我的渲染代码以支持着色器,并且在渲染到后台缓冲区时一切正常。所以现在我正在努力渲染到 FBO,但我得到的只是颜色和法线的白色纹理。

这是我的 FBO 创建代码:

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

我用来设置当前渲染目标的代码:

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

最后,我的实际绘图代码:

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
4

1 回答 1

3

GL_TEXTURE_MIN_FILTERGL_NEAREST_MIPMAP_LINEAR默认情况下。提供 mipmap 或切换到GL_LINEARGL_NEAREST

OpenGL Wiki有更多.

于 2012-05-16T20:02:37.990 回答