Light Problem

Hello,

I have just enabled a second light, and now I have two lights red and white. Both are enabled, but when executed, it show only the red light, not the white.

GLfloat diffuse_1 = { 1, 1, 1, 1 };
GLfloat diffuse_2 = { 1, 0, 0, 1 };
GLfloat ambient = { .1, .1, .1, 1 };
GLfloat specular = { 1, 1, 1, 1 };
GLfloat shininess = { 50.0 };
GLfloat position = { 1, 1, 1, 0 };

//////////
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glShadeModel( GL_SMOOTH );

glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_LIGHT1 );

glEnable( GL_DEPTH_TEST );
//////////

glPushMatrix();

glMaterialfv ( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse_1 );
glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse_1 );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glLightfv( GL_LIGHT0, GL_POSITION, position );

glTranslated( 0.0, 0.0, 5.0 );
glDisable( GL_LIGHTING );
glColor3f( 1.0, 1.0, 1.0 );
glutWireCube( 0.1 );
glEnable( GL_LIGHTING );

glPopMatrix();

glPushMatrix();

glMaterialfv ( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse_2 );
glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse_2 );
glLightfv( GL_LIGHT1, GL_SPECULAR, specular );
glLightfv( GL_LIGHT1, GL_POSITION, position );

glTranslated( 0.0, 0.0, -5.0 );
glDisable( GL_LIGHTING );
glColor3f( 1.0, 0.0, 0.0 );
glutWireCube( 0.1 );
glEnable( GL_LIGHTING );

glPopMatrix();

Why?

  • VC6-OGL

[This message has been edited by VC6-OGL (edited 01-06-2003).]

I would be surprised if ANY light showed up, cause you disable lighting right before drawing the cube.

As Bob said, you are disabling the lighting right before you draw the first cube. Then you define the color to be white, so you are getting an unlit, white colored cube rasterized and drawn. Then after than you reenable the lighting and set up the red light and then draw the red cube after turning the lighting off. And again it is red (it’s not the lighting) because you set the color to be red.

My contribution to this post is that I was wondering if you were trying to light the same cube using two different lights?

Edit: Forgot to mention…If you are trying to use two lights to render to the same cube then i think the first glTranslated procedure should have a -5.0 for the third argument . If that is what you are trying to do then the following code is all you need:

GLfloat diffuse_1 = { 1, 1, 1, 1 };
GLfloat diffuse_2 = { 1, 0, 0, 1 };
GLfloat ambient = { .1, .1, .1, 1 };
GLfloat specular = { 1, 1, 1, 1 };
GLfloat shininess = { 50.0 };
GLfloat position = { 1, 1, 1, 0 };

/* Set the material so that it reflects all the
colors */
GLfloat material_diffuse = {1.0f,1.0f,1.0f,1.0f};

//////////
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glShadeModel( GL_SMOOTH );

// Declare light 0
glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse_1 );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glLightfv( GL_LIGHT0, GL_POSITION, position );

// Declare light 1
glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse_2 );
glLightfv( GL_LIGHT1, GL_SPECULAR, specular );
glLightfv( GL_LIGHT1, GL_POSITION, position );

// Enable lighting
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);

glEnable( GL_DEPTH_TEST );
//////////
glPushMatrix();
// Transform into the screen by 5 units
glTranslated( 0.0, 0.0, -5.0 );
// Set the material for the following primitives
glMaterialfv ( GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse );
// Draw the cube
glutWireCube( 0.1 );
glPopMatrix();

[This message has been edited by Halcyon (edited 01-06-2003).]

I am trying to create light with both cubes, white and red, on different sides of my Camera. White Light Translatef is 5 and Red Light Translatef is -5.

  • VC6-OGL

If you were trying to draw two different cubes that reflected light two different ways this is the code:

GLfloat diffuse_1[] = { 1, 1, 1, 1 };
GLfloat diffuse_2[] = { 1, 0, 0, 1 };
GLfloat ambient[] = { .1, .1, .1, 1 };
GLfloat specular[] = { 1, 1, 1, 1 };
GLfloat shininess[] = { 50.0 };
GLfloat position[] = { 1, 1, 1, 0 };

/* Set the material so that it reflects all the
   colors */
GLfloat material_diffuse1[] = {1.0f,1.0f,1.0f,1.0f};

/* Set the second material to not reflect red light 
GLfloat material_diffuse2[] = {0.0f,1.0f,1.0f,1.0f};

//////////
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glShadeModel( GL_SMOOTH );

// Declare light 0
glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse_1 );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glLightfv( GL_LIGHT0, GL_POSITION, position );

// Declare light 1
glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse_2 );
glLightfv( GL_LIGHT1, GL_SPECULAR, specular );
glLightfv( GL_LIGHT1, GL_POSITION, position );

// Enable lighting
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);

glEnable( GL_DEPTH_TEST );
//////////

// Translate scene back 5
glTranslated( 0.0, 0.0, -5.0 );

// Draw left cube
glPushMatrix();
   // Transform into the screen by 5 units
   glTranslated( -2.0, 0.0, -0.0 );
   // Set the material for the following primitives
   glMaterialfv ( GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse1);
   // Draw the cube
   glutWireCube( 0.1 );
glPopMatrix();

// Draw right cube
glPushMatrix();
   // Transform into the screen by 5 units
   glTranslated( 2.0, 0.0, 0.0 );
   // Set the material for the following primitives
   glMaterialfv ( GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse2);
   // Draw the cube
   glutWireCube( 0.1 );
glPopMatrix();

Note that the material’s diffuse is not the same as the light’s diffuse. It could be…but the material’s diffuse is telling OpenGL how much of each color the material reflects (how much is visible). If you set the red element to 0, then all the red is absorbed and thus you can’t see it because it is never reflected!

I hope this code is what you are looking for. If it isn’t, then just tell me on here.

  • Halcyon

Edit: Ok, sorry I didn’t see that you replied to my first post so fast. Well I think i’m doing what you are looking for, but instead of along the z axis, the cubes are along the x-axis. But the lighting works. To actuall see it in action, you will have to keep rotating your scene a certain degrees every frame (animate the rotation).

[This message has been edited by Halcyon (edited 01-06-2003).]

Is the above code what you are looking for? I didn’t quite understand everything you said in the small post you made between my two bigs ones. Are you trying to create point lights that moves around? You said lighting translation so i’m not sure if you mean translate the lights to that location or the cubes. Well the code i put up works by creating two lights (white and red) at the same location (1,1,1) and two cubes along the x-axis. I don’t think glTranslate*() and glRotate*()work on lights. I’m not sure though. Oh yeah and the code to declare the lights and enable lighting can go in a procedure that is only called once…so like InitializeGL() or something like that. I hope the above code is what you need.

Here is my code, and I have two lights but only one shows up. My Lights move with the Mouse buttons.

  • Code Starts Below:

#include <gl/glut.h>
#include <iostream.h>

//////////
float zCord = -6.0;

static int spinx, spiny, spinz;

GLfloat material_diffuse1[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat material_diffuse2[] = { 0.0f, 1.0f, 1.0f, 1.0f };
GLfloat diffuse_1[] = { 1, 1, 1, 1 };
GLfloat diffuse_2[] = { 0, 1, 1, 1 };
GLfloat ambient[] = { .1, .1, .1, 1 };
GLfloat specular[] = { 1, 1, 1, 1 };
GLfloat shininess[] = { 50.0 };
GLfloat position[] = { 1, 1, 1, 0 };
GLfloat position_2[] = { 1, 0, 0, 0 };
//////////

void init( void ) {
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glShadeModel( GL_SMOOTH );

glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse_1 );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glLightfv( GL_LIGHT0, GL_POSITION, position );

glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse_2 );
glLightfv( GL_LIGHT1, GL_SPECULAR, specular );
glLightfv( GL_LIGHT1, GL_POSITION, position_2 );

glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_LIGHT1 );

glEnable( GL_DEPTH_TEST );
}

void display( void ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glTranslatef( 0.0, -2.65, zCord );

glPushMatrix();
glTranslatef( 0.0, 2.0, 0.0 );
glutSolidTorus( 0.275, 0.85, 15, 42 );
glPopMatrix();

glPushMatrix();
glRotated( ( GLdouble ) spinx, 1.0, 0.0, 0.0 );
glRotated( ( GLdouble ) spiny, 0.0, 1.0, 0.0 );
glRotated( ( GLdouble ) spinz, 0.0, 0.0, 1.0 );

glMaterialfv ( GL_FRONT_AND_BACK, GL_AMBIENT, ambient );
glMaterialfv ( GL_FRONT_AND_BACK, GL_SPECULAR, specular );
glMaterialfv ( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse_1 );
glMaterialfv ( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse_1 );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glLightfv( GL_LIGHT0, GL_POSITION, position );

glTranslated( 0.0, 0.0, 5.0 );
glDisable( GL_LIGHTING );
glColor3f( 1.0, 1.0, 1.0 );
glutWireCube( 0.1 );
glEnable( GL_LIGHTING );
glPopMatrix();

glPushMatrix();
glRotated( ( GLdouble ) spinx, 1.0, 0.0, 0.0 );
glRotated( ( GLdouble ) spiny, 0.0, 1.0, 0.0 );
glRotated( ( GLdouble ) spinz, 0.0, 0.0, 1.0 );

glMaterialfv ( GL_FRONT_AND_BACK, GL_AMBIENT, ambient );
glMaterialfv ( GL_FRONT_AND_BACK, GL_SPECULAR, specular );
glMaterialfv ( GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse_2 );
glMaterialfv ( GL_FRONT_AND_BACK, GL_SHININESS, shininess );
glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse_2 );
glLightfv( GL_LIGHT1, GL_SPECULAR, specular );
glLightfv( GL_LIGHT1, GL_POSITION, position_2 );

glTranslated( 0.0, 0.0, -5.0 );
glDisable( GL_LIGHTING );
glColor3f( 0.0, 0.0, 1.0 );
glutWireCube( 0.1 );
glEnable( GL_LIGHTING );
glPopMatrix();

glFlush();

glutSwapBuffers();
glutPostRedisplay();
}

void reshape( int w, int h ) {
h = h < 1 ? 1 : h;

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

glViewport( 0, 0, w, h );

float ratio = 1.0 * w / h;
gluPerspective( 75.0, ratio, 1.0, 1000.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}

void keyboard( unsigned char key, int x, int y ) {
switch ( key ) {
case 27:
exit( 0 );
break;
}
glutPostRedisplay();
}

void mouse( int button, int state, int x, int y ) {
switch( button ) {
case GLUT_LEFT_BUTTON:
if ( state == GLUT_DOWN )
spinx = ( spinx + 10 ) % 360;
break;
case GLUT_MIDDLE_BUTTON:
if ( state == GLUT_DOWN )
spinz = ( spinz + 10 ) % 360;
break;
case GLUT_RIGHT_BUTTON:
if ( state == GLUT_DOWN )
spiny = ( spiny + 10 ) % 360;
break;
default:
break;
}
glutPostRedisplay();
}

int main( int argc, char** argv ) {
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutCreateWindow( “3D Object” );
glutFullScreen();

init();

glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutMouseFunc( mouse );

glutMainLoop();
return 0;
}

End of Code.

  • VC6-OGL

If you remove the call to glutPostRedislpay in the display function, you will see that there really is two lights enabled. It will be more obvious if you set the color of one light to, say, pure red, and the other to pure blue. The object itself is white (by default as required by the spec), and is lit by a red and a blue light.

The problem is next frame. After the torus is drawn, you draw two boxes. You also change the material for these boxes so their color matches the color of the light, but you never restore it before drawing the torus next time, so the torus will now have the material of the last cube. Say that the last cube has a red material, the torus will also have a red material. Shining a pure blue light on a pure red surface yields a pure black color; a pure red surface reflects only red light, but a pure blue light has no red light in it, so no light is reflected.

[This message has been edited by Bob (edited 01-07-2003).]

Hi!
You might me interested with this program: http://www30.brinkster.com/mishuk/light_master.htm

source code will be uploaded soon.

Mishuk

Nice Program!!!

  • VC6-OGL

Ok, your second light was on the same axis as the torus. So when you pressed the left mouse button you were rotating the light around the x-axis and you couldnt’ notice a change (the y and z coordinates were 0). So if you change the light position to be negative of the first position (the opposite side), then all is well. I posted up some changes i made to you code. The material doesn’t need to be set all the time. Just set it to 1, 1, 1, 1 and that way it reflects all the lights (if that is what you want). If you want your white light reflected as blue then set the material to {0,0,1,1} and so on. Anyways. Here is the code:

#include <gl/glut.h>
#include <iostream.h>

//////////
float zCord = -6.0;

static int spinx, spiny, spinz;

GLfloat material_diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat diffuse_1 = { 1, 0, 0, 1 };
GLfloat diffuse_2 = { 0, 0, 1, 1 };
GLfloat ambient = { .1, .1, .1, 1 };
GLfloat specular = { 1, 1, 1, 1 };
GLfloat shininess = { 50.0 };
GLfloat position = { 1, 1, 1, 0 };
GLfloat position_2 = { -1, -1, -1, 0 };
//////////

void init( void ) {
glClearColor( 0.0, 0.0, 0.0, 0.0 );
glShadeModel( GL_SMOOTH );

glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse_1 );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glLightfv( GL_LIGHT0, GL_POSITION, position );

glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse_2 );
glLightfv( GL_LIGHT1, GL_SPECULAR, specular );
glLightfv( GL_LIGHT1, GL_POSITION, position_2 );

glMaterialfv ( GL_FRONT_AND_BACK, GL_AMBIENT, ambient );
glMaterialfv ( GL_FRONT_AND_BACK, GL_SPECULAR, specular );
glMaterialfv ( GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
glMaterialfv ( GL_FRONT_AND_BACK, GL_SHININESS, shininess );

glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
glEnable( GL_LIGHT1 );

glEnable( GL_DEPTH_TEST );
}

void display( void ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
glTranslatef( 0.0, -2.65, zCord );

/* Enable and disable lights to see if they work…
Take the next two lines of code out. */
//glDisable(GL_LIGHT0);
//glDisable(GL_LIGHT1);

glPushMatrix();
glRotated( ( GLdouble ) spinx, 1.0, 0.0, 0.0 );
glRotated( ( GLdouble ) spiny, 0.0, 1.0, 0.0 );
glRotated( ( GLdouble ) spinz, 0.0, 0.0, 1.0 );

glLightfv( GL_LIGHT0, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuse_1 );
glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
glLightfv( GL_LIGHT0, GL_POSITION, position );

glTranslated( 0.0, 0.0, 5.0 );
glDisable( GL_LIGHTING );
glColor3f( 1.0, 1.0, 1.0 );
glutWireCube( 0.1 );
glEnable( GL_LIGHTING );
glPopMatrix();

glPushMatrix();
glRotated( ( GLdouble ) spinx, 1.0, 0.0, 0.0 );
glRotated( ( GLdouble ) spiny, 0.0, 1.0, 0.0 );
glRotated( ( GLdouble ) spinz, 0.0, 0.0, 1.0 );

glLightfv( GL_LIGHT1, GL_AMBIENT, ambient );
glLightfv( GL_LIGHT1, GL_DIFFUSE, diffuse_2 );
glLightfv( GL_LIGHT1, GL_SPECULAR, specular );
glLightfv( GL_LIGHT1, GL_POSITION, position_2 );

glTranslated( 0.0, 0.0, -5.0 );
glDisable( GL_LIGHTING );
glColor3f( 0.0, 0.0, 1.0 );
glutWireCube( 0.1 );
glEnable( GL_LIGHTING );
glPopMatrix();

glPushMatrix();
glTranslatef( 0.0, 2.0, 0.0 );
glutSolidTorus( 0.275, 0.85, 15, 42 );
glPopMatrix();

glFlush();

glutSwapBuffers();
glutPostRedisplay();
}

void reshape( int w, int h ) {
h = h < 1 ? 1 : h;

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

glViewport( 0, 0, w, h );

float ratio = 1.0 * w / h;
gluPerspective( 75.0, ratio, 1.0, 1000.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}

void keyboard( unsigned char key, int x, int y ) {
switch ( key ) {
case 27:
exit( 0 );
break;
}
glutPostRedisplay();
}

void mouse( int button, int state, int x, int y ) {
switch( button ) {
case GLUT_LEFT_BUTTON:
if ( state == GLUT_DOWN )
spinx = ( spinx + 10 ) % 360;
break;
case GLUT_MIDDLE_BUTTON:
if ( state == GLUT_DOWN )
spinz = ( spinz + 10 ) % 360;
break;
case GLUT_RIGHT_BUTTON:
if ( state == GLUT_DOWN )
spiny = ( spiny + 10 ) % 360;
break;
default:
break;
}
glutPostRedisplay();
}

int main( int argc, char** argv ) {
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutCreateWindow( “3D Object” );
glutFullScreen();

init();

glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutMouseFunc( mouse );

glutMainLoop();
return 0;
}

Oh yeah…i set the lights as red and blue so you can see them more clearly. I have two lines commented out that you can take out. I used those to just turn the light on and off.

  • Halcyon

Hi!
Sorry!! I forgot to tell you that, you can use 1,2 and 3 key to turn on and off the light.

Mishuk

Don’t Worry, I figured it out when I opened It.

  • VC6-OGL

Hey Halcyon,

My Program Is Working, Thanks!!!

  • VC6-OGL