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.
- */
-
- /* perspective.c - open a window and clear the background.
- * Set up callbacks to handle keyboard input.
- * Model some objects. Use perspective projection.
- *
- * F1 key - print help information
- * SPACE key - generates a random background color
- * <s> key - toggle smooth/flat shading
- * 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 drawScene( GLvoid );
-
- void checkError( char * );
- void printHelp( char * );
-
- /* Global Variables */
-
- static char *progname;
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- 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 );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutDisplayFunc( drawScene );
-
- progname = argv[0];
-
- printHelp( progname );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout,
- "\n%s - model some objects using a perspective projection\n\n"
- "F1 key - print help information\n"
- "SPACE Key - generates a random background color\n"
- "<s> key - toggle smooth/flat shading\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- /* set clear color to blue */
- glClearColor( 0.0, 0.0, 1.0, 1.0 );
-
- glMatrixMode( GL_PROJECTION );
- gluPerspective( 45.0, 1.0, 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;
-
- switch (key) {
- case ' ': /* SPACE key */
- /* generate a random background color */
- glClearColor( drand48(), drand48(), drand48(), 1.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;
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static GLfloat red[] = { 1.0, 0.0, 0.0 };
- static GLfloat yellow[] = { 1.0, 1.0, 0.0 };
- static GLfloat blue[] = { 0.0, 0.0, 1.0 };
- static GLfloat green[] = { 0.0, 1.0, 0.0 };
- static GLfloat darkgreen[] = { 0.0, 0.25, 0.0 };
-
- /* left window */
- static GLfloat v0[] = { -0.4, 0.8 };
- static GLfloat v1[] = { -0.4, 0.4 };
- static GLfloat v2[] = { -0.3, 0.8 };
- static GLfloat v3[] = { -0.3, 0.4 };
- static GLfloat v4[] = { -0.2, 0.8 };
- static GLfloat v5[] = { -0.2, 0.4 };
-
- /* right window */
- static GLfloat v6[] = { 0.2, 0.8 };
- static GLfloat v7[] = { 0.2, 0.4 };
- static GLfloat v8[] = { 0.3, 0.8 };
- static GLfloat v9[] = { 0.3, 0.4 };
- static GLfloat v10[] = { 0.4, 0.8 };
- static GLfloat v11[] = { 0.4, 0.4 };
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- glPushMatrix();
-
- /* move inside the viewing volume */
- glTranslatef( 0.0, 0.0, -4.0 );
-
- /* draw the ground in different shades of green */
- glBegin( GL_POLYGON );
- glColor3fv( green );
- glVertex2f( -2.0, -2.0 );
- glVertex2f( 2.0, -2.0 );
- glColor3fv( darkgreen );
- glVertex2f( 2.0, 0.0 );
- glVertex2f( -2.0, 0.0 );
- glEnd();
-
- /* draw the house */
- glColor3f( 1.0, 1.0, 1.0 ); /* white */
- glRectf( -0.5, -0.5, 0.5, 1.0 );
-
- /* draw the door */
- glColor3f( 0.5, 0.2, 0.1 ); /* brown */
- glRectf( -0.2, -0.5, 0.2, 0.2 );
-
- /* draw the roof */
- glBegin( GL_TRIANGLES );
- glColor3f( 0.0, 0.0, 0.0 ); /* black */
- glVertex2f( -0.5, 1.0 );
- glVertex2f( 0.5, 1.0 );
- glVertex2f( 0.0, 1.5 );
- glEnd();
-
- /* draw 2 quadrilateral strip to make a window */
- 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 2 quadrilateral strip to make a window */
- glBegin( GL_QUAD_STRIP );
- glVertex2fv (v6);
- glColor3f( 0.3, 0.6, 1.0 );
- glVertex2fv (v7);
- glColor3f( 0.2, 0.7, 0.0 );
- glVertex2fv (v8);
- glColor3f( 0.1, 0.8, 1.0 );
- glVertex2fv (v9);
- glColor3f( 0.0, 0.9, 0.0 );
- glVertex2fv (v10);
- glColor3f( 0.0, 1.0, 1.0 );
- glVertex2fv (v11);
- glEnd();
-
- /* Draw a triangle fan for the sun */
- glBegin( GL_TRIANGLE_FAN );
- glColor3f( 1.0, 1.0, 1.0 );
- glVertex2f( 1.5, 1.5 );
- glColor3fv( yellow );
- glVertex2f( 1.5, 1.3 );
- glVertex2f( 1.7, 1.4 );
- glVertex2f( 1.7, 1.6 );
- glVertex2f( 1.5, 1.7 );
- glVertex2f( 1.3, 1.6 );
- glVertex2f( 1.3, 1.4 );
- glVertex2f( 1.5, 1.3 );
- glEnd();
-
- /* draw a tulip in the foreground */
-
- glTranslatef( 1.0, -1.0, 1.0 );
-
- /* draw the stem with 2 leaves */
-
- glColor3fv( darkgreen );
- 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();
-
- /* move to the top of the stem */
-
- glTranslatef( 0.0, 0.25, 0.0 );
-
- /* use a triangle fan for the flower head */
-
- glBegin( GL_TRIANGLE_FAN );
- glColor3f( 1.0, 1.0, 1.0 );
- glVertex2f( 0.0, 0.0 );
- glColor3f( 1.0, 0.0, 0.0 );
- glVertex2f( 0.0, -0.03 );
- glVertex2f( 0.03, -0.02 );
- glVertex2f( 0.03, 0.02 );
- glVertex2f( 0.0, 0.03 );
- glVertex2f( -0.03, 0.02 );
- glVertex2f( -0.03, -0.02 );
- glVertex2f( 0.0, -0.03 );
- glEnd();
-
- glPopMatrix();
-
- checkError( "drawScene" );
- glFlush();
- }
-