Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.

Clash Rocker

[SDL-GL]Work arounds for non power of 2 texture

This topic is 5952 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

Hello all, I''m writing a 2D SDL platform game, and just the other day I decided to play around with OpenGL. I like the look of having my SDL surfaces converted to an OpenGL texture using Bilinear filtering. But I have run into a problem, most of 2D surfaces are not n^2 I''ve just been resizing my bitmaps in an image editor, but I don''t like the look of the distorting when I resize. So now I''m wondering on a way to keep my bitmaps there true size but still use OpenGL textures. What would be a good way to do this? I have one idea: Divide my 2D surface into 4(or whatever) 256x256 textures and then map them onto a quad...is this a good idea? Are there better ways?

Share this post

Link to post
Share on other sites
I haven''t used OpenGL textures in SDL, but in OpenGL (or D3D) itself you pretty much have two options:

Make the graphic a power of 2 sized image, but larger than you need and use texture clamping, the texture matrix or alpha to mask out the part you don''t want to display.

For example, say your graphic is 190x190 without being altered, so you''d put it into a 256x256 graphic without altering it, and the 66 pixels on either side that are unused by your graphic, you''d mask out using one of the above techniques.

The other is to make your image a mosaic of smaller textures, which is pretty much what you suggested yourself (you''d be better of mapping each texture to its own quad rather than trying to map all four onto one though because most people''s videocards can''t map 4 textures onto a quad in a single pass still). The big problem with this method over the above one is that you lose the ability to easily rotate the quad (and thus the texture on it) in an easy hardware accelerated manner.

Share this post

Link to post
Share on other sites
I think most people who have difficulty with the "my image size
is not a power of 2" think that you must use the
whole texture if you fit it into the next power of 2 size.

Such is not the case. For example, say your image is 96x96.
No problem. Load it up to a 128x128 texture object. When
you define your texture coordinates, just use the 0.0 - 0.75
range (say you put the image to the bottom-left corner).

The rest of the memory is unused. If you think that is
wasteful, you can "stuff" other smaller images into that
space. It''s up to you.

Stretching the image to fit power of 2 ends up using the
same amount of memory, more disk space, and a (badly)
distorted image. I don''t recommend it.

Kami no Itte ga ore ni zettai naru!

Share this post

Link to post
Share on other sites
One other thing..As far as which method you should use for masking if you use the bigger-texture method, it kinda depends on your graphics (how many of them there will be in memory), etc, so you may want to test multiple methods against your data set.

Using the texture matrix is the most flexible because you can write a texture manager which will put lots of your graphics into one large texture, using some basic fitting algorithm to try and avoid too much vram wastage. For example you might make a 512x512 texture and put a 190x190 image on it, plus another 190x190 image, plus a 60x60, tiling the images whereever you have space to do so (and keeping track of where they are in texture space so you can set the texture matrix appropriately when it comes time to use the image you want.

The drawback of this method, though, is most drivers optimize under the assumption that the texture matrix will be set to its default and not changed. Changing the texture matrix will cause them to fallback on an unoptimized path that could slow you down quite a bit depending upon the card/driver. So if you aren''t using a ton of 2D graphics you might be better off using alphablending to do the masking and just wasting some more vram for each image. Again, its best to try with a couple different popular video cards from different manufacturers with your data set, if you can.

Share this post

Link to post
Share on other sites
Hey thanks for the feedback guys :-)

Now my original post was made in hast..and sleepiness of course. So a few things I might add:

My program is 2D. I''ve set up OpenGL already for this. All the work is done on a SDL surface(just a 2D bitmap in memory) and then converted to a GL texture then rendered. Now model transformation are no worry, as they won''t happen at all.

I really like the input you''ve giving too. My game is actully at a fixed 640x400 res. You''ve all giving me some interesting things to try out now and I really appreciate the feed back....I''m still kinda scared of this beast OpenGL ;-)

Oh and thanks for that URL HellRiZZer, I''ll be sure to check that out.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!