1.源码实现
#include <iostream>
#include <vector>
#include <stdexcept>
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
using namespace std;
typedef OpenMesh::PolyMesh_ArrayKernelT<> MyMesh;
int main()
{
MyMesh mesh;
MyMesh::VertexHandle vhandle[8];
vhandle[0] = mesh.add_vertex(MyMesh::Point(-1, -1, 1));
vhandle[1] = mesh.add_vertex(MyMesh::Point (1 , -1, 1));
vhandle[2] = mesh.add_vertex(MyMesh::Point (1 , 1 , 1));
vhandle[3] = mesh.add_vertex(MyMesh::Point(-1, 1 , 1));
vhandle[4] = mesh.add_vertex(MyMesh::Point(-1, -1, -1));
vhandle[5] = mesh.add_vertex(MyMesh::Point (1 , -1, -1));
vhandle[6] = mesh.add_vertex(MyMesh::Point (1 , 1 , -1));
vhandle[7] = mesh.add_vertex(MyMesh::Point(-1, 1 , -1));
//生成(四边形)表面
std::vector<MyMesh::VertexHandle> face_vhandles;
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[3]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[4]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[4]);
face_vhandles.push_back(vhandle[5]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[5]);
face_vhandles.push_back(vhandle[6]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[6]);
face_vhandles.push_back(vhandle[7]);
mesh.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[7]);
face_vhandles.push_back(vhandle[4]);
mesh.add_face(face_vhandles);
//导出output.obj格式文件
try
{
if(!OpenMesh::IO::write_mesh(mesh, "output.obj"))
{
std::cerr << "Cannot write mesh to file output.obj" << std::endl;
return 1;
}
}
catch(std::exception &x)
{
std::cerr << x.what() << std::endl;
return 1;
}
return 0;
}
2.编译源码
$ g++ -o test test.cpp -std=c++11 -lOpenMeshCore -Wl,-rpath=/usr/local/lib
3.运行程序
$ ./test
4.结果截图