vtk读取3ds文件并显示c++版本
vtk提供了很方便的函数可以读取3ds文件,这种文件是3dmax软件制作的三维物件,之前的别人的文章写了python版本的读取和显示的代码,这里翻译成了c++版本的。
VTK imported and displayed in STL, 3DS file - Programmer Sought
VTK中导入并显示STL、3DS文件_weixin_34115824的博客-CSDN博客
官方也给了3ds文件的c++例子
https://kitware.github.io/vtk-examples/site/Cxx/IO/3DSImporter/
这次从爱给下载了3ds文件,Original VW Beetle 大众甲壳虫_3ds|max - 大小:1m-3d模型_3dMax|3ds|obj-免费下载-爱给网 (aigei.com)
具体的c++代码如下:
#include <vtk3DSImporter.h>
#include <vtkNew.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
/*
# This example demonstrates the use of vtk3DSImporter.
# vtk3DSImporter is used to load 3D Studio files.Unlike writers,
# importers can load scenes(data as well as lights, cameras, actors
# etc.).Importers will either generate an instance of vtkRenderWindow
#and /or vtkRenderer or will use the ones you specify.
*/
#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
int main(int argc, char* argv[]) {
if (argc != 2)
{
cout << "Required parameters: Filename" << endl;
return EXIT_FAILURE;
}
vtkNew<vtk3DSImporter> importer;
importer->SetFileName(argv[1]);
importer->ComputeNormalsOn();
importer->Read();
//# Here we let the importer create a renderer and a render window for
//# us.We could have also createand assigned those ourselves like so :
//# renWin = vtk.vtkRenderWindow()
//# importer.SetRenderWindow(renWin)
//# Assign an interactor.
//# We have to ask the importer for it's render window.
auto renWin = importer->GetRenderWindow();
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
//# Set the render window's size
renWin->SetSize(500, 500);
//# Set some properties on the renderer.
//# We have to ask the importer for it's renderer.
auto ren = importer->GetRenderer();
ren->SetBackground(0.1, 0.2, 0.4);
iren->Initialize();
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
显示效果如下