I am new to OpenGL and am currently trying to render a cube with four faces, each with a different texture.
As you all know, having a separate texture for each face type is very memory intensive and makes the application slow.
At the moment I am trying to use a texture sheet for the sprite. I have the graphic file with each texture 16×16 pixels with 256 sprites arranged in a square (16×16).
I know that
GL11.glTexCoord2f(1.0f, 0.0f);
GL11.glVertex3f(1.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
GL11.glVertex3f(0.0f, 1.0f, 1.0f);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(0.0f, 0.0f, 1.0f);
GL11.glTexCoord2f(1.0f, 1.0f);
GL11.glVertex3f(1.0f, 0.0f, 1.0f);
gives me a rectangle with the whole sprite sheet so u and v of glTexCoord2f have to be less than 1.0f.
What I need now is a formula which will calculate the u and v of any sprite id in the texture.
The IDs go as following in the texture bitmap:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
17 18 16 20 21 22 23 24...
and I’d like to have a u and v for any of these IDs. The last bit isn’t explained very well, so I’ll explain it better if you would like.
Thank you in advance!
The locations of the sprites’ texture in the image are in a 16 x 16 array, so the location of a texture (if you count the first one as 0) are:
The coordinates are then:
Then replace 1.0f with u1 or v1 as appropriate and 0.0f with u0 or v0 in the glTexCoord2Df calls.