/* In the following code we are going continue our study of the graphics pipeline by defining a simple bitmap and rendering it on a glut object. 1. Run the code as is. Why does it render a teapot without any sense of 3D shape? 2. Uncomment the call to lightingInit in main , and re-run. 3. Comment the call to lightingInit in main , then uncomment the call to textureInit in main . Why does it look hollow, and wrong? Fix it. 4. Try various combinations of the calls in textureInit . 5. Turn on both lighting/material properties and texturing. 6. Add a glut pre-defined object to the scene, which is not texture mapped. */ #include #include static GLfloat theta[ ] = { 0.0, 0.0, 0.0 }; static GLint axis = 2; void display( void ) { // Clear frame buffer and z buffer glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // Rotate and draw teapot glLoadIdentity( ); glRotatef( theta[ 0 ], 1.0, 0.0, 0.0 ); glRotatef( theta[ 1 ], 0.0, 1.0, 0.0 ); glRotatef( theta[ 2 ], 0.0, 0.0, 1.0 ); glutSolidTeapot( 1.0 ); // Swap Buffers to display new teapot glutSwapBuffers( ); } void spinTeapot( ) { // Spin teapot two degrees about selected axis // and redisplay theta[ axis ] += 2.0; if ( theta[ axis ] > 360.0 ) theta[ axis ] -= 360.0; glutPostRedisplay( ); } void mouse( int button, int state, int x, int y ) { // Selects the axis about which to rotate if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN ) axis = 0; if ( button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN ) axis = 1; if ( button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN ) axis = 2; } void myReshape(int width, int height) { glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity( ); if ( width <= height ) glOrtho( -2.0, 2.0, -2.0 * ( GLfloat ) height / ( GLfloat ) width, 2.0 * ( GLfloat ) height / ( GLfloat ) width, -10.0, 10.0 ); else glOrtho( -2.0 * ( GLfloat ) width / ( GLfloat ) height, 2.0 * ( GLfloat ) width / ( GLfloat ) height, -2.0, 2.0, -10.0, 10.0 ); glMatrixMode( GL_MODELVIEW ); } void keyboard( unsigned char key, int x, int y ) { // Choose whether to rotate teapot or not if ( key == '1' ) glutIdleFunc( spinTeapot ); if ( key == '2' ) glutIdleFunc( NULL ); } void lightingInit( ) { // set the lighting glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); glShadeModel(GL_SMOOTH); GLfloat light_diffuse[] = { 1.0 , 1.0 , 1.0 , 1.0 }; GLfloat light_specular[] = { 1.0 , 1.0 , 1.0 , 1.0 }; GLfloat light_ambient[] = { 0.2 , 0.2 , 0.2 , 1.0 }; GLfloat light_position[] = { -1,1,1 , 0 }; glLightfv(GL_LIGHT0 , GL_POSITION , light_position); glLightfv(GL_LIGHT0 , GL_DIFFUSE , light_diffuse); glLightfv(GL_LIGHT0 , GL_AMBIENT , light_ambient); glLightfv(GL_LIGHT0 , GL_SPECULAR , light_specular); //set the material properties GLfloat mat_ambient[3]; GLfloat mat_diffuse[3]; GLfloat mat_specular[3]; GLfloat mat_shininess[1]; mat_ambient[0]=0.2125 ; mat_ambient[1]=0.1275 ; mat_ambient[2]=0.054; mat_diffuse[0]=0.714 ; mat_diffuse[1]=0.4284 ; mat_diffuse[2]=0.18144; mat_specular[0]=0.393548; mat_specular[1]=0.271906; mat_specular[2]=0.166721; mat_shininess[0]=25.6; 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_shininess); } void textureInit( ) { //---------------------------------------------- // The texture settings are below GLfloat planes[ ] = { -1.0, 0.0, 1.0, 0.0 }; GLfloat planet[ ] = { 0.0, -1.0, 0.0, 1.0 }; GLubyte image[ 64 ][ 64 ][ 3 ]; int i, j, c; // Generate checkerboard texture for ( i = 0; i < 64; i++ ) { for ( j = 0 ; j < 64; j++ ) { c = ( ( ( ( i & 0x8 ) == 0 ) ^ ( ( j & 0x8 ) ) == 0 ) ) * 255; image[ i ][ j ][ 0 ] = ( GLubyte ) c; image[ i ][ j ][ 1 ] = ( GLubyte ) c; image[ i ][ j ][ 2 ] = ( GLubyte ) c; } } // Enable Texture Mapping glEnable( GL_TEXTURE_2D ); // Enable the z-buffer //glEnable(GL_DEPTH_TEST); // For some strange effects, try adding back some of the following // commented out texture commands! Try for instance to put // all of these in but comment out the last glEnable; then try // commenting out the first glTexGeni as well */ // Generate texture using distance from "sea level" as specified by // texture generating function specified by "planes" - plane for s // coordinate // glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); // glTexGenfv(GL_S, GL_OBJECT_PLANE, planes); // enables s texture coordinate to be computed, using function // specified above // glEnable(GL_TEXTURE_GEN_S); // Generate texture using distance from "sea level" as specified by // texture generating function specified by "planet" - plane for t // coordinate // glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); // glTexGenfv(GL_T, GL_OBJECT_PLANE, planet); // enables t texture coordinate to be computed using function // specified above // glEnable(GL_TEXTURE_GEN_T); // Specifies 2D texture image glTexImage2D( GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, image ); // Sets the wrap parameter for the horizontal coordinate, s, to repeat glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); // Sets the wrap parameter for the vertical coordinate, t, to repeat glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); // Sets the magification parameter to that texture element nearest to // the center of the pixel glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); // Sets the minifying parameter to that texture element nearest to // the center of the pixel glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); // Set background color glClearColor( 1.0, 1.0, 1.0, 1.0 ); } int main( int argc, char **argv ) { glutInit( &argc, argv ); // need both double buffering and z buffer glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); glutInitWindowSize(500, 500 ); glutCreateWindow( "Textured Teapot" ); // Register Callback Functions glutReshapeFunc( myReshape ); glutDisplayFunc( display ); glutIdleFunc( spinTeapot ); glutMouseFunc( mouse ); glutKeyboardFunc( keyboard ); //lightingInit( ); //textureInit( ); glutMainLoop( ); return 0; }