3D模型文件的导入导出

3D模型文件的导入导出

由多边形构成的3D模型文件的导入导出库,可以对应的类型如下。

  • OBJ文件导入导出
  • DXF文件导入导出(仅限3DFACE entities)
  • VRML文件导入导出(仅限IndexedFaceSet)
  • 3DS文件导入导出
  • STL文件导入导出
  • OFF文件导入导出

下载

源文件下载如下(一直在用的研究室的库,个人没做更改)。

filerx_model_v0.52.zip

使用例 

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 
#include "rx_model.h"
 
/*!
 * 读取OBJ文件
 * @param[in] 传递filename obj文件
 * @param[inout] poly 3D物体
 * @param[in] cen,ext 导入3D形状的正规化参数
 */
void ReadOBJ(const string filename, rxPolygons &polys, Vec3 cen, Vec3 ext)
{
    if(!polys.vertices.empty()){
        polys.vertices.clear();
        polys.normals.clear();
        polys.faces.clear();
        polys.materials.clear();
        polys.open = 0;
    }
    rxOBJ obj;
    if(obj.Read(filename, polys.vertices, polys.normals, polys.faces, polys.materials, true)){
        cout << filename << " have been read." << endl;
 
        if(polys.normals.empty()){
            CalVertexNormals(polys);
        }
 
        cout << " the number of vertex   : " << polys.vertices.size() << endl;
        cout << " the number of normal   : " << polys.normals.size() << endl;
        cout << " the number of polygon  : " << polys.faces.size() << endl;
        cout << " the number of material : " << polys.materials.size() << endl;
 
        FitVertices(cen, ext, polys.vertices);
 
        // 读取贴图
        if(!polys.materials.empty()){
            rxMTL::iterator iter = polys.materials.begin();
            for(; iter != polys.materials.end(); ++iter){
                if(iter->second.tex_file.empty()) continue;
 
                cout << iter->first << " : " << iter->second.tex_file;
                LoadGLTexture(iter->second.tex_file, iter->second.tex_name, true, false);
 
                cout << " : " << iter->second.tex_name << endl;
            }
        }
 
        polys.open = 1;
    }
}
 
/*!
 * 导出OBJ文件
 * @param[in] 传递给filename obj文件
 * @param[inout] poly 3D物体
 */
void SaveOBJ(const string filename, rxPolygons &polys)
{
    rxOBJ obj;
    if(obj.Save(filename, polys.vertices, polys.normals, polys.faces, polys.materials)){
        cout << filename << " have been saved." << endl;
    }
}

你可能感兴趣的:(OpenGL)