-1

I'm trying to learn OpenGL ES 2.0 and I went to load 3d models on Android. I can now load properly with the model texture, but I have a problem on the display depth. When I place my model in perspective, and part of the model is hidden by another part of it, it happens to me that a triangle or two before another draw and this is what I see through some parts .

I try setEGLConfigChooser (8, 8, 8, 8, 16, 0); and (8, 8, 8, 8, 24, 0), but my problem remains the same, except that when I put (8, 8, 8, 8, 24, 0) and display a little better defined, but when the 3d object moves, the colors make a strobe effect that is disturbing to me.

I also try glDepthFunc function (GL_LEQUAL); with glEnable (GL_DEPTH_TEST), but this does not rule over my problem.

Here's the pictures of the probleme:

The probleme : Link is broken

The good : Link is broken

Sorry for my link picture, I do not have more than 10 reputation to post picture in the question.

Here my code My GLSurfaceView

public MyGLSurfaceView(Context context) {
    super(context);
    this.context = context;

    setEGLContextClientVersion(2);
    setEGLConfigChooser(true);

    //setZOrderOnTop(true);
    //setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    //setEGLConfigChooser(8, 8, 8, 8, 24, 0);
    //getHolder().setFormat(PixelFormat.RGBA_8888);

    mRenderer = new Renderer(context);
    setRenderer(mRenderer);
}

My renderer

 @Override
 public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);  
    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glFrontFace(GL_CCW);
    glEnable(GL_DEPTH_TEST);

    mushroom = new Mushroom();

    textureProgram = new TextureShaderProgram(context);
    texture = TextureHelper.loadTexture(context, R.drawable.mushroom);
}

@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) {                
    glViewport(0, 0, width, height);

    MatrixHelper.perspectiveM(projectionMatrix, 45, (float) width
            / (float) height, 0f, 10f);
    setLookAtM(viewMatrix, 0, 0f, 1.2f, -10.2f, 0f, 0f, 0f, 0f, 1f, 0f);
}

@Override    
public void onDrawFrame(GL10 glUnused) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    multiplyMM(viewProjectionMatrix, 0, projectionMatrix, 0, viewMatrix, 0);

    glDepthFunc(GL_LEQUAL);
    //glDepthMask(true);

    positionMushroomInScene();
    textureProgram.useProgram();
    textureProgram.setUniforms(modelViewProjectionMatrix, texture);
    mushroom.bindData(textureProgram);

    mushroom.draw();
    //glDepthFunc(GL_LESS);
}

private void positionMushroomInScene() {
    setIdentityM(modelMatrix, 0);
    translateM(modelMatrix, 0, 0f, 0f, 5f);
    rotateM(modelMatrix, 0, -yRotation, 1f, 0f, 0f);
    rotateM(modelMatrix, 0, xRotation, 0f, 1f, 0f);
    multiplyMM(modelViewProjectionMatrix, 0, viewProjectionMatrix,
            0, modelMatrix, 0);
}

My matrix Helper

public static void perspectiveM(float[] m, float yFovInDegrees, float aspect, float n, float f) {
    final float angleInRadians = (float) (yFovInDegrees * Math.PI / 180.0);
    final float a = (float) (1.0 / Math.tan(angleInRadians / 2.0));

    m[0] = a / aspect;
    m[1] = 0f;
    m[2] = 0f;
    m[3] = 0f;
    m[4] = 0f;
    m[5] = a;
    m[6] = 0f;
    m[7] = 0f;
    m[8] = 0f;
    m[9] = 0f;
    m[10] = -((f + n) / (f - n));
    m[11] = -1f;
    m[12] = 0f;
    m[13] = 0f;
    m[14] = -((2f * f * n) / (f - n));
    m[15] = 0f;
}
Maxime4000
  • 77
  • 2
  • 11

1 Answers1

0

The problem is most likely with the way you set up your projection matrix:

MatrixHelper.perspectiveM(projectionMatrix, 45, (float) width
        / (float) height, 0f, 10f);

The 4th argument in your definition of this function is the near plane. This value should never be 0.0. It should typically be a reasonable fraction of the far distance. Choosing the ideal value can be somewhat of a tradeoff. The larger far / near is, the less depth precision you get. On the other hand, if you set the near value too large, you risk clipping off close geometry that you actually wanted to see.

A ratio of maybe 100 or 1000 for far / near should normally give you reasonable depth precision, without undesirable front clipping. You'll need to be a little more conservative with the ratio if you use a 16-bit depth buffer than if you have a 24-bit depth buffer.

For your purpose, try changing near to 0.1, and see how that works for you:

MatrixHelper.perspectiveM(projectionMatrix, 45, (float) width
        / (float) height, 0.1f, 10f);
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133