VTK Tutorial Step1 学习

概述.

在本例中创建一个圆锥体的多边形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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值