Outline effects in OpenGL

dupersuper picture dupersuper · Oct 17, 2012 · Viewed 20.8k times · Source

In OpenGL, I can outline objects by drawing the object normally, then drawing it again as a wireframe, using the stencil buffer so the original object is not drawn over. However, this results in outlines with one solid color.

enter image description here

In this image, the pixels of the creature's outline seem to get more transparent the further they are from the creature they outline. How can I achieve a similar effect with OpenGL?

Answer

EmericBeaufays picture EmericBeaufays · May 16, 2016

I'm late for an answer but I was trying to achieve the same thing and thought I'd share the solution I'm using.

A similar effect can be achieved in a single draw operation with a not so complex shader.

In the fragment shader, you will calculate the color of the fragment based on lightning and texture giving you the un-highlighted color 'colorA'.

Your second color is the outline color, 'colorB'.

You should obtain the fragment to camera vector, normalize it, then get the dot product of this vector with the fragment's normal.

The fragment to camera vector is simply the inverse of the fragment's position in eye-space.

The colour of the fragment is then:

float CameraFacingPercentage = dot(v_fragmentToCamera, v_Normal);
gl_FragColor = ColorA * CameraFacingPercentage + ColorB * (1 - FacingCameraPercentage);

This is the basic idea but you'll have to play around to have more or less of the outline color. Also, the concave parts of your model will also be highlighted but that is also the case in the image posted in the question.