• Advertisement
Sign in to follow this  

This topic is 4052 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

How do I make an ambient light that sets a world light? I've looked through the red book but it's kinda vague how it's said... this is what i have so far float light[] = {1.0f, 1.0f, 1.0f, 1.0f}; float pos[] = {0.0f, -1.0f, 0.0f, 0.0f}; glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_AMBIENT, light); glLightfv(GL_LIGHT0, GL_DIFFUSE, light); glLightfv(GL_LIGHT0, GL_POSITION, pos); but everything is still really dark and seems to somewhat of a green tint... is there a reason for this?

Share this post


Link to post
Share on other sites
Advertisement
I think your positioning is wrong.

You have your fourth value set to 0. The fourth value controls whether the light is a directional or source light. If the value is 0, then it is light coming from an object inside your scene(such as a lamp), and the other three values are it's position in world coordinates. If the fourth value is non-zero, then the light is a directional light. This means that the light is coming from an object infinitelly far away from your scene(such as the sun), and the first three values specify direction(so GLfloat lpos[]={0.0, 1.0f,1.0f,1.0f}; would be an infinetally far light source coming from the top right corner of the screen).

You have your light set to be a source light, so your light is at -1.0f on the x axis. Depending on how you draw your scene, then your light could be inside your object, wich would not look right.


So try moving the light and object around, and if you don't need your light to be a source light change it, because directional lights are easier.


[EDIT]: Non-zero is a source(positional) light, 0 is a directional light.
-Levi


[Edited by - levjs on March 22, 2007 5:29:20 PM]

Share this post


Link to post
Share on other sites
After practicing what works and what doesn't I figured a lot out. First, having the 4th component 0 makes a directional light, anything else (1) is positional. Also, which brings me to my question; is seems that the light i have (whether it be directional or positional) seems to always be in relation to my "camera", so when i have a light positioned at (-100.0f, 150.0f, 0.0f, 1.0f), it seems that the light is coming out of that position in relation to the camera. Is there a way to disable that? or am i doing it completely wrong, here's my code:


float amb[] = {1.0f, 1.0f, 1.0f, 1.0f};
float dif[] = {1.0f, 1.0f, 1.0f, 1.0f};
float pos[] = {-100.0f, 40.0f, 0.0f, 1.0f};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);

//glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
glLightfv(GL_LIGHT0, GL_POSITION, pos);

Share this post


Link to post
Share on other sites
Ok, this is easy to solve, you just have to position the camera in world coordinates. After drawing all the other objects in your scene, put the following code in your drawing function.




glLoadIdentity();
glTranslatef(0,0,0)
glLightfv(GL_LIGHT0,GL_POSITION, pos);



And sorry about the mix up with 1's and 0's before. : )

Share this post


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

  • Advertisement