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.
- */
-
- /* koosh.c
- * This program draws a koosh ball. The lighting resources are kept in
- * display lists which are called by the koosh ball display list.
- *
- * <m> key - change material propertes
- * <l> key - toggle infinite/local light model
- * <i> key - change light intensity
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( int );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid makeKooshList( GLvoid );
- GLvoid drawKoosh( GLvoid );
-
- GLvoid modifyMaterial( void );
- GLvoid modifyLightIntensity( void );
- GLvoid modifyLightModel( void );
-
- void printHelp( char *progname );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- #define MATERIAL 0
- #define LIGHT 1
- #define LIGHTMODEL 2
-
- /* Global Variables */
-
- static GLfloat spin = 0.0;
-
- static GLuint kooshIndex, properties[3];
-
- /* light properties */
-
- static GLfloat mat_ambient[] = { 0.25, 0.05, 0.4, 1.0 };
- static GLfloat mat_diffuse[] = { 0.5, 0.1, 0.8, 1.0 };
- static GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- static GLfloat mat_shininess[] = { 10.0 };
-
- static GLfloat light_ambient[] = { 0.75, 0.95, 0.6, 1.0 };
- static GLfloat light_diffuse[] = { 0.5, 0.9, 0.2, 1.0 };
- static GLfloat light_specular[] = { 0.0, 0.0, 0.0, 1.0 };
- static GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
-
- static GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
- static GLfloat lmodel_viewer[] = { 0.0 };
-
- void
- 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 );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf( stdout,
- "\n%s draws a koosh ball. The lighting resources are kept in\n"
- "display lists which are called by the main koosh ball "
- "display list.\n\n"
- "<m> key - change material properties\n"
- "<l> key - change light model\n"
- "<i> key - change light intensity\n"
- "Escape key - exit the program\n\n",
- progname );
- }
-
- GLvoid
- initgfx( void )
- {
- /* initialize lighting property display lists */
- properties[MATERIAL] = glGenLists( 3 );
- glNewList( properties[MATERIAL], GL_COMPILE );
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glEndList();
-
- properties[LIGHT] = properties[MATERIAL] + 1;
- glNewList( properties[LIGHT], GL_COMPILE );
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glEndList();
-
- properties[LIGHTMODEL] = properties[MATERIAL] + 2;
- glNewList( properties[LIGHTMODEL], GL_COMPILE );
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
- glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_viewer);
- glEndList();
-
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glClearColor( 0, 0, 0, 1 );
-
- glEnable(GL_DEPTH_TEST);
-
- makeKooshList();
- }
-
- GLvoid
- modifyMaterial( void )
- {
- int i;
-
- for (i = 0; i < 3; i++) {
- mat_ambient[i] = drand48();
- mat_diffuse[i] = drand48();
- mat_specular[i] = drand48();
- }
- mat_shininess[0] = drand48() * 128.0;
- printf("\nChanging MATERIAL properties to:\n"
- "\tambient = %g %g %g\n\tdiffuse = %g %g %g\n"
- "\tspecular = %g %g %g\n\tshininess = %g\n",
- mat_ambient[0], mat_ambient[1], mat_ambient[1],
- mat_diffuse[0], mat_diffuse[1], mat_diffuse[1],
- mat_specular[0], mat_specular[1], mat_specular[1],
- mat_shininess[0]);
-
- glNewList( properties[MATERIAL], GL_COMPILE );
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glEndList();
- }
-
- GLvoid
- modifyLightIntensity( void )
- {
- int i;
-
- for (i = 0; i < 3; i++) {
- light_ambient[i] = drand48();
- light_diffuse[i] = drand48();
- light_specular[i] = drand48();
- }
- printf("\nChanging LIGHT properties to:\n"
- "\tambient = %g %g %g\n\tdiffuse = %g %g %g\n"
- "\tspecular = %g %g %g\n",
- light_ambient[0], light_ambient[1], light_ambient[1],
- light_diffuse[0], light_diffuse[1], light_diffuse[1],
- light_specular[0], light_specular[1], light_specular[1]);
-
- glNewList( properties[LIGHT], GL_COMPILE );
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glEndList();
- }
-
- GLvoid
- modifyLightModel( void )
- {
- int i;
-
- for (i = 0; i < 3; i++) {
- lmodel_ambient[i] = drand48();
- }
- lmodel_viewer[0] = !lmodel_viewer[0];
- printf("\nChanging LIGHT MODEL properties to:\n"
- "\tambient = %g %g %g\n\tmodel is %s\n",
- lmodel_ambient[0], lmodel_ambient[1], lmodel_ambient[1],
- (lmodel_viewer[0] == 0 ? "local" : "infinite"));
-
- glNewList( properties[LIGHTMODEL], GL_COMPILE );
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
- glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_viewer);
- glEndList();
- }
-
- GLvoid
- makeKooshList( GLvoid )
- {
- kooshIndex = glGenLists(1);
- glNewList( kooshIndex, GL_COMPILE );
- glCallLists( 3, GL_UNSIGNED_INT, properties );
- drawKoosh();
- glEndList();
- }
-
- GLvoid
- drawKoosh( GLvoid )
- {
- #define MAX_STRANDS 60
- #define RADIAN_INCREMENT 2*M_PI/MAX_STRANDS
- #define DEGREE_INCREMENT 360.0/MAX_STRANDS
-
- register int i, j;
- static GLfloat origin[] = { 0.0, 0.0 };
- static GLfloat v[] = { 0.0, 0.0, 0.0 };
- float radians;
-
- for( i = 0; i < MAX_STRANDS; i++ ) {
- glRotatef( i * DEGREE_INCREMENT, 0, 1, 0 );
- glBegin( GL_LINES );
- for( j = 0; j < MAX_STRANDS; j++ ) {
- radians = j * RADIAN_INCREMENT;
- v[0] = cosf( radians ); v[1] = sinf( radians );
- glNormal3fv( v );
- glVertex2fv( origin );
- glVertex2fv( v );
- }
- glEnd();
- }
- }
-
- GLvoid
- keyboard( char key, int x, int y )
- {
- switch (key) {
- case 'm':
- modifyMaterial();
- break;
- case 'l':
- modifyLightModel();
- break;
- case 'i':
- modifyLightIntensity();
- break;
- case KEY_ESC:
- exit(0);
- }
- glutPostRedisplay();
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- aspect = (GLdouble) width / (GLdouble) height;
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, aspect, 1.0, 9.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.0, 0.0, -5.0 );
- }
-
- GLvoid
- animate( GLvoid )
- {
- spin = fmodf( spin + 1.0, 360.0 );
- glutPostRedisplay(); /* Tell GLUT to redraw the scene */
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glPushMatrix();
- glRotatef( spin, 1, 1, 1 );
- glCallList( kooshIndex );
- glPopMatrix();
- glutSwapBuffers();
- }
-