Home c++ How to use UV coordinates in OpenGL along with index buffer?

How to use UV coordinates in OpenGL along with index buffer?

Author

Date

Category

I’m trying to draw a cube using Index Buffer. That is, I have 8 points and an index buffer that shows in what order you need to draw these points so that the cube is from the triangles. It looks like this:

float positions [] = {
  -0.5F, 0.5F, 0.5F, // 0
   0.5F, 0.5F, 0.5F, // 1
  -0.5F, -0.5F, 0.5F, // 2
   0.5F, -0.5F, 0.5F, // 3
  -0.5F, 0.5F, -0.5F, // 4
   0.5F, 0.5F, -0.5F, // 5
  -0.5F, -0.5F, -0.5F, // 6
   0.5F, -0.5F, -0.5F, // 7
};
Unsigned int Indices [] = {
  0, 1, 2,
  1, 2, 3,
  4, 5, 6,
  5, 6, 7,
  1, 5, 3,
  5, 3, 7,
  0, 4, 2,
  4, 2, 6,
  4, 5, 0,
  5, 0, 1,
  6, 7, 2,
  7, 2, 3
};
/ * ... I skip in this example preparation and transfer to OpenGL all data for this does not relate to the problem * /
GlDrawelements (GL_TRIANGLES, 36, GL_UNSIGNED_INT, NULLPTR));

and everything works. But after I decided to pull the texture using the index buffer and faced the fact that it is not possible to associate each vertex of the cube with one single position on a two-dimensional texture, because for each single face (consisting of two triangles), you need different textures coordinates and in this case to use Indices for drawing failure. For if add UV coordinates next to the usual and try to draw the cube, then something seems to be:

float positions [] = {
     // POS // UV
  -0.5F, 0.5F, 0.5F, 0.F, 1.F, // 0
   0.5F, 0.5F, 0.5F, 1.F, 1.F, // 1
  -0.5F, -0.5F, 0.5F, 0.F, 0.F, // 2
   0.5F, -0.5F, 0.5F, 1.F, 0.F, // 3
  -0.5F, 0.5F, -0.5F, 0.F, 1.F, // 4
   0.5F, 0.5F, -0.5F, 0.F, 1.F, // 5
  -0.5F, -0.5F, -0.5F, 0.F, 0.F, // 6
   0.5F, -0.5F, -0.5F, 0.F, 0.F // 7
};
Unsigned int Indices [] = {
  0, 1, 2,
  1, 2, 3,
  4, 5, 6,
  5, 6, 7,
  1, 5, 3,
  5, 3, 7,
  0, 4, 2,
  4, 2, 6,
  4, 5, 0,
  5, 0, 1,
  6, 7, 2,
  7, 2, 3
};
/ * ... I skip in this example preparation and transfer to OpenGL all data for this does not relate to the problem * /
GlDrawelements (GL_TRIANGLES, 36, GL_UNSIGNED_INT, NULLPTR));

And since in OpenGL I am a beginner, I do not know how to do it right.
You can of course spit and not use indexes and just describe all the triangles individually something like this:

float positions [] = {
// Position UV.
-0.5F, 0.5F, 0.5F, 0.F, 1.F,
-0.5F, -0.5F, 0.5F, 0.F, 0.F,
 0.5F, 0.5F, 0.5F, 1.F, 1.F,
 0.5F, 0.5F, 0.5F, 1.F, 1.F,
-0.5F, -0.5F, 0.5F, 0.F, 0.F,
 0.5F, -0.5F, 0.5F, 1.F, 0.F,
-0.5F, 0.5F, -0.5F, 0.F, 1.F,
 0.5F, 0.5F, -0.5F, 1.F, 1.F,
-0.5F, -0.5F, -0.5F, 0.F, 0.F,
-0.5F, -0.5F, -0.5F, 0.F, 0.F,
 0.5F, 0.5F, -0.5F, 1.F, 1.F,
 0.5F, -0.5F, -0.5F, 1.F, 0.F,
 0.5F, 0.5F, -0.5F, 1.F, 1.F,
 0.5F, 0.5F, 0.5F, 0.F, 1.F,
 0.5F, -0.5F, 0.5F, 0.F, 0.F,
 0.5F, 0.5F, -0.5F, 1.F, 1.F,
 0.5F, -0.5F, 0.5F, 0.F, 0.F,
 0.5F, -0.5F, -0.5F, 1.F, 0.F,
-0.5F, 0.5F, 0.5F, 0.F, 1.F,
-0.5F, 0.5F, -0.5F, 1.F, 1.F,
-0.5F, -0.5F, 0.5F, 0.F, 0.F,
-0.5F, -0.5F, 0.5F, 0.F, 0.F,
-0.5F, 0.5F, -0.5F, 1.F, 1.F,
-0.5F, -0.5F, -0.5F, 1.F, 0.F,
 0.5F, 0.5F, -0.5F, 1.F, 1.F,
-0.5F, 0.5F, -0.5F, 0.F, 1.F,
-0.5F, 0.5F, 0.5F, 0.F, 0.F,
 0.5F, 0.5F, -0.5F, 1.F, 1.F,
-0.5F, 0.5F, 0.5F, 0.F, 0.F,
 0.5F, 0.5F, 0.5F, 1.F, 0.F,
-0.5F, -0.5F, -0.5F, 0.F, 1.F,
 0.5F, -0.5F, -0.5F, 1.F, 1.F,
-0.5F, -0.5F, 0.5F, 0.F, 0.F,
-0.5F, -0.5F, 0.5F, 0.F, 0.F,
 0.5F, -0.5F, -0.5F, 1.F, 1.F,
 0.5F, -0.5F, 0.5F, 1.F, 0.F,
};
/ * ... I skip in this example preparation and transfer to OpenGL all data for this does not relate to the problem * /
GLDRAWARRAYS (GL_TRIRIANGLES, 0, 36));

and at the output it turns out absolutely correct and expected result:

But unfortunately, the code becomes more closely and has to store much more data than when using the index buffer. Is it possible to somehow transmit different UV coordinates for individual faces and at the same time still use the index buffer?


Answer 1, Authority 100%

In general, one vertex is one UV coordinate. If you need to break / duplicate UV, then the vertices will have to be broken / duplicated.

cube, this is the brightest example of the need to duplicate the vertices due to UV scan – (8 vertices are transformed into 24 (which is still better than just the triangles with which there will be 36 vertices). For other models and forms, everything is much better. So do not throw indexes, they will be more useful for you)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions