The OBJ format isnot fully supportedand should be avoided.so we should use fbx-conv to convert our OBJ file to g3dj /g3db.
Later on in this tutorial we will seehow to convert a model to a more suitable file format.
If you use your own (OBJ) model, you might notice that it might not render correctly.
That's to be expected and will be resolved later on.
Notice that it contains three files, which must be included in the same folder:
- ship.obj: the wavefront model file we're going to load
- ship.mtl: the wavefront material file the model uses
- ship.png: the texture the material uses
ModelLoader loader = new ObjLoader();
model = loader.loadModel(Gdx.files.internal("data/ship.obj"));
instance = new ModelInstance(model);
or AssetManager
assets = new AssetManager();
assets.load("data/ship.obj", Model.class);
private void doneLoading() {
Model ship = assets.get("data/ship.obj", Model.class);
ModelInstance shipInstance = new ModelInstance(ship);
instances.add(shipInstance);
loading = false;
}
Let's go through the changes. We removed the Model instance and replaced it by the AssetManager. Instead of one ModelInstance we replaced it with an array of instances, which is more realistic scenario and allows us to render more instaces later on. We also added a flag to indicate if we are still loading.
Now in the Create method we create the asset manager and tell it to load the ship model. Next we set the loading flag, so we know we need to update the assetmanager. In our render method we check if the loading flag is set and if so we call assets.update(). If assets.update() return true we know it's done loading so we call a new method called doneLoading(). Also in the render method we render all the instances instead of just one. If the assets aren't loaded yet, this means the array is empty.
The new method doneLoading() fetches the ship model we just loaded, creates an instance called shipInstance and adds it to the instances array, causing it to be rendered. Finally we need to set the loading flag to false, so the assets.update() method isn't called anymore.
If you run this you'll see the output is the same as before. Although you might shortly see a black screen before the ship pops in, which is caused by the model being loaded asynchronous now.
Luckily there's fbx-conv, which converts models exported from modeling software to a format suitable for rendering using LibGDX. Unlike the name might suggest, fbx-conv is suitable for converting many file formats (including obj), although fbx is the preferred file format because almost every modeling application support that format. There are two file formats LibGDX support, g3dj (which is json textual for easy debugging) andg3db (binary which you should use on release, because it's smaller and faster to load).
// command line : fbx-conv-win32.exe -f -v myModel.fbx convertedModel.g3db