如何调试默认的帧和深度缓冲区?

问题描述 投票:0回答:1

我正在尝试通过动态阴影映射和正向渲染在我的自定义渲染器上实现阴影(延迟渲染将在以后实现)。例如,所有内容均正确渲染到用于生成阴影贴图的帧缓冲区。但是,当使用默认的帧缓冲区正常渲染场景时,仅天空盒会被渲染(这意味着使用默认的帧缓冲区),而我唯一的假设是,由于禁用了对DrawActors(。)的调用,问题与深度缓冲区有关。 。)(在ForwardRenderShadows中)似乎可以解决问题,但如果这样做,则无法生成深度图。有什么建议吗?

代码:

void Scene::DrawActors(const graphics::Shader& shader)
{
    for(const auto& actor : actors_)
         actor->Draw(shader);
}

template <typename T>
void ForwardRenderShadows(const graphics::Shader& shadow_shader, const std::vector<T>& lights)
{   
    for(const auto& light : lights)
    {
          if(light->Shadow())
          {
               light->DrawShadows(shadow_shader); 
               DrawActors(shadow_shader); //removing this line "solves the problem"
               glBindFramebuffer(GL_FRAMEBUFFER, 0);
          }
     } 
}

/* 
    shadow mapping is only implemented on directional lights for the moment, and that is the 
    relevant code that gets called when the process starts, more code details at the end of code
    snippet.
*/

void DirectionalLight::SetupShadows()
{
     glGenFramebuffers(1, &framebuffer_);
     glGenTextures(1, &shadow_map_);

     glBindTexture(GL_TEXTURE_2D, shadow_map_);
     glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, constants::SHADOW_WIDTH, constants::SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadow_map_, 0);
     glDrawBuffer(GL_NONE);
     glReadBuffer(GL_NONE);

     if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
          throw std::runtime_error("Directional light framebuffer is not complete \n");

     glBindFramebuffer(GL_FRAMEBUFFER, 0);

     ShadowSetup(true);
}


void DirectionalLight::DrawShadows(const graphics::Shader& shader)
{   
     if(!ShadowSetup())
          SetupShadows();

     glViewport(0, 0,  constants::SHADOW_WIDTH, constants::SHADOW_HEIGHT);
     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer_);
     glClear(GL_DEPTH_BUFFER_BIT);

     shader.Use();

     projection_ = clutch::Orthopraphic(-10.0f, 10.0f, -10.0f, 10.0f, 1.0f, 100.0f);
     transform_  = clutch::LookAt(direction_ * -1.0f, {0.0f, 0.0f,  0.0f, 0.0f}, {0.0f, 1.0f,  0.0f, 0.0f}); 

     shader.SetUniformMat4("light_transform", projection_ * transform_);
}

void DirectionalLight::Draw(const graphics::Shader& shader)
{
    shader.SetUniform4f("light_dir",   direction_);
    shader.SetUniform4f("light_color", color_);
    shader.SetUniformMat4("light_transform",  transform_);
    shader.SetUniformMat4("light_projection", projection_);
    shader.SetUniformInt("cast_shadow", shadows_);

    glActiveTexture(GL_TEXTURE12);
    shader.SetUniformInt("shadow_map", 12);
    glBindTexture(GL_TEXTURE_2D, shadow_map_);
} 

为代码仓库:https://github.com/rxwp5657/Nitro

该问题的相关文件:

  • include / core / scene.hpp
  • include / core / directional_light.hpp
  • include / core / light_shadow.hpp
  • include / core / directional_light.hpp
  • include / graphics / mesh.hpp

  • src / core / scene.cpp

  • src / core / directional_light.cpp
  • src / core / light_shadow.cpp
  • src / core / directional_light.cpp
  • src / graphics / mesh.cpp

最后,到目前为止,我尝试过的是:

  • 使用glDepthMask(GL_FALSE)和glDisable(GL_DEPTH_TEST)禁用深度测试//相同的问题。
  • 将深度函数更改为glDepthFunc(GL_ALWAYS); //没有预期的结果;

“错误示例”

c++ opengl graphics
1个回答
0
投票

如果您具有NVIDIA显卡,则可以查看Nsight。您可以捕获帧并查看所有发生的总帐调用。enter image description here

然后,您可以选择一个事件,例如在我的示例中为事件22,并检查所有纹理,颜色缓冲区,深度缓冲区和模板缓冲区。

enter image description here

此外,您可以一次查看所有GL状态参数。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.