-1

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);
}
genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

2

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".

datenwolf
  • 159,371
  • 13
  • 185
  • 298
  • I have been searching on how to use those but none of them work – Jasper Creyf Oct 17 '12 at 05:41
  • @JasperCreyf: What did you try? "It does not work" is the most generic, undetailed error description imaginable. – datenwolf Oct 17 '12 at 08:24
  • 3
    @JasperCreyf: I took your comment as that you did try to implement the algorithm yourself. For working demos: Here's a Stencil Shadows demo http://humus.name/index.php?page=3D&ID=10 and here's a Shadow Mapping demo (that also does color shadows!) http://humus.name/index.php?page=3D&ID=39 – datenwolf Oct 17 '12 at 16:58
  • Thank you. I have found several that are in C, but I don't know C at all. This is why in the title and tags I have LWJGL. I know that the vertex and fragment shader files are in C, and this is why I have been looking for examples. – Jasper Creyf Oct 17 '12 at 22:22
  • 3
    @JasperCreyf: The language doesn't matter, what matters are the concepts and ideas of the algorithms involved. And what's done in C does work 1:1 in Java's OpenGL bindings. And no, shaders are not written in C, but in GLSL. – datenwolf Oct 17 '12 at 23:05