Problems calling GL11.glDrawElements

Hi. I’ve created a method for rendering meshes using vertex arrays and glDrawElements function. Here it is:


public static void draft(Mesh3D mesh) {
        GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        FloatBuffer buf = Buffers.wrap(mesh.getVertices());
        GL11.glVertexPointer(3, 0, buf);
        IntBuffer ibuf = mesh.getVertexIndices();
        GL11.glDrawElements(GL11.GL_TRIANGLE_STRIP, ibuf);
        GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
    }    

When I use the method, nothing happens, although I can’t find any errors in the code above.

But if I use the glArrayElement combination instead, my mesh renders correctly. Here is a slightly corrected version of the previous code.


public static void draft(Mesh3D mesh) {
        GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        FloatBuffer buf = Buffers.wrap(mesh.getVertices());
        GL11.glVertexPointer(3, 0, buf);
        IntBuffer ibuf = mesh.getVertexIndices();
        GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
        for(int i = 0, n = ibuf.capacity(); i < n; i++) {
            GL11.glArrayElement(ibuf.get(i));
        }
        GL11.glEnd();
        GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
    } 

Could you explain what I am doing wrong? Why doesn’t my first code work?

You can’t wrap an array. You have to create a direct buffer and copy the data into it.

[quote]You can’t wrap an array. You have to create a direct buffer and copy the data into it.
[/quote]
I know I can’t wrap an array. I just have a method called “wrap” in my utility class for buffer operations that converts vectors to direct ByteBuffers (or FloatBuffers). And it has nothing to do with the wrap method in standard java Buffer classes. Here’s the method:


public static FloatBuffer wrap(ReadableVector[] vertices) {
        float[] array = vertices[0].value();
        FloatBuffer buf = createFloatBuffer(vertices.length*array.length).put(array);
        
        for(int i = 1; i < vertices.length; i++)
            buf.put(vertices[i].value());
        
        return (FloatBuffer)buf.rewind();
        
    }

NOTE: ReadableVector is MY OWN interface created by myself.

I guess I have to show other methods of the class:



public final static int FLOAT_BYTES = 4;

public static FloatBuffer createFloatBuffer(int floatCount) {
        return createByteBuffer(FLOAT_BYTES * floatCount).asFloatBuffer();
    }

 public static ByteBuffer createByteBuffer(int size) {
        return ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
    }

Satisfied now?

Very weird. Are you sure the indices IntBuffer is properly rewound?

  • elias

You’re right. It was my fault - I forgot to rewind the indices buffer after filling it with data. Now everything works just fine.
Thanks awfully for helping me!

I recommend flip() rather than rewind() as it works for both buffers that are exactly the same size as their data and also for buffers that are much bigger than the amount of data that is in them.

Cas :slight_smile:

Thanks again.