Loading models using libGDX

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
Notice that it contains three files, which must be included in the same folder:

 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 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值