From 7fc9b0fe97ab1b814c0ac0b0f937652f13b188f2 Mon Sep 17 00:00:00 2001 From: Merlyn Morgan-Graham Date: Sun, 2 Oct 2016 00:52:45 -0700 Subject: [PATCH] Add stream based material reader implementation --- tiny_obj_loader.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tiny_obj_loader.h b/tiny_obj_loader.h index a7ed6ab..228e819 100644 --- a/tiny_obj_loader.h +++ b/tiny_obj_loader.h @@ -179,6 +179,19 @@ class MaterialFileReader : public MaterialReader { std::string m_mtlBasePath; }; +class MaterialStreamReader : public MaterialReader { + public: + explicit MaterialStreamReader(std::istream &inStream) + : m_inStream(inStream) {} + virtual ~MaterialStreamReader() {} + virtual bool operator()(const std::string &matId, + std::vector *materials, + std::map *matMap, std::string *err); + + private: + std::istream &m_inStream; +}; + /// Loads .obj from a file. /// 'attrib', 'shapes' and 'materials' will be filled with parsed shape data /// 'shapes' will be filled with parsed shape data @@ -1010,6 +1023,22 @@ bool MaterialFileReader::operator()(const std::string &matId, return true; } +bool MaterialStreamReader::operator()(const std::string &matId, + std::vector *materials, + std::map *matMap, + std::string *err) { + LoadMtl(matMap, materials, &m_inStream); + if (!m_inStream) { + std::stringstream ss; + ss << "WARN: Material stream in error state." + << " Created a default material."; + if (err) { + (*err) += ss.str(); + } + } + return true; +} + bool LoadObj(attrib_t *attrib, std::vector *shapes, std::vector *materials, std::string *err, const char *filename, const char *mtl_basepath,