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 // TextureWrap.c | 11 // TextureWrap.c |
12 // | 12 // |
13 // This is an example that demonstrates the three texture | 13 // This is an example that demonstrates the three texture |
14 // wrap modes available on 2D textures | 14 // wrap modes available on 2D textures |
15 // | 15 // |
16 #include <stdlib.h> | 16 #include <stdlib.h> |
17 #include "esUtil.h" | 17 #include "TextureWrap.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 // Vertex buffer object handle | |
38 GLuint vboIds[2]; | |
39 | |
40 } UserData; | |
41 | 18 |
42 /// | 19 /// |
43 // Generate an RGB8 checkerboard image | 20 // Generate an RGB8 checkerboard image |
44 // | 21 // |
45 static GLubyte* GenCheckImage( int width, int height, int checkSize ) | 22 static GLubyte* GenCheckImage( int width, int height, int checkSize ) |
46 { | 23 { |
47 int x, | 24 int x, |
48 y; | 25 y; |
49 GLubyte *pixels = malloc( width * height * 3 ); | 26 GLubyte *pixels = malloc( width * height * 3 ); |
50 | 27 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); | 83 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); |
107 | 84 |
108 return textureId; | 85 return textureId; |
109 | 86 |
110 } | 87 } |
111 | 88 |
112 | 89 |
113 /// | 90 /// |
114 // Initialize the shader and program object | 91 // Initialize the shader and program object |
115 // | 92 // |
116 int Init ( ESContext *esContext ) | 93 int twInit ( ESContext *esContext ) |
117 { | 94 { |
118 UserData *userData = esContext->userData; | 95 TWUserData *userData = esContext->userData; |
119 GLbyte vShaderStr[] = | 96 GLbyte vShaderStr[] = |
120 "uniform float u_offset; \n" | 97 "uniform float u_offset; \n" |
121 "attribute vec4 a_position; \n" | 98 "attribute vec4 a_position; \n" |
122 "attribute vec2 a_texCoord; \n" | 99 "attribute vec2 a_texCoord; \n" |
123 "varying vec2 v_texCoord; \n" | 100 "varying vec2 v_texCoord; \n" |
124 "void main() \n" | 101 "void main() \n" |
125 "{ \n" | 102 "{ \n" |
126 " gl_Position = a_position; \n" | 103 " gl_Position = a_position; \n" |
127 " gl_Position.x += u_offset;\n" | 104 " gl_Position.x += u_offset;\n" |
128 " v_texCoord = a_texCoord; \n" | 105 " v_texCoord = a_texCoord; \n" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); | 153 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); |
177 return TRUE; | 154 return TRUE; |
178 } | 155 } |
179 | 156 |
180 /// | 157 /// |
181 // Draw a triangle using the shader pair created in Init() | 158 // Draw a triangle using the shader pair created in Init() |
182 // | 159 // |
183 #define VTX_POS_SIZE 4 | 160 #define VTX_POS_SIZE 4 |
184 #define VTX_TEX_SIZE 2 | 161 #define VTX_TEX_SIZE 2 |
185 #define VTX_STRIDE (6 * sizeof(GLfloat)) | 162 #define VTX_STRIDE (6 * sizeof(GLfloat)) |
186 void Draw ( ESContext *esContext ) | 163 void twDraw ( ESContext *esContext ) |
187 { | 164 { |
188 UserData *userData = esContext->userData; | 165 TWUserData *userData = esContext->userData; |
189 GLuint offset = 0; | 166 GLuint offset = 0; |
190 | 167 |
191 // Set the viewport | 168 // Set the viewport |
192 glViewport ( 0, 0, esContext->width, esContext->height ); | 169 glViewport ( 0, 0, esContext->width, esContext->height ); |
193 | 170 |
194 // Clear the color buffer | 171 // Clear the color buffer |
195 glClear ( GL_COLOR_BUFFER_BIT ); | 172 glClear ( GL_COLOR_BUFFER_BIT ); |
196 | 173 |
197 // Use the program object | 174 // Use the program object |
198 glUseProgram ( userData->programObject ); | 175 glUseProgram ( userData->programObject ); |
(...skipping 26 matching lines...) Expand all Loading... |
225 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); | 202 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); |
226 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); | 203 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); |
227 glUniform1f ( userData->offsetLoc, 0.0f ); | 204 glUniform1f ( userData->offsetLoc, 0.0f ); |
228 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); | 205 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); |
229 | 206 |
230 // Draw quad with mirrored repeat | 207 // Draw quad with mirrored repeat |
231 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT ); | 208 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT ); |
232 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT ); | 209 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT ); |
233 glUniform1f ( userData->offsetLoc, 0.7f ); | 210 glUniform1f ( userData->offsetLoc, 0.7f ); |
234 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); | 211 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); |
235 | |
236 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); | |
237 } | 212 } |
238 | 213 |
239 /// | 214 /// |
240 // Cleanup | 215 // Cleanup |
241 // | 216 // |
242 void ShutDown ( ESContext *esContext ) | 217 void twShutDown ( ESContext *esContext ) |
243 { | 218 { |
244 UserData *userData = esContext->userData; | 219 TWUserData *userData = esContext->userData; |
245 | 220 |
246 // Delete texture object | 221 // Delete texture object |
247 glDeleteTextures ( 1, &userData->textureId ); | 222 glDeleteTextures ( 1, &userData->textureId ); |
248 | 223 |
249 // Delete program object | 224 // Delete program object |
250 glDeleteProgram ( userData->programObject ); | 225 glDeleteProgram ( userData->programObject ); |
251 | 226 |
252 // Delete vertex buffer objects | 227 // Delete vertex buffer objects |
253 glDeleteBuffers ( 2, userData->vboIds ); | 228 glDeleteBuffers ( 2, userData->vboIds ); |
254 } | 229 } |
255 | |
256 | |
257 int main ( int argc, char *argv[] ) | |
258 { | |
259 ESContext esContext; | |
260 UserData userData; | |
261 | |
262 esInitContext ( &esContext ); | |
263 esContext.userData = &userData; | |
264 | |
265 esCreateWindow ( &esContext, "MipMap 2D", 640, 480, ES_WINDOW_RGB ); | |
266 | |
267 if ( !Init ( &esContext ) ) | |
268 return 0; | |
269 | |
270 esRegisterDrawFunc ( &esContext, Draw ); | |
271 | |
272 esMainLoop ( &esContext ); | |
273 | |
274 ShutDown ( &esContext ); | |
275 } | |
OLD | NEW |