[Solved] VBO issue - having trouble with rendering

EDIT: I forgot that this site isn’t LWJGL exclusive; I’m using LWJGL. :smiley:

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?

You forgot to flip your buffers. >_>
EDIT: If you’re making a DK2 clone, let me tell you I peed on that streetlight first! xD

@theagentd
Damn too many people forgetting to flip buffers these days! Flipping buffers is a pain in the arse!!

I am indeed making a DK2 clone. :slight_smile: But only to build the engine I need for a different, unique game. How did yours go / is going?

Thanks for the response. I took a look at the LWJGL VBO page again and did a ctrl+F for “flip” – no mention. I then went to the FloatBuffer documentation and worked out what flip() does before employing some “random engineering” with buffered*.flip() calls and yielded nothing.

Can you show me where in my code a buffer flip should occur?

Before line 49 when you give VBOHandler the data, you should flip your buffers by calling .flip() on all 3 of them.

Perfect, thanks! It didn’t work at first, but then I also noticed that I never actually buffered my colour data. So I threw in “VBOHandler.bufferData(_coloursID, bufferedColours);” as well as your solution and it worked.

Time to hunt around for a “Thanks” or “+rep” button on the forum, and go update all my other posts :slight_smile:

Haha, time for a sticky in the OpenGL sub forum? xD I’ve forgot to flip my buffers so many times since I started with LWJGL that that’s the first thing I check if something isn’t working when buffers are involved.

I haven’t gotten very far yet so I won’t be posting any screenshots. Hell, I’m not even sure I will ever manage to finish it. I only have terrain rendering, fog of war and exploration (things don’t update until your units see them) done at the moment. I’m currently working on the unit system, player system and rooms, all at the same time, while trying to keep all the other stuff I’ll have to implement in mind. Loading maps/units from files, multiplayer, semi-static objects, decorations, AI… Everything has to fit together in the end, so because I’m trying to take everything into consideration it’s going a little bit slow at the moment. I’m having a big CPU performance issue with my fog of war too, which will require some optimizations later.
Without fog of war I get around 200 FPS with 800 units and a 256x256 map. As the the game is extremely CPU-limited now (I will implement multithreading later) and I only have very simple graphics, I can enable as much anti-aliasing as I want without affecting the FPS at all, and I’m on a laptop.

See that nifty “Appreciate” link next to the “Quote” button? Use it for anyone who helps you :smiley:

http://dl.dropbox.com/u/18809996/nope.png

Nope…

Wow. That’s not bad. I was getting ~80fps running my clone with 25 units on a 128sq map with XNA. Mine has a map generator, renderer, and a LOT of AI. There’s a video (a few months old now) that reflects my progress at the “Game” link in my sig. It doesn’t use buffers either, so the first thing I’m doing with my port is making it use VBOs with a chunk system.

Fog of war doesn’t have to be rendered every frame, s remaking it every 4th to 10th should be enough with some clever interpolation between the last one map and the current one. The problem is that just rendering the full fog of war (which can cost up to 20ms when the units are in the thousands) gives me so much micro stuttering that it doesn’t really change anything. I’ll have to split up the fog of war rendering between multiple frames (at the cost of SLI/Crossfire support xD), but I’m not gonna dive into this now. It works, and I have more important things to implement. My point is that I can make fog of war rendering basically free.

Your video looks really nice! Keep it up!

Concerning the appreciate link, were you logged in when taking that screenshot?