2

I'm learning OpenGl. I have been exposed to making solid shapes with a given set of coordinates and wrapping a texture over it.

So this is what would my vertices and texture coordinates looked like.

float vertices[] = {
        //<---Positions---->  <-texCoords->
         -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
          0.5f, -0.5f, -0.5f,  1.0f, 0.0f,
          0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
          0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
         -0.5f, -0.5f, -0.5f,  0.0f, 0.0f,
    
         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
          0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
          0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
          0.5f,  0.5f,  0.5f,  1.0f, 1.0f,
         -0.5f,  0.5f,  0.5f,  0.0f, 1.0f,
         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
    
         -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         -0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         -0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    
          0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
          0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
          0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
          0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
          0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
          0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
    
         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
          0.5f, -0.5f, -0.5f,  1.0f, 1.0f,
          0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
          0.5f, -0.5f,  0.5f,  1.0f, 0.0f,
         -0.5f, -0.5f,  0.5f,  0.0f, 0.0f,
         -0.5f, -0.5f, -0.5f,  0.0f, 1.0f,
    
         -0.5f,  0.5f, -0.5f,  0.0f, 1.0f,
          0.5f,  0.5f, -0.5f,  1.0f, 1.0f,
          0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
          0.5f,  0.5f,  0.5f,  1.0f, 0.0f,
         -0.5f,  0.5f,  0.5f,  0.0f, 0.0f,
         -0.5f,  0.5f, -0.5f,  0.0f, 1.0f
    };

The first three would be the positional coordinates and the last two would be texture coordinates for that particular vertex. I know to adjust the buffersize etc.. before using a draw call. As you can see from this set of vertices, there are coordinates for each and every face of the cube and I know it is quite inefficient.So coming to my problem, I have looked up a little bit of index buffers, and from that I have realized we just need 8 vertices to draw a cube and 36 indices which define each and every face. This significantly reduced memory usage. After changes this is how my set of coordinates and indices look like:

float vertices[] = {
        // front
        -0.5f, -0.5f,  0.5f,
         0.5f, -0.5f,  0.5f,
         0.5f,  0.5f,  0.5f,
        -0.5f,  0.5f,  0.5f,
        // back
        -0.5f, -0.5f, -0.5f,
         0.5f, -0.5f, -0.5f,
         0.5f,  0.5f, -0.5f,
        -0.5f,  0.5f, -0.5f
    };
unsigned int index[] = {
        // front
         0, 1, 2,
         2, 3, 0,
         // right
         1, 5, 6,
         6, 2, 1,
         // back
         7, 6, 5,
         5, 4, 7,
         // left
         4, 0, 3,
         3, 7, 4,
         // bottom
         4, 5, 1,
         1, 0, 4,
         // top
         3, 2, 6,
         6, 7, 3
    };

But after doing so I am left with no idea on where to implement the same texture coordinates as before.

Earlier I took each line as a buffer with specific layouts for positions and texture coordinates from the vertices[] and feed those to my shader program.

But this time I have no faces, just 8 vertices. I'm guessing we need to create another array for texture coordinates and bind those to a new buffer, even then I lack a bit of clarity.

I would really appreciate the help.(the cubes render fine with a solid color the same way in both cases)

madhavpcm
  • 19
  • 2
  • I'm not an expert in OpenGl, but I think in this scenario you would need to create a new VBO with the texture coordinates (whether or not new VBO also uses an element VBO). I'm curious to know what the correct answer is to this – Human-Compiler Aug 12 '20 at 16:22
  • Yea, me too, still doubting whether we need an element VBO. – madhavpcm Aug 12 '20 at 17:46

0 Answers0