146 CHAPTER 5: Textures^
The blend function determines how the source and destination
pixels/fragments are mixed together. The most common form is where
the source overwrites the destination, but others can create some
interesting effects. Because this is such a large topic, it deserves its
own chapter, which as it turns out is Chapter 6.
Line 5 ensures that the texture we want is the current one. Like the
other OpenGL objects, textures are assigned a ‘‘name,’’ (a unique
integer ID number), which will be referenced until it’s deleted.
Line 6 is where the texture coordinates are handed off to the
hardware.
And just as you had to tell the client to handle the colors and vertices,
you need to do the same for the texture coordinates here in line 7.
Line 8 you’ll recognize, but this time besides drawing the colors and
the geometry, it now takes the information from the current texture,
matches up the four texture coordinates to the four corners specified
by the squareVertices[] array (each vertex of the textured object
needs to have a texture coordinate assigned to it), and blends it using
the values specified in line 4.
Finally, disable the client state for texture, line 9, the same way it was
disabled for color and vertices.
If everything works right, you should see something like Figure 5-13a. You don’t you
say? It’s upside down? Depending on the format used, your texture could very well be
inverted, with its internal origin in the upper-left corner instead of the lower left. The fix is
easy for this. Change loadTexture to look like:
-(GLKTextureInfo *)loadTexture:(NSString *)filename
{
NSError *error;
GLKTextureInfo *info;
NSDictionary *options=[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
GLKTextureLoaderOriginBottomLeft,nil];
NSString *path=[[NSBundle mainBundle]pathForResource:filename ofType:NULL];
info=[GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
glBindTexture(GL_TEXTURE_2D, info.name);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
return info;
}
What you’re telling the loader to do is to flip the origin of the texture to anchor it at the
bottom left of the screen. Now does it look like 5-13 (left)?