代码效果
本代码编译运行均在如下链接文章生成的库执行成功,若无VTK库则请先参考如下链接编译vtk源码:
VTK —— 一、Windows10下编译VTK源码,并用Vs2017代码测试(附编译流程、附编译好的库、vtk测试源码)
教程描述
本示例演示在渲染窗口中使用多个渲染器。
完整源码
#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>
#include <iostream>
#include <thread>
int main(int, char*[])
{
// 创建VTK命名颜色
vtkNew<vtkNamedColors> colors;
// 创建多边形圆锥体
vtkNew<vtkConeSource> cone;
cone->SetHeight(3.0); // 设置圆锥体的高度
cone->SetRadius(1.0); // 设置圆锥的底半径
cone->SetResolution(10); // 设置用于表示圆锥的刻面数
// 将多边形数据映射到图形基元映射器
vtkNew<vtkPolyDataMapper> coneMapper;
coneMapper->SetInputConnection(cone->GetOutputPort()); // 设置给定输入端口索引的连接
// 创建渲染场景中的实体(几何体和属性)
vtkNew<vtkActor> coneActor;
coneActor->SetMapper(coneMapper); // 设置映射器: 将参与者连接到可视化管道末尾
coneActor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData()); // 设置模型颜色
// 创建渲染器1
vtkNew<vtkRenderer> ren1;
ren1->AddActor(coneActor); // 在渲染器中添加实体
ren1->SetBackground(colors->GetColor3d("RoyalBlue").GetData()); // 设置渲染屏幕背景色
ren1->SetViewport(0.0, 0.0, 0.5, 1.0); // 指定要在渲染窗口中绘制的视口。坐标表示为(xmin,ymin,xmax,ymax),其中每个坐标为0<=坐标<=1.0。
// 创建渲染器2
vtkNew<vtkRenderer> ren2;
ren2->AddActor(coneActor); // 在渲染器中添加实体
ren2->SetBackground(colors->GetColor3d("DodgerBlue").GetData()); // 设置渲染屏幕背景色
ren2->SetViewport(0.5, 0.0, 1.0, 1.0); // 指定要在渲染窗口中绘制的视口。坐标表示为(xmin,ymin,xmax,ymax),其中每个坐标为0<=坐标<=1.0。
// 为渲染器创建绘制窗口
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(ren1); // 添加渲染器1
renWin->AddRenderer(ren2); // 添加渲染器2
renWin->SetSize(600, 300); // 设置渲染窗口的大小
renWin->SetWindowName("Tutorial_Step3"); // 设置渲染窗口名称
ren1->ResetCamera(); // 重置渲染器摄像机
ren1->GetActiveCamera()->Azimuth(90); // 获取当前相机后将围绕以焦点为中心的向上视图矢量旋转相机,结果是相机的水平旋转90°
// 360度上循环,每次都渲染圆锥体
for (int i = 0; i < 360; ++i)
{
// 要求此渲染窗口所有渲染器渲染其图像并同步此过程
renWin->Render();
// 获取当前相机后将围绕以焦点为中心的向上视图矢量旋转相机,结果是相机的水平旋转。
ren1->GetActiveCamera()->Azimuth(1);
ren2->GetActiveCamera()->Azimuth(1);
// 每10ms执行一次
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
system("pause");
return EXIT_SUCCESS;
}
笔者
笔者 - jxd