vtk

Hello World

Hello World Example

#include <vtkAutoInit.h>

VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);

#include <vtkSmartPointer.h>
#include <vtkTextActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

int main(int /*argc*/, char ** /*argv*/)
{
    auto textActor = vtkSmartPointer<vtkTextActor>::New();
    textActor->SetInput("Hello World");

    auto renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(textActor);
    renderer->ResetCamera();

    auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    
    auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    renderWindow->SetInteractor(interactor);
    
    interactor->Start();

    return 0;
}

Breakdown:

#include <vtkAutoInit.h>

VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);

VTK design uses a factory method design pattern to create new instances of vtkObject derived classes using the <ClassName>::New() method. This allows a platform specific implementation to be selected during runtime to satisfy a required interface.

For this mechanism to work, factory classes need to “register” themselves so that they can be selected by the vtk infrastructure. Details on this topic is available here.

VTK_MODULE_INIT is a macro used to automatically initialize the required modules/library(ies)(vtkRenderingOpenGL2, vtkRenderingFreeType, vtkInteractionStyle in this example). Failure to initialize the modules will result in <ClassName>::New() calls to return NULL and therefore runtime errors.

#include <vtkSmartPointer.h>
#include <vtkTextActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>

vtkSmartPointer role is similar to that of a std::unique_ptr in that it manages the reference count that controls the lifetime of vtkObject derived class instances.

vtkTextActor is a simple class that can be used to display strings on the screen.

vtkRenderer is a class responsible for managing a scene’s contents. Specifically it manages the collection of

vtkRenderWindow is a class that provides platform independent interface for

  • managing a collection of renderers.
  • handling user input and forwarding it to vtkRenderWindowInteractor for further processing

vtkRenderWindowInteractor is a class responsible for mapping the user input (mouse/keyboard/timing) events to a corresponding action. Internally it uses a vtkInteractorStyle to provide different mapping behaviors.

auto textActor = vtkSmartPointer<vtkTextActor>::New();
textActor->SetInput("Hello World");

Create text actor and set the string to display

auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(textActor);
renderer->ResetCamera();
  • Create a renderer
  • Add the text actor to it
  • Resets the camera position to make sure the actor is visible in the screen.
auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetInteractor(interactor);

Create a window to render into, add the renderer to it and set the interactor. The factory function will automatically pick a suitable implementation based on the available/registered factory classes

interactor->Start();

This is a blocking call that returns only when the user requests a quit (q key) or closes the window. Runs a message loop and dispatches the messages.

Running this should create a window that looks like this

Hello World

Notes

This list of DLLs that were used by this exe are:

VTKCommonCore-7.0.DLL

VTKInteractionStyle-7.0.DLL

VTKRenderingCore-7.0.DLL

VTKRenderingFreeType-7.0.DLL

VTKRenderingOpenGL2-7.0.DLL


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow