Jump to content
  • Advertisement
Sign in to follow this  
STORM76

OpenGL Sun Light with OpenGL

This topic is 4848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, I am trying to build a small OpenGL 3D engine with a simple SceneGraph for scene-management. Now, that there exist directional and positional lights, I decided to use a directional light source as my overall sun light for the scene. Thus, the lighting information is placed in the root-node of the scene-graph. My implementation looks like this: //... glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //... float dir[4]; dir[0] = sunLight.GetDirection().x; dir[1] = sunLight.GetDirection().y; dir[2] = sunLight.GetDirection().z; dir[3] = 0.0f; glLightfv(GL_LIGHT0, GL_POSITION, dir); glLightfv(GL_LIGHT0, GL_AMBIENT, sunLight.GetAmbient().rgba); glLightfv(GL_LIGHT0, GL_DIFFUSE, sunLight.GetDiffuse().rgba); glLightfv(GL_LIGHT0, GL_SPECULAR, sunLight.GetSpecular().rgba); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); //... // traverse the scene graph and render objects //... Now, I have the following problem: when I add a big cube to the scene with a small cube attached as a child (like the earth-moon-relationship), the big cube is lit correctly. But the small cube is not. In the redbook I found the hint, that the direction of a directional light is transformed by the modelview-matrix, too. Thus, after rendering the big cube, other scene-nodes (like translation, rotation and scale) modify the modelview-matrix to position and place the small cube properly. Unfortunately, the sun-light direction seems to be transformed, too. I also tried the following for testing: // later within my renderer... //... // render object node: glPushMatrix(); glLoadMatrixf(sunMatrix.m); float dir[] = {1.0f, 1.0f, 1.0f, 0.0f}; glLightfv(GL_LIGHT0, GL_POSITION, dir); glEnable(GL_LIGHT0); glPopMatrix(); // render cube... The sunMatrix is the matrix saved right after setting up the viewport and camera-position and -direction. But the effect is still the same. The big cube is lit properly and the small cube is also lit, but from another (wrong) direction. Can anybody help me on this issue? Thanks in advance! Guido

Share this post


Link to post
Share on other sites
Advertisement
you must position the lights before drawing anything...


i.e


PositionSun();

..

DrawStuff();



EDIT: Just saw that you did this already... :/ dunno what the problem is... perhaps you´re disabling lighting after you´ve drawn the first cube?

Share this post


Link to post
Share on other sites
Hm, I will check this. Additionally I will introduce a sphere object to see the sun-light direction better, because the cubes aren't lit very fine granulated.

Share this post


Link to post
Share on other sites
Quote:
Original post by STORM76
In the redbook I found the hint, that the direction of a directional light is transformed by the modelview-matrix, too. Thus, after rendering the big cube, other scene-nodes (like translation, rotation and scale) modify the modelview-matrix to position and place the small cube properly. Unfortunately, the sun-light direction seems to be transformed, too.
Light positions are transformed just like any vertex. They are transformed only when you call glLight with pname as GL_POSITION. The usual order of things is at the beginning of the frame you load the identity matrix and set up your camera transform (gluLookAt probably), then after that you send your light's position, and then for each object you set its transform and render it.
Quote:
Original post by STORM76
The sunMatrix is the matrix saved right after setting up the viewport and camera-position and -direction. But the effect is still the same. The big cube is lit properly and the small cube is also lit, but from another (wrong) direction.
To me that sounds like the normals are incorrect for your small cube. You should render the normals as well to make sure they look alright.

Share this post


Link to post
Share on other sites
Yes, I do render my normals and they look right. Do you know, if the following has any effect to the GL_POSITION vector?

// before rendering an object:
glPushMatrix();
glLoadIdentity();
// set light direction
glPopMatrix();
// render object:
// ...

I have already tested this but without success. But I do not understand why it doesn't work.

Share this post


Link to post
Share on other sites
Quote:
Original post by STORM76
Yes, I do render my normals and they look right. Do you know, if the following has any effect to the GL_POSITION vector?

// before rendering an object:
glPushMatrix();
glLoadIdentity();
// set light direction
glPopMatrix();
// render object:
// ...

I have already tested this but without success. But I do not understand why it doesn't work.


Do this,


glLoadIdentity();

glLookAt();

glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
//render stuff here




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!