Updating Mesh Vertices

I have a spherical mesh that I wish to re-render on key press. However when I attempt to do this it takes quite a bit of time and the result is in no way right. I was wondering if there was a way to update the mesh more quickly and besides that to re-render it correctly. Here is the code used to re-render the mesh when a key is pressed.
[java]
for(int i = 0; i<vertices.length; i++){
vertices[i] = setup(sphere_points[i], seed, lacunarity, vertices.length, i );
// setup is the method which generates the mesh-vertices
}
mesh.clearBuffer(Type.Position);
mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals(vertices)));
mesh.setBuffer(Type.Color, 4, getColors());
mesh.updateBound();
geo.setMesh(mesh);
[/java]

Thanks for any help, I apologize in advance if I am doing something overtly wrong…

You create new buffers, theres no need for that if the size doesn’t change, just modify the existing ones:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_meshes

Well, and how many points are we talking about? What is “quite a bit of time”? And what is “in no way right”?

How long does the for loop take? How do you know it is producing good results?

@pspeed said: Well, and how many points are we talking about? What is "quite a bit of time"? And what is "in no way right"?

How long does the for loop take? How do you know it is producing good results?

upwards of 20,000 vertices. The loop right now takes about 1 minute and a half and the result is a severely distorted image of the original object.

@okelly4408 said: upwards of 20,000 vertices. The loop right now takes about 1 minute and a half and the result is a severely distorted image of the original object.

Well, then it’s the loop taking the time and not the mesh update, right?

The code calling the mesh looks pretty straight forward. I don’t see any problems with it so the problem must be with the data generated in that loop.

I know this is an old post, but I have a question

You create new buffers, theres no need for that if the size doesn’t change, just modify the existing ones:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_meshes8

@Normen or anyone else that knows. When you say modify existing buffers how do I get access to them and how do I set them?
I want to update the z value on the vertices postion and set the colorArray value but I do not know how to convert the Buffer back into the original Array type

Vector3f[] vertices =  mesh.getBuffer(VertexBuffer.Type.Position).???;  
 float[] colorArray =  mesh.getBuffer(VertexBuffer.Type.Color).???;  

Thanks for any help

Write directly into the buffers. Don’t convert them to arrays.

FloatBuffer posData  =  (FloatBuffer) mesh.getBuffer(VertexBuffer.Type.Position).getData();  
FloatBuffer colorData =  (FloatBuffer)mesh.getBuffer(VertexBuffer.Type.Color).getData();

//write to buffers, see how to write into a FloatBuffer on google, that's very classic java.io stuff.

mesh.getBuffer(VertexBuffer.Type.Position).setData(posData);
mesh.getBuffer(VertexBuffer.Type.Color).setData(colorData);

If you want to write more data into the buffer than the existing capacity of the buffer, there is a convenience “ensureLargeEnough” method in JME BufferUtils. (unfortunately this method only works for buffers :wink: )

2 Likes