Sign in to follow this  

power of 2 textures limitation. How to bypass it?

This topic is 4686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

quote from Nehe's tutorial 6
Quote:
The image height and width MUST be a power of 2. The width and height must be at least 64 pixels, and for compatability reasons, shouldn't be more than 256 pixels. If the image you want to use is not 64, 128 or 256 pixels on the width or height, resize it in an art program. There are ways around this limitation, but for now we'll just stick to standard texture sizes
I am exploring the idea of making a simple picture viewer with a openGL surface in 2d ortho projection... and would like to be able to load pictures of any given size. I couldnt find anything about the "ways around this limitation" he's speaking of by googling around so could anyone pass me a few pointers? thanks

Share this post


Link to post
Share on other sites
If by chance you don't need to repeat the texture, you can simply reserve a bigger texture (say for a 640x480 texture you reserve 1024x512) and use a scale factor into the texture matrix so that the [0,1] range maps to the correct sub-texture range.

Share this post


Link to post
Share on other sites
even easier is just gluBuild2DMipmaps(). I havent checked this for sure but I think that this function resizes to powers of two before it passes the data to OpenGL

see lesson 7

Share this post


Link to post
Share on other sites
I always thought it did, or is it something that was supposed to work in that way but just doesn't? To quote from The Blue Book, Chapter 6: "If the dimensions of the input image are not powers of two, then the image is scaled so that both the width and height are powers of two before the mipmaps are generated.".

Not wanting to be cocky here, just want some clarification :)

Share this post


Link to post
Share on other sites
Quote:
Original post by eSCHEn
I always thought it did, or is it something that was supposed to work in that way but just doesn't? To quote from The Blue Book, Chapter 6: "If the dimensions of the input image are not powers of two, then the image is scaled so that both the width and height are powers of two before the mipmaps are generated.".

Not wanting to be cocky here, just want some clarification :)


You're correct: gluBuild2DMipmaps() calls gluScaleImage() if the image is not power of 2...in accordance with the prophecies, er, specs.

Share this post


Link to post
Share on other sites
Another solution is to have the code resize the textures for you, before generating the the OpenGL texture.

Load the image and the get its dimensions (lWidthPixels, lHeightPixels). Then, get the max texture dimensions supported by OpenGL. Then, calculate:


// Resize Image To Closest Power Of Two
if (lWidthPixels <= glMaxTexDim) // Is Image Width Less Than Or Equal To Cards Limit
lWidthPixels = 1 << (int)floor((log((double)lWidthPixels)/log(2.0f)) + 0.5f);
else // Otherwise Set Width To "Max Power Of Two" That The Card Can Handle
lWidthPixels = 1 << (int)floor((log((double)glMaxTexDim)/log(2.0f)) + 0.5f);

if (lHeightPixels <= glMaxTexDim) // Is Image Height Greater Than Cards Limit
lHeightPixels = 1 << (int)floor((log((double)lHeightPixels)/log(2.0f)) + 0.5f);
else // Otherwise Set Height To "Max Power Of Two" That The Card Can Handle
lHeightPixels = 1 << (int)floor((log((double)glMaxTexDim)/log(2.0f)) + 0.5f);


Then you create a temporary bitmap of that size, blit your loading image onto it, and convert that bitmap to the OpenGL texture. A few hoops to jump through, but it works without any OpenGL extensions.

Nehe's IPicture basecode does exactly this. Download http://nehe.gamedev.net/counter.asp?file=files/misc/nehe_ipicture.zip and see the function BuildTexture in the file NeHe_IPicture.cpp

Note Nehe's code uses Windows-only stuff, so if you're using something else, you'll have to rewrite some of it.

Brian

Share this post


Link to post
Share on other sites

This topic is 4686 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this