diff --git a/WFObj.cc b/WFObj.cc index 184fcdd..60dd298 100644 --- a/WFObj.cc +++ b/WFObj.cc @@ -308,288 +308,3 @@ WFObj::VertexRef WFObj::readVertexRef(const std::string ref) } return fr; } - - - - -/****** WFMtl functions ******/ - -void WFObj::WFMtl::clear() -{ - m_data = map< string, vector< vector > >(); - m_currentMaterialName = ""; - m_attributesPushed = false; -} - -bool WFObj::WFMtl::load(const FileLoader::Path & path) -{ - clear(); - - FileLoader::Buffer buff = m_obj->m_fileLoader->load(path); - if (buff.size <= 0) - return false; - - m_path = path; - string str(buff.data, buff.size); - stringstream istr(str, ios_base::in); - load(istr, buff.size); - - return true; -} - -bool WFObj::WFMtl::load(std::istream & istr, unsigned int size) -{ - char buf[size+1]; - - string buildup; - while (istr.good()) - { - istr.getline(buf, size+1); - string input = trimString(buf); - int sz = input.size(); - if (sz == 0 || input[0] == '#') - continue; - if (input[sz-1] == '\\') - { - input[sz-1] = ' '; - buildup = input; - continue; - } - if (buildup != "") - input = buildup + input; - buildup = ""; - processInputLine(input); - } - return true; -} - -void WFObj::WFMtl::processInputLine(const std::string & input) -{ - string line = input; - vector lineParts; - for (;;) - { - string token = stripFirstToken(line); - if (token == "") - break; - lineParts.push_back(token); - } - if (lineParts.size() > 0) - { - if ( (lineParts.size() >= 2) && (lineParts[0] == "newmtl") ) - { - m_currentMaterialName = lineParts[1]; - } - else if (m_currentMaterialName != "") - { - m_data[m_currentMaterialName].push_back(lineParts); - } - } -} - -void WFObj::WFMtl::renderBegin(const string & mtlname, bool doTextureInfo, - bool enableBlending) -{ - map< string, vector< vector > >::iterator it = m_data.find(mtlname); - if (it == m_data.end()) - return; - vector< vector > & stmts = it->second; - int num_stmts = stmts.size(); - bool foundTexture = false; - bool didSomething = false; - checkGLError(); - for (int i = 0; i < num_stmts; i++) - { - string & type = stmts[i][0]; - if (type == "Ka") /* set ambient color */ - { - if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") ) - { - pushAttributes(); - float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f}; - for (int j = 0; j < 3; j++) - sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]); -#ifdef DEBUGGL - cout << " glMaterialfv(GL_FRONT, GL_AMBIENT, {"; - for (int j = 0; j < 4; j++) - cout << mat[j] << (j < 3 ? ", " : "})"); - cout << endl; -#endif - glMaterialfv(GL_FRONT, GL_AMBIENT, mat); - didSomething = true; - } - } - else if (type == "Kd") /* set diffuse color */ - { - if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") ) - { - pushAttributes(); - float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f}; - for (int j = 0; j < 3; j++) - sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]); -#ifdef DEBUGGL - cout << " glMaterialfv(GL_FRONT, GL_DIFFUSE, {"; - for (int j = 0; j < 4; j++) - cout << mat[j] << (j < 3 ? ", " : "})"); - cout << endl; -#endif - glMaterialfv(GL_FRONT, GL_DIFFUSE, mat); - didSomething = true; - } - } - else if (type == "Ks") /* set specular color */ - { - if ( (stmts[i].size() == 4) && (stmts[i][1] != "spectral") ) - { - pushAttributes(); - float mat[4] = {0.0f, 0.0f, 0.0f, 1.0f}; - for (int j = 0; j < 3; j++) - sscanf(stmts[i][j+1].c_str(), "%f", &mat[j]); -#ifdef DEBUGGL - cout << " glMaterialfv(GL_FRONT, GL_SPECULAR, {"; - for (int j = 0; j < 4; j++) - cout << mat[j] << (j < 3 ? ", " : "})"); - cout << endl; -#endif - glMaterialfv(GL_FRONT, GL_SPECULAR, mat); - didSomething = true; - } - } - else if (type == "Ns") /* set shininess */ - { - if (stmts[i].size() == 2) - { - pushAttributes(); - GLfloat shininess = 0.0f; - sscanf(stmts[i][1].c_str(), "%f", &shininess); -#ifdef DEBUGGL - cout << " glMaterialf(GL_FRONT, GL_SHININESS, " - << shininess << ")" << endl; -#endif - glMaterialf(GL_FRONT, GL_SHININESS, shininess); - didSomething = true; - } - } - else if (type == "map_Kd") /* load a diffuse texture */ - { - if (doTextureInfo) - { - if (stmts[i].size() == 2) - { - if (m_obj->m_textureLoader != NULL) - { - 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) - { - pushAttributes(); /* jh 2009-11-16 */ -#ifdef DEBUGGL - cout << " glBindTexture(GL_TEXTURE_2D, " << tex << ")" << endl; -#endif - glBindTexture(GL_TEXTURE_2D, tex); - foundTexture = true; - didSomething = true; - } - } - } - } - } - } - - checkGLError(); - if (didSomething) - { - pushAttributes(); - if (doTextureInfo) - { - if (enableBlending) - { - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); - } - if (foundTexture) - { -#ifdef DEBUGGL - cout << " glEnable(GL_TEXTURE_2D)" << endl; -#endif - glEnable(GL_TEXTURE_2D); - } - else - { -#ifdef DEBUGGL - cout << " glDisable(GL_TEXTURE_2D)" << endl; -#endif - glDisable(GL_TEXTURE_2D); - } - } - } - checkGLError(); -} - -void WFObj::WFMtl::pushAttributes() -{ - if (m_attributesPushed) - return; - m_attributesPushed = true; -#ifdef DEBUGGL - cout << " glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT)" - << endl; -#endif - checkGLError(); - glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT); - checkGLError(); -} - -void WFObj::WFMtl::renderEnd(const string & mtlname, bool doTextureInfo, - bool enableBlending) -{ - map< string, vector< vector > >::iterator it = m_data.find(mtlname); - if (it == m_data.end()) - return; - if (m_attributesPushed) - { -#ifdef DEBUGGL - cout << " glPopAttrib()" << endl; -#endif - checkGLError(); - glPopAttrib(); - checkGLError(); - m_attributesPushed = false; - } -} - - -/****** 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 index 4ecf5d3..bfc0d74 100644 --- a/WFObj.h +++ b/WFObj.h @@ -21,33 +21,6 @@ public: size_t length; }; - class WFMtl - { - public: - WFMtl(WFObj * obj) { m_obj = obj; } - bool load(const FileLoader::Path & path); - void renderBegin(const std::string & mtlname, - bool doTextureInfo = true, - bool enableBlending = false); - void renderEnd(const std::string & mtlname, - bool doTextureInfo = true, - bool enableBlending = false); - - 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; - }; - enum { VERTEX, VERTEX_TEXTURE, VERTEX_NORMAL, VERTEX_TYPES }; /* constructors */