This is an old revision of the document!
An Interactive Volume Renderer
The user interface, showing the rendered output, and controls for modifying the transfer function and camera parameters.
For professor Gordon Kindlmann's Scientific Visualization class, I re-wrote the volume renderer assignment to run interactively using WebGL. The original renderer is written in C, but my version is written in client-side TypeScript instead, so it can run in the browser.
As its name implies, the volume renderer produces images from volume data: a 3d array where each sample represents the density of some point in space. To render this data to the screen, we can cast a ray out from each pixel, passing it through the volume, collecting information that we can use to produce a final pixel color.
This problem is well-suited to implementation using the GPU since each pixel is colored using the exact same procedure. Furthermore, these procedures don't need to communicate or synchronize at all.
The code that casts a ray out for each pixel is implemented as a fragment shader. That is, a shader which is run once for every pixel in the output image. Typically, GPU programs also involve a vertex shader, which is a program that runs once for each vertex in the input mesh. In our case, though, the input mesh is a single quad which we construct before rendering and never touch again. A texture is a read-only array of values which is shared between fragments and can be sampled by the fragment shader. There's even a way to upload a 3D texture to the GPU, which makes the setup especially simple.
All of the class data uses a file format called NRRD, which closely mirrors the input texture format that the GPU expects. Once the user selects an input file, the file is parsed into a Float32Array type, along with some metadata like the size of each of the axes. This data can be uploaded to the GPU as a 3D texture.
The implementation contains a function called volFromNRRD which performs the upload:
export function volFromNRRD(gl: WebGL2RenderingContext, nrrd: nrrd.DecodedNRRD): Texture { ... Some checks on the nrrd file ... // Allocate a new WebGL texture and bind it. const texture = gl.createTexture(); gl.bindTexture(gl.TEXTURE_3D, texture); gl.texImage3D( gl.TEXTURE_3D, ... Lots of parameters ... gl.RED, gl.FLOAT, nrrd.data // <- Passing the Float32Array to the GPU! ); // Making the texture "texture complete" gl.texParameteri(gl.TEXTURE_3D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); ... Additional calls to texParameteri ... return { texture, width: nrrd.sizes[0], height: nrrd.sizes[1], depth: nrrd.sizes[2], }; }
Once the texture is uploaded to the GPU, it's the fragment shader's job to sample it. The primary complexity in sampling the volume texture is interpolating between the sample locations. As each ray works its way through the volume, we want to know the density of the volume at the current position of the ray.
One easy way to find the density is to simply reach for the nearest sample to the ray's position. But, we can do much better in reconstructing the surface if we use convolution to interpolate between a neighborhood of samples. This is precisely what the fragment shader does for each sample location.
In addition to finding an approximation of the volume at each position, it's useful to also find an approximation of the gradient. This allows us to reconstruct a surface normal, allowing us to find if the rendered surface is in light or shadow.
Below is a very rough sketch of what the fragment shader does many times for each ray to find an approximation of the volume and its gradient:
struct ConvoResult { bool inside; float value; vec3 grad; }; // Perform convolution of the volume encoded in uSampler at the // world-space position given by `xw`. ConvoResult conv(vec3 xw) { ConvoResult result; // Find the position of the sample point in index space. uWtoI is // a matrix that converts from world-space coordinates to the // index-space of the texture -- i.e. a space in which the // coordinate [0, 0, 0] is the first sample of the texture, [1, 0, // 0] is the second sample, and so on, upto [max0 - 1, max1 - 1, // max2 - 1], where max0, max1, and max2 are the size of the // texture. vec4 xi = uWtoI * xwh; ... A bunch of index math to figure out the upper and lower bounds of the "neighborhood" of xi ... // Iterate over all of the voxels in the neighborhood of xi. The // SUPPORT is the size of this neighborhood. for(int i0 = 0; i0 < SUPPORT; ++i0) { for(int i1 = 0; i1 < SUPPORT; ++i1) { for(int i2 = 0; i2 < SUPPORT; ++i2) { // Sample the texture! float v = texelFetch(uSampler, i, 0).r; ... Multiply v by a bunch of factors, basically the result of evaluating the kernel. ... } } } return result; }
At each location, we need to find the (closest) index into the texture that the location corresponds to (an affine mapping defined by the uWtoI matrix above), and then look around a neighborhood of that location to reconstruct the value at the precise world location that we care about.
Now that we can find an approxmiation of the volume at each point along the ray, we need to map the volume (and its gradient) to a color. The way that we perform this mapping is with a so called “transfer function”. This is a mapping directly from densities in the volume to colors, including alpha, the “opaqueness” of the color.
Including this alpha component is important because it allows us to “hide” certain densities from the rendered output, allowing us to visualize surfaces that would otherwise be obscured. The below video demonstrates adding a new control point to the transfer function in order to visualize and color the more dense dentin of the tooth.
The user can edit the transfer function, exposing interior features of the volume.
Technically, the transfer function is implemented as yet another texture, albeit with 1 dimension instead of three. Every time the user modifies the transfer function using the widget, a new texture is generated which linearly interpolates between the control points. This texture is used by the fragment shader to associate a color with each density.
In addition to changing the transfer function on-the-fly, the user can rotate and relocate the virtual “camera” in order to view different parts of the volume. When the user clicks and drags on the viewport, this motion is mapped onto a rotation which is applied to the camera about the camera's origin.
When the user presses shift and drags, the drag vector (a vector pointing from the beginning of the drag to the end in screen-space) is projected into world-space, and the camera's origin is offset by this vector. This ensures that the object's origin moves “under” the cursor, irrespective of the camera's distance from the origin or its field of view.
Navigating the viewport using keyboard and mouse.
You can play with the visualizer here.
