Model Loading

Learn how to load models in the Kakara Engine.

The Model Loading system is not finalized and can change in a future version.

Manually typing in the vertex, normal, texture, and index values for a Mesh can be a pain for large meshes. To solve this issue models can be created using 3D modeling software (like Blender). Using these software you can make meshes and export them to a model file (.obj, .fbx, etc.). The Kakara Engine allows you to load in these meshes.

Simple Model Loading

The Kakara Engine has a utility class that allows you to do simple mesh loading. That utility class is the StaticModelLoader. You can use StaticModelLoader.load()to load in models.

StaticModelLoader.load(resource, texture_directory, scene, resource_manager);

Resource

This is the resource for the model itself.

Texture Directory

This is a string containing the location to the textures for the model. (If you have no textures for your model you can use and empty string.)

Loading a Monkey

Let's delete our old cube code from the loadGraphics method in the MainScene class and start fresh. In this tutorial we will be loading a simple monkey mesh (which is a primitive mesh in Blender). You can download the model used in this tutorial here (hosted by DropBox). Put the monkey.fbx file in the resources folder (the same one that we put Example_Cube.png in).

Once again start off by storing the instance of the ResourceManager. Next you want to call the StaticModelLoad.load() method.

ResourceManager rm = gameHandler.getResourceManager();
Mesh[] monkeyMesh = StaticModelLoader.load(rm.getResource("/monkey.fbx"), 
    "", this, rm);

The StaticModelLoader will automatically create a material for us. Now the only thing we need to do is make a GameItem to hold our mesh array.

GameItem monkey = new GameItem(monkeyMesh);
monkey.transform.setPosition(0, 0, -10);
add(monkey);

If you run the code you can see that the monkey is not facing the right way. We can correct this by rotating the monkey. Add the following below setting the position:

monkey.transform.getRotation().rotateX((float) Math.toRadians(-90));

That rotates the monkey by -90 (or 270) degrees on the X axis. Now you can see that the monkey is facing us.

It appears that the monkey could appear all white depending on the GPU and the drivers installed. This does not seem to be an issue with all models.

Complete Code

import org.kakara.engine.GameHandler;
import org.kakara.engine.gameitems.GameItem;
import org.kakara.engine.gameitems.mesh.Mesh;
import org.kakara.engine.models.StaticModelLoader;
import org.kakara.engine.resources.ResourceManager;
import org.kakara.engine.scene.AbstractGameScene;

public class MainScene extends AbstractGameScene {
    public MainScene(GameHandler gameHandler) {
        super(gameHandler);
    }

    @Override
    public void work() {

    }

    @Override
    public void loadGraphics(GameHandler gameHandler) throws Exception {
        ResourceManager rm = gameHandler.getResourceManager();

        Mesh[] monkeyMesh = StaticModelLoader.load(rm.getResource("/monkey.fbx"), 
            "", this, rm);
        GameItem monkey = new GameItem(monkeyMesh);
        monkey.transform.setPosition(0, 0, -10);
        monkey.transform.getRotation().rotateX((float) Math.toRadians(-90));
        add(monkey);
    }

    @Override
    public void update(float v) {
    }
}

Valid File Formats

A complete list of supported file formats can be found here.

Last updated