How to get the triangle vertex of a mesh?

I made a mesh with blender (actually I’ve imported an svg from inkscape), and I know that jme converts the mesh into triangles. I’d like to have a list of these triangles vertex, but I don’t know how to do it…
Thanks!

(The intended purpose is to feed these triangles to dyn4j)

Well I just did this code to iterate over each vertex.

public static void iterateOverMesh(Mesh m) {
        VertexBuffer vB = m.getBuffer(VertexBuffer.Type.Position);
        
        if (vB.getNumComponents() != 3) {
            throw new RuntimeException("Position Buffer doesn't have 3-component Vectors but " + vB.getNumComponents());
        }

        FloatBuffer buf = (FloatBuffer)vB.getDataReadOnly();
        Vector3f vTemp = new Vector3f();
        
        for (int i = 0; i < vB.getNumElements(); i++) {
            vTemp.set(buf.get(i * 3), buf.get(i * 3 + 1), buf.get(i * 3 + 2));
           // do stuff
    }

That way you could easily build an ArrayList<Vector3f> vertices (Note that you should use the capacity constructor and pass getNumElements for performance reasons).

Now you need to do the same with a shortBuffer for the IndexBuffer. There you could also grab three indices and store them as a Vector3f (for convenience).

Then something like:
Triangle = { vertices.get(v.x), vertices.get(v.y), vertices.get(v.z)};

1 Like

I’ve noticed that indexbuffer has only 1 components instead of 3.

And the content is:
0
1
2
3

Does this mean that, on a j3o loaded mesh the vertex are always layed out like this?

If you want to iterate over triangles then you could iterate over the triangles.
http://javadoc.jmonkeyengine.org/com/jme3/scene/Mesh.html#getTriangleCount--
http://javadoc.jmonkeyengine.org/com/jme3/scene/Mesh.html#getTriangle-int-com.jme3.math.Vector3f-com.jme3.math.Vector3f-com.jme3.math.Vector3f-

…then you don’t have to worry about whether it has an index buffer or not or if it’s int or short or whatever.

1 Like

This code

        Vector3f v1=new Vector3f();
        Vector3f v2=new Vector3f();
        Vector3f v3=new Vector3f();
        for (int i = 0; i < m.getTriangleCount(); i++) {
            m.getTriangle(i, v1,v2,v3);
            System.out.println(i+" ");
            System.out.println(v1);
            System.out.println(v2);
            System.out.println(v3);
        }

Gives me a
java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkIndex(Buffer.java:540)
at java.nio.DirectShortBufferU.get(DirectShortBufferU.java:253)
at com.jme3.scene.mesh.IndexShortBuffer.get(IndexShortBuffer.java:53)
at com.jme3.scene.mesh.WrappedIndexBuffer.get(WrappedIndexBuffer.java:80)
at com.jme3.scene.Mesh.getTriangle(Mesh.java:893)

Got it. The mesh is in LineStrip mode. This also explains why the indexbuffer has only 1 component.

Now to make blender make triangles instead of a linestrip… :thinking:

Line strip is very strange… what kind of shape is it in blender?

I draw a shape with inkscape, then import the svg

Then convert to j3o

So… I guess it’s just a wire frame and not an actual shape. You won’t have triangles in that case.

Check mesh mode and you’ll see.

Blender also shows “Faces: 0” in your case. Just pressing F will make a n-gon face which is then triangulated upon Import :wink:

I think blender hates me… F does nothing, googled for ngon but can’t pull the Mesh Face tool :frowning:
Alt-F moves the view…?

By the way, the shape is a bit more complex than it should:

Did you mark all the vertices before?

F should make a Face out of all selected vertices.
“Make Face” might be more helpful that the ngon thing

Selected vertices, pressed F and then

:frowning:

Strange. Try to add edges by selecting those unconnected vertices (two at a time ), click f.
Then you should have a watertight mesh.

Maybe you have to select the edges instead of vertices?

I needed to convert “curve to mesh” :slight_smile:

1 Like