Best way to get all vertices for a mesh?

Hi,

I have a Mesh instance and need to get a list/set of all the vertices that compose it (so I can run inspection/analysis on each of their (x,y,z) coordinates).

I don’t see a getVertex(...) method but was thinking that I might be able to do something like:

Mesh mesh = getSomehow();
Vector3f v1, v2, v3;
List<Vector3f> allVertices = new ArrayList<Vector3f>();
for(int i = 0; i < mesh.getVertexCount(); i++) {
    // I *believe* this will populate all vectors with unique vertex info
    mesh.getTriangle(i, v1, v2, v3);

    allVertices.add(v1);
    allVertices.add(v2);
    allVertices.add(v3);
}

But this feels messy and possibly will generate duplicate indices (vertices) in the list. Either way, if there is a more straight-forward way to do this (ideally an API method or util class somewhere) I would rather use that approac.

Any thoughts? Thanks!

mesh.getBuffer(VertexBuffer.Type.Position)
Will give you all vertices, separated in their x, y, z components

Thanks @rickard - I see that method now and am looking at the VertexBuffer class.

Once I get that VertexBuffer instance, any idea as to how I can iterate through all the vertices and get their individual (x,y,z) components? I see a getComponentSize() as well as a getData(), but no obvious way of iterating the buffer no converting the buffered data into, say, a Vertex instance, etc. Thoughts? Thanks again!

FloatBuffer pos = (FloatBuffer)(mesh.getBuffer(VertexBuffer.Type.Position).getData());

and I think you are able to iterate float buffer, right?

Thanks @The_Leo, I did a search for the FloatBuffer class but don’t see it on GitHub, any idea where its hiding out? Once I can look at its source I should be able to figure out the best way to iterate its internal data.

Also, just to confirm, the FloatBuffer pos variable will contain all vertices for the mesh, right? I’m only asking because the variable name “pos” is throwing me off (position)? Thanks again!

Yes Position vertex buffer contains the vertices of a mesh.
FloatBuffer is part of java library.

eg. you can iterate like:

pos.rewind();
while(pos.hasRemaining()) {
      float x = pos.get();
      ...
}

Btw, do you know what is an IndexBuffer?

FloatBuffer is part of the core Java: FloatBuffer (Java Platform SE 7 )

Thanks both @The_Leo and @shamanDevel - I’ve never seen FloatBuffer before and figured it was part of jME3!

So it looks like there is only a single FloatBuffer#get() method…

So how would I extract Y and Z values here? For example:

pos.rewind();
while(pos.hasRemaining()) {
      float x = pos.get();
      float y = ???
      float z = ???
}

You go on with pos.get()… its x,y,z,x,y,z,x,y,z etc.

Ehhhhhh, thanks @normen

At the end of iteration, once I have all my X’s, Ys and Z’s, I assume it should be an error/exception if I don’t have equal numbers/counts of each?

If you already rendered that object, OpenGL would have complained before that I suppose :slight_smile: You don’t check for every text file if each byte has 8 bits do you? :wink:

Nope :slight_smile: thanks for the help, everyone!

Code that iterates over all of the vertexes in a mesh in order to update them:

Note: you haven’t really said why you are doing this which means we can’t forewarn you about other potential issues that may crop up.