In 2007, I revisited VTK and seeking for possibilities of using VTK with Cocoa framework in MacOSX. I thought that should be somehow straight forward since Osirix has been out for a while and it is using VTK doing graphics rendering. After digging deeper into Kitware website, I found that the CocoaVTK entry in VTK wiki is really helpful. Below are my step by step walkthrough to have VTK facilities into Xcode and had it work in Xcode, Cocoa, Interface Builder way.

Build VTK libraries

  • Download and build cmake from here.
  • I use MacPorts, so I can just do:

    $port install cmake

  • Download VTK from here
  • I used VTK release 5.0.2, vtk-5.0.2.tar.gz

  • Initialize separate building directory as following steps
  • $cd ~/dev

    $tar xvzf vtk-5.0.2.tar.gz

    $mkdir ~/dev/VTKBuild

    $cd ~/dev/VTKBuild

    $/opt/local/bin/cmake ../VTK

  • Modify CMake building config file: CMakeCache.txt
  • Things that you need to change:

    ...

    //Build classes using Carbon API.

    VTK_USE_CARBON:BOOL=OFF

    //Build classes using Cocoa API.

    VTK_USE_COCOA:BOOL=ON

    ...

    CMAKE_OSX_ARCHITECTURES:STRING=ppc;i386 CMAKE_OSX_SYSROOT:STRING=/Developer/SDKs/MacOSX10.4u.sdk/

    You can download my CMackeCache.txt from here.

    $cd ~/dev/VTKBin

    $/opt/local/bin/cmake ../VTK

    $make

    And wait. Depending on your machine speed, the building process might take from 15 minutes to an hour. If you don't want to build it yourself and you trust my pre-built binary, you can download statically pre-built library files from here.


    Config and use Xcode + Interface Builder

  • Launch Xcode
  • Xcode -> Preferences -> Source Tree

    Add "vtk-lib" pointing to "~/dev/VTKBuild/bin"

    Add "vtk-include" pointing to "~/dev/VTKBuild/include/vtk-5.0"

    The config result should look like this (I renamed the "VTKBuild/bin" directory to "VTKBuild/lib" in pre-built library ZIP file):

  • Start a "Cocoa Application" project in Xcode
  • Add existing files (vtk static libraries)

    Modify building options

  • Launch Interface Builder with bundled "MainMenu.nib" file
  • Copy files from Cocoa_VTK Wiki entry example:

    BasicVTKView.h

    BasicVTKView.mm

    Drag a "Custom View" to your "Window", bring up its inspector, choose BasicVTKView in "Custom Class" drop down menu.

  • More...
  • Download and look at the example application here.


  • Result SciViz application screenshot
  • Notes

  • Because we are going to use C++ instead of just C in using VTK, the code you generate should be using Objective-C++ instead of just Objective-C. gcc is strict in source code extension, so change Xcode auto-generated .m code files to .mm files to avoid gcc complaints.

  • References

  • Cocoa_VTK in VTK Wiki - Link
  • My CMakeCache.txt - Download
  • Pre-built VTK static libraries and headers for MacOSX (using Cocoa) - Download
  • Example VTK Program - Download