So I tried to get VBOs to work but it doesn’t render anything and I couldn’t find anything being the noob I am :o
Here’s the code for rendering a box
public class TestModel implements Model {
int vertexBufferID;
// cube ///////////////////////////////////////////////////////////////////////
// v6----- v5
// /| /|
//v1------v0|
//| | | |
//| |v7---|-|v4
//|/ |/
//v2------v3
//vertex coords array
FloatBuffer vertices = BufferUtils.createFloatBuffer(3 * 24).put(new float[] {1,1,1, -1,1,1, -1,-1,1, 1,-1,1, // v0-v1-v2-v3
1,1,1, 1,-1,1, 1,-1,-1, 1,1,-1, // v0-v3-v4-v5
1,1,1, 1,1,-1, -1,1,-1, -1,1,1, // v0-v5-v6-v1
-1,1,1, -1,1,-1, -1,-1,-1, -1,-1,1, // v1-v6-v7-v2
-1,-1,-1, 1,-1,-1, 1,-1,1, -1,-1,1, // v7-v4-v3-v2
1,-1,-1, -1,-1,-1, -1,1,-1, 1,1,-1}); // v4-v7-v6-v5
//normal array
FloatBuffer normals = BufferUtils.createFloatBuffer(3 * 24).put(new float[] {0,0,1, 0,0,1, 0,0,1, 0,0,1, // v0-v1-v2-v3
1,0,0, 1,0,0, 1,0,0, 1,0,0, // v0-v3-v4-v5
0,1,0, 0,1,0, 0,1,0, 0,1,0, // v0-v5-v6-v1
-1,0,0, -1,0,0, -1,0,0, -1,0,0, // v1-v6-v7-v2
0,-1,0, 0,-1,0, 0,-1,0, 0,-1,0, // v7-v4-v3-v2
0,0,-1, 0,0,-1, 0,0,-1, 0,0,-1}); // v4-v7-v6-v5
//color array
FloatBuffer colors = BufferUtils.createFloatBuffer(3 * 24).put(new float[] {1,1,1, 1,1,0, 1,0,0, 1,0,1, // v0-v1-v2-v3
1,1,1, 1,0,1, 0,0,1, 0,1,1, // v0-v3-v4-v5
1,1,1, 0,1,1, 0,1,0, 1,1,0, // v0-v5-v6-v1
1,1,0, 0,1,0, 0,0,0, 1,0,0, // v1-v6-v7-v2
0,0,0, 0,0,1, 1,0,1, 1,0,0, // v7-v4-v3-v2
0,0,1, 0,0,0, 0,1,0, 0,1,1}); // v4-v7-v6-v5
public TestModel() {
vertexBufferID = createVBOID();
System.out.println("VBO id: " + vertexBufferID);
renderToVbo();
}
public static int createVBOID() {
if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
IntBuffer buffer = BufferUtils.createIntBuffer(1);
ARBVertexBufferObject.glGenBuffersARB(buffer);
return buffer.get(0);
}
return 0;
}
// Helper method
public int sizeof(FloatBuffer fb) {
return fb.capacity();
}
public void renderToVbo() {
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, sizeof(vertices)+sizeof(normals)+sizeof(colors), ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
ARBVertexBufferObject.glBufferSubDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, 0, vertices); // copy vertices starting from 0 offest
ARBVertexBufferObject.glBufferSubDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, sizeof(vertices), normals); // copy normals after vertices
ARBVertexBufferObject.glBufferSubDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, sizeof(vertices)+sizeof(normals), colors); // copy colours after normals
}
@Override
public void render() {
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glNormalPointer(GL11.GL_FLOAT, 0, sizeof(vertices));
GL11.glColorPointer(3, GL11.GL_FLOAT, 0, (sizeof(vertices)+sizeof(normals)));
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
GL11.glDrawArrays(GL11.GL_QUADS, 0, 24);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY); // disable vertex arrays
GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, 0);
}
}
To render it I simply use testModel.render()