2 * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
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.
12 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
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
32 static void Init(void)
36 static void Reshape(int width
, int height
)
39 glViewport(0, 0, (GLint
)width
, (GLint
)height
);
41 glMatrixMode(GL_PROJECTION
);
43 glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
44 glMatrixMode(GL_MODELVIEW
);
47 static void Key(unsigned char key
, int x
, int y
)
52 printf("Exiting...\n");
55 printf("Redisplaying...\n");
59 printf("No such key '%c'...\n", key
);
64 static void Draw(void)
66 glShadeModel(GL_FLAT
);
69 glClearColor(0.0, 0.0, 0.0, 0.0);
71 glClear(GL_COLOR_BUFFER_BIT
|GL_DEPTH_BUFFER_BIT
|GL_STENCIL_BUFFER_BIT
);
76 glEnable(GL_STENCIL_TEST
);
77 glStencilFunc(GL_ALWAYS
, 1, 1);
78 glStencilOp(GL_KEEP
, GL_KEEP
, GL_REPLACE
);
80 /* red triangle (setting stencil to 1) */
81 glColor3ub(200, 0, 0);
83 glVertex3i(-4, -4, 0);
84 glVertex3i( 4, -4, 0);
89 glStencilFunc(GL_EQUAL
, 1, 1);
90 glStencilOp(GL_INCR
, GL_KEEP
, GL_DECR
);
92 /* green quad (if over red, decr stencil to 0, else incr to 1) */
93 glColor3ub(0, 200, 0);
97 glVertex3i(-3, -3, 0);
103 glStencilFunc(GL_EQUAL
, 1, 1);
104 glStencilOp(GL_KEEP
, GL_KEEP
, GL_KEEP
);
106 /* blue quad (where stencil == 1) */
107 glColor3ub(0, 0, 200);
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);
119 static GLenum
Args(int argc
, char **argv
)
124 for (i
= 1; i
< argc
; i
++) {
125 if (strcmp(argv
[i
], "-dr") == 0) {
127 printf("%s (Bad option).\n", argv
[i
]);
134 int main(int argc
, char **argv
)
138 glutInit(&argc
, argv
);
140 if (Args(argc
, argv
) == GL_FALSE
) {
144 glutInitWindowPosition(0, 0);
145 glutInitWindowSize( 300, 300);
147 type
= GLUT_RGB
| GLUT_SINGLE
| GLUT_DEPTH
| GLUT_STENCIL
;
148 glutInitDisplayMode(type
);
150 if (glutCreateWindow(*argv
) == GL_FALSE
) {
156 glutReshapeFunc(Reshape
);
157 glutKeyboardFunc(Key
);
158 glutDisplayFunc(Draw
);