LIBGDX New 3D API Tutorial--Loading models with LIBGDX

Source: Internet
Author: User
Tags libgdx


In the previous tutorial, we have seen how to set the Libgdx render 3D scene. We've set up the camera, added some lights and rendered a green box. Now let's add a more interesting thing than a box model.

You can get from your favorite modeling application or use an existing model. I found the gdx-invaders inside the spaceship model file, you can click here to download. You can unzip the file and place it in the assets directory of the Android project. Note that it contains three files that need to be placed in the same folder:

Ship.obj: The wavefront model file we want to load
Material file used by the Ship.mtl:wavefront model file
Ship.png: Texture file

Now let's modify the Basic3dtest and replace the original square box with the model:

    1. public class Basic3dtest implements Applicationlistener {
    2. ...
    3. @Override
    4. public void Create () {
    5. modelbatch = new Modelbatch ();
    6. lights = new Lights ();
    7. Lights.ambientLight.set (0.4f, 0.4f, 0.4f, 1f);
    8. Lights.add (New DirectionalLight (). Set (0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
    9. cam = new Perspectivecamera ( (), ());
    10. Cam.position.set (1f, 1f, 1f);
    11. Cam.lookat (0,0,0);
    12. cam.near = 0.1f;
    13. Cam.far = 300f;
    14. cam.update ();
    15. Modelloader loader = new Objloader ();
    16. model = Loader.loadmodel (Gdx.files.internal ("Data/ship.obj"));
    17. instance = new Modelinstance (model);
    18. camcontroller = new Camerainputcontroller (CAM);
    19. Gdx.input.setInputProcessor (camcontroller);
    20. }
    21. ...
    22. }
Copy Code

Only a little change, first, the camera is closer to the origin, because the ship is too small, then we deleted the ModelBuilder, a new modelloader, through this loader, loaded a model ship. The result is like this:


2013-6-27 18:15:50 Upload Download Attachments (84.89 KB)

In doing so, there is nothing wrong with testing, but in large applications you will need to Assetmanager to manage the model files. Below, let's add:

  1. public class Basic3dtest implements Applicationlistener {
  2. public Perspectivecamera Cam;
  3. Public Camerainputcontroller Camcontroller;
  4. Public Modelbatch Modelbatch;
  5. public Assetmanager assets;
  6. Public array<modelinstance> instances = new array<modelinstance> ();
  7. Public Lights Lights;
  8. public boolean loading;
  9. @Override
  10. public void Create () {
  11. Modelbatch = new Modelbatch ();
  12. Lights = new Lights ();
  13. Lights.ambientLight.set (0.4f, 0.4f, 0.4f, 1f);
  14. Lights.add (New DirectionalLight (). Set (0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
  15. Cam = new Perspectivecamera ( (), ());
  16. Cam.position.set (1f, 1f, 1f);
  17. Cam.lookat (0,0,0);
  18. Cam.near = 0.1f;
  19. Cam.far = 300f;
  20. Cam.update ();
  21. Camcontroller = new Camerainputcontroller (CAM);
  22. Gdx.input.setInputProcessor (Camcontroller);
  23. Assets = new Assetmanager ();
  24. Assets.load ("Data/ship.obj", Model.class);
  25. Loading = true;
  26. }
  27. private void doneloading () {
  28. Model ship = assets.get ("Data/ship.obj", Model.class);
  29. Modelinstance shipinstance = new modelinstance (ship);
  30. Instances.add (shipinstance);
  31. Loading = false;
  32. }
  33. @Override
  34. public void render () {
  35. if (Loading && assets.update ())
  36. Doneloading ();
  37. Camcontroller.update ();
  38. (0, 0, (), ());
  39. (Gl10.gl_color_buffer_bit | Gl10.gl_depth_buffer_bit);
  40. Modelbatch.begin (CAM);
  41. for (Modelinstance instance:instances)
  42. Modelbatch.render (instance, lights);
  43. Modelbatch.end ();
  44. }
  45. @Override
  46. public void Dispose () {
  47. Modelbatch.dispose ();
  48. Instances.clear ();
  49. Assets.dispose ();
  50. }
  51. public Boolean needsGL20 () {
  52. return true;
  53. }
  54. public void Resume () {
  55. }
  56. public void Resize (int width, int height) {
  57. }
  58. public void Pause () {
  59. }
  60. }
Copy Code


has changed over and over, removed the model instance, created the Assetmanager, and we did not use a single Modelinstance object, but we created an array to render multiple instances, which looks more realistic. We also added a flag that marks whether the load is in progress.
Now, in the Create method, we create a asset manager and load the spaceship's model file through him. Shortly thereafter, we set the loading flag and, through it, we can tell that we need to update Assetmanager to get the resource loading state. In the Render method, we read the value of loading flag, if true, indicating that asset manager is loading the resource, we need to call Assets.update (), and if True is returned, the load ends, Call Doneloading (). Also in the Render method, we render all objects in the instance array, not just one. If the resource is not successfully loaded, the array is empty. The

New Method doneloading () Gets the ship model that we just loaded, creates a modelinstance named Shipinstance, and adds it to the instances array so that it is rendered. Finally, we need to set the load flag to false, after which assets.update () will no longer be called.

If you run this, you'll see the same picture as before. And, you'll see a black screen before you see the ship, which is caused by the asset manager loading the model asynchronously.

Our program supports multiple model instances, so let's change to the following.

    1. public class Basic3dtest implements Applicationlistener {
    2. ...
    3. @Override
    4. public void Create () {
    5. ...
    6. Cam.position.set (7f, 7f, 7f);
    7. ...
    8. }
    9. private void doneloading () {
    10. Model ship = assets.get ("Data/ship.obj", Model.class);
    11. for (float x = -5f; x <= 5f; x + = 2f) {
    12. for (float z = -5f; z <= 5f; z + = 2f) {
    13. Modelinstance shipinstance = new modelinstance (ship);
    14. ShipInstance.transform.setToTranslation (x, 0, z);
    15. Instances.add (shipinstance);
    16. }
    17. }
    18. Loading = false;
    19. }
    20. ...
    21. }
Copy Code

We've turned the camera away again, so we can show all the ship on the screen. You can use the mouse wheel to control the distance (zoom in, zoom out). In the Doneloading method, we create multiple instance and place it in a grid based on the X-Z axis.


2013-6-27 18:15:49 Upload Download Attachments (118.78 KB)

Testing with obj (wavefront) files is good, but in real-world applications, it's not appropriate because this format doesn't have enough information to store complex models. And in Libgdx, Objloader is for testing purposes, not too much may be the function you want.

Fortunately, we have: Fbx-conv, which can convert model files generated by modeling software into LIBGDX recognizable formats. The name can be a bit misleading, FBX-CONV can convert many formats including FBX, (including obj), FBX is the preferred file format because almost all modeling programs support it.
Get Fbx-conv Running program, if you haven't, you can download the source code, or download the Windows version of, click to download (should not be up to date). Fbx-conv can generate two formats, G3DJ (JSON, for debugging) &g3db (binary, when the program is published, because it is smaller and loads faster).

The command runs as follows:

    1. Fbx-conv Ship.obj
Copy Code

Make sure that the other files are in the same directory, and it will generate a file named ship.g3db for you. Let's take a look at the program:

    1. public class Basic3dtest implements Applicationlistener {
    2. ...
    3. @Override
    4. public void Create () {
    5. ...
    6. Assets.load ("data/ship.g3db", Model.class);
    7. ...
    8. }
    9. private void doneloading () {
    10. Model ship = assets.get ("data/ship.g3db", Model.class);
    11. ...
    12. }
    13. ...
    14. }
Copy Code

Original address:

LIBGDX New 3D API Tutorial--Loading models with LIBGDX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.