1 //
2 // Copyright 2012-2015, Syoyo Fujita.
3 //
4 // Licensed under 2-clause BSD liecense.
5 //
6 #ifndef _TINY_OBJ_LOADER_H
7 #define _TINY_OBJ_LOADER_H
8 
9 #include <string>
10 #include <vector>
11 #include <map>
12 
13 namespace tinyobj {
14 
15 typedef struct {
16   std::string name;
17 
18   float ambient[3];
19   float diffuse[3];
20   float specular[3];
21   float transmittance[3];
22   float emission[3];
23   float shininess;
24   float ior;      // index of refraction
25   float dissolve; // 1 == opaque; 0 == fully transparent
26   // illumination model (see http://www.fileformat.info/format/material/)
27   int illum;
28 
29   std::string ambient_texname;            // map_Ka
30   std::string diffuse_texname;            // map_Kd
31   std::string specular_texname;           // map_Ks
32   std::string specular_highlight_texname; // map_Ns
33   std::string bump_texname;               // map_bump, bump
34   std::string displacement_texname;       // disp
35   std::string alpha_texname;              // map_d
36   std::map<std::string, std::string> unknown_parameter;
37 } material_t;
38 
39 typedef struct {
40   std::vector<float> positions;
41   std::vector<float> normals;
42   std::vector<float> texcoords;
43   std::vector<unsigned int> indices;
44   std::vector<int> material_ids; // per-mesh material ID
45 } mesh_t;
46 
47 typedef struct {
48   std::string name;
49   mesh_t mesh;
50 } shape_t;
51 
52 class MaterialReader {
53 public:
MaterialReader()54   MaterialReader() {}
~MaterialReader()55   virtual ~MaterialReader() {}
56 
57   virtual std::string operator()(const std::string &matId,
58                                  std::vector<material_t> &materials,
59                                  std::map<std::string, int> &matMap) = 0;
60 };
61 
62 class MaterialFileReader : public MaterialReader {
63 public:
MaterialFileReader(const std::string & mtl_basepath)64   MaterialFileReader(const std::string &mtl_basepath)
65       : m_mtlBasePath(mtl_basepath) {}
~MaterialFileReader()66   virtual ~MaterialFileReader() {}
67   virtual std::string operator()(const std::string &matId,
68                                  std::vector<material_t> &materials,
69                                  std::map<std::string, int> &matMap);
70 
71 private:
72   std::string m_mtlBasePath;
73 };
74 
75 /// Loads .obj from a file.
76 /// 'shapes' will be filled with parsed shape data
77 /// The function returns error string.
78 /// Returns empty string when loading .obj success.
79 /// 'mtl_basepath' is optional, and used for base path for .mtl file.
80 std::string LoadObj(std::vector<shape_t> &shapes,       // [output]
81                     std::vector<material_t> &materials, // [output]
82                     const char *filename, const char *mtl_basepath = NULL);
83 
84 /// Loads object from a std::istream, uses GetMtlIStreamFn to retrieve
85 /// std::istream for materials.
86 /// Returns empty string when loading .obj success.
87 std::string LoadObj(std::vector<shape_t> &shapes,       // [output]
88                     std::vector<material_t> &materials, // [output]
89                     std::istream &inStream, MaterialReader &readMatFn);
90 
91 /// Loads materials into std::map
92 /// Returns an empty string if successful
93 std::string LoadMtl(std::map<std::string, int> &material_map,
94                     std::vector<material_t> &materials, std::istream &inStream);
95 }
96 
97 #endif // _TINY_OBJ_LOADER_H
98