Reputation: 47
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];
Upvotes: 0
Views: 527
Reputation: 1427
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)
size
) type
) stride
)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
Upvotes: 2