How to update meshes on the fly?

So if you’ve seen me before in the forums, you’ll know that I have been working on a Voxel engine for my game. I have it mostly working now and I have included some methods to add/remove blocks from the scene. These methods work if I use them before I build (a method to construct the mesh) the mesh, but when I try to use them after the mesh doesn’t want to update.

My question is what must be done to update a mesh whilst the program is running and the mesh is being rendered?

Again, adding and removing the blocks isn’t the problem - I can do that. I just need to know any methods you need to use to update the mesh or geometry?

On your float buffers (vertex/normal/index) you need to call buffer.setUpdateNeeded(). Unless you directly call buffer.updateData(buffer), then it does that for you.

This will flag them to re-send the data to the video card.

@Sploreg

Thanks for the quick reply :slight_smile:

I have added in setUpdateNeeded() where I initialise my buffers and create the mesh geometry etc. And where I add a new set of vertices, indexes and tex-coords to my ArrayList’s I add in the updateData() method - still no luck :frowning:

I am using picking to get the closest collision from a ray sent out of the camera and a block should be placed there but nothing happens. Occasionally the program crashes with various errors due to re-submitting data to the mesh.

Thanks again :slight_smile:

If you want to perform collision you also need to call updateCollisionData() (or something like that)

Sounds like there is something else going wrong in there then, and possibly a threading issue with the errors you are getting.

Confirm that you are getting the mesh you want back from the geometry, and you are indeed setting the values in the buffers. And also if any of the mesh calculation is done on a background thread, use an UpdateControl or just make sure that the geometry is set on the openGL thread.

1 Like

I’ll try these and report back.

Thanks :slight_smile:

The problem isn’t with collision as I print out the location at which it should be placing the blocks and it gives me accurate results. The problem is I believe with setting the data. I am unsure of how a mesh operates once it is being rendered - since it is the geometry, do I need to update the geometry for the mesh or just the mesh itself?

What exactly you mean? No, not if you don’t change anything but the mesh. Whats the issue?

So I have two methods: addBlock() and removeBlock() - If I use these methods before I build my mesh (i.e. set all of the vertex buffers etc.) it adds the blocks/removes them from the specified location just fine. However, if I call these methods after the vertex buffer information has been added, nothing happens. I have tried calling the updateData() methods on my mesh but no luck, I also set each buffer to setUpdateNeeded(). Now I was unsure about how a mesh operates once it is in the scene graph but possibly the mesh is updating but maybe you need to create a new geometry for the new updated mesh? I was just wondering what needs to change/update once I have called updateData(buffer) after adding/removing a block.

Hm, normally you only need to either change the data and call setUpdateNeeded or you set the data newly using updateData… No need to recreate the geometry…

I’ll have a thorough look at the code to see if it is a silly error…

I managed to fix it - it was a silly error. Calling updateData() worked fine, it was because I needed to place the block at x, y + 1, z because the block needs to be created on top of the other one.

Thanks for the help guys :slight_smile: