Access vertices by reference

Hello, I want modify mesh vertices and for better performace is change to modify vertices directly in any array? For example vertices[14] = …

Mesh verts use a float buffer since it's a lot faster so you'll have to modify them that way.

Can you explain me please how can be faster making and coping arrays of thousands vertices against changing several direct values in array? I found only way to make arraycopy, change it and set it back to mesh. How better can I use it?

I’m might not be understanding your question exactly.



Do you have just a few verts that are used multiple (thousands) of times and are thinking referencing these few verts in an array?  As I said buffers in Java are a lot faster than arrays (Java 7 will be faster at accessing arrays though: http://lingpipe-blog.com/2009/03/30/jdk-7-twice-as-fast-as-jdk-6-for-arrays-and-arithmetic/).  JME uses float buffers as the vertex buffers for a mesh, I mostly positive without rewriting some core classes that you must use these buffers to change the vertex data.  For an example on how to do this look at how a Quad is created: http://www.google.com/codesearch/p?hl=en#Z8vJL-JqpQ0/trunk/src/com/jme/scene/shape/Quad.java



I’m not exactly sure, but you might want to take a look at some LOD samples in the jmetest package or the wiki.  These might be what your looking for, since they help optimize mesh rendering, by rendering less verts when a object is farther from the camera.  Start here: http://www.google.com/codesearch/p?hl=en#Z8vJL-JqpQ0/trunk/src/jmetest/TutorialGuide/HelloLOD.java



Just wondering are you a native english speaker?  ;)  Not a problem if you are not.

Thank you for your help and sorry for my English :slight_smile:

In this case I don't need measure array vs buffer.

Imagine I have mesh with 10000 vertices and I need change 100 vertices.

I must get float buffer, read every vertex, build new buffer and set it back.

My question is can I modify these 100 vertices directly withou rebuilding buffer?

This is something done very often inside the engine… There's many ways to do it, for example using BufferUtils but the simplest way:


TriMesh mesh = ...
FloatBuffer verts = mesh.getVertexBuffer();
Vector3f vec = new Vector3f();
for (int i = 0; i < verts.capacity() / 3; i++){
      vec.set(verts.get(), verts.get(), verts.get());
      // do thing with "vec"
      // (optionally) a writeback
      verts.position(verts.position()-3);
      verts.put(vec.x).put(vec.y).put(vec.z);
}
verts.clear(); // set position and other params back to default


Note that this way is quite slow. The most efficent way (e.g if you need to process many meshes every frame) is to have a small float array like
private static float[] buf = new float[512];
and copy parts of the verticies there and then read from there. Reading from a direct-buffer in java incurs a native call which is quite bad in a loop like this.

Thank you Momoko.

Small float array you mean using of this functions?

verts.get(dst, offset, length)

verts.put(src, offset, length)

GeniuZ said:

Thank you Momoko.
Small float array you mean using of this functions?
verts.get(dst, offset, length)
verts.put(src, offset, length)


Yeah that's correct. But not a small array.. the bigger the better, but don't use more than 1 MB for all the data you need.