Shepard
Shepard

Reputation: 811

Visual issue using Frambuffer Object as texture

My OpenGL engine draws a given scene into a Framebuffer Object, then uses its color attachment as a texture. It is then put on a square in the viewport.

The problem is that I see a strange visual artifact:

enter image description here

The square is built with

glm::vec2 square[4];
square[0] = glm::vec2(0.f, 0.f);
square[1] = glm::vec2(engWidth, 0.f);
square[2] = glm::vec2(0.f, engHeight);
square[3] = glm::vec2(engWidth, engHeight);
glm::vec2 texcoords[4];
texcoords[0] = glm::vec2(0.f, 0.f);
texcoords[1] = glm::vec2(1.f, 0.f);
texcoords[2] = glm::vec2(0.f, 1.f);
texcoords[3] = glm::vec2(1.f, 1.f);

glGenBuffers(2, Buffer2D);
glBindBuffer(GL_ARRAY_BUFFER, Buffer2D[0]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(glm::vec2), square, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, Buffer2D[1]);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(glm::vec2), texcoords, GL_STATIC_DRAW);

orthographicProj = glm::ortho(0.f, (float)engWidth, 0.f, (float)engHeight, -1.f, 1.f);

Where engWidth and engHeight are the actual window size.

Then the frame is rendered with

shaderProgram->setMatrix("Clip", orthographicProj);

glBindBuffer(GL_ARRAY_BUFFER, Buffer2D[0]);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, Buffer2D[1]);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(1);

glDisableVertexAttribArray(2);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Vertex and fragment shaders for the final phase are simple

const char *frontVertexShader = R"(
    #version 440 core

    uniform mat4 Clip;

    layout(location = 0) in vec2 Position;
    layout(location = 1) in vec2 TexCoord;

    out vec2 Tex_Coord;

    void main() {
        gl_Position = Clip * vec4(Position, 0.0, 1.0);
        Tex_Coord = TexCoord;
    }
)";

const char *frontFragmentShader = R"(
    #version 440 core

    in vec2 Tex_Coord;  
    uniform sampler2D sampler;

    out vec4 Fragment;

    void main() {
        Fragment = texture(sampler, Tex_Coord);
    }
)";

I tried using triangles instead a triangle strip but with the same result.

Any idea? I can post more code if needed.

Upvotes: 3

Views: 69

Answers (2)

Shepard
Shepard

Reputation: 811

Found it! I forgot to call glClear on the backbuffer and for a strange reason only some pixel presented the issue.

Upvotes: 2

Robinson
Robinson

Reputation: 10122

I don't know if this will help, but for what it's worth this is what I do for a full screen quad:

(Note the vertex has no texture component - it's just -1 to 1).

v[0].x = -1.f; v[0].y = -1.f; v[0].z = 0.f;
v[1].x = -1.f; v[1].y =  1.f; v[1].z = 0.f;
v[2].x =  1.f; v[2].y = -1.f; v[2].z = 0.f;
v[3].x =  1.f; v[3].y =  1.f; v[3].z = 0.f;

The vertex shader is simply:

void main()
{
    attrib_Fragment_Texture = attrib_Position.xy * 0.5 + 0.5; 

    gl_Position = vec4(attrib_Position.xy, 0.0, 1.0);
}

With the following pixel shader:

void main(void)
{
    Out_Colour = texture2D(Map_Diffuse, attrib_Fragment_Texture);
}

You don't need an orthographic projection matrix.

Upvotes: 2

Related Questions