/* tex_teapot.cpp, based on teatex.c by E. Angel */ /* An illuminated teapot, with texture mapping that can be turned on/off. * The texture mapping can be toggled between replace and modulate modes. * */ /* E. Angel, Interactive Computer Graphics */ /* A Top-Down Approach with OpenGL, Third Edition */ /* Addison-Wesley Longman, 2003 */ #include #include #define ImageWidth 64 #define ImageHeight 64 GLubyte image[ImageHeight][ImageWidth][3]; static GLfloat theta[] = {0.0,0.0,0.0}; static GLint axis = 2; int texture = 0; // specifies whether texturing is on or off (default) int mode = 0; // specifies texture environment mode (default is replace) void init_light(void) { GLfloat mat_diffuse[] = { 1.0, 0.0, 0.0, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 60.0 }; GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); } void init_texture(void) { int i, j, c; // create a checker texture pattern for(i=0; i 360.0 ) theta[axis] -= 360.0; glutPostRedisplay(); } /* mouse callback, selects an axis about which to rotate */ void mouse(int btn, int state, int x, int y) { if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0; // X axis if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1; // Y axis if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2; // Z axis } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= h) glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w, 2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0); else glOrtho(-2.0 * (GLfloat) w / (GLfloat) h, 2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); } void keyboard(unsigned char key, int x, int y) { if (key == 'q' || key == 'Q') exit(0); else if(key == '1') glutIdleFunc(spinTeapot); else if(key == '2') glutIdleFunc(NULL); else if(key == 'p') { texture = 0; // plain old shading, turn texturing off glutPostRedisplay(); } else if(key == 't') { texture = 1; // turn texturing on glutPostRedisplay(); } else if(key == 'r') { texture = 1; // turn texturing on mode = 0; // select Replace mode glutPostRedisplay(); } else if(key == 'm') { texture = 1; // turn texturing on mode = 1; // select Modulate mode glutPostRedisplay(); } } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(500, 500); glutCreateWindow("tex_teapot"); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutIdleFunc(NULL); glutMouseFunc(mouse); glutKeyboardFunc(keyboard); init_light(); init_texture(); glutMainLoop(); return 0; }