Adding mesh vertices piecemeal

This is sort of related to http://www.twolivesleft.com/Codea/Talk/discussion/754/modifying-mesh-vertices, but not quite. I’d like a way to add vertices piecemeal to a mesh. The mesh:vertex() method talked about in the linked discussion works - so far as I can tell - only on existing mesh vertices.

I’m using a single mesh to render a conglomeration of objects. This seemed to me to be the most efficient method. So I add the objects to the mesh by constructing a huge list of triangles and then assigning it to the mesh. That’s fine in setup() (though with my meshes it takes a non-trivial amount of time!), but if I want to add or delete one of those objects mid-program, I have to rebuild the entire list of mesh vertices - so far as I can tell. I’d quite like to be able to simply erase a segment and add a segment of vertices. Something like what the mesh:addRect() method currently does, but more flexibly (as that seems limited to genuine rectangles in some plane). Deleting vertices might be a bit of a minefield (especially with keeping track of indices), but certainly adding shouldn’t be an issue, and for extras I’d like mesh:addVertex() to be able to take a list of vertices (don’t mind if it is mesh:addVertex({a,b,c,d}) or mesh:addVertex(a,b,c,d)) and return one of the indices of the added vertices (not sure if the first or last would be more useful).

Could this be added, or if it is already there and I’ve missed it … where is it?

I agree this is a useful feature. mesh.addVertex seems like the right API for it, too.

For best performance you generally want to pre-allocate your meshes.

To delete vertices from the mesh, rather than actually removing vertices, a good thing to do is to set them to an invalid triangle — e.g., all vertices placed at one point. This allows you to maintain the buffer size while visually growing or shrinking the mesh.

I’m glad to hear that about deleting vertices as that’s what I’ve just implemented! I did it that way because I wanted to temporarily remove a part of a mesh, and then recreate it a bit later on.

For the addVertex, at the moment I have lots of routines of the form AddCube, AddJewel, AddThisThatOrTheOther and at the moment they have to work on tables rather than on meshes which makes them a little inflexible and harder to be modular. So most of the time I do this in setup where performance is less of an issue, but I’m sure it wouldn’t take me long before I figured out a reason why I wanted it in draw() as well.