c++openglshadervertextext-rendering

Isn't a quad always composed of 4 vertex?


I'm getting into OpenGL. I'm following learnopengl.com and I reached the text rendering part, where I read (at the end of In Practice > Text Rendering > Shaders section)

The 2D quad requires 6 vertices of 4 floats each, so we reserve 6 * 4 floats of memory. Because we'll be updating the content of the VBO's memory quite often we'll allocate the memory with GL_DYNAMIC_DRAW.

So far I was thinking of a quad as a pair of triangles, four vertex. How a quad can require 6 vertex?


Solution

  • If a quad contains 2 triangles it can take 6 vertices, 2 vertices will be the duplicated in this case.

    Alternatively, you can use 4 vertices and GL_TRIANGLE_STRIP. All vertices will be unique, no duplicates.

    Alternatively, there is a trick with only one triangle, 3 vertices only. Vertex shader would look like:

    out vec2 texCoord;
     
    void main()
    {
        float x = -1.0 + float((gl_VertexID & 1) << 2);
        float y = -1.0 + float((gl_VertexID & 2) << 1);
        texCoord.x = (x+1.0)*0.5;
        texCoord.y = (y+1.0)*0.5;
        gl_Position = vec4(x, y, 0, 1);
    }
    

    And a discussion what pros and cons have these methods.