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.
54 GLfloat yellow_diffuse
[] = { 0.7, 0.7, 0.0, 1.0 };
55 GLfloat yellow_specular
[] = { 1.0, 1.0, 1.0, 1.0 };
57 GLfloat blue_diffuse
[] = { 0.1, 0.1, 0.7, 1.0 };
58 GLfloat blue_specular
[] = { 0.1, 1.0, 1.0, 1.0 };
60 GLfloat position_one
[] = { 1.0, 1.0, 1.0, 0.0 };
62 glNewList(YELLOWMAT
, GL_COMPILE
);
63 glMaterialfv(GL_FRONT
, GL_DIFFUSE
, yellow_diffuse
);
64 glMaterialfv(GL_FRONT
, GL_SPECULAR
, yellow_specular
);
65 glMaterialf(GL_FRONT
, GL_SHININESS
, 64.0);
68 glNewList(BLUEMAT
, GL_COMPILE
);
69 glMaterialfv(GL_FRONT
, GL_DIFFUSE
, blue_diffuse
);
70 glMaterialfv(GL_FRONT
, GL_SPECULAR
, blue_specular
);
71 glMaterialf(GL_FRONT
, GL_SHININESS
, 45.0);
74 glLightfv(GL_LIGHT0
, GL_POSITION
, position_one
);
77 glEnable(GL_LIGHTING
);
79 glEnable(GL_DEPTH_TEST
);
82 glEnable(GL_STENCIL_TEST
);
86 /* Draw a sphere in a diamond-shaped section in the
87 * middle of a window with 2 tori.
91 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
93 /* draw blue sphere where the stencil is 1 */
94 glStencilFunc (GL_EQUAL
, 0x1, 0x1);
96 glutSolidSphere (0.5, 15, 15);
98 /* draw the tori where the stencil is not 1 */
99 glStencilFunc (GL_NOTEQUAL
, 0x1, 0x1);
100 glStencilOp (GL_KEEP
, GL_KEEP
, GL_KEEP
);
102 glRotatef (45.0, 0.0, 0.0, 1.0);
103 glRotatef (45.0, 0.0, 1.0, 0.0);
104 glCallList (YELLOWMAT
);
105 glutSolidTorus (0.275, 0.85, 15, 15);
107 glRotatef (90.0, 1.0, 0.0, 0.0);
108 glutSolidTorus (0.275, 0.85, 15, 15);
115 /* Whenever the window is reshaped, redefine the
116 * coordinate system and redraw the stencil area.
118 void myReshape(int w
, int h
)
120 glViewport(0, 0, w
, h
);
122 glClear(GL_STENCIL_BUFFER_BIT
);
123 /* create a diamond shaped stencil area */
124 glMatrixMode(GL_PROJECTION
);
126 glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
127 glMatrixMode(GL_MODELVIEW
);
130 glStencilFunc (GL_ALWAYS
, 0x1, 0x1);
131 glStencilOp (GL_REPLACE
, GL_REPLACE
, GL_REPLACE
);
133 glVertex3f (-1.0, 0.0, 0.0);
134 glVertex3f (0.0, 1.0, 0.0);
135 glVertex3f (1.0, 0.0, 0.0);
136 glVertex3f (0.0, -1.0, 0.0);
139 glMatrixMode(GL_PROJECTION
);
141 gluPerspective(45.0, (GLfloat
) w
/(GLfloat
) h
, 3.0, 7.0);
142 glMatrixMode(GL_MODELVIEW
);
144 glTranslatef(0.0, 0.0, -5.0);
148 * Open window with initial window size, title bar,
149 * RGBA display mode, and handle input events.
151 int main(int argc
, char** argv
)
153 glutInit(&argc
, argv
);
154 glutInitDisplayMode (GLUT_SINGLE
| GLUT_RGB
| GLUT_DEPTH
| GLUT_STENCIL
);
155 glutInitWindowSize (400, 400);
156 glutCreateWindow (argv
[0]);
158 glutReshapeFunc (myReshape
);
159 glutDisplayFunc(display
);
161 return 0; /* ANSI C requires main to return int. */