Displaying 3D Models with OrthographicCamera in LibGDX

So, basically I am trying to render a .obj 3D model with OrthographicCamera in 2D and not 3D with PerspectiveCamera.

Everything works fine when rendering with the PerspectiveCamera, however if I switch it to Orthographic nothing is displayed, just a blank background.

Below is the code I am using with the PerspectiveCamera material. I'm not sure if I need to customize the OrthographicCamera, or if I need to use the PerspectiveCamera with some view changes to achieve this. At this point, it really doesn't matter which axis I "exclude".

Basically, I want to use Blender for my 2D animation. I figured it out on the Blender side and it works very well, however they have to be imported as .obj (or .fbx) in LibGDX, they are loaded in LibGDX as models and as such should be rendered with ModelBatch using ModelInstance and Environment. I need a way to view this object using a 2D orthographic projection, not a 3D perspective projection.

Anyone have any suggestions for this?

public class Test extends ApplicationAdapter {

//public PerspectiveCamera cam;
public OrthographicCamera cam;
public CameraInputController camController;

public ModelBatch modelBatch;

public ModelInstance ship;

public Environment environment;

@Override
public void create() {
    modelBatch = new ModelBatch();

    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
    environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));

    /*
    cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(10f, 10f, 10f);
    cam.lookAt(0, 0, 0);
    cam.near = 1f;
    cam.far = 10f;
    cam.update();
    */

    cam = new OrthographicCamera();
    cam.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.update();

    camController = new CameraInputController(cam);
    Gdx.input.setInputProcessor(camController);

    ObjLoader loader = new ObjLoader();
    Model model = loader.loadModel(Gdx.files.internal("data/invader.obj"));
    ship = new ModelInstance(model);
}

@Override
public void render() {
    camController.update();

    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    modelBatch.begin(cam);
    modelBatch.render(ship, environment);
    modelBatch.end();
}

@Override
public void dispose() {
    modelBatch.dispose();
}
}

      

+3


source to share





All Articles