| 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 // MipMap2D.c | 11 // MipMap2D.c |
| 12 // | 12 // |
| 13 // This is a simple example that demonstrates generating a mipmap chain | 13 // This is a simple example that demonstrates generating a mipmap chain |
| 14 // and rendering with it | 14 // and rendering with it |
| 15 // | 15 // |
| 16 #include <stdlib.h> | 16 #include <stdlib.h> |
| 17 #include "esUtil.h" | 17 #include "MipMap2D.h" |
| 18 | |
| 19 typedef struct | |
| 20 { | |
| 21 // Handle to a program object | |
| 22 GLuint programObject; | |
| 23 | |
| 24 // Attribute locations | |
| 25 GLint positionLoc; | |
| 26 GLint texCoordLoc; | |
| 27 | |
| 28 // Sampler location | |
| 29 GLint samplerLoc; | |
| 30 | |
| 31 // Offset location | |
| 32 GLint offsetLoc; | |
| 33 | |
| 34 // Texture handle | |
| 35 GLuint textureId; | |
| 36 | |
| 37 } UserData; | |
| 38 | |
| 39 | 18 |
| 40 /// | 19 /// |
| 41 // From an RGB8 source image, generate the next level mipmap | 20 // From an RGB8 source image, generate the next level mipmap |
| 42 // | 21 // |
| 43 GLboolean GenMipMap2D( GLubyte *src, GLubyte **dst, int srcWidth, int srcHeight,
int *dstWidth, int *dstHeight ) | 22 static GLboolean GenMipMap2D( GLubyte *src, GLubyte **dst, int srcWidth, int src
Height, int *dstWidth, int *dstHeight ) |
| 44 { | 23 { |
| 45 int x, | 24 int x, |
| 46 y; | 25 y; |
| 47 int texelSize = 3; | 26 int texelSize = 3; |
| 48 | 27 |
| 49 *dstWidth = srcWidth / 2; | 28 *dstWidth = srcWidth / 2; |
| 50 if ( *dstWidth <= 0 ) | 29 if ( *dstWidth <= 0 ) |
| 51 *dstWidth = 1; | 30 *dstWidth = 1; |
| 52 | 31 |
| 53 *dstHeight = srcHeight / 2; | 32 *dstHeight = srcHeight / 2; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 (*dst)[ ( y * (*dstWidth) + x ) * texelSize + 2] = (GLubyte)( b ); | 77 (*dst)[ ( y * (*dstWidth) + x ) * texelSize + 2] = (GLubyte)( b ); |
| 99 } | 78 } |
| 100 } | 79 } |
| 101 | 80 |
| 102 return GL_TRUE; | 81 return GL_TRUE; |
| 103 } | 82 } |
| 104 | 83 |
| 105 /// | 84 /// |
| 106 // Generate an RGB8 checkerboard image | 85 // Generate an RGB8 checkerboard image |
| 107 // | 86 // |
| 108 GLubyte* GenCheckImage( int width, int height, int checkSize ) | 87 static GLubyte* GenCheckImage( int width, int height, int checkSize ) |
| 109 { | 88 { |
| 110 int x, | 89 int x, |
| 111 y; | 90 y; |
| 112 GLubyte *pixels = malloc( width * height * 3 ); | 91 GLubyte *pixels = malloc( width * height * 3 ); |
| 113 | 92 |
| 114 if ( pixels == NULL ) | 93 if ( pixels == NULL ) |
| 115 return NULL; | 94 return NULL; |
| 116 | 95 |
| 117 for ( y = 0; y < height; y++ ) | 96 for ( y = 0; y < height; y++ ) |
| 118 for ( x = 0; x < width; x++ ) | 97 for ( x = 0; x < width; x++ ) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 135 pixels[(y * height + x) * 3 + 1] = 0; | 114 pixels[(y * height + x) * 3 + 1] = 0; |
| 136 pixels[(y * height + x) * 3 + 2] = bColor; | 115 pixels[(y * height + x) * 3 + 2] = bColor; |
| 137 } | 116 } |
| 138 | 117 |
| 139 return pixels; | 118 return pixels; |
| 140 } | 119 } |
| 141 | 120 |
| 142 /// | 121 /// |
| 143 // Create a mipmapped 2D texture image | 122 // Create a mipmapped 2D texture image |
| 144 // | 123 // |
| 145 GLuint CreateMipMappedTexture2D( ) | 124 static GLuint CreateMipMappedTexture2D( ) |
| 146 { | 125 { |
| 147 // Texture object handle | 126 // Texture object handle |
| 148 GLuint textureId; | 127 GLuint textureId; |
| 149 int width = 256, | 128 int width = 256, |
| 150 height = 256; | 129 height = 256; |
| 151 int level; | 130 int level; |
| 152 GLubyte *pixels; | 131 GLubyte *pixels; |
| 153 GLubyte *prevImage; | 132 GLubyte *prevImage; |
| 154 GLubyte *newImage; | 133 GLubyte *newImage; |
| 155 | 134 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); | 182 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); |
| 204 | 183 |
| 205 return textureId; | 184 return textureId; |
| 206 | 185 |
| 207 } | 186 } |
| 208 | 187 |
| 209 | 188 |
| 210 /// | 189 /// |
| 211 // Initialize the shader and program object | 190 // Initialize the shader and program object |
| 212 // | 191 // |
| 213 int Init ( ESContext *esContext ) | 192 int mmInit ( ESContext *esContext ) |
| 214 { | 193 { |
| 215 UserData *userData = esContext->userData; | 194 MMUserData *userData = esContext->userData; |
| 216 GLbyte vShaderStr[] = | 195 GLbyte vShaderStr[] = |
| 217 "uniform float u_offset; \n" | 196 "uniform float u_offset; \n" |
| 218 "attribute vec4 a_position; \n" | 197 "attribute vec4 a_position; \n" |
| 219 "attribute vec2 a_texCoord; \n" | 198 "attribute vec2 a_texCoord; \n" |
| 220 "varying vec2 v_texCoord; \n" | 199 "varying vec2 v_texCoord; \n" |
| 221 "void main() \n" | 200 "void main() \n" |
| 222 "{ \n" | 201 "{ \n" |
| 223 " gl_Position = a_position; \n" | 202 " gl_Position = a_position; \n" |
| 224 " gl_Position.x += u_offset;\n" | 203 " gl_Position.x += u_offset;\n" |
| 225 " v_texCoord = a_texCoord; \n" | 204 " v_texCoord = a_texCoord; \n" |
| 226 "} \n"; | 205 "} \n"; |
| 227 | 206 |
| 207 // TODO(alokp): Shaders containing "precision" do not compile. |
| 228 GLbyte fShaderStr[] = | 208 GLbyte fShaderStr[] = |
| 229 "precision mediump float; \n" | 209 "//precision mediump float; \n" |
| 230 "varying vec2 v_texCoord; \n" | 210 "varying vec2 v_texCoord; \n" |
| 231 "uniform sampler2D s_texture; \n" | 211 "uniform sampler2D s_texture; \n" |
| 232 "void main() \n" | 212 "void main() \n" |
| 233 "{ \n" | 213 "{ \n" |
| 234 " gl_FragColor = texture2D( s_texture, v_texCoord );\n" | 214 " gl_FragColor = texture2D( s_texture, v_texCoord );\n" |
| 235 "} \n"; | 215 "} \n"; |
| 236 | 216 |
| 217 GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, 1.5f, // Position 0 |
| 218 0.0f, 0.0f, // TexCoord 0 |
| 219 -0.5f, -0.5f, 0.0f, 0.75f, // Position 1 |
| 220 0.0f, 1.0f, // TexCoord 1 |
| 221 0.5f, -0.5f, 0.0f, 0.75f, // Position 2 |
| 222 1.0f, 1.0f, // TexCoord 2 |
| 223 0.5f, 0.5f, 0.0f, 1.5f, // Position 3 |
| 224 1.0f, 0.0f // TexCoord 3 |
| 225 }; |
| 226 GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; |
| 227 |
| 237 // Load the shaders and get a linked program object | 228 // Load the shaders and get a linked program object |
| 238 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); | 229 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); |
| 230 if (userData->programObject == 0) return FALSE; |
| 239 | 231 |
| 240 // Get the attribute locations | 232 // Get the attribute locations |
| 241 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos
ition" ); | 233 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos
ition" ); |
| 242 userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_tex
Coord" ); | 234 userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_tex
Coord" ); |
| 243 | 235 |
| 244 // Get the sampler location | 236 // Get the sampler location |
| 245 userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_tex
ture" ); | 237 userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_tex
ture" ); |
| 246 | 238 |
| 247 // Get the offset location | 239 // Get the offset location |
| 248 userData->offsetLoc = glGetUniformLocation( userData->programObject, "u_offse
t" ); | 240 userData->offsetLoc = glGetUniformLocation( userData->programObject, "u_offse
t" ); |
| 249 | 241 |
| 250 // Load the texture | 242 // Load the texture |
| 251 userData->textureId = CreateMipMappedTexture2D (); | 243 userData->textureId = CreateMipMappedTexture2D (); |
| 252 | 244 |
| 245 // Load vertex data |
| 246 glGenBuffers ( 2, userData->vboIds ); |
| 247 glBindBuffer ( GL_ARRAY_BUFFER, userData->vboIds[0] ); |
| 248 glBufferData ( GL_ARRAY_BUFFER, sizeof(vVertices), |
| 249 vVertices, GL_STATIC_DRAW); |
| 250 glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->vboIds[1] ); |
| 251 glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), |
| 252 indices, GL_STATIC_DRAW ); |
| 253 |
| 253 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); | 254 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); |
| 254 return TRUE; | 255 return TRUE; |
| 255 } | 256 } |
| 256 | 257 |
| 257 /// | 258 /// |
| 258 // Draw a triangle using the shader pair created in Init() | 259 // Draw a triangle using the shader pair created in Init() |
| 259 // | 260 // |
| 260 void Draw ( ESContext *esContext ) | 261 #define VTX_POS_SIZE 4 |
| 262 #define VTX_TEX_SIZE 2 |
| 263 #define VTX_STRIDE (6 * sizeof(GLfloat)) |
| 264 void mmDraw ( ESContext *esContext ) |
| 261 { | 265 { |
| 262 UserData *userData = esContext->userData; | 266 MMUserData *userData = esContext->userData; |
| 263 GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, 1.5f, // Position 0 | 267 GLuint offset = 0; |
| 264 0.0f, 0.0f, // TexCoord 0 | |
| 265 -0.5f, -0.5f, 0.0f, 0.75f, // Position 1 | |
| 266 0.0f, 1.0f, // TexCoord 1 | |
| 267 0.5f, -0.5f, 0.0f, 0.75f, // Position 2 | |
| 268 1.0f, 1.0f, // TexCoord 2 | |
| 269 0.5f, 0.5f, 0.0f, 1.5f, // Position 3 | |
| 270 1.0f, 0.0f // TexCoord 3 | |
| 271 }; | |
| 272 GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; | |
| 273 | 268 |
| 274 // Set the viewport | 269 // Set the viewport |
| 275 glViewport ( 0, 0, esContext->width, esContext->height ); | 270 glViewport ( 0, 0, esContext->width, esContext->height ); |
| 276 | 271 |
| 277 // Clear the color buffer | 272 // Clear the color buffer |
| 278 glClear ( GL_COLOR_BUFFER_BIT ); | 273 glClear ( GL_COLOR_BUFFER_BIT ); |
| 279 | 274 |
| 280 // Use the program object | 275 // Use the program object |
| 281 glUseProgram ( userData->programObject ); | 276 glUseProgram ( userData->programObject ); |
| 282 | 277 |
| 283 // Load the vertex position | 278 // Load the vertex position |
| 284 glVertexAttribPointer ( userData->positionLoc, 4, GL_FLOAT, | 279 glVertexAttribPointer ( userData->positionLoc, VTX_POS_SIZE, GL_FLOAT, |
| 285 GL_FALSE, 6 * sizeof(GLfloat), vVertices ); | 280 GL_FALSE, VTX_STRIDE, (GLvoid*) offset ); |
| 281 offset += VTX_POS_SIZE * sizeof(GLfloat); |
| 286 // Load the texture coordinate | 282 // Load the texture coordinate |
| 287 glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT, | 283 glVertexAttribPointer ( userData->texCoordLoc, VTX_TEX_SIZE, GL_FLOAT, |
| 288 GL_FALSE, 6 * sizeof(GLfloat), &vVertices[4] ); | 284 GL_FALSE, VTX_STRIDE, (GLvoid*) offset ); |
| 289 | 285 |
| 290 glEnableVertexAttribArray ( userData->positionLoc ); | 286 glEnableVertexAttribArray ( userData->positionLoc ); |
| 291 glEnableVertexAttribArray ( userData->texCoordLoc ); | 287 glEnableVertexAttribArray ( userData->texCoordLoc ); |
| 292 | 288 |
| 293 // Bind the texture | 289 // Bind the texture |
| 294 glActiveTexture ( GL_TEXTURE0 ); | 290 glActiveTexture ( GL_TEXTURE0 ); |
| 295 glBindTexture ( GL_TEXTURE_2D, userData->textureId ); | 291 glBindTexture ( GL_TEXTURE_2D, userData->textureId ); |
| 296 | 292 |
| 297 // Set the sampler texture unit to 0 | 293 // Set the sampler texture unit to 0 |
| 298 glUniform1i ( userData->samplerLoc, 0 ); | 294 glUniform1i ( userData->samplerLoc, 0 ); |
| 299 | 295 |
| 300 // Draw quad with nearest sampling | 296 // Draw quad with nearest sampling |
| 301 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); | 297 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); |
| 302 glUniform1f ( userData->offsetLoc, -0.6f ); | 298 glUniform1f ( userData->offsetLoc, -0.6f ); |
| 303 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); | 299 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); |
| 304 | 300 |
| 305 // Draw quad with trilinear filtering | 301 // Draw quad with trilinear filtering |
| 306 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINE
AR ); | 302 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINE
AR ); |
| 307 glUniform1f ( userData->offsetLoc, 0.6f ); | 303 glUniform1f ( userData->offsetLoc, 0.6f ); |
| 308 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); | 304 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); |
| 309 | |
| 310 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); | |
| 311 } | 305 } |
| 312 | 306 |
| 313 /// | 307 /// |
| 314 // Cleanup | 308 // Cleanup |
| 315 // | 309 // |
| 316 void ShutDown ( ESContext *esContext ) | 310 void mmShutDown ( ESContext *esContext ) |
| 317 { | 311 { |
| 318 UserData *userData = esContext->userData; | 312 MMUserData *userData = esContext->userData; |
| 319 | 313 |
| 320 // Delete texture object | 314 // Delete texture object |
| 321 glDeleteTextures ( 1, &userData->textureId ); | 315 glDeleteTextures ( 1, &userData->textureId ); |
| 322 | 316 |
| 317 // Delete VBOs |
| 318 glDeleteBuffers ( 2, userData->vboIds ); |
| 319 |
| 323 // Delete program object | 320 // Delete program object |
| 324 glDeleteProgram ( userData->programObject ); | 321 glDeleteProgram ( userData->programObject ); |
| 325 } | 322 } |
| 326 | |
| 327 | |
| 328 int main ( int argc, char *argv[] ) | |
| 329 { | |
| 330 ESContext esContext; | |
| 331 UserData userData; | |
| 332 | |
| 333 esInitContext ( &esContext ); | |
| 334 esContext.userData = &userData; | |
| 335 | |
| 336 esCreateWindow ( &esContext, "MipMap 2D", 320, 240, ES_WINDOW_RGB ); | |
| 337 | |
| 338 if ( !Init ( &esContext ) ) | |
| 339 return 0; | |
| 340 | |
| 341 esRegisterDrawFunc ( &esContext, Draw ); | |
| 342 | |
| 343 esMainLoop ( &esContext ); | |
| 344 | |
| 345 ShutDown ( &esContext ); | |
| 346 } | |
| OLD | NEW |