link with -lGLU -lGL instead of -lMesaGLU -lMesaGL
[mesa.git] / progs / redbook / stencil.c
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 #include <stdlib.h>
47 #include <GL/glut.h>
48
49 #define YELLOWMAT 1
50 #define BLUEMAT 2
51
52 void myinit (void)
53 {
54 GLfloat yellow_diffuse[] = { 0.7, 0.7, 0.0, 1.0 };
55 GLfloat yellow_specular[] = { 1.0, 1.0, 1.0, 1.0 };
56
57 GLfloat blue_diffuse[] = { 0.1, 0.1, 0.7, 1.0 };
58 GLfloat blue_specular[] = { 0.1, 1.0, 1.0, 1.0 };
59
60 GLfloat position_one[] = { 1.0, 1.0, 1.0, 0.0 };
61
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);
66 glEndList();
67
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);
72 glEndList();
73
74 glLightfv(GL_LIGHT0, GL_POSITION, position_one);
75
76 glEnable(GL_LIGHT0);
77 glEnable(GL_LIGHTING);
78 glDepthFunc(GL_LESS);
79 glEnable(GL_DEPTH_TEST);
80
81 glClearStencil(0x0);
82 glEnable(GL_STENCIL_TEST);
83
84 }
85
86 /* Draw a sphere in a diamond-shaped section in the
87 * middle of a window with 2 tori.
88 */
89 void display(void)
90 {
91 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
92
93 /* draw blue sphere where the stencil is 1 */
94 glStencilFunc (GL_EQUAL, 0x1, 0x1);
95 glCallList (BLUEMAT);
96 glutSolidSphere (0.5, 15, 15);
97
98 /* draw the tori where the stencil is not 1 */
99 glStencilFunc (GL_NOTEQUAL, 0x1, 0x1);
100 glStencilOp (GL_KEEP, GL_KEEP, GL_KEEP);
101 glPushMatrix();
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);
106 glPushMatrix();
107 glRotatef (90.0, 1.0, 0.0, 0.0);
108 glutSolidTorus (0.275, 0.85, 15, 15);
109 glPopMatrix();
110 glPopMatrix();
111
112 glFlush();
113 }
114
115 /* Whenever the window is reshaped, redefine the
116 * coordinate system and redraw the stencil area.
117 */
118 void myReshape(int w, int h)
119 {
120 glViewport(0, 0, w, h);
121
122 glClear(GL_STENCIL_BUFFER_BIT);
123 /* create a diamond shaped stencil area */
124 glMatrixMode(GL_PROJECTION);
125 glLoadIdentity();
126 glOrtho(-3.0, 3.0, -3.0, 3.0, -1.0, 1.0);
127 glMatrixMode(GL_MODELVIEW);
128 glLoadIdentity();
129
130 glStencilFunc (GL_ALWAYS, 0x1, 0x1);
131 glStencilOp (GL_REPLACE, GL_REPLACE, GL_REPLACE);
132 glBegin(GL_QUADS);
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);
137 glEnd();
138
139 glMatrixMode(GL_PROJECTION);
140 glLoadIdentity();
141 gluPerspective(45.0, (GLfloat) w/(GLfloat) h, 3.0, 7.0);
142 glMatrixMode(GL_MODELVIEW);
143 glLoadIdentity();
144 glTranslatef(0.0, 0.0, -5.0);
145 }
146
147 /* Main Loop
148 * Open window with initial window size, title bar,
149 * RGBA display mode, and handle input events.
150 */
151 int main(int argc, char** argv)
152 {
153 glutInit(&argc, argv);
154 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
155 glutInitWindowSize (400, 400);
156 glutCreateWindow (argv[0]);
157 myinit ();
158 glutReshapeFunc (myReshape);
159 glutDisplayFunc(display);
160 glutMainLoop();
161 return 0; /* ANSI C requires main to return int. */
162 }