文章目录
-
- [1. 概述](#1. 概述)
- [2. CMake链接VTK](#2. CMake链接VTK)
- [3. main.cpp文件](#3. main.cpp文件)
- [4. 演示效果](#4. 演示效果)
更多精彩内容 |
---|
👉内容导航 👈 |
👉VTK开发 👈 |
1. 概述
演示如何添加交互器vtkRenderWindowInteractor和交互器样式vtkInteractorStyleTrackballCamera。
交互器启动事件循环后窗口不会自动退出。
环境 | 说明 |
---|---|
系统 | ubuntu22.04、windows11 |
cmake | 3.22、3.25 |
Qt | 5.14.2 |
编译器 | g++11.4、msvc2017 |
VTK | 9.4.1 |
2. CMake链接VTK
shell
cmake_minimum_required(VERSION 3.20 FATAL_ERROR) # 设置CMake最低版本, 如果版本低于3.20, 则报错
project(vtk1) # 设置工程名
# 设置C++标准
set(CMAKE_CXX_STANDARD 14)
# 设置MSVC编译器使用UTF-8编码
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
endif()
# 输出路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)
set(VTK_DIR "E:/lib/VTK/lib/cmake/vtk-9.4") # 设置VTK的路径
# 查找VTK
find_package(VTK
COMPONENTS
CommonColor
CommonCore
FiltersSources
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingOpenGL2
)
if (NOT VTK_FOUND) # 如果VTK没有找到
message(FATAL_ERROR "VTK 没找到") # 报错
return()
endif()
add_executable(vtk1 main.cpp) # 添加可执行文件
target_link_libraries(vtk1 PRIVATE ${VTK_LIBRARIES}) # 链接VTK库
# 设置VTK模块自动初始化
vtk_module_autoinit(
TARGETS vtk1
MODULES ${VTK_LIBRARIES}
)
3. main.cpp文件
cpp
/********************************************************************************
* 文件名: main.cpp
* 创建时间: 2025-03-10 21:46:33
* 开发者: MHF
* 邮箱: [email protected]
* 功能:
*********************************************************************************/
#include<iostream>
#include<vtkNew.h>
#include<vtkNamedColors.h>
#include<vtkConeSource.h>
#include<vtkPolyDataMapper.h>
#include<vtkActor.h>
#include<vtkProperty.h>
#include<vtkRenderer.h>
#include<vtkRenderWindow.h>
#include<vtkCamera.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkInteractorStyleTrackballCamera.h>
using namespace std;
int main()
{
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> coneActor1; //创建一个圆锥演员
coneActor1->SetMapper(coneMapper); //设置圆锥演员的映射器
coneActor1->GetProperty()->SetColor(0.2, 0.63, 0.79); //设置圆锥演员的颜色
coneActor1->GetProperty()->SetDiffuse(0.7); //设置圆锥演员的漫反射系数
coneActor1->GetProperty()->SetSpecular(0.6); //设置圆锥演员的镜面反射系数
coneActor1->GetProperty()->SetSpecularPower(30); //设置圆锥演员的镜面反射功率
//创建一个渲染器1
vtkNew<vtkRenderer> renderer1; //创建一个渲染器
renderer1->AddActor(coneActor1); //将圆锥演员添加到渲染器中
renderer1->SetBackground(colors->GetColor3d("SlateGray").GetData()); //设置渲染器的背景颜色
//创建一个渲染窗口
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer1); //将渲染器添加到渲染窗口中
renderWindow->SetWindowName("vtk示例5"); //设置渲染窗口的名称
renderWindow->SetSize(640, 480); //设置渲染窗口的大小
vtkNew<vtkRenderWindowInteractor> iren; //创建一个渲染窗口交互器
iren->SetRenderWindow(renderWindow); //设置渲染窗口交互器的渲染窗口
vtkNew<vtkInteractorStyleTrackballCamera> style; //创建一个交互器样式
iren->SetInteractorStyle(style); //设置渲染窗口交互器的样式
iren->Initialize(); //初始化渲染窗口交互器
iren->Start(); //启动渲染窗口交互器,事件循环开始执行
return 0;
}
4. 演示效果
