VTK提供了两种方式用于文本绘制,分别用于二维和三维。二维显示是在三维图形窗口顶部绘制文本,通常会涉及的是在层叠平面上绘制。三维标注是创建三维的多边形数据文本,可以想三维图形对象一样进行变换和显示。
二维文本
绘制二维文本需要用到二维的actor(vtkActor2D或它的子类vtkTextActor)和mapper(vtkMapper2D或它的子类vtkTextMapper)。二维的actor和mapper和三维的类似,不同的是前者是在图形的顶层层叠平面上进行渲染的。
类及具体用法:
vtkTextActor
用vtkTextActor绘制二维文本,可以通过vtkTextProperty设置字体,文本颜色,粗体,斜体以及字体的阴影等效果。
auto textActor =
vtkSmartPointer<vtkTextActor>::New();
textActor->SetInput(HELLO_TEXT);
textActor->SetPosition2(20, 40);
textActor->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
textActor->GetTextProperty()->SetFontSize(32);
renderer->AddActor2D(textActor);
三维文本
三维文本绘制的实现是使用vtkVectorText创建文本字符串的多边形表达式,然后把它渲染到三维场景中。放置三维文本的类可以是vtkActor,也可是它的子类vktFollower。区别在于,vtkFollower对象总是面向渲染器当前的相机,因此这种文本总是可见的。
vtkVectorText
创建表达式:
auto textSource =
vtkSmartPointer<vtkVectorText>::New();
textSource->SetText(HELLO_TEXT);
三维渲染:
vtkActor
用vtkActor渲染:
auto textActor =
vtkSmartPointer<vtkActor>::New();
textActor->SetMapper(textMapper);
textActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
renderer->AddActor(textActor);
vtkFollower
用vtkFollower渲染:
auto textFollower =
vtkSmartPointer<vtkFollower>::New();
textFollower->SetMapper(textMapper);
textFollower->GetProperty()->SetColor(1.0, 0.0, 0.0);
textFollower->SetCamera(renderer->GetActiveCamera());
renderer->AddActor(textFollower);
DrawText.cxx
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
#include <vtkVectorText.h>
#include <vtkFollower.h>
#include <vtkInteractorStyle.h>
const char* const HELLO_TEXT = "Hello world";
int main(int argc, char* argv[])
{
if (argc < 2)
{
return EXIT_FAILURE;
}
auto sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetRadius(3.0);
sphereSource->SetCenter(0.0, 0.0, 0.0);
auto sphereMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
auto sphereActor =
vtkSmartPointer<vtkActor>::New();
sphereActor->SetMapper(sphereMapper);
sphereActor->GetProperty()->SetOpacity(0.3);
auto renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(sphereActor);
auto renWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renWindow->SetSize(400, 400);
renWindow->AddRenderer(renderer);
auto interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renWindow);
int dtype = atoi(argv[1]);
if (dtype == 1)
{
auto textActor =
vtkSmartPointer<vtkTextActor>::New();
textActor->SetInput(HELLO_TEXT);
textActor->SetPosition2(20, 40);
textActor->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
textActor->GetTextProperty()->SetFontSize(24);
renderer->AddActor2D(textActor);
}
else if (dtype == 2 || dtype == 3)
{
auto textSource =
vtkSmartPointer<vtkVectorText>::New();
textSource->SetText(HELLO_TEXT);
auto textMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
textMapper->SetInputConnection(textSource->GetOutputPort());
if (dtype == 2)
{
auto textActor =
vtkSmartPointer<vtkActor>::New();
textActor->SetMapper(textMapper);
textActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
renderer->AddActor(textActor);
}
else if (dtype == 3)
{
auto textFollower =
vtkSmartPointer<vtkFollower>::New();
textFollower->SetMapper(textMapper);
textFollower->GetProperty()->SetColor(1.0, 0.0, 0.0);
textFollower->SetCamera(renderer->GetActiveCamera());
renderer->AddActor(textFollower);
}
}
renWindow->Render();
renderer->ResetCamera();
renWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
Ref
VTK/Examples/Cxx/Visualization/DrawText
VTK/Examples/Cxx/Visualization/VectorText
VTK/Examples/Cxx/Visualization/Follower