Textured cube with glDrawElements

Started by
4 comments, last by Brother Bob 9 years, 3 months ago

Hi all, beginner question here.

So initially, I have managed to draw a textured cube by specifying all 36 vertices in a vertices[] array, each one with its own texture coordinates, and using glDrawArrays(). Worked fine.

After that, i wanted to do that using glDrawElements, so i specified the indices[] array (size of 36) and specified only 8 vertices in the vertices[] array.

Although the geometry was rendered ok, I had problems with the textures, and I then realized that this approach would not work. I understood the reason for that: A single point needed to have different texture coordinates each time it was part of a different face, so it has to be considered a different vertex, although these vertices would have the same position.

What is the correct approach to do this? Is there in any way doable (i'm pretty sure it is, somehow) or do you have to define different vertices, end of story? Just a few pointers to the right direction will suffice, I'll handle the rest :)

Thanks in advance!

Advertisement
If 2 vertices have the same values for their positions, normals, UV’s, etc., then you can merge them into 1 vertex.
If not, then you can’t.

It is as simple as that. If your points are the same but your UV’s are not, then your vertices are not duplicates, period.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks for the quick & solid response L. Spiro.

Although, it kind of generated another (more generic and slightly off topic) question in my head:

So I have to define 36 vertices. Do I still have any advantage of using glDrawElements() with my 36 vertices and indices[] array, or should I ignore the indices[] and stick with glDrawArrays() for this specific situation?

I (till now) considered glDrawElements() a way to render objects by sending less data to the gpu, compared to glDrawArrays(). But in this example it just seems not to be the case anymore.

A cube with more attributes than just position ultimately has 24 unique vertices. If you build the sides of the cube using triangles, you have to generate a total of 32 vertices. However, the shared edge of the two triangles per side also share unique vertices so out of the 32 vertices you need to generate there are only 24 unique ones.

So you can still gain something by using indexed primitives because some of the triangles share unique vertices. On the other hand, a cube is close to the worst case for indexed primitives because it only has sharp edges with discontinuous vertex attributes that prevents vertex sharing. Compare to smooth surfaces, such as a sphere, where attributes typically flow smoothly across the surface most of the time.


If you build the sides of the cube using triangles, you have to generate a total of 32 vertices.

Is this a typo or am I missing something again? You have 6 sides * 2 triangles per side * 3 vertices per triangle = 36, no?


A cube with more attributes than just position ultimately has 24 unique vertices.

Thanks! You subtract 2 shared vertices from each side, leaving you with 24 unique vertices. I hadn't thought of that, nice :)

Much obliged for the insight!


If you build the sides of the cube using triangles, you have to generate a total of 32 vertices.

Is this a typo or am I missing something again? You have 6 sides * 2 triangles per side * 3 vertices per triangle = 36, no?

It should be 36, sorry. Have no idea where I got 32 from. You even wrote 36 in your first post so I probably just typed too fast.

This topic is closed to new replies.

Advertisement