Only one block in chunk being drawn when using textures

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

In line 6 you bind a texture:

TextureBuffer texture = TextureManager.getInstance().getTexture(t);

But I don’t know what t is and where it comes from. Maybe this is important.

Anyway, it seems you are binding a texture with every call of renderBlock. This means, every rendered block gets it’s own call to bind(). This is a waste of ressources, because bind() is very expensive. When you have a TextureManager, you should use it to bind a texture when it is not currently bound. Like this:

public class TextureManager {
	
	private static TextureManager reference;
	private HashMap<String,Image> dataBase;
	private int boundTexture;  //################ this one here ##############
	private static boolean debug;
	
	public TextureManager(){
		boundTexture = 0;
		dataBase = new HashMap<String,Image>();
	}
	
	public static TextureManager getTextureManager(){
		if(reference == null)
			reference = new TextureManager();
		return reference;
	}
	
	public void addTexture(String name, String path){
		if(dataBase.containsKey(name))
			System.err.println("Image with key " + name + " already in database!");
		else{
			Texture tex = loadTexture(path);
			dataBase.put(name, new Image(tex));
		}
	}
	
	public int getID(String name){
		return dataBase.get(name).getTextureID();
	}
	
	public void bind(String texName){
		bind(this.getID(texName));
	}

   public void bind(int texture){ // ############# and this method here ############
		if(texture != boundTexture){
			GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
			if(debug)
				System.out.println("Bind texture: " + texture);
		}
		else{
			if(debug)
				System.out.println("No bind necessary for: " + texture);
		}
		boundTexture = texture;
		
		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);
	}
        // ... and so on

This way you can bind whatever you want, it will only bind a new texture when needed.

By the way:
you are using

 GL11.glPushMatrix();
GL11.glPopMatrix();

in your code, but there is no transformation. At least I can’t find one… Maybe this is a relict of previous versions of your code? But this can also be a source of problems, if your translation isn’t done right at other places in your code.
Maybe all your blocks are rendered at the same position?

@steg90

How are you rendering a Chunk?
What’s in the loop that draws the single blocks?

  • Longor1996

PS: Im asking this because you are using my “off_vertex” method wrapper trick.

Ah, thats why there is no translation, because of your trick? I forgot about that. :slight_smile:

External code probably sets glBegin once, while for each block, you call glEnd. Therefore only the first block is rendered.

Hi,

If I don’t use textures I get all the cubes drawn.

Code which creates a chunk:


private int createChunkList() {

		tx = 0; ty = 0; tz = 0;
		int iHidden = 0;
		int iBlockCount = 0;
		boolean bLeft = !false;
		boolean bRight = !false;
		boolean bAbove = !false;
		boolean bBelow = !false;
		boolean bFront = !false;
		boolean bBack = !false;

		boolean bDefault = true;
		
    	glEnable(GL11.GL_CULL_FACE);
		glBegin(GL11.GL_QUADS);

		for (int x = 0; x < Chunk.CHUNK_SIZE; x++) {
			for (int y = 0; y < Chunk.CHUNK_SIZE; y++) {
				for (int z = 0; z < Chunk.CHUNK_SIZE; z++) {
					if(!GetBlock(x,y,z).IsActive())  // 
					{
						continue;
					}
					bLeft = bDefault;
					if(x > 0)
					{
						bLeft = Blocks[x-1][y][z].IsActive();
					} else bLeft = false;
					bRight = bDefault;
					if(x < Chunk.CHUNK_SIZE - 1)
					{
						bRight = Blocks[x+1][y][z].IsActive();
					} else bRight = false;
					bAbove = bDefault;
					if(y > 0)
					{
						bAbove = Blocks[x][y-1][z].IsActive();
					} else bAbove = false;
					bBelow = bDefault;
					if(y < Chunk.CHUNK_SIZE - 1)
					{
						bBelow = Blocks[x][y+1][z].IsActive();
					} else bBelow = false;
					bFront = bDefault;
					if(z > 0)
					{
						bFront = Blocks[x][y][z-1].IsActive();
					} else bFront = false;
					bBack = bDefault;
					if(z < Chunk.CHUNK_SIZE - 1)
					{
						bBack = Blocks[x][y][z+1].IsActive();
					} else bBack = false;
					
					boolean bResult = bLeft & bRight & bAbove &
							          bBelow & bFront & bBack;
					
					if(!bResult) // Block is not hidden by neighbouring blocks
					{
						tx = ((xOffset * Chunk.CHUNK_SIZE) ) + (x);// << 1);
	                    ty = ((yOffset * Chunk.CHUNK_SIZE) ) + (y);// << 1);
	                    tz = ((zOffset * Chunk.CHUNK_SIZE) ) + (z);// << 1);
	            	
//	            		GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_LINE);
					
						renderBlock(GetBlock(x,y,z).getType());
	          
						iBlockCount++;   // total of blocks that can be seen
					}
					else
						iHidden++;   // amount of blocks that are surrounded
				}
			}
		}
		glEnd();
    	glDisable(GL11.GL_CULL_FACE);

		return iBlockCount;

	}

Yep, no translation thanks to @Longor1996 trick.

In this line:

TextureBuffer texture = TextureManager.getInstance().getTexture(t);

t is the block type - grass, dirt etc and is pulled out of my hashmap, I use this
as the key.

Do you think the binding is the issue?

Thanks

The GL11.glTexImage2D part looks suspicious.
Edit: Nevermind, Riven is right.

Second time… please pay attention:

External code probably sets glBegin once, while for each block, you call glEnd. Therefore only the first block is rendered.

@Riven - I’ve put the glBegin in like so now:


private int createChunkList() {

		tx = 0; ty = 0; tz = 0;
		int iHidden = 0;
		int iBlockCount = 0;
		boolean bLeft = !false;
		boolean bRight = !false;
		boolean bAbove = !false;
		boolean bBelow = !false;
		boolean bFront = !false;
		boolean bBack = !false;

		boolean bDefault = true;
		
    	glEnable(GL11.GL_CULL_FACE);

		for (int x = 0; x < Chunk.CHUNK_SIZE; x++) {
			for (int y = 0; y < Chunk.CHUNK_SIZE; y++) {
				for (int z = 0; z < Chunk.CHUNK_SIZE; z++) {
              				
                                        GL11.glEnable(GL_TEXTURE_2D);
					glBegin(GL11.GL_QUADS);  // placed here

					if(!GetBlock(x,y,z).IsActive())  // 
					{
						continue;
					}
					bLeft = bDefault;
					if(x > 0)
					{
						bLeft = Blocks[x-1][y][z].IsActive();
					} else bLeft = false;
					bRight = bDefault;
					if(x < Chunk.CHUNK_SIZE - 1)
					{
						bRight = Blocks[x+1][y][z].IsActive();
					} else bRight = false;
					bAbove = bDefault;
					if(y > 0)
					{
						bAbove = Blocks[x][y-1][z].IsActive();
					} else bAbove = false;
					bBelow = bDefault;
					if(y < Chunk.CHUNK_SIZE - 1)
					{
						bBelow = Blocks[x][y+1][z].IsActive();
					} else bBelow = false;
					bFront = bDefault;
					if(z > 0)
					{
						bFront = Blocks[x][y][z-1].IsActive();
					} else bFront = false;
					bBack = bDefault;
					if(z < Chunk.CHUNK_SIZE - 1)
					{
						bBack = Blocks[x][y][z+1].IsActive();
					} else bBack = false;
					
					boolean bResult = bLeft & bRight & bAbove &
							          bBelow & bFront & bBack;
					
					if(!bResult) // Block is not hidden by neighbouring blocks
					{
						tx = ((xOffset * Chunk.CHUNK_SIZE) ) + (x);// << 1);
	                    ty = ((yOffset * Chunk.CHUNK_SIZE) ) + (y);// << 1);
	                    tz = ((zOffset * Chunk.CHUNK_SIZE) ) + (z);// << 1);
	            	
//	            		GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_LINE);
					
						renderBlock(GetBlock(x,y,z).getType());
	          
						iBlockCount++;   // total of blocks that can be seen
					}
					else
						iHidden++;   // amount of blocks that are surrounded
				//glEnd();
				}
			}
		}
		glEnd();
    	glDisable(GL11.GL_CULL_FACE);

		return iBlockCount;

	}

Also had to add the enable texture, but now very very slow, maybe this is the binding you mentioned?

Regards,
Steve

On inspection, it is this line in renderBlock that is taking up all the processing time:


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

This needs removing - well, needs sorting out how I do this so not done in the render loop…

I once had 884 textures with dimensions 20*20 pixel rendered in one frame. Every single one with a call to bind(). And the FPS were down to 7 on my Intel Core i3 M330 with Radeon HD5650. Having many bind() calls is not good.

Thanks Sparky but the bind doesn’t seem to be the issue, if I remove the glTexImage2D call from the renderBlock code I 60fps again, but of course, not the correct texture.

@Sparky83 - when do you call glTexImage2D? I am obviously doing this badly as it shouldn’t be in my renderBlock method.

Thanks

I just did this once in this method, which is loading a texture out of a PNG.

private Texture loadTexture(String path){
		int width = 0;
		int height = 0;
		int id = 0;
		PNGDecoder decoder = null;
		InputStream stream = null;
		ByteBuffer buff;
		Texture tD = null;
		stream = ClassLoader.getSystemResourceAsStream(path);
		
		try {
			
			decoder = new PNGDecoder(stream);
		
			// read image size
			width = decoder.getWidth();
			height = decoder.getHeight();
			
			// pixel data size
			int bpp = 4;
			
			// create buffer for storage
			buff = BufferUtils.createByteBuffer(width * height * bpp);
			
			decoder.decode(buff, bpp * width, PNGDecoder.Format.RGBA);
			
			buff.flip();
			
			// create the texture from RGBA buffer
			GL11.glEnable(GL11.GL_TEXTURE_2D);
			
			id = GL11.glGenTextures();
			GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
	
			GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
			GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);
					
			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.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);
			
			GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buff);
			
			// store data
			tD = new Texture(id, width, height);
			if(stream != null)
				stream.close();
			
		} catch (IOException e1) { e1.printStackTrace(); }
		
		return tD;
	}

EDIT: for credit: I think this is out of a tutorial. May be from ClickerMonkey of davedes.

Thanks for that,

And then with the Texture returned, is that what you bind with when you want to use it?

I’m just confused as to how you use the texture when it is loaded?

Thanks again

I usually have a wrapper class for that, that I call Image. Every Image get’s stored in a hasmap when loaded:

private HashMap<String,Image> dataBase; // attribute in my TextureManager class

Adding a texture is done with this method:

	public void addTexture(String name, String path){
		if(dataBase.containsKey(name))
			System.err.println("Image with key " + name + " already in database!");
		else{
			Texture tex = loadTexture(path);
			dataBase.put(name, new Image(tex));
		}
	}

And getting an Image back from the textureManager with this method:

public Image getImage(String name){
		return dataBase.get(name);
	}

Every Image then has some methods to obtain any data like the corresponding texture ID or things like that. Of course images can be drawn with this method:

public void draw(SpriteBatch batch, float x, float y, float z, int f){
		frame = f;
		batch.draw(this, (int)x, (int)y, z);
	}

As you see, a SpriteBatch is needed for this. The SpriteBatch class then does binding for the texture.
This system is some work and takes some time to be done, but it is a huge help.

Yeah I have something similiar to that with my TextureManager, but where does Texture object come from? I’m trying to instantiate one like you but getting an error saying I cannot instantiate this object? Which are you using, the slick util one?

Is it texture that gets bound in your render block code?

Sorry for all the questions, just getting a tad confused.

I have to add, that I am confusing a few things here, because I was usually working in 2D environments:
At the moment I am not using any batch class for textures. I am manually binding them with my TextureManager.

Here is what I do in my Chunk class:

// texture stuff
		TextureManager tM = TextureManager.getTextureManager();
		if(tM.getTexture("tex") == null){
			tM.addTexture("tex", "img/testTextur.png");
			tM.addTexture("tex2", "img/testTextur2.png");
			tM.addTexture("controls", "img/controls_eng.png");
		}
		tex = tM.getImage("tex2"); // tex is an attribute of type Image
		tM.bind(tex);

This sets up two box textures (Textur is not misspelled, this is in german, btw) and the controls info screen for the start of my alpha demo.

Then you wanted to know about how I get my textures working.
I got a pretty small class I called Texture. As you see, it contains some needed information about the texture:


import org.lwjgl.opengl.GL11;

public class Texture{
	
	private int texID;
	private int width;
	private int height;
	
	public Texture(int id, int w, int h){
		texID = id;
		width = w;
		height = h;
	}
	
	public int getImageWidth(){
		return width;
	}
	
	public int getImageHeight(){
		return height;
	}
	
	public int getTextureID(){
		return texID;
	}
	
	public void release(){
		GL11.glDeleteTextures(texID);
	}
}

This is just inspired by slickUtil. But I am not using slick anymore.

And as I previously said, the Texture gets wrapped in my Image class. This happens here:

public void addTexture(String name, String path){
		if(dataBase.containsKey(name))
			System.err.println("Image with key " + name + " already in database!");
		else{
			Texture tex = loadTexture(path);
			dataBase.put(name, new Image(tex)); // #### HERE ######## new Image(tex)
		}
	}

Thanks again,

I now have it working, it was a lesson in learn to walk before I can run! I’m new to OpenGL so really need to spend more time on some tutorials before jumping right in!

This is my new createChunkList method:


private int createChunkList() {

		tx = 0; ty = 0; tz = 0;
		int iHidden = 0;
		int iBlockCount = 0;
		boolean bLeft = !false;
		boolean bRight = !false;
		boolean bAbove = !false;
		boolean bBelow = !false;
		boolean bFront = !false;
		boolean bBack = !false;

		boolean bDefault = true;
		
    	glEnable(GL11.GL_CULL_FACE);
	    GL11.glEnable(GL_TEXTURE_2D);

		TextureBuffer texture = TextureManager.getInstance().getTexture(BlockType.BlockType_Grass);
		GL11.glBindTexture(GL_TEXTURE_2D, texture.textureId);

		for (int x = 0; x < Chunk.CHUNK_SIZE; x++) {
			for (int y = 0; y < Chunk.CHUNK_SIZE; y++) {
				for (int z = 0; z < Chunk.CHUNK_SIZE; z++) {

					glBegin(GL11.GL_QUADS);
					
					if(!GetBlock(x,y,z).IsActive())  // 
					{
						continue;
					}
					bLeft = bDefault;
					if(x > 0)
					{
						bLeft = Blocks[x-1][y][z].IsActive();
					} else bLeft = false;
					bRight = bDefault;
					if(x < Chunk.CHUNK_SIZE - 1)
					{
						bRight = Blocks[x+1][y][z].IsActive();
					} else bRight = false;
					bAbove = bDefault;
					if(y > 0)
					{
						bAbove = Blocks[x][y-1][z].IsActive();
					} else bAbove = false;
					bBelow = bDefault;
					if(y < Chunk.CHUNK_SIZE - 1)
					{
						bBelow = Blocks[x][y+1][z].IsActive();
					} else bBelow = false;
					bFront = bDefault;
					if(z > 0)
					{
						bFront = Blocks[x][y][z-1].IsActive();
					} else bFront = false;
					bBack = bDefault;
					if(z < Chunk.CHUNK_SIZE - 1)
					{
						bBack = Blocks[x][y][z+1].IsActive();
					} else bBack = false;
					
					boolean bResult = bLeft & bRight & bAbove &
							          bBelow & bFront & bBack;
					
					if(!bResult) // Block is not hidden by neighbouring blocks
					{
						tx = ((xOffset * Chunk.CHUNK_SIZE) ) + (x);// << 1);
	                    ty = ((yOffset * Chunk.CHUNK_SIZE) ) + (y);// << 1);
	                    tz = ((zOffset * Chunk.CHUNK_SIZE) ) + (z);// << 1);
	            	
//	            		GL11.glPolygonMode(GL11.GL_FRONT, GL11.GL_LINE);
					
						renderBlock(GetBlock(x,y,z).getType());
	          
						iBlockCount++;   // total of blocks that can be seen
					}
					else
						iHidden++;   // amount of blocks that are surrounded
				
				}
			}
		}
		glEnd();
    	glDisable(GL11.GL_CULL_FACE);

		return iBlockCount;

	}

Notice the call the my TextureManager to get the texture for a block type of grass - hard coded, but will sort out later.

My renderBlock method:


private void renderBlock(Block.BlockType type) {
	  
		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);
	}

Note the removal of the loading the texture - what was I thinking?!?!

And in my TextureManager class:


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();
			GL11.glEnable(GL11.GL_TEXTURE_2D);
			GL11.glBindTexture(GL_TEXTURE_2D, tb.textureId);
			GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
			GL11.glPixelStorei(GL11.GL_PACK_ALIGNMENT, 1);

			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.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S,
					GL11.GL_CLAMP);
			GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T,
					GL11.GL_CLAMP);

			GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA,
					tb.decoder.getWidth(), tb.decoder.getHeight(), 0,
					GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, tb.buf);
			

		} catch (Exception e) {
			System.out.println(e.getMessage());
			bLoaded = false;
		} finally {
			in.close();
		}
		return bLoaded;
	}

This loads the texture.

Need to check on the texture id also - to do, as it may already have been generated.

Many thanks for the help, and your raycasting looks cool on picking the blocks. How did you do your collision checks with the blocks? AABB’s?

Thanks