Remiel
Remiel

Reputation: 61

libGdx--Blank black screen when trying to load obj models using libgdx.g3d

I get nothing but a black screen when trying to load and render an .obj model using libgdx. I have tried different models (exporting using blender in .obj format) I've tried loading .g3db files instead of obj files and come up with the same result. Nothing renders onscreen but my glClearColor (and yes I've tried something other than black to see if my model was rendering pure black) I have all the assets loading correctly for an .obj file (skeleton.mtl, skeleton.obj, texture.png) and I'm sure my code is doing something with them because if I remove say, the texture.png, I get an error saying it cannot find the asset. So why do I get NOTHING when rendering? I would like to understand why this is happening. Here is my code:

    public void show() {
    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(1f, 1f, 1f);
    cam.lookAt(0,0,0);
    cam.near = 0.1f;
    cam.far = 300f;
    cam.update();
    ModelLoader loader = new ObjLoader();
    model = loader.loadModel(Gdx.files.internal("models/skeleton.obj"));
    instance = new ModelInstance(model);

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

    ModelLoader loader = new ObjLoader();
    model = loader.loadModel(Gdx.files.internal("models/skeleton.obj"));
    instance = new ModelInstance(model);

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

Then my Render loop:

@Override
    public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    camController.update();

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

Upvotes: 1

Views: 1846

Answers (2)

Remiel
Remiel

Reputation: 61

I forgot to clear my Depth buffer.

In my:

public void render(float delta) {

I changed this:

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

to this

Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

If you were using GL10 it would go like this:

Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

I knew it was something stupid. I also made the changes suggested by the other poster about making sure my Transparency is set above zero in blender and also backing off my camera because the camera was too close, it wasnt rendering anything at that distance also. Thank you.

Upvotes: 2

Daahrien
Daahrien

Reputation: 10320

cam.position.set(1f, 1f, 1f);
cam.lookAt(0,0,0);
cam.near = 0.1f;

Your camera is way to near the origin, so your object may be too big and the camera may be "inside" it, so it wont be rendered.

Also, you probably exported your object with zero opacity: https://github.com/libgdx/libgdx/wiki/Importing-Blender-models-in-LibGDX#wiki-troubleshooting-missing-textures

Also, it is quite common that the materials from Blender export with opacity set to Zero. If you notice your model is not being rendered. Go to the Material in Blender, and below "Transparency" set its Alpha to the desired one (usually 1, for full opacity).

Upvotes: 0

Related Questions