Light Problems

Hello,

I have two lights but only one shows up. Can someone help me figure out what the problem is. 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

Hi!
I pasted your code to my Visual Studio .NET project. It was working fine. Two light is showing. But the white box (light) was out of a clipping plane. When I rotated using the left mouse button for several time it came under the clipping plane. SO i think no problem with the code but some coordinate problem. you can increase the fov and it will come under clipping plane faster (May be, i didn’t tested it).

Mishuk

Do not worry, it’s all figured out now.