Click here to Skip to main content
13,559,455 members
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 19 Jul 2009
Licenced CPOL

OpenGL ES: Texture2D and the Power of Two

, 19 Jul 2009
Rate this:
Please Sign up or sign in to vote.
How to adjust your Texture2D object to allow any sized textures in your OpenGL ES project.


As most of you have figured out, OpenGL ES doesn't like textures to be non-power of two, and I really didn't want to have to force all of my textures to be powers of two before loading them into my game. This presented me with a problem that had to be solved in-code.

To really understand texture mapping in OpenGL, I had to do a lot of research. To be fair, I still don't fully understand it, although there are a few articles that do a really great job at explaining things. One of those articles is titled "OpenGL ES from the ground up" and can be located at Look up chapter 6 on Texture Mapping.

The problem that I was faced with, is that only powers of two can be used inside OpenGL ES, so how do you convert our texture to a power of two if it was loaded in as a 40 x 40 texture (for example)? There are several ways of doing just this, but I settled with the way Apple did it for their Texture objects, and moulded that to fit my own code. They came up with the following to convert the width and height:

//Adjust the width and height to be a power of two.
if(( _width != 1) && ( _width & (_width - 1) )
    i = 1;
    while( (sizeToFit ? 2 * i : i) < _width)
        i *= 2;
    _width = i;

if(( _height != 1) && ( _height & (_height - 1) )
    i = 1;
    while( (sizeToFit ? 2 * i : i) < _height)
        i *= 2;
    _height = i; 

The first portion of the code will convert the width to a power of two, then the height. Since the texture will be stretched, and never shrunk, Apple had to figure out a way to shrink the texture if it became longer than the max texture size defined by kMaxTextureSize = 1024. The following code will do just that:

//      scale down an image greater than the max texture size
while((_width > kMaxTextureSize) || (_height > kMaxTextureSize))
    _width /= 2;
    _height /= 2;
    transform = CGAffineTransformScale(transform, 0.5, 0.5);
    imageSize.x *= 0.5;
    imageSize.y *= 0.5; 

The "transform" is nothing more than a CGAffineTransform (Core Graphics). So, now that you've glanced over the code, you remember in the article where Jeff was talking about S and T coordinates? We'll take care of those like this:

_maxS = imageSize.x / (float)_width;
_maxT = imageSize.y / (float)_height;

This will give us a number between 0 and 1 which can be used in our textureCoordinates[] for drawing. This should only render the portion of the texture that has the data we wish to make it to the screen, and not the padding we added to ensure it was a power of two.

While I am still learning OpenGL, I figured that a lot of people would be interested in the following solution... especially if you had done what I had, and thrown out the Apples Texture2D class while working on iPhone OpenGL applications. The full source code to the Texture2D, TextureManager, and any other class used by my C++ iPhone Engine can be found at

Hope this was informative! If anyone has any further information that would be useful, feel free to comment!

Happy coding everyone!


  • 19th July, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Craig Giles
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionWhy not using Texture atlas instead? Pin
Simone Serponi11-Oct-09 23:11
memberSimone Serponi11-Oct-09 23:11 
GeneralFaster way to find next power of two Pin
JeanLuc_19-Jul-09 12:18
memberJeanLuc_19-Jul-09 12:18 
GeneralRe: Faster way to find next power of two Pin
Craig Giles19-Jul-09 16:47
memberCraig Giles19-Jul-09 16:47 
GeneralRe: Faster way to find next power of two Pin
cmk19-Jul-09 18:33
membercmk19-Jul-09 18:33 
GeneralRe: Faster way to find next power of two Pin
yarp19-Jul-09 23:11
memberyarp19-Jul-09 23:11 
Thanks, I got inspired by this.
Here's my 2 cts about Power of 2 related functions:
// Is a power of 2
inline bool FIsPow2(int n)
  if (n<=0)  return false;
  return (n & (n-1))==0;

// Next power of 2
inline int FNextPow2(int n)
	n |= n>>1;
	n |= n>>2;
	n |= n>>4;
	n |= n>>8;
	n |= n>>16;
	return n;

// Previous power of 2
inline int FPrevPow2(int n)
  int n2 = n>>1;  // divide by 2
  if (n2>0)
    return FNextPow2(n2);
    return FNextPow2(n);

// Nearest power of 2
inline int FNearPow2(int n)
  if (FIsPow2(n))  return n;
  int nNext = FNextPow2(n);
  int nPrev = FPrevPow2(n);
  if (nNext-n>abs(n-nPrev))
    return nPrev;
    return nNext;


General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01-2016 | 2.8.180515.1 | Last Updated 19 Jul 2009
Article Copyright 2009 by Craig Giles
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid