简体   繁体   中英

Vertex Array for VBO -OpenGL

I have an array which contains coordinates. It looks like:

    glm::vec3* Vertices;

How can I pass its elements to glVertexAttribPointer. Is the only way just one dimensional array? For example; can I use something like that:

    float* Vertices[3];

Before reading my answer: Apparently my answer is only partially correct, and you can use glVertexAttribPointer without manually writing to the GRAM but instead using client memory, but it is still preferable to use VBOs instead of using this approach.

You don't pass something to glVertexAttribPointer . You will have to write it to the GRAM first by using glBufferData after binding a buffer with glBindBuffer . glVertexAttribPointer will only describe where in this bound buffer the data will be found. And here is the point that shows that you can pass almost any data you wish to the GRAM. If you have your array of vec3 (i assume vec3 only contains floats x, y and z) your memory for this array will look like x1, y1, z1, x2, y2, z2, x3, y3, z3 and so on. If that is written to your GRAM you will have to specify this data so your GPU knows what to do. Example:

glBindBuffer(GL_ARRAY_BUFFER, bufferID); 

bufferID must be allocated before ( glGenBuffers ).

glBufferData(GL_ARRAY_BUFFER, 36, vertices, GL_STATIC_DRAW); 

Write to the buffer bound in GL_ARRAY_BUFFER (with glBindBuffer). Write 36 bytes (float = 4bytes, 1 vertex with only position = 3 floats, 1 triangle = 3 vertices, sums up to 36 bytes for 1 triangle) GL_STATIC_DRAW indicates that we will write this once and then only draw it.

glVertexPointer(3, GL_FLOAT, 0, 0)
  • You have 3 coordinates per Vertex ( size )
  • They are floats (GL_FLOAT, type )
  • You have 12 byte length for a whole data set in the buffer sizeof(float) * 3 and because you only use a single pointer here it can compute the stride for itself when you pass 0 (elements * typesize) ( stride )
  • And they begin at the beginning of the buffer (0, offset )

I used glVertexPointer here for simplicity.

You can find a very good tutorial on VBOs in the LWJGL Wiki

http://www.lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_%28VBO%29

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM