I took a sample code from apple to make a texture loader with openglES…
Here it is :
// Creates a Core Graphics image from an image file
spriteImage = [UIImage imageNamed:@"Sprite.png"].CGImage;
// Get the width and height of the image
width = CGImageGetWidth(spriteImage);
height = CGImageGetHeight(spriteImage);
// Texture dimensions must be a power of 2. If you write an application that allows users to supply an image,
// you'll want to add code that checks the dimensions and takes appropriate action if they are not a power of 2.
Why the heck textures dimensions should be power of 2 ??
Does anyone know another way to load textures (which size is not obviously power of 2) ?
Does anyone know another way to load textures (which size is not obviously power of 2) ?
Find the smallest power of 2 testure that still contains the non-power-of-two (NPOT) texture. Then adjust texture coordinates accordingly.
Quick example :
NPOT original texture is x=11 ; y=34
POT texture should be x2 = 2^ceil(log2(x)) = 16 ; y2 = 2^ceil(log2(x)) = 64
The remaining texel data should be set to black, as it is the default border color (wait, does GL ES have texture borders ? EDIT: not in ES 1.1). It works well for non-repeating textures, but if you want a repeating texture, a pixel shader is probably needed (ES 2.0) …
NPOT original texcoords = 0,0 0,1 1,1 1,0
POT adapted texcoords = 0,0 0,1y/y2 1x/x2,1y/y2 1x/x2,0
Et roulez jeunesse !!!
The main drawback is that some texture memory is wasted. The worst case is you need almost 4 times more texture space (1717 -> 3232)