[Solved] Problem with directional light.

Started by
6 comments, last by Voloskaya 14 years, 1 month ago
Hi everyone! Here is my problem, i created a directional light like this:

GLfloat ambient[] = { 1.0, 1.0f, 1.0f, 1.0f };
GLfloat diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat position[] = { 0.0f, 1.0f, 1.0f, 0.0f };
   
glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse );
glLightfv( GL_LIGHT0, GL_POSITION, position );
   
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );


But only faces with a vertical normal are lit (see screenshot below) for a reason that i don't understand. I read a bunch of topics on lights but can't find a solution. Screenshot: http://img14.imageshack.us/img14/8943/capturevf.jpg Thanks for your help! [Edited by - Voloskaya on March 17, 2010 11:30:07 PM]
Advertisement
Are you sure your mesh normals are right?

[Edit: Also, it could have something to do with how your transforms are set up and where you're setting the lighting parameters (getting fixed-function lighting to work correctly with arbitrary view and model transforms in OpenGL can be a little confusing).]
Yep my normals are corrects, i checked and checked again.
I'm also checking my transformations as you mentioned but didn't find anything so far!
Quote:Original post by Voloskaya
Yep my normals are corrects, i checked and checked again.
I'm also checking my transformations as you mentioned but didn't find anything so far!
Hm, it's hard to say what the problem might be, since we don't know what all is going on in your code. If the code is short enough (e.g. a single source file), you could try posting it here so that we could take a look.

Also, I'll go ahead and point out that the fixed-function pipeline is on its way out, more or less. Unless you have a particular reason for sticking with the FFP, I'd recommend looking into a shader-based solution instead (IMX at least, working with the programmable pipeline is actually easier and less complicated overall than working with the fixed-function pipeline, for what it's worth).
Well I wouldn't expect the vertical sides to be lit. Here's why:
    D = direction of light vector = <0, 1, 1>    N = normal of a vertical side = <a, 0, 0>    dotProduct(D, N) = 0*a + 1*0 + 1*0 = 0
In other words, the lighting contributes no diffuse component. If you want the sides to be lit too, consider changing the directional light's vector. For example, try GLfloat position[] = { 1.0f, 1.0f, 1.0f, 0.0f };

But note that above I'm assuming no transformations. Since the directional light does have a z component I would expect that rotating the model *should* light the sides. I'm not too knowledgeable in the area of fixed-function lighting in OpenGL though.
Quote:Original post by aryx
Well I wouldn't expect the vertical sides to be lit. Here's why:
    D = direction of light vector = <0, 1, 1>    N = normal of a vertical side = <a, 0, 0>    dotProduct(D, N) = 0*a + 1*0 + 1*0 = 0
In other words, the lighting contributes no diffuse component. If you want the sides to be lit too, consider changing the directional light's vector. For example, try GLfloat position[] = { 1.0f, 1.0f, 1.0f, 0.0f };

But note that above I'm assuming no transformations. Since the directional light does have a z component I would expect that rotating the model *should* light the sides.
Assuming the cube has six sides, at least one of the vertical sides should indeed be lit. Now, depending on how the transforms are set up (which the OP hasn't specified), it could be the near side, which is facing away from us and is not visible.

That said, it's not quite clear from the picture (to me at least) what is lit and what isn't. It looks like the top and bottom surfaces are lit, which you wouldn't expect given the lighting setup. (It would probably be easier to tell what's going on if the surfaces were all rendered with exactly the same properties, including the same texture and texture coordinates.)
Quote:Original post by jyk
Assuming the cube has six sides, at least one of the vertical sides should indeed be lit. Now, depending on how the transforms are set up (which the OP hasn't specified), it could be the near side, which is facing away from us and is not visible.

You're right, there was a lapse in my thought there for a moment. I zoned in on the sides whose normals only have a "horizontal" component. Thanks for clearing that up. I expect that what you point out is quite possibly the issue here.

I'll also second your comment on moving away from the fixed-function pipeline if there's no reason to stick with it.
Actually aryx was right, changing light position to { 1.0f, 1.0f, 1.0f, 0.0f } solved my problem, i didn't thought the fact that my light was parralel to a face would affect it's lighting as i was trying to create an ambient light and so considered that it should lit in every direction equally... Seems like i still have a lot to learn!
And, jyk was right too: "Assuming the cube has six sides, at least one of the vertical sides should indeed be lit.", i'm ashamed to say that the the normal of the front side, that i checked and checked again was... false, sorry for that, hope i didn't waste too much of your time on this!

Regarding what you said about FFP, i'll keep that in mind as i'm currently beginning to learn GLSL.

Thanks to both of you for your precious help!

This topic is closed to new replies.

Advertisement