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 // Simple_TextureCubemap.c | 11 // Simple_TextureCubemap.c |
12 // | 12 // |
13 // This is a simple example that draws a sphere with a cubemap image applied. | 13 // This is a simple example that draws a sphere with a cubemap image applied. |
14 // | 14 // |
15 #include <stdlib.h> | 15 #include <stdlib.h> |
16 #include "esUtil.h" | 16 #include "Simple_TextureCubemap.h" |
17 | |
18 typedef struct | |
19 { | |
20 // Handle to a program object | |
21 GLuint programObject; | |
22 | |
23 // Attribute locations | |
24 GLint positionLoc; | |
25 GLint normalLoc; | |
26 | |
27 // Sampler location | |
28 GLint samplerLoc; | |
29 | |
30 // Texture handle | |
31 GLuint textureId; | |
32 | |
33 // Vertex data | |
34 int numIndices; | |
35 GLuint vboIds[3]; | |
36 } UserData; | |
37 | 17 |
38 /// | 18 /// |
39 // Create a simple cubemap with a 1x1 face with a different | 19 // Create a simple cubemap with a 1x1 face with a different |
40 // color for each face | 20 // color for each face |
41 static GLuint CreateSimpleTextureCubemap( ) | 21 static GLuint CreateSimpleTextureCubemap( ) |
42 { | 22 { |
43 GLuint textureId; | 23 GLuint textureId; |
44 // Six 1x1 RGB faces | 24 // Six 1x1 RGB faces |
45 GLubyte cubePixels[6][3] = | 25 GLubyte cubePixels[6][3] = |
46 { | 26 { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); | 73 glTexParameteri ( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); |
94 | 74 |
95 return textureId; | 75 return textureId; |
96 | 76 |
97 } | 77 } |
98 | 78 |
99 | 79 |
100 /// | 80 /// |
101 // Initialize the shader and program object | 81 // Initialize the shader and program object |
102 // | 82 // |
103 int Init ( ESContext *esContext ) | 83 int stcInit ( ESContext *esContext ) |
104 { | 84 { |
105 UserData *userData = esContext->userData; | 85 STCUserData *userData = esContext->userData; |
106 int numSlices = 20; | 86 int numSlices = 20; |
107 int numVertices = ( (numSlices / 2) + 1 ) * ( numSlices + 1 ); | 87 int numVertices = ( (numSlices / 2) + 1 ) * ( numSlices + 1 ); |
108 GLfloat *vertices = NULL; | 88 GLfloat *vertices = NULL; |
109 GLfloat *normals = NULL; | 89 GLfloat *normals = NULL; |
110 GLushort *indices = NULL; | 90 GLushort *indices = NULL; |
111 GLbyte vShaderStr[] = | 91 GLbyte vShaderStr[] = |
112 "attribute vec4 a_position; \n" | 92 "attribute vec4 a_position; \n" |
113 "attribute vec3 a_normal; \n" | 93 "attribute vec3 a_normal; \n" |
114 "varying vec3 v_normal; \n" | 94 "varying vec3 v_normal; \n" |
115 "void main() \n" | 95 "void main() \n" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 if ( normals != NULL ) free ( normals ); | 140 if ( normals != NULL ) free ( normals ); |
161 if ( indices != NULL ) free ( indices ); | 141 if ( indices != NULL ) free ( indices ); |
162 | 142 |
163 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); | 143 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); |
164 return TRUE; | 144 return TRUE; |
165 } | 145 } |
166 | 146 |
167 /// | 147 /// |
168 // Draw a triangle using the shader pair created in Init() | 148 // Draw a triangle using the shader pair created in Init() |
169 // | 149 // |
170 void Draw ( ESContext *esContext ) | 150 void stcDraw ( ESContext *esContext ) |
171 { | 151 { |
172 UserData *userData = esContext->userData; | 152 STCUserData *userData = esContext->userData; |
173 | 153 |
174 // Set the viewport | 154 // Set the viewport |
175 glViewport ( 0, 0, esContext->width, esContext->height ); | 155 glViewport ( 0, 0, esContext->width, esContext->height ); |
176 | 156 |
177 // Clear the color buffer | 157 // Clear the color buffer |
178 glClear ( GL_COLOR_BUFFER_BIT ); | 158 glClear ( GL_COLOR_BUFFER_BIT ); |
179 | 159 |
180 | 160 |
181 glCullFace ( GL_BACK ); | 161 glCullFace ( GL_BACK ); |
182 glEnable ( GL_CULL_FACE ); | 162 glEnable ( GL_CULL_FACE ); |
(...skipping 13 matching lines...) Expand all Loading... |
196 | 176 |
197 // Bind the texture | 177 // Bind the texture |
198 glActiveTexture ( GL_TEXTURE0 ); | 178 glActiveTexture ( GL_TEXTURE0 ); |
199 glBindTexture ( GL_TEXTURE_CUBE_MAP, userData->textureId ); | 179 glBindTexture ( GL_TEXTURE_CUBE_MAP, userData->textureId ); |
200 | 180 |
201 // Set the sampler texture unit to 0 | 181 // Set the sampler texture unit to 0 |
202 glUniform1i ( userData->samplerLoc, 0 ); | 182 glUniform1i ( userData->samplerLoc, 0 ); |
203 | 183 |
204 glDrawElements ( GL_TRIANGLES, userData->numIndices, | 184 glDrawElements ( GL_TRIANGLES, userData->numIndices, |
205 GL_UNSIGNED_SHORT, 0 ); | 185 GL_UNSIGNED_SHORT, 0 ); |
206 | |
207 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); | |
208 } | 186 } |
209 | 187 |
210 /// | 188 /// |
211 // Cleanup | 189 // Cleanup |
212 // | 190 // |
213 void ShutDown ( ESContext *esContext ) | 191 void stcShutDown ( ESContext *esContext ) |
214 { | 192 { |
215 UserData *userData = esContext->userData; | 193 STCUserData *userData = esContext->userData; |
216 | 194 |
217 // Delete texture object | 195 // Delete texture object |
218 glDeleteTextures ( 1, &userData->textureId ); | 196 glDeleteTextures ( 1, &userData->textureId ); |
219 | 197 |
220 // Delete program object | 198 // Delete program object |
221 glDeleteProgram ( userData->programObject ); | 199 glDeleteProgram ( userData->programObject ); |
222 | 200 |
223 // Delete vertex buffer objects | 201 // Delete vertex buffer objects |
224 glDeleteBuffers ( 3, userData->vboIds ); | 202 glDeleteBuffers ( 3, userData->vboIds ); |
225 } | 203 } |
226 | |
227 | |
228 int main ( int argc, char *argv[] ) | |
229 { | |
230 ESContext esContext; | |
231 UserData userData; | |
232 | |
233 esInitContext ( &esContext ); | |
234 esContext.userData = &userData; | |
235 | |
236 esCreateWindow ( &esContext, "Simple Texture Cubemap", 320, 240, ES_WINDOW_RG
B ); | |
237 | |
238 if ( !Init ( &esContext ) ) | |
239 return 0; | |
240 | |
241 esRegisterDrawFunc ( &esContext, Draw ); | |
242 | |
243 esMainLoop ( &esContext ); | |
244 | |
245 ShutDown ( &esContext ); | |
246 } | |
OLD | NEW |