/* independent.c * Ray S. Babcock, CS425, F'02 * * This program demonstrates the use of the GL lighting * system with an independently moving light. * * Draw a fixed pillar (cube scaled then translated) * with a cube (translated then rotated) * * Cube rotates around an axis shifted to the right. * * NO fly-around for this program. One object spins * and the light spins around the scene in the opposite direction. */ #include #include #include /* DELTA_DEG is the increment for rotation, useful for * different speed computers. Set lower to slow down rotation. */ #define DELTA_DEG 1.0 #define DELTA_FLY_RAD 0.01 static double rot_deg = 0.0; /* define light 0 position (4th param 0=>directional, * !0=>positional */ static GLfloat light0_position[]={1.0, 0.0, 1.0, 0.0}; GLuint theCube; GLuint thePillar; void draw_scene(); void display(void); void rotate_objects(void); void mainMenu(int); void mycube(void) { GLfloat vertices[] = { -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0}; GLubyte frontIndices[] = { 4, 5, 6, 7 }; GLubyte rightIndices[] = { 5, 1, 2, 6 }; GLubyte bottomIndices[] = { 0, 1, 5, 4 }; GLubyte backIndices[] = { 1, 0, 3, 2 }; GLubyte leftIndices[] = { 0, 4, 7, 3 }; GLubyte topIndices[] = { 7, 6, 2, 3 }; glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); /* front face */ glColor3f(1.0,0.0,0.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, frontIndices); /* right face */ glColor3f(0.0,1.0,0.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, rightIndices); /* bottom face */ glColor3f(0.0,0.0,1.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, bottomIndices); /* back face */ glColor3f(1.0,1.0,0.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, backIndices); /* left face */ glColor3f(0.0,1.0,1.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, leftIndices); /* top face */ glColor3f(1.0,0.0,1.0); glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, topIndices); } void myinit (void) { /* define global light vectors */ GLfloat global_light_ambient[]={0.3, 0.3, 0.3, 1.0}; /* define light0 vectors */ GLfloat light0_ambient[]={0.0, 0.0, 0.0, 1.0}; GLfloat light0_diffuse[]={0.7, 0.7, 0.0, 1.0}; GLfloat light0_specular[]={1.0, 1.0, 1.0, 1.0}; /* define material vectors */ /* all white */ GLfloat mat_ambient[]={1.0, 1.0, 1.0, 1.0}; GLfloat mat_diffuse[]={1.0, 1.0, 1.0, 1.0}; GLfloat mat_specular[]={1.0, 1.0, 1.0, 1.0}; GLfloat mat_shiny[]={10.0}; GLfloat mat2_ambient[]={1.0, 1.0, 1.0, 1.0}; GLfloat mat2_diffuse[]={1.0, 0.0, 0.0, 1.0}; GLfloat mat2_specular[]={1.0, 1.0, 1.0, 1.0}; GLfloat mat2_shiny[]={10.0}; glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective(40.0, 1.0, 1.0, 200.0); glMatrixMode (GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 6.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_light_ambient); glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); /* define LIGHT0 */ glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular); glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.0); /* Turn on lighting */ glEnable(GL_LIGHTING); /* Turn on LIGHT0 */ glEnable(GL_LIGHT0); /* Put a cube call in a display list */ theCube = glGenLists(1); glNewList(theCube, GL_COMPILE); /* define materials */ glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shiny); glutSolidTorus(1.0, 3.0, 30, 30); /* other things to spin glutSolidTeapot(2.0); glutSolidSphere(2.0, 30, 30); glutSolidCube(2.0); mycube(); */ glEndList(); thePillar = glGenLists(2); glNewList(thePillar, GL_COMPILE); /* define materials */ glMaterialfv(GL_FRONT, GL_AMBIENT, mat2_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat2_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat2_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat2_shiny); glutSolidTeapot(2.0); /* other objects to keep stationary glutSolidSphere(2.0, 30, 30); glutSolidSphere(2.0, 30, 30); glutSolidCube(2.0); */ glEndList(); /* Enable hidden line/surface removal */ glEnable(GL_DEPTH_TEST); glShadeModel (GL_SMOOTH); } void rotate_objects() { rot_deg += DELTA_DEG; if(rot_deg>=360.0) rot_deg=0.0; glutPostRedisplay(); } void draw_scene() { glPopMatrix(); /* rotate the view */ glLoadIdentity(); gluLookAt(0.0, 6.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); /* position light after look at call to keep it stationary */ glPushMatrix(); glRotated(-1.0*rot_deg, 0, 1, 0); glLightfv(GL_LIGHT0, GL_POSITION, light0_position); /* draw the pillar (cube scaled and translated) */ glPopMatrix(); /* retrieve the original viewing matrix */ glPushMatrix(); /* save for later use */ glTranslatef(-4.0, 0.0, 0.0); /* translate the modeling coord system to left in X */ //glCallList(thePillar); /* draw the cube (scaled and translated) */ /* draw the cube rotated CCW after translated */ glPopMatrix(); /* retrieve the original viewing matrix */ glPushMatrix(); /* save for later use */ glTranslatef(2.0, 0.0, 0.0); /* translate the modeling coord system to the right in X */ /* rotate the modeling coord system around Y by an increasing amount */ //glRotated(rot_deg, 0, 1, 0); glCallList(theCube); /* draw the cube (translated and rotated) */ } void display (void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); draw_scene(); glFlush(); glutSwapBuffers(); } void myReshape(int w, int h) { /* minimal reshap support */ glViewport (0, 0, w, h); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 27: /* escape key */ exit(0); break; } } void mainMenu(int value) { switch(value) { case 1: { } break; case 2: { } break; case 3: { } break; case 4: { } break; case 5: { } break; case 6: { exit(0); } break; case 7: { } break; case 8: { } break; case 9: { } break; }; } void makeMenu(void) { int menu_id; menu_id=glutCreateMenu(mainMenu); glutAddMenuEntry("wire frame",1); glutAddMenuEntry("solid flat",2); glutAddMenuEntry("solid smooth",3); glutAddMenuEntry("Show Normals",4); glutAddMenuEntry("Hide Normale",5); glutAddMenuEntry("exit program",6); glutAddMenuEntry("cube",7); glutAddMenuEntry("teapot",8); glutAddMenuEntry("torus",9); glutAttachMenu(GLUT_RIGHT_BUTTON); } /* Main Loop * Open window with initial window size, title bar, * RGB display mode, and handle input events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (500, 500); glutCreateWindow (argv[0]); myinit (); glutIdleFunc(rotate_objects); glutReshapeFunc (myReshape); glutDisplayFunc(display); glutKeyboardFunc (keyboard); makeMenu(); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }