Mac OS X is a great platform for 3D visualization. It includes OpenGL as standard, which means many existing libraries and applications can easily be ported to the platform. One such library is the Visualization Toolkit (VTK), an open source project run by Kitware, Inc. VTK is at a level above OpenGL: where OpenGL deals with simple polygons, VTK deals with isosurfaces, cut planes, and other 3D actors. It’s at a level useful to scientists, and relatively easy to learn.

VTK works great on the Mac, and even has built in support for Carbon and Cocoa. In the coming weeks, I will show you how to use VTK in Cocoa applications, but in this tutorial we will do a bit of preparation: we are going to compile and install VTK.

Name Your Sources

I want to begin by giving credit where it’s due: most of the information presented here has been stolen from a great piece on the VTK wiki by Ryan Glover. He covers building and installing VTK on the Mac for use with Cocoa, and I will follow his instructions fairly closely here, just adding my experiences where appropriate.

I also want to thank Marc Baaden before I get started, because this tutorial, and future ones, were largely inspired by him. He recently rewrote some code that I developed to demonstrate Cocoa and VTK integration many years ago, making it work on the latest versions of Mac OS X. We’ll meet that code in the next tutorial.

Installing CMake

VTK uses an autoconf like tool called CMake to configure the make files used to build VTK. So before you can build VTK, you need to install CMake.

Go to the CMake download site and locate the most recent binaries for your system. At the time of writing, the file was called cmake-2.4.6-Darwin-universal.dmg. Open the disk image, and double click the package to install.

Installing VTK5

Now to VTK itself. Go to the VTK download page and download the latest source files. I downloaded the vtk-5.0.3.tar.gz file. Decide where you would like to build VTK and unpack the sources there by double clicking the tar bundle. I choose to unpack the sources in the directory /Users/drew/Develop, and will use that path for the rest of this tutorial. After unpacking, the sources themselves were in /Users/drew/Develop/VTK.

Now we need to create a couple of directories: one is used for building, and one is for installing the VTK binaries. I created these directories as follows:

mkdir /Users/drew/Develop/VTKBuild
mkdir /Users/drew/Develop/VTKBin

To configure ready for building, change to the build directory, and run cmake passing the path to the VTK source root as argument.

cd /Users/drew/Develop/VTKBuild
cmake /Users/drew/Develop/VTK

This will generate a file called CMakeCache.txt in the VTKBuild directory; this file contains the configuration that will be used to build VTK.

The default configuration targets Carbon, rather than Cocoa, so we need to make a few changes. Use your favorite text editor to open CMakeCache.txt, and one by one, locate the lines shown below. Edit them as shown.

VTK_USE_CARBON:BOOL=OFF
VTK_USE_COCOA:BOOL=ON
CMAKE_INSTALL_PREFIX:PATH=/Users/drew/Develop/VTKBin

Ryan Glover also suggests changing the following line

CMAKE_OSX_ARCHITECTURES:STRING=ppc;i386

such that VTK is built to run universally, but when I tried it I got compile errors. If you are a daredevil, you might give it a try. Maybe by the time you read this, the problem will be fixed.

To update the build settings based on the changes you just made, rerun cmake from the VTKBuild directory. It will read the CMakeCache.txt file, and propagate the changes to the make files.

cmake /Users/drew/Develop/VTK

We are finally ready to compile. Set the following environment variable to target Tiger

export MACOSX_DEPLOYMENT_TARGET=10.4

and then enter

make -j 2

The -j option will run the build in parallel, so that if you have a multi-core Mac, things will go much faster.

Assuming that all goes well, the last step is to install the binaries in the VTKBin directory:

make install

With any luck, you should see that VTKBin contains include and lib directories, and you are ready to start using VTK in your Cocoa apps.