home *** CD-ROM | disk | FTP | other *** search
- /* Copyright 1993, 1995, 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.
- */
- /* spotLight.c - spotlight shining on a stack of flat grids
- *
- * Escape key - exit the program
- * Left Mouse Button - change incidence and azimuth angles
- * <d> key - toggle depth buffering
- * <s> key - toggle flat/smooth grid
- * <R> Key - reset viewpoint
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.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 mouse( GLint, GLint, GLint, GLint );
- GLvoid motion( GLint, GLint );
-
- GLvoid toggleDepthBuffer( GLvoid );
- GLvoid toggleSmooth( 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 GLboolean moving = GL_FALSE;
-
- static GLdouble xStart = 0.0, yStart = 0.0;
-
- static GLdouble fovy, near, far;
- static GLfloat distance, twistAngle, incAngle, azimAngle;
-
- void
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( 0, height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutMouseFunc( mouse );
- glutMotionFunc( motion );
- glutKeyboardFunc( keyboard );
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - demonstrate spot lights\n\n"
- "Escape key - exit the program\n"
- "Left Mousebutton - move eye position\n"
- "<d> key - toggle depth buffering\n"
- "<s> key - toggle flat/smooth grid\n"
- "<R> key - reset viewpoint \n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
-
- glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse );
-
- 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();
- }
-
- 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
- animate( GLvoid )
- {
- /* update the rotation of the spotlight */
- spin = fmodf( (spin + 3.0), 360.0 );
-
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case 'd': /* toggle depth buffering */
- toggleDepthBuffer();
- glutPostRedisplay();
- break;
- case 's':
- toggleSmooth();
- glutPostRedisplay();
- break;
- case 'R':
- resetView();
- glutPostRedisplay();
- break;
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- mouse( GLint button, GLint state, GLint x, GLint y )
- {
- if (state == GLUT_DOWN && button == GLUT_LEFT_BUTTON) {
- /* Update the saved mouse position */
- xStart = x;
- yStart = y;
- moving = GL_TRUE;
- } else {
- moving = GL_FALSE;
- }
- }
-
- GLvoid
- motion( GLint x, GLint y )
- {
- if (moving) {
- /* Adjust the eye position based on the mouse position */
- azimAngle += (GLdouble) (x - xStart);
- incAngle -= (GLdouble) (y - yStart);
-
- /* Update the stored mouse position for later use */
- xStart = x;
- yStart = y;
-
- glutPostRedisplay();
- }
- }
-
- void
- resetView()
- {
- distance = 4.5;
- incAngle = -25.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
- toggleSmooth( GLvoid )
- {
- static GLboolean drawSmooth = GL_TRUE;
-
- drawSmooth = !drawSmooth;
- if (drawSmooth) {
- glShadeModel( GL_SMOOTH );
- } else {
- glShadeModel( GL_FLAT );
- }
- }
-
- GLvoid
- toggleDepthBuffer( GLvoid )
- {
- static GLboolean depthFlag = GL_TRUE;
-
- depthFlag = !depthFlag;
- if (depthFlag) {
- glEnable( GL_DEPTH_TEST );
- printf("Depth test ON\n");
- } else {
- glDisable( GL_DEPTH_TEST );
- printf("Depth test OFF\n");
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- int i;
-
- GLfloat spot_dir[] = { 0.2, -1.0, 0.0 };
- GLfloat spot_cutoff = 10.0;
- GLfloat spot_exp = 128.0;
-
- /* Note that a spotlight is a local light */
- GLfloat light_position[] = { 0.0, 2.0, 0.0, 1.0 };
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- glPushMatrix();
- polarView( distance, azimAngle, incAngle, 0 );
-
- glPushMatrix();
- /* make the spotlight rotate */
- glRotatef( spin, 0.0, 1.0, 0.0 );
- glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spot_dir );
- glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, spot_cutoff );
- glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, spot_exp );
- glLightfv( GL_LIGHT0, GL_POSITION, light_position );
- glPopMatrix();
-
- for( i = -2; i < 2; i++ ) {
- glPushMatrix();
- glTranslatef( 0.0, i * 0.5, 0.0 );
- glRotatef( -90.0, 1.0, 0.0, 0.0 );
- SolidGrid();
- glPopMatrix();
- }
- glPopMatrix();
-
- glutSwapBuffers();
- }
-
-