lighting in OpenGL (with C language)
Hello everybody,
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. Code:
GLfloat light_position[] = { 0.0, -1.7, 2.0, 1.0 }; Thanks, Corien |
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...) Hope that helps somewhat. |
Oh boy, apparently I haven't understood modelviewmatrices and stuff well. I'll hide myself in documentation and come back in a few weeks of so, lol :). Hard stuff, opengl...
I knew about the vertex-problem. Breaking up my walls and floor already helped something. Thanks, now I have an idea what causes the problem. Corien |
Here's an excerpt from the glLight man page, discussing setting the GL_POSITION property of a light:
Quote:
|
Good to know OpenGL has manpages!
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'. Thanks for the help! Corien |
All times are GMT -5. The time now is 02:50 PM. |