EDIT: I forgot that this site isn’t LWJGL exclusive; I’m using LWJGL.
Hey, I’m experimenting with setting up a VBO to render my (cube based) world. This code should draw a cube (at least that’s the intention), it generates no errors:
public class Renderfield3D implements IDrawable{
private int _verticesID = 0;
private int _indicesID = 0;
private int _coloursID = 0;
@Override
public void load() {
float[] vertices = new float[]
{ // X Y Z
0.0f, 0.0f, 0.0f, // 0 LBF
1.0f, 0.0f, 0.0f, // 1 RBF
0.0f, 0.0f, 1.0f, // 2 LBB
1.0f, 0.0f, 1.0f, // 3 RBB
0.0f, 1.0f, 0.0f, // 4 LTF
1.0f, 1.0f, 0.0f, // 5 RTF
0.0f, 1.0f, 1.0f, // 6 LTB
1.0f, 1.0f, 1.0f // 7 RTB
};
int[] indices = new int[]
{
4, 5, 1, 0, // FRONT
5, 7, 3, 1, // RIGHT
7, 6, 2, 3, // BACK
6, 4, 0, 2, // LEFT
0, 1, 3, 2, // BOTTOM
6, 7, 5, 4 // TOP
};
float[] colours = new float[]
{
1.0f, 1.0f, 1.0f, 1.0f
};
_verticesID = VBOHandler.createVBO();
_indicesID = VBOHandler.createVBO();
_coloursID = VBOHandler.createVBO();
FloatBuffer bufferedColours = BufferUtils.createFloatBuffer(colours.length);
bufferedColours.put(colours);
FloatBuffer bufferedvertices = BufferUtils.createFloatBuffer(vertices.length);
bufferedvertices.put(vertices);
IntBuffer bufferedindices = BufferUtils.createIntBuffer(indices.length);
bufferedindices.put(indices);
VBOHandler.bufferData(_verticesID, bufferedvertices);
VBOHandler.bufferElementData(_indicesID, bufferedindices);
}
@Override
public void draw() {
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, _verticesID);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, _coloursID);
GL11.glColorPointer(4, GL11.GL_FLOAT, 0, 0);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, _indicesID);
GL12.glDrawRangeElements(GL11.GL_QUADS, 0, 24, 24,
GL11.GL_UNSIGNED_INT, 0);
}
}
public class VBOHandler {
public static IntBuffer createVBOs(int amount)
{
if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
{
IntBuffer buffer = BufferUtils.createIntBuffer(amount);
ARBVertexBufferObject.glGenBuffersARB(buffer);
return buffer;
}
return null;
}
public static int createVBO()
{
return createVBOs(1).get(0);
}
public static void bufferData(int id, FloatBuffer buffer)
{
if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
{
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer,
ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
}
}
public static void bufferElementData(int id, IntBuffer buffer)
{
if(GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
{
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, buffer,
ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
}
}
}
I’ve been over the code on “Using Vertex Buffer Objects” at the LWJGL site a fair few times now. The trouble I am having, is that my game does not draw a cube at all. There are two classes in that paste - the VBOHandler class that I wrote is at the bottom. To rule out camera issues, I set up a camera that points at a reference grid around the origin too. This is a screenshot of what my game sees
http://dl.dropbox.com/u/18809996/game.png
The reference grid is made with just plain vertices - not buffered, not using an index buffer, nothing. Just vertices.
Can anyone see what is wrong with my code there?