2
// Book: OpenGL(R) ES 2.0 Programming Guide
3
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
5
// ISBN-13: 9780321502797
6
// Publisher: Addison-Wesley Professional
7
// URLs: http://safari.informit.com/9780321563835
8
// http://www.opengles-book.com
13
// This is an example that demonstrates the three texture
14
// wrap modes available on 2D textures
21
// Handle to a program object
24
// Attribute locations
40
// Generate an RGB8 checkerboard image
42
GLubyte* GenCheckImage( int width, int height, int checkSize )
46
GLubyte *pixels = malloc( width * height * 3 );
51
for ( y = 0; y < height; y++ )
52
for ( x = 0; x < width; x++ )
57
if ( ( x / checkSize ) % 2 == 0 )
59
rColor = 255 * ( ( y / checkSize ) % 2 );
60
bColor = 255 * ( 1 - ( ( y / checkSize ) % 2 ) );
64
bColor = 255 * ( ( y / checkSize ) % 2 );
65
rColor = 255 * ( 1 - ( ( y / checkSize ) % 2 ) );
68
pixels[(y * height + x) * 3] = rColor;
69
pixels[(y * height + x) * 3 + 1] = 0;
70
pixels[(y * height + x) * 3 + 2] = bColor;
77
// Create a mipmapped 2D texture image
79
GLuint CreateTexture2D( )
81
// Texture object handle
87
pixels = GenCheckImage( width, height, 64 );
91
// Generate a texture object
92
glGenTextures ( 1, &textureId );
94
// Bind the texture object
95
glBindTexture ( GL_TEXTURE_2D, textureId );
97
// Load mipmap level 0
98
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height,
99
0, GL_RGB, GL_UNSIGNED_BYTE, pixels );
101
// Set the filtering mode
102
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
103
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
111
// Initialize the shader and program object
113
int Init ( ESContext *esContext )
115
esContext->userData = malloc(sizeof(UserData));
117
UserData *userData = esContext->userData;
118
GLbyte vShaderStr[] =
119
"uniform float u_offset; \n"
120
"attribute vec4 a_position; \n"
121
"attribute vec2 a_texCoord; \n"
122
"varying vec2 v_texCoord; \n"
125
" gl_Position = a_position; \n"
126
" gl_Position.x += u_offset;\n"
127
" v_texCoord = a_texCoord; \n"
130
GLbyte fShaderStr[] =
131
"precision mediump float; \n"
132
"varying vec2 v_texCoord; \n"
133
"uniform sampler2D s_texture; \n"
136
" gl_FragColor = texture2D( s_texture, v_texCoord );\n"
139
// Load the shaders and get a linked program object
140
userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
142
// Get the attribute locations
143
userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
144
userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" );
146
// Get the sampler location
147
userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
149
// Get the offset location
150
userData->offsetLoc = glGetUniformLocation( userData->programObject, "u_offset" );
153
userData->textureId = CreateTexture2D ();
155
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
160
// Draw a triangle using the shader pair created in Init()
162
void Draw ( ESContext *esContext )
164
UserData *userData = esContext->userData;
165
GLfloat vVertices[] = { -0.3f, 0.3f, 0.0f, 1.0f, // Position 0
166
-1.0f, -1.0f, // TexCoord 0
167
-0.3f, -0.3f, 0.0f, 1.0f, // Position 1
168
-1.0f, 2.0f, // TexCoord 1
169
0.3f, -0.3f, 0.0f, 1.0f, // Position 2
170
2.0f, 2.0f, // TexCoord 2
171
0.3f, 0.3f, 0.0f, 1.0f, // Position 3
172
2.0f, -1.0f // TexCoord 3
174
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
177
glViewport ( 0, 0, esContext->width, esContext->height );
179
// Clear the color buffer
180
glClear ( GL_COLOR_BUFFER_BIT );
182
// Use the program object
183
glUseProgram ( userData->programObject );
185
// Load the vertex position
186
glVertexAttribPointer ( userData->positionLoc, 4, GL_FLOAT,
187
GL_FALSE, 6 * sizeof(GLfloat), vVertices );
188
// Load the texture coordinate
189
glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
190
GL_FALSE, 6 * sizeof(GLfloat), &vVertices[4] );
192
glEnableVertexAttribArray ( userData->positionLoc );
193
glEnableVertexAttribArray ( userData->texCoordLoc );
196
glActiveTexture ( GL_TEXTURE0 );
197
glBindTexture ( GL_TEXTURE_2D, userData->textureId );
199
// Set the sampler texture unit to 0
200
glUniform1i ( userData->samplerLoc, 0 );
202
// Draw quad with repeat wrap mode
203
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
204
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
205
glUniform1f ( userData->offsetLoc, -0.7f );
206
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
208
// Draw quad with clamp to edge wrap mode
209
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
210
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
211
glUniform1f ( userData->offsetLoc, 0.0f );
212
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
214
// Draw quad with mirrored repeat
215
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
216
glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
217
glUniform1f ( userData->offsetLoc, 0.7f );
218
glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
225
void ShutDown ( ESContext *esContext )
227
UserData *userData = esContext->userData;
229
// Delete texture object
230
glDeleteTextures ( 1, &userData->textureId );
232
// Delete program object
233
glDeleteProgram ( userData->programObject );
235
free ( esContext->userData);
238
int main ( int argc, char *argv[] )
243
esInitContext ( &esContext );
244
esContext.userData = &userData;
246
esCreateWindow ( &esContext, "MipMap 2D", 320, 240, ES_WINDOW_RGB );
248
if ( !Init ( &esContext ) )
251
esRegisterDrawFunc ( &esContext, Draw );
253
esMainLoop ( &esContext );
255
ShutDown ( &esContext );