Pro OpenGL ES for iOS

(singke) #1

144 CHAPTER 5: Textures^


This can now be initialized from viewDidLoad() using the following:
[EAGLContext setCurrentContext:self.context];
m_Texture=[self loadTexture:@"hedly.png"];
The two texture parameters specify how to handle repeating textures, covered below.
My image, hedly.png, is the photo of one of the mysterious huge stone heads on Easter
Island in the Pacific. For ease of testing, use a power-of-two (POT) image, 32 bits,
RGBA.

Note By default, OpenGL requires each row of texels in the image data to be aligned on a 4-
byte boundary. Our RGBA textures adhere to that; for other formats, consider using the call
glPixelStorei(GL_PACK_ALIGNMENT,x), where x can be 1, 2, 4, or 8 bytes for
alignment. Use 1 to cover all cases.

Note that there is usually a size limitation for textures, which depends on the actual
graphics hardware used. On both the first- and second-generation iPhones (the original
and 3G) and iPod/Touch devices, textures were limited to no larger than 1024 ̄ 1024
because of using the Power VR MBX platform. On all others, the newer PowerVR SGX is
used, which doubles the max size of textures to 2048 ̄2048. You can find out how big a
texture a particular platform can use by calling the following, where maxSize is an
integer, and then compensate at runtime:
glGetIntegerv(GL_MAX_TEXTURE_SIZE,&maxSize);
Now change the drawInRect() routine, as shown in Listing 5-2. Most of this you have
seen before, with the new stuff detailed below. And while you’re at it, go ahead and add
GLKTextureInfo *m_Texture to the header.
Listing 5-2. Render the geometry with the texture


  • (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
    {


static const GLfloat squareVertices[] =
{
-0.5f, -0.33f,
0.5f, -0.33f,
-0.5f, 0.33f,
0.5f, 0.33f,
};

static const GLubyte squareColors[] = {
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
};
Free download pdf