#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkOBJReader.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkTexture.h>
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkLight.h>
#include <vtkJPEGReader.h>
#include <vtkProperty.h> // 添加这行,确保包含 vtkProperty 的定义
int main(int argc, char* argv[]) {
// 创建OBJ读取器
vtkSmartPointer<vtkOBJReader> objReader = vtkSmartPointer<vtkOBJReader>::New();
objReader->SetFileName("D:\\mesh_2_img\\LowerJaw.obj");
objReader->Update();
// 创建纹理读取器(以JPEG为例)
vtkSmartPointer<vtkJPEGReader> jpegReader = vtkSmartPointer<vtkJPEGReader>::New();
jpegReader->SetFileName("D:\\mesh_2_img\\LowerJaw.jpg"); // 设置纹理图片的路径
jpegReader->Update();
// 创建纹理对象
vtkSmartPointer<vtkTexture> texture = vtkSmartPointer<vtkTexture>::New();
texture->SetInputConnection(jpegReader->GetOutputPort());
// 创建mapper和actor
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(objReader->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->SetTexture(texture); // 设置纹理
// 设置材质属性,使表面呈现清淡色彩
actor->GetProperty()->SetAmbient(0.9); // 环境光成分,值越高则表面颜色越清淡
actor->GetProperty()->SetDiffuse(0.3); // 漫反射成分,减少漫反射以降低颜色的鲜艳度
actor->GetProperty()->SetSpecular(0.1); // 镜面反射成分,稍微增加光泽
actor->GetProperty()->SetSpecularPower(5.0); // 镜面反射强度设置为较低的值
// 创建渲染器、窗口和交互器
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(1920, 1080);
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// 添加actor到渲染器
renderer->AddActor(actor);
添加光源以实现较柔和的光照效果
//vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New();
//light->SetPositional(true);
//light->SetPosition(10, 10, 10); // 设置光源位置
//light->SetFocalPoint(actor->GetPosition()); // 光源指向对象中心
//light->SetColor(1.0, 1.0, 1.0); // 设置光的颜色为白色
//light->SetIntensity(0.5); // 调整光源强度,较低强度使表面颜色更加柔和
//renderer->AddLight(light);
// 设置渲染背景颜色
renderer->SetBackground(0.9, 0.9, 0.9); // 背景色设置为浅色,更加突出清淡效果
// 开始渲染
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}