1
votes

I am drawing a basic rectangle.

void renderQuad()
{
    if (quadVAO == 0)
    {
        float quadVertices[] = { // vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates.
        // positions   // texCoords
        -1.0f,  1.0f,  0.0f, 1.0f,
        -1.0f, -1.0f,  0.0f, 0.0f,
         1.0f, -1.0f,  1.0f, 0.0f,

        -1.0f,  1.0f,  0.0f, 1.0f,
         1.0f, -1.0f,  1.0f, 0.0f,
         1.0f,  1.0f,  1.0f, 1.0f
        };
        //  VAO
        glGenVertexArrays(1, &quadVAO);
        glGenBuffers(1, &quadVBO);
        glBindVertexArray(quadVAO);
        glBindBuffer(GL_ARRAY_BUFFER, quadVBO);
        glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), &quadVertices, GL_STATIC_DRAW);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float)));
    }
    glBindVertexArray(quadVAO);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
    glBindVertexArray(0);
}

this is the render function where i am drawing the rectangle.

void RenderThread()
{   
    Shader shader("C:\\Shaders\\Screen\\Vert.txt", "C:\\Shaders\\Screen\\Frag.txt");
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    shader.use();
    while (!glfwWindowShouldClose(window))
    {
        glClearColor(0.0, 0.0, 0.0, 0.0);
        glClear(GL_COLOR_BUFFER_BIT );
        processInput(window);
        renderQuad();       
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}

These are the shaders.

Vertex shader

#version 330 core
layout( location = 0 ) in vec2 aPos;
layout( location = 1) in vec2 aTexCoord;

out vec2 TexCoord;

void main()
{
   TexCoord = aTexCoord;
   gl_Position =  vec4( aPos , 0.0 ,  1.0 );
}

Fragment shader

#version 330 core
in vec2 TexCoord;
out vec4 OutColor;
void main()
{

   OutColor = vec4( 0.5 , 0.0 , 0.0 , 0.3);
}

The rectangle is drawn with two different shades.

enter image description here

if i remove this line "glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);" than it gets drawn correctly.

1

1 Answers

2
votes

Because you use the wrong primitive type. If you want to draw 2 triangle primitives (by 6 coordinates), then you have to use the triangle primitive type GL_TRIANGLES:

glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);

glDrawArrays(GL_TRIANGLES, 0, 6); 

For 2 triangles by a GL_TRIANGLE_STRIP, 4 coordinates are sufficient:

float quadVertices[] = {
        // positions   // texCoords
        -1.0f,  1.0f,  0.0f, 1.0f,
        -1.0f, -1.0f,  0.0f, 0.0f,

         1.0f,  1.0f,  1.0f, 1.0f,
         1.0f, -1.0f,  1.0f, 0.0f,
};
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);