0

尝试通过我的屏幕外缓冲区传递多个纹理时遇到问题。问题是没有正确写出其他纹理。我被分配的任务是延迟渲染 sponza 场景。

你能帮我解决这个问题吗?

创建程序:

mySponza_.program = glCreateProgram();
glAttachShader(mySponza_.program, 
    mySponza_.vertex_shader);
glAttachShader(mySponza_.program, 
    mySponza_.fragment_shader);
glBindFragDataLocation(mySponza_.program, 0, "def_position");
glBindFragDataLocation(mySponza_.program, 1, "def_normal");
glLinkProgram(mySponza_.program);

将纹理绑定到 FBO:

GLenum framebuffer_status = 0;
//DEFINES FRAMEBUFFER TO BE MODIFIED
glBindFramebuffer(GL_FRAMEBUFFER, gbuffer_fbo_);
//-----------------------------------------------

//ATTACHES TEXTURES TO FRAMEBUFFER (GBUFFER)
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE, gbuffer_position_tex_, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_RECTANGLE, gbuffer_normal_tex_, 0);
//-----------------------------------------------

//ATTACHES RENDERBUFFER OBJECT TO FRAMEBUFFER (GBUFFER)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depth_rbo_);
//-----------------------------------------------

framebuffer_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (framebuffer_status != GL_FRAMEBUFFER_COMPLETE) {
    tglDebugMessage(GL_DEBUG_SEVERITY_HIGH, "framebuffer not complete");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);

构建 GBuffer:

glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE); 
glDepthFunc(GL_LEQUAL);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 

glDisable(GL_BLEND);

glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT);

glUseProgram(mySponza_.program);


for(int j=0; j < scene_->modelCount(); ++j)
{
    model_matrices.model_xform = glm::mat4(scene_->model(j).xform);

    model_matrices.combined_xform = model_matrices.project_xform * model_matrices.view_xform * model_matrices.model_xform;

    glUniformMatrix4fv(
        glGetUniformLocation(mySponza_.program, "combined_xform"),
        1, GL_FALSE, glm::value_ptr(model_matrices.combined_xform));

    glUniformMatrix4fv(
        glGetUniformLocation(mySponza_.program, "model_xform"),
        1, GL_FALSE, glm::value_ptr(model_matrices.model_xform));

    int mesh_index = scene_->model(j).mesh_index;
    glBindVertexArray(mesh_[mesh_index].vao);
    glDrawElements(GL_TRIANGLES, mesh_[mesh_index].element_count, GL_UNSIGNED_INT, 0);
}


glBindFramebuffer(GL_READ_FRAMEBUFFER, gbuffer_fbo_);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, viewport[2], viewport[3], 0, 0, viewport[2], viewport[3], GL_COLOR_BUFFER_BIT, GL_LINEAR);

顶点着色器:

#version 330

uniform mat4 combined_xform;
uniform mat4 model_xform;

in vec3 vertex_position;
in vec3 vertex_normal;

out vec3 varying_position;
out vec3 varying_normal;

void main(void)
{
    varying_position = mat4x3(model_xform) * vec4(vertex_position, 1.0);
    varying_normal = mat3(model_xform) * vertex_normal;
    gl_Position = combined_xform * vec4(vertex_position, 1.0);
}

片段着色器:

#version 330

in vec3 varying_normal;
in vec3 varying_position;

out vec3 def_normal;
out vec3 def_position;

void main(void)
{
    def_normal = varying_normal;
    def_position = varying_position;
}
4

0 回答 0