glDrawArrays(GL_TRIANGLES, ......)

Started by
2 comments, last by sjhalayka 6 years, 8 months ago

I'm creating some vertex data using a vector<GLfloat>:


	    static vector<GLfloat> vertices = {
        
        // 3D position, 2D texture coordinate
        
        // card front
        -0.1, -0.1,  0.0,   1*0.125, 4*0.125, // vertex 0
         0.1, -0.1,  0.0,   2*0.125, 4*0.125, // vertex 1
         0.1,  0.1,  0.0,   2*0.125, 5*0.125, // vertex 2
        -0.1, -0.1,  0.0,   1*0.125, 4*0.125, // vertex 0
         0.1,  0.1,  0.0,   2*0.125, 5*0.125, // vertex 2
        -0.1,  0.1,  0.0,   1*0.125, 5*0.125, // vertex 3
	        // card back
         0.1,  0.1,  0.0,   4*0.125, 2*0.125, // vertex 2
         0.1, -0.1,  0.0,   4*0.125, 1*0.125, // vertex 1
        -0.1, -0.1,  0.0,   5*0.125, 1*0.125, // vertex 0
         0.1,  0.1,  0.0,   4*0.125, 2*0.125, // vertex 2
        -0.1, -0.1,  0.0,   5*0.125, 1*0.125, // vertex 0
        -0.1,  0.1,  0.0,   5*0.125, 2*0.125  // vertex 3
    };
	

Then I draw it:


	    glDrawArrays(GL_TRIANGLES, 0, vertices.size());
	

What is the correct value for the final parameter of glDrawArrays()? vertices.size() or vertices.size()/5? Both appear to work.

 

Advertisement

You have here 12 vertices, so your number should be 12.
Your vertices vector contains 60 elements (3x float for position, 2x float for uv), so doing


vector.size() / 5         // => 60/5 = 12 

is the correct way.

If you prefer to keep vertex data in one array you might want to consider adding some struct 


struct MyVector
{
  float x, y, z;
  float u, v;
}

and:


static vector<MyVector> vertices

then you can simply do:


vertices.size() // gives 12 now

There are also others methods how you can organize your vertex data, for example keeping vertex and uv data as separate arrays (you can look it up if you want) but I hope this answers your question.

Thank you so much for the reply, and for the ideas!

This topic is closed to new replies.

Advertisement