1// 2// Book: OpenGL(R) ES 2.0 Programming Guide 3// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner 4// ISBN-10: 0321502795 5// ISBN-13: 9780321502797 6// Publisher: Addison-Wesley Professional 7// URLs: http://safari.informit.com/9780321563835 8// http://www.opengles-book.com 9// 10 11// TextureWrap.c 12// 13// This is an example that demonstrates the three texture 14// wrap modes available on 2D textures 15// 16#include <stdlib.h> 17#include "esUtil.h" 18 19typedef 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/// 40// Generate an RGB8 checkerboard image 41// 42GLubyte* GenCheckImage( int width, int height, int checkSize ) 43{ 44 int x, 45 y; 46 GLubyte *pixels = malloc( width * height * 3 ); 47 48 if ( pixels == NULL ) 49 return NULL; 50 51 for ( y = 0; y < height; y++ ) 52 for ( x = 0; x < width; x++ ) 53 { 54 GLubyte rColor = 0; 55 GLubyte bColor = 0; 56 57 if ( ( x / checkSize ) % 2 == 0 ) 58 { 59 rColor = 255 * ( ( y / checkSize ) % 2 ); 60 bColor = 255 * ( 1 - ( ( y / checkSize ) % 2 ) ); 61 } 62 else 63 { 64 bColor = 255 * ( ( y / checkSize ) % 2 ); 65 rColor = 255 * ( 1 - ( ( y / checkSize ) % 2 ) ); 66 } 67 68 pixels[(y * height + x) * 3] = rColor; 69 pixels[(y * height + x) * 3 + 1] = 0; 70 pixels[(y * height + x) * 3 + 2] = bColor; 71 } 72 73 return pixels; 74} 75 76/// 77// Create a mipmapped 2D texture image 78// 79GLuint CreateTexture2D( ) 80{ 81 // Texture object handle 82 GLuint textureId; 83 int width = 256, 84 height = 256; 85 GLubyte *pixels; 86 87 pixels = GenCheckImage( width, height, 64 ); 88 if ( pixels == NULL ) 89 return 0; 90 91 // Generate a texture object 92 glGenTextures ( 1, &textureId ); 93 94 // Bind the texture object 95 glBindTexture ( GL_TEXTURE_2D, textureId ); 96 97 // Load mipmap level 0 98 glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height, 99 0, GL_RGB, GL_UNSIGNED_BYTE, pixels ); 100 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 ); 104 105 return textureId; 106 107} 108 109 110/// 111// Initialize the shader and program object 112// 113int Init ( ESContext *esContext ) 114{ 115 UserData *userData = esContext->userData; 116 GLbyte vShaderStr[] = 117 "uniform float u_offset; \n" 118 "attribute vec4 a_position; \n" 119 "attribute vec2 a_texCoord; \n" 120 "varying vec2 v_texCoord; \n" 121 "void main() \n" 122 "{ \n" 123 " gl_Position = a_position; \n" 124 " gl_Position.x += u_offset;\n" 125 " v_texCoord = a_texCoord; \n" 126 "} \n"; 127 128 GLbyte fShaderStr[] = 129 "precision mediump float; \n" 130 "varying vec2 v_texCoord; \n" 131 "uniform sampler2D s_texture; \n" 132 "void main() \n" 133 "{ \n" 134 " gl_FragColor = texture2D( s_texture, v_texCoord );\n" 135 "} \n"; 136 137 // Load the shaders and get a linked program object 138 userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); 139 140 // Get the attribute locations 141 userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); 142 userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" ); 143 144 // Get the sampler location 145 userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" ); 146 147 // Get the offset location 148 userData->offsetLoc = glGetUniformLocation( userData->programObject, "u_offset" ); 149 150 // Load the texture 151 userData->textureId = CreateTexture2D (); 152 153 glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); 154 return TRUE; 155} 156 157/// 158// Draw a triangle using the shader pair created in Init() 159// 160void Draw ( ESContext *esContext ) 161{ 162 UserData *userData = esContext->userData; 163 GLfloat vVertices[] = { -0.3f, 0.3f, 0.0f, 1.0f, // Position 0 164 -1.0f, -1.0f, // TexCoord 0 165 -0.3f, -0.3f, 0.0f, 1.0f, // Position 1 166 -1.0f, 2.0f, // TexCoord 1 167 0.3f, -0.3f, 0.0f, 1.0f, // Position 2 168 2.0f, 2.0f, // TexCoord 2 169 0.3f, 0.3f, 0.0f, 1.0f, // Position 3 170 2.0f, -1.0f // TexCoord 3 171 }; 172 GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; 173 174 // Set the viewport 175 glViewport ( 0, 0, esContext->width, esContext->height ); 176 177 // Clear the color buffer 178 glClear ( GL_COLOR_BUFFER_BIT ); 179 180 // Use the program object 181 glUseProgram ( userData->programObject ); 182 183 // Load the vertex position 184 glVertexAttribPointer ( userData->positionLoc, 4, GL_FLOAT, 185 GL_FALSE, 6 * sizeof(GLfloat), vVertices ); 186 // Load the texture coordinate 187 glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT, 188 GL_FALSE, 6 * sizeof(GLfloat), &vVertices[4] ); 189 190 glEnableVertexAttribArray ( userData->positionLoc ); 191 glEnableVertexAttribArray ( userData->texCoordLoc ); 192 193 // Bind the texture 194 glActiveTexture ( GL_TEXTURE0 ); 195 glBindTexture ( GL_TEXTURE_2D, userData->textureId ); 196 197 // Set the sampler texture unit to 0 198 glUniform1i ( userData->samplerLoc, 0 ); 199 200 // Draw quad with repeat wrap mode 201 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); 202 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); 203 glUniform1f ( userData->offsetLoc, -0.7f ); 204 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); 205 206 // Draw quad with clamp to edge wrap mode 207 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); 208 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); 209 glUniform1f ( userData->offsetLoc, 0.0f ); 210 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); 211 212 // Draw quad with mirrored repeat 213 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT ); 214 glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT ); 215 glUniform1f ( userData->offsetLoc, 0.7f ); 216 glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); 217 218 eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); 219} 220 221/// 222// Cleanup 223// 224void ShutDown ( ESContext *esContext ) 225{ 226 UserData *userData = esContext->userData; 227 228 // Delete texture object 229 glDeleteTextures ( 1, &userData->textureId ); 230 231 // Delete program object 232 glDeleteProgram ( userData->programObject ); 233} 234 235 236int main ( int argc, char *argv[] ) 237{ 238 ESContext esContext; 239 UserData userData; 240 241 esInitContext ( &esContext ); 242 esContext.userData = &userData; 243 244 esCreateWindow ( &esContext, TEXT("Texture Wrap"), 640, 480, ES_WINDOW_RGB ); 245 246 if ( !Init ( &esContext ) ) 247 return 0; 248 249 esRegisterDrawFunc ( &esContext, Draw ); 250 251 esMainLoop ( &esContext ); 252 253 ShutDown ( &esContext ); 254} 255