OpenGL Multiple Lights - opengl

I'm attempting to add a second light to my scene. I was under the impression that all I needed to do, was enable another light (LIGHT1 in this case), and set it's parameters in order for it to work alongside the existing light. With this in mind, this is my lighting initialization:
void ThemePark::lightInit(GLfloat sun_position[], GLfloat light1_position[])
{
// Enable Lighting
glEnable(GL_LIGHTING);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
glLightfv(GL_LIGHT0, GL_POSITION, sun_position); // Position the lights
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
// Set light intensity and color for each component
glLightf(GL_LIGHT0, GL_DIFFUSE, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT0, GL_AMBIENT, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT0, GL_SPECULAR, (1,1,1,1));
glLightf(GL_LIGHT1, GL_DIFFUSE, (0.7,0.7,0.7,1));
glLightf(GL_LIGHT1, GL_AMBIENT, (0.3,0.3,0.3,1));
glLightf(GL_LIGHT1, GL_SPECULAR, (1,1,1,1));
// Set attenuation
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.5);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, -1.0);
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.2);
// Enable Lights
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
}
I am also positioning my lights again in my display function as follows:
if(lighting)
{
glLightfv(GL_LIGHT0, GL_POSITION, sun_position);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
}
Their positions are:
GLfloat sun_position[] = {10, 10, -2, 1};
GLfloat light1_position[] = {1, 1, 1, 1};
However, when doing this I still only have a single active light, LIGHT0. Taking out the enable statement for LIGHT0 gives me a scene with no lights. As a test, I modified the lighting initialization function to the following, which essentially makes LIGHT1 the same as LIGHT0, but never enables or initializes LIGHT0.
void ThemePark::lightInit(GLfloat sun_position[], GLfloat light1_position[])
{
// Enable Lighting
glEnable(GL_LIGHTING);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0);
glLightfv(GL_LIGHT1, GL_POSITION, sun_position);
// Set light intensity and color for each component
glLightf(GL_LIGHT1, GL_DIFFUSE, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT1, GL_AMBIENT, (0.5,0.5,0.5,1));
glLightf(GL_LIGHT1, GL_SPECULAR, (1,1,1,1));
// Set attenuation
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.5);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, -1.0);
// Enable Lights
glEnable(GL_LIGHT1);
}
I also modified my display function accordingly. However, I still see no lighting in my scene. Is there something I'm missing here?

GL_LIGHT1-7 actually have different defaults than GL_LIGHT0 ... so you will have to find the ones you forgot to set. Perhaps GL_QUADRATIC_ATTENUATION? If not try setting all the params in the spec Many of them default to zero/off states for GL_LIGHT1-7.

Related

OpenGL Lightning

After enabling lighting in OpenGL, color in lines and polygons are no longer rendering. (Created color from glColor3f() )
This is code:
glLightModelf(GL_LIGHT_MODEL_AMBIENT, GL_TRUE);
GLfloat light1_position[] = { 5000, 2000, 1000,1 };
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light1_position);
GLfloat light_ambient[] = { 1,1,1,1 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glEnable(GL_LIGHTING);
What am I doing wrong?
You must enable these things before using them :
glEnable ( GL_LIGHTING);
glEnable( GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glShadeModel( GL_SMOOTH);
You do not have to use GL_SMOOTH, you can also use GL_FLAT.

C++ Opengl - lighting using spotlight

I have a model that needs to be under a spotlight/directional light,
Meaning, I need to switch between the modes (Spotlight and directional).
Here is the code with some explanation:
I can rotate the model / light source with mouse movements so I am using
glRotate and glTranslate for that.
Once the user pressed the 'L' key I'm supposed to switch between the modes.
here is the code for the lightning:
void LightBall::projectLight(void)
{
if(LIGHT == _lightMode){
printf("Entering LIGHT mode\n"); <--- Supposed to be a directional light
glDisable(GL_LIGHT1);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, _light_position);
}
if(SPOT_LIGHT == _lightMode){
printf("Entering SPOTLIGHT mode\n"); <--- Supposed to be a spotlight
glDisable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_POSITION, _light_position);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 10.0);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
glLightfv(GL_LIGHT1,GL_SPOT_DIRECTION,_spotlight_position);
}
}
The problem is I always get the same light mode when switching between them,
Which is the following:
And another example after switching between 2 light modes and still getting same light
source with light source rotation (the small ball):
How can I get the wanted result?
Here are the LIGHT0 & LIGHT1 definitions:
GLfloat light_ambient[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
_light_position[0] = 0.0;
_light_position[1] = 1.0;
_light_position[2] = 0.0;
_light_position[3] = 0.0;
_spotlight_position[0] = 0.0;
_spotlight_position[1] = -1.0;
_spotlight_position[2] = 0.0;
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
Thanks!
Whether a GL light is a directional light or a spotlight depends on the w (4th) component of its position. If the coordinate is 0, it's directional. If nonzero (usually 1), it's a spotlight. You'll have to modify _lightPosition accordingly before calling glLightfv(..., GL_POSITION, ...).

OpenGL lighting don't work

I'm trying to setup lighting in my scene
but for some reason it doesn't work, i'm trying to figure out why
here's how I initialize OpenGL:
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHT0);
and here is how I try to draw with lighting on:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GLfloat diffuse0[]={1.0, 1.0, 1.0, 1.0};
GLfloat ambient0[]={1.0, 1.0, 1.0, 1.0};
GLfloat specular0[]={1.0, 1.0, 1.0, 1.0};
GLfloat light0_pos[]={1.0, 1.0, 1,0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular0);
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 2.0);
object->draw();
the object is not affected by the lighting at all
all normals are set and all the information that opengl require for lighting a scene
is there... what am I missing?
EDIT:
OK! I finally found a solution to the problem.
my opengl init function was misplaced in the code
I had to call it after showing the window and not before
and that's what caused the problem...
Probably you should tell us the position of the object.
Suspect 1: The position of the object and the light are too far
Suspect 2: Hide all ATTENUATION settings or make LINEARATTENUATION to 0.0

working with openGL

im trying to do a little 3d scene with openGL and include a bit of lighting, im fine with my scene (although it isnt anything special) and im trying to add some lighting to give it some effect. however I can add material to my podium (which isnt textured) and that gives me light and anything that IS textured does not apply any material to it so it defies the point of having lights. Heres some of the code.
// Setup GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular); // Setup specular light
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, qa);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.0);
// Setup GL_LIGHT1
glLightfv(GL_LIGHT1, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT1, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT1, GL_SPECULAR, lightSpecular); // Setup specular light
glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, qa);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 0.0);
// Setup GL_LIGHT2
glLightfv(GL_LIGHT2, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT2, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT2, GL_SPECULAR, lightSpecular); // Setup specular light
glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, qa);
glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 0.0);
// Setup GL_LIGHT3
glLightfv(GL_LIGHT3, GL_AMBIENT, lightAmbient); // Setup ambient light
glLightfv(GL_LIGHT3, GL_DIFFUSE, lightDiffuse); // Setup diffuse light
glLightfv(GL_LIGHT3, GL_SPECULAR, lightSpecular); // Setup specular light
glLightf(GL_LIGHT3, GL_CONSTANT_ATTENUATION, ca);
glLightf(GL_LIGHT3, GL_LINEAR_ATTENUATION, la);
glLightf(GL_LIGHT3, GL_QUADRATIC_ATTENUATION, qa);
glLightf(GL_LIGHT3, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHT3, GL_SPOT_EXPONENT, 0.0);
// OpenGL provides a global ambient light component - we don't want this so set to zero
GLfloat global_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
And to render my room + 3d object.
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
glEnable(GL_LIGHT3);
// Define position and direction
// NOTE: Placing these commands AFTER the above viewing (camera) transformations means the light position and direction appear to have a set point / direction in the 3D environment. If these commands are placed BEFORE the above viewing transformations, then the light appears to move with the camera (as if it is attached to the camera!)
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, lightDirection);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, lightDirection1);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition1);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, lightDirection2);
glLightfv(GL_LIGHT2, GL_POSITION, lightPosition2);
glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, lightDirection3);
glLightfv(GL_LIGHT3, GL_POSITION, lightPosition3);
// setup materials for objects to draw
glMaterialfv(GL_FRONT, GL_AMBIENT, ambientMaterial);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularMaterial);
// 2. Draw scene
glPushMatrix();
drawRoom();
glTranslatef(0.0,-0.75,0.0);
glRotatef(90.0, 1.0,0.0, 0.0);
glRotatef(-spin, 0.0,0.0, 1.0);
glColor3f(1.0, 1.0, 1.0);
drawPodium();
glPopMatrix();
// Save transformations prior to rendering scene
glPushMatrix();
glColor3f(0.0, 0.0, 1.0);
// Setup texture state
glEnable(GL_TEXTURE_2D);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glRotatef(spin, 0.0,1.0, 0.0);
// Render model
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial2);
glTranslatef(0.0,0.8,0.0);
glRotatef(1.0,1.0,0.0,0.0);
myModel->renderTexturedModel();
// Reset texture state
glDisable(GL_TEXTURE_2D);
// restore transformations after rendering scene
glPopMatrix();
And the current outcome with my 4 lights, in a square shape above the helicopter all looking down towards 0.0 is:
If you can make out the white dotted lines they are the direction the lights are looking. the surface of the podium is lit and the sides of the podium are lit as it spins. However the rest of the room and helicopter does not respond to light and act like lighting isnt even enabled hence why i think its because everything except the podium is textured.
You need to setup glTexEnv to specify how lighting should work for the textured objects
See:
21.030 Why doesn't lighting work when I turn on texture mapping?
http://www.opengl.org/resources/faq/technical/texture.htm
Or to address your problem more directly, the line
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
is what causes the behaviour. As the faq explains, when you specify GL_REPLACE, you replace the primitive lighting colour with the texture colour, which overwrites any lighting calculations. You can remove this line completely as GL_MODULATE is the default behaviour.
I don't know if the above method works for you. I had the same problem. My problem was that I had not specified the normals of the object.
//specify normals
glNormal3f();
hopefully this will solve the problem for anyone (i tried every other method over the internet but nothing worked; only this method worked).

Making a light source visible in OpenGL

I want to move my light source in my OpenGL-Scene, which is working. But to actually I want to see thee light source as well.
How can I do this?
...
glPushMatrix();
GLfloat lightPos[] = {0, 0, 200, 1};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
GLfloat ambientLight[] = { 0.2, 0.2, 0.2, 1.0};
GLfloat lightColor[] = { 0.5, 0.5, 0.5, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor);
glutSolidSphere(5,50,50);
glPopMatrix();
...
A light source is not a visible object, per se. If you want to "see" a light, you have to place some object at the position that your light is. Maybe use a sphere, a sprite, an arrow (pointing in the direction the light is pointing in, if it's a directional light), etc.