CCObjLoader.h 3.2 KB

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