#include #include #include #define RED 0 #define GREEN 1 #define BLUE 2 #define AMBIENT 0 #define DIFFUSE 1 #define SPECULAR 2 #define SHINE 3 int width,height; int cmode=RED, rmode=AMBIENT; GLfloat light_position[] = {0.0,10.0,10.0,0.0}; GLfloat light_ambient[] = {1.0,1.0,1.0,1.0}; GLfloat light_diffuse[] = {1.0,1.0,1.0,1.0}; GLfloat light_specular[] = {1.0,1.0,1.0,1.0}; GLfloat sphere[]={.20,.10,0.0,1.0,.20,.20,.05,1.0,.80,.80,.80,1.0}; GLfloat shine_factor=50.0; void init(void) { int i; /* background color */ glClearColor(0.0,0.0,0.0,0.0); /* shade model */ glShadeModel(GL_SMOOTH); /* enable z-buffering */ glEnable(GL_DEPTH_TEST); /* lighting */ glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); /* reset material properties */ glMaterialfv(GL_FRONT, GL_AMBIENT, &sphere[0]); glMaterialfv(GL_FRONT, GL_DIFFUSE, &sphere[4]); glMaterialfv(GL_FRONT, GL_SPECULAR, &sphere[8]); glMaterialf(GL_FRONT, GL_SHININESS, shine_factor); /* draw sphere */ glTranslatef(0.0,0.0,-500.0); glutSolidTeapot(125.00); glFlush(); glutSwapBuffers(); return; } void reshape(int w, int h) { width=w; height=h; glViewport(0,0,(GLsizei) w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho((float) (-w)/2.0, (float) w/2.0,(float) (-h)/2.0, (float) h/2.0, 10.0, 1000.0); glMatrixMode(GL_MODELVIEW); } void keyboard(unsigned char key, int x, int y) { // set color mode to R,G, or B and reflection mode to // ambient, diffuse, specular, or shine switch(key) { case 'r': case 'R': cmode = RED; break; case 'b': case 'B': cmode = BLUE; break; case 'g': case 'G': cmode = GREEN; break; case '1': rmode = AMBIENT; break; case '2': rmode = DIFFUSE; break; case '3': rmode = SPECULAR; break; case 's': case 'S': rmode = SHINE; break; case 'p': case 'P': printf("reflection/color red green blue\n"); printf("ambient %1.2f %1.2f %1.2f\n",sphere[0],sphere[1],sphere[2]); printf("diffuse %1.2f %1.2f %1.2f\n",sphere[4],sphere[5],sphere[6]); printf("specular %1.2f %1.2f %1.2f\n",sphere[8],sphere[9],sphere[10]); printf("shininess %3.0f\n",shine_factor); break; default: break; } } void mouse(int button, int state, int x, int y) { if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) { if (rmode !=SHINE && sphere[rmode*4 + cmode]>= 0.05) sphere[rmode*4 + cmode] -= .05; if (rmode==SHINE && shine_factor >= 3.0) shine_factor -=3; } if (button==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { if (rmode != SHINE && sphere[rmode*4 + cmode]<=.95) sphere[rmode*4 + cmode] += .05; if (rmode == SHINE) shine_factor += 3; } glutPostRedisplay(); }; main(int argc, char* argv[]) { width=height=500; glutInit(&argc,argv); glutInitWindowPosition(100,100); glutInitWindowSize(width,height); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); glutCreateWindow("MATERIAL EFFECTS"); init(); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutDisplayFunc(display); glutKeyboardFunc(keyboard); glutMainLoop(); return(1); }