tinySceneGraph Home Images Contact

Faking X-Ray rendering

Achieving an x-ray effect with OpenGL is extremely easily done with just a few lines in OpenGL. Although you can work out much more sophisticated algorithms using color and alpha information of incoming geometry in a shader, these few lines of OpenGL already provide nice results:
ISS in x-ray mode, lighting disabled
Visible Human, regular OpenGL blending (click to enlarge)
  RenderSettings_t RS = viewer.GetSettings ();
  if (enable) {
    RS.stateOverrides |= (CSG_OVERRIDE_MATERIAL
                        | CSG_OVERRIDE_SHADER);
    glBlendFunc(GL_CONSTANT_ALPHA_EXT, GL_ONE);
    glBlendColorEXT(0.1, 0.1, 0.1, 0.1);
    glEnable (GL_BLEND);
    glDisable (GL_DEPTH_TEST);
  } else {
    RS.stateOverrides &= ~(CSG_OVERRIDE_MATERIAL
                         | CSG_OVERRIDE_SHADER);
    glDisable (GL_BLEND);
    glEnable (GL_DEPTH_TEST);
  }
  viewer.AssignSettings (RS);

There are two keys to create an x-ray effect: Enable additive blending and disable the depth test. glBlendColor() eliminates the necessity to provide blending weights via alpha values by specifying fixed blending weights. The glBlendFunc() directs OpenGL to use the blend colors alpha value for scaling all incoming color values (GL_CONSTANT_ALPHA_EXT) before adding them to what already is in the frame buffer (taken as is since the destination factor is set to GL_ONE).

ISS in x-ray mode, lighting disabled ISS in x-ray mode, lighting enabled
ISS model with lighting disabled ISS model with lighting enabled

Using GL_CONSTANT_ALPHA_EXT will take the blend colors alpha value for all color components, the blend colors rgb values are left unused. The value of 0.1 is chosen arbitrarily and determines how many polygons can be blended together before reaching the maximum intensity of 1.0. Since incoming colors are only scaled and added to the framebuffer, lighting calculations remain active. You will still see some geometry curves caused by lighting effects. The state overrides is a tinySG traverser render state setting, instructing the traverser to start traversal with flags to skip all material and shader nodes it encounters. This will keep material settings constant for all geometry in the scene and use a grey default material for all geometry.

Keep rendering,
Christian

Mountaineer engine x-ray
csgEdit running different render modes simultaneously, x-ray mode on the left and regular rendering on the right, clipping enabled.
(click to enlarge)


Acknowledgements:

  • The ISS model is by courtesy of NASA.
  • The human body dataset has been created from the Visible Human project and is available at the large geometric models archive from Georgia Tech.
  • The mountaineer engine was converted from the SPECapc benchmark datasets.


Copyright by Christian Marten, 2012
Last change: 09.03.2012