home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 June / SGI IRIX Patches 1995 Jun.iso / 5.3_patches / patchSG0000154 / patchSG0000154.idb / usr / share / src / OpenGL / examples / stencil.c.z / stencil.c
Encoding:
C/C++ Source or Header  |  1995-06-12  |  5.1 KB  |  159 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. /*  stencil.c
  38.  *  This program draws two rotated tori in a window.  
  39.  *  A diamond in the center of the window masks out part 
  40.  *  of the scene.  Within this mask, a different model 
  41.  *  (a sphere) is drawn in a different color.
  42.  */
  43. #include <GL/gl.h>
  44. #include <GL/glu.h>
  45. #include "aux.h"
  46.  
  47. #define YELLOWMAT   1
  48. #define BLUEMAT 2
  49.  
  50. void myinit (void) 
  51. {
  52.     GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
  53.     GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  54.  
  55.     GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 };
  56.     GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 };
  57.  
  58.     GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 };
  59.  
  60.     glNewList(YELLOWMAT, GL_COMPILE);
  61.     glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
  62.     glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
  63.     glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
  64.     glEndList();
  65.  
  66.     glNewList(BLUEMAT, GL_COMPILE);
  67.     glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
  68.     glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
  69.     glMaterialf(GL_FRONT, GL_SHININESS, 45.0);
  70.     glEndList();
  71.  
  72.     glLightfv(GL_LIGHT0, GL_POSITION, position_one);
  73.  
  74.     glEnable(GL_LIGHT0);
  75.     glEnable(GL_LIGHTING);
  76.     glDepthFunc(GL_LESS);
  77.     glEnable(GL_DEPTH_TEST);
  78.  
  79.     glClearStencil(0x0);
  80.     glEnable(GL_STENCIL_TEST);
  81.  
  82. }
  83.  
  84. /*  Draw a sphere in a diamond-shaped section in the
  85.  *  middle of a window with 2 tori.
  86.  */
  87. void display(void)
  88. {
  89.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  90.  
  91. /* draw blue sphere where the stencil is 1 */
  92.     glStencilFunc (GL_EQUAL, 0x1, 0x1);
  93.     glCallList (BLUEMAT);
  94.     auxSolidSphere (0.5);
  95.  
  96. /* draw the tori where the stencil is not 1 */
  97.     glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
  98.     glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
  99.     glPushMatrix();
  100.     glRotatef (45.0, 0.0, 0.0, 1.0);
  101.     glRotatef (45.0, 0.0, 1.0, 0.0);
  102.     glCallList (YELLOWMAT);
  103.     auxSolidTorus (0.275, 0.85);
  104.     glPushMatrix();
  105.         glRotatef (90.0, 1.0, 0.0, 0.0);
  106.         auxSolidTorus (0.275, 0.85);
  107.     glPopMatrix();
  108.     glPopMatrix();
  109.  
  110.     glFlush();
  111. }
  112.  
  113. /*  Whenever the window is reshaped, redefine the 
  114.  *  coordinate system and redraw the stencil area.
  115.  */
  116. void myReshape(int w, int h)
  117. {
  118.     glViewport(0, 0, w, h);
  119.  
  120.     glClear(GL_STENCIL_BUFFER_BIT);
  121. /* create a diamond shaped stencil area */
  122.     glMatrixMode(GL_PROJECTION);
  123.     glLoadIdentity();
  124.     glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
  125.     glMatrixMode(GL_MODELVIEW);
  126.     glLoadIdentity();
  127.  
  128.     glStencilFunc (GL_ALWAYS, 0x1, 0x1);
  129.     glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
  130.     glBegin(GL_QUADS);
  131.     glVertex3f (-1.0, 0.0, 0.0);
  132.     glVertex3f (0.0, 1.0, 0.0);
  133.     glVertex3f (1.0, 0.0, 0.0);
  134.     glVertex3f (0.0, -1.0, 0.0);
  135.     glEnd();
  136.  
  137.     glMatrixMode(GL_PROJECTION);
  138.     glLoadIdentity();
  139.     gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);
  140.     glMatrixMode(GL_MODELVIEW);
  141.     glLoadIdentity();
  142.     glTranslatef(0.0, 0.0, -5.0);
  143. }
  144.  
  145. /*  Main Loop
  146.  *  Open window with initial window size, title bar, 
  147.  *  RGBA display mode, and handle input events.
  148.  */
  149. int main(int argc, char** argv)
  150. {
  151.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB 
  152.     | AUX_DEPTH | AUX_STENCIL);
  153.     auxInitPosition (0, 0, 400, 400);
  154.     auxInitWindow (argv[0]);
  155.     myinit ();
  156.     auxReshapeFunc (myReshape);
  157.     auxMainLoop(display);
  158. }
  159.