i'm trying to learn how to handle VBOs (VertexBufferObjects), but i can't get further than the Vertex Arrays. I followed some basic tutorials on this topic, each teaching a different way, making it hard for me to understand and implement.
Problem: Rendering with glDrawArrays();
should work the same as rendering in immediate Mode. So i tried to test the code by songho.ca but all i'm getting is a JVM crash inside the native code. Asking Google didn't help at all, because of dozens of variants of this Method. But i assume that my Parameters are the main problem.
Here is what i got so far:
I work with LWJGL, so i'm storing the Data in a FloatBuffer:
FloatBuffer buffer = BufferUtils.createFloatBuffer(triangles.size() * 3 * 3);
for(Triangle tri : triangles) {
buffer.put((float) tri.getA().x);
buffer.put((float) tri.getA().y);
buffer.put((float) tri.getA().z);
buffer.put((float) tri.getB().x);
buffer.put((float) tri.getB().y);
buffer.put((float) tri.getB().z);
buffer.put((float) tri.getC().x);
buffer.put((float) tri.getC().y);
buffer.put((float) tri.getC().z);
}
My Triangle class consists of 3 Vectors A B C, each defining one Vertex. In the first line i'm allocating space for the vertices, 9 floats per Triangle (3 floats per Vertex, 3 Vertex per Triangle). So far nothing unusual, the amount of floats put inside the buffer equals exactly the allocated size.
Rendering in immediate Mode:
glBegin(GL_TRIANGLES);
for (Triangle tri : this.triangles) {
glVertex3d(tri.getA().x, tri.getA().y, tri.getA().z);
glVertex3d(tri.getB().x, tri.getB().y, tri.getB().z);
glVertex3d(tri.getC().x, tri.getC().y, tri.getC().z);
}
glEnd();
Nothing special too, everything seen so far, works fine for me. But the Array variant gives me headaches...
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, 0, buffer);
glDrawArrays(GL_TRIANGLES, 0, buffer.capacity());
glDisableClientState( GL_VERTEX_ARRAY );
The errorlog produced by the crashing JVM points to the glDrawArrays(GL_TRIANGLES, 0, buffer.capacity());
line, but i can't find my mistake...