View Full Version : Texture dimensions

07-16-2008, 02:30 AM
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) ?

07-16-2008, 03:02 AM
Because it is a hardware restriction.

07-16-2008, 05:26 AM
Errr i'm trying to develop an openglES on my iphone (no trolling on the SDK, i know it's forbidden...)
And still, is it a hardware restriction ?

07-16-2008, 09:12 AM
GL ES 1.1 requires textures to be power of two (look in the spec, section 3.7).

07-16-2008, 02:10 PM
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,1*y/y2 1*x/x2,1*y/y2 1*x/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 (17*17 -> 32*32)

07-17-2008, 12:40 AM
Et roulez jeunesse !!! :D

If borders are not supported he will always be able to use GL_CLAMP_TO_EDGE as texture wrapping parameter...unless this is also not supported by ES.

07-17-2008, 03:24 AM
whoa guys you rock !
I'm lucky, i have GL_CLAMP_TO_EDGE

Thanks a lot !