Passing a pointer to an array to glGenBuffers
I'm currently passing an array to a function, then attempting to use glGenBuffers with the array that is passed to the function. I can't figure out a way to get glGenBuffers to work with the array that I've passed. I have a decent grasp of the basics of pointers, but this is beyond me.
Buffers are generated (and deleted) elsewhere. I can render everything if I do it fixed, but this makes it more flexible. The current issue is "&renderArray[0]". If I move this (everything into the function) out so that it is in the main drawing code, it works. I also have something very similar that uses a constant array working as well.
开发者_开发技巧This is basically how the render code works. It's a bit more complex, (colours using the same array idea, also not working) but the basic idea is as follows:
void drawFoo(const GLfloat *renderArray, GLuint verticeBuffer) {
glBindBuffer(GL_ARRAY_BUFFER, verticeBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(renderArray)*sizeof(GLfloat), &renderArray[0], GL_STATIC_DRAW);
glVertexPointer(2, GL_FLOAT, 0, 0);
glEnableClientState(GL_VERTEX_BUFFER);
glDrawArrays(GL_TRIANGLE_FAN, 0, 45);
glDisableClientState(GL_VERTEX_BUFFEr);
}
Thanks in advance for the help
If you pass an array as a pointer, you lose its associated type information:
GLfloat array[N];
GLfloat* pointer = array;
sizeof(array ); // => sizeof(GLfloat) * N
sizeof(pointer); // => sizeof(GLfloat*);
sizeof
is a compile time operator which gives you the size of the type of its operand, which in your case is GLfloat*
.
You need to pass the size of the array as an additional parameter:
void drawFoo(const GLfloat* renderArray, size_t nFloats, GLuint verticeBuffer) {
// ...
glBufferData(GL_ARRAY_BUFFER, nFloats * sizeof(GLFloat),
renderArray, GL_STATIC_DRAW);
Also note that the &array[0]
syntax is redundant, renderArray
already is a pointer. Even with arrays such an expression is not needed as arrays can implicitly decay to a pointer to their first element.
精彩评论