home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 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.
- */
-
- /* solar12.c - a simple solar system.
- * Added a reshape handler to reset the viewport and viewing volume.
- * Add depth buffering and back face culling.
- * Add a viewing transformation and control it using mouse input.
- * Enable lighting and give the sun, earth and moon material properties
- * Add continuous animation and a menu to control it.
- * Add two-sided lighting and a local light.
- * Add text.
- *
- * F1 key - print help information
- * Left Mouse Button - change incidence and azimuth angles
- * Middle Mouse Button - change the twist angle based on
- * horizontal mouse movement
- * Right Mouse Button - zoom in and out based on vertical
- * mouse movement
- * <r> Key - toggle rotation on / off
- * <R> Key - reset viewpoint
- * SPACE Key - toggle between solid/wireframe models
- * 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 mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
- GLvoid animate( GLvoid );
- GLvoid visibility( GLint );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid drawScene( GLvoid );
-
- void checkError( char * );
- void printHelp( char * );
-
- void resetView( GLvoid );
- void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
-
- /* Global Variables */
-
- static char *progname;
-
- static GLboolean filledFlag = GL_TRUE;
-
- static enum actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
- static GLint action;
-
- static GLdouble xStart = 0.0, yStart = 0.0;
-
- static GLfloat sunRadius = 0.7;
- static GLfloat earthRadius = 0.4, earthOrbit = 3.5;
- static GLfloat moonRadius = 0.2, moonOrbit = 1.0;
- static GLfloat near, far, distance, twistAngle, incAngle, azimAngle;
-
- static GLfloat year = 45.0, day = -90.0;
-
- static void *fixedFont, *strokeFont;
-
- /* 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 | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutReshapeFunc( reshape );
- glutKeyboardFunc( keyboard );
- glutSpecialFunc( specialkeys );
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutDisplayFunc( drawScene );
-
- progname = argv[0];
-
- printHelp( progname );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout,
- "\n%s - model some objects\n\n"
- "F1 key - print help information\n"
- "Left Mousebutton - move eye position\n"
- "Middle Mousebutton - change twist angle\n"
- "Right Mousebutton - move up / down to zoom in / out\n"
- "<r> Key - toggle rotation on / off\n"
- "<R> Key - reset viewpoint\n"
- "SPACE key - toggle between solid/wireframe mode\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat maxOrbit;
-
- /* Set up properties for the light */
- GLfloat sunSpecular[] = { 0.0, 0.0, 0.0, 1.0 };
-
- GLfloat two_side = 1.0;
-
- GLfloat lmodelAmbient[] = { 0.8, 0.8, 0.8, 1.0 };
-
- /* set clear color to black */
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
-
- /* enable the depth buffer */
- glEnable( GL_DEPTH_TEST );
-
- /* Maximum size of all the objects in your scene */
- maxOrbit = earthOrbit + moonOrbit + moonRadius;
-
- /* Set up near and far so that ( far - near ) > maxObjectSize, */
- /* and determine the viewing distance (adjust for zooming) */
- near = 1.0;
- far = near + 8*maxOrbit;
-
- resetView();
-
- /* Set the light properties */
- glLightfv( GL_LIGHT0, GL_SPECULAR, sunSpecular );
-
- glLightModelf( GL_LIGHT_MODEL_TWO_SIDE, two_side );
- glLightModelfv( GL_LIGHT_MODEL_AMBIENT, lmodelAmbient );
-
- /* Turn on the default light */
- glEnable( GL_LIGHT0 );
-
- /* Enable fast material changes for diffuse material */
- glColorMaterial( GL_FRONT, GL_DIFFUSE );
-
- /* Set up two fonts to use for rendering */
- fixedFont = GLUT_BITMAP_9_BY_15;
- strokeFont = GLUT_STROKE_ROMAN;
- }
-
- 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, near, far );
-
- 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 rotateFlag = GL_TRUE;
-
- switch (key) {
- case 'r':
- rotateFlag = !rotateFlag;
- if (rotateFlag) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- glutPostRedisplay();
- break;
- case ' ': /* toggle fill mode */
- filledFlag = !filledFlag;
- glutPostRedisplay();
- break;
- case 'R':
- resetView();
- 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
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- 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 {
- 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 = fmodf(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();
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* update the rotation of the earth and moon in their orbits */
- year = fmodf( (year + 0.5), 360.0 );
- day = fmodf( (day + 5.0), 360.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- void
- resetView( GLvoid )
- {
- distance = near + (far - near) / 2.0;
- twistAngle = 0.0; /* rotation of viewing volume (camera) */
- incAngle = 0.0;
- azimAngle = 0.0;
- }
-
- void
- polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
- GLfloat twist)
- {
- glTranslatef( 0.0, 0.0, -distance);
- glRotatef( -twist, 0.0, 0.0, 1.0);
- glRotatef( -incidence, 1.0, 0.0, 0.0);
- glRotatef( -azimuth, 0.0, 0.0, 1.0);
- }
-
- GLvoid
- renderBitmapString( void *font, char *string )
- {
- int i;
- int len = (int) strlen(string);
- for (i = 0; i < len; i++) {
- glutBitmapCharacter(font, string[i]);
- }
- }
-
- GLvoid
- renderStrokeString( void *font, char *string )
- {
- int i;
- int len = (int) strlen(string);
- for (i = 0; i < len; i++) {
- glutStrokeCharacter(font, string[i]);
- }
- }
-
- /* print mouse and keyboard input commands */
- GLvoid
- printInputCommands( void *font )
- {
- static char *lmouse = "left mouse - change incidence & azimuth";
- static char *mmouse = "middle mouse - change twist";
- static char *rmouse = "right mouse - zoom in and out";
- static char *Rkey = "R key - reset view";
-
- GLfloat yoffset;
- GLsizei winHeight = glutGet(GLUT_WINDOW_HEIGHT);
-
- yoffset = winHeight-15.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- renderBitmapString( font, lmouse );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- renderBitmapString( font, mmouse );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- renderBitmapString( font, rmouse );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- renderBitmapString( font, Rkey );
- }
-
- /* update status of polarView parameters */
- GLvoid
- printStatus( void *font )
- {
- static char s[50];
- GLfloat yoffset = 65.0;
-
- /* update polarview information */
- glRasterPos3f( 10.0, yoffset, 0.0 );
- sprintf (s, "incidence is %4d", (GLint) incAngle);
- renderBitmapString( font, s );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- sprintf (s, "azimuth is %4d", (GLint) azimAngle);
- renderBitmapString( font, s );
-
- yoffset -= 20.0;
- glRasterPos3f( 10.0, yoffset, 0.0 );
- sprintf (s, "Twist is %4d", (GLint) twistAngle);
- renderBitmapString( font, s );
- }
-
- GLvoid
- renderStationaryText( void *font )
- {
- GLsizei winWidth, winHeight;
-
- winWidth = glutGet(GLUT_WINDOW_WIDTH);
- winHeight = glutGet(GLUT_WINDOW_HEIGHT);
-
- /* Turn off the depth buffer */
- glDisable( GL_DEPTH_TEST );
-
- /* save and then clear modelview matrix */
- glPushMatrix();
- glLoadIdentity();
-
- /* save Projection matrix */
- glMatrixMode( GL_PROJECTION );
- glPushMatrix();
- /* reset world space to display status */
- glLoadIdentity();
- glOrtho( 0.0, winWidth, 0.0, winHeight, -1.0, 1.0 );
-
- printInputCommands( font );
- printStatus( font );
-
- /* restore projection matrix */
- glPopMatrix();
-
- /* restore previous modelview transformations */
- glMatrixMode( GL_MODELVIEW );
- glPopMatrix();
-
- /* Turn on the depth buffer */
- glEnable( GL_DEPTH_TEST );
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- static GLfloat yellow[] = { 1.0, 1.0, 0.0, 1.0 };
- static GLfloat blue[] = { 0.0, 0.0, 1.0, 1.0 };
- static GLfloat gray[] = { 0.4, 0.4, 0.4, 1.0 };
- static GLfloat moltenRed[] = { 1.0, 0.1, 0.05, 1.0 };
-
- static GLfloat noEmission[] = { 0.0, 0.0, 0.0, 1.0 };
- static GLfloat defaultDiffuse[] = { 0.8, 0.8, 0.8, 1.0 };
-
- /* a local light */
- static GLfloat lightPosition[] = { 0.0, 0.0, 0.0, 1.0 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
-
- /* set up viewing transformation */
- polarView( distance, azimAngle, incAngle, twistAngle );
-
- /* draw sun */
-
- /* position the light at the same location as the sun,
- * so that the light emanates from it */
- glLightfv( GL_LIGHT0, GL_POSITION, lightPosition );
-
- /* use the same color for the labels as for the planets */
- glColor3f( 1.0, 1.0, 0.0 );
- glPushMatrix();
- glTranslatef(0.0, sunRadius+0.1, 0.0);
- glScalef( 0.003, 0.003, 0.003 );
- renderStrokeString(strokeFont, "sun");
- glPopMatrix();
-
- glEnable( GL_LIGHTING );
- glEnable( GL_COLOR_MATERIAL );
-
- /* Give the sun a yellow glow */
- glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, yellow );
-
- glPushMatrix();
- /* rotate on our own axis */
- glRotatef( 90.0, 1.0, 0.0, 0.0 );
- if (filledFlag)
- glutSolidSphere( sunRadius, 15, 15 );
- else
- glutWireSphere( sunRadius, 15, 15 );
- glPopMatrix();
-
- /* turn emmission off while drawing planets */
- glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, noEmission );
-
- /* set material for the earth */
- glColor4fv( blue );
-
- /* set material for the core */
- glMaterialfv( GL_BACK, GL_AMBIENT_AND_DIFFUSE,
- moltenRed );
-
- glPushMatrix();
-
- /* draw earth */
-
- /* rotate to the right time of year */
- glRotatef( year, 0.0, 1.0, 0.0 );
-
- /* translate out to our orbit about the sun */
- glTranslatef( earthOrbit, 0.0, 0.0 );
-
- glDisable( GL_LIGHTING );
- glPushMatrix();
- glTranslatef(0.0, earthRadius+0.1, 0.0);
- glRotatef(360-year, 0.0, 1.0, 0.0);
- glScalef( 0.003, 0.003, 0.003 );
- renderStrokeString(strokeFont, "earth");
- glPopMatrix();
- glEnable( GL_LIGHTING );
-
- glPushMatrix();
- /* rotate on our own axis */
- glRotatef( 90.0, 1.0, 0.0, 0.0 );
- if (filledFlag)
- glutSolidSphere( earthRadius, 15, 15 );
- else
- glutWireSphere( earthRadius, 15, 15 );
- glPopMatrix();
-
- /* set material for the moon */
- glColor4fv( gray );
-
- /* set material for the core of the moon */
- glMaterialfv( GL_BACK, GL_AMBIENT_AND_DIFFUSE, gray );
-
- /* draw moon */
- glPushMatrix();
- /* rotate to the right time of day */
- glRotatef( day, 0.0, 1.0, 0.0 );
-
- /* translate out to our orbit about the earth */
- glTranslatef( moonOrbit, 0.0, 0.0 );
-
- glDisable( GL_LIGHTING );
- glPushMatrix();
- glTranslatef(0.0, moonRadius+0.1, 0.0);
- glRotatef(360-year-day, 0.0, 1.0, 0.0);
- glScalef( 0.003, 0.003, 0.003 );
- renderStrokeString(strokeFont, "moon");
- glPopMatrix();
- glEnable( GL_LIGHTING );
-
- /* rotate on our axis */
- glRotatef( 90.0, 1.0, 0.0, 0.0 );
- if (filledFlag)
- glutSolidSphere( moonRadius, 15, 15 );
- else
- glutWireSphere( moonRadius, 15, 15 );
-
- glPopMatrix();
- glPopMatrix();
-
- glDisable( GL_COLOR_MATERIAL );
- glDisable( GL_LIGHTING );
-
- glPopMatrix();
-
- glColor3f( 1.0, 1.0, 0.0 );
- renderStationaryText( fixedFont );
-
- checkError( "drawScene" );
-
- glutSwapBuffers();
- }
-