I'm trying to use VBO's to load my vertices into the graphics card's memory, so that I can call DrawArrays() and the graphics card can draw lots of triangles on its own without much CPU involvement. Note: I am not using shaders, at least not yet.
However, when I try to use VBO's, the vertices are displayed in wrong positions, not the positions that I specified. It seems the first vertex is always wrong.
When I do this:
float Vertices[] = { -10, 0, 10, 10, 0, 10, -10, 0, -10,
-10, 0, -10, 10, 0, 10, 10, 0, -10 };
uint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, 4*6*3, Vertices, GL_STATIC_DRAW);
glVertexPointer(3, GL_FLOAT, 0, 0);
// later...
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 6);
It works, and I see my floating square:
However, when I do this:
float Vertices[] = { -10, 0, 10, 10, 0, 10, -10, 0, -10,
-10, 0, -10, 10, 0, 10, 10, 0, -10 };
uint VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, 4*6*3, 0, GL_STATIC_DRAW);
void* Map = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(Map, Vertices, 4*6*3);
glUnmapBuffer(GL_ARRAY_BUFFER);
glVertexPointer(3, GL_FLOAT, 0, 0);
// later...
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLES, 0, 6);
I see a misshapen square, with the first vertex in the wrong position:
It was my understanding that these two methods should be equivalent. I seem to have a similar problem, even when using the first method (passing pointer directly to glBufferData), if I place the buffer loading code into a separate function, and then my "Vertices" variable goes out of scope. But I don't understand why this should matter, I thought the vertex data was being copied to the graphics card?
I've tried every which way to make this work, studied documentation, updated my video drivers, tested this code on different PCs, always a problem.
Here's the full source code, in case I missed mentioning something important. Any help would be greatly appreciated! Thanks!