Jota_sk
Jota_sk

Reputation: 169

Rendering multiple 3D Objects

I'm learning how to render objects with libGdx. I have one square model, that creates a few model instance from them. If I have only one model it renders fine. enter image description here

But if I have more instances it doesn't properly. Looks like the front objects are draw first, and the background the last one, so always the background objects are visible and the front objects you can see through them. enter image description here

To render I use the following

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

    mb.begin(cam);
    worldManager.render(mb, environment);
    mb.end();

mb variable is the ModelBatch instance, and inside worldManager.render each model instance is draw as follow:

mb.render(model, environment);

I'm not sure what is happening. But I think it is some GL attribute that I need enable

Is not 100% related to the following post because, yes it uses OPENGL like libgdx, but the solution provided in that post is not working and I think the problem comes from ModelBatch from libgdx

Reproduction of the problem

Upvotes: 0

Views: 852

Answers (3)

Savehdarbandsari
Savehdarbandsari

Reputation: 1

for show the difference dimentions you can use the fog

Upvotes: 0

Xoppa
Xoppa

Reputation: 8113

You didn't setup your camera correctly. First of all your camera's near plane is 0f, which means it is infinitely small. Set it to a value of at least 1f. Secondly you set the camera to look at it's own position, which is impossible (you can't look inside your own eyes, can you ;)).

So it would look something like:

camera = new PerspectiveCamera(90, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(0, 10, 0);
camera.lookAt(0,0,0);
camera.near = 1f;
camera.far = 100f;
camera.update();

You probably want to start here: https://xoppa.github.io/blog/basic-3d-using-libgdx/

For more information on how the camera works have a look at: http://www.badlogicgames.com/wordpress/?p=1550

Btw, calling Gdx.gl20.glEnable(GL20.GL_DEPTH_TEST); will not help at that location and should definitely not be done when mixed with ModelBatch. ModelBatch manages its own render context, see the documentation for more information: https://github.com/libgdx/libgdx/wiki/ModelBatch

Upvotes: 3

Archie
Archie

Reputation: 551

There are a lot of possible answer, but I would say that

glEnable (GL_DEPTH_TEST) ; 

could help if you haven't done it yet. Also, enabling depth buffer only works if you actually have a depth buffer, which means you must makes sure you have one, and the method for this depends on your window context.

Upvotes: 1

Related Questions