Loading non-power-of-two images in OpenGL ES iPhone
Does anyone know an efficient way to load textures into OpenGL ES that are not in sizes of a power of two? I am new to OpenGL, and I'm working on a 2D game for iPhone and I have a l开发者_如何转开发ot of textures already made. It would be very tedious job to go back and resize all of my textures to a power of two.
For performance reasons, it's best to putt all your sprites into an atlas. An atlas is a large texture, that contains all your sprites. There are tools to automate this process. For example TexturePacker: http://www.texturepacker.com/
Depending on which technology you're using, you might have to parse the output from texture packer to get the UV-Offsets.
unsigned int NextPOT(unsigned int x)
{
x = x - 1;
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >>16);
return x + 1;
}
unsigned int width = CGImageGetWidth(image.CGImage);
unsigned int height = CGImageGetHeight(image.CGImage);
unsigned int Width_POT = NextPOT(width);
unsigned int Height_POT = NextPOT(height);
CGRect Rect = CGRectMake(0,0,width, height);
CGSize size = CGSizeMake(Width_POT, Height_POT);
UIGraphicsBeginImageContext(size);
[image drawInRect:Rect];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
image is the source image which size is non pow of 2, result is the image you can pass to OpenGL
精彩评论