经过配置后,VS2019+VTK 9.3,第一个VTK程序,可显示。
#include <vtkAutoInit.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkConeSource.h>
#include <array>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
int main(int, char* [])
{
vtkNew<vtkNamedColors> colors;
// 设置背景颜色
std::array<unsigned char, 4> bkg{ {26, 51, 102, 255} };
colors->SetColor("BkgColor", bkg.data());
// 创建一个8棱圆柱体
vtkNew<vtkCylinderSource> cylinder;
cylinder->SetResolution(8);
// 创建映射器
vtkNew<vtkPolyDataMapper> cylinderMapper;
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
// 输入映射器,设置属性、转换角度或者纹理贴图;
vtkNew<vtkActor> cylinderActor;
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
// 渲染器将生成图像,然后显示在渲染窗口上。
vtkNew<vtkRenderer> renderer;
renderer->AddActor(cylinderActor);
renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
// 重置相机,设置缩放倍数
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
// 显示窗口
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(300, 300);
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("Cylinder");
// 渲染窗口交互器可捕获鼠标事件,并将执行适当的照相机或演员操作;
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// 启动事件循环,整条管线被动执行;
renderWindow->Render();
renderWindowInteractor->Start();
}
显示: