简体   繁体   中英

LWJGL Sprite reader

i am trying to get my sprite sheet which is 256x256 and the tiles size is 32 to just show one sprite on the sheet but at the moment it is showing the whole sheet. so how can i get it to only show one sprite and make it easily changeable.

the code i have atm is:

public void Render(){
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureBank.Instance().GetTexture("spriteSheet").getTextureID());        

    GL11.glTranslatef(x, y, 0.0f);
    GL11.glBegin(GL11.GL_QUADS);

        GL11.glTexCoord2f(0.0f, 0.0f);
            GL11.glVertex2f(0.0f, 0.0f);
        GL11.glTexCoord2f(1.0f, 0.0f);
            GL11.glVertex2f(w, 0.0f);
        GL11.glTexCoord2f(1.0f, 1.0f);
            GL11.glVertex2f(w, h);
        GL11.glTexCoord2f(0.0f, 1.0f);
            GL11.glVertex2f(0.0f, h);

    GL11.glEnd();
}

For all of your texCoords which are 1.0f, you need to divide by the number of sprites in their direction. Then to choose which sprite you want add the number you found above times the number of sprites to the left and below of the one you want to the x value and y value respectively.

this is what i have atm this show the top left one i believe but i still dont know how to completely to this with ease. is there any way to do so its something like this. sprite(colX,colY)

    GL11.glBindTexture(GL_TEXTURE_RECTANGLE_ARB, TextureBank.Instance().GetTexture("spriteSheet").getTextureID());      

    GL11.glTranslatef(x, y, 1.0f);
    GL11.glRotatef(Angle, 0f, 0f, 1.0f);
    GL11.glBegin(GL11.GL_QUADS);

        GL11.glTexCoord2f(0.0f, 0.0f);//top left
            GL11.glVertex2f(0.0f, 0.0f);
        GL11.glTexCoord2f(1.0f /8f, 0.0f);//top right
            GL11.glVertex2f(w, 0.0f);
        GL11.glTexCoord2f(1.0f /8f, 1.0f /8f);//bottom right
            GL11.glVertex2f(w, h);
        GL11.glTexCoord2f(0.0f, 1.0f /8f);//bottom left
            GL11.glVertex2f(0.0f, h);
    GL11.glEnd();
    GL11.glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);

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