三维模型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

智能推荐

while循环&CPU占用率高问题深入分析与解决方案_main函数使用while(1)循环cpu占用99-程序员宅基地

文章浏览阅读3.8k次,点赞9次,收藏28次。直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方。使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果,但是一旦调用,CPU占用就直接100%(部署环境是win server服务器)。因此查看了下相关的老代码并使用JProfiler查看发现是在某个while循环的时候有问题。具体项目代码就不贴了,类似于下面这段代码。​​​​​​while(flag) {//your code;}这里的flag._main函数使用while(1)循环cpu占用99

【无标题】jetbrains idea shift f6不生效_idea shift +f6快捷键不生效-程序员宅基地

文章浏览阅读347次。idea shift f6 快捷键无效_idea shift +f6快捷键不生效

node.js学习笔记之Node中的核心模块_node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是-程序员宅基地

文章浏览阅读135次。Ecmacript 中没有DOM 和 BOM核心模块Node为JavaScript提供了很多服务器级别,这些API绝大多数都被包装到了一个具名和核心模块中了,例如文件操作的 fs 核心模块 ,http服务构建的http 模块 path 路径操作模块 os 操作系统信息模块// 用来获取机器信息的var os = require('os')// 用来操作路径的var path = require('path')// 获取当前机器的 CPU 信息console.log(os.cpus._node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是

数学建模【SPSS 下载-安装、方差分析与回归分析的SPSS实现(软件概述、方差分析、回归分析)】_化工数学模型数据回归软件-程序员宅基地

文章浏览阅读10w+次,点赞435次,收藏3.4k次。SPSS 22 下载安装过程7.6 方差分析与回归分析的SPSS实现7.6.1 SPSS软件概述1 SPSS版本与安装2 SPSS界面3 SPSS特点4 SPSS数据7.6.2 SPSS与方差分析1 单因素方差分析2 双因素方差分析7.6.3 SPSS与回归分析SPSS回归分析过程牙膏价格问题的回归分析_化工数学模型数据回归软件

利用hutool实现邮件发送功能_hutool发送邮件-程序员宅基地

文章浏览阅读7.5k次。如何利用hutool工具包实现邮件发送功能呢?1、首先引入hutool依赖<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.19</version></dependency>2、编写邮件发送工具类package com.pc.c..._hutool发送邮件

docker安装elasticsearch,elasticsearch-head,kibana,ik分词器_docker安装kibana连接elasticsearch并且elasticsearch有密码-程序员宅基地

文章浏览阅读867次,点赞2次,收藏2次。docker安装elasticsearch,elasticsearch-head,kibana,ik分词器安装方式基本有两种,一种是pull的方式,一种是Dockerfile的方式,由于pull的方式pull下来后还需配置许多东西且不便于复用,个人比较喜欢使用Dockerfile的方式所有docker支持的镜像基本都在https://hub.docker.com/docker的官网上能找到合..._docker安装kibana连接elasticsearch并且elasticsearch有密码

随便推点

Python 攻克移动开发失败!_beeware-程序员宅基地

文章浏览阅读1.3w次,点赞57次,收藏92次。整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)近年来,随着机器学习的兴起,有一门编程语言逐渐变得火热——Python。得益于其针对机器学习提供了大量开源框架和第三方模块,内置..._beeware

Swift4.0_Timer 的基本使用_swift timer 暂停-程序员宅基地

文章浏览阅读7.9k次。//// ViewController.swift// Day_10_Timer//// Created by dongqiangfei on 2018/10/15.// Copyright 2018年 飞飞. All rights reserved.//import UIKitclass ViewController: UIViewController { ..._swift timer 暂停

元素三大等待-程序员宅基地

文章浏览阅读986次,点赞2次,收藏2次。1.硬性等待让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作线程休眠,强制等待 Thread.sleep(long mills)package com.example.demo;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.firefox.Firefox.._元素三大等待

Java软件工程师职位分析_java岗位分析-程序员宅基地

文章浏览阅读3k次,点赞4次,收藏14次。Java软件工程师职位分析_java岗位分析

Java:Unreachable code的解决方法_java unreachable code-程序员宅基地

文章浏览阅读2k次。Java:Unreachable code的解决方法_java unreachable code

标签data-*自定义属性值和根据data属性值查找对应标签_如何根据data-*属性获取对应的标签对象-程序员宅基地

文章浏览阅读1w次。1、html中设置标签data-*的值 标题 11111 222222、点击获取当前标签的data-url的值$('dd').on('click', function() { var urlVal = $(this).data('ur_如何根据data-*属性获取对应的标签对象

推荐文章

热门文章

相关标签