Merge branch '7.8'
[mesa.git] / progs / tests / stencil_wrap.c
1 /*
2 * (C) Copyright IBM Corporation 2004
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEM, IBM AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file stencil_wrap.c
27 *
28 * Simple test of GL_EXT_stencil_wrap functionality. Four squares are drawn
29 * with different stencil modes, but all should be rendered with the same
30 * final color.
31 *
32 * \author Ian Romanick <idr@us.ibm.com>
33 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <GL/glew.h>
38 #include <GL/glut.h>
39
40 static int Width = 550;
41 static int Height = 200;
42 static const GLfloat Near = 5.0, Far = 25.0;
43
44
45 static void Display( void )
46 {
47 GLint max_stencil;
48 GLint stencil_bits;
49 unsigned i;
50
51
52 glGetIntegerv( GL_STENCIL_BITS, & stencil_bits );
53 max_stencil = (1U << stencil_bits) - 1;
54 printf( "Stencil bits = %u, maximum stencil value = 0x%08x\n",
55 stencil_bits, max_stencil );
56
57 glClearStencil( 0 );
58 glClearColor( 0.2, 0.2, 0.8, 0 );
59 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT
60 | GL_STENCIL_BUFFER_BIT );
61
62
63 glPushMatrix();
64
65 /* This is the "reference" square.
66 */
67
68 glDisable(GL_STENCIL_TEST);
69 glTranslatef(-6.0, 0, 0);
70 glBegin(GL_QUADS);
71 glColor3f( 0.5, 0.5, 0.5 );
72 glVertex2f(-1, -1);
73 glVertex2f( 1, -1);
74 glVertex2f( 1, 1);
75 glVertex2f(-1, 1);
76 glEnd();
77
78
79 glEnable(GL_STENCIL_TEST);
80
81 /* Draw the first two squares using the two non-wrap (i.e., saturate)
82 * modes.
83 */
84
85 glStencilFunc(GL_ALWAYS, 0, ~0);
86 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
87
88 glTranslatef(3.0, 0, 0);
89 glBegin(GL_QUADS);
90 glColor3f( 0.9, 0.9, 0.9 );
91
92 for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
93 glVertex2f(-1, -1);
94 glVertex2f( 1, -1);
95 glVertex2f( 1, 1);
96 glVertex2f(-1, 1);
97 }
98 glEnd();
99
100 glStencilFunc(GL_EQUAL, max_stencil, ~0);
101 glBegin(GL_QUADS);
102 glColor3f( 0.5, 0.5, 0.5 );
103 glVertex2f(-1, -1);
104 glVertex2f( 1, -1);
105 glVertex2f( 1, 1);
106 glVertex2f(-1, 1);
107 glEnd();
108
109
110 glStencilFunc(GL_ALWAYS, 0, ~0);
111 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
112
113 glTranslatef(3.0, 0, 0);
114 glBegin(GL_QUADS);
115 glColor3f( 0.9, 0.9, 0.9 );
116
117 for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
118 glVertex2f(-1, -1);
119 glVertex2f( 1, -1);
120 glVertex2f( 1, 1);
121 glVertex2f(-1, 1);
122 }
123 glEnd();
124
125 glStencilFunc(GL_EQUAL, 0, ~0);
126 glBegin(GL_QUADS);
127 glColor3f( 0.5, 0.5, 0.5 );
128 glVertex2f(-1, -1);
129 glVertex2f( 1, -1);
130 glVertex2f( 1, 1);
131 glVertex2f(-1, 1);
132 glEnd();
133
134
135
136
137 /* Draw the last two squares using the two wrap modes.
138 */
139
140 glStencilFunc(GL_ALWAYS, 0, ~0);
141 glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP);
142
143 glTranslatef(3.0, 0, 0);
144 glBegin(GL_QUADS);
145 glColor3f( 0.9, 0.9, 0.9 );
146
147 for ( i = 0 ; i < (max_stencil + 5) ; i++ ) {
148 glVertex2f(-1, -1);
149 glVertex2f( 1, -1);
150 glVertex2f( 1, 1);
151 glVertex2f(-1, 1);
152 }
153 glEnd();
154
155 glStencilFunc(GL_EQUAL, 4, ~0);
156 glBegin(GL_QUADS);
157 glColor3f( 0.5, 0.5, 0.5 );
158 glVertex2f(-1, -1);
159 glVertex2f( 1, -1);
160 glVertex2f( 1, 1);
161 glVertex2f(-1, 1);
162 glEnd();
163
164
165
166 glStencilFunc(GL_ALWAYS, 0, ~0);
167 glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP);
168
169 glTranslatef(3.0, 0, 0);
170 glBegin(GL_QUADS);
171 glColor3f( 0.9, 0.9, 0.9 );
172
173 for ( i = 0 ; i < 5 ; i++ ) {
174 glVertex2f(-1, -1);
175 glVertex2f( 1, -1);
176 glVertex2f( 1, 1);
177 glVertex2f(-1, 1);
178 }
179 glEnd();
180
181 glStencilFunc(GL_EQUAL, (max_stencil - 4), ~0);
182 glBegin(GL_QUADS);
183 glColor3f( 0.5, 0.5, 0.5 );
184 glVertex2f(-1, -1);
185 glVertex2f( 1, -1);
186 glVertex2f( 1, 1);
187 glVertex2f(-1, 1);
188 glEnd();
189
190
191
192 glPopMatrix();
193
194 glutSwapBuffers();
195 }
196
197
198 static void Reshape( int width, int height )
199 {
200 GLfloat ar = (float) width / (float) height;
201 Width = width;
202 Height = height;
203 glViewport( 0, 0, width, height );
204 glMatrixMode( GL_PROJECTION );
205 glLoadIdentity();
206 glFrustum( -ar, ar, -1.0, 1.0, Near, Far );
207 glMatrixMode( GL_MODELVIEW );
208 glLoadIdentity();
209 glTranslatef( 0.0, 0.0, -15.0 );
210 }
211
212
213 static void Key( unsigned char key, int x, int y )
214 {
215 (void) x;
216 (void) y;
217 switch (key) {
218 case 27:
219 exit(0);
220 break;
221 }
222 glutPostRedisplay();
223 }
224
225
226 static void Init( void )
227 {
228 const char * const ver_string = (const char * const)
229 glGetString( GL_VERSION );
230
231 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
232 printf("GL_VERSION = %s\n", ver_string);
233
234 if ( !glutExtensionSupported("GL_EXT_stencil_wrap")
235 && (atof( ver_string ) < 1.4) ) {
236 printf("Sorry, this program requires either GL_EXT_stencil_wrap or OpenGL 1.4.\n");
237 exit(1);
238 }
239
240 printf("\nAll 5 squares should be the same color.\n");
241 glEnable( GL_BLEND );
242 }
243
244
245 int main( int argc, char *argv[] )
246 {
247 glutInit( &argc, argv );
248 glutInitWindowPosition( 0, 0 );
249 glutInitWindowSize( Width, Height );
250 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL );
251 glutCreateWindow( "GL_EXT_stencil_wrap test" );
252 glewInit();
253 glutReshapeFunc( Reshape );
254 glutKeyboardFunc( Key );
255 glutDisplayFunc( Display );
256 Init();
257 glutMainLoop();
258 return 0;
259 }