home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / mesa-1.2.8 / book / stencil.c < prev    next >
C/C++ Source or Header  |  1996-05-27  |  5KB  |  162 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 <stdlib.h>
  46. #include "glaux.h"
  47.  
  48. #define YELLOWMAT   1
  49. #define BLUEMAT 2
  50.  
  51. void myinit (void) 
  52. {
  53.     GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
  54.     GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  55.  
  56.     GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 };
  57.     GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 };
  58.  
  59.     GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 };
  60.  
  61.     glNewList(YELLOWMAT, GL_COMPILE);
  62.     glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
  63.     glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
  64.     glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
  65.     glEndList();
  66.  
  67.     glNewList(BLUEMAT, GL_COMPILE);
  68.     glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
  69.     glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
  70.     glMaterialf(GL_FRONT, GL_SHININESS, 45.0);
  71.     glEndList();
  72.  
  73.     glLightfv(GL_LIGHT0, GL_POSITION, position_one);
  74.  
  75.     glEnable(GL_LIGHT0);
  76.     glEnable(GL_LIGHTING);
  77.     glDepthFunc(GL_LESS);
  78.     glEnable(GL_DEPTH_TEST);
  79.  
  80.     glClearStencil(0x0);
  81.     glEnable(GL_STENCIL_TEST);
  82.  
  83. }
  84.  
  85. /*  Draw a sphere in a diamond-shaped section in the
  86.  *  middle of a window with 2 tori.
  87.  */
  88. void display(void)
  89. {
  90.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  91.  
  92. /* draw blue sphere where the stencil is 1 */
  93.     glStencilFunc (GL_EQUAL, 0x1, 0x1);
  94.     glCallList (BLUEMAT);
  95.     auxSolidSphere (0.5);
  96.  
  97. /* draw the tori where the stencil is not 1 */
  98.     glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
  99.     glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
  100.     glPushMatrix();
  101.     glRotatef (45.0, 0.0, 0.0, 1.0);
  102.     glRotatef (45.0, 0.0, 1.0, 0.0);
  103.     glCallList (YELLOWMAT);
  104.     auxSolidTorus (0.275, 0.85);
  105.     glPushMatrix();
  106.         glRotatef (90.0, 1.0, 0.0, 0.0);
  107.         auxSolidTorus (0.275, 0.85);
  108.     glPopMatrix();
  109.     glPopMatrix();
  110.  
  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.  
  131.     glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE );
  132.     glBegin(GL_QUADS);
  133.     glVertex3f (-1.0, 0.0, 0.0);
  134.     glVertex3f (0.0, 1.0, 0.0);
  135.     glVertex3f (1.0, 0.0, 0.0);
  136.     glVertex3f (0.0, -1.0, 0.0);
  137.     glEnd();
  138.     glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE );
  139.  
  140.     glMatrixMode(GL_PROJECTION);
  141.     glLoadIdentity();
  142.     gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);
  143.     glMatrixMode(GL_MODELVIEW);
  144.     glLoadIdentity();
  145.     glTranslatef(0.0, 0.0, -5.0);
  146. }
  147.  
  148. /*  Main Loop
  149.  *  Open window with initial window size, title bar, 
  150.  *  RGBA display mode, and handle input events.
  151.  */
  152. int main(int argc, char** argv)
  153. {
  154.     auxInitDisplayMode (AUX_SINGLE | AUX_RGB 
  155.     | AUX_DEPTH | AUX_STENCIL);
  156.     auxInitPosition (0, 0, 400, 400);
  157.     auxInitWindow (argv[0]);
  158.     myinit ();
  159.     auxReshapeFunc (myReshape);
  160.     auxMainLoop(display);
  161. }
  162.