I am sort of new to using LWJGL and have been using lots of resources online for examples and tutorials to aid my learning. I have also been using a number of books dedicated to OpenGL such as "The Red Book", and "Beginning OpenGL Game Programming, 2e", although they are designed for use with C++ they are giving some great knowledge which crosses over.
My problem is using GL12.glDrawRangeElements(..). Everything I read explains that I can "hop" around, although I cannot seem to get it to function properly. First, my the code I am using (which is a modified version of the VBO tutorial at
http://lwjgl.org/wiki/index.php?title=Using_Vertex_Buffer_Objects_%28VBO%29.
My VBO buffer initialization:
private static void loadBlockVBO() {
final float[] verts = new float[] {
0.0f, 0.0f, 0.0f, // 0
0.0f, 1.0f, 0.0f, // 1
1.0f, 1.0f, 0.0f, // 2
1.0f, 0.0f, 0.0f, // 3
1.0f, 1.0f, -1.0f, // 4
1.0f, 0.0f, -1.0f, // 5
0.0f, 0.0f, -1.0f, // 6
0.0f, 1.0f, -1.0f // 7
};
final int[] inds = new int[] {
0, 3, 1, // FBL
3, 2, 1, // FTL
3, 5, 2, // RBL
2, 5, 4, // RTL
5, 4, 6, // B(ack)BL
4, 6, 7, // B(ack)TL
7, 6, 0, // LBL
7, 0, 1, // LTL
0, 6, 3, // B(ottom)BL
3, 6, 5, // B(ottom)TL
7, 1, 2, // TBL
7, 2, 4 // TTL
};
vert_buff_id = ARBVertexBufferObject.glGenBuffersARB();
index_buff_id = ARBVertexBufferObject.glGenBuffersARB();
FloatBuffer vertBuff = BufferUtils.createFloatBuffer(verts.length);
vertBuff.put(verts).flip();
IntBuffer indBuff = BufferUtils.createIntBuffer(inds.length);
indBuff.put(inds).flip();
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB,
vert_buff_id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB,
vertBuff,
ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB,
index_buff_id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB,
indBuff,
ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
}
My Render Code:
public final void render(final float x, final float y, final float z) {
if (renderable) {
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
GL11.glTranslatef(x, y, z);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vert_buff_id);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0);
GL11.glEnableClientState(GL11.GL_INDEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, index_buff_id);
final int max_x = openglbook.world_blocks[0].length;
final int max_y = openglbook.world_blocks.length;
final int max_z = openglbook.world_blocks[0][0].length;
// If x,y,z+1 (front) block id == 0 then draw front.
if ((z + 1 >= max_z) || (z + 1 < max_z && openglbook.world_blocks[(int)y][(int)x][(int)z+1] == 0)) {
GL12.glDrawRangeElements(GL11.GL_TRIANGLES, 0, 5, 6, GL11.GL_UNSIGNED_INT, 0);
}
// x, y, z - 1 (back) block id == 0 then draw back.
if ((z-1 < 0) || (z-1 >= 0 && openglbook.world_blocks[(int)y][(int)x][(int)z-1] == 0)) {
GL12.glDrawRangeElements(GL11.GL_TRIANGLES, 12, 17, 6, GL11.GL_UNSIGNED_INT, 0);
}
// x, y + 1, z (top) block id == 0 then draw top.
if ((y+1 >= max_y) || (y + 1 < max_y && openglbook.world_blocks[(int)y + 1][(int)x][(int)z] == 0)) {
GL12.glDrawRangeElements(GL11.GL_TRIANGLES, 30, 35, 6 , GL11.GL_UNSIGNED_INT, 0);
}
// x, y - 1, z (bottom) block id == 0 then draw bottom.
if ((y-1 < 0) || y - 1 >= 0 && openglbook.world_blocks[(int)y-1][(int)x][(int)z] == 0) {
GL12.glDrawRangeElements(GL11.GL_TRIANGLES, 24, 29, 6 , GL11.GL_UNSIGNED_INT, 0);
}
// x -1, y, z (left) block id == 0 then draw left.
if ((x-1 < 0) || x - 1 >= 0 && openglbook.world_blocks[(int)y][(int)x-1][(int)z] == 0) {
GL12.glDrawRangeElements(GL11.GL_TRIANGLES, 18, 23, 6 , GL11.GL_UNSIGNED_INT, 0);
}
// x +1, y, z (right) block id == 0 then draw right.
if ((x+1 >= max_x) || x + 1 < max_x && openglbook.world_blocks[(int)y][(int)x+1][(int)z] == 0) {
GL12.glDrawRangeElements(GL11.GL_TRIANGLES, 6, 11, 6 , GL11.GL_UNSIGNED_INT, 0);
}
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glPopMatrix();
}
}
My world_blocks array is nothing more than a 3d array of int's storing which type of block is in the location. I'm attempting to build a sort of minecraft type sandbox world. My question is, how am i suppose to use GL12.glDrawRangeElements(..) to only draw a select number of vertices from the bound buffers? The above implementation calls glDrawRangeElements but for some reason no matter what it always draws the same portion of the cube, i.e - i can put any start and end index values in the method call and the same portion of the cube is drawn. What am i missing?
(This is by far not the best implementation of the logic between frames, i.e checking for index out of bounds and such, but for now i just want the cubes to render properly before i worry about any sort of performance issues.)