Libgdx weird modelling - depth error?

android, java, libgdx

Solution

When using the libgdx 3D api (ModelBatch and/or Shader), you should not change the opengl state outside the Shader class. So, enabling/disabling depth test etc. is useless and might result in unpredicted behavior.

You should not use obj files. Instead use fbx-conv and use the g3db or g3dj file format. Also, your model is missing the mtl file, causing the material not to be applied.

You are using your own shader, you should not have to call shader.end(), modelbatch does this for you.

I tried your model (without material obviously) and it renders correctly using the default shader.

Problem

I'm getting this when I load any .obj file with ObjLoader: How it looks like in real: How I'm loading it: ``` ModelInstance instance = new ModelInstance(model); instance.transform.setToTranslation(-4, 0, 0); instance.transform.mul(transform.setToRotation(Axis.X,(float)(Math.random()*360))); ``` Then in onCreate(): ``` Gdx.gl.glClearDepthf(1.0f); Gdx.gl.glEnable(GL20.GL_DEPTH_TEST); Gdx.gl.glDepthFunc(GL20.GL_LESS); Gdx.gl.glDepthRangef(0f, 1f); Gdx.gl.glEnable(GL20.GL_TEXTURE_2D); ``` And in render(): ``` Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); Gdx.gl.glClearColor(.1f, .1f, .1f, 1f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); shader.begin(camera, instances.get(i).transform); modelBatch.begin(camera); if (environment !=null) modelBatch.render(instances.get(i), environment, shader); else modelBatch.render(instances.get(i), shader); modelBatch.end(); shader.end(); ``` Clean source code here: source code SOLUTION: ``` 1, probleme was with the .mtl file, copy pasted from the ship.mtl and rewrite 2, probleme was with the camera near, and far plane (0.1 and 1000 is the good) 3, probleme was with the obj file texture, because it flipped on the obj file, solution was to convert g3db with -f ```

Original source