home *** CD-ROM | disk | FTP | other *** search
/ Aminet 10 / aminetcdnumber101996.iso / Aminet / gfx / x11 / Mesa_Amiwin.lha / Mesa-Amiwin / samples / stencil.c < prev    next >
C/C++ Source or Header  |  1995-10-03  |  3KB  |  150 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #include "gltk.h"
  29.  
  30.  
  31. GLenum directRender;
  32.  
  33.  
  34. static void Init(void)
  35. {
  36.  
  37.     glClearColor(0.0, 0.0, 0.0, 0.0);
  38.  
  39.     glClearStencil(0);
  40.     glStencilMask(1);
  41.     glEnable(GL_STENCIL_TEST);
  42. }
  43.  
  44. static void Reshape(int width, int height)
  45. {
  46.  
  47.     glViewport(0, 0, (GLint)width, (GLint)height);
  48.  
  49.     glMatrixMode(GL_PROJECTION);
  50.     glLoadIdentity();
  51.     glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
  52.     glMatrixMode(GL_MODELVIEW);
  53. }
  54.  
  55. static GLenum Key(int key, GLenum mask)
  56. {
  57.  
  58.     switch (key) {
  59.       case TK_ESCAPE:
  60.     tkQuit();
  61.     }
  62.     return GL_FALSE;
  63. }
  64.  
  65. static void Draw(void)
  66. {
  67.  
  68.     glClear(GL_COLOR_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  69.  
  70.     glStencilFunc(GL_ALWAYS, 1, 1);
  71.     glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
  72.  
  73.     glColor3ub(200, 0, 0);
  74.     glBegin(GL_POLYGON);        
  75.     glVertex3i(-4, -4, 0);
  76.     glVertex3i( 4, -4, 0);
  77.     glVertex3i( 0,  4, 0);
  78.     glEnd();
  79.  
  80.     glStencilFunc(GL_EQUAL, 1, 1);
  81.     glStencilOp(GL_INCR, GL_KEEP, GL_DECR);
  82.  
  83.     glColor3ub(0, 200, 0);
  84.     glBegin(GL_POLYGON);
  85.     glVertex3i(3, 3, 0);
  86.     glVertex3i(-3, 3, 0);
  87.     glVertex3i(-3, -3, 0);
  88.     glVertex3i(3, -3, 0);
  89.     glEnd();
  90.  
  91.     glStencilFunc(GL_EQUAL, 1, 1);
  92.     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  93.  
  94.     glColor3ub(0, 0, 200);
  95.     glBegin(GL_POLYGON);
  96.     glVertex3i(3, 3, 0);
  97.     glVertex3i(-3, 3, 0);
  98.     glVertex3i(-3, -3, 0);
  99.     glVertex3i(3, -3, 0);
  100.     glEnd();
  101.  
  102.     glFlush();
  103. }
  104.  
  105. static GLenum Args(int argc, char **argv)
  106. {
  107.     GLint i;
  108.  
  109.     directRender = GL_TRUE;
  110.  
  111.     for (i = 1; i < argc; i++) {
  112.     if (strcmp(argv[i], "-dr") == 0) {
  113.         directRender = GL_TRUE;
  114.     } else if (strcmp(argv[i], "-ir") == 0) {
  115.         directRender = GL_FALSE;
  116.     } else {
  117.         printf("%s (Bad option).\n", argv[i]);
  118.         return GL_FALSE;
  119.     }
  120.     }
  121.     return GL_TRUE;
  122. }
  123.  
  124. void main(int argc, char **argv)
  125. {
  126.     GLenum type;
  127.  
  128.     if (Args(argc, argv) == GL_FALSE) {
  129.     tkQuit();
  130.     }
  131.  
  132.     tkInitPosition(0, 0, 300, 300);
  133.  
  134.     type = TK_RGB | TK_SINGLE | TK_STENCIL;
  135.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  136.     tkInitDisplayMode(type);
  137.  
  138.     if (tkInitWindow("Stencil Test") == GL_FALSE) {
  139.     tkQuit();
  140.     }
  141.  
  142.     Init();
  143.  
  144.     tkExposeFunc(Reshape);
  145.     tkReshapeFunc(Reshape);
  146.     tkKeyDownFunc(Key);
  147.     tkDisplayFunc(Draw);
  148.     tkExec();
  149. }
  150.