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.
- */
-
- /* twoSide.c - demonstrates two-sided lighting model and ambient
- * lighting; it renders a rotating cylinder which as different
- * material properties for the front and back sides of a cylinder.
- *
- * <l> key - toggle LIGHT0 on / off
- * Escape key - exit the program
- */
- #include <math.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid visibility( GLint );
- GLvoid animate( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid toggleLight( GLvoid );
-
- 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 */
-
- static GLfloat spin = 0.0;
-
- static GLdouble fovy, near, far;
- static GLfloat distance, twistAngle, incAngle, azimAngle;
-
- GLvoid
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowSize( width / 2, height / 2 );
- glutInitWindowPosition( width / 4, height / 4 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutVisibilityFunc( visibility );
- glutIdleFunc( animate );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrate two sided lighting\n\n"
- "Escape key - exit the program\n"
- "<l> key - toggle LIGHT0 on / off\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat mat_ambient[] = { 0.25, 0.05, 0.4, 1.0 };
- GLfloat mat_diffuse[] = { 0.5, 0.1, 0.8, 1.0 };
- GLfloat mat_diffuse_back[] = { 1.0, 1.0, 0.0, 1.0 };
-
- GLfloat lmodel_ambient[] = { 0.8, 0.8, 0.8, 1.0 };
- GLfloat two_side = 1.0;
-
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_BACK, GL_DIFFUSE, mat_diffuse_back);
-
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
- glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, two_side);
-
- glEnable(GL_LIGHTING);
- glEnable( GL_LIGHT0 );
-
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glEnable( GL_DEPTH_TEST );
-
- fovy = 60.0; /* field of view in Y */
- near = 3.0; /* Near clipping plane location */
- far = 12.0; /* Far clipping plane location */
-
- resetView();
- }
-
- void
- animate()
- {
- spin = fmodf(spin + 1.0, 360.0);
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- keyboard( char key, int x, int y )
- {
-
- switch (key) {
- case 'l':
- toggleLight();
- break;
- case KEY_ESC:
- exit(0);
- }
- }
-
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( 0, 0, width, height );
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- aspect = (GLdouble) width / (GLdouble) height;
- gluPerspective( fovy, aspect, near, far );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- toggleLight( GLvoid )
- {
- static GLboolean lightOn = GL_TRUE;
-
- lightOn = !lightOn;
- if ( lightOn )
- glEnable( GL_LIGHT0 );
- else
- glDisable( GL_LIGHT0 );
- }
-
- void
- resetView()
- {
- distance = 8.0;
- incAngle = 30.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
- drawScene( GLvoid )
- {
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
-
- polarView( distance, azimAngle, incAngle, 0 );
-
- glPushMatrix();
- glRotatef( spin, 1.0, 1.0, 0.0 );
- SolidCylinder( 1.0, 2.0 );
- glPopMatrix();
-
- glPopMatrix();
-
- glutSwapBuffers();
- }
-