How to Get VTK Working on Your Home Machine

(or How to be 1337)

by Brent Hughes

 

 

This tutorial assumes that you’re using Visual Studio 2005.

 

First, download the source and the data from:

 

http://www.vtk.org/get-software.php

 

Don’t bother downloading the Win32 installer.  It doesn’t work.

 

 

 

 


The following steps assume that you’ve stored the downloaded files in a directory called C:\VTK as shown below:

 

 


Next, just extract each of the files to a folder of the same name as shown below:

 

I’m using WinRAR for this. You can use WinZip or whatever you prefer.


Okay, so we’ve extracted the source and the data files. Our directory should look like the one shown below:

 


Next, create a bin directory. We’ll use this later.


Okay, now we need to install CMake. Grab the files from here:

 

http://www.cmake.org/HTML/Download.html

 

You want the Win32 installer. Simply download and run the installer. Accept the defaults for everything except when it asks you to add CMake to the system path. For this choice, tell it to add CMake to the system path for all users.


Now there will be an entry for CMake on your start menu. Click the CMake shortcut there to run CMake. You will see a window like the one shown below:

 

This dialog box is asking two simple questions. It wants to know where the source code is and where you want to place the output. Populate the form as shown above and hit the configure button.


Next you will see a screen like the one shown below:

 

Since you are using Visual Studio 8 2005, you can just hit OK. After hitting OK, you will see a bunch of status messages in the status bar of the main form. It will take a while for this configuration process to complete. Now would be a good time to brush up on your leetspeak:

 

http://en.wikipedia.org/wiki/L33t

 

Okay, now that you are 1337, we can continue the installation. It is actually impossible for a n00b to install VTK. Thank goodness for Wikipedia.


Next you will see a screen that looks like this:

 

We need to do two things on this screen. We need to tell CMake to build the examples for us. We also need to enter the proper location for the VTK_DATA_ROOT variable.


Set the screen up so that it looks like the one below:

 

Then, hit the configure button.


The little pink bars will change to little gray ones as shown below:

 


Next, you’ll want to hit the OK button. You’ll see some data in the status bar stating that the program is generating output. This is shown below:

 

 

Once this number becomes 100%, the program will terminate.


Now we need to go to the bin directory we created earlier and open the solution file in there:

 


Once you’ve opened the solution file, you’ll see a project in the solution called INSTALL. You’ll need to right click on this project and say “Build”.

 

The INSTALL project is shown highlighted above. After you start the build you will have some time on your hands. Now would be a good time to have a couple beers. Programming drunk helps build up your skills.

 

Wow! This really takes a long time.

 

Okay. It’s been about an hour…

 

Okay. It’s finally installed.


Now that VTK is installed, we can make a little project and test it out. Open a new instance of Visual Studio and create an empty console project. Then add a file called main.cpp.

 

Grab some sample C++ code from here:

http://www.vtk.org/example-code.php

 

Paste it into your main.cpp file and hit F5. The compiler’s going to tell you something like “blah blah blah can’t find include directory”. Here’s a picture.

 


Now we need to add our include directories. Add the "C:\Program Files\VTK\include\vtk-5.0" directory to the include path as shown below:

 

 


Hit F5 again. You’ll get an error on this line:

 

aSphere->GetProperty()->SetColor(0,0,1); // sphere color blue

 

Simply remove the line or comment it out.

 

Hit F5 again. Oh no!!! Linker errors. It looks like we need to link some .lib files. First, add the lib path as shown in the screenshot below:

 


Hit F5 again. Oh no!!! We still have linker errors. Looks like we need to specify the linked files explicitly. Do so as shown in the screenshot below:

 

It’s under LinkeràInputàAdditional Dependencies. Type in the names of all the lib files in the directory “C:\Program Files\VTK\lib”. The easiest way to extract this list is to use a command window.


Hit F5 again. Oh no!!! We still have linker errors. This time it’s complaining about the OpenGL libraries. Add opengl32.lib to the list of additional dependencies as shown below.

 


Hit F5 again. Surprise! Your program actually runs this time.

 

 

You have now acquired a small amount of 1337n355. But that console window is pretty lame. Let’s get rid of it.


Open a new instance of Visual Studio 2005. Create a new Win32 project as shown below.


Go to Application Settings and click Empty Project as shown below:

 

Then click finish. Add a main.cpp file as before. Then, add your include and lib directories and explicitly link your lib files. Also, paste in the source code as before.


Hit F5 again. Oh no!! More link errors. You’ll see some link errors as shown below:

 

It says something about an unresolved external symbol called WinMain.

 


Okay… Long story short, you’re working with a windows program now, so you’ll have to use this prototype in place of main():

 

int WINAPI WinMain(HINSTANCE hInstance,

                   HINSTANCE hPrevInstance,

                   LPSTR lpCmdLine,

                   int nCmdShow)

 

When you’ve changed the prototype, included windows.h, and set the routine to return 0, your program will be ready to run.

 

You’ll see something like this:

 

No more lame console window. Welcome to VTK! You are now ready to join the ranks of the 5up3r1337. In case you need the final code, I’ve pasted it in on the next page.


#include <windows.h>

 

#include "vtkSphereSource.h"

#include "vtkPolyDataMapper.h"

#include "vtkActor.h"

#include "vtkRenderWindow.h"

#include "vtkRenderer.h"

#include "vtkRenderWindowInteractor.h"

 

int WINAPI WinMain(HINSTANCE hInstance,

                   HINSTANCE hPrevInstance,

                   LPSTR lpCmdLine,

                   int nCmdShow)

{

 

      // create sphere geometry

      vtkSphereSource *sphere = vtkSphereSource::New();

      sphere->SetRadius(1.0);

      sphere->SetThetaResolution(18);

      sphere->SetPhiResolution(18);

 

      // map to graphics library

      vtkPolyDataMapper *map = vtkPolyDataMapper::New();

      map->SetInput(sphere->GetOutput());

 

      // actor coordinates geometry, properties, transformation

      vtkActor *aSphere = vtkActor::New();

      aSphere->SetMapper(map);

 

      // a renderer and render window

      vtkRenderer *ren1 = vtkRenderer::New();

      vtkRenderWindow *renWin = vtkRenderWindow::New();

      renWin->AddRenderer(ren1);

 

      // an interactor

vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();

      iren->SetRenderWindow(renWin);

 

      // add the actor to the scene

      ren1->AddActor(aSphere);

      ren1->SetBackground(1,1,1); // Background color white

 

      // render an image (lights and cameras are created automatically)

      renWin->Render();

 

      // begin mouse interaction

      iren->Start();

 

      return 0;

}