Texture wrapping and sprite sheets

Started by
4 comments, last by MARS_999 15 years, 9 months ago
If I load a texture which I want to tile across a surface, that's pretty easy to do:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
And then just specify texture coordinates outside of the range 0 to 1 when I'm drawing stuff. However, as far as I can tell, this will only work for textures I've loaded from a single file (like an image of brickwork which I know tiles properly). If I want to incorporate that brickwork into a sprite sheet, with a bunch of other textures as well, I'm not sure how to wrap. Say if my sprite sheet is divided into quarters, and the brickwork is the top left corner, it's bounding texture coordinates are (0,0) and (0.25, 0.25). If I specify texture coordinates bigger than that, GL won't know that I want it to wrap and will start rendering bits of the other 3 sprites in my brickwork. Is there any way around this, or is the solution just to load all wrapping textures indvidually, and save the sprite sheets for things which don't wrap?
"We two, the World and I, are stubborn fellows at loggerheads, and naturally whichever has the thinner skull will get it broken" - Richard Wagner
Advertisement
All your sub textures in that atlas, need to fall on fractional parts.

e.g. 4 textures in 1, tiled 4 times.


0,1,2,3
.25, 1.25, 2.25, 3.25
.5, 1.5, 2.5, 3.5
.75, 1.75, 2.75, 3.75


I'm not really sure what you mean. Do you mean that the only way to wrap a texture which is part of a sprite sheet is to draw it as multiple quads? Or have I completely misunderstood?

I'm vaguely wondering if there could be a solution which involves loading a spritesheet as one file, but then splitting that file up into multiple textures in memory, so the end result is pretty much what you'd get if you'd loaded all the textures seperately. It would save a bit of time doing the actual loading, but I guess that way you still pay the overhead of switching between different textures when you're rendering a bunch of sprites :-/
"We two, the World and I, are stubborn fellows at loggerheads, and naturally whichever has the thinner skull will get it broken" - Richard Wagner
Nope, you just need to specify the texture coordinates that fall on the offset alignment. So if you have a texture with 16 textures in one, a texture atlas, you would need to offset .25 on s,t texture coordinates and multiply that value by the tile amount. Hope that clears this up. BTW are you using shaders? This is so much easier to do with a shader.
I'm not using shaders, no. I might look into them, but for now I'm just trying to keep things as simple as possible. I've not heard of a "tile atlas" before, so that could be where I'm misunderstanding you.

I've not had a chance to try out what I think you're suggesting yet, but it kinda sounds like you're saying that if I have a sprite sheet with 4 textures, laid out like this:

AB
CD

Then to draw a quad with A tiled 4 times, ie:

AA
AA

I'd need to do something like this (clockwise, from top left)

glBegin(GL_QUADS);	glTexCoord2f(0, 0);	glVertex3f(-1, 1, 0);	glTexCoord2f(1.5, 0);	glVertex3f(1, 1, 0);	glTexCoord2f(1.5, 1.5);	glVertex3f(1, -1, 0);	glTexCoord2f(0, 1.5);	glVertex3f(-1, -1, 0);glEnd();


Is this right? As I say, I haven't tested it, but in my head this would give me a quad textured like this:

ABA
CDC
ABA
"We two, the World and I, are stubborn fellows at loggerheads, and naturally whichever has the thinner skull will get it broken" - Richard Wagner
I would like to help, but unless you move to shaders I am afraid I know of no way to do this with out switching texture a lot, and you may want to invest in a good visibility determination scheme if you choose that route. Honestly if you are going for simple shaders are simple. Unless you want to target very old hardware, and if that is the case you could always try using assembly shaders.

This topic is closed to new replies.

Advertisement