Android Opengl 2d Intersection between two graphic objects – Education Career Blog

I’m new to opengl and android and couldn’t find an answer in the forums so here I am…

I need to be able to ‘draw’ only the intersection of two triangles. I tried to use stencil test but it doesn’t work in the android emulator.

Then I tried to use depth test and it works but only for a fraction of a second, then the screen goes blank.

I have a config with depth size 8.

What am I doing wrong?

This is the crude code I wrote for drawing the 2D ‘scene’ to verify that clipping works

      gl.glEnable(GL10.GL_DEPTH_TEST);

  gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT   | GL10.GL_STENCIL_BUFFER_BIT);

  gl.glColorMask(false, false, false, false);

  outlineColor.setAsActive(gl);

  triangleOne(gl);

  gl.glDepthFunc(GL10.GL_EQUAL);

  gl.glColorMask(true, true, true, true);

  fillColor.setAsActive(gl);

  triangleTwo(gl);

  gl.glDisable(GL10.GL_DEPTH_TEST);

And here are the triangles…

private void triangleOne(GL10 gl) {
  float coords = { 1f, height, width, height / 2, 1f, 1f };
  drawItem(gl, 2, coords);
}

private void triangleTwo(GL10 gl) {
  float coords = { width, height, width, 1f, 1f, height / 2 };
  drawItem(gl, 2, coords);
}

private void drawItem(GL10 gl, int size, float coords){
  fillColor.setAsActive(gl);
  FloatBuffer vertexBuffer;
  ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length * 4);
  vbb.order(ByteOrder.nativeOrder());
  vertexBuffer = vbb.asFloatBuffer();
  vertexBuffer.put(coords);
  vertexBuffer.position(0);

  gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  gl.glVertexPointer(size, GL10.GL_FLOAT, 0, vertexBuffer);
  gl.glDrawArrays(GL10.GL_TRIANGLES, 0, vertexBuffer.capacity() / size);
  gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

Thanks for helping

,

Try gl.glDepthFunc(GL10.GL_ALWAYS) for the first triangle.

(Btw, what z-value ends up in the depth buffer if you use 2D vertexes? Assume it’ll be the near plane of your view frustum, but can’t find out for definite…)

Leave a Comment