Search code examples
opengllwjglshadows

LWJGL having shadows for the entire 3D environment


I am having a bit of trouble with shadows in OpenGL. The models have a shadow on every face, OpenGL gives it a darkness through which direction the face is pointing from the source of light. I noticed that the light just passes through the faces, meaning if two planes where right next to each other and a light source is on one side, the plane on the farther side is still light up.

How would I stop this from happening? I need some shadow code that works for the entire 3D environment.

private static void setUpLighting() {
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightModel(GL_LIGHT_MODEL_AMBIENT, BufferTools.asFlippedFloatBuffer(new float[]{0.05f,
            0.05f, 0.05f, 1f}));
    glLight(GL_LIGHT0, GL_POSITION,
            BufferTools.asFlippedFloatBuffer(new float[]{0, 0, 0, 1}));
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_DIFFUSE);
    glEnable(GL_TEXTURE_2D);
}

Solution

  • I noticed that the light just passes through the faces, meaning if two planes where right next to each other and a light source is on one side, the plane on the farther side is still light up.

    You mistake OpenGL for something that isn't: OpenGL is not a scene graph. There's no internal scene representation in OpenGL. Every point, line and triangle you send it to draw it processed individually without taking into account any of the other points, lines and triangled being drawn. In fact OpenGL acts more like sort of sophisticated paper and pencils.

    Since OpenGL doesn't maintian a scene representation, there's no way it could do a so called "global illumination" model on its own. So if you want to have shadows in your scene, it's upon you, to manage a scene and implement drawing algorithms (that use OpenGL) which outcome looks like shadows and global illumination.

    You should look into the topics of "Stencil Volume Shadows" and "Depth Buffer Shadow Maps".