Non-power-of-two textures with TextureIO

Hey guys,

what is a safe way to load in non-power of two textures, so that it will work on any machine?

The funny thing about my laptop is, when running my application on linux(ubuntu 8.10) I can use non power of two textures without problems, but when running on windows xp, it goes all wrong and rounds up to the nearest power of two for my textures.

This bothers mer quite some, and yes my drivers are updated in windows.

http://mr.myogl.org/ says that I dont have the non_power_of_two ext thing, and not the rectangle ext either. Here is the link: http://www.myogl.org/?target=show&id=150

Can someone help me?

Create the next-largest power-of-two texture and load your image into that - then adjust your texture coordinates to match.

For example, say I have a 5x3 image that I want to use as a texture: I create a 8x4 OpenGL texture object, load my image data, and then use texture coordinates (0,0), (5/8,0), (0,3/4) and (5/8,3/4) to address the bottom left, top left, bottom right and top right corners respectively.

The next step is to notice that this introduces some unused space in each texture object, consuming VRAM. However, it’s only wasted if you don’t put anything else in there - pack as many images as you can into each texture object.

The step after that is to notice that you can reduce expensive texture binding calls by sorting your rendering operations so that things using textures that are resident in the same OpenGL texture object are drawn together.

I think that it is best to separate square textures from rectangular textures because there are some significant differences with how they’re handled. If you’re just using square non-power-of-two textures, bleb’s suggestions will work fine.

However, when using rectangles, texture coordinates are treated differently. They go from 0 to width/height instead of 0 to 1. It is possible to correctly normalize these coordinates in the event that rectangular textures aren’t supported. Another concern is that when using texture rectangles, you can’t mipmap them or use certain clamp modes for the coordinates.

My advice is to only use square textures (preferably of power-of-two dimensions since performance can be better) and pack as many images inside them as possible to not waste space. I would only use the rectangular textures for fullscreen effects where a rectangle would be truly useful.

This is incorrect. Vanilla texture objects do not have to be square, they just have to have power-of-two dimensions i.e. they have dimensions 2m by 2n, where m does not necessarily equal n

Thanks, I was not aware of that. In either case, though, the actual TEXTURE_RECT_ARB target should IMO be treated separately at a high level. I feel it’s dangerous that the TextureIO utilities will choose a target even though the programmer may not be aware of the consequences.

Ok, it seems it might be easiest to just make sure my textures are in power of two dimensions, rather than loading rectangular textures in at this point. It is a solo project, so I might just accept the empty space in my textures rather than trying to fit several spritesheets in one. Currently I have 300+ fps in my game, so it won’t be a problem. This is on a very old machine to boot, so other optimizations will only interest me later if at all.

It’s very, very easy to pack textures - look here for a handy rectangle-packing class. Just remember to specify a border of 1 when you construct the packer to avoid adjacent textures blending into each other.
It probably won’t make any difference to performance, but it will give you a warm feeling of efficiency-increasing satisfaction inside.

Ahh yeah, good old knap-sack huh? :slight_smile: Right now it’s wasted work for me, but thanks I will look at it if i find the need :smiley: