| OLD | NEW |
| 1 // | 1 // |
| 2 // Book: OpenGL(R) ES 2.0 Programming Guide | 2 // Book: OpenGL(R) ES 2.0 Programming Guide |
| 3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner | 3 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner |
| 4 // ISBN-10: 0321502795 | 4 // ISBN-10: 0321502795 |
| 5 // ISBN-13: 9780321502797 | 5 // ISBN-13: 9780321502797 |
| 6 // Publisher: Addison-Wesley Professional | 6 // Publisher: Addison-Wesley Professional |
| 7 // URLs: http://safari.informit.com/9780321563835 | 7 // URLs: http://safari.informit.com/9780321563835 |
| 8 // http://www.opengles-book.com | 8 // http://www.opengles-book.com |
| 9 // | 9 // |
| 10 | 10 |
| 11 // Stencil_Test.c | 11 // Stencil_Test.c |
| 12 // | 12 // |
| 13 // This example shows various stencil buffer | 13 // This example shows various stencil buffer |
| 14 // operations. | 14 // operations. |
| 15 // | 15 // |
| 16 #include <stdlib.h> | 16 #include <stdlib.h> |
| 17 #include "esUtil.h" | 17 #include "Stencil_Test.h" |
| 18 | |
| 19 typedef struct | |
| 20 { | |
| 21 // Handle to a program object | |
| 22 GLuint programObject; | |
| 23 | |
| 24 // Attribute locations | |
| 25 GLint positionLoc; | |
| 26 | |
| 27 // Uniform locations | |
| 28 GLint colorLoc; | |
| 29 | |
| 30 // Vertex buffer object handles | |
| 31 GLuint vboIds[2]; | |
| 32 | |
| 33 } UserData; | |
| 34 | 18 |
| 35 /// | 19 /// |
| 36 // Initialize the shader and program object | 20 // Initialize the shader and program object |
| 37 // | 21 // |
| 38 int Init ( ESContext *esContext ) | 22 int stInit ( ESContext *esContext ) |
| 39 { | 23 { |
| 40 UserData *userData = esContext->userData; | 24 STUserData *userData = esContext->userData; |
| 41 GLbyte vShaderStr[] = | 25 GLbyte vShaderStr[] = |
| 42 "attribute vec4 a_position; \n" | 26 "attribute vec4 a_position; \n" |
| 43 "void main() \n" | 27 "void main() \n" |
| 44 "{ \n" | 28 "{ \n" |
| 45 " gl_Position = a_position; \n" | 29 " gl_Position = a_position; \n" |
| 46 "} \n"; | 30 "} \n"; |
| 47 | 31 |
| 48 GLbyte fShaderStr[] = | 32 GLbyte fShaderStr[] = |
| 49 "precision mediump float; \n" | 33 "precision mediump float; \n" |
| 50 "uniform vec4 u_color; \n" | 34 "uniform vec4 u_color; \n" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 glEnable( GL_DEPTH_TEST ); | 99 glEnable( GL_DEPTH_TEST ); |
| 116 glEnable( GL_STENCIL_TEST ); | 100 glEnable( GL_STENCIL_TEST ); |
| 117 | 101 |
| 118 return TRUE; | 102 return TRUE; |
| 119 } | 103 } |
| 120 | 104 |
| 121 /// | 105 /// |
| 122 // Initialize the stencil buffer values, and then use those | 106 // Initialize the stencil buffer values, and then use those |
| 123 // values to control rendering | 107 // values to control rendering |
| 124 // | 108 // |
| 125 void Draw ( ESContext *esContext ) | 109 void stDraw ( ESContext *esContext ) |
| 126 { | 110 { |
| 127 int i; | 111 int i; |
| 128 GLubyte *offset = NULL; | 112 GLubyte *offset = NULL; |
| 129 | 113 |
| 130 UserData *userData = esContext->userData; | 114 STUserData *userData = esContext->userData; |
| 131 | 115 |
| 132 #define NumTests 4 | 116 #define NumTests 4 |
| 133 GLfloat colors[NumTests][4] = { | 117 GLfloat colors[NumTests][4] = { |
| 134 { 1.0f, 0.0f, 0.0f, 1.0f }, | 118 { 1.0f, 0.0f, 0.0f, 1.0f }, |
| 135 { 0.0f, 1.0f, 0.0f, 1.0f }, | 119 { 0.0f, 1.0f, 0.0f, 1.0f }, |
| 136 { 0.0f, 0.0f, 1.0f, 1.0f }, | 120 { 0.0f, 0.0f, 1.0f, 1.0f }, |
| 137 { 1.0f, 1.0f, 0.0f, 0.0f } | 121 { 1.0f, 1.0f, 0.0f, 0.0f } |
| 138 }; | 122 }; |
| 139 | 123 |
| 140 GLint numStencilBits; | 124 GLint numStencilBits; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 // can test against them without modifying the values we | 228 // can test against them without modifying the values we |
| 245 // generated. | 229 // generated. |
| 246 glStencilMask( 0x0 ); | 230 glStencilMask( 0x0 ); |
| 247 offset += 6; | 231 offset += 6; |
| 248 for ( i = 0; i < NumTests; ++i ) | 232 for ( i = 0; i < NumTests; ++i ) |
| 249 { | 233 { |
| 250 glStencilFunc( GL_EQUAL, stencilValues[i], 0xff ); | 234 glStencilFunc( GL_EQUAL, stencilValues[i], 0xff ); |
| 251 glUniform4fv( userData->colorLoc, 1, colors[i] ); | 235 glUniform4fv( userData->colorLoc, 1, colors[i] ); |
| 252 glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, offset ); | 236 glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, offset ); |
| 253 } | 237 } |
| 254 | |
| 255 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); | |
| 256 } | 238 } |
| 257 | 239 |
| 258 /// | 240 /// |
| 259 // Cleanup | 241 // Cleanup |
| 260 // | 242 // |
| 261 void ShutDown ( ESContext *esContext ) | 243 void stShutDown ( ESContext *esContext ) |
| 262 { | 244 { |
| 263 UserData *userData = esContext->userData; | 245 STUserData *userData = esContext->userData; |
| 264 | 246 |
| 265 // Delete program object | 247 // Delete program object |
| 266 glDeleteProgram ( userData->programObject ); | 248 glDeleteProgram ( userData->programObject ); |
| 267 | 249 |
| 268 // Delete vertex buffer objects | 250 // Delete vertex buffer objects |
| 269 glDeleteBuffers ( 2, userData->vboIds ); | 251 glDeleteBuffers ( 2, userData->vboIds ); |
| 270 } | 252 } |
| 271 | |
| 272 | |
| 273 int main ( int argc, char *argv[] ) | |
| 274 { | |
| 275 ESContext esContext; | |
| 276 UserData userData; | |
| 277 | |
| 278 esInitContext ( &esContext ); | |
| 279 esContext.userData = &userData; | |
| 280 | |
| 281 esCreateWindow ( &esContext, "Stencil Test", 320, 240, | |
| 282 ES_WINDOW_RGB | ES_WINDOW_DEPTH | ES_WINDOW_STENCIL ); | |
| 283 | |
| 284 if ( !Init ( &esContext ) ) | |
| 285 return 0; | |
| 286 | |
| 287 esRegisterDrawFunc ( &esContext, Draw ); | |
| 288 | |
| 289 esMainLoop ( &esContext ); | |
| 290 | |
| 291 ShutDown ( &esContext ); | |
| 292 } | |
| OLD | NEW |