Jump to content

  • Log In with Google      Sign In   
  • Create Account

Sun Light with OpenGL


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 STORM76   Members   -  Reputation: 148

Like
0Likes
Like

Posted 04 August 2005 - 09:15 PM

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

Sponsor:

#2 Rasmadrak   Members   -  Reputation: 196

Like
0Likes
Like

Posted 05 August 2005 - 12:42 AM

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?

#3 STORM76   Members   -  Reputation: 148

Like
0Likes
Like

Posted 05 August 2005 - 02:29 AM

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.

#4 Kalidor   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 05 August 2005 - 02:57 AM

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.

#5 STORM76   Members   -  Reputation: 148

Like
0Likes
Like

Posted 05 August 2005 - 04:09 AM

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.

#6 MARS_999   Members   -  Reputation: 1297

Like
0Likes
Like

Posted 05 August 2005 - 11:27 AM

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









Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS