Updating/Growing a Mesh

I’m experiencing some unintended and unwanted behavior while trying to ‘grow’ a mesh. By ‘grow’ I mean the adding of new vertices and formation of new polys within an existing mesh.



Now my weird bug is this. I have a method updateGeometry() that is the following

public void updateGeometry()

{

// Setting buffers

mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices.getArray()));

mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoords.getArray()));

mesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes.getArray()));

mesh.updateBound();



// Geometry creation

geom.setMesh(mesh);

geom.updateModelBound();

geom.setMaterial(mat);

}



NOTE: vertices.getArray() literally ONLY returns the array of Vector3f objects, it does not do anything else at all to the array. Same for texCoords and indexes getArray() methods.



If I add multiple surfaces to my arrays and call updateGeometry() once at the end, then all the surfaces are display properly, but if I add one surface at a time, calling updateGeometry() inbetween each, then (it seems) that only the very first surface added is ever displayed.



I’m relatively new here and I don’t understand much about how buffers work beyond what the Custom Mesh Shapes tutorial taught me. I found a post from a few months back that mentioned using updateData instead of setMesh, but there doesn’t appear to be an updateData or updateMesh method available for class Mesh.



Any thoughts internet?

After you added the position buffer do this:

[java]

mesh.getBuffer(Type.Position).clear();

[/java]

I’m sorry but that line is invalid. There is no clear() method available. I found clearUpdateNeeded() but that accomplished nothing. I am running Alpha 4 now btw. I also found clearBuffer() but that only resulted in extremely odd looking polys if used after setting the position buffer. If used before setting the position buffer than nothing at all changed from when not using clear at all.

Nevermind internet, solved my problem. I changed the updateGeometry() to create a new mesh object, and then set that one’s buffers and make the geometry object use this new mesh. So whenever updateGeometry() is called the old mesh is discarded and a new one created. This works because my arrays are persistent since they belong to a different object and not just to the mesh.



Now if there is a more efficient way to update a mesh’s buffers instead of throwing it away I’d still like to know, but at least for now I have a working solution. ^.^

I think what normen meant is getFloatBuffer not getBuffer

That code does work, as in it compiles, but unfortunately it still results in the same bug. The mesh won’t update, only the initial surface exists. However, if I throw the mesh away and create a new one, the new one will contain both the old mesh’s polys as well as the new polys I add before calling the update.



My only concern is that throwing away the entire mesh and generating a new one sounds wasteful, but I don’t actually understand how buffers work so the idea of ‘clearing’ a buffer and reloading the array data into it might essentially be the same as deleting the mesh and creating a new one.



If there’s any good reading out there to be done on jME3’s buffers I’d like to know. I realize I can’t master all things of the universe, but I’d certainly like to be at least versed in some of these core concepts.



I also realize that worrying over micro-optimizing might be a little silly, seeing as I’m already using Java which according to all the C++ fanatics out there is the bane of all efficiency, but I am intending on these little pieces of my engine being used thousands or millions of times over and every little bit counts. It has to work, and it has to be coder friendly (easy to understand, easy to develop), which is why I chose Java, but it also have to run efficiently, at least as efficiently as I can manage.

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_meshes

:troll: