0
votes

I have been working on loading, rendering and lighting 3D models with LWJGL. It works mostly, but there's currently problem with the lighting, I think its the lighting normals but I can't fix it. Here is a picture the problem;

enter image description here

Lighting code:

public class Light {

private Vector4f position;

public Light(Vector3f position)
{
    this.position = new Vector4f(position.x, position.y, position.z, 1);

    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightModel(GL_LIGHT_MODEL_AMBIENT, getFlipedFloatBuffer(new Vector4f(0.05f, 0.05f, 0.05f, 1)));
    glLight(GL_LIGHT0, GL_POSITION, getFlipedFloatBuffer(this.position));
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_DIFFUSE);
}

private FloatBuffer getFlipedFloatBuffer(Vector4f values)
{
    return (FloatBuffer)BufferUtils.createFloatBuffer(4).put(new float[]{values.x, values.y, values.z, values.w}).flip();
}

public void update() {
    glLight(GL_LIGHT0, GL_POSITION, getFlipedFloatBuffer(this.position));
}
}
1

1 Answers

0
votes

The normals are not evenly oriented. If you do calculate the normals yourself from the geometry, you'd first have to realign all normals to point toward the same surface orientation. The algorithm for this, though, is a bit tedious to get right.

Better just load the model into a 3D modeller, use that modeller's "Fix Normal Orientation" tool and save the models together with the normals. Blender has the function accessible through hotkey CTRL+N