LWJGL - Voxel Engine

Hey that’s the current state of the engine.

SIGCNIGiL4M


[s]Hello there, I just started the development of a voxel engine. I seperated the world to 100 chunks, each one containes 16x16x16 dirt blocks. So, I used the distance formula in order to draw only the chunks which are max 32 blocks away from me.

The problem is that when I am starting to go away from the chunks the first chunks that dissappear are the ones which are closer to me, when the opposite was supposed to happend. The whole idea doesn’t seem to work good. Can you see what’s wrong or can you recommend any other way (with an example) about how to draw only the closest chunks in a x radious?

The distance formula I used:[/s]

(float)Math.sqrt(Math.pow(camera.position.x - chunk.centerX,2) + Math.pow(camera.position.y - chunk.centerY,2) + Math.pow(camera.position.z - chunk.centerZ,2)) <  32*Block.width

Where Block.width = 0.7f;

wbVG8tneqoY

Thank you.

Hi

You could make it easier to start by just drawing the ones in a square around the player,

Pseudo code;

If(abs(Cam.position.x-chunk.centre.x)<chunk.width*howManyCuncksToDraw) && same for z

Then draw.

Also, it may be best to change the block width to 1f, this may make future things easier.

It’s looking good tho :slight_smile:

Thank you for your reply.

I get the exact same result. :clue:

Math.abs(camera.position.x - chunk.centerX) < World.CHUNK_WIDTH && Math.abs(camera.position.z - chunk.centerZ) < World.CHUNK_LENGTH

Yep, I agree… The problem must lie somewhere else, what about how you

Draw the chunks… Are you offsetting the vertecies or chunks, or translating them?

Offsetting.

I tried this code in my game and it works as expected.

for(int c= 0;c<numberOfChunks;c++){
			if(Math.abs(chunks[c].getX()-(int)xpos)<CHUNK_SIZE*2){
				if(Math.abs(chunks[c].getZ()-(int)zpos)<CHUNK_SIZE*2){
				GL11.glLoadIdentity();
				GL11.glRotatef(lookupdown, 1.0f, 0, 0);
				GL11.glRotatef(360.0f - yrot, 0, 1.0f, 0);
				GL11.glTranslatef(-xpos, camHieght , -zpos);
				GL11.glBindTexture(GL11.GL_TEXTURE_2D, 1);
				render4(chunks[c]);
				}
			}
		}

Did you offset your vertecies per chunk, or use the world co-ordinates. Mine are per cunk.

I hope this may be of some help, perhapse more experienced mebers can identify the problem you have?

I offset the cubes using common axis for all chunks.

:o

[icode]Math.pow(…);[/icode] ?!? You shouldn’t use that one. It’s horribly slow. It used for mathematical expressions like [icode]Math.pow(10, 1.45);[/icode].

If you want to have 10 squared you do this:

10*10

If you want to have an expression to be squared then you save it into a variable and multiply it by itself:

int expression = ... ;
int squared = expression * expression;

Think point to plane distance.

I am pretty sure, it gonna have the same result.

Maybe this is a silly question, but are you sure that when you move the camera, you are moving it in the correct direction without any kind of translate or inverted axis? :slight_smile:

Mike

view frustum is a near plane, a far plane + top,bottom,right,left. near/far are just offsets from one another and a uniform grid is easy to parametrize in terms of the planes coordinate frame, you can then hierarchically (and very cheaply) reject points…aka outside of view.

Dude, you are awesome, thank you. Yeah, the axis where inverted in the camera class, not in the distance formula as I thought in the first place. It works now.

Leave this open if it is possible because I am sure much more problems will popup, soon.

I guess the wrong post was quoted? :slight_smile:

No problem at all, will you stop ruining other people’s buildings in SoF now? :wink:

Mike

Another two problems. I cannot really see what causes them.

The first one is that at the left bottom corner of the bottom side of every chunk is not being drawn correctly. It seems that for some reason it grabs some parts of the neighbour texture. (Keep in mind that I use the old minecraft spritesheet for testing.)

The second one is that the back side of the grass blocks is not being rendered correctly, too. I found out that when I remove the texture from the top side of the grass block this bug dissapears.

First one:

Second one:

Normal block render function: (The chunk render function calls this method for each block.)
http://pastebin.java-gaming.org/5b8dd3c1753

Grass block render: (I used a differend method because it doesn’t folow the same pattern. It takes the texture rotated, and it uses another texture for top side.)
http://pastebin.java-gaming.org/b8ddc47135e

miI4i0Qp76k

Your problems are caused by incorrect texture coordinates.

Unfortunately, trying to read the provided code hurts my brain. :wink:

Check over all the coordinates.

Also: DON’T USE GL_QUADS!

I use display lists and and I have reduced the use of some rendering commands as much as possible.

Also, I don’t believe that it comes from a coords mistake, because I had the same problem at an old project. (Except if I made the same mistake back then.)

If these errors are caused by coords mistakes then these mistakes are surerly good hidden.

PS: My next step would be to use vbos.

Well, it definitely looks to me like a coords mistake.
I think that because the textures are warped towards the corners - a common sign of misplaced tex coords.

It is definitely looks like a texture coord issue, maybe not in the code you posted, but somewhere it is. You need to post more code to be able to say where the issue is.

Mike