Skip to content

DrawText

web-test/Cxx/Visualization/DrawText



Description

This example writes "Hello world" in the bottom left corner of the render window. The text size and color are set. There are many properties that can be set that control how the text is changed when the render window is resized, but they are not covered in this example.

This example draws text in 2D. For an example using 3D text that can be placed on the scene like a regular vtkActor, see VectorText. For an example of drawing text, also in 2D, but into a vtkImageData object rather than into the render window, see ImageText.

Seealso

TextWidget

Other languages

See (Java)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

DrawText.cxx

#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Create a sphere.
  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->SetCenter(0.0, 0.0, 0.0);
  sphereSource->SetRadius(5.0);
  sphereSource->Update();

  // Create a mapper.
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputData(sphereSource->GetOutput());

  // Create an actor.
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  // Create a renderer.
  vtkNew<vtkRenderer> renderer;
  actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

  // Create a render window.
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("DrawText");

  // Create an interactor.
  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Setup the text and add it to the renderer.
  vtkNew<vtkTextActor> textActor;
  textActor->SetInput("Hello world");
  textActor->SetPosition2(10, 40);
  textActor->GetTextProperty()->SetFontSize(24);
  textActor->GetTextProperty()->SetColor(colors->GetColor3d("Gold").GetData());
  renderer->AddActor2D(textActor);

  // Render and interact.
  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(DrawText)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "DrawText: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(DrawText MACOSX_BUNDLE DrawText.cxx )
  target_link_libraries(DrawText PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS DrawText
  MODULES ${VTK_LIBRARIES}
)

Download and Build DrawText

Click here to download DrawText and its CMakeLists.txt file. Once the tarball DrawText.tar has been downloaded and extracted,

cd DrawText/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./DrawText

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.