2 /* Copyright (c) Mark J. Kilgard, 1994. */
5 * (c) Copyright 1993, Silicon Graphics, Inc.
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.
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.
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.
38 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
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.
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.
69 GLfloat yellow_diffuse
[] = { 0.7, 0.7, 0.0, 1.0 };
70 GLfloat yellow_specular
[] = { 1.0, 1.0, 1.0, 1.0 };
72 GLfloat blue_diffuse
[] = { 0.1, 0.1, 0.7, 1.0 };
73 GLfloat blue_specular
[] = { 0.1, 1.0, 1.0, 1.0 };
75 GLfloat position_one
[] = { 1.0, 1.0, 1.0, 0.0 };
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);
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);
89 glLightfv(GL_LIGHT0
, GL_POSITION
, position_one
);
92 glEnable(GL_LIGHTING
);
94 glEnable(GL_DEPTH_TEST
);
97 glEnable(GL_STENCIL_TEST
);
101 /* Draw a sphere in a diamond-shaped section in the
102 * middle of a window with 2 tori.
106 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
108 glStencilOp (GL_KEEP
, GL_KEEP
, GL_KEEP
);
110 /* draw blue sphere where the stencil is 1 */
111 glStencilFunc (GL_EQUAL
, 0x1, 0x1);
112 glCallList (BLUEMAT
);
113 glutSolidSphere (0.5, 15, 15);
115 /* draw the tori where the stencil is not 1 */
116 glStencilFunc (GL_NOTEQUAL
, 0x1, 0x1);
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);
123 glRotatef (90.0, 1.0, 0.0, 0.0);
124 glutSolidTorus (0.275, 0.85, 15, 15);
132 /* Whenever the window is reshaped, redefine the
133 * coordinate system and redraw the stencil area.
135 void myReshape(int w
, int h
)
137 glViewport(0, 0, w
, h
);
139 glClear(GL_STENCIL_BUFFER_BIT
);
140 /* create a diamond shaped stencil area */
141 glMatrixMode(GL_PROJECTION
);
143 glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
144 glMatrixMode(GL_MODELVIEW
);
147 glStencilFunc (GL_ALWAYS
, 0x1, 0x1);
148 glStencilOp (GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
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);
156 glMatrixMode(GL_PROJECTION
);
158 gluPerspective(45.0, (GLfloat
) w
/(GLfloat
) h
, 3.0, 7.0);
159 glMatrixMode(GL_MODELVIEW
);
161 glTranslatef(0.0, 0.0, -5.0);
165 key(unsigned char k
, int x
, int y
)
168 case 27: /* Escape */
178 * Open window with initial window size, title bar,
179 * RGBA display mode, and handle input events.
181 int main(int argc
, char** argv
)
183 glutInit(&argc
, argv
);
184 glutInitDisplayMode (GLUT_DOUBLE
| GLUT_RGB
| GLUT_DEPTH
| GLUT_STENCIL
);
185 glutInitWindowSize (400, 400);
186 glutCreateWindow (argv
[0]);
188 glutReshapeFunc (myReshape
);
189 glutDisplayFunc(display
);
190 glutKeyboardFunc(key
);
192 return 0; /* ANSI C requires main to return int. */