/* * 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. */ /* normals.c - Show the effect of lighting on a flat surface * when using face normals and vertex normals. The red * lines represent the directions of the normal vectors. * * Left Mousebutton - move eye position * Middle Mousebutton - change twist angle * Right Mousebutton - move up / down to zoom in / out * Escape Key - exit the program */ #include /* includes gl.h, glu.h */ #include "3d.h" #include #include /* Function Prototypes */ GLvoid initgfx( GLvoid ); GLvoid drawScene( GLvoid ); GLvoid reshape( GLsizei, GLsizei ); GLvoid keyboard( GLubyte, GLint, GLint ); GLvoid mouse( GLint, GLint, GLint, GLint ); GLvoid motion( GLint, GLint ); GLvoid drawNormal( GLfloat, GLfloat, GLfloat, GLfloat, GLfloat, GLfloat ); 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 */ enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE }; static GLint action; static GLdouble xStart = 0.0, yStart = 0.0; static GLfloat fovy, nearClip, farClip, distance, twistAngle, incAngle, azimAngle; void main( int argc, char *argv[] ) { GLsizei width, height; glutInit( &argc, argv ); /* create a window that is 1/4 the size of the screen */ 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(); glutMouseFunc( mouse ); glutMotionFunc( motion ); glutKeyboardFunc( keyboard ); glutReshapeFunc( reshape ); glutDisplayFunc( drawScene ); printHelp( argv[0] ); glutMainLoop(); } void printHelp( char *progname ) { fprintf(stdout, "\n%s - demonstrate face and vertex normals\n\n" "Left Mousebutton - move eye position\n" "Middle Mousebutton - change twist angle\n" "Right Mousebutton - move up / down to zoom in / out\n" "Escape Key - exit the program\n\n", progname); } GLvoid initgfx( GLvoid ) { 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(); /* Turn on a default light */ glEnable( GL_LIGHT0 ); } GLvoid keyboard( GLubyte key, GLint x, GLint y ) { switch (key) { case KEY_ESC: /* Exit whenever the Escape key is pressed */ exit(0); } } GLvoid mouse( GLint button, GLint state, GLint x, GLint y ) { static GLint buttons_down = 0; if (state == GLUT_DOWN) { switch (button) { case GLUT_LEFT_BUTTON: action = MOVE_EYE; break; case GLUT_MIDDLE_BUTTON: action = TWIST_EYE; break; case GLUT_RIGHT_BUTTON: action = ZOOM; break; } /* Update the saved mouse position */ xStart = x; yStart = y; } else { if (--buttons_down == 0) action = MOVE_NONE; } } GLvoid motion( GLint x, GLint y ) { switch (action) { case MOVE_EYE: /* Adjust the eye position based on the mouse position */ azimAngle += (GLdouble) (x - xStart); incAngle -= (GLdouble) (y - yStart); break; case TWIST_EYE: /* Adjust the eye twist based on the mouse position */ twistAngle = fmod(twistAngle+(x - xStart), 360.0); break; case ZOOM: /* Adjust the eye distance based on the mouse position */ distance -= (GLdouble) (y - yStart)/10.0; break; default: printf("unknown action %d\n", action); } /* Update the stored mouse position for later use */ xStart = x; yStart = y; glutPostRedisplay(); } void resetView( GLvoid ) { distance = nearClip + (farClip - nearClip) / 2.0; twistAngle = 0.0; /* rotation of viewing volume (camera) */ incAngle = 60.0; azimAngle = 0.0; fovy = 60.0f; /* Field of view in Y angle */ } GLvoid reshape( GLsizei width, GLsizei height ) { GLdouble aspect; glViewport( 0, 0, width, height ); aspect = (GLdouble) width / (GLdouble) height; glMatrixMode( GL_PROJECTION ); glLoadIdentity(); 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 drawNormal( GLfloat px, GLfloat py, GLfloat pz, GLfloat nx, GLfloat ny, GLfloat nz ) { GLdouble normal[3], cross[3], zaxis[3]; GLdouble angle; #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #define DegreesToRadians (M_PI / (GLfloat) 180.0) glColor3f( 1.0f, 0.0f, 0.0f ); glPushMatrix(); glTranslatef( px, py, pz ); glBegin( GL_LINES ); glVertex3f( 0.0f, 0.0f, 0.0f ); glVertex3f( nx, ny, nz ); glEnd(); /* normalize the normal vector */ normal[0] = nx; normal[1] = ny; normal[2] = nz; normalize( normal ); /* determine angle between z axis and normal */ zaxis[0] = 0; zaxis[1] = 0; zaxis[2] = 1; angle = acos( dot3( zaxis, normal ) )/DegreesToRadians; if ( angle != 0.0 ) { /* find the axis of rotation */ crossprod( zaxis, normal, cross ); glRotatef( angle, cross[0], cross[1], cross[2] ); } /* move to end of normal vector */ glTranslatef( 0.0f, 0.0f, 1.0f ); glutSolidCone( 0.05, 0.1, 8, 8 ); glPopMatrix(); } GLvoid drawScene( GLvoid ) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glPushMatrix(); polarView( distance, azimAngle, incAngle, twistAngle ); /* Draw a lit flat-shaded triangle on the left */ glPushMatrix(); glTranslatef( -2.0f, 0.0f, 0.0f ); /* Turn on lighting computations */ glEnable( GL_LIGHTING ); glBegin( GL_TRIANGLES ); glNormal3f( 0.0f, 0.0f, 1.0f ); glVertex2f( 0.0f, 1.0f ); glVertex2f( -0.866f, -0.50f ); glVertex2f( 0.866f, -0.50f ); glEnd(); /* Turn off lighting computations */ glDisable( GL_LIGHTING ); /* Draw an unlit line representing the face normal */ drawNormal( 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ); glPopMatrix(); /* Draw a lit, smooth-shaded triangle on the right */ glPushMatrix(); glTranslatef( 2.0f, 0.0f, 0.0f ); /* Turn on lighting computations */ glEnable( GL_LIGHTING ); glBegin( GL_TRIANGLES ); glNormal3f( 0.0f, 0.0f, 1.0f ); glVertex2f( 0.0f, 1.0f ); glNormal3f( 0.455f, 0.796f, 0.398f ); glVertex2f( -0.866f, -0.50f ); glNormal3f( 0.162f, 0.566f, 0.808f ); glVertex2f( 0.866f, -0.50f ); glEnd(); /* Turn off lighting computations */ glDisable( GL_LIGHTING ); /* Draw unlit lines representing the vertex normals */ drawNormal( 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f ); drawNormal( -0.866f, -0.50f, 0.0f, 0.455f, 0.796f, 0.398f ); drawNormal( 0.866f, -0.50f, 0.0f, 0.162f, 0.566f, 0.808f ); glPopMatrix(); glPopMatrix(); glutSwapBuffers(); }