OpenGL & multiple textures as output

227 views Asked by At

I'm having an issue when trying to pass multiple textures through my offscreen buffer. The problem being that the other texture is not being written up properly. The task that I've been assigned is deferred rendering of the sponza scene.

Could you help me solve this issue?

Creating the program:

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

Binding textures to 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);

Building the 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);

Vertex Shader:

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

Fragment Shader:

#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;
}
0

There are 0 answers