blob: b33e40a03072be00d3139aed63766c9ac320060b [file] [log] [blame]
jtgafb833d1999-08-19 00:55:39 +00001
2/* Copyright (c) Mark J. Kilgard, 1994. */
3
4/*
5 * (c) Copyright 1993, Silicon Graphics, Inc.
Ted Jump83c02ef1999-09-17 02:40:51 +00006 * ALL RIGHTS RESERVED
7 * Permission to use, copy, modify, and distribute this software for
jtgafb833d1999-08-19 00:55:39 +00008 * 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
Ted Jump83c02ef1999-09-17 02:40:51 +000010 * and this permission notice appear in supporting documentation, and that
jtgafb833d1999-08-19 00:55:39 +000011 * the name of Silicon Graphics, Inc. not be used in advertising
12 * or publicity pertaining to distribution of the software without specific,
Ted Jump83c02ef1999-09-17 02:40:51 +000013 * written prior permission.
jtgafb833d1999-08-19 00:55:39 +000014 *
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.
Ted Jump83c02ef1999-09-17 02:40:51 +000027 *
28 * US Government Users Restricted Rights
jtgafb833d1999-08-19 00:55:39 +000029 * 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
Ted Jump83c02ef1999-09-17 02:40:51 +000041 * 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
jtgafb833d1999-08-19 00:55:39 +000044 * (a sphere) is drawn in a different color.
45 */
Brian Paulfda3b1a2000-10-31 20:41:06 +000046
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
jtgafb833d1999-08-19 00:55:39 +000061#include <stdlib.h>
62#include <GL/glut.h>
63
64#define YELLOWMAT 1
65#define BLUEMAT 2
66
Ted Jump83c02ef1999-09-17 02:40:51 +000067void myinit (void)
jtgafb833d1999-08-19 00:55:39 +000068{
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 */
104void display(void)
105{
106 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
107
Brian Paulfda3b1a2000-10-31 20:41:06 +0000108 glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
109
jtgafb833d1999-08-19 00:55:39 +0000110/* 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);
jtgafb833d1999-08-19 00:55:39 +0000117 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();
Brian Paulfda3b1a2000-10-31 20:41:06 +0000129 glutSwapBuffers();
jtgafb833d1999-08-19 00:55:39 +0000130}
131
Ted Jump83c02ef1999-09-17 02:40:51 +0000132/* Whenever the window is reshaped, redefine the
jtgafb833d1999-08-19 00:55:39 +0000133 * coordinate system and redraw the stencil area.
134 */
135void 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
Ted Jump83c02ef1999-09-17 02:40:51 +0000164static void
165key(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
jtgafb833d1999-08-19 00:55:39 +0000177/* Main Loop
Ted Jump83c02ef1999-09-17 02:40:51 +0000178 * Open window with initial window size, title bar,
jtgafb833d1999-08-19 00:55:39 +0000179 * RGBA display mode, and handle input events.
180 */
181int main(int argc, char** argv)
182{
183 glutInit(&argc, argv);
Brian Paulfda3b1a2000-10-31 20:41:06 +0000184 glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
jtgafb833d1999-08-19 00:55:39 +0000185 glutInitWindowSize (400, 400);
186 glutCreateWindow (argv[0]);
187 myinit ();
188 glutReshapeFunc (myReshape);
189 glutDisplayFunc(display);
Ted Jump83c02ef1999-09-17 02:40:51 +0000190 glutKeyboardFunc(key);
jtgafb833d1999-08-19 00:55:39 +0000191 glutMainLoop();
192 return 0; /* ANSI C requires main to return int. */
193}