Sprite Sheet Instruction Needed

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):

http://pastebin.java-gaming.org/37d0f6c75191d (Block Renderer)
http://pastebin.java-gaming.org/7d0fc75791d10 (Sprite Sheet Manager)
http://pastebin.java-gaming.org/d0fc5897d101b (Current Texture Manager, singe file support)

Thanks for the help :slight_smile:

PS: I’m using LWJGL and Slick

First question anyone would ask, what lib are you using for the game?

Nvm i seen it xD hmm ill look it up 1 sec

I’m not sure what the slick SpriteSheet code is doing as I’m unfamiliar with the engine, but I am assuming that your entire spritesheet mage is being loaded into one texture object. Please correct me if this is wrong.

If I’m right, then the problem is with your texture coordinates ( GL11.glTexCoord2f(?,? ) ). Since 0.0f is the left side of the image and 1.0f is the right, you need to know the bounding coordinates of the sprite sub texture you intend to use, and divide those by the width /height of the spritesheet image.

So:

    GL11.glTexCoord2f(0.0f, 0.0f);
    GL11.glTexCoord2f(1.0f, 0.0f);
    GL11.glTexCoord2f(1.0f, 1.0f);
    GL11.glTexCoord2f(0.0f, 1.0f);

Becomes:

    GL11.glTexCoord2f(sprite_left/sheet_width, sprite_top/sheet_height);
    GL11.glTexCoord2f(sprite_right/sheet_width,sprite_top/sheet_height);
    GL11.glTexCoord2f(sprite_right/sheet_width, sprite_bottom/sheet_height);
    GL11.glTexCoord2f(sprite_left/sheet_width, sprite_bottom/sheet_height);

And similarly modified for the rest of your draw statements

Ok, so I did what you said and here are the results:

http://pixelnet.net84.net/a.png

And my code:

http://pastebin.java-gaming.org/59d0b2e8e181f

First off, ‘a’ seems to be your render method, which is called a TON of times per second. You are loading your texture every time that method is called, which is insanely inefficient. You should create a single instance of that texture, and re-use it every time that method is called. Loading a texture every render call will quickly kill your performance.

Second, as for your problem, I actually have made a video explaining how a spritesheet works, and how to use a specified portion of that sheet. Sorry if this seems like self promotion, I really am just trying to help you!

TH2UU5BLyzU

Ok, so I tweaked it a bit (trying to follow your video). But it still does’nt work. Any ideas?

Code: http://pastebin.java-gaming.org/d0bee488f1710
Image: http://pixelnet.net84.net/aa.png

Nevermind, I fixed it. Also I will post the fixed code for anyone else who has this problem (sprite-sheeting). Thanks for the video, it helped ALOT! ;D 8)

You are using the hardcoded value [icode]32f[/icode] in your code, you should assign this to a variable for greater code readability. I’m going to assume that it is meant to be the width of your sheet. Assuming so, the mistake you’ve made is quite obvious. Since it seems that you’re unfortunately making another minecraft clone, let me refer to this Figure 1.
Now, I’m going to be referring to the [icode]S[/icode] coord, which is the texture coordinate along the bottom edge of the image, and the [icode]T[/icode] coord, along the vertical edge. [icode]glTexCoord[/icode] takes the parameters [icode](S, T)[/icode]. As such, the bottom left is [icode](0, 0)[/icode], the bottom right is [icode](0, 1)[/icode], the top left is [icode](1, 0)[/icode] and the top right is [icode](1, 1)[/icode].

[icode](1, 0)[/icode] [icode](1, 1)[/icode]

[icode](0, 0)[/icode] [icode](0, 1)[/icode]
Figure 1

Now, as you are currently seeing your entire spritesheet on your box, you obviously sending [icode](0, 0)[/icode], [icode](0, 1)[/icode], [icode](1, 0)[/icode] and [icode](1, 1)[/icode]. To pick out say, the dirt, you’re going to have to know it’s relative position in the sheet, in this case it is at [icode](6, 4)[/icode], the width in pixels of the entire sheet (in this case, 16), and the width in sprites of the entire sheet (in this case, 16 again). From this, we can calculate where in the sheet you want to render.

Okay so I got this far through the post before you solved it…

I vlove how everyone used minecrafts texture pack when debugging a 3D game xD

Mine = not original mine craft. ;D

Hey guys, I’m here again with yet another bug :-. It seems that the texture on one side of the quad, is upside down. I tried debuging it but couldnt find it.

Thanks for the help!

Code: http://pastebin.java-gaming.org/0bee85f87101e
Image:

http://pixelnet.net84.net/aaa.png

I think bottom right is icode[/icode] and top left is icode[/icode] :slight_smile:

Oh and I know it’s the front face 8)

Anyone?

Sorry for rushing, just another noob ;D

Hey guys, after an epic re-write I have re-coded my voxel engine and have (not supprisingly) ran into the bug of the purple textures!

Here’s a screenshot of what happens:

http://pixelnet.net84.net/aaaa.png

The code can be found at: Github.com/BiggerOnTheInside/Binder-Development

Try [icode]GL11.glColor3f(1, 1, 1);[/icode] before rendering blocks.