Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dotted back edges of object

In my Android app I need to display a 3D object (that's no problem), but the front edges should be solid and the back edges should be dotted. I need to have something like on the picture. How can I achieve it using OpenGL ES 1 or 2?

I have tried the Tim's idea(using Depth buffer). It works but there are some artefacts like on the pictures, dotted(purple) lines overlap solid(red) lines:

enter image description hereenter image description here

It happens because dotted lines(purple colour(GL_GREATER)) are drawn AFTER red lines(GL_LEQUAL). It there any ideas how to prevent it?

Thank you everyone for helping. Now it looks great!! enter image description here

like image 225
agent-10 Avatar asked Oct 18 '12 17:10

agent-10


1 Answers

Here's a rough outline of what I would do. You'll need one mesh for the object itself, and another mesh of GL_LINES for the 'edge lines' that you want to draw.

  1. Draw the solid mesh into the depth buffer.
  2. Draw your 'edge mesh' with a standard shader, such that you get solid lines, but set the depth test to GL_LEQUAL (only draw objects nearer than the depth value.
  3. Draw your edge mesh a second time with a depth test of GL_GREATER. In this pass use a stipple shader to get the dotted line effect (explained further below).

This might have problems depending on what other objects are in the scene, as you might get dotted lines drawn in the case where the cube is obscured by something else, but maybe that's not a problem for you or you can somehow cull the mesh if it is obscured.

For the stipple shader, you can set a texcoord of one vertex of the line to be zero, and another endpoint of the line to be one. Then in your shader you can use a step function similar to suggested by tencent, to reject fragments to create a stipple pattern.

You can also skip the messing with the depth buffer if you want to calculate yourself which lines are behind the object, though it could be a lot of work with anything more than a cube.

If you want to use OpenGLES 1 instead of OpenGLES 2, replace the stipple shader with a 1D texture of a stipple pattern, and texture the back lines with that. However I'm not 100% sure you can texture a line in OpenGL, so maybe this won't work.

like image 106
Tim Avatar answered Nov 14 '22 23:11

Tim