Hi,
Strange one this, when I draw all my blocks for a chunk, if using textures, only one block per chunk is drawn?
This is my render code for a block:
private void renderBlock(Block.BlockType type) {
GL11.glPushMatrix();
GL11.glEnable(GL_TEXTURE_2D);
// Get texture for what player is holding - we use the TextureManager singleton for this
TextureBuffer texture = TextureManager.getInstance().getTexture(t);
GL11.glBindTexture(GL_TEXTURE_2D, texture.textureId);
GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
GL11.glTexParameteri(GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
GL11.glTexParameteri(GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
GL11.glTexParameteri( GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST ); // GL_LINEAR
GL11.glTexParameteri( GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST ); // GL_LINEAR
// Make sure we are using our TextureBuffer object
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA,
texture.decoder.getWidth(),
texture.decoder.getHeight(),
0, GL11.GL_RGBA,
GL11.GL_UNSIGNED_BYTE,
texture.buf);
GL11.glTexCoord2f(0.0f, 0.0f);
off_glVertex3f(-0.5f, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
off_glVertex3f( 0.5f, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
off_glVertex3f( 0.5f, 0.5f, 0.5f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
off_glVertex3f(-0.5f, 0.5f, 0.5f); // Top Left Of The Texture and Quad
// Back Face
GL11.glTexCoord2f(1.0f, 0.0f);
off_glVertex3f(-0.5f, -0.5f, -0.5f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
off_glVertex3f(-0.5f, 0.5f, -0.5f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
off_glVertex3f( 0.5f, 0.5f, -0.5f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
off_glVertex3f( 0.5f, -0.5f, -0.5f); // Bottom Left Of The Texture and Quad
// Top Face
GL11.glTexCoord2f(0.0f, 1.0f);
off_glVertex3f(-0.5f, 0.5f, -0.5f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
off_glVertex3f(-0.5f, 0.5f, 0.5f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
off_glVertex3f( 0.5f, 0.5f, 0.5f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
off_glVertex3f( 0.5f, 0.5f, -0.5f); // Top Right Of The Texture and Quad
// Bottom Face
GL11.glTexCoord2f(1.0f, 1.0f);
off_glVertex3f(-0.5f, -0.5f, -0.5f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
off_glVertex3f( 0.5f, -0.5f, -0.5f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
off_glVertex3f( 0.5f, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
off_glVertex3f(-0.5f, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
// Right face
GL11.glTexCoord2f(1.0f, 0.0f);
off_glVertex3f( 0.5f, -0.5f, -0.5f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
off_glVertex3f( 0.5f, 0.5f, -0.5f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
off_glVertex3f( 0.5f, 0.5f, 0.5f); // Top Left Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 0.0f);
off_glVertex3f( 0.5f, -0.5f, 0.5f); // Bottom Left Of The Texture and Quad
// Left Face
GL11.glTexCoord2f(0.0f, 0.0f);
off_glVertex3f(-0.5f, -0.5f, -0.5f); // Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 0.0f);
off_glVertex3f(-0.5f, -0.5f, 0.5f); // Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(1.0f, 1.0f);
off_glVertex3f(-0.5f, 0.5f, 0.5f); // Top Right Of The Texture and Quad
GL11.glTexCoord2f(0.0f, 1.0f);
off_glVertex3f(-0.5f, 0.5f, -0.5f); // Top Left Of The Texture and Quad
GL11.glEnd();
glColor3f(1,1,1);
GL11.glPopMatrix();
}
Is it because I’m binding the same texture id for each block?
My TextureManager class:
public class TextureManager {
Map<BlockType,TextureBuffer> myTextures = new HashMap<BlockType, TextureBuffer>();
// Add ALL texture types here - see Block.java
private final static BlockType types[] =
{ BlockType.BlockType_Grass,
BlockType.BlockType_Dirt,
BlockType.BlockType_Wood,
};
private InputStream in;
private static TextureManager instance = null;
public synchronized static TextureManager getInstance()
{
if(instance == null)
{
instance = new TextureManager();
}
return instance;
}
public TextureManager() {
}
public TextureBuffer getTexture(BlockType t)
{
TextureBuffer tb = (TextureBuffer)myTextures.get(t);
return tb;
}
public boolean loadAllTextures() {
boolean bAllLoaded = true;
try
{
for (int i = 0; i < types.length; i++) {
TextureBuffer tb = new TextureBuffer();
if (!loadToolTexture(tb, types[i])) {
System.out.println("Failed to load texture:" + types[i]);
bAllLoaded = false;
break;
} else {
myTextures.put(types[i],tb);
System.out.println("Texture:"+types[i] + " added.");
}
}
} catch(IOException io) { System.out.println(io.getMessage()); bAllLoaded = false;}
return bAllLoaded;
}
private boolean loadToolTexture(TextureBuffer tb, BlockType t) throws IOException{
String texturePath = "";
boolean bLoaded = true;
try {
switch (t) {
case BlockType_Grass:
texturePath = "grass16x16.png";
break;
case BlockType_Dirt:
texturePath = "grassdirt.png";
break;
case BlockType_Wood:
texturePath = "crate16x16.png";
break;
case BlockType_Stone:
texturePath = "stone.png";
break;
case BlockType_Sand:
texturePath = "sand.png";
break;
case BlockType_Water:
texturePath = "water.png";
break;
case BlockType_Snow:
texturePath = "snow.png";
break;
default:
texturePath = "grass16x16.png";
}
in = new FileInputStream("res/" + texturePath);
tb.decoder = new PNGDecoder(in);
tb.buf = ByteBuffer.allocateDirect(4 * tb.decoder.getWidth()
* tb.decoder.getHeight());
tb.decoder.decode(tb.buf, tb.decoder.getWidth() * 4, Format.RGBA);
tb.buf.flip();
tb.textureId = GL11.glGenTextures();
} catch (Exception e) {
System.out.println(e.getMessage());
bLoaded = false;
}
finally {
in.close();
}
return bLoaded;
}
and TextureBuffer:
public class TextureBuffer {
public BlockType t;
public ByteBuffer buf;
public int textureId;
public PNGDecoder decoder;
}
All textures are loaded at the start of the world creation. Only three textures at the moment, so just three texture id’s.
I guess I’m doing something stupid?!
Any help is much appreciated and sorry if posted this in wrong section!
Steve