vtk加载模型obj\stl\gltf

.cpp文件

引入

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


//不添加报错,原因???
#include 
#include 
#include 
#include 
#include 
#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);

加载obj模型

//加载obj模型
vtkSmartPointer reader = vtkSmartPointer::New(); // 读取OBJ
reader->SetFileName("path/demo.obj");
reader->Update();
vtkSmartPointer  mapper = vtkSmartPointer::New();
mapper->SetInputConnection(reader->GetOutputPort());

vtkSmartPointer  actor = vtkSmartPointer::New();
actor->SetMapper(mapper);
renderer->AddActor(actor);

加载stl模型

//加载stl模型
vtkSmartPointer reader = vtkSmartPointer::New();	// 读取STL
reader->SetFileName("path/demo.stl");
reader->Update();	
vtkSmartPointer  mapper = vtkSmartPointer::New();
mapper->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer  actor = vtkSmartPointer::New();
actor->SetMapper(mapper); 
renderer->AddActor(actor);

加载gltf模型

//加载gltf模型
vtkSmartPointer importer = vtkSmartPointer::New();
importer->SetFileName("path/demo.gltf");
importer->Read();
vtkSmartPointer actors = importer->GetRenderer()->GetActors();
actors->InitTraversal();
for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i) {
	vtkActor* actor = actors->GetNextActor();
	//actor->GetProperty()->SetEdgeColor(255, 241, 0);	// 显示边框颜色
	//actor->GetProperty()->SetEdgeVisibility(1);			// 显示边框
	// 处理每个 actor
	renderer->AddActor(actor);
}

加载gltf文件,尝试加载glb文件时,部分文件报错,估计材质vtk不支持

vtkGLTFDocumentLoader (0000021F4E54CB20): glTF extension KHR_materials_pbrSpecularGlossiness is used in this model, but not supported by this loader. The extension will be ignored.

全部代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


#include 
#include 
#include 
#include 
#include 
#include 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);

int main()
{
	vtkSmartPointer renderer = vtkSmartPointer::New();

	vtkSmartPointer renderWindow = vtkSmartPointer::New();
	renderWindow->AddRenderer(renderer);
	//设置窗口尺寸
	renderWindow->SetSize(600, 600);
	//窗口title
	renderWindow->SetWindowName("vtkGLTFReader");
	///*开启全屏*/
	renderWindow->SetFullScreen(true);
	开启全屏
	 renderWindow->FullScreenOn();
	关闭全屏
	renderWindow->FullScreenOff();

	vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New();
	renderWindowInteractor->SetRenderWindow(renderWindow);

	vtkSmartPointer style = vtkSmartPointer::New();
	renderWindowInteractor->SetInteractorStyle(style);

	//加载gltf模型
    vtkSmartPointer importer = vtkSmartPointer::New();
	importer->SetFileName("path/demo.gltf");
	importer->Read();
	vtkSmartPointer actors = importer->GetRenderer()->GetActors();
	actors->InitTraversal();
	for (vtkIdType i = 0; i < actors->GetNumberOfItems(); ++i) {
		vtkActor* actor = actors->GetNextActor();
		//actor->GetProperty()->SetEdgeColor(255, 241, 0);	// 显示边框颜色
		//actor->GetProperty()->SetEdgeVisibility(1);			// 显示边框
		// 处理每个 actor
		renderer->AddActor(actor);
	}

	加载obj模型
	//vtkSmartPointer reader = vtkSmartPointer::New();	    // 读取OBJ
	//reader->SetFileName("path/demo.obj");
	//reader->Update();
	//vtkSmartPointer  mapper = vtkSmartPointer::New();
	//mapper->SetInputConnection(reader->GetOutputPort());

	//vtkSmartPointer  actor = vtkSmartPointer::New();
	//actor->SetMapper(mapper);
	//renderer->AddActor(actor);
	加载stl模型
	//vtkSmartPointer reader = vtkSmartPointer::New();	    // 读取STL
	//reader->SetFileName("path/demo.stl");
	//reader->Update();	
	//vtkSmartPointer  mapper = vtkSmartPointer::New();
	//mapper->SetInputConnection(reader->GetOutputPort());
	//vtkSmartPointer  actor = vtkSmartPointer::New();
	//actor->SetMapper(mapper); 
	//renderer->AddActor(actor);


    // 设置背景颜色
	renderer->SetBackground(0.1, 0.2, 0.4); 
	renderWindow->Render();
	renderWindowInteractor->Start();
	return 0;
}

你可能感兴趣的:(c++,开发语言)