java - Displaying texture in spritesheet based on ID's -
i've been trying display blocks on screen, each different textures. i've got spritesheet set code. can't manage specific coordinates in spritesheet, can't display else first image.
i've got render function set up:
public static void render(int texture, int blockid) { float xpos = (float)blockid; float ypos = 0; while(xpos > 9) { xpos -= 10; ypos++; } glbindtexture(gl_texture_2d, texture); glpushmatrix(); glbegin(gl_triangles); gltexcoord2f((8f / 160f) * xpos, (8f / 160f) * ypos); glvertex2i(16, 0); gltexcoord2f((8f / 160f) * xpos, (8f / 160f) * ypos); glvertex2i(0, 0); gltexcoord2f((8f / 160f) * xpos, (8f / 160f) * ypos); glvertex2i(0, 16); gltexcoord2f((8f / 160f) * xpos, (8f / 160f) * ypos); glvertex2i(0, 16); gltexcoord2f((8f / 160f) * xpos, (8f / 160f) * ypos); glvertex2i(16, 16); gltexcoord2f((8f / 160f) * xpos, (8f / 160f) * ypos); glvertex2i(16, 0); glend(); glpopmatrix(); } it decides texture take blockid, if blockid 1 takes second image spritesheet, if it's 2 takes third image, etc.
but don't know have multiply xpos , ypos variables gltexcoord2f. i've tried different ways like:
gltexcoord2f(xpos / 128f, 0f / 128f); glvertex2i(16, 0); gltexcoord2f(0f / 128f, ypos / 128f); glvertex2i(0, 16); or
gltexcoord2f((0f / 128f) * xpos, 0f / 128f; glvertex2i(16, 0); gltexcoord2f(0f / 128f, (8f / 128f)) * ypos; glvertex2i(0, 16); and even
gltexcoord2f(0f / 128f, 0f / 128f); glvertex2i(16 * xpos, 0); gltexcoord2f(0f / 128f, 8f / 128f); glvertex2i(0, 16 * ypos); but none worked.
=== edit ===
i've managed working. odd blockid's. if blockid 1 displays first texture, if it's 2 displays brownish color (that isn't on spritesheet) , if blockid 3 displays second texture on sheet etc.
texture coordinates exist within range: 0.0f --> 1.0f
you need calculate how far along (in percentage) image sprite is, feed texcoord float.
i think should using:
(8.0f / 128.0f) * xpos (you should cache float once works) if not work, ensure these 2 things:
- hard coded values work (proving texture loaded , initialised fine)
- the correct values being calculated within render method
alternatively, don't forget render triangles glcolor(1.0f, 1.0f, 1.0f) ensure rest of rendering in application works.
Comments
Post a Comment