Hello Guest

Sprite Sheet Help

  • 2 Replies
  • 2854 Views
Sprite Sheet Help
« on: January 11, 2015, 19:10:10 »
Hey guys,

This is my first post so please let me know if I do anything wrong  ;D.

I am making a, yes another, minecraft clone. I am in imediate mode, and need some help with sprite sheets. I am using a sheet (attached) and it has sprites of the block textures 16x16 px.

The problem is that when I bind the subimage it renders the entire sheet.

Heres my code (need to clean it up, after I get it working  :P)

Code: [Select]
import java.io.FileInputStream;
import java.io.IOException;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.opengl.TextureLoader;

public class SpriteSheetManager {
public static SpriteSheet sheet;

public static Image getImage(int x, int y){

return sheet.getSprite(x, y);
}

public static void init(){
try {
Image i = new Image(new FileInputStream("/terrain.png"), "Sheet", true);
sheet = new SpriteSheet(i, 16, 16);
} catch (SlickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Block Renderer code:

Code: [Select]

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.opengl.Texture;

/**
 *
 * @author Walt Pach (wxwsk8er)
 *
 */
public class BlockRenderer {
/*
* Global Constants.
*/
public static float BLOCK_WIDTH = 1f;
public static float BLOCK_HEIGHT = 1f;
public static float BLOCK_LENGTH = 1f;
public static Texture blockTextureTemp;

/* Renders the block. */
/**
*
* @param b The block object you want to render.
* @param x The X-Coordinate for the block you want to render.
* @param y The Y-Coordinate for the block you want to render.
* @param z The Z-Coordinate for the block you want to render.
*/
public static void a(Block b, float x, float y, float z){
//blockTextureTemp = TextureManager.getTexture(b);
        //blockTextureTemp.bind();
SpriteSheetManager.init();
SpriteSheetManager.sheet.getSprite(2, 0).bind();

GL11.glPushMatrix();
GL11.glTranslatef(x, y, z);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glBegin(GL11.GL_QUADS);

// Front Face
GL11.glNormal3f(0.0f, 0.0f, -1.0f);
GL11.glTexCoord2f(0.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f,  1.0f);   // Bottom Left Of The Texture and Quad
        GL11.glTexCoord2f(1.0f, 0.0f);
        GL11.glVertex3f( 1.0f, -1.0f,  1.0f);   // Bottom Right Of The Texture and Quad
        GL11.glTexCoord2f(1.0f, 1.0f);
        GL11.glVertex3f( 1.0f,  1.0f,  1.0f);   // Top Right Of The Texture and Quad
        GL11.glTexCoord2f(0.0f, 1.0f);
        GL11.glVertex3f(-1.0f,  1.0f,  1.0f);   // Top Left Of The Texture and Quad

//GL11.glColor3f(0f, 1.0f, 0f);
GL11.glNormal3f(0.0f, 0.0f, 1.0f);
// Back Face
        GL11.glTexCoord2f(1.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f);   // Bottom Right Of The Texture and Quad
        GL11.glTexCoord2f(1.0f, 1.0f);
        GL11.glVertex3f(-1.0f,  1.0f, -1.0f);   // Top Right Of The Texture and Quad
        GL11.glTexCoord2f(0.0f, 1.0f);
        GL11.glVertex3f( 1.0f,  1.0f, -1.0f);   // Top Left Of The Texture and Quad
        GL11.glTexCoord2f(0.0f, 0.0f);
        GL11.glVertex3f( 1.0f, -1.0f, -1.0f);   // Bottom Left Of The Texture and Quad


   // GL11.glColor3f(0f, 0f, 1f);
    GL11.glNormal3f(1.0f, 0.0f, 0.0f);
// Top Face
        GL11.glTexCoord2f(0.0f, 1.0f);
        GL11.glVertex3f(-1.0f,  1.0f, -1.0f);   // Top Left Of The Texture and Quad
        GL11.glTexCoord2f(0.0f, 0.0f);
        GL11.glVertex3f(-1.0f,  1.0f,  1.0f);   // Bottom Left Of The Texture and Quad
        GL11.glTexCoord2f(1.0f, 0.0f);
        GL11.glVertex3f( 1.0f,  1.0f,  1.0f);   // Bottom Right Of The Texture and Quad
        GL11.glTexCoord2f(1.0f, 1.0f);
        GL11.glVertex3f( 1.0f,  1.0f, -1.0f);   // Top Right Of The Texture and Quad

    GL11.glNormal3f(-1.0f, 0.0f, 0.0f);
    // Bottom Face
        GL11.glTexCoord2f(1.0f, 1.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f);   // Top Right Of The Texture and Quad
        GL11.glTexCoord2f(0.0f, 1.0f);
        GL11.glVertex3f( 1.0f, -1.0f, -1.0f);   // Top Left Of The Texture and Quad
        GL11.glTexCoord2f(0.0f, 0.0f);
        GL11.glVertex3f( 1.0f, -1.0f,  1.0f);   // Bottom Left Of The Texture and Quad
        GL11.glTexCoord2f(1.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f,  1.0f);   // Bottom Right Of The Texture and Quad

    GL11.glNormal3f(0.0f, -1.0f, 0.0f);
// Right face
        GL11.glTexCoord2f(1.0f, 0.0f);
        GL11.glVertex3f( 1.0f, -1.0f, -1.0f);   // Bottom Right Of The Texture and Quad
        GL11.glTexCoord2f(1.0f, 1.0f);
        GL11.glVertex3f( 1.0f,  1.0f, -1.0f);   // Top Right Of The Texture and Quad
        GL11.glTexCoord2f(0.0f, 1.0f);
        GL11.glVertex3f( 1.0f,  1.0f,  1.0f);   // Top Left Of The Texture and Quad
        GL11.glTexCoord2f(0.0f, 0.0f);
        GL11.glVertex3f( 1.0f, -1.0f,  1.0f);   // Bottom Left Of The Texture and Quad

    GL11.glNormal3f(0.0f, 1.0f, 0.0f);
// Left Face
        GL11.glTexCoord2f(0.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f, -1.0f);   // Bottom Left Of The Texture and Quad
        GL11.glTexCoord2f(1.0f, 0.0f);
        GL11.glVertex3f(-1.0f, -1.0f,  1.0f);   // Bottom Right Of The Texture and Quad
        GL11.glTexCoord2f(1.0f, 1.0f);
        GL11.glVertex3f(-1.0f,  1.0f,  1.0f);   // Top Right Of The Texture and Quad
        GL11.glTexCoord2f(0.0f, 1.0f);
        GL11.glVertex3f(-1.0f,  1.0f, -1.0f);   // Top Left Of The Texture and Quad
    GL11.glEnd();
   
    GL11.glPopMatrix();
}

public enum BlockFace{
Top, Bottom, Left, Right, Back, Front;
}
}

Thanks ahead of time :)


*

Offline abcdef

  • ****
  • 335
Re: Sprite Sheet Help
« Reply #1 on: January 12, 2015, 06:49:56 »
you will need to post some code where where you create the texture data and where you bind it, your code doesn't seem to relate to where the problem lies.

Re: Sprite Sheet Help
« Reply #2 on: January 20, 2015, 01:59:00 »
What about setting the UV coordinates so that they only encompass the necessary part of the sprite sheet?

So since the sprite sheet is 16x16 blocks, a box is only a small 0.0625 height/width in terms of UV co-ordinates, not pixels.

With that, the top left square would have the following co-ordinates:
X - Y
Top Left: 0.0 - 1.0
Top Right: 0.0625 - 1.0
Bottom Left: 0.0 - 0.9375
Bottom Right: 0.0625 - 0.9375
I believe.
Will soon be an Oracle Certified Associate :D