Creating a voxel chunk with a VBO - How to translate the coordinates of each block and add it to the VBO chunk?

Posted by sunsunsunsunsun on Game Development See other posts from Game Development or by sunsunsunsunsun
Published on 2014-06-10T21:45:04Z Indexed on 2014/06/10 21:48 UTC
Read the original article Hit count: 266

Filed under:
|
|
|

Im trying to make a voxel engine similar to minecraft as a little learning experience and a way to learn some opengl. I have created a chunk class and I want to put all of the vertices for the whole chunk into a single VBO. I was previously only putting each block into a vbo and making a call to render each block. Anyways, I am a bit confused about how I can translate the coordinates of each block in the chunk when I'm putting all vertices into one vbo.

This is what I have at the moment.

public void putVertices(float tx, float ty, float tz) {
    float l_length = 1.0f;
    float l_height = 1.0f;
    float l_width = 1.0f;
    vertexPositionData.put(new float[]{
            xOffset + l_length + tx, l_height + ty, zOffset + -l_width + tz,
            xOffset + -l_length + tx, l_height + ty, zOffset + -l_width + tz,
            xOffset + -l_length + tx, l_height + ty, zOffset + l_width + tz,
            xOffset + l_length + tx, l_height + ty, zOffset + l_width + tz,

            xOffset + l_length + tx, -l_height + ty, zOffset + l_width + tz,
            xOffset + -l_length + tx, -l_height + ty, zOffset + l_width + tz,
            xOffset + -l_length + tx, -l_height + ty, zOffset + -l_width + tz,
            xOffset + l_length + tx, -l_height + ty, zOffset + -l_width + tz,

            xOffset + l_length + tx, l_height + ty, zOffset + l_width + tz,
            xOffset + -l_length + tx, l_height + ty,zOffset +  l_width + tz,
            xOffset + -l_length + tx, -l_height + ty,zOffset +  l_width + tz,
            xOffset + l_length + tx, -l_height + ty, zOffset + l_width + tz,

            xOffset + l_length + tx, -l_height + ty, zOffset + -l_width + tz,
            xOffset + -l_length + tx, -l_height + ty,zOffset +  -l_width + tz,
            xOffset + -l_length + tx, l_height + ty, zOffset + -l_width + tz,
            xOffset + l_length + tx, l_height + ty, zOffset + -l_width + tz,

            xOffset + -l_length + tx, l_height + ty, zOffset + l_width + tz,
            xOffset + -l_length + tx, l_height + ty, zOffset + -l_width + tz,
            xOffset + -l_length + tx, -l_height + ty, zOffset + -l_width + tz,
            xOffset + -l_length + tx, -l_height + ty,zOffset +  l_width + tz,

            xOffset + l_length + tx, l_height + ty,zOffset +  -l_width + tz,
            xOffset + l_length + tx, l_height + ty, zOffset + l_width + tz,
            xOffset + l_length + tx, -l_height + ty, zOffset + l_width + tz,
            xOffset + l_length + tx, -l_height + ty, zOffset + -l_width + tz

    });
}

public void createChunk() {
    vertexPositionData = BufferUtils.createFloatBuffer((24*3)*activateBlocks);

    Random random = new Random();
    for (int x = 0; x < CHUNK_SIZE; x++) {
        for (int y = 0; y < CHUNK_SIZE; y++) {
            for (int z = 0; z < CHUNK_SIZE; z++) {
                if(blocks[x][y][z].getActive()) {
                    putVertices(x*2.0f, y*2.0f, z*2.0f);
                }
            }
        }
    }

Whats any easy way to translate the vertices of each block into its correct position? I was previously using glTranslatef with each call to render block but this wont work now. What I am doing now also does not work, the blocks all render in stacks on top of each other and it looks like this:

http://i.imgur.com/NyFtBTI.png

Thanks

© Game Development or respective owner

Related posts about opengl

Related posts about java