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

  1. /*
  2.  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. /*
  39.  *  polyoff.c
  40.  *  This program demonstrates polygon offset to draw a shaded
  41.  *  polygon and its wireframe counterpart without ugly visual
  42.  *  artifacts ("stitching").
  43.  */
  44. #include <GL/glut.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47.  
  48. #ifdef GL_VERSION_1_1
  49. GLuint list;
  50. GLint spinx = 0;
  51. GLint spiny = 0;
  52. GLfloat tdist = 0.0;
  53. GLfloat polyfactor = 1.0;
  54. GLfloat polyunits = 1.0;
  55.  
  56. /*  display() draws two spheres, one with a gray, diffuse material,
  57.  *  the other sphere with a magenta material with a specular highlight.
  58.  */
  59. void display (void)
  60. {
  61.     GLfloat gray[] = { 0.8, 0.8, 0.8, 1.0 };
  62.     GLfloat black[] = { 0.0, 0.0, 0.0, 1.0 };
  63.  
  64.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  65.     glPushMatrix ();
  66.     glTranslatef (0.0, 0.0, tdist);
  67.     glRotatef ((GLfloat) spinx, 1.0, 0.0, 0.0);
  68.     glRotatef ((GLfloat) spiny, 0.0, 1.0, 0.0);
  69.  
  70.     glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
  71.     glMaterialfv(GL_FRONT, GL_SPECULAR, black);
  72.     glMaterialf(GL_FRONT, GL_SHININESS, 0.0);
  73.     glEnable(GL_LIGHTING);
  74.     glEnable(GL_LIGHT0);
  75.     glEnable(GL_POLYGON_OFFSET_FILL);
  76.     glPolygonOffset(polyfactor, polyunits);
  77.     glCallList (list);
  78.     glDisable(GL_POLYGON_OFFSET_FILL);
  79.  
  80.     glDisable(GL_LIGHTING);
  81.     glDisable(GL_LIGHT0);
  82.     glColor3f (1.0, 1.0, 1.0);
  83.     glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  84.     glCallList (list);
  85.     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  86.  
  87.     glPopMatrix ();
  88.     glFlush ();
  89. }
  90.  
  91. /*  specify initial properties
  92.  *  create display list with sphere  
  93.  *  initialize lighting and depth buffer
  94.  */
  95. void gfxinit (void)
  96. {
  97.     GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  98.     GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  99.     GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  100.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  101.  
  102.     GLfloat global_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  103.  
  104.     glClearColor (0.0, 0.0, 0.0, 1.0);
  105.  
  106.     list = glGenLists(1);
  107.     glNewList (list, GL_COMPILE);
  108.        glutSolidSphere(1.0, 20, 12);
  109.     glEndList ();
  110.  
  111.     glEnable(GL_DEPTH_TEST);
  112.  
  113.     glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
  114.     glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  115.     glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
  116.     glLightfv (GL_LIGHT0, GL_POSITION, light_position);
  117.     glLightModelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient);
  118. }
  119.  
  120. /*  call when window is resized  */
  121. void reshape(int width, int height)
  122. {
  123.     glViewport (0, 0, width, height);
  124.     glMatrixMode (GL_PROJECTION);
  125.     glLoadIdentity ();
  126.     gluPerspective(45.0, (GLdouble)width/(GLdouble)height,
  127.         1.0, 10.0);
  128.     glMatrixMode (GL_MODELVIEW);
  129.     glLoadIdentity ();
  130.     gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  131. }
  132.  
  133. /*  call when mouse button is pressed  */
  134. /* ARGSUSED2 */
  135. void mouse(int button, int state, int x, int y) {
  136.     switch (button) {
  137.     case GLUT_LEFT_BUTTON:
  138.         switch (state) {
  139.         case GLUT_DOWN:
  140.             spinx = (spinx + 5) % 360; 
  141.                     glutPostRedisplay();
  142.             break;
  143.         default:
  144.             break;
  145.             }
  146.             break;
  147.     case GLUT_MIDDLE_BUTTON:
  148.         switch (state) {
  149.         case GLUT_DOWN:
  150.             spiny = (spiny + 5) % 360; 
  151.                     glutPostRedisplay();
  152.             break;
  153.         default:
  154.             break;
  155.             }
  156.             break;
  157.     case GLUT_RIGHT_BUTTON:
  158.         switch (state) {
  159.         case GLUT_UP:
  160.             exit(0);
  161.             break;
  162.         default:
  163.             break;
  164.             }
  165.             break;
  166.         default:
  167.             break;
  168.     }
  169. }
  170.  
  171. /* ARGSUSED1 */
  172. void keyboard (unsigned char key, int x, int y)
  173. {
  174.    switch (key) {
  175.       case 't':
  176.          if (tdist < 4.0) {
  177.             tdist = (tdist + 0.5);
  178.             glutPostRedisplay();
  179.          }
  180.          break;
  181.       case 'T':
  182.          if (tdist > -5.0) {
  183.             tdist = (tdist - 0.5);
  184.             glutPostRedisplay();
  185.          }
  186.          break;
  187.       case 'F':
  188.          polyfactor = polyfactor + 0.1;
  189.      printf ("polyfactor is %f\n", polyfactor);
  190.          glutPostRedisplay();
  191.          break;
  192.       case 'f':
  193.          polyfactor = polyfactor - 0.1;
  194.      printf ("polyfactor is %f\n", polyfactor);
  195.          glutPostRedisplay();
  196.          break;
  197.       case 'U':
  198.          polyunits = polyunits + 1.0;
  199.      printf ("polyunits is %f\n", polyunits);
  200.          glutPostRedisplay();
  201.          break;
  202.       case 'u':
  203.          polyunits = polyunits - 1.0;
  204.      printf ("polyunits is %f\n", polyunits);
  205.          glutPostRedisplay();
  206.          break;
  207.       default:
  208.          break;
  209.    }
  210. }
  211.  
  212. static void
  213. key(unsigned char k, int x, int y)
  214. {
  215.   switch (k) {
  216.   case 27:  /* Escape */
  217.     exit(0);
  218.     break;
  219.   default:
  220.     return;
  221.   }
  222.   glutPostRedisplay();
  223. }
  224.  
  225. /*  Main Loop
  226.  *  Open window with initial window size, title bar, 
  227.  *  RGBA display mode, and handle input events.
  228.  */
  229. int main(int argc, char** argv)
  230. {
  231.     glutInit(&argc, argv);
  232.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  233.     glutCreateWindow(argv[0]);
  234.     glutReshapeFunc(reshape);
  235.     glutDisplayFunc(display);
  236.     glutMouseFunc(mouse);
  237.     glutKeyboardFunc(keyboard);
  238.     gfxinit();
  239.     glutKeyboardFunc(key);
  240.     glutMainLoop();
  241.     return 0;
  242. }
  243. #else
  244. int main(int argc, char** argv)
  245. {
  246.     fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");
  247.     fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");
  248.     fprintf (stderr, "you may be able to modify this program to make it run.\n");
  249.     return 0;
  250. }
  251. #endif
  252.