Get vertices and indices from meshes


I know there may be some topics about this, and I’ve done a search, but still I couldn’t find what I need. I have this navigation mesh generator library, and the input for the nav mesh generation is the scene itself I want to generate the mesh from. So, as far I as I know, I need to send to it the vertices and the indices. Looking at the lib’s javadoc, I got this


build(float[] vertices, int[] indices)

  • @param vertices The source geometry vertices in the form (x, y, z)
  • @param indices The triangle mesh vertices in the form
  • (vertA, vertB, vertC), wrapped clockwise.[/java]

    So the solution for the vertices parameter should be this:


    float[] vertices = vectorToFloat(getVertices(SPATIAL));

    public float[] vectorToFloat(Vector3f[] vec) {

    float[] vertices = new float[vec.length * 3];

    int i = -1;

    int k = 0;

    while (k < vec.length) {

    vertices[++i] = vec[k].x;

    vertices[++i] = vec[k].y;

    vertices[++i] = vec[k].z;



    return vertices;


    public Vector3f[] getVertices(Spatial s) {

    if (s instanceof Geometry) {

    Geometry geometry = (Geometry) s;

    FloatBuffer vertexBuffer = geometry.getMesh().getFloatBuffer(Type.Position);

    return BufferUtils.getVector3Array(vertexBuffer);

    } else if (s instanceof Node) {

    Node n = (Node) s;

    ArrayList<Vector3f[]> array = new ArrayList<Vector3f[]>();

    for (Spatial ss : n.getChildren()) {



    int count = 0;

    for (Vector3f[] vec : array) {

    count += vec.length;


    Vector3f[] returnn = new Vector3f[count];

    count = -1;

    for (Vector3f[] vec : array) {

    for (int i = 0; i < vec.length; i++) {

    returnn[++count] = vec;



    return returnn;


    return new Vector3f[0];


    So far so good. And the solution for the second parameter? Can someone help me out here?

Looking at the Navigation Mesh library in the jME plugins there is a solution:

[java] FloatBuffer pb = mesh.getFloatBuffer(Type.Position);

IndexBuffer ib = mesh.getIndexBuffer();

// copy positions to float array

float[] positions = new float[pb.capacity()];



// generate int array of indices

int[] indices = new int[ib.size()];

for (int i = 0; i < indices.length; i++) {

indices = ib.get(i);


I had already seen this like 1-2 months ago, but I totally forgot, when I was looking into NavMesh generation and the NMGen