1

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?

KcFnMi
  • 5,516
  • 10
  • 62
  • 136
  • 2
    Do you believe one triangle has 3 vertices? The code accompanying your quote defines _two_ of those. You can also see in the code that the two triangles have two points in common. – Drew Dormann Dec 04 '22 at 02:47
  • 1
    Oh, maybe I got it, `3 + 3 = 6`. I was thinking in the index case, where we only need 4 vertex to define a quad. – KcFnMi Dec 04 '22 at 02:51

1 Answers1

2

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.

rokuz
  • 386
  • 1
  • 7