OBJLoader.h
Go to the documentation of this file.
1 #ifndef B_OBJ_LOADER_H
2 #define B_OBJ_LOADER_H
3 
4 #include <unordered_map>
5 #include <boost/bind.hpp>
6 
7 #include "GeometryData.h"
8 #include "ModelData.h"
9 
10 #include "vmmlib/matrix.hpp"
11 #include "external/obj_parser/obj_parser.hpp"
12 
16 class OBJLoader
17 {
18 public:
19  /* Typedefs */
20  typedef std::unordered_map< std::string, MaterialData > MaterialMap;
21 
22  /* Structs */
23  struct FaceData
24  {
28  vmml::Vector3f normal;
29  vmml::Vector3f tangent;
30  vmml::Vector3f bitangent;
31  };
32 
33  struct VertexData
34  {
35  std::vector<Index> faces;
36  vmml::Vector3f position;
37  vmml::Vector3f normal;
38  vmml::Vector3f tangent;
39  vmml::Vector3f bitangent;
40 
41  GLfloat x() { return position.x(); }
42  GLfloat y() { return position.y(); }
43  GLfloat z() { return position.z(); }
44 
45  VertexData(const vmml::Vector3f &position)
46  : position(position)
47  {}
48  };
49 
50  /* Functions */
51 
55  OBJLoader(obj::obj_parser::flags_type flags)
56  : _flags(flags)
57  {}
58 
63  OBJLoader(ModelData *data, obj::obj_parser::flags_type flags)
64  : _data(data)
65  , _flags(flags)
66  {}
67 
70  virtual ~OBJLoader() {}
71 
75  bool load(std::istream& istream);
76 
81  static MaterialData loadMaterial(const std::string &fileName, const std::string &materialName);
82 
85  void createFaceNormals();
86 
89  void createVertexNormals();
90 
93  ModelData::GroupMap getData() { return _groups; }
94 
95 private:
96 
97  /* Functions */
98 
99  void createGroup(const std::string& name)
100  {
101  _groupName = name;
102  auto &group = _groups[_groupName];
103  if (!group)
104  {
105  group = GeometryDataPtr(new GeometryData);
106  }
107  _group = group;
108  }
109 
110  void info_callback(const std::string& filename, std::size_t line_number, const std::string& message);
111  void warning_callback(const std::string& filename, std::size_t line_number, const std::string& message);
112  void error_callback(const std::string& filename, std::size_t line_number, const std::string& message);
113  void geometric_vertex_callback(obj::float_type x, obj::float_type y, obj::float_type z);
114  void texture_vertex_callback(obj::float_type u, obj::float_type v);
115  void vertex_normal_callback(obj::float_type x, obj::float_type y, obj::float_type z);
116  void triangular_face_geometric_vertices_callback(obj::index_type v1, obj::index_type v2, obj::index_type v3);
117  void triangular_face_geometric_vertices_texture_vertices_callback(const obj::index_2_tuple_type& v1_vt1, const obj::index_2_tuple_type& v2_vt2, const obj::index_2_tuple_type& v3_vt3);
118  void triangular_face_geometric_vertices_vertex_normals_callback(const obj::index_2_tuple_type& v1_vn1, const obj::index_2_tuple_type& v2_vn2, const obj::index_2_tuple_type& v3_vn3);
119  void triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback(const obj::index_3_tuple_type& v1_vt1_vn1, const obj::index_3_tuple_type& v2_vt2_vn2, const obj::index_3_tuple_type& v3_vt3_vn3);
120  void polygonal_face_geometric_vertices_begin_callback(obj::index_type v1, obj::index_type v2, obj::index_type v3);
121  void polygonal_face_geometric_vertices_vertex_callback(obj::index_type v);
122  void polygonal_face_geometric_vertices_end_callback();
123  void polygonal_face_geometric_vertices_texture_vertices_begin_callback(const obj::index_2_tuple_type& v1_vt1, const obj::index_2_tuple_type& v2_vt2, const obj::index_2_tuple_type& v3_vt3);
124  void polygonal_face_geometric_vertices_texture_vertices_vertex_callback(const obj::index_2_tuple_type& v_vt);
125  void polygonal_face_geometric_vertices_texture_vertices_end_callback();
126  void polygonal_face_geometric_vertices_vertex_normals_begin_callback(const obj::index_2_tuple_type& v1_vn1, const obj::index_2_tuple_type& v2_vn2, const obj::index_2_tuple_type& v3_vn3);
127  void polygonal_face_geometric_vertices_vertex_normals_vertex_callback(const obj::index_2_tuple_type& v_vn);
128  void polygonal_face_geometric_vertices_vertex_normals_end_callback();
129  void polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback(const obj::index_3_tuple_type& v1_vt1_vn1, const obj::index_3_tuple_type& v2_vt2_vn2, const obj::index_3_tuple_type& v3_vt3_vn3);
130  void polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback(const obj::index_3_tuple_type& v_vt_vn);
131  void polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback();
132  void group_name_callback(const std::string& group_name);
133  void smoothing_group_callback(obj::size_type group_number);
134  void object_name_callback(const std::string& object_name);
135  void material_library_callback(const std::string& filename);
136  void material_name_callback(const std::string& material_name);
137  void comment_callback(const std::string& comment);
138 
139  template< bool POSITION, bool TEX_COORD, bool NORMAL >
140  void genVertex(const IndexData &d);
141 
142  template< bool NORMAL >
143  void genFace(const IndexData &d1, const IndexData &d2, const IndexData &d3);
144 
150  static void loadObjMtl(const std::string &fileName, MaterialMap &materials, const std::string &materialName = "");
151 
152  /* Variables */
153 
154  obj::obj_parser::flags_type _flags;
155  ModelData *_data = nullptr;
156  MaterialMap _materials;
157  std::string _groupName;
158  ModelData::GroupMap _groups;
159  GeometryDataPtr _group = nullptr;
160 
161  std::vector<FaceData> _faces;
162  std::vector<VertexData> _vertices;
163  std::vector<vmml::Vector2f> _texCoords;
164  std::vector<vmml::Vector3f> _normals;
165  std::vector<vmml::Vector3f> _tangents;
166  std::vector<vmml::Vector3f> _bitangents;
167 };
168 
169 
170 #endif /* defined(B_OBJ_LOADER_H) */
vmml::Vector3f bitangent
Definition: OBJLoader.h:30
vmml::Vector3f position
Definition: OBJLoader.h:36
Index v2
Definition: OBJLoader.h:26
Index v3
Definition: OBJLoader.h:27
OBJLoader(obj::obj_parser::flags_type flags)
Constructor.
Definition: OBJLoader.h:55
GLfloat x()
Definition: OBJLoader.h:41
bool load(std::istream &istream)
Loads the model data from an input stream.
Definition: OBJLoader.cpp:282
vmml::Vector3f normal
Definition: OBJLoader.h:37
void createVertexNormals()
Creates the normal vectors of the vertex.
Definition: OBJLoader.cpp:529
std::vector< Index > faces
Definition: OBJLoader.h:35
Definition: OBJLoader.h:23
Definition: GeometryData.h:10
OBJLoader(ModelData *data, obj::obj_parser::flags_type flags)
Constructor.
Definition: OBJLoader.h:63
vmml::Vector3f tangent
Definition: OBJLoader.h:38
std::shared_ptr< GeometryData > GeometryDataPtr
Definition: GeometryData.h:138
std::unordered_map< std::string, MaterialData > MaterialMap
Definition: OBJLoader.h:20
VertexData(const vmml::Vector3f &position)
Definition: OBJLoader.h:45
vmml::Vector3f bitangent
Definition: OBJLoader.h:39
virtual ~OBJLoader()
Virtual destructor.
Definition: OBJLoader.h:70
Definition: OBJLoader.h:33
vmml::Vector3f normal
Definition: OBJLoader.h:28
vmml::Vector3f tangent
Definition: OBJLoader.h:29
GLfloat y()
Definition: OBJLoader.h:42
The underlying data of a geometry object.
Definition: GeometryData.h:125
The underlying data of a material.
Definition: MaterialData.h:10
void createFaceNormals()
Creates the normal vectors of the face.
Definition: OBJLoader.cpp:476
GLfloat z()
Definition: OBJLoader.h:43
Index v1
Definition: OBJLoader.h:25
GLushort Index
Definition: GeometryData.h:120
ModelData::GroupMap getData()
Returns the geometry groups.
Definition: OBJLoader.h:93
Definition: ModelData.h:11
std::unordered_map< std::string, GeometryDataPtr > GroupMap
Definition: ModelData.h:15
Loads and processes OBJ models and materials.
Definition: OBJLoader.h:16
static MaterialData loadMaterial(const std::string &fileName, const std::string &materialName)
Loads an obj material.
Definition: OBJLoader.cpp:367