Blaise Wang
Blaise Wang

Reputation: 556

How do I draw a wireframe mesh with hidden lines removed in OpenGL ES?

The cube I want to draw:

The above cube is pretty easy to be created in OpenGL with function glPolygonMode(GL_FRONT_AND_BACK, GL_LINE), but it is known that OpenGL ES does not include the function I want to use. In a word, I don't want to draw cube like this:

I would like to thank the people who could provide any help.

========================least update picture============================

The back edge is a little thinner than front edge. Is there have any solution to solve this problem?

Current code:

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    GLES20.glDisable(GLES20.GL_POLYGON_OFFSET_FILL);
    // Draw edge
    GLES20.glDrawElements(GLES20.GL_LINES, lineIndices.length,
            GLES20.GL_UNSIGNED_SHORT, lineIndexBuffer);

    GLES20.glEnable(GLES20.GL_POLYGON_OFFSET_FILL);
    GLES20.glPolygonOffset(1.0f, 1.0f);
    // Apply background color and draw faces
    GLES20.glUniform4fv(mColorHandle, 1, faceColor, 0);
    GLES20.glDrawElements(GLES20.GL_TRIANGLES, triangleIndices.length,
            GLES20.GL_UNSIGNED_SHORT, triangleIndexBuffer);

Upvotes: 5

Views: 5969

Answers (1)

wcochran
wcochran

Reputation: 10896

Here is a standard trick (assumes depth buffer enabled):

  1. Draw the mesh edges (lines)
  2. Fill the mesh polygons with the background color (to cover up the hidden lines)

Since the mesh edges and the mesh faces will have the same z-values where they meet you need to solve the z-fighting problem. The simplest way it use polygon offset so that the edges "hover" slightly "above" the polygon faces:

gl.disable(gl.POLYGON_OFFSET_FILL)
/* draw polygon edges */
gl.enable(gl.POLYGON_OFFSET_FILL);
gl.polygonOffset(1.0, 1.0);
/* fill polygons in background color */

Here is a wireframe wineglass that illustrates the z-fighting problem:

enter image description here

Here I solved the z-fighting problem using polygon offset:

enter image description here

Upvotes: 3

Related Questions