home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / Mesa-3.1 / Mesa-aux / book.aux / stencil.c < prev    next >
C/C++ Source or Header  |  1999-06-25  |  5KB  |  179 lines

  1. /*
  2.  * (c) Copyright 1993, 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(TM) is a trademark of Silicon Graphics, Inc.
  36.  */
  37. /*
  38.  * stencil.c
  39.  * *  This program draws two rotated tori in a window.  
  40.  * *  A diamond in the center of the window masks out part 
  41.  * *  of the scene.  Within this mask, a different model 
  42.  * *  (a sphere) is drawn in a different color.
  43.  */
  44. #include <GL/gl.h>
  45. #include <GL/glu.h>
  46. #include <stdlib.h>
  47. #include <GL/glaux.h>
  48.  
  49. #define YELLOWMAT   1
  50. #define BLUEMAT 2
  51.  
  52. void myinit(void)
  53. {
  54.   GLfloat yellow_diffuse[] =
  55.   {0.7, 0.7, 0.0, 1.0};
  56.   GLfloat yellow_specular[] =
  57.   {1.0, 1.0, 1.0, 1.0};
  58.  
  59.   GLfloat blue_diffuse[] =
  60.   {0.1, 0.1, 0.7, 1.0};
  61.   GLfloat blue_specular[] =
  62.   {0.1, 1.0, 1.0, 1.0};
  63.  
  64.   GLfloat position_one[] =
  65.   {1.0, 1.0, 1.0, 0.0};
  66.  
  67.   glNewList(YELLOWMAT, GL_COMPILE);
  68.   glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
  69.   glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
  70.   glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
  71.   glEndList();
  72.  
  73.   glNewList(BLUEMAT, GL_COMPILE);
  74.   glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
  75.   glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
  76.   glMaterialf(GL_FRONT, GL_SHININESS, 45.0);
  77.   glEndList();
  78.  
  79.   glLightfv(GL_LIGHT0, GL_POSITION, position_one);
  80.  
  81.   glEnable(GL_LIGHT0);
  82.   glEnable(GL_LIGHTING);
  83.   glDepthFunc(GL_LESS);
  84.   glEnable(GL_DEPTH_TEST);
  85.  
  86.   glClearStencil(0x0);
  87.   glEnable(GL_STENCIL_TEST);
  88.  
  89. }
  90.  
  91. /*
  92.  * Draw a sphere in a diamond-shaped section in the
  93.  * *  middle of a window with 2 tori.
  94.  */
  95. void display(void)
  96. {
  97.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  98.  
  99. /*
  100.  * draw blue sphere where the stencil is 1 
  101.  */
  102.   glStencilFunc(GL_EQUAL, 0x1, 0x1);
  103.   glCallList(BLUEMAT);
  104.   auxSolidSphere(0.5);
  105.  
  106. /*
  107.  * draw the tori where the stencil is not 1 
  108.  */
  109.   glStencilFunc(GL_NOTEQUAL, 0x1, 0x1);
  110.   glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  111.   glPushMatrix();
  112.   glRotatef(45.0, 0.0, 0.0, 1.0);
  113.   glRotatef(45.0, 0.0, 1.0, 0.0);
  114.   glCallList(YELLOWMAT);
  115.   auxSolidTorus(0.275, 0.85);
  116.   glPushMatrix();
  117.   glRotatef(90.0, 1.0, 0.0, 0.0);
  118.   auxSolidTorus(0.275, 0.85);
  119.   glPopMatrix();
  120.   glPopMatrix();
  121.  
  122. }
  123.  
  124. /*
  125.  * Whenever the window is reshaped, redefine the 
  126.  * *  coordinate system and redraw the stencil area.
  127.  */
  128. void myReshape(int w, int h)
  129. {
  130.   glViewport(0, 0, w, h);
  131.  
  132.   glClear(GL_STENCIL_BUFFER_BIT);
  133. /*
  134.  * create a diamond shaped stencil area 
  135.  */
  136.   glMatrixMode(GL_PROJECTION);
  137.   glLoadIdentity();
  138.   glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
  139.   glMatrixMode(GL_MODELVIEW);
  140.   glLoadIdentity();
  141.  
  142.   glStencilFunc(GL_ALWAYS, 0x1, 0x1);
  143.   glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
  144.  
  145.   glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  146.   glBegin(GL_QUADS);
  147.   glVertex3f(-1.0, 0.0, 0.0);
  148.   glVertex3f(0.0, 1.0, 0.0);
  149.   glVertex3f(1.0, 0.0, 0.0);
  150.   glVertex3f(0.0, -1.0, 0.0);
  151.   glEnd();
  152.   glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  153.  
  154.   glMatrixMode(GL_PROJECTION);
  155.   glLoadIdentity();
  156.   gluPerspective(45.0, (GLfloat) w / (GLfloat) h, 3.0, 7.0);
  157.   glMatrixMode(GL_MODELVIEW);
  158.   glLoadIdentity();
  159.   glTranslatef(0.0, 0.0, -5.0);
  160. }
  161.  
  162. /*
  163.  * Main Loop
  164.  * *  Open window with initial window size, title bar, 
  165.  * *  RGBA display mode, and handle input events.
  166.  */
  167. int main(int argc, char **argv)
  168. {
  169.   auxInitDisplayMode(AUX_SINGLE | AUX_RGB
  170.              | AUX_DEPTH | AUX_STENCIL);
  171.   auxInitPosition(0, 0, 400, 400);
  172.   if (!auxInitWindow(argv[0]))
  173.     auxQuit();
  174.   myinit();
  175.   auxReshapeFunc(myReshape);
  176.   auxMainLoop(display);
  177.   return 0;
  178. }
  179.