Lighting
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?
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]
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]
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);
If you want an ambient world light, try an ambient lighting model:
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambient_light);
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.
And sorry about the mix up with 1's and 0's before. : )
glLoadIdentity();glTranslatef(0,0,0)glLightfv(GL_LIGHT0,GL_POSITION, pos);
And sorry about the mix up with 1's and 0's before. : )
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement