Volume Rendering with Style Transfer Function

An interactive volume renderer where style transfer functions decide how each part of a scan looks. Every density range gets a lit sphere image, a “style”, along with its color and opacity, so bone, skin or leaves each get their own material. The volume lights itself with soft shadows, ambient occlusion and one bounce of indirect light. The technique is based on this paper.

The source code is on my GitHub, and builds with Visual Studio 2026 with Cinder as a submodule. It is made with C++, OpenGL 4.3 compute and fragment shaders, Cinder, GLM and Dear ImGui. Five volumes come with it: a bonsai, a skull, a foot, an aneurysm and an engine block.

Rays are cast through the volume from the front to the back faces of its bounding box, with jittered start positions to hide banding. Each sample is classified by the transfer function and shaded by looking up its normal in the style’s lit sphere. Gradients are central differences, smoothed in compute shaders. They keep their magnitude, so shading can tell a surface from the inside of a homogeneous region.

The lighting is computed in the volume itself, so anything in front of the light shadows what is behind it, at any depth. Two 3D textures hold it, at half the volume’s resolution:

  • A light volume stores, for every voxel, how much light reaches it through the material between it and the light.
  • An ambient volume marches 16 short rays from every voxel. The share that escapes is its ambient occlusion. The lit material the rays run into is reflected back as one bounce of indirect light.

Both are rebuilt in compute shaders only when the light, the volume’s rotation or the transfer function change, so moving the camera costs nothing extra. Samples on surfaces get Lambert and a normalized Blinn-Phong highlight. Inside homogeneous material, where the gradient is only noise, they scatter light like a medium instead.

The original lighting, Lambert with screen space shadows and ambient occlusion, is still a toggle, for comparison:

The transfer function editor places alpha, color and style points over the volume’s histogram. Any image can be added as a style, and functions save to files. Every lighting term has its own toggle and strength:


comments powered by Disqus