home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* lightprops.c - open a window and clear the background.
- * Set up callbacks to handle keyboard input.
- * Model some objects. Use perspective projection.
- * Use independent modeling transformations to position objects.
- * Added reshape callback to reset the viewport and viewing volume.
- * Add depth buffering.
- * Add a viewing transformation and control it using mouse input.
- * Add continuous animation and a menu to control it.
- * Add a local light with distance attenuation, and a spot light.
- *
- * F1 key - print help information
- * Right Mouse Button - popup menu
- * Left Arrow Key - move the reference point to the left
- * Right Arrow Key - move the reference point to the right
- * Up Arrow Key - move the reference point up
- * Down Arrow Key - move the reference point down
- * <f> key - toggle front/back face culling
- * <n> key - toggle between night/day
- * <s> key - toggle smooth/flat shading
- * SPACE key - generates a random background color
- * Escape Key - exit program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <stdio.h>
- #include <math.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid keyboard( GLubyte, GLint, GLint );
- GLvoid specialkeys( GLint, GLint, GLint );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid drawScene( GLvoid );
- GLvoid menuFunc( GLint );
-
- void initGround( GLvoid );
-
- void checkError( char * );
- void printHelp( char * );
-
- /* Global Variables */
-
- static GLdouble xRef = 0.0, yRef = 0.0;
-
- static GLfloat sunPosition = 2.5;
- static GLboolean night = GL_FALSE;
-
- static GLfloat green[] = { 0.0, 1.0, 0.0 };
- static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
- static GLfloat red[] = { 1.0, 0.0, 0.0 };
- static GLfloat magenta[] = { 1.0, 0.0, 1.0 };
- static GLfloat yellow[] = { 1.0, 1.0, 0.0 };
- static GLfloat blue[] = { 0.0, 0.0, 1.0 };
-
- static GLfloat white[] = { 1.0, 1.0, 1.0, 1.0 };
- static GLfloat black[] = { 0.0, 0.0, 0.0, 0.0 };
-
- static char *progname;
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* menu options */
- #define MENU_ANIMATE 1 /* animation control option */
- #define MENU_EXIT 2 /* exit option */
-
- #define GRIDPOINTS 41 /* number of vertices in ground mesh */
-
- static GLfloat groundMesh[GRIDPOINTS][GRIDPOINTS][3] = { 0 };
-
- static GLboolean animateFlag = GL_TRUE;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- /* create a window that is 1/4 the size of the screen,
- * and position it in the middle of the screen.
- */
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitWindowSize( width / 2, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutReshapeFunc( reshape );
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutDisplayFunc( drawScene );
-
- /* create menu and add menu options */
- glutCreateMenu( menuFunc );
- glutAddMenuEntry( "Stop Animation", MENU_ANIMATE );
- glutAddMenuEntry( "Exit", MENU_EXIT );
- glutAttachMenu( GLUT_RIGHT_BUTTON );
-
- progname = argv[0];
-
- printHelp( progname );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout,
- "\n%s - create a scene with a local light and attenuation\n\n"
- "F1 key - print help information\n"
- "Left Arrow Key - move reference point to the left\n"
- "Right Arrow Key - move reference point to the right\n"
- "Up Arrow Key - move reference point up\n"
- "Down Arrow Key - move reference point down\n"
- "SPACE Key - generates a random background color\n"
- "<f> key - toggle front/back face culling\n"
- "<n> key - toggle between night/day\n"
- "<s> key - toggle smooth/flat shading\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat sunAmbient[4] = { 0.5, 0.5, 0.5, 1.0 };
-
- /* set clear color to blue */
- glClearColor( 0.0, 0.0, 1.0, 1.0 );
-
- /* enable the depth buffer */
- glEnable( GL_DEPTH_TEST );
-
- /* enable the face culling */
- glEnable( GL_CULL_FACE );
-
- initGround();
-
- glMaterialfv( GL_FRONT, GL_SPECULAR, white );
- glMaterialf( GL_FRONT, GL_SHININESS, 10.0 );
-
- /* Add a light for the sun */
- glLightfv( GL_LIGHT0, GL_DIFFUSE, white );
- glLightfv( GL_LIGHT0, GL_AMBIENT, sunAmbient );
- glEnable( GL_LIGHT0 );
- glEnable( GL_LIGHTING );
-
- /* have OpenGL automatically normalize the normals,
- * since we have lighting turned on and are scaling
- */
- glEnable( GL_NORMALIZE );
-
- /* Enable fast material changes for diffuse material */
- glColorMaterial( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
- glEnable( GL_COLOR_MATERIAL );
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- /* compute aspect ratio */
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
-
- /* Reset world coordinates first ... */
- glLoadIdentity();
-
- /* Reset the viewing volume based on the new aspect ratio */
- gluPerspective( 45.0, aspect, 3.0, 7.0 );
-
- glMatrixMode( GL_MODELVIEW );
- }
-
- void
- checkError( char *label )
- {
- GLenum error;
- while ( (error = glGetError()) != GL_NO_ERROR )
- printf( "%s: %s\n", label, gluErrorString(error) );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- static GLboolean flat = GL_FALSE;
- static GLboolean cullFront = GL_FALSE;
-
- switch (key) {
- case ' ': /* SPACE key */
- /* generate a random background color */
- glClearColor( drand48(), drand48(), drand48(), 1.0 );
- glutPostRedisplay();
- break;
- case 'f': /* f key */
- /* toggle between back and front face culling */
- cullFront = !cullFront;
- if (cullFront) {
- glCullFace( GL_FRONT );
- printf("Culling FRONT faces\n");
- } else {
- glCullFace( GL_BACK );
- printf("Culling BACK faces\n");
- }
- glutPostRedisplay();
- break;
- case 'n': /* n key */
- /* toggle between night and day */
- night = !night;
- if (night) {
- /* make the light fall of with distance much
- * more quickly when it is night
- */
- glLightf( GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.1 );
- } else {
- glLightf( GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.0 );
- }
- glutPostRedisplay();
- break;
- case 's': /* s key */
- /* toggle between smooth and flat shading */
- flat = !flat;
- if (flat)
- glShadeModel( GL_FLAT );
- else
- glShadeModel( GL_SMOOTH );
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit when the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- specialkeys( GLint key, GLint x, GLint y )
- {
- switch (key) {
- case GLUT_KEY_F1: /* Function key #1 */
- /* print help information */
- printHelp( progname );
- break;
-
- case GLUT_KEY_LEFT: /* move reference point to the left */
- xRef -= 0.5;
- if (xRef < -4.0) xRef = -4.0;
- glutPostRedisplay();
- break;
-
- case GLUT_KEY_RIGHT: /* move reference point to the right */
- xRef += 0.5;
- if (xRef > 4.0) xRef = 4.0;
- glutPostRedisplay();
- break;
-
- case GLUT_KEY_UP: /* move reference point up */
- yRef += 0.5;
- if (yRef > 3.0) yRef = 3.0;
- glutPostRedisplay();
- break;
-
- case GLUT_KEY_DOWN: /* move reference point down */
- yRef -= 0.5;
- if (yRef < -3.0) yRef = -3.0;
- glutPostRedisplay();
- break;
- }
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* update the position of the sun */
- sunPosition -= 0.01;
- if (sunPosition < -3.0) sunPosition = 2.5;
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE && animateFlag) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- menuFunc( int value )
- {
- switch (value) {
- case MENU_ANIMATE:
- animateFlag = !animateFlag;
- if (animateFlag) {
- glutIdleFunc( animate );
- glutChangeToMenuEntry( MENU_ANIMATE, "Stop Animation",
- MENU_ANIMATE );
- } else {
- glutIdleFunc( NULL );
- glutChangeToMenuEntry( MENU_ANIMATE, "Start Animation",
- MENU_ANIMATE );
- }
- break;
- case MENU_EXIT:
- exit(0);
- }
- }
-
- void
- initGround( void )
- {
- int i, j;
- GLfloat x, y, spacing;
-
- /* Create a flat grid composed of points with the z value = 0 */
-
- /* spacing = distance between adjacent grid points in x and y */
- spacing = 2.0 / (float)(GRIDPOINTS - 1);
-
- /* x and z range from -1.0 to 1.0 by spacing interval */
- for ( x = -1.0, i = 0; i < GRIDPOINTS; x += spacing, i++ ) {
- for ( y = -1.0, j = 0; j < GRIDPOINTS; y += spacing, j++ ) {
- groundMesh[i][j][0] = x;
- groundMesh[i][j][1] = y;
- groundMesh[i][j][2] = 0.0;
- }
- }
- }
-
- GLvoid
- drawGround( GLvoid )
- {
- register int i, j;
-
- glNormal3f( 0.0, 0.0, 1.0 );
-
- /* Draw quad strips connecting grid points.
- * Grid is drawn column by column,
- * where each column is one quad strip.
- * Every pair of vertices after first two vertices
- * adds a new quad to the strip.
- */
- for ( i = 0; i < (GRIDPOINTS - 1); i++ ) {
- glBegin( GL_QUAD_STRIP );
- for ( j = 0; j < (GRIDPOINTS); j++ ) {
- glColor3f( 0.0, 1.0 - (GLfloat) j/GRIDPOINTS, 0.0 );
- glVertex3fv( &groundMesh[i][j][0] );
- glVertex3fv( &groundMesh[i + 1][j][0] );
- }
- glEnd();
- }
- }
-
- GLvoid
- drawWindow()
- {
- /* window */
- static GLfloat v0[] = { 0.0, 0.4 };
- static GLfloat v1[] = { 0.0, 0.0 };
- static GLfloat v2[] = { 0.1, 0.4 };
- static GLfloat v3[] = { 0.1, 0.0 };
- static GLfloat v4[] = { 0.2, 0.4 };
- static GLfloat v5[] = { 0.2, 0.0 };
-
- /* draw 2 quadrilateral strip to make a window */
- glNormal3f( 0.0, 0.0, 1.0 );
- glBegin( GL_QUAD_STRIP );
- glColor3f( 1.0, 0.0, 0.0 );
- glVertex2fv (v0);
- glColor3f( 0.9, 0.0, 1.0 );
- glVertex2fv (v1);
- glColor3f( 0.8, 0.1, 0.0 );
- glVertex2fv (v2);
- glColor3f( 0.7, 0.2, 1.0 );
- glVertex2fv (v3);
- glColor3f( 0.6, 0.3, 0.0 );
- glVertex2fv (v4);
- glColor3f( 0.5, 0.4, 1.0 );
- glVertex2fv (v5);
- glColor3f( 0.4, 0.5, 0.0 );
- glEnd();
- }
-
- /* draw a "circle" using a triangle fan; set the
- * center of the circle to white, and the outside
- * to the color passed in
- */
- GLvoid
- drawFan( GLfloat *color )
- {
- /* Draw a triangle fan centered at the current coordinate
- * system origin
- */
- glNormal3f( 0.0, 0.0, 1.0 );
- glBegin( GL_TRIANGLE_FAN );
- glColor3f( 1.0, 1.0, 1.0 );
- glVertex2f( 0.0, 0.0 );
- glColor3fv( color );
- glVertex2f( 0.0, -0.2 );
- glVertex2f( 0.2, -0.1 );
- glVertex2f( 0.2, 0.1 );
- glVertex2f( 0.0, 0.2 );
- glVertex2f( -0.2, 0.1 );
- glVertex2f( -0.2, -0.1 );
- glVertex2f( 0.0, -0.2 );
- glEnd();
- }
-
- /* draw a flower with the base of the stem
- * at the current location
- */
- GLvoid
- drawFlower( GLfloat *color )
- {
- /* draw the stem with 2 leaves */
- glColor3fv( darkgreen );
- glNormal3f( 0.0, 0.0, 1.0 );
- glBegin( GL_LINES );
- glVertex2f( 0.0, 0.0 ); /* stem */
- glVertex2f( 0.0, 0.25 );
- glVertex2f( 0.0, 0.1 ); /* leaf */
- glVertex2f( 0.05, 0.15 );
- glVertex2f( 0.0, 0.05 ); /* leaf */
- glVertex2f( -0.05, 0.2 );
- glEnd();
-
- glPushMatrix();
- /* move to the top of the stem */
- glTranslatef( 0.0, 0.25, 0.0 );
-
- /* use a scaled triangle fan for the flower head */
- glScalef( 0.1, 0.1, 1.0 );
- drawFan( color );
- glPopMatrix();
- }
-
- /* draw a house centered at the current origin;
- * the bounding box for the house is 1.0 x 2.0 units
- */
- GLvoid
- drawHouse()
- {
- /* draw a 3D house */
- glColor3f( 1.0, 1.0, 1.0 ); /* white */
- SolidBox( 1.0, 1.5, 1.0 );
-
- glPushMatrix();
- /* move to the peak of the roof */
- glTranslatef( 0.0, 1.25, 0.0 );
-
- /* draw a triangle fan for the roof */
- glColor3f( 0.0, 0.0, 0.0 ); /* black */
- glNormal3f( 0.0, 1.0, 1.0 );
- glBegin( GL_TRIANGLE_FAN );
- glVertex3f( 0.0, 0.0, 0.0 ); /* peak */
- glVertex3f( -0.5, -0.5, 0.5 ); /* front left */
- glVertex3f( 0.5, -0.5, 0.5 ); /* front right */
- glVertex3f( 0.5, -0.5, -0.5 ); /* back right */
- glVertex3f( -0.5, -0.5, -0.5 ); /* back left */
- glVertex3f( -0.5, -0.5, 0.5 ); /* front left */
- glEnd();
- glPopMatrix();
-
- glPushMatrix();
- /* move to just in front of the house */
- glTranslatef( 0.0, 0.0, 0.50001 );
-
- /* draw the door */
- glColor3f( 0.5, 0.2, 0.1 ); /* brown */
- glNormal3f( 0.0, 0.0, 1.0 );
- glRectf( -0.2, -0.75, 0.2, 0.0 );
-
- glPushMatrix();
- /* move to the location for the left window */
- glTranslatef( -0.4, 0.2, 0.0 );
- drawWindow();
- glPopMatrix();
-
- glPushMatrix();
- /* move to the location for the right window */
- glTranslatef( 0.2, 0.2, 0.0 );
- drawWindow();
- glPopMatrix();
- glPopMatrix();
- }
-
- /* draw a lamp post centered at the current origin;
- * the bounding box for the lamp is 0.5 x 3.0 units
- */
- GLvoid
- drawLamp( GLint light )
- {
- GLfloat spot_color[] = { 1.0, 1.0, 0.0, 1.0 };
-
- GLfloat spot_dir[] = { 0.0, 0.0, -1.0 };
- GLfloat spot_cutoff = 30.0;
- GLfloat spot_exp = 20.0;
-
- /* Note that a spotlight is a local light */
- GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 };
-
- /* draw lamp post */
- glColor3f( 0.0, 0.0, 0.0 );
- SolidCylinder( 0.05, 3.0 );
-
- glPushMatrix();
- glTranslatef( 0.0, 1.5, 0.0 );
- glRotatef( -90, 0.0, 0.0, 1.0 );
- glTranslatef( 0.0, 0.25, 0.0 );
- SolidCylinder( 0.02, 0.5 );
-
- glTranslatef( 0.0, 0.2, 0.0 );
- glRotatef( -90.0, 0.0, 1.0, 0.0 );
- glTranslatef( 0.0, 0.0, -0.1 );
- glutSolidCone( 0.05, 0.1, 15, 15 );
-
- if ( night ) { /* turn on the lamp */
- glLightfv( light, GL_DIFFUSE, spot_color );
- glLightfv( light, GL_SPECULAR, spot_color );
- glLightfv( light, GL_SPOT_DIRECTION, spot_dir );
- glLightf( light, GL_SPOT_CUTOFF, spot_cutoff );
- glLightf( light, GL_SPOT_EXPONENT, spot_exp );
- glLightfv( light, GL_POSITION, light_position );
-
- glEnable( light );
- } else
- glDisable( light );
- glPopMatrix();
-
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- /* local light */
- GLfloat lightPosition[] = { 0.0, 0.0, 0.0, 1.0 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
-
- /* Move the reference point */
- gluLookAt( 0.0, 0.0, 4.0, xRef, yRef, 0.0, 0.0, 1.0, 0.0 );
-
- /* draw the sun */
- glPushMatrix();
- /* move to the location of the sun */
- glTranslatef( sunPosition, 2.0, -1.5 );
-
- /* use a local light for the sun */
- glLightfv( GL_LIGHT0, GL_POSITION, lightPosition );
-
- if ( night ) {
- glColor3fv( white );
- glMaterialfv( GL_FRONT, GL_EMISSION, white );
- } else {
- glColor3fv( yellow );
- }
- glutSolidSphere( 0.2, 8, 15 );
- if ( night )
- glMaterialfv( GL_FRONT, GL_EMISSION, black );
- glPopMatrix();
-
- glPushMatrix();
- glTranslatef( -1.5, 0.0, 0.5 );
- glScalef( 0.7, 0.7, 0.7 );
- drawLamp( GL_LIGHT1 );
- glPopMatrix();
-
- /* draw the ground as a quad mesh so that it will
- * be lit properly
- */
- glPushMatrix();
- glTranslatef( 0.0, -1.0, 0.0 );
- glRotatef( -45.0, 1.0, 0.0, 0.0 );
- glScalef( 3.0, 1.0, 1.0 );
- drawGround();
- glPopMatrix();
-
- /* draw the house */
- glPushMatrix();
- /* move to where the center of the house should be */
- glTranslatef( -0.5, 0.0, 0.0 );
-
- /* shrink the house slightly */
- glScalef( 0.7, 0.7, 0.7 );
- drawHouse();
- glPopMatrix();
-
- /* draw several tulips in the foreground */
- glPushMatrix();
- glTranslatef( 1.0, -1.0, 1.0 );
- drawFlower( red );
- glTranslatef( 0.0, 0.0, -0.5 );
- drawFlower( yellow );
- glPopMatrix();
- glPushMatrix();
- glTranslatef( -1.0, -1.0, 1.0 );
- drawFlower( blue );
- glTranslatef( 0.0, 0.0, -0.5 );
- drawFlower( magenta );
- glPopMatrix();
-
- glPopMatrix();
-
- checkError( "drawScene" );
- glutSwapBuffers();
- }
-