Jump to content

  • Log In with Google      Sign In   
  • Create Account

chunks of cubes


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 24 September 2013 - 12:36 PM

A few months ago i was working on a voxel engine, but gave up after facing a lot of problems. Ive done a lot of 2d and some 3d programming since then so i decided to give this another shot.

I have a cube class that draws a singke textured cube, using a BuildFace method. This method will be very useful when culling the sides of cubes. Im not dealing with indices right now, i only have a vertex buffer. The cube constructor has three parameters, graphicsdevice, a vector3 location, and a texture.

My next step is figuring out how to store cubes in chunks of 50x50x50. I think that would be reasonable enough speed-wise.
Each chunk would have its own vertex buffer im guessing? but what confuses me is how to add cubes to that buffer. What kind of helper methods would i need? If im going for the minimum amount of draw calls id somehow need to use one giant buffer.

The other thing is the actual draw call itself. Looping through a chunk of cubes and calling each cubes draw method doesnt seem very effective and it hasn't worked in the past. But what if a cube has a different texture? I would want a single draw call from each chunk.

Im trying to take this as slow as possible so i.don't end up with the mess i had months ago :P

If you see a post from me, you can safely assume its C# and XNA :)


Sponsor:

#2 Ravyne   GDNet+   -  Reputation: 8160

Like
0Likes
Like

Posted 24 September 2013 - 01:58 PM

If you want to draw chunks in one call, what I'd do is maintain the cube data, vertex buffer, and index buffer separately, you don't want to waste time drawing faces that couldn't possibly be seen, like the common face of two adjacent cubes.

 

Have all the textures for the faces in a single texture. The texture coordinates you need to give to each face are implied by the face (North, South, East, West, Top, Bottom), and the kind of the cube (Dirt, rock, whatever). Really, the vertices never change -- they might belong to 0, one, or many cubes, but they don't change. You can probably have a single, unchanging vertex buffer for all chunks, even, or compute it as needed. Build the chunk index buffer initially by iterating over the cubes and adding indexes into the vertex buffer for every face that's shared with an adjacent cube that's transparent (e.g. "air", water, ice, glass), as well as the very outside faces (if you want each chunk fully independent of its neighbors for drawing purposes). A further possible optimization would be to then make a second pass and collect together similar adjacent faces in a triangle strip, but I suspect its more trouble than its worth.

 

Also, the chunk size is not arbitrary, it's size is bound by the worst-case scenario size of the vertex and index buffer (if your aim is a single draw call per chunk). The number of vertices required is (W+1) x (H+1) x (D+1) where W, H, and D are the width, height and depth of a chunk measured in cubes. Assuming cubes only,  the worst-case scenario for indices is chunk filled with semi-transparent cubes (like ice or glass -- you can see through it, but it has a face and a texture). With a triangle list, this is 48 indices per cube, so 48 x INDEX_SIZE x (W+1) x (H+1) x (D+1) < MAX_INDEX_BUFFER_SIZE will give you the largest chunk that's drawable in a single call. allowing blocks to have more complicated geometry greatly complicates this scheme.

 

You could also just used instance rendering, and draw each chunk in several calls, one for each permutation of geometry, texture, orientation, etc. You could find which cubes need to be draw similar to above, or possibly by ray-casting.



#3 burnt_casadilla   Members   -  Reputation: 443

Like
0Likes
Like

Posted 24 September 2013 - 02:40 PM

There's really only two types of block, dirt and grass. If a blocks top face is visible its grass. Empty blocks could be made with an IsActive bool i would think. The texture for each block is the same on all.sides.


While adding each block to the chunk, i coukd check if sides are adjacent, but i don't know how to do that.

I have looked at instance rendering, but the blocks will be manipulated almost all the time so i don't know if that's the best choice right now

If you see a post from me, you can safely assume its C# and XNA :)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS