I've been trying to implement a better way of drawing stuff, and tried using Vertex Buffers.
Now, I've been following the Tutorial from the LWJGL Wiki [Using Vertex Buffer Objects (VBO)], but it does not work for me, it does not draw at all (as far as I can see).
If I draw with glVertex3d()
instead, all works fine.
This is how it looks like:
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBVertexBufferObject;
import org.lwjgl.opengl.GLContext;
public abstract class Shape {
public void render()
{
if(vertexBufferID > 0 && indexBufferID > 0)
{
glEnableClientState(GL_VERTEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
glVertexPointer(3, GL_FLOAT, 0, 0);
if(colorBufferID > 0)
{
glEnableClientState(GL_COLOR_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, colorBufferID);
glColorPointer(4, GL_FLOAT, 0, 0);
}
if(textureCoordBufferID > 0)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, textureCoordBufferID);
glColorPointer(2, GL_FLOAT, 0, 0);
}
if(normalBufferID > 0)
{
glEnableClientState(GL_NORMAL_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, normalBufferID);
glNormalPointer(GL_FLOAT, 0, 0);
}
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, indexBufferID);
glDrawRangeElements(drawMode, 0, verticesNumber, verticesNumber,
GL_UNSIGNED_INT, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
}
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;
}
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);
}
}
@Override
protected void finalize() throws Throwable
{
if(vertexBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(vertexBufferID);
if(colorBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(colorBufferID);
if(textureCoordBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(textureCoordBufferID);
if(indexBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(indexBufferID);
super.finalize();
}
protected int verticesNumber;
protected int drawMode;
protected int vertexBufferID;
protected int colorBufferID;
protected int textureCoordBufferID;
protected int normalBufferID;
protected int indexBufferID;
}
And the subclass, actually filling the Content in:
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
public class ShapeCube extends Shape{
public ShapeCube()
{
drawMode = GL11.GL_QUADS;
verticesNumber = 6 * 4;
FloatBuffer vertices = BufferUtils.createFloatBuffer(verticesNumber * 3);
IntBuffer indices = BufferUtils.createIntBuffer(verticesNumber);
for(float x = -0.5F; x < 1; x++) for(float y = -0.5F; y < 1; y++) for(float z = -0.5F; z < 1; z++)
{
vertices.put(new float[]{x, y, z});
}
indices.put(new int[]{0, 1, 2, 3, 4, 5, 6, 7});
indices.put(new int[]{0, 1, 4, 5, 2, 3, 6, 7});
indices.put(new int[]{0, 2, 4, 6, 1, 3, 5, 7});
vertexBufferID = createVBOID();
bufferData(vertexBufferID, vertices);
indexBufferID = createVBOID();
bufferElementData(indexBufferID, indices);
}
}
As I said, this code doesn't draw anything (I can see) for me. I'm sure I'm invoking the right code at the right spot, and it should draw a simple colored cube. I'm pretty sure I must be missing something easy, but I don't have any experience with Vertex Buffer objects.
I've found the answer myself now. For everyone wondering, the position of the Buffers I've been using must be set to Zero. Then it works perfectly :)