2

Problem

When using a vertex buffer objects to draw textured primitives, we have to define a separate buffer to store a list of texture coordinates at each vertex. This texture coordinate array has to map to each vertex in the VBO, thus, we need a large array if we are drawing many primitives.

However, if all primitives have the same texture coordinates, it is a waste to repeat the coordinates for each primitive that will be drawn with glDraw* call.

Example

I have a large number of textured quads that need to be drawn. The quads share the same texture. All vertices are put in a VBO and a large texture coordinate array is created. The textured quads are drawn with a call to glDrawArrays.

4----3    8----7
|   /|    |   /|
|  / |    |  / |
| /  |    | /  |
|/   |    |/   |
1----2    5----6

vertex buffer: { 1, 2, 3, 4, 5, 6, 7, 8 }
Texture coordinate array: { 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 }
                                                    <-------------------->
                                                     coordinates repeated
                                                        for second quad

The texture coordinate array repeats every 4 vertices. This buffer has to be recreated every time a batch of quads has to be drawn as we do not know the number of quads beforehand. The repeating texture coordinates also waste memory.

Question

Is there a way to remove the repetition in texture coordinate arrays when the primitives to be drawn have all the same texture coordinates?

Skul
  • 303
  • 1
  • 6
  • 19
  • 1
    The first question should be why don't you do this for quad vertices first? Then, you won't have to deal with texture coordinates. – eozgonul Mar 03 '14 at 07:35
  • Each quad has already been transformed. What goes into the VBO is the transformed vertices of the quads. – Skul Mar 03 '14 at 07:39
  • You should transform them in your vertex shader. – eozgonul Mar 03 '14 at 07:41
  • I'm sticking with the fixed function for now. – Skul Mar 03 '14 at 07:46
  • 1
    OpenGL-ES-2.0 doesn't do fixed function. Either your tags are wrong or I don't understand your comment. If you *are* stuck with fixed function the answer to your question is unfortunately "no, you can't". – Paul-Jan Mar 03 '14 at 08:10
  • 1
    Assuming you are indeed on fixed function OpenGL (not ES), look into glTexGeni(GL_OBJECT_LINEAR) and glTexGenfv(GL_OBJECT_PLANE). Generating all the UV coordinates as opposed to providing them might help. – Paul-Jan Mar 03 '14 at 08:22
  • Ah. Was hoping there was a way to reuse texcoords but looks like the most elegant way would be to use generated coordinates. Thanks for the tip! – Skul Mar 03 '14 at 09:59

0 Answers0