0
votes

I'm fairly new to LWJGL/OpenGL and I've come across this problem which I can't fix, whatever I try:

When I draw multiple cubes to the screen, the light is the same brightness when it hits it, regardless of how far away the cube is from the 'camera'. This means that when I draw lots of cubes on the screen, they get the same amount of light as each other and it looks strange.

Here's a GIF demonstrating what I mean.

My source code: http://pastebin.com/qd5dsZ89

How can I fix it so that the light blends across all of them without sudden breaks?

1
Tip: Reduce the size of the code base down to a small sample of what you think may be the problem. Play with it from there and see if you can either solve the solution (ideal) or observe where your error may be coming from. Asking others to read your entire code is a bit much.Makoto
What kind of lighting are you trying to achieve? Fixed position lighting (sun), or moving with camera (flashlight)? Do you want it to falloff with distance?Tim
I'm aiming for a fixed light which falls off with distance. I set it to be moving with the camera for the demonstration.user1248420

1 Answers

5
votes

The important bit of your code:

            // Render a wall
            glTexCoord2f(0, 0);
            glNormal3f(wall.a.x > 0 ? 1 : -1, wall.a.y > 0 ? 1 : -1, wall.a.z > 0 ? 1 : -1);
            glVertex3f(wall.a.x, wall.a.y, wall.a.z);
            glTexCoord2f(xS, 0);
            glNormal3f(wall.b.x > 0 ? 1 : -1, wall.b.y > 0 ? 1 : -1, wall.b.z > 0 ? 1 : -1);
            glVertex3f(wall.b.x, wall.b.y, wall.b.z);
            glTexCoord2f(xS, yS);
            glNormal3f(wall.c.x > 0 ? 1 : -1, wall.c.y > 0 ? 1 : -1, wall.c.z > 0 ? 1 : -1);
            glVertex3f(wall.c.x, wall.c.y, wall.c.z);
            glTexCoord2f(0, yS);
            glNormal3f(wall.d.x > 0 ? 1 : -1, wall.d.y > 0 ? 1 : -1, wall.d.z > 0 ? 1 : -1);
            glVertex3f(wall.d.x, wall.d.y, wall.d.z);

If I read correctly, that gives the 8 corners of your cube normal vectors pointing outwards. All the vertices (of different faces) at the same corner have the same normal. Thus, your making a 'rounded' cube, there are no edges visible on a single cube.

I would give all the vertices making up one side of the cube the same normal. For example, for the x-most wall, use the normal (1,0,0) for all its faces.

Solution: call glNormal3f once for each side with the correct normal vector.