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 <stdlib.h>
27#include <string.h>
28#include <math.h>
29#include "glut_wrap.h"
30
31
32static void Init(void)
33{
34}
35
36static void Reshape(int width, int height)
37{
38
39    glViewport(0, 0, (GLint)width, (GLint)height);
40
41    glMatrixMode(GL_PROJECTION);
42    glLoadIdentity();
43    glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
44    glMatrixMode(GL_MODELVIEW);
45}
46
47static void Key(unsigned char key, int x, int y)
48{
49
50    switch (key) {
51      case 27:
52        printf("Exiting...\n");
53	exit(1);
54      case 'r':
55        printf("Redisplaying...\n");
56        glutPostRedisplay();
57        break;
58      default:
59        printf("No such key '%c'...\n", key);
60        break;
61    }
62}
63
64static void Draw(void)
65{
66   glShadeModel(GL_FLAT);
67
68   {
69      glClearColor(0.0, 0.0, 0.0, 0.0);
70      glClearStencil(0);
71      glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
72   }
73
74
75   glStencilMask(1);
76   glEnable(GL_STENCIL_TEST);
77   glStencilFunc(GL_ALWAYS, 1, 1);
78   glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
79
80   /* red triangle (setting stencil to 1) */
81   glColor3ub(200, 0, 0);
82   glBegin(GL_POLYGON);
83   glVertex3i(-4, -4, 0);
84   glVertex3i( 4, -4, 0);
85   glVertex3i( 0,  4, 0);
86   glEnd();
87
88#if 1
89   glStencilFunc(GL_EQUAL, 1, 1);
90   glStencilOp(GL_INCR, GL_KEEP, GL_DECR);
91
92   /* green quad (if over red, decr stencil to 0, else incr to 1) */
93   glColor3ub(0, 200, 0);
94   glBegin(GL_POLYGON);
95   glVertex3i(3, 3, 0);
96   glVertex3i(-3, 3, 0);
97   glVertex3i(-3, -3, 0);
98   glVertex3i(3, -3, 0);
99   glEnd();
100#endif
101
102#if 1
103   glStencilFunc(GL_EQUAL, 1, 1);
104   glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
105
106   /* blue quad (where stencil == 1) */
107   glColor3ub(0, 0, 200);
108   glBegin(GL_POLYGON);
109   glVertex3f(2.5, 2.5, 0);
110   glVertex3f(-2.5, 2.5, 0);
111   glVertex3f(-2.5, -2.5, 0);
112   glVertex3f(2.5, -2.5, 0);
113   glEnd();
114#endif
115
116   glFlush();
117}
118
119static GLenum Args(int argc, char **argv)
120{
121    GLint i;
122
123
124    for (i = 1; i < argc; i++) {
125	if (strcmp(argv[i], "-dr") == 0) {
126	} else {
127	    printf("%s (Bad option).\n", argv[i]);
128	    return GL_FALSE;
129	}
130    }
131    return GL_TRUE;
132}
133
134int main(int argc, char **argv)
135{
136    GLenum type;
137
138    glutInit(&argc, argv);
139
140    if (Args(argc, argv) == GL_FALSE) {
141	exit(1);
142    }
143
144    glutInitWindowPosition(0, 0);
145    glutInitWindowSize( 300, 300);
146
147    type = GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH | GLUT_STENCIL;
148    glutInitDisplayMode(type);
149
150    if (glutCreateWindow(*argv) == GL_FALSE) {
151	exit(1);
152    }
153
154    Init();
155
156    glutReshapeFunc(Reshape);
157    glutKeyboardFunc(Key);
158    glutDisplayFunc(Draw);
159    glutMainLoop();
160	return 0;
161}
162