home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / book / sccolorlight.c < prev    next >
C/C++ Source or Header  |  1999-09-23  |  5KB  |  142 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /*
  5.  * (c) Copyright 1993, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. /*
  41.  *  sccolorlight.c
  42.  *  This program demonstrates the use of a colored 
  43.  *  (magenta, in this example) light source.  Objects 
  44.  *  are drawn using a grey material characteristic.  
  45.  *  A single light source illuminates the objects.
  46.  */
  47. #include <stdlib.h>
  48. #include <GL/glut.h>
  49.  
  50. /*  Initialize material property and light source.
  51.  */
  52. void myinit(void)
  53. {
  54.     GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  55.     GLfloat light_diffuse[] = { 1.0, 0.0, 1.0, 1.0 };
  56.     GLfloat light_specular[] = { 1.0, 0.0, 1.0, 1.0 };
  57. /*    light_position is NOT default value    */
  58.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  59.  
  60.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  61.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  62.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  63.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  64.     
  65.     glEnable(GL_LIGHTING);
  66.     glEnable(GL_LIGHT0);
  67.     glDepthFunc(GL_LESS);
  68.     glEnable(GL_DEPTH_TEST);
  69. }
  70.  
  71. void display(void)
  72. {
  73.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  74.     glPushMatrix ();
  75.     glRotatef (20.0, 1.0, 0.0, 0.0);
  76.  
  77.     glPushMatrix ();
  78.     glTranslatef (-0.75, 0.5, 0.0); 
  79.     glRotatef (90.0, 1.0, 0.0, 0.0);
  80.     glutSolidTorus (0.275, 0.85, 20, 20);
  81.     glPopMatrix ();
  82.  
  83.     glPushMatrix ();
  84.     glTranslatef (-0.75, -0.5, 0.0); 
  85.     glRotatef (270.0, 1.0, 0.0, 0.0);
  86.     glutSolidCone (1.0, 2.0, 20, 20);
  87.     glPopMatrix ();
  88.  
  89.     glPushMatrix ();
  90.     glTranslatef (0.75, 0.0, -1.0); 
  91.     glutSolidSphere (1.0, 20, 20);
  92.     glPopMatrix ();
  93.  
  94.     glPopMatrix ();
  95.     glFlush();
  96. }
  97.  
  98. void myReshape(int w, int h)
  99. {
  100.     glViewport(0, 0, w, h);
  101.     glMatrixMode(GL_PROJECTION);
  102.     glLoadIdentity();
  103.     if (w <= h) 
  104.     glOrtho (-2.5, 2.5, -2.5*(GLfloat)h/(GLfloat)w, 
  105.         2.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
  106.     else 
  107.     glOrtho (-2.5*(GLfloat)w/(GLfloat)h, 
  108.         2.5*(GLfloat)w/(GLfloat)h, -2.5, 2.5, -10.0, 10.0);
  109.     glMatrixMode(GL_MODELVIEW);
  110. }
  111.  
  112. static void
  113. key(unsigned char k, int x, int y)
  114. {
  115.   switch (k) {
  116.   case 27:  /* Escape */
  117.     exit(0);
  118.     break;
  119.   default:
  120.     return;
  121.   }
  122.   glutPostRedisplay();
  123. }
  124.  
  125. /*  Main Loop
  126.  *  Open window with initial window size, title bar, 
  127.  *  RGBA display mode, and handle input events.
  128.  */
  129. int main(int argc, char** argv)
  130. {
  131.     glutInit(&argc, argv);
  132.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  133.     glutInitWindowSize (500, 500);
  134.     glutCreateWindow (argv[0]);
  135.     myinit();
  136.     glutReshapeFunc (myReshape);
  137.     glutDisplayFunc(display);
  138.     glutKeyboardFunc(key);
  139.     glutMainLoop();
  140.     return 0;             /* ANSI C requires main to return int. */
  141. }
  142.