Main Page | File List

movelight.c

00001 /*
00002  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
00003  * ALL RIGHTS RESERVED 
00004  * Permission to use, copy, modify, and distribute this software for 
00005  * any purpose and without fee is hereby granted, provided that the above
00006  * copyright notice appear in all copies and that both the copyright notice
00007  * and this permission notice appear in supporting documentation, and that 
00008  * the name of Silicon Graphics, Inc. not be used in advertising
00009  * or publicity pertaining to distribution of the software without specific,
00010  * written prior permission. 
00011  *
00012  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
00013  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
00014  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
00015  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
00016  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
00017  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
00018  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
00019  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
00020  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
00021  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
00022  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
00023  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
00024  * 
00025  * US Government Users Restricted Rights 
00026  * Use, duplication, or disclosure by the Government is subject to
00027  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
00028  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
00029  * clause at DFARS 252.227-7013 and/or in similar or successor
00030  * clauses in the FAR or the DOD or NASA FAR Supplement.
00031  * Unpublished-- rights reserved under the copyright laws of the
00032  * United States.  Contractor/manufacturer is Silicon Graphics,
00033  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
00034  *
00035  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
00036  */
00037 
00038 /*
00039  *  movelight.c
00040  *  This program demonstrates when to issue lighting and
00041  *  transformation commands to render a model with a light
00042  *  which is moved by a modeling transformation (rotate or
00043  *  translate).  The light position is reset after the modeling
00044  *  transformation is called.  The eye position does not change.
00045  *
00046  *  A sphere is drawn using a grey material characteristic.
00047  *  A single light source illuminates the object.
00048  *
00049  *  Interaction:  pressing the left mouse button alters
00050  *  the modeling transformation (x rotation) by 30 degrees.
00051  *  The scene is then redrawn with the light in a new position.
00052  */
00053 #include <GL/glut.h>
00054 #include <stdlib.h>
00055 
00056 static int spin = 0;
00057 
00058 /*  Initialize material property, light source, lighting model,
00059  *  and depth buffer.
00060  */
00061 void init(void) 
00062 {
00063    glClearColor (0.0, 0.0, 0.0, 0.0);
00064    glShadeModel (GL_SMOOTH);
00065    glEnable(GL_LIGHTING);
00066    glEnable(GL_LIGHT0);
00067    glEnable(GL_DEPTH_TEST);
00068 }
00069 
00070 /*  Here is where the light position is reset after the modeling
00071  *  transformation (glRotated) is called.  This places the
00072  *  light at a new position in world coordinates.  The cube
00073  *  represents the position of the light.
00074  */
00075 void display(void)
00076 {
00077    GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 };
00078 
00079    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00080    glPushMatrix ();
00081    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
00082 
00083    glPushMatrix ();
00084    glRotated ((GLdouble) spin, 1.0, 0.0, 0.0);
00085    glLightfv (GL_LIGHT0, GL_POSITION, position);
00086 
00087    glTranslated (0.0, 0.0, 1.5);
00088    glDisable (GL_LIGHTING);
00089    glColor3f (0.0, 1.0, 1.0);
00090    glutWireCube (0.1);
00091    glEnable (GL_LIGHTING);
00092    glPopMatrix ();
00093 
00094    glutSolidTorus (0.275, 0.85, 8, 15);
00095    glPopMatrix ();
00096    glFlush ();
00097 }
00098 
00099 void reshape (int w, int h)
00100 {
00101    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
00102    glMatrixMode (GL_PROJECTION);
00103    glLoadIdentity();
00104    gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
00105    glMatrixMode(GL_MODELVIEW);
00106    glLoadIdentity();
00107 }
00108 
00109 void mouse(int button, int state, int x, int y)
00110 {
00111    switch (button) {
00112       case GLUT_LEFT_BUTTON:
00113          if (state == GLUT_DOWN) {
00114             spin = (spin + 30) % 360;
00115             glutPostRedisplay();
00116          }
00117          break;
00118       default:
00119          break;
00120    }
00121 }
00122 
00123 void keyboard(unsigned char key, int x, int y)
00124 {
00125    switch (key) {
00126       case 27:
00127          exit(0);
00128          break;
00129    }
00130 }
00131 
00132 int main(int argc, char** argv)
00133 {
00134    glutInit(&argc, argv);
00135    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
00136    glutInitWindowSize (500, 500); 
00137    glutInitWindowPosition (100, 100);
00138    glutCreateWindow (argv[0]);
00139    init ();
00140    glutDisplayFunc(display); 
00141    glutReshapeFunc(reshape);
00142    glutMouseFunc(mouse);
00143    glutKeyboardFunc(keyboard);
00144    glutMainLoop();
00145    return 0;
00146 }

Generated on Tue Jul 20 16:28:39 2004 by doxygen 1.3.6