Question

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...

Was it helpful?

Solution

You need to add buffer.flip () to your code for one thing.

Buffers in Java have an internal current position, which is what LWJGL uses to mark the beginning of your data when you call glVertexPointer (...). Each time you call buffer.put (...), it increases the size of your buffer and also sets the current position to the end of the buffer.

Calling flip (...) will move the current position to the the beginning of the buffer. This way you will not overrun your buffer when you try to draw capacity (...)-many vertices starting at the "current" position.

Additionally, each vertex is built from 3 floats. The capacity here is measured in terms of the number of floating-point components, so the number of vertices stored in your buffer is actually its capacity divided by 3.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top