Rendering Tubes and Spheres in VTK

VTK has long had an ability to render edges of a mesh on top of the mesh to aid in understanding the underlying geometry. This is accomplished using vtkProperty’s EdgeVisibility and EdgeColor methods. That has been a popular feature and yields nice results as shown below with the dragon dataset.

But wouldn’t it be nice if we could take that to the next level and render the mesh edges as tubes and even highlight the vertices as spheres? Previously to do this in VTK you would need to use the tube filter and glyph3D but now there are options built into the OpenGL2 backend to render lines as tubes and points as spheres. Let’s take a look at the same dragon mesh using the new methods.

To accomplish this we use a few new methods in vtkProperty. First we turn on RenderLinesAsTubes which directs the rendering engine to render any lines as tubes with a width in pixels set by the existing LineWidth property. Now when EdgeVisibility is turned on we get the surface rendered with tubes for the mesh edges. But what about the vertices? There is a similar method named RenderPointsAsSpheres that when rendering points, will render them as spheres with a diameter in pixels specified by the existing PointSize property. To get the vertices rendered as points while the mesh is also rendered as a surface we have added methods named VertexVisibility and VertexColor. These methods parallel the EdgeVisibility and EdgeColor methods already in vtkProperty but operate on the vertices of the mesh. Combining all this, the code looks like:

Generally you want the line width to be half the point size for best results. This feature is available in the OpenGL2 backend and it isn’t really rendering tubes and spheres. That would require lots of additional geometry. Instead what it does is use an impostor technique to make wide lines look like tubes to the lighting and depth buffer. For spheres it takes square point sprites and makes them look like spheres to the lighting and depth buffers. Be aware that for large meshes, rendering a 12×12 point sprite per vertex can result in a lot of pixels being drawn when the entire mesh is shown, so use this power judiciously.