javaopengllwjglvoxelshadows

Cube normals on voxel engine producing odd results


I am trying to add normals to my voxel engines 16x16 cube chunks. When I render everything I get odd results. I am not sure if I have placed my normals incorrectly or if implementing simple shadows with lighting wont work how I am using it.

The following is the vertexes for each of my cubes and there respective normals.

public void putVertices(float tx, float ty, float tz, FloatBuffer vertexPositionData) {
    float l_length = Constants.BLOCK_SIZE;
    float l_height = Constants.BLOCK_SIZE;
    float l_width = Constants.BLOCK_SIZE;
    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 putNormals(FloatBuffer vertexNormalData) {
    float[] normals = new float[] {
            0.0f, 0.0f, -1.0f,
            0.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f,
            -1.0f, 0.0f, 0.0f,
            0.0f, -1.0f, 0.0f,
            0.0f, 1.0f, 0.0f,
    };

This is what it looks like when I run the program, Weird shadows with normals

It appears that the light is calculated as if each block surrounding it isnt there. How can I correct this? The result should look seamless when blocks are flat like in the picture.


Solution

  • You have 24 positions, but only 6 normal vectors, in your vertex data. You need a normal vector for each vertex. So with 24 positions, you also need 24 normal vectors.

    Since the 4 vertices per side have the same normal vector, you need to repeat each of the normal vectors in your normals array 4 times, which will give you the necessary 24 normal vectors.

    vertexNormalData.put(new float[] {
            0.0f, 0.0f, -1.0f,
            0.0f, 0.0f, -1.0f,
            0.0f, 0.0f, -1.0f,
            0.0f, 0.0f, -1.0f,
            0.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f,
            1.0f, 0.0f, 0.0f,
            1.0f, 0.0f, 0.0f,
            1.0f, 0.0f, 0.0f,
            1.0f, 0.0f, 0.0f,
            -1.0f, 0.0f, 0.0f,
            -1.0f, 0.0f, 0.0f,
            -1.0f, 0.0f, 0.0f,
            -1.0f, 0.0f, 0.0f,
            0.0f, -1.0f, 0.0f,
            0.0f, -1.0f, 0.0f,
            0.0f, -1.0f, 0.0f,
            0.0f, -1.0f, 0.0f,
            0.0f, 1.0f, 0.0f,
            0.0f, 1.0f, 0.0f,
            0.0f, 1.0f, 0.0f,
            0.0f, 1.0f, 0.0f
    });