CHAPTER 5: Textures (^143)
Also a format requirement of sorts is that, generally, OpenGL can use only texture
images that are power-of-two on a side. Some systems can get around that, such as
iOS with certain limitations, but for the time being, just stick with the standard.
So, with all of this stuff out of the way, it’s time to start coding.
Back to the Bouncy Square One
Let’s take a step back and fetch the generic bouncy square again example again, which
we first worked on in Chapter 3. We’ll apply a texture to it and then manipulate it to
show off some of the tricks detailed in this chapter, such as repeating, animating, and
distortion.
Previous to iOS 5, programmers needed to create their own texture conversion code or
use code supplied by Apple, which took nearly 40 lines of Core Graphics to convert a
.png to OpenGL compatible format. Now we have two nice shiney new toys to play with
called GLKTexture, and GLKTextureInfo.
In your view controller add Listing 5-1.
Listing 5-1. Loading and converting a texture to OpenGL format.
-(GLKTextureInfo )loadTexture:(NSString )filename
{
NSError error;
GLKTextureInfo info;
NSString *path=[[NSBundle mainBundle]pathForResource:filename ofType:NULL];
info=[GLKTextureLoader textureWithContentsOfFile:path options:NULL error:&error];
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
return info;
}
Table 5-2. All the GL_TEXTURE Parameters for glTexParameter* Calls in OpenGL ES 1.1
Name Purpose
GL_TEXTURE_MIN_FILTER Sets the minification type (see Table 5-3)
GL_TEXTURE_MAG_FILTER Sets the magnification type (see Table 5-4)
GL_TEXTURE_WRAP_S Specifies how textures are to be wrapped in the S direction,
GL_CLAMP or GL_REPEAT
GL_TEXTURE_WRAP_T Specifies how textures are to be wrapped in the T direction,
GL_CLAMP or GL_REPEAT