前言
最近跑實驗代碼需要找一些三維模型,但是網上的大部分都是obj格式的模型,off的模型比較少,因此就想把obj格式的模型轉換成off格式,這種工作對於程序元來說也不麻煩。而且OpenMesh庫裏面提供了很好的函數支持。
主要步驟
主要步驟只有三步如下非常簡單
一、下載OpenMesh庫
OpenMesh的官方下載地址http://www.openmesh.org/download/,如下按照VS的版本下載相應的庫
如上圖紅框所標註,不管你的電腦是64位的還是32位的都下載32位的比較好,我的電腦是64位的,下載的64位的跑出一堆bug,然後還是32位的跑出來的。
二、VS中配置OpenMesh庫
配置OpenMesh庫主要分爲兩個步驟:
步驟一:點擊下載的OpenMesh的exe文件進行安裝默認安裝地址是
C:\Program Files (x86)\OpenMesh 6.3
或者 C:\Program Files\OpenMesh 6.3
,記住安裝的地址在後面的VS配置中要用到。
步驟二:
點擊項目——》屬性——》VC++目錄。然後進行如下配置
上面紅色框框中是安裝的OpenMesh的目錄地址中的include包和lib包。
三、代碼
//OPENMESH
#define _USE_MATH_DEFINES
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#ifdef _DEBUG
#pragma comment(lib, "OpenMeshCored.lib")
#pragma comment(lib, "OpenMeshToolsd.lib")
#else
#pragma comment(lib, "OpenMeshCore.lib")
#pragma comment(lib, "OpenMeshTools.lib")
#endif // _DEBUG
typedef OpenMesh::TriMesh_ArrayKernelT<> MyTriMesh;
using namespace OpenMesh;
using namespace std;
int main(int argc, char** argv)
{
MyTriMesh mesh;
IO::Options ropt;
ropt += IO::Options::Binary;
ropt += IO::Options::Swap;
char filename[256];
for (int i = 0; i<1; i++) {
//Load Mesh with openmesh
sprintf_s(filename, "D:/論文代碼/models/person1.obj", i);
//要轉換的obj文件地址
if (!IO::read_mesh(mesh, filename, ropt))
{
std::cerr << "Error loading mesh from file " << filename << std::endl;
return false;
}
sprintf_s(filename, "D:/論文代碼/Geometry/person1.off", i);
//目的off文件地址
if (!OpenMesh::IO::write_mesh(mesh, filename))
{
std::cerr << "Cannot write mesh to file " << filename << std::endl;
}
else
cout << "successfully save mesh to " << filename << endl;
}
}