Sign in to follow this  
SapphireG

Textured cube with glDrawElements

Recommended Posts

SapphireG    175

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!

Share this post


Link to post
Share on other sites
L. Spiro    25638
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 Edited by L. Spiro

Share this post


Link to post
Share on other sites
SapphireG    175

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.

Share this post


Link to post
Share on other sites
Brother Bob    10344

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.

Share this post


Link to post
Share on other sites
SapphireG    175


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!

Share this post


Link to post
Share on other sites
Brother Bob    10344

 


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.

Edited by Brother Bob

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this