1
2 /* Copyright (c) Mark J. Kilgard, 1994. */
3
4 /*
5 * (c) Copyright 1993, Silicon Graphics, Inc.
6 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
8 * any purpose and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both the copyright notice
10 * and this permission notice appear in supporting documentation, and that
11 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
13 * written prior permission.
14 *
15 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
16 * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
18 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SILICON
19 * GRAPHICS, INC. BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
20 * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
21 * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
22 * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
23 * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC. HAS BEEN
24 * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
26 * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
27 *
28 * US Government Users Restricted Rights
29 * Use, duplication, or disclosure by the Government is subject to
30 * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
31 * (c)(1)(ii) of the Rights in Technical Data and Computer Software
32 * clause at DFARS 252.227-7013 and/or in similar or successor
33 * clauses in the FAR or the DOD or NASA FAR Supplement.
34 * Unpublished-- rights reserved under the copyright laws of the
35 * United States. Contractor/manufacturer is Silicon Graphics,
36 * Inc., 2011 N. Shoreline Blvd., Mountain View, CA 94039-7311.
37 *
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
39 */
40 /* stencil.c
41 * This program draws two rotated tori in a window.
42 * A diamond in the center of the window masks out part
43 * of the scene. Within this mask, a different model
44 * (a sphere) is drawn in a different color.
45 */
46
47 /*
48 * !!! NOTE !!!
49 *
50 * This demo is poorly written. The stencil buffer should be
51 * redrawn in display(), not in the myReshape() function.
52 * The reason is if the window gets "damaged" then the stencil buffer
53 * contents will be in an undefined state (myReshape is not called when
54 * a window is damaged and needs to be redrawn). If the stencil buffer
55 * contents are undefined, the results of display() are unpredictable.
56 *
57 * -Brian
58 */
59
60
61 #include <stdlib.h>
62 #include "glut_wrap.h"
63
64 #define YELLOWMAT 1
65 #define BLUEMAT 2
66
myinit(void)67 static void myinit (void)
68 {
69 GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
70 GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 };
71
72 GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 };
73 GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 };
74
75 GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 };
76
77 glNewList(YELLOWMAT, GL_COMPILE);
78 glMaterialfv(GL_FRONT, GL_DIFFUSE, yellow_diffuse);
79 glMaterialfv(GL_FRONT, GL_SPECULAR, yellow_specular);
80 glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
81 glEndList();
82
83 glNewList(BLUEMAT, GL_COMPILE);
84 glMaterialfv(GL_FRONT, GL_DIFFUSE, blue_diffuse);
85 glMaterialfv(GL_FRONT, GL_SPECULAR, blue_specular);
86 glMaterialf(GL_FRONT, GL_SHININESS, 45.0);
87 glEndList();
88
89 glLightfv(GL_LIGHT0, GL_POSITION, position_one);
90
91 glEnable(GL_LIGHT0);
92 glEnable(GL_LIGHTING);
93 glDepthFunc(GL_LESS);
94 glEnable(GL_DEPTH_TEST);
95
96 glClearStencil(0x0);
97 glEnable(GL_STENCIL_TEST);
98
99 }
100
101 /* Draw a sphere in a diamond-shaped section in the
102 * middle of a window with 2 tori.
103 */
display(void)104 static void display(void)
105 {
106 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
107
108 glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
109
110 /* draw blue sphere where the stencil is 1 */
111 glStencilFunc (GL_EQUAL, 0x1, 0x1);
112 glCallList (BLUEMAT);
113 glutSolidSphere (0.5, 15, 15);
114
115 /* draw the tori where the stencil is not 1 */
116 glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
117 glPushMatrix();
118 glRotatef (45.0, 0.0, 0.0, 1.0);
119 glRotatef (45.0, 0.0, 1.0, 0.0);
120 glCallList (YELLOWMAT);
121 glutSolidTorus (0.275, 0.85, 15, 15);
122 glPushMatrix();
123 glRotatef (90.0, 1.0, 0.0, 0.0);
124 glutSolidTorus (0.275, 0.85, 15, 15);
125 glPopMatrix();
126 glPopMatrix();
127
128 glFlush();
129 glutSwapBuffers();
130 }
131
132 /* Whenever the window is reshaped, redefine the
133 * coordinate system and redraw the stencil area.
134 */
myReshape(int w,int h)135 static void myReshape(int w, int h)
136 {
137 glViewport(0, 0, w, h);
138
139 glClear(GL_STENCIL_BUFFER_BIT);
140 /* create a diamond shaped stencil area */
141 glMatrixMode(GL_PROJECTION);
142 glLoadIdentity();
143 glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
144 glMatrixMode(GL_MODELVIEW);
145 glLoadIdentity();
146
147 glStencilFunc (GL_ALWAYS, 0x1, 0x1);
148 glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
149 glBegin(GL_QUADS);
150 glVertex3f (-1.0, 0.0, 0.0);
151 glVertex3f (0.0, 1.0, 0.0);
152 glVertex3f (1.0, 0.0, 0.0);
153 glVertex3f (0.0, -1.0, 0.0);
154 glEnd();
155
156 glMatrixMode(GL_PROJECTION);
157 glLoadIdentity();
158 gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);
159 glMatrixMode(GL_MODELVIEW);
160 glLoadIdentity();
161 glTranslatef(0.0, 0.0, -5.0);
162 }
163
164 static void
key(unsigned char k,int x,int y)165 key(unsigned char k, int x, int y)
166 {
167 switch (k) {
168 case 27: /* Escape */
169 exit(0);
170 break;
171 default:
172 return;
173 }
174 glutPostRedisplay();
175 }
176
177 /* Main Loop
178 * Open window with initial window size, title bar,
179 * RGBA display mode, and handle input events.
180 */
main(int argc,char ** argv)181 int main(int argc, char** argv)
182 {
183 glutInit(&argc, argv);
184 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
185 glutInitWindowSize (400, 400);
186 glutCreateWindow (argv[0]);
187 myinit ();
188 glutReshapeFunc (myReshape);
189 glutDisplayFunc(display);
190 glutKeyboardFunc(key);
191 glutMainLoop();
192 return 0; /* ANSI C requires main to return int. */
193 }
194