Creating mesh data from list of Triangle objects

Is there an easy way to convert a Triangle object to a Mesh with the correct vertex and index buffer and texture coordinates? I’m trying to build the terrain for a level from a scalar field with marching cubes and I just stored all the data in a list of Triangles. I’m pretty new to JME so I just thought this was the proper class to use for this, but I’m starting to think it was a bad idea. Is there a better way to do this?

Edit: After playing around a while I’ve determined that the problem is in setting the indices in the correct order. I can loop through the list of triangles and build the vertex buffer fine, but I can’t think of a good way to make sure that the indices will be set in the right order since the triangles can have any orientation.

It’s a bit less efficient but you can set the backface culling off and then the orientation of the faces doesn’t matter…

I thought of doing either that or just having a complimentary set of triangles for every cube arrangement, but since I want to have destructible terrain and will consequently be looping through the field quite often, I need to be as efficient as possible.

I’ve been thining about it for a while, and the best solution I can come up with right now is to make a multidimensional array of float values, where each entry represents the triangles for a possible state of a grid cell in the scalar field. If all the values are defined in the array in the correct order (counter clockwise), I should be able to just build the index buffer with a simple for loop that counts up to the size of the vertex buffer and adds the value of i to the index buffer each step. If this works, I think it will be an extremely fast and efficient solution. If anyone sees a flaw in my reasoning I would really like to hear it since this method will take an extremely long time to implement (there are 256 unique cases!)

As an approach it sounds reasonable although you will need to be careful with all the corner cases and details.

You should look at the existing terrain stuff and also the cube world implementation in the plugins.

Will you ever have overhangs or completely vertical walls in your terrain? If not, you can just see which side of triangle is most ‘up’ and chose the winding based on that. If you do have them, then it is not possible to determine it in most generic case - you will need to provide some extra information to your generated triangle (like normal) - which might be easy to produce at generation time. Then, use normal to decide which way to wind the triangle.

Okay I figured it out. In case anyone is interested, I define two static arrays, one representing the vertices for each case, the other representing the index order. There’s one index set per unique case excluding symmetries, so every entry in the vertex array is ordered so that the points get picked in the right order, defined by the index array entry for the number of points under the isosurface. That probably made no sense, but it works perfectly. I think it should be inceredibly fast as well, since it’s basically all array lookups and I’m writing directly to the mesh buffers instead of storing the data in some other construct.