Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying texture in spritesheet based on ID's
    primarykey
    data
    text
    <p>I've been trying to display blocks on screen, each with different textures. I've got the spritesheet set up as well as the code. But I can't manage to get specific coordinates in the spritesheet, I can't display anything else as the first image.</p> <p>I've got this render function set up:</p> <pre><code>public static void render(int texture, int blockID) { float xPos = (float)blockID; float yPos = 0; while(xPos &gt; 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(); } </code></pre> <p>It decides which texture to take from the blockID, so if the blockID is 1 it takes the second image from the spritesheet, if it's 2 it takes the third image, etc.</p> <p>But I don't know where I have to multiply the xPos and yPos variables to the glTexCoord2f. I've tried different ways like:</p> <pre><code>glTexCoord2f(xPos / 128f, 0f / 128f); glVertex2i(16, 0); glTexCoord2f(0f / 128f, yPos / 128f); glVertex2i(0, 16); </code></pre> <p>or</p> <pre><code>glTexCoord2f((0f / 128f) * xPos, 0f / 128f; glVertex2i(16, 0); glTexCoord2f(0f / 128f, (8f / 128f)) * yPos; glVertex2i(0, 16); </code></pre> <p>and even</p> <pre><code>glTexCoord2f(0f / 128f, 0f / 128f); glVertex2i(16 * xPos, 0); glTexCoord2f(0f / 128f, 8f / 128f); glVertex2i(0, 16 * yPos); </code></pre> <p>But none worked.</p> <p><strong>=== EDIT ===</strong></p> <p>I've almost managed to get it working. But only with odd blockID's. If the blockID is 1 it displays the first texture, but if it's 2 it displays a brownish color (that isn't on my spritesheet) and if the blockID is 3 it displays the second texture on the sheet etc.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload