三维模型obj文件的格式解析与读取-程序员宅基地

技术标签: python  运维  c/c++  

请先看这两个中文博客中对于obj的介绍:

读取Obj格式的模型文件(Dx10)

C++读入obj格式模型文件

更为详细的英文资料(用google或者aol搜索 "obj format"即可得到):

http://en.wikipedia.org/wiki/Wavefront_.obj_file

Wavefront OBJ File Format Summary

最详细的资料 obj spec: http://www.martinreddy.net/gfx/3d/OBJ.spec

http://people.cs.clemson.edu/~dhouse/courses/405/docs/brief-obj-file-format.html

http://www.scratchapixel.com/lessons/3d-advanced-lessons/obj-file-format/obj-file-format/

 

看完以上随便一个内容,obj的格式可以说了解了。下面实现对obj文件的读取。

 

 .obj文件中,每一行都有表明该行意义的标志符。对obj的读取中,处理以下标志符

"v"--点的坐标,三维模型为x, y, z的顺序;程序中以

1 typedef struct ObjVector3
2 {
3     ObjFloat x;
4     ObjFloat y;
5     ObjFloat z;
6 } ObjVector3;

结构存储。

"vt"--纹理坐标,程序中以

1 typedef struct ObjVector2
2 {
3     ObjFloat x;
4     ObjFloat y;
5 } ObjVector2;

结构存储

"vn"--法向量坐标,程序中与“v”的存储结构相同

"f"--面所用到的点坐标/纹理坐标/法向量坐标的索引,

"mtllib"--.obj文件用到的material库文件,“usemtl”标志符用到的material都是从material库文件中取出的

"g"--组group的名称,group里面的"f"可以使用0-N个"usemtl"对面的显示渲染进行控制,当使用了大于1个“usemtl”标志符,程序处理时对于已经读取的"f"很难控制;同时查看obj读取的源码,有的用到了这个标志符,有的没有使用该标志,有的使用了"usemtl"标志符对所读取的"f"面进行分割,本文的处理是使用"usemtl"标志符将"f"面分割为mesh,但是考虑到不同的group可以使用相同的"usemtl”标志(即不同的group都使用了 usemtl AAA),因此将"g"与"usemtl"结合起来,二者的名称作为mesh的名称

"usemtl"--参见"mtllib","g"。一旦使用了该标志符,则在该标志符后面的"f"全部受影响,直到遇到下一个"usemtl"

mesh结构,解析已经读取的“f”存储所面的点坐标/纹理坐标/法向量坐标,其结构为

 1 struct Mesh
 2 {
 3     string name;        // name
 4     ObjIntd mtl_idx;    // the index of material used by mesh 
 5 
 6     vector<ObjVector3> positions;    // "v" flag
 7     vector<ObjVector2> texcoords;    // "vt" flag
 8     vector<ObjVector3> normals;        // "vn" flag
 9     vector<ObjDword> indices;    // the indices of points of face in positions
10 
11     void reset()
12     { 
13         mtl_idx = -1;
14 
15         name.clear();
16         positions.clear();
17         texcoords.clear();
18         normals.clear();
19         indices.clear();
20     }
21 };
View Code

结构中出现的

ObjFloat ObjIntd

是一组typedef,具体为

 1 typedef int32_t ObjBool;
 2 typedef uint8_t ObjByte;
 3 typedef uint16_t ObjWord;
 4 typedef uint32_t ObjDword;
 5 typedef int8_t ObjIntb;
 6 typedef int16_t ObjIntw;
 7 typedef int32_t ObjIntd;
 8 
 9 typedef float ObjFloat;
10 typedef double ObjDouble;
View Code

 对obj文件读取的主体程序如下

 1     ObjBool obj_file_load(const string& objname)
 2     {
 3         if (objname.empty())
 4             return LIBOBJ_FALSE;
 5 
 6         int mesh_count = 0;
 7         Mesh mesh_;
 8         string groupname;
 9         string usemtl;
10         vector<ObjVector3> positions;
11         vector<ObjVector3> normals;
12         vector<ObjVector2> texcoords;
13         vector<vector<ObjVertexIndex> > faces;
14         map<string, ObjIntd> mtlMap;
15 
16         fstream obj_stream;    
17         obj_stream.open(objname.c_str(), std::ios_base::in);
18         if (!obj_stream.is_open())
19             return LIBOBJ_FALSE;
20 
21         string obj_flag;
22         while (obj_stream.good())
23         {
24             obj_stream >> obj_flag;
25 
26             if (obj_flag.empty() || (obj_flag[0] == '#'))
27             {
28                 obj_stream.ignore(1024, '\n');            // skip line
29                 continue;
30             }
31 
32             if (obj_flag == "v")            // position flag
33                 if (!obj_parse_vector3(obj_stream, positions))
34                     return LIBOBJ_FALSE;
35             else if (obj_flag == "vt")    // texture coordinate flag
36                 if (!obj_parse_vector2(obj_stream, texcoords))
37                     return LIBOBJ_FALSE;
38             else if (obj_flag == "vn")    // normal flag
39                 if (!obj_parse_vector3(obj_stream, normals))
40                     return LIBOBJ_FALSE;
41             else if (obj_flag == "g")        //group name
42                 if (!obj_parse_group(obj_stream, groupname))
43                     return LIBOBJ_FALSE;
44             else if (obj_flag == "f")        // face    flag
45             {
46                 vector<ObjVertexIndex> face;
47 
48                 obj_parse_face(obj_stream, face, positions, texcoords, normals);
49 
50                 faces.push_back(face);
51             }
52             else if (obj_flag == "mtllib")    // Material library
53             {
54                 vector<string> materialnames;
55 
56                 obj_parse_mtllib(obj_stream, materialnames);
57 
58                 for (unsigned i = 0; i < materialnames.size(); i++)
59                     if (!mtl_file_load(get_file_path(objname) + '/' + materialnames[i]))
60                         return LIBOBJ_FALSE;
61             }
62             else if (obj_flag == "usemtl")
63             {
64                 if (obj_save_mesh(positions, texcoords, normals, faces, groupname+usemtl, mesh_))
65                 {
66                     m_meshes.back().mtl_idx = mtl_index(usemtl);
67                     
68                     faces.clear();
69                     mesh_.reset();
70                 }
71 
72                 obj_parse_group(obj_stream, usemtl);
73             }
74             else
75             {
76                 obj_stream.ignore(1024, '\n');            // skip line
77             }
78         }  
79 
80         if (!obj_stream.eof())
81             return LIBOBJ_FALSE;
82 
83         //save mesh data
84         if (obj_save_mesh(positions, texcoords, normals, faces, groupname+usemtl, mesh_))
85         {
86             m_meshes.back().mtl_idx = mtl_index(usemtl);
87 
88             faces.clear();
89             mesh_.reset();
90         }
91 
92         obj_stream.close();
93 
94         return LIBOBJ_TRUE;
95     }
View Code

 

obj文件用到了.mtl格式的material,在对“mtllib”标志符解析时开始读取该.mtl文件,

对于.mtl文件的介绍,请阅读

obj + mtl 格式

mtl文件格式

MTL文件格式分析

mtl文件的简要说明

更为详细的英文资料:

http://www.fileformat.info/format/material/

http://people.cs.clemson.edu/~dhouse/courses/405/docs/brief-mtl-file-format.html

本程序中存储mtl文件的结构较为简单,只使用了mtl文件中的几个结构而没有全部处理

 1 struct ObjMaterial
 2 {
 3     string name;
 4     
 5     ObjRgb ambient;
 6     ObjRgb diffuse;
 7     ObjRgb specular;
 8 
 9     ObjWord shininess;
10     ObjByte illumination_model;
11     ObjFloat transparency;
12 
13     string ambient_texture;
14     string diffuse_texture;
15     string specular_texture;
16 };
View Code

对mtl文件的解析代码:

 1     ObjBool mtl_file_load(const string& mtlname)
 2     {
 3         if (mtlname.empty())        // if obj has no material
 4             return LIBOBJ_FALSE;
 5 
 6         ObjMaterial* pmaterial = NULL;
 7 
 8         fstream mtlstream;
 9         mtlstream.open(mtlname.c_str(), std::ios_base::in);    //open strFileName file
10         if (!mtlstream.is_open())
11             return LIBOBJ_FALSE;
12         
13         string mtlflag;
14         while (mtlstream.good())
15         {
16             mtlstream >> mtlflag;
17 
18             if (mtlflag == "newmtl")
19             {
20                 string materialname;
21                 mtlstream >> materialname;
22                 if (mtl_index(materialname) != -1)
23                     continue;
24 
25                 pmaterial = new ObjMaterial;
26                 pmaterial->name = materialname;
27                 m_materials.push_back(pmaterial);
28             }
29             else if (mtlflag == "Ka")        //Ambient color
30                 if (!mtl_parse_color(mtlstream, pmaterial->ambient))
31                     return LIBOBJ_FALSE;
32             else if (mtlflag == "Kd")        //Diffuse color
33                 if (!mtl_parse_color(mtlstream, pmaterial->diffuse))
34                     return LIBOBJ_FALSE;
35             else if (mtlflag == "Ks")        //Specular color
36                 if (!mtl_parse_color(mtlstream, pmaterial->specular))
37                     return LIBOBJ_FALSE;
38             else if (mtlflag == "Tr"/*"d"*/)        // Alpha, that is transparent
39                 if (!mtl_parse_light(mtlstream, pmaterial->transparency))
40                     return LIBOBJ_FALSE;
41             else if (mtlflag == "Ns")        //Shininess
42                 if (!mtl_parse_light(mtlstream, pmaterial->shininess))
43                     return LIBOBJ_FALSE;
44             else if (mtlflag == "illum")        //Illumination type
45                 if (!mtl_parse_light(mtlstream, pmaterial->illumination_model))
46                     return LIBOBJ_FALSE;
47             else if (mtlflag == "map_Ka")    //ambient Texture
48                 if (!mtl_parse_texture(mtlstream, pmaterial->ambient_texture))
49                     return LIBOBJ_FALSE;
50             else if (mtlflag == "map_Kd")    //diffuse Texture
51                 if (!mtl_parse_texture(mtlstream, pmaterial->diffuse_texture))
52                     return LIBOBJ_FALSE;
53             else if (mtlflag == "map_Ks")    //specular Texture
54                 if (!mtl_parse_texture(mtlstream, pmaterial->specular_texture))
55                     return LIBOBJ_FALSE;
56             else
57                 mtlstream.ignore(1024, '\n');    // ignore this line, on the assumption that the max characters at this line is 1024.                
58         }
59 
60         if (!mtlstream.eof())
61             return LIBOBJ_FALSE;
62 
63         mtlstream.close();
64         return LIBOBJ_TRUE;
65     }
View Code

完整的代码放在了https://github.com/priseup/obj_load

ps: 手上没有数据,还没有进行测试

转载于:https://www.cnblogs.com/Taiwantomzhang/p/3993703.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_34335458/article/details/93371984

智能推荐

使用nginx解决浏览器跨域问题_nginx不停的xhr-程序员宅基地

文章浏览阅读1k次。通过使用ajax方法跨域请求是浏览器所不允许的,浏览器出于安全考虑是禁止的。警告信息如下:不过jQuery对跨域问题也有解决方案,使用jsonp的方式解决,方法如下:$.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty..._nginx不停的xhr

在 Oracle 中配置 extproc 以访问 ST_Geometry-程序员宅基地

文章浏览阅读2k次。关于在 Oracle 中配置 extproc 以访问 ST_Geometry,也就是我们所说的 使用空间SQL 的方法,官方文档链接如下。http://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/gdbs-in-oracle/configure-oracle-extproc.htm其实简单总结一下,主要就分为以下几个步骤。..._extproc

Linux C++ gbk转为utf-8_linux c++ gbk->utf8-程序员宅基地

文章浏览阅读1.5w次。linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombsmbstowcs将多字节编码转换为宽字节编码wcstombs将宽字节编码转换为多字节编码这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。linux下输入命名locale -a查看系统支持的编码_linux c++ gbk->utf8

IMP-00009: 导出文件异常结束-程序员宅基地

文章浏览阅读750次。今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到“ IMP-00009:导出文件异常结束” 错误,google一下,发现可能有如下原因导致imp的数据太大,没有写buffer和commit两个数据库字符集不同从低版本exp的dmp文件,向高版本imp导出的dmp文件出错传输dmp文件时,文件损坏解决办法:imp时指定..._imp-00009导出文件异常结束

python程序员需要深入掌握的技能_Python用数据说明程序员需要掌握的技能-程序员宅基地

文章浏览阅读143次。当下是一个大数据的时代,各个行业都离不开数据的支持。因此,网络爬虫就应运而生。网络爬虫当下最为火热的是Python,Python开发爬虫相对简单,而且功能库相当完善,力压众多开发语言。本次教程我们爬取前程无忧的招聘信息来分析Python程序员需要掌握那些编程技术。首先在谷歌浏览器打开前程无忧的首页,按F12打开浏览器的开发者工具。浏览器开发者工具是用于捕捉网站的请求信息,通过分析请求信息可以了解请..._初级python程序员能力要求

Spring @Service生成bean名称的规则(当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致)_@service beanname-程序员宅基地

文章浏览阅读7.6k次,点赞2次,收藏6次。@Service标注的bean,类名:ABDemoService查看源码后发现,原来是经过一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String C..._@service beanname

随便推点

二叉树的各种创建方法_二叉树的建立-程序员宅基地

文章浏览阅读6.9w次,点赞73次,收藏463次。1.前序创建#include&lt;stdio.h&gt;#include&lt;string.h&gt;#include&lt;stdlib.h&gt;#include&lt;malloc.h&gt;#include&lt;iostream&gt;#include&lt;stack&gt;#include&lt;queue&gt;using namespace std;typed_二叉树的建立

解决asp.net导出excel时中文文件名乱码_asp.net utf8 导出中文字符乱码-程序员宅基地

文章浏览阅读7.1k次。在Asp.net上使用Excel导出功能,如果文件名出现中文,便会以乱码视之。 解决方法: fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);_asp.net utf8 导出中文字符乱码

笔记-编译原理-实验一-词法分析器设计_对pl/0作以下修改扩充。增加单词-程序员宅基地

文章浏览阅读2.1k次,点赞4次,收藏23次。第一次实验 词法分析实验报告设计思想词法分析的主要任务是根据文法的词汇表以及对应约定的编码进行一定的识别,找出文件中所有的合法的单词,并给出一定的信息作为最后的结果,用于后续语法分析程序的使用;本实验针对 PL/0 语言 的文法、词汇表编写一个词法分析程序,对于每个单词根据词汇表输出: (单词种类, 单词的值) 二元对。词汇表:种别编码单词符号助记符0beginb..._对pl/0作以下修改扩充。增加单词

android adb shell 权限,android adb shell权限被拒绝-程序员宅基地

文章浏览阅读773次。我在使用adb.exe时遇到了麻烦.我想使用与bash相同的adb.exe shell提示符,所以我决定更改默认的bash二进制文件(当然二进制文件是交叉编译的,一切都很完美)更改bash二进制文件遵循以下顺序> adb remount> adb push bash / system / bin /> adb shell> cd / system / bin> chm..._adb shell mv 权限

投影仪-相机标定_相机-投影仪标定-程序员宅基地

文章浏览阅读6.8k次,点赞12次,收藏125次。1. 单目相机标定引言相机标定已经研究多年,标定的算法可以分为基于摄影测量的标定和自标定。其中,应用最为广泛的还是张正友标定法。这是一种简单灵活、高鲁棒性、低成本的相机标定算法。仅需要一台相机和一块平面标定板构建相机标定系统,在标定过程中,相机拍摄多个角度下(至少两个角度,推荐10~20个角度)的标定板图像(相机和标定板都可以移动),即可对相机的内外参数进行标定。下面介绍张氏标定法(以下也这么称呼)的原理。原理相机模型和单应矩阵相机标定,就是对相机的内外参数进行计算的过程,从而得到物体到图像的投影_相机-投影仪标定

Wayland架构、渲染、硬件支持-程序员宅基地

文章浏览阅读2.2k次。文章目录Wayland 架构Wayland 渲染Wayland的 硬件支持简 述: 翻译一篇关于和 wayland 有关的技术文章, 其英文标题为Wayland Architecture .Wayland 架构若是想要更好的理解 Wayland 架构及其与 X (X11 or X Window System) 结构;一种很好的方法是将事件从输入设备就开始跟踪, 查看期间所有的屏幕上出现的变化。这就是我们现在对 X 的理解。 内核是从一个输入设备中获取一个事件,并通过 evdev 输入_wayland

推荐文章

热门文章

相关标签