图中左边是几何数据渲染的方法 右边是体数据渲染方法
虚线部分表示两者不同之处,从上图我们能够看出来,两种渲染方式不同点主要在Mapper和Actor对象上
代码实现
CMakeLists.txt文件代码如下
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(volumeRender)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(volumeRender volumeRender.cpp)
TARGET_LINK_LIBRARIES(volumeRender ${VTK_LIBRARIES})
volumeRender.cpp文件代码如下
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
#include "vtkDICOMImageReader.h"
#include <vtkSmartPointer.h>
#include <vtkMetaImageReader.h>
#include <vtkPiecewiseFunction.h>
#include <vtkColorTransferFunction.h>
#include <vtkVolumeProperty.h>
#include <vtkSmartVolumeMapper.h>
#include <vtkVolume.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkImageCast.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkImageData.h>
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
int main()
{
vtkSmartPointer<vtkMetaImageReader> reader =
vtkSmartPointer<vtkMetaImageReader>::New();
reader->SetFileName("/home/jack/projects/VTK/9demo/1.3.6.1.4.1.14519.5.2.1.6279.6001.105756658031515062000744821260.mhd");
reader->Update();
//设置不透明度传递函数,此为一维分段传输函数
vtkSmartPointer<vtkPiecewiseFunction> opacityTransferFunction =
vtkSmartPointer<vtkPiecewiseFunction>::New();
opacityTransferFunction->AddPoint(20, 0.0);
opacityTransferFunction->AddPoint(255, 0.1);
opacityTransferFunction->AddPoint(1005, 0.6);
//设置颜色传递函数
vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction =
vtkSmartPointer<vtkColorTransferFunction>::New();
colorTransferFunction->AddRGBPoint(0.0, 0.0, 0.5, 0.0);
colorTransferFunction->AddRGBPoint(60.0, 1.0, 0.0, 0.0);
colorTransferFunction->AddRGBPoint(128.0, 0.2, 0.1, 0.9);
colorTransferFunction->AddRGBPoint(196.0, 0.27, 0.21, 0.1);
colorTransferFunction->AddRGBPoint(255.0, 0.8, 0.8, 0.8);
vtkSmartPointer<vtkVolumeProperty> volumeProperty =
vtkSmartPointer<vtkVolumeProperty>::New();
//设定渲染属性
volumeProperty->SetColor(colorTransferFunction);//传输函数颜色
volumeProperty->SetScalarOpacity(opacityTransferFunction);//传输函数不透明度
volumeProperty->ShadeOn();//阴影
volumeProperty->SetInterpolationTypeToLinear();//函数插值方法
volumeProperty->SetAmbient(0.2);//环境光
volumeProperty->SetDiffuse(0.4);//漫反射
volumeProperty->SetSpecular(0.6);//高光系数
volumeProperty->SetSpecularPower(10); //高光强度
//定义渲染Mapper
//由于我使用的是vtk-8.2.0 所以vtkVolumeRayCastMapper已经被vtkSmartVolumeMapper取代了
vtkSmartPointer<vtkSmartVolumeMapper> volumeMapper =
vtkSmartPointer<vtkSmartVolumeMapper>::New();
volumeMapper->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkVolume> volume =
vtkSmartPointer<vtkVolume>::New();
//几何数据是vtkActor
//而体数据是vtVolume
volume->SetMapper(volumeMapper);
volume->SetProperty(volumeProperty);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
//体数据是AddVolume
renderer->AddVolume( volume );
renderer->SetBackground( 1.0, 1.0, 1.0 );
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer( renderer );
renWin->SetSize( 640, 480 );
renWin->Render();
renWin->SetWindowName("volumeRender");
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
iren->SetInteractorStyle(style);
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}