ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm a beginner with OpenGL. I'm now writing a program to learn OpenGL, and I'm confused by the way lighting works.
I've programmed a sort of room without a ceiling, quite basic. There's also a cube in the room. I haven't used any stacks to make this.
I programmed moving around with the glTranslatef command, and rotating with glTranslatef (back to the center), glRotatef (rotating) and glTranslatef again(back to original position). Moving works really fine.
But now I started adding light to my scene, and ran in trouble: where-ever I put the light, the amount of light always seems dependent on your own position (not on the light's position). So when I am in the one side of the room, everything is dark. When I go to the other side, everything is light.
This code is a light that makes everything really bright, except when I'm near to the x-axis. The walls in the room are on the x-axis, z-axis, 50 units parallel from the x-axis and 50 units parallel from the z-axis. The floor is the x-z-plane.
The light position is affected by the modelview matrix. You don't show any of your modifications to that when you move, but try setting your light positions before or after your modelview transformations to see how it differs.
Also, lighting in OpenGL is vertex-based, so if you have a quad where the lighting doesn't affect any of the vertices much, it may seem darker than it should if the light should be affecting the middle of that quad. You can improve your lighting results by tessellating your quads more. (Break single quads up into multiple quads...)
Here's an excerpt from the glLight man page, discussing setting the GL_POSITION property of a light:
GL_POSITION params contains four integer or floating-point val-
ues that specify the position of the light in homo-
geneous object coordinates. Both integer and
floating-point values are mapped directly. Neither
integer nor floating-point values are clamped.
The position is transformed by the modelview matrix
when glLight is called (just as if it were a
point), and it is stored in eye coordinates. If
the w component of the position is 0, the light is
treated as a directional source. Diffuse and spec-
ular lighting calculations take the light's direc-
tion, but not its actual position, into account,
and attenuation is disabled. Otherwise, diffuse
and specular lighting calculations are based on the
actual location of the light in eye coordinates,
and attenuation is enabled. The initial position
is (0, 0, 1, 0); thus, the initial light source is
directional, parallel to, and in the direction of
the -z axis.
I'm reading the first chapters of the red book again, and now I finally understand the story about the different matrices. I think I understand my problem: I made my world, and after that I did viewing transformations, and for every movement I just added more viewing transformations, so I moved the light with me as I noticed.
I'm gonna read on until I understand it all. Apparently OpenGL is not something to fool around with. Then I will have to rewrite big parts of my 'game'.