Search code examples
javaopengltexture-mapping

Texture coordinates don't work?


Seriously I'm getting mad with this code, because don't work:

switch(particle) {
case 0:
    glBegin(GL_TRIANGLE_STRIP);         // STONE
        glNormal3f(0.0f, 0.0f, 1.0f);
        glTexCoord2d(1, 1); glVertex3f(x+size, y+size, 0.0f); // Top Right
        glTexCoord2d(0.5, 1); glVertex3f(x-size, y+size, 0.0f); // Top Left
        glTexCoord2d(1, 0.5); glVertex3f(x+size, y-size, 0.0f); // Bottom Right
        glTexCoord2d(0.5, 0.5); glVertex3f(x-size, y-size, 0.0f); // Bottom Left
    glEnd();
    break;
case 1:
    glBegin(GL_TRIANGLE_STRIP);         // EARTH
        glNormal3f(0.0f, 0.0f, 1.0f);
        glTexCoord2d(0.5, 0.5); glVertex3f(x+size, y+size, 0.0f); // Top Right
        glTexCoord2d(0, 0.5); glVertex3f(x-size, y+size, 0.0f); // Top Left
        glTexCoord2d(0.5, 0); glVertex3f(x+size, y-size, 0.0f); // Bottom Right
        glTexCoord2d(0, 0); glVertex3f(x-size, y-size, 0.0f); // Bottom Left
    glEnd();
    break;
}

The case 0 works fine, but the case 1 doesn't and I don't know why... This is the image (32x32 with two 16x16 sub-textures):

enter image description here


Solution

  • Give that case 0 apparently works fine, your texture coordinates for case 1 are wrong. They should be:

    glBegin(GL_TRIANGLE_STRIP);         // EARTH
        glNormal3f(0.0f, 0.0f, 1.0f);
        glTexCoord2d(0.5, 1.0); glVertex3f(x+size, y+size, 0.0f); // Top Right
        glTexCoord2d(0, 1.0); glVertex3f(x-size, y+size, 0.0f); // Top Left
        glTexCoord2d(0.5, 0.5); glVertex3f(x+size, y-size, 0.0f); // Bottom Right
        glTexCoord2d(0, 0.5); glVertex3f(x-size, y-size, 0.0f); // Bottom Left
    glEnd();