概述.
在本例中创建一个圆锥体的多边形model,并将其在屏幕上渲染。圆锥体将会旋转360°后退出。在VTK代码中最基本的步骤是 source->mapper->actor->renderer->renderwindow
Tutorial_Step1.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkConeSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
int main(int, char*[])
{
//
// 我们实例化一个 color 用来选择颜色和背景颜色
vtkNew<vtkNamedColors> colors;
//
// 实例化一个source 并设定其一些属性
// vtkConeSource "cone" 是pipline中的 source 部分
// 其输出的 vtkPolyData 类型的数据可被其他 filter 处理
vtkNew<vtkConeSource> cone;
cone->SetHeight(3.0);
cone->SetRadius(1.0);
cone->SetResolution(10);
//
// 创建一个mapper来结束这个pipline (可以使用例如vtkShrinkPolyData的filter在source和mapper
// 中间)
// 实例化一个vtkPolyDataMapper将 polygonaldata 映射到 graphics primitive
// 链接cone source的输出,输入到 coneMapper中
//
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputConnection(cone->GetOutputPort());
//
// 创建一个actor来代表这个cone.
// 这个actor负责渲染mapper的graphics primitives
// 同时actor通过vtkProperty 来引用其属性 , 包括一个内部的转换矩阵
// 我们把这个actor的映射器设置为我们上面创建的coneMapper
vtkNew<vtkActor> coneActor;
coneActor->SetMapper(coneMapper);
coneActor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
//
// 创建一个renderer 并给其分配一个actor
// renderer 就类似一个可视化出口
// 它是屏幕上一个窗口的一部分或全部,它负责绘制它的演员
// 同时我们在此设置背景颜色
//
vtkNew<vtkRenderer> ren1;
ren1->AddActor(coneActor);
ren1->SetBackground(colors->GetColor3d("MidnightBlue").GetData());
//
// 最终我们创建了一个渲染窗口将会展示在屏幕
// 我们使用AddRenderer放入我们的renderer到这个渲染窗口
// 设置窗口大小为 300 pixels by 300
//
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(ren1);
renWin->SetSize(300, 300);
renWin->SetWindowName("Tutorial_Step1");
//
// 循环360个角度并渲染这个cone
for (int i = 0; i < 360; ++i)
{
// 渲染
renWin->Render();
// 旋转相机视角在每个角度
ren1->GetActiveCamera()->Azimuth(1);
}
return EXIT_SUCCESS;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(Tutorial_Step1)
find_package(VTK COMPONENTS
vtkCommonColor
vtkCommonCore
vtkFiltersSources
vtkInteractionStyle
vtkRenderingContextOpenGL2
vtkRenderingCore
vtkRenderingFreeType
vtkRenderingGL2PSOpenGL2
vtkRenderingOpenGL2
QUIET
)
if (NOT VTK_FOUND)
message("Skipping Tutorial_Step1: ${VTK_NOT_FOUND_MESSAGE}")
return()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
# old system
include(${VTK_USE_FILE})
add_executable(Tutorial_Step1 MACOSX_BUNDLE Tutorial_Step1.cxx )
target_link_libraries(Tutorial_Step1 PRIVATE ${VTK_LIBRARIES})
else()
# 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(Tutorial_Step1 MACOSX_BUNDLE Tutorial_Step1.cxx )
target_link_libraries(Tutorial_Step1 PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS Tutorial_Step1
MODULES ${VTK_LIBRARIES}
)
endif()