FBO的C ++ OpenGL问题不显示着色器的光照

时间:2015-08-26 00:37:33

标签: c++ opengl shader fbo

我现在已经解决了这个问题几天了。我不确定这是否是一个非常简单的修复,但我似乎无法解决它。问题是如果我没有渲染到FBO,我的灯光着色器功能可以正常工作,但是当我将场景渲染到FBO时,灯光着色器不起作用。

FBO初始化

//create fbo texture id
glGenTextures(1, &m_fboTexture);
glBindTexture(GL_TEXTURE_2D, m_fboTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_width, m_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);

//I have removed this and doesn't change anything
glGenRenderbuffers(1, &m_fboDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, m_fboDepthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_width, m_height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

//create fbo id
glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_fboTexture, 0);

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_fboDepthBuffer);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

我这样绑定/写FBO:

//I have also used GL_FRAMEBUFFER here as well
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo); 
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

//render my geometry here

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

这是我没有FBO的原因以及它应该如何看待: enter image description here

这是我将场景渲染到FBO时得到的结果: enter image description here

正如您所看到的,由于某种原因,照明不适用于FBO。你们有什么感想?如果您想看到任何其他代码,请告诉我。再次感谢您的帮助!

更新

这是我渲染几何体的方式:

使用FBO

if(hasFBOs)
{
    m_fbo->Begin();

    view = m_camera->GetViewMatrix();
    proj = m_camera->GetProjectionMatrix();

    Render3D(&view, &proj);

    view.SetIdentity();
    proj = m_camera->GetOrthoMatrix();

    Render2D(*spriteBatch, &view, &proj);

    m_fbo->End();

    RenderFbos(*spriteBatch, view, proj);
}
else
{
    view = m_camera->GetViewMatrix();
    proj = m_camera->GetProjectionMatrix();

    Render3D(&view, &proj);

    view.SetIdentity();
    proj = m_camera->GetOrthoMatrix();

    Render2D(*spriteBatch, &view, &proj);
}

我的RenderFbos函数只渲染fbo:

void RenderFbos(SpriteBatch& spriteBatch, IGSRMatrix4x4 viewMatrix, IGSRMatrix4x4 orthoMatrix)
{
    IGSRMatrix4x4 world;

    m_fbo->Bind(0);

    m_fboShader->BeginShader();
    m_fboShader->SetShaderMatrixParameter("worldMatrix", world.AsArray());
    m_fboShader->SetShaderMatrixParameter("viewMatrix", viewMatrix.AsArray());
    m_fboShader->SetShaderMatrixParameter("projectionMatrix", orthoMatrix.AsArray());

    m_fboShader->SetShaderSampler("texture1");

    float width = SCREEN_WIDTH;
    float height = SCREEN_HEIGHT;
    RecTangle rect = RecTangle(width/2.0f, height/2.0f, width, height);
    spriteBatch.Draw(rect);

    m_fboShader->EndShader();

    m_fbo->UnBind(0);
}

FBO绑定功能:

void Bind(GLuint slot)
{
    glActiveTexture(GL_TEXTURE0 + slot);
    glBindTexture(GL_TEXTURE_2D, m_fboTexture);
}

FBO UnBind功能:

void UnBind(GLuint slot)
{
    glActiveTexture(GL_TEXTURE0 + slot);
    glBindTexture(GL_TEXTURE_2D, 0);
}

0 个答案:

没有答案