8

I am working on a project that I take a "base" model/mesh, and I form it to look like a more defined terrain. I am having an issue, though, when I try to edit the mesh. This is basically how I do it: (assets is an AssetManager)

Model terrain = assets.get("terrain.g3db", Model.class);
Mesh template = null;
for (float x = 0; x <= maxX; x += 1f) {
    for (float z = 0; z <= maxZ; z += 1f) {
        Mesh m = template.copy(false);

        // get proper vertices here

        mesh.setVertices(vertices);
        terrain.calculateTransforms();

        terrain.meshes.set(0, mesh);

        ModelInstance terrainInstance = new ModelInstance(terrain);
        terrainInstance.transform.setToTranslation(x, 0, z);
        instances.add(terrainInstance);
    }
}    

The only issue is when I do that I just get a flat terrain. When I remove this line:

terrain.meshes.set(0, mesh);

I get the terrain, but every model instance has the same thing. To fix this, I have to remove that line, and add this one:

terrain.meshes.get(0).setVertices(vertices);

What I think is happening with the line that causes the flat map is when I set it, it removes anything that might have said, "hey, this mesh relates to this part of the model".

Screenshots:
With the line that causes a flat map: Picture
Without that line: Picture

UPDATE: Thanks to Xoppa's comment suggesting that I load a new model for each spot, but in doing that, I have to use a G3dModelLoader. I would like to use an AssetManager if that is even possible, but I don't think it is...

glennsl
  • 28,186
  • 12
  • 57
  • 75
Fishy
  • 1,275
  • 1
  • 12
  • 26
  • So, what's the question? – Xoppa Mar 07 '17 at 22:47
  • How do I fix it, so that I don't have the models just stacked into an array. I want the models to have meshes that are independent of each other. – Fishy Mar 07 '17 at 22:49
  • If you want to create multiple models then create multiple models. In your code you only have one model. – Xoppa Mar 07 '17 at 22:50
  • Although I am getting that model from an `AssetManager`. If I did not do that, do you think that would fix it? – Fishy Mar 07 '17 at 22:52
  • Please carefully read http://stackoverflow.com/help/how-to-ask and especially http://stackoverflow.com/help/mcve. Make sure to include all relevant information about your question (including your question) and the information required to reproduce your problem. Don't expect people to guess what your code might look like. – Xoppa Mar 07 '17 at 22:56

1 Answers1

3

To fix this, I made a custom loader that uses the G3dModelLoader. To make sure I do not have any memory leaks, I made two variables in the class, being the loader itself, and an array of the initialized models. One you run the #getNewModel() method it loads the new model, adds it to the list of initialized models, and returns the new model. In the #dispose() method, it just runs through the list of initialized models, and runs the dispose method on those models.

Fishy
  • 1,275
  • 1
  • 12
  • 26