Question

I'm trying to read and create a VBO from an ArrayList then render it. The problem is that I just render a blank screen. Everything worked fine when I was immediately rendering it; only now with VBO's is it not working.

The game loop looks like this, with Camera calling glTranslate and glRotate functions.

game.clearScreen();
Camera.update(delta);
try {
    game.render();
} catch (IOException e) {
    System.out.println(e);
}
Display.update();
Display.sync(FRAME_RATE);

Render method:

world.render(vertices,colorVertices);
FloatBuffer VertexPositionData = BufferUtils.createFloatBuffer(vertices.size());
float[] floats = new float[vertices.size()];
int i = 0;
for (Float f : vertices) {
    floats[i] = Float.intBitsToFloat(Float.floatToIntBits(f));
    i++;
}
VertexPositionData.put(floats);
VertexPositionData.flip();

FloatBuffer VertexColorData = BufferUtils.createFloatBuffer(colorVertices.size());
float[] colorFloats = new float[colorVertices.size()];
int j = 0;
for (Float f : colorVertices) {
    colorFloats[j] = Float.intBitsToFloat(Float.floatToIntBits(f));
    j++;
}
VertexColorData.put(colorFloats);
VertexColorData.flip();

GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,VBOVertexHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER,VertexPositionData,GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,VBOColorHandle);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER,VertexColorData,GL15.GL_STATIC_DRAW);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,0);

GL11.glPushMatrix();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOColorHandle);
GL11.glColorPointer(3, GL11.GL_FLOAT, 0, 0L);
GL11.glDrawArrays(GL11.GL_QUADS, 0, vertices.size()/3);
GL11.glPopMatrix();

The ArrayLists have vertices and linked colors. Each voxel adds its vertices to the ArrayList (instead of immediately rending it).

Was it helpful?

Solution

Before you render you need to call glEnableClientState(GL_VERTEX_ARRAY) and glEnableClientState(GL_COLOR_ARRAY). You should also disable them after rendering.

Also, you are recreating your VBOs in each render pass currently, which is usually not a good idea. If you are rendering a static scene you only need to create them once and if you need to update your scene you should use glBufferSubData or glMapBuffer to update only the parts which need to be changed, along with Gl_STREAM_DRAW instead of GL_STATIC_DRAW as the usage hint.

Aswell, I dont know why you are using Float.intBitsToFloat(Float.floatToIntBits(f)); or creating a float array when you could put your floats directly into the buffer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top