Advertisement

Loading multiple textures

Started by February 09, 2006 08:54 PM
3 comments, last by eSCHEn 18 years, 9 months ago
I created grid 1024 x 1024 and gridded it with 64x64 squares to ensure my sprites wuold be a power of 2. I filled each square with a texture in photoshop. I am tying to figure out the best way to extract the images out the grid and then display them. Nehe doesn't currently have any tutorials on this as far as I can tell. Any suggestions? or a website on a good texture loader tutorial? Thank you.
First load the whole texture. Then bind it. Now access it using texture coordinates. You can get the texture coordinates using x * 64/1024 and y * 64/1024. Well something along those lines.

If you want to load it seperately best keep the textures seperate.
The more applications I write, more I find out how less I know
Advertisement
As Crack123 suggests, there are two ways to achieve what you want.

Your first option would be to separate your 1024 x 1024 image into its individual sprites, each one a 64 x 64 image, and load them all in as individual textures, meaning a total of 256 sprites. This approach would be perfectly acceptable but could prove to be a pain to keep the textures up to date.

Your second option would be to load the 1024 x 1024 as one large texture and then use the algorithm provided by Crack123 to refer to the individual textures within the large texture. Texture co-ordinates within OpenGL have a mathematical origin, where the bottom left of the texture is point (0, 0) and the top right is point (1, 1). Using this system, to refer to a subtexture within a large texture is easy as you simply use fractional co-ordinates to refer to it. For example, to get the bottom left texture in a 256 x 256 image, where there are four equal square subtextures:

A picture showing OpenGL Texture Co-ordinates

This would give the co-ordinates of the white square as being:
(1 / 256) * 0,   (1 / 256) * 0    // bottom left(1 / 256) * 128, (1 / 256) * 0    // bottom right(1 / 256) * 128, (1 / 256) * 128  // top right(1 / 256) * 0,   (1 / 256) * 128  // top left
As you can see, by referring to the width and height as being 1 and then dividing by the number of pixels in that dimension you will get the width of a pixel in OpenGL texture co-ordinates. To find the dimension you want, in this case 128 along and 128 up (from the bottom left) you simply multiply the value you obtained by the number of pixels of how far you want to go.

The algorithm outlined by Crack123 does exactly the same as the previous explanation:

Dimension of image is 1024 x 1024 so 1/1024 gives texture co-ordinate size of one pixel. Call this value t.

Each sprite is 64 x 64 so we know that 64t is the width and height of a sprite in texture co-ordinates, this can be expressed as 64/1024.

As each sprite is the same size in a particular axis, we can say that if the nth sprite is number (x, y), where x and y refer to which number image along that axis the sprite is (starting at image 1 on the bottom left and continuing left to right and then bottom to top), then the co-ordinates in OpenGL for that sprite are:
64t * (x - 1), 64t * (y - 1)64t * x,       64t * (y - 1)64t * x,       64t * y64t * (x - 1), 64t * y


Some additional information:

  • Textures for OpenGL do indeed have to be a power of two in each dimension unless you use some alternate means such as extensions but texture co-ordinates do not have to be a power of two.

  • A point that bears mentioning is that older graphics cards will have difficulty loading an image as large as 1024 x 1024 so you may want to do some investigation into that.

  • There are obvious places for performance gains, such as minimizing muliplications, but I have left those out to try and make the explanation clearer.


Hope that helps.
--
Cheers,
Darren Clark
Both of you guys Thank you VERY VERY much, damn I so need that explanation!!! you helped out more than you can imagine!! much appreciated!!
Not a problem, happy to help. I would suggest you read over "The Red Book" as it's an invaluable resource for learning OpenGL. An online version is linked to at the top of the work-in-progress FAQ for this forum.

Happy coding!
--
Cheers,
Darren Clark

This topic is closed to new replies.

Advertisement