2
// Book: OpenGL(R) ES 2.0 Programming Guide
3
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
5
// ISBN-13: 9780321502797
6
// Publisher: Addison-Wesley Professional
7
// URLs: http://safari.informit.com/9780321563835
8
// http://www.opengles-book.com
13
// This example shows various stencil buffer
21
// Handle to a program object
24
// Attribute locations
33
// Initialize the shader and program object
35
int Init ( ESContext *esContext )
37
UserData *userData = esContext->userData;
39
"attribute vec4 a_position; \n"
42
" gl_Position = a_position; \n"
46
"precision mediump float; \n"
47
"uniform vec4 u_color; \n"
50
" gl_FragColor = u_color; \n"
53
// Load the shaders and get a linked program object
54
userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
56
// Get the attribute locations
57
userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
59
// Get the sampler location
60
userData->colorLoc = glGetUniformLocation ( userData->programObject, "u_color" );
62
// Set the clear color
63
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
65
// Set the stencil clear value
66
glClearStencil ( 0x1 );
68
// Set the depth clear value
69
glClearDepthf( 0.75f );
71
// Enable the depth and stencil tests
72
glEnable( GL_DEPTH_TEST );
73
glEnable( GL_STENCIL_TEST );
79
// Initialize the stencil buffer values, and then use those
80
// values to control rendering
82
void Draw ( ESContext *esContext )
86
UserData *userData = esContext->userData;
88
GLfloat vVertices[] = {
89
-0.75f, 0.25f, 0.50f, // Quad #0
93
0.25f, 0.25f, 0.90f, // Quad #1
97
-0.75f, -0.75f, 0.50f, // Quad #2
98
-0.25f, -0.75f, 0.50f,
99
-0.25f, -0.25f, 0.50f,
100
-0.75f, -0.25f, 0.50f,
101
0.25f, -0.75f, 0.50f, // Quad #3
102
0.75f, -0.75f, 0.50f,
103
0.75f, -0.25f, 0.50f,
104
0.25f, -0.25f, 0.50f,
105
-1.00f, -1.00f, 0.00f, // Big Quad
106
1.00f, -1.00f, 0.00f,
111
GLubyte indices[][6] = {
112
{ 0, 1, 2, 0, 2, 3 }, // Quad #0
113
{ 4, 5, 6, 4, 6, 7 }, // Quad #1
114
{ 8, 9, 10, 8, 10, 11 }, // Quad #2
115
{ 12, 13, 14, 12, 14, 15 }, // Quad #3
116
{ 16, 17, 18, 16, 18, 19 } // Big Quad
120
GLfloat colors[NumTests][4] = {
121
{ 1.0f, 0.0f, 0.0f, 1.0f },
122
{ 0.0f, 1.0f, 0.0f, 1.0f },
123
{ 0.0f, 0.0f, 1.0f, 1.0f },
124
{ 1.0f, 1.0f, 0.0f, 0.0f }
127
GLint numStencilBits;
128
GLuint stencilValues[NumTests] = {
129
0x7, // Result of test 0
130
0x0, // Result of test 1
131
0x2, // Result of test 2
132
0xff // Result of test 3. We need to fill this
133
// value in a run-time
137
glViewport ( 0, 0, esContext->width, esContext->height );
139
// Clear the color, depth, and stencil buffers. At this
140
// point, the stencil buffer will be 0x1 for all pixels
141
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
143
// Use the program object
144
glUseProgram ( userData->programObject );
146
// Load the vertex position
147
glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,
148
GL_FALSE, 0, vVertices );
150
glEnableVertexAttribArray ( userData->positionLoc );
154
// Initialize upper-left region. In this case, the
155
// stencil-buffer values will be replaced because the
156
// stencil test for the rendered pixels will fail the
157
// stencil test, which is
159
// ref mask stencil mask
160
// ( 0x7 & 0x3 ) < ( 0x1 & 0x7 )
162
// The value in the stencil buffer for these pixels will
165
glStencilFunc( GL_LESS, 0x7, 0x3 );
166
glStencilOp( GL_REPLACE, GL_DECR, GL_DECR );
167
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[0] );
171
// Initialize the upper-right region. Here, we'll decrement
172
// the stencil-buffer values where the stencil test passes
173
// but the depth test fails. The stencil test is
175
// ref mask stencil mask
176
// ( 0x3 & 0x3 ) > ( 0x1 & 0x3 )
178
// but where the geometry fails the depth test. The
179
// stencil values for these pixels will be 0x0.
181
glStencilFunc( GL_GREATER, 0x3, 0x3 );
182
glStencilOp( GL_KEEP, GL_DECR, GL_KEEP );
183
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[1] );
187
// Initialize the lower-left region. Here we'll increment
188
// (with saturation) the stencil value where both the
189
// stencil and depth tests pass. The stencil test for
190
// these pixels will be
192
// ref mask stencil mask
193
// ( 0x1 & 0x3 ) == ( 0x1 & 0x3 )
195
// The stencil values for these pixels will be 0x2.
197
glStencilFunc( GL_EQUAL, 0x1, 0x3 );
198
glStencilOp( GL_KEEP, GL_INCR, GL_INCR );
199
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[2] );
203
// Finally, initialize the lower-right region. We'll invert
204
// the stencil value where the stencil tests fails. The
205
// stencil test for these pixels will be
207
// ref mask stencil mask
208
// ( 0x2 & 0x1 ) == ( 0x1 & 0x1 )
210
// The stencil value here will be set to ~((2^s-1) & 0x1),
211
// (with the 0x1 being from the stencil clear value),
212
// where 's' is the number of bits in the stencil buffer
214
glStencilFunc( GL_EQUAL, 0x2, 0x1 );
215
glStencilOp( GL_INVERT, GL_KEEP, GL_KEEP );
216
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[3] );
218
// Since we don't know at compile time how many stecil bits are present,
219
// we'll query, and update the value correct value in the
220
// stencilValues arrays for the fourth tests. We'll use this value
221
// later in rendering.
222
glGetIntegerv( GL_STENCIL_BITS, &numStencilBits );
224
stencilValues[3] = ~(((1 << numStencilBits) - 1) & 0x1) & 0xff;
226
// Use the stencil buffer for controlling where rendering will
227
// occur. We diable writing to the stencil buffer so we
228
// can test against them without modifying the values we
230
glStencilMask( 0x0 );
232
for ( i = 0; i < NumTests; ++i )
234
glStencilFunc( GL_EQUAL, stencilValues[i], 0xff );
235
glUniform4fv( userData->colorLoc, 1, colors[i] );
236
glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices[4] );
239
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
245
void ShutDown ( ESContext *esContext )
247
UserData *userData = esContext->userData;
249
// Delete program object
250
glDeleteProgram ( userData->programObject );
254
int main ( int argc, char *argv[] )
259
esInitContext ( &esContext );
260
esContext.userData = &userData;
262
esCreateWindow ( &esContext, "Stencil Test", 320, 240,
263
ES_WINDOW_RGB | ES_WINDOW_DEPTH | ES_WINDOW_STENCIL );
265
if ( !Init ( &esContext ) )
268
esRegisterDrawFunc ( &esContext, Draw );
270
esMainLoop ( &esContext );
272
ShutDown ( &esContext );