Only one block in chunk being drawn when using textures

[quote]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
[/quote]
I have no collision testing in my released alpha. I am currently fixing some issues with it. Then I will update my thread. :slight_smile: But it will be AABB/AABB collision testing.
Glad you like the raycasting. I am fascinated by it too. I guess you found the source for it in my thread. It may be helpful.

BTW: In your posted source you are still calling glEnd() after every rendered cube. Doesn’t it work without this at this position?

Any thoughts on how to optimize this:


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++) {
					Block block = GetBlock(x,y,z);
					TextureBuffer texture = TextureManager.getInstance().getTexture(block.getType());
					GL11.glBindTexture(GL_TEXTURE_2D, texture.textureId);

					glBegin(GL11.GL_QUADS);

// etc - render block

The glBindTexture is slowing the code down a little, if I move it out of the loop it is fine, but obviously all the blocks would show the same texture.

I guess it comes down to checking if texture has already been bound as previously mentioned in the thread?

Thanks

Hi,

You mean the glEnd in my createChunkList code? All works with it where it is?

Yep, raycasting looks cool, how do you get the direction the player is facing, this some
simple trig?

Just wanting to optimize the binding of different textures…

@steg90

You are making three major mistakes:

  1. The off_vertex method never get’s it’s tx/ty/tz values.
  2. You are not using a single texture for the block textures.
  3. You are using glBegin/glEnd wrong (Wrong placed).
  • Longor1996

No, like Riven said:

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
     [...]
      off_glVertex3f(-0.5f, 0.5f, -0.5f);   // Top Left Of The Texture and Quad      
        GL11.glEnd(); // this one here ########################
        glColor3f(1,1,1);
   }

[quote]Yep, raycasting looks cool, how do you get the direction the player is facing, this some
simple trig?
[/quote]
I have made my own Ray class and it takes an origin of Vector3 and a uniform delta of Vector3. Then the uniform vector gets scaled by scalarProduct.

uniRay.setPosition(-pos.getX(),-pos.getY(),-pos.getZ());
		uniRay.setDelta((float)Math.sin(Math.toRadians(-rotY))*(float)-Math.cos(Math.toRadians(-rotX)),
									(float)Math.sin(Math.toRadians(-rotX)),
									(float)-Math.cos(Math.toRadians(-rotY))*(float)Math.cos(Math.toRadians(-rotX)));
		uniRay.scalarProduct(RAY_LENGTH); // change length of the uniform vector

The rotX variables are the rotation in degrees on every axis by the player.

@Longor1996

  1. The off_vertex does get the tx/ty/tz values ?!

  2. Single texture for the block textures - you mean I’m binding them again?? Or have one big texture which has multiple textures in it?

  3. glBegin / glEnd - in my createChunkList method?

I appreciate your advice @Longor1996

Oops, lol, removed it now. Thanks for pointing it out guys.

You have lost me with the ray class, guess I need to research some ray cast stuff, is the game math book any good, I notice there is a new edition?

Hi.

  1. Where? I can’t see any getter/setter that sets the values for the method.

  2. Yep. Multiple textures in a single Texture. This is the way to go. (Google: Tile Atlas/Map)

  3. Yep. You are using them wrong. You only need one at the beginning of the nested loop that draws the blocks, and at the end of the nested loop.

  4. Thanks!

  • Longor1996

If you really study math with this one, you will be up to the task of 3D game programming.
http://gamemath.com/about-the-book/

I am not advertising, I just love this book as it helped me so much btw. :slight_smile:
But you can of course also do it without it.

@Sparky83 I will get that book :slight_smile:

@Longor1996 - I originally use to have the glBegin out of the nested loops, my issue is needing to bind the texture before the glBegin and to do that I use:

GetBlock(x,y,z);


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++) {
					Block block = GetBlock(x,y,z);
					TextureBuffer texture = TextureManager.getInstance().getTexture(block.getType());
					GL11.glBindTexture(GL_TEXTURE_2D, texture.textureId);

					glBegin(GL11.GL_QUADS);

Need another way of getting the current block in order to get its texture id so I can bind its texture before the glBegin…

Is there a way of binding a texture to a block before the above stage?

Thanks

If you wan’t to give every cube it’s own texture, you have to use glBegin/glEnd inside the nested loop.
And this will give you very big performance drops!

As I said: Use a texture atlas! Then you have to bind the atlas only once right before drawing the chunks and that’s it.

  • Longor1996

Can this be done even when using displaylists?

Thanks,
Steve

Yes. You just have to bind an texture atlas,
and then specify the texture coordinates per face in a way that only a defined portion af the atlas is visible.

Say, you atlas contains 4 textures, and has a power-of-two size of 32x32.
Then the code to use only the texture in the top left corner of the atlas is:


glTexCoord(0,0);
glVertex(0,0);
glTexCoord(0.5,0);
glVertex(1,0);
glTexCoord(0.5,0.5);
glVertex(1,1);
glTexCoord(0,0.5);
glVertex(0,1);

  • Longor1996

Many thanks again @Longor1996, learning so much from you!

Im always happy to help!

Oh, and there is a lot more of things that you can do to optimize your voxel-engine.
But some of them are invented by myself and therefore secret!

  • Longor1996

Thanks @Longor1996

Well, once I get more done to it I will look at optimising :wink:

Hi steg,

Hope your project is going well. I posted my entire code in my thread, it uses a texture atlas 16x16 sprites

It’s 512 x512 texture, so 1/16 = 0.0625 for each offset texture. Gives you quite a few block to play with. For grass, the offsets are just changed so you can draw the sides an top differently. See – GrassBlock !

This forum is not a chat room.

Ok, sorry about that.

Think I’ve worked out texture atlas.

I’ve got a texture atlas of 1024x1024, textures are 64x64.

So, that is 1024 / 64 = 16 textures wide

1024 / 64 = 16 textures high

Then I just do 1 / 16 to give texture x-coordinate (i.e. 0.0625f )
and this is the same for texture y-coordinate.

So, to get 4th texture, I use this:


GL11.glTexCoord2f(4.0f * 0.0625f, 1 * 0.0625f);
GL11.glVertex3f(-0.1f, 0.1f, -0.1f);	// Top Left Of The Texture and Quad
GL11.glTexCoord2f(4.0f * 0.0625f, 0.0f * 0.0625f);
GL11.glVertex3f(-0.1f, 0.1f, 0.1f);	// Bottom Left Of The Texture and Quad
GL11.glTexCoord2f(5 * 0.0625f, 0.0f * 0.0625f);
GL11.glVertex3f( 0.1f, 0.1f, 0.1f);	// Bottom Right Of The Texture and Quad
GL11.glTexCoord2f(5 * 0.0625f, 1 * 0.0625f);
GL11.glVertex3f( 0.1f, 0.1f, -0.1f);	// Top Right Of The Texture and Quad

Hope this may help somebody?!

Thanks,
Steve

@Vermeer,

It is going slowly, lol, been busy working.

Thanks with regards to textures, worked it out now, going to use atlas so only one texture bind.

Will have a look at your code if I get chance, although I do enjoy trying to work things out - oh and the help on these forums is invaluable!

It is great to see so many people working on these cube worlds, sure has given me an insight into using OpenGL. Thanks everyone!