| 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 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 GLint positionLoc; | 24 GLint positionLoc; |
| 25 GLint normalLoc; | 25 GLint normalLoc; |
| 26 | 26 |
| 27 // Sampler location | 27 // Sampler location |
| 28 GLint samplerLoc; | 28 GLint samplerLoc; |
| 29 | 29 |
| 30 // Texture handle | 30 // Texture handle |
| 31 GLuint textureId; | 31 GLuint textureId; |
| 32 | 32 |
| 33 // Vertex data | 33 // Vertex data |
| 34 int numIndices; | 34 int numIndices; |
| 35 GLfloat *vertices; | 35 GLuint vboIds[3]; |
| 36 GLfloat *normals; | |
| 37 GLuint *indices; | |
| 38 | |
| 39 } UserData; | 36 } UserData; |
| 40 | 37 |
| 41 /// | 38 /// |
| 42 // Create a simple cubemap with a 1x1 face with a different | 39 // Create a simple cubemap with a 1x1 face with a different |
| 43 // color for each face | 40 // color for each face |
| 44 GLuint CreateSimpleTextureCubemap( ) | 41 static GLuint CreateSimpleTextureCubemap( ) |
| 45 { | 42 { |
| 46 GLuint textureId; | 43 GLuint textureId; |
| 47 // Six 1x1 RGB faces | 44 // Six 1x1 RGB faces |
| 48 GLubyte cubePixels[6][3] = | 45 GLubyte cubePixels[6][3] = |
| 49 { | 46 { |
| 50 // Face 0 - Red | 47 // Face 0 - Red |
| 51 255, 0, 0, | 48 255, 0, 0, |
| 52 // Face 1 - Green, | 49 // Face 1 - Green, |
| 53 0, 255, 0, | 50 0, 255, 0, |
| 54 // Face 3 - Blue | 51 // Face 3 - Blue |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 96 |
| 100 } | 97 } |
| 101 | 98 |
| 102 | 99 |
| 103 /// | 100 /// |
| 104 // Initialize the shader and program object | 101 // Initialize the shader and program object |
| 105 // | 102 // |
| 106 int Init ( ESContext *esContext ) | 103 int Init ( ESContext *esContext ) |
| 107 { | 104 { |
| 108 UserData *userData = esContext->userData; | 105 UserData *userData = esContext->userData; |
| 106 int numSlices = 20; |
| 107 int numVertices = ( (numSlices / 2) + 1 ) * ( numSlices + 1 ); |
| 108 GLfloat *vertices = NULL; |
| 109 GLfloat *normals = NULL; |
| 110 GLushort *indices = NULL; |
| 109 GLbyte vShaderStr[] = | 111 GLbyte vShaderStr[] = |
| 110 "attribute vec4 a_position; \n" | 112 "attribute vec4 a_position; \n" |
| 111 "attribute vec3 a_normal; \n" | 113 "attribute vec3 a_normal; \n" |
| 112 "varying vec3 v_normal; \n" | 114 "varying vec3 v_normal; \n" |
| 113 "void main() \n" | 115 "void main() \n" |
| 114 "{ \n" | 116 "{ \n" |
| 115 " gl_Position = a_position; \n" | 117 " gl_Position = a_position; \n" |
| 116 " v_normal = a_normal; \n" | 118 " v_normal = a_normal; \n" |
| 117 "} \n"; | 119 "} \n"; |
| 118 | 120 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 132 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos
ition" ); | 134 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_pos
ition" ); |
| 133 userData->normalLoc = glGetAttribLocation ( userData->programObject, "a_norma
l" ); | 135 userData->normalLoc = glGetAttribLocation ( userData->programObject, "a_norma
l" ); |
| 134 | 136 |
| 135 // Get the sampler locations | 137 // Get the sampler locations |
| 136 userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_tex
ture" ); | 138 userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_tex
ture" ); |
| 137 | 139 |
| 138 // Load the texture | 140 // Load the texture |
| 139 userData->textureId = CreateSimpleTextureCubemap (); | 141 userData->textureId = CreateSimpleTextureCubemap (); |
| 140 | 142 |
| 141 // Generate the vertex data | 143 // Generate the vertex data |
| 142 userData->numIndices = esGenSphere ( 20, 0.75f, &userData->vertices, &userDat
a->normals, | 144 userData->numIndices = esGenSphere ( numSlices, 0.75f, &vertices, &normals, |
| 143 NULL, &userData->indices ); | 145 NULL, &indices ); |
| 144 | 146 glGenBuffers( 3, userData->vboIds ); |
| 147 // Load vertex positions |
| 148 glBindBuffer ( GL_ARRAY_BUFFER, userData->vboIds[0] ); |
| 149 glBufferData ( GL_ARRAY_BUFFER, 3 * numVertices * sizeof(GLfloat), |
| 150 vertices, GL_STATIC_DRAW ); |
| 151 // Load vertex normals |
| 152 glBindBuffer ( GL_ARRAY_BUFFER, userData->vboIds[1] ); |
| 153 glBufferData ( GL_ARRAY_BUFFER, 3 * numVertices * sizeof(GLfloat), |
| 154 normals, GL_STATIC_DRAW ); |
| 155 // Load vertex indices |
| 156 glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->vboIds[2] ); |
| 157 glBufferData ( GL_ELEMENT_ARRAY_BUFFER, userData->numIndices * sizeof(GLushor
t), |
| 158 indices, GL_STATIC_DRAW ); |
| 159 if ( vertices != NULL ) free ( vertices ); |
| 160 if ( normals != NULL ) free ( normals ); |
| 161 if ( indices != NULL ) free ( indices ); |
| 145 | 162 |
| 146 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); | 163 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); |
| 147 return TRUE; | 164 return TRUE; |
| 148 } | 165 } |
| 149 | 166 |
| 150 /// | 167 /// |
| 151 // Draw a triangle using the shader pair created in Init() | 168 // Draw a triangle using the shader pair created in Init() |
| 152 // | 169 // |
| 153 void Draw ( ESContext *esContext ) | 170 void Draw ( ESContext *esContext ) |
| 154 { | 171 { |
| 155 UserData *userData = esContext->userData; | 172 UserData *userData = esContext->userData; |
| 156 | 173 |
| 157 // Set the viewport | 174 // Set the viewport |
| 158 glViewport ( 0, 0, esContext->width, esContext->height ); | 175 glViewport ( 0, 0, esContext->width, esContext->height ); |
| 159 | 176 |
| 160 // Clear the color buffer | 177 // Clear the color buffer |
| 161 glClear ( GL_COLOR_BUFFER_BIT ); | 178 glClear ( GL_COLOR_BUFFER_BIT ); |
| 162 | 179 |
| 163 | 180 |
| 164 glCullFace ( GL_BACK ); | 181 glCullFace ( GL_BACK ); |
| 165 glEnable ( GL_CULL_FACE ); | 182 glEnable ( GL_CULL_FACE ); |
| 166 | 183 |
| 167 // Use the program object | 184 // Use the program object |
| 168 glUseProgram ( userData->programObject ); | 185 glUseProgram ( userData->programObject ); |
| 169 | 186 |
| 170 // Load the vertex position | 187 // Load the vertex position |
| 171 glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, | 188 glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT, |
| 172 GL_FALSE, 0, userData->vertices ); | 189 GL_FALSE, 0, 0 ); |
| 173 // Load the normal | 190 // Load the normal |
| 174 glVertexAttribPointer ( userData->normalLoc, 3, GL_FLOAT, | 191 glVertexAttribPointer ( userData->normalLoc, 3, GL_FLOAT, |
| 175 GL_FALSE, 0, userData->normals ); | 192 GL_FALSE, 0, 0 ); |
| 176 | 193 |
| 177 glEnableVertexAttribArray ( userData->positionLoc ); | 194 glEnableVertexAttribArray ( userData->positionLoc ); |
| 178 glEnableVertexAttribArray ( userData->normalLoc ); | 195 glEnableVertexAttribArray ( userData->normalLoc ); |
| 179 | 196 |
| 180 // Bind the texture | 197 // Bind the texture |
| 181 glActiveTexture ( GL_TEXTURE0 ); | 198 glActiveTexture ( GL_TEXTURE0 ); |
| 182 glBindTexture ( GL_TEXTURE_CUBE_MAP, userData->textureId ); | 199 glBindTexture ( GL_TEXTURE_CUBE_MAP, userData->textureId ); |
| 183 | 200 |
| 184 // Set the sampler texture unit to 0 | 201 // Set the sampler texture unit to 0 |
| 185 glUniform1i ( userData->samplerLoc, 0 ); | 202 glUniform1i ( userData->samplerLoc, 0 ); |
| 186 | 203 |
| 187 glDrawElements ( GL_TRIANGLES, userData->numIndices, | 204 glDrawElements ( GL_TRIANGLES, userData->numIndices, |
| 188 GL_UNSIGNED_INT, userData->indices ); | 205 GL_UNSIGNED_SHORT, 0 ); |
| 189 | 206 |
| 190 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); | 207 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); |
| 191 } | 208 } |
| 192 | 209 |
| 193 /// | 210 /// |
| 194 // Cleanup | 211 // Cleanup |
| 195 // | 212 // |
| 196 void ShutDown ( ESContext *esContext ) | 213 void ShutDown ( ESContext *esContext ) |
| 197 { | 214 { |
| 198 UserData *userData = esContext->userData; | 215 UserData *userData = esContext->userData; |
| 199 | 216 |
| 200 // Delete texture object | 217 // Delete texture object |
| 201 glDeleteTextures ( 1, &userData->textureId ); | 218 glDeleteTextures ( 1, &userData->textureId ); |
| 202 | 219 |
| 203 // Delete program object | 220 // Delete program object |
| 204 glDeleteProgram ( userData->programObject ); | 221 glDeleteProgram ( userData->programObject ); |
| 205 | 222 |
| 206 free ( userData->vertices ); | 223 |
| 207 free ( userData->normals ); | |
| 208 } | 224 } |
| 209 | 225 |
| 210 | 226 |
| 211 int main ( int argc, char *argv[] ) | 227 int main ( int argc, char *argv[] ) |
| 212 { | 228 { |
| 213 ESContext esContext; | 229 ESContext esContext; |
| 214 UserData userData; | 230 UserData userData; |
| 215 | 231 |
| 216 esInitContext ( &esContext ); | 232 esInitContext ( &esContext ); |
| 217 esContext.userData = &userData; | 233 esContext.userData = &userData; |
| 218 | 234 |
| 219 esCreateWindow ( &esContext, "Simple Texture Cubemap", 320, 240, ES_WINDOW_RG
B ); | 235 esCreateWindow ( &esContext, "Simple Texture Cubemap", 320, 240, ES_WINDOW_RG
B ); |
| 220 | 236 |
| 221 if ( !Init ( &esContext ) ) | 237 if ( !Init ( &esContext ) ) |
| 222 return 0; | 238 return 0; |
| 223 | 239 |
| 224 esRegisterDrawFunc ( &esContext, Draw ); | 240 esRegisterDrawFunc ( &esContext, Draw ); |
| 225 | 241 |
| 226 esMainLoop ( &esContext ); | 242 esMainLoop ( &esContext ); |
| 227 | 243 |
| 228 ShutDown ( &esContext ); | 244 ShutDown ( &esContext ); |
| 229 } | 245 } |
| OLD | NEW |