Adding/Updating Model meshes at runtime

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

Adding/Updating Model meshes at runtime

Postby ThomasOBVI » Mon Jun 12, 2017 4:14 pm

Hello,
I'm new to LibGDX, I must say that I don't really get it with the ModelBuilder, MeshBuilder and the MeshPartBuilder.
What I'm trying to achieve is simple, I'm receiving data from an ordoned point cloud (with everything I need, vertices, faces, normal, colors) and I want to display it with OpenGL. I did that before with Rajawali OpenGL Library. But I decided to switch to LibGDX, because it's more complete.

So before starting, All the values of the vertices are good !

The thing is, when Google Tango have a valid "mesh", it register it, and after I send it to LibGDX. So in my application I have an array (an HashMap to be more precise) of point clouds data. What I want to achieve is, each time I get a new "tangoMesh" -> IF it's a new one : I add it into a TangoModel / ELSE -> I find the good mesh into my model, and I update it with the new data.
I want to have only one model, but with multiple meshes into.

My problems : when I try to add the new mesh into my model, it doesn't work at all, I have to recreate a brand new model each time (with ModelBuilder) if I want my model updated. Which is really heavy on performance.

Here is some code :
Code: Select all
//The create() function of my app
//TANGO
        modelBuilder.begin();
        Node mainTangoNode = modelBuilder.node();
        mainTangoNode.id = MAIN_NODE_ID;
        //Create the tangoMeshPart
        MeshPartBuilder meshBuilder;
        meshBuilder = modelBuilder.part("mainPart", GL20.GL_TRIANGLES, VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal, new Material());

        mTangoModel = modelBuilder.end();
        mTangoModelInstance = new ModelInstance(mTangoModel);
        //Add the renderable objects into the array

        mInstanceToRender = new Array<ModelInstance>();
        mInstanceToRender.add(mFPCameraHelper);
        mInstanceToRender.add(mTangoModelInstance);

        mModelsBatch = new ModelBatch();


Code: Select all
//The function where I update the model (vertices and faces was float[] and short[] created before)

MeshBuilder meshBuilder = new MeshBuilder();
        meshBuilder.begin(VertexAttributes.Usage.Position | VertexAttributes.Usage.ColorPacked);
        mTangoModel.meshParts.set(0, meshBuilder.part("part1", GL20.GL_TRIANGLES));
        meshBuilder.addMesh(vertices, faces);
        Mesh mesh = meshBuilder.end();
        mTangoModel.meshes.add(mesh);

        //If the hash already exist (look at mesh index)
        //mTangoModel.meshes.add(meshBuilder.end());


        mInstanceToRender.removeValue(mTangoModelInstance, false);
        mTangoModelInstance = new ModelInstance(mTangoModel);
        mInstanceToRender.add(mTangoModelInstance);


If anybody have an idea to do this without recreating the Model with the ModelBuilder ?
Thanks in advance !
ThomasOBVI
 
Posts: 5
Joined: Mon Jun 12, 2017 12:29 pm

Re: Adding/Updating Model meshes at runtime

Postby litelitelite » Tue Oct 31, 2017 9:17 pm

Just to share my experience of trying to do something similar (I think). In summary, my deformable object was represented by a Mesh rather than a Model. The method used was:

Create a mesh (I didn't set indices - "faces" - as this improved the performance of calculating the new mesh. Instead I just repeated vertices to create the triangles):
Code: Select all
private float[] _vertices = new float[32768]; // Good starting size for me
private Mesh _mesh = new Mesh(true, _vertices.length, 0, VertexAttribute.Position(), VertexAttribute.TexCoords(0));


Then update the vertices later however you want:
Code: Select all
    // XYZ
    _vertices[_verticesCount++] = x;
    _vertices[_verticesCount++] = y;
    _vertices[_verticesCount++] = z;

    // UV
    _vertices[_verticesCount++] = u;
    _vertices[_verticesCount++] = v;


Then assign the new vertices:
Code: Select all
    _mesh.setVertices(_vertices, 0, _verticesCount);


The create my own renderable from the mesh and material etc.:
Code: Select all
  @Override
  public void getRenderables(final Array<Renderable> renderables, final Pool<Renderable> pool) {
    Renderable renderable = pool.obtain();
    renderable.material = _material;
    renderable.meshPart.mesh = _mesh;
    renderable.meshPart.offset = 0;
    renderable.meshPart.size = _mesh.getNumVertices();
    renderable.meshPart.primitiveType = GL20.GL_TRIANGLES;
    renderable.worldTransform.setToTranslation(getTranslation()); // (a Vector3)
    renderables.add(renderable);
  }


Hope that this helps! It is fast and you can see it in action for the deformable sand in this silly digging game I made https://www.youtube.com/watch?v=WpmF4KY6QH0
litelitelite
 
Posts: 11
Joined: Sat Jul 15, 2017 9:43 pm


Return to General Development

Who is online

Users browsing this forum: No registered users and 4 guests