Texture dimensions

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) ?

Because it is a hardware restriction.

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 ?

GL ES 1.1 requires textures to be power of two (look in the spec, section 3.7).

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)

Et roulez jeunesse !!!

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.

whoa guys you rock !
I’m lucky, i have GL_CLAMP_TO_EDGE

Thanks a lot !

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.