Search code examples
javaopenglvoxelsimplex

Voxel chunk rendering upside down


I am trying to implement a voxel engine with opengl in Java. When I try to render a voxel chunk on the screen with some simplex noise to generate terrain it appears to be upside down. I am assuming it's because of the was I am looping through my chunk to generate it.

    for (int x = 0; x < CHUNK_SIZE; x++) {
        for (int z = 0; z < CHUNK_SIZE; z++) {
            int height = (int)(sno.noise(x/16f, z/16f)*Math.random()*16);
            if(height <= 0)
                height = 1;
            for (int y = 0; y < height; y++) {
                blocks[x][y][z].setActive(true);
                activateBlocks += 1;
            }
        }
    }
}

public void drawChunk() {
        GL11.glPushMatrix();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOVertexHandle);
        GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0L);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBOTextureHandle);
        GL11.glColorPointer(3, GL11.GL_FLOAT, 0, 0L);


        GL11.glDrawArrays(GL11.GL_QUADS, 0, ((24)*activateBlocks));
        GL11.glPopMatrix();
}

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 z = 0; z < CHUNK_SIZE; z++) {
            for (int y = 0; y < CHUNK_SIZE; y++) {
                if(blocks[x][y][z].getActive()) {
                    putVertices(x*2, y*2, z*2);
                }
            }
        }
    }

    vertexPositionData.flip();

}

Any ideas?

Here is what it looks like in game -

Chunk upside down


Solution

  • Without seeing your rendering code it's hard to be sure, but I would bet good money that you have forgotten that Y coordinates in Java start at the top of the screen and go down.