Sun Light with OpenGL

Started by
4 comments, last by MARS_999 18 years, 8 months ago
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
Greetings,STORM!
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?
"Game Maker For Life, probably never professional thou." =)
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.
Greetings,STORM!
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.
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.
Greetings,STORM!
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



This topic is closed to new replies.

Advertisement