5
\$\begingroup\$

enter image description here

Hi, I am using LibGDX and OpenGL 2.0 to create a game. So far I have created two meshes. The front mesh is the one with the brown circle texture and another mesh on the top with a green to white gradient texture. I am using a perspective camera and everything looks ok except for one small artefact where one of the textures that should be obscured from view by another is showing through(see image).

I am fairly new to OpenGL and LibGDX. I have culling enabled on the back faces and was under the impression the problem could be solved with GL_DEPTH_TEST. If I set the glDepthFunc to GREATER or to LESS nothing is displayed. Currently I am using LEQUAL. So I am not sure if I am on the right track or if the problem is caused else where. Any help would be appreciated.

I am using some very simple shaders:

    String vertexShader = "attribute vec4 a_position;    \n" + 
                "attribute vec4 a_color;\n" +
                "attribute vec2 a_texCoord0;\n" + 
                "uniform mat4 u_worldView;\n" + 
                "varying vec4 v_color;" + 
                "varying vec2 v_texCoords;" + 
                "void main()                  \n" + 
                "{                            \n" + 
                "   v_color = vec4(1, 1, 1, 1); \n" + 
                "   v_texCoords = a_texCoord0; \n" + 
                "   gl_Position =  u_worldView * a_position;  \n"      + 
                "}                            \n" ;
String fragmentShader = "#ifdef GL_ES\n" +
                  "precision mediump float;\n" + 
                  "#endif\n" + 
                  "varying vec4 v_color;\n" + 
                  "varying vec2 v_texCoords;\n" + 
                  "uniform sampler2D u_texture;\n" + 
                  "void main()                                  \n" + 
                  "{                                            \n" + 
                  "  gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n"+
                  "}";

Culling and depth has been set with:

Gdx.gl.glEnable(GL20.GL_CULL_FACE);
Gdx.gl.glCullFace(GL20.GL_BACK);

Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
Gdx.gl.glDepthFunc(GL20.GL_LEQUAL);

Render code:

grassTexture.bind(); //the top mesh texture
shader2.begin();
shader2.setUniformMatrix("u_worldView", cameraSystem.camera.combined);
shader2.setUniformi("u_texture", 1);
topMesh.render(shader2, GL20.GL_TRIANGLES);
shader2.end();  

texture.bind(); //the brown circle texture
shader2.begin();
shader2.setUniformMatrix("u_worldView", cameraSystem.camera.combined);
shader2.setUniformi("u_texture", 0);
mesh.render(shader2, GL20.GL_TRIANGLES);
shader2.end();

Clearing:

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

UPDATED INFO:

The idea is that I am trying to create a 2.5D terrain. The brown area is the cross section of the terrain and the 2D part. The green terrain is the "roof" and it has the z-depth set to give it the 2.5D perspective look. The problem is that the section in the circle should not be visible. One can see that there is a slight hill with the green roof, and then the terrain dips down. The roof of the terrain in the dip is not being blocked form view by the top roof part. It is the only section that this is happening. I have shaded the part that should be hidden in pink.

enter image description here

As for my z-orders I have set the indices to create triangles in a counter-clockwise order. I think that part is correct as if I change the culling from back to front I get the expected results. The way I create the top mesh is to iterate backwards through a Box2D chainshape (that the physics uses) and setting the z to -10, I then loop again but this time from the start and add the positions with z set to 0. This creates a circular mesh. From top down this creates a rectangle that I subdivide into quads which I then break down into triangles. This is the code that does it:

  ChainShape shape =  ((ChainShape)fix.getShape());
      vertices= new float[(shape.getVertexCount()*2*9)];

      float b = 1f;
      indicies = new short[(shape.getVertexCount()-1)*2*3];
      int c = 0;

      for(int i=shape.getVertexCount()-1;i>=0;i--)
      {


          //back part of mesh starting from right to left
          Vector2 va = new Vector2();
          shape.getVertex(i, va);
          vertices[c] = va.x*b+pos.x;
          c++;
          vertices[c] = va.y*b+pos.y;
          c++;
          vertices[c] = -10.0f;
          c++;

          //colour
          vertices[c] =1;
          c++;
          vertices[c]= 1;
          c++;
          vertices[c] =1f;
          c++;
          vertices[c] =1f;
          c++;

          //uv
          vertices[c] =(va.x*b+pos.x)/20f;
          c++;
          vertices[c] =0;
          c++;

      }

      for(int i=0;i<shape.getVertexCount();i++)
      {
          //front part of mesh starting from left to right
          Vector2 va = new Vector2();
          shape.getVertex(i, va);
          vertices[c] = va.x*b+pos.x;
          c++;
          vertices[c] = va.y*b+pos.y;
          c++;
          vertices[c] = 0.0f;
          c++;

          vertices[c] =1f;
          c++;
          vertices[c] =1f;
          c++;
          vertices[c] =1f;
          c++;
          vertices[c] =1f;
          c++;

          vertices[c] =(va.x*b+pos.x)/20f;
          c++;
          vertices[c] =1;
          c++;
      }


      short val = (short)(0);
      for(int i=0;i<indicies.length;)
      {

          indicies[i]=(short) (val);
          i++;
          indicies[i]=(short) ((shape.getVertexCount()*2-1)-val);
          i++;
          indicies[i]=(short) (val+1);
          i++;

          indicies[i]=(short) ((shape.getVertexCount()*2-1)-val);
          i++;
          indicies[i]=(short) ((shape.getVertexCount()*2-1)-(val+1));
          i++;
          indicies[i]=(short) (val+1);
          i++;
          val++;

      } 
\$\endgroup\$
  • \$\begingroup\$ It is unclear from the question which mesh should be in front, as the question states that the brown mesh is in front, but the picture suggests that this is not what you want. Also the important part is missing from the question: How are you defining the z-order of the meshes in your code? \$\endgroup\$ – msell Oct 4 '13 at 5:29
  • \$\begingroup\$ @msell I have edited my post to add more info to your questions. Hope that helps. \$\endgroup\$ – Allan Oct 4 '13 at 6:19
  • 1
    \$\begingroup\$ Sorry, I did not initially see the depth in that picture, as it looked like a "Worms-style" landscape. Now it's clear. Enabling depth test should be enough for you. One common problem that breaks depth test seems to be using 0.0 as the near plane for gluPerspective \$\endgroup\$ – msell Oct 4 '13 at 6:59
  • \$\begingroup\$ @msell Awesome! Thanks! That was the problem. I had my camera near plane set to 0. Changing it to 1 solved the problem. Feel free to write that as the answer so I can accept it. \$\endgroup\$ – Allan Oct 4 '13 at 7:08
4
\$\begingroup\$

You are setting up the depth test correctly. Note that the backface culling is not needed for this case, although it's still good to enable for other reasons.

A likely reason why your depth test is not working correctly could be using 0.0 as the near plane when calling gluPerspective. See https://stackoverflow.com/questions/4764057/opengl-depth-test-not-working-as-expected for more information. After fixing this by using a positive near z, the default depth function GL_LESS should also work.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.