/* * Copyright (c) 1996-1999 Silicon Graphics, Inc. All rights reserved. * * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. * * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* colorMaterial.c - demonstrates using glColorMaterial to quickly * change material properties. * * Escape key - exit the program */ #include /* includes gl.h, glu.h */ #include #include /* Function Prototypes */ GLvoid initgfx( GLvoid ); GLvoid drawScene( GLvoid ); GLvoid reshape( GLsizei, GLsizei ); GLvoid keyboard( GLubyte, GLint, GLint ); void resetView( GLvoid ); void polarView( GLfloat, GLfloat, GLfloat, GLfloat ); void printHelp( char * ); /* Global Definitions */ #define KEY_ESC 27 /* ascii value for the escape key */ /* Global Variables */ static GLdouble fovy, nearClip, farClip; static GLfloat distance, twistAngle, incAngle, azimAngle; GLvoid main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); width = glutGet( GLUT_SCREEN_WIDTH ); height = glutGet( GLUT_SCREEN_HEIGHT ); glutInitWindowPosition( width / 4, height / 4 ); glutInitWindowSize( (width / 2) - 4, height / 2 ); glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE ); glutCreateWindow( argv[0] ); initgfx(); glutKeyboardFunc( keyboard ); glutReshapeFunc( reshape ); glutDisplayFunc( drawScene ); printHelp( argv[0] ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - demonstrate fast material changes\n\n" "Escape key - exit the program\n\n", progname); } GLvoid initgfx( GLvoid ) { GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat mat_shininess[] = { 10.0f }; glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); /* changing AMBIENT and DIFFUSE properties to the same value * for the front face only */ glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glClearColor( 0.0, 0.0, 0.0, 1.0 ); glEnable( GL_DEPTH_TEST ); fovy = 60.0f; /* field of view in Y */ nearClip = 3.0f; /* Near clipping plane location */ farClip = 12.0f; /* Far clipping plane location */ resetView(); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case KEY_ESC: exit(0); } } void resetView( GLvoid ) { distance = nearClip + (farClip - nearClip) / 2.0f; twistAngle = 0.0f; /* rotation of viewing volume (camera) */ incAngle = 0.0f; azimAngle = 0.0f; } GLvoid reshape( GLsizei width, GLsizei height ) { GLdouble aspect; glViewport( 0, 0, width, height ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); aspect = (GLdouble) width / (GLdouble) height; gluPerspective( fovy, aspect, nearClip, farClip ); glMatrixMode( GL_MODELVIEW ); } void polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence, GLfloat twist) { glTranslatef( 0.0f, 0.0f, -distance); glRotatef( -twist, 0.0f, 0.0f, 1.0f); glRotatef( -incidence, 1.0f, 0.0f, 0.0f); glRotatef( -azimuth, 0.0f, 0.0f, 1.0f); } GLvoid drawScene( GLvoid ) { int i, slices = 8; glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); polarView( distance, azimAngle, incAngle, 0.0f ); for ( i = 0; i < slices; i++ ) { /* change ambient and diffuse material properties * using GL_COLOR_MATERIAL mode */ glColor3f( i/10.0f, i/10.0f, 1.0f - i/10.0f ); glPushMatrix(); glRotatef( i * 360.0f/slices, 0.0f, 0.0f, 1.0f ); glTranslatef( 1.5f, 0.0f, 0.0f ); glRotatef( i * 360.0f/slices, 0.0f, 1.0f, 0.0f ); glutSolidTorus( 0.25, 0.75, 8, 15 ); glPopMatrix(); } glPopMatrix(); glutSwapBuffers(); }