简体   繁体   中英

Texture Coordinates not Functioning Properly?

I don't know if I am just... not seeing something here. I have been working on this for quite some time trying to get it functioning. I have used sprite sheets and texture coordinates before - so I am familiar with them.

I have a sprite sheet (shown in picture) that is 9 x 9. It is an isometric sprite sheet (because I have isometric tiles). The total image is 1152 x 576.

I calculated that, based on the dimensions, the width of each tile is 128 pixels and the height is 64.

The percentage of the image each tile takes up is 0.111 (both length-wise and height-wise [128 / 1152 | 64 / 576]).

My code, however, is not working.

Image of code and tilesheet: http://i.imgur.com/JU4YYaB.png

Raw code:

@Override
public void drawTile(int tileX,  int tileY, Texture atlas) {

    int tileXcoord, tileYcoord;

    tileXcoord = (tileX - tileY) * (TILE_XHALF); 
    tileYcoord = (tileX + tileY) * (TILE_YHALF); 

    atlas.bind();


    //GL11.glColor3f(Red, Green, Blue); 

    GL11.glBegin(GL11.GL_QUADS);
    {
        setTextureCoordinates(1);
        GL11.glVertex2f(tileXcoord, tileYcoord);
        setTextureCoordinates(2);
        GL11.glVertex2f(tileXcoord + TILE_XHALF, tileYcoord + TILE_YHALF);
        setTextureCoordinates(3);
        GL11.glVertex2f(tileXcoord + (TILE_XHALF * 2), tileYcoord);
        setTextureCoordinates(4);
        GL11.glVertex2f(tileXcoord + TILE_XHALF, tileYcoord - TILE_YHALF);
    }
    GL11.glEnd();

}

//***** SET TEXTURE COORDINATES *****
    public void setTextureCoordinates(int pos) {
        Rectangle rect = textureAtlas.getTexture(ID);
        switch (pos) {
        case 1 :
            GL11.glTexCoord2f(0.0f, 0.0555f); // left-middle
            //GL11.glTexCoord2f(0.0f, 0.5f);
            break;

        case 2:
            GL11.glTexCoord2f(0.0555f, 0.111f); //top-middle
            //GL11.glTexCoord2f(0.5f, 1f);
            break;

        case 3:
            GL11.glTexCoord2f(0.111f, 0.0555f); // right-middle
            //GL11.glTexCoord2f(1f, 0.5f);
            break;

        case 4:
            GL11.glTexCoord2f(0.0555f, 0.0f); // bottom-middle
            //GL11.glTexCoord2f(0.5f, 0.0f);
            break;
        }

}

EDIT: Output: http://i.imgur.com/YYKo37m.png

It looks like you're getting about a tile and a half of texture on each rendered tile in game.

On a hunch, I'm going to guess that whatever you're using to load textures is padding the image out to the next power of two (2048 x 1024).

Your coordinates would then be (in pixel coordinates):

(0,     56.8 )
(113.7, 113.7)
(227.3, 56.8 )
(113.7, 0    )

This roughly seems to match your texture seams. I'm not familiar with the Java libraries that provide OpenGL bindings, but seeing as you're explicitly using OpenGL 1.1, it would make sense that the library would do this. The hardware of the time (1997) couldn't handle textures that weren't a power of two. This was added as an extension in 2003 .

You can:

  1. Pad out the texture to the next power of two yourself and use proper coordinates for that (0.0625 instead of 0.111)
  2. Look at the documentation for texture loading for the library you're using and see if there's a way to force non-power-of-two textures. I would be pretty surprised if there weren't.

In general, it's good to pad your textures out to a power of two, as the floats you'd use to represent a tile will always be exactly represented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM