diff --git a/WFObj.cc b/WFObj.cc index 9320fb9..708131c 100644 --- a/WFObj.cc +++ b/WFObj.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include "WFObj.hh" using namespace std; @@ -83,23 +84,54 @@ int WFObj::filesize(const char * filename) return st.st_size; } -bool WFObj::load(const string & filename, loadTextureFunc_t loadTexture) +bool WFObj::load(const string & filename, + loadTextureFunc_t loadTexture, + loadFileFunc_t loadFile) { clear(); - int size = filesize(filename.c_str()); - if (size < 1) - return false; + if (loadFile) + { + unsigned int size; + const char * filedat = (char *) loadFile(filename.c_str(), &size); + if (filedat && size > 0) + { + string str(filedat, size); + stringstream istr(str, ios_base::in); + load(istr, size); + } + else + return false; + } + else + { + int size = filesize(filename.c_str()); + if (size < 1) + return false; - ifstream ifs(filename.c_str()); - if (!ifs.is_open()) - return false; + ifstream ifs(filename.c_str()); + if (!ifs.is_open()) + return false; + + load(ifs, size); + + ifs.close(); + } + + m_loadTexture = loadTexture; + m_loadFile = loadFile; + m_fileName = filename; + return true; +} + +bool WFObj::load(std::istream & istr, unsigned int size) +{ char buf[size+1]; string buildup; - while (ifs.good()) + while (istr.good()) { - ifs.getline(buf, size+1); + istr.getline(buf, size+1); string input = trimString(buf); int sz = input.size(); if (sz == 0 || input[0] == '#') @@ -115,14 +147,9 @@ bool WFObj::load(const string & filename, loadTextureFunc_t loadTexture) buildup = ""; processInputLine(input); } - - ifs.close(); - m_loadTexture = loadTexture; - m_fileName = filename; return true; } - void WFObj::processInputLine(const std::string & input) { string line = input; @@ -284,7 +311,9 @@ GLuint WFObj::render() { if (m_data[i].size() >= 2) { - material.load(basePath(m_fileName) + m_data[i][1], m_loadTexture); + material.load(basePath(m_fileName) + m_data[i][1], + m_loadTexture, + m_loadFile); } } } @@ -370,23 +399,53 @@ int WFMtl::filesize(const char * filename) return st.st_size; } -bool WFMtl::load(const string & filename, WFObj::loadTextureFunc_t loadTexture) +bool WFMtl::load(const string & filename, + WFObj::loadTextureFunc_t loadTexture, + WFObj::loadFileFunc_t loadFile) { clear(); - int size = filesize(filename.c_str()); - if (size < 1) - return false; + if (loadFile) + { + unsigned int size; + const char * filedat = (char *) loadFile(filename.c_str(), &size); + if (filedat && size > 0) + { + string str(filedat, size); + stringstream istr(str, ios_base::in); + load(istr, size); + } + else + return false; + } + else + { + int size = filesize(filename.c_str()); + if (size < 1) + return false; - ifstream ifs(filename.c_str()); - if (!ifs.is_open()) - return false; + ifstream ifs(filename.c_str()); + if (!ifs.is_open()) + return false; + + load(ifs, size); + + ifs.close(); + } + + m_loadTexture = loadTexture; + m_fileName = filename; + return true; +} + +bool WFMtl::load(std::istream & istr, unsigned int size) +{ char buf[size+1]; string buildup; - while (ifs.good()) + while (istr.good()) { - ifs.getline(buf, size+1); + istr.getline(buf, size+1); string input = trimString(buf); int sz = input.size(); if (sz == 0 || input[0] == '#') @@ -402,10 +461,6 @@ bool WFMtl::load(const string & filename, WFObj::loadTextureFunc_t loadTexture) buildup = ""; processInputLine(input); } - - ifs.close(); - m_loadTexture = loadTexture; - m_fileName = filename; return true; } diff --git a/WFObj.hh b/WFObj.hh index 4adaa21..39dd915 100644 --- a/WFObj.hh +++ b/WFObj.hh @@ -8,7 +8,11 @@ class WFObj { public: typedef GLuint (*loadTextureFunc_t)(const char * texture); - bool load(const std::string & filename, loadTextureFunc_t loadTexture); + typedef void * (*loadFileFunc_t)(const char * filename, + unsigned int * length); + bool load(const std::string & filename, + loadTextureFunc_t loadTexture = NULL, + loadFileFunc_t loadFile = NULL); GLuint render(); const float * const getAABB() { return m_aabb; } @@ -31,10 +35,12 @@ protected: Vertex readVertex(const std::vector & parts); void parseVertexIndices(const std::string & vtxref, int * ret); void updateAABB(const float * const vertex); + bool load(std::istream & istr, unsigned int size); /* variables */ std::vector< std::vector > m_data; loadTextureFunc_t m_loadTexture; + loadFileFunc_t m_loadFile; std::string m_fileName; float m_aabb[6]; bool m_loadedVertex; @@ -44,7 +50,9 @@ protected: class WFMtl { public: - bool load(const std::string & filename, WFObj::loadTextureFunc_t loadTexture); + bool load(const std::string & filename, + WFObj::loadTextureFunc_t loadTexture = NULL, + WFObj::loadFileFunc_t loadFile = NULL); void renderBegin(const std::string & mtlname); void renderEnd(const std::string & mtlname); @@ -54,6 +62,7 @@ protected: int filesize(const char * filename); void processInputLine(const std::string & input); void pushAttributes(); + bool load(std::istream & istr, unsigned int size); /* variables */ std::map< std::string, std::vector< std::vector > > m_data;