diff --git a/WFObj.cc b/WFObj.cc index 3ffee28..112f489 100644 --- a/WFObj.cc +++ b/WFObj.cc @@ -1,6 +1,7 @@ #include #include +#include #include #include // isspace() #include // strlen() @@ -11,7 +12,7 @@ #include #include #include -#include "WFObj.hh" +#include "WFObj.h" using namespace std; #define WHITESPACE " \n\r\t\v" @@ -68,59 +69,70 @@ static string basePath(const string & str) return ""; } + /****** WFObj functions ******/ +WFObj::WFObj() +{ + init(); +} + +WFObj::WFObj(FileLoader & fileLoader) +{ + init(&fileLoader); +} + +WFObj::WFObj(TextureLoader & textureLoader) +{ + init(NULL, &textureLoader); +} + +WFObj::WFObj(FileLoader & fileLoader, TextureLoader & textureLoader) +{ + init(&fileLoader, &textureLoader); +} + +void WFObj::init (FileLoader * fileLoader, + TextureLoader * textureLoader) +{ + m_fileLoader = fileLoader; + if (m_fileLoader == NULL) + { + m_fileLoader = new WFFileLoader(); + m_iCreatedFileLoader = true; + } + else + { + m_iCreatedFileLoader = false; + } + m_textureLoader = textureLoader; +} + +WFObj::~WFObj() +{ + if (m_iCreatedFileLoader) + delete m_fileLoader; +} + void WFObj::clear() { m_data = std::vector< std::vector >(); m_loadedVertex = false; } -int WFObj::filesize(const char * filename) -{ - struct stat st; - if (stat(filename, &st)) - return -1; - return st.st_size; -} - -bool WFObj::load(const string & filename, - loadTextureFunc_t loadTexture, - loadFileFunc_t loadFile) +bool WFObj::load(const FileLoader::Path & path) { clear(); - 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; + FileLoader::Buffer buff = m_fileLoader->load(path); + if (buff.size <= 0) + return false; - ifstream ifs(filename.c_str()); - if (!ifs.is_open()) - return false; + m_path = path; + string str(buff.data, buff.size); + stringstream istr(str, ios_base::in); + load(istr, buff.size); - load(ifs, size); - - ifs.close(); - } - - m_loadTexture = loadTexture; - m_loadFile = loadFile; - m_fileName = filename; return true; } @@ -177,7 +189,7 @@ GLuint WFObj::render(bool doTextureInfo) bool inFace = false; bool inMaterial = false; string currentMaterialName; - WFMtl material; + WFMtl material(this); for (int i = 0; i < len; i++) { string type = m_data[i][0]; @@ -315,9 +327,10 @@ GLuint WFObj::render(bool doTextureInfo) { if (m_data[i].size() >= 2) { - material.load(basePath(m_fileName) + m_data[i][1], - m_loadTexture, - m_loadFile); + FileLoader::Path path( + basePath(m_path.fullPath) + m_data[i][1], + m_data[i][1]); + material.load(path); } } } @@ -388,61 +401,30 @@ void WFObj::parseVertexIndices(const string & vtxref, int * ret) /****** WFMtl functions ******/ -void WFMtl::clear() +void WFObj::WFMtl::clear() { m_data = map< string, vector< vector > >(); m_currentMaterialName = ""; m_attributesPushed = false; } -int WFMtl::filesize(const char * filename) -{ - struct stat st; - if (stat(filename, &st)) - return -1; - return st.st_size; -} - -bool WFMtl::load(const string & filename, - WFObj::loadTextureFunc_t loadTexture, - WFObj::loadFileFunc_t loadFile) +bool WFObj::WFMtl::load(const FileLoader::Path & path) { clear(); - 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; + FileLoader::Buffer buff = m_obj->m_fileLoader->load(path); + if (buff.size <= 0) + return false; - ifstream ifs(filename.c_str()); - if (!ifs.is_open()) - return false; + m_path = path; + string str(buff.data, buff.size); + stringstream istr(str, ios_base::in); + load(istr, buff.size); - load(ifs, size); - - ifs.close(); - } - - m_loadTexture = loadTexture; - m_fileName = filename; return true; } -bool WFMtl::load(std::istream & istr, unsigned int size) +bool WFObj::WFMtl::load(std::istream & istr, unsigned int size) { char buf[size+1]; @@ -468,7 +450,7 @@ bool WFMtl::load(std::istream & istr, unsigned int size) return true; } -void WFMtl::processInputLine(const std::string & input) +void WFObj::WFMtl::processInputLine(const std::string & input) { string line = input; vector lineParts; @@ -492,7 +474,7 @@ void WFMtl::processInputLine(const std::string & input) } } -void WFMtl::renderBegin(const string & mtlname, bool doTextureInfo) +void WFObj::WFMtl::renderBegin(const string & mtlname, bool doTextureInfo) { map< string, vector< vector > >::iterator it = m_data.find(mtlname); if (it == m_data.end()) @@ -577,13 +559,15 @@ void WFMtl::renderBegin(const string & mtlname, bool doTextureInfo) { if (doTextureInfo) { - /* TODO: figure out how i want to load the texture */ if (stmts[i].size() == 2) { - if (m_loadTexture != NULL) + if (m_obj->m_textureLoader != NULL) { - GLuint tex = m_loadTexture( (basePath(m_fileName) + - stmts[i][1]).c_str() ); + FileLoader::Path path( + basePath(m_path.fullPath + stmts[i][1]), + stmts[i][1]); + GLuint tex = m_obj->m_textureLoader->load(path, + *m_obj->m_fileLoader); if (tex > 0) { #ifdef DEBUGGL @@ -622,7 +606,7 @@ void WFMtl::renderBegin(const string & mtlname, bool doTextureInfo) } } -void WFMtl::pushAttributes() +void WFObj::WFMtl::pushAttributes() { if (m_attributesPushed) return; @@ -633,7 +617,7 @@ void WFMtl::pushAttributes() glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT); } -void WFMtl::renderEnd(const string & mtlname, bool doTextureInfo) +void WFObj::WFMtl::renderEnd(const string & mtlname, bool doTextureInfo) { map< string, vector< vector > >::iterator it = m_data.find(mtlname); if (it == m_data.end()) @@ -648,3 +632,34 @@ void WFMtl::renderEnd(const string & mtlname, bool doTextureInfo) } } + +/****** WFFileLoader functions ******/ + +int WFObj::WFFileLoader::getSize(const Path & path) +{ + struct stat st; + + if (path.fullPath == "") + return -1; + if (stat(path.fullPath.c_str(), &st)) + return -2; + return st.st_size; +} + +FileLoader::Buffer WFObj::WFFileLoader::load(const Path & path) +{ + int size = getSize(path); + if (size > 0) + { + int fd = open(path.fullPath.c_str(), O_RDONLY); + if (fd > 0) + { + Buffer buf(size); + int num_read = read(fd, buf.data, size); + close(fd); + if (num_read > 0) + return buf; + } + } + return Buffer(0); +} diff --git a/WFObj.h b/WFObj.h new file mode 100644 index 0000000..52e03ac --- /dev/null +++ b/WFObj.h @@ -0,0 +1,89 @@ + +#ifndef WFOBJ_H +#define WFOBJ_H + +#include "FileLoader/FileLoader.h" +#include "TextureLoader/TextureLoader.h" +#include +#include +#include +#include + +class WFObj +{ +public: + WFObj(); + WFObj(FileLoader & fileLoader); + WFObj(TextureLoader & textureLoader); + WFObj(FileLoader & fileLoader, TextureLoader & textureLoader); + ~WFObj(); + + class WFMtl + { + public: + WFMtl(WFObj * obj) { m_obj = obj; } + bool load(const FileLoader::Path & path); + void renderBegin(const std::string & mtlname, + bool doTextureInfo = true); + void renderEnd(const std::string & mtlname, + bool doTextureInfo = true); + + protected: + /* methods */ + void clear(); + 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; + std::string m_currentMaterialName; + bool m_attributesPushed; + FileLoader::Path m_path; + WFObj * m_obj; + }; + + class WFFileLoader : public FileLoader + { + public: + virtual int getSize(const Path & path); + virtual Buffer load(const Path & path); + }; + + bool load(const FileLoader::Path & path); + GLuint render(bool doTextureInfo = true); + const float * const getAABB() { return m_aabb; } + +protected: + /* types */ + class Vertex + { + public: + float operator[](int idx) const { return data[idx]; } + float & operator[](int idx) { return data[idx]; } + float * getData() { return data; } + private: + float data[4]; + }; + + /* methods */ + void init(FileLoader * fileLoader = NULL, + TextureLoader * textureLoader = NULL); + void clear(); + void processInputLine(const std::string & input); + 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; + FileLoader::Path m_path; + float m_aabb[6]; + bool m_loadedVertex; + FileLoader * m_fileLoader; + TextureLoader * m_textureLoader; + bool m_iCreatedFileLoader; +}; + +#endif diff --git a/WFObj.hh b/WFObj.hh deleted file mode 100644 index 7027c7f..0000000 --- a/WFObj.hh +++ /dev/null @@ -1,78 +0,0 @@ - -#ifndef WFOBJ_H -#define WFOBJ_H - -#include -#include -#include -#include - -class WFObj -{ -public: - typedef GLuint (*loadTextureFunc_t)(const char * texture); - 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(bool doTextureInfo = true); - const float * const getAABB() { return m_aabb; } - -protected: - /* types */ - class Vertex - { - public: - float operator[](int idx) const { return data[idx]; } - float & operator[](int idx) { return data[idx]; } - float * getData() { return data; } - private: - float data[4]; - }; - - /* methods */ - void clear(); - int filesize(const char * filename); - void processInputLine(const std::string & input); - 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; -}; - - -class WFMtl -{ -public: - bool load(const std::string & filename, - WFObj::loadTextureFunc_t loadTexture = NULL, - WFObj::loadFileFunc_t loadFile = NULL); - void renderBegin(const std::string & mtlname, bool doTextureInfo = true); - void renderEnd(const std::string & mtlname, bool doTextureInfo = true); - -protected: - /* methods */ - void clear(); - 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; - std::string m_currentMaterialName; - bool m_attributesPushed; - WFObj::loadTextureFunc_t m_loadTexture; - std::string m_fileName; -}; - -#endif