Tom S.
Tom S.

Reputation: 164

3D objects are seen through each other in libGDX

I am working on a 3D simulation of traveling through space with objects in it. The 3D objects that are rendered seem to be "transparent": image

The orbs are the same size. the large one is in front. The model color is NOT set to transparent. I assumed the order of rendering might cause it (as in 2D), and made the objects comparable and sorted them prior to rendering. That did not solve the problem.

Here is the code:

 public void render(float delta) {
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        //We clear the screen
        camera.update();
        this._game_.modelBatch.begin(camera);
        ModelInstance inst;
        Orb closestOrb = null;
        float minDistance = Float.MAX_VALUE;
        for ( Orb orb : this._game_.orbsList)
        {
            if (orb.getZ()< _game_.player.getPosition().z)
            {
            inst = new ModelInstance(this._game_.orbModel);
                inst.transform.setToTranslation(orb.getX(),orb.getY(),orb.getZ());
            this._game_.modelBatch.render(inst, this._game_.environment);
            if (minDistance > Physics.getDistacne(_game_.player.getPosition(),orb.getPosition()))
                {
                    minDistance = Physics.getDistacne(_game_.player.getPosition(), orb.getPosition());
                    closestOrb = orb;
                }
            }
        }
        this._game_.modelBatch.end();

here is the model's code in game:

        modelBatch = new ModelBatch();//the screen uses this
        modelBuilder  = new ModelBuilder();
        Material orbMaterial = new Material(ColorAttribute.createDiffuse(0.5f, 0.5f, 1f,1f));
        //createDiffuse(Color.MAGENTA));
        long orbAttributes =  Usage.Position | Usage.Normal;
        orbModel = modelBuilder.createSphere(Orb.STANDARD_ORB_SIZE,Orb.STANDARD_ORB_SIZE, Orb.STANDARD_ORB_SIZE,30,30, orbMaterial, orbAttributes);
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));

            orbsList = new ArrayList<Orb>(); 
            for (int i = 0; i < SimGame.NUM_OF_ORBS; i++)
            {
                Orb orb = Orb.generateOrb(-SimGame.WORLD_SIZE,SimGame.WORLD_SIZE, Orb.STANDARD_ORB_SIZE);
                orbsList.add(orb);

            }
            Collections.sort(orbsList);

Upvotes: 2

Views: 1495

Answers (3)

Tom S.
Tom S.

Reputation: 164

The problem was that the zNear factor was set to 0. This caused the depth buffering to function abnormally. You can read more about his here: http://www.opengl.org/archives/resources/faq/technical/depthbuffer.htm I thank everyone who pointed me in the right direction.

Upvotes: 3

Paddyd
Paddyd

Reputation: 1870

Depth-Buffering is what is the problem here.

According to this source, You are enabling it in your call to glClear (notice the second parameter):

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

So the way it looks, it should be rendering correctly. I would suggest removing the ordering of your obejcts, as they should be calculated automatically with their position relative to the camera.

Upvotes: 0

OrangeMan
OrangeMan

Reputation: 692

I don't know libGDX that well, although i know LWJGL and openGL pretty well.

The problem you are having is that the depth of objects are not being tested, meaning further away objects can be rendered over closer ones. I'm guessing that at the beginning of the program you need to call GL11.glEnable(GL11.GL_DEPTH_TEST). It may be different in libGDX. If you cannot find that i would recommend searching 'libGDX depth testing' or something similar into google.

Upvotes: 1

Related Questions