From 0ba39b3669fc0bce77cd2623ed12c90d34c24a46 Mon Sep 17 00:00:00 2001 From: josh Date: Mon, 28 Jan 2008 01:30:29 +0000 Subject: [PATCH] materials rendering colors, still need texture support git-svn-id: svn://anubis/misc/wfobj@23 bd8a9e45-a331-0410-811e-c64571078777 --- WFMtl.cc | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- WFMtl.hh | 4 ++- WFObj.cc | 1 + 3 files changed, 110 insertions(+), 3 deletions(-) diff --git a/WFMtl.cc b/WFMtl.cc index e77f27e..6c588bd 100644 --- a/WFMtl.cc +++ b/WFMtl.cc @@ -20,6 +20,7 @@ void WFMtl::clear() { m_data = map< string, vector< vector > >(); m_currentMaterialName = ""; + m_attributesPushed = false; } int WFMtl::filesize(const char * filename) @@ -77,6 +78,7 @@ bool WFMtl::load(const string & filename) } cout << endl; } + it++; } /* END DEBUG */ @@ -134,7 +136,103 @@ void WFMtl::renderBegin(const string & mtlname) map< string, vector< vector > >::iterator it = m_data.find(mtlname); if (it == m_data.end()) return; - /* TODO: draw the material at it->second */ + vector< vector > & stmts = it->second; + int num_stmts = stmts.size(); + bool foundTexture = false; + bool didSomething = false; + 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 == "map_Kd") /* load a diffuse texture */ + { + /* TODO: figure out how i want to load the texture */ + foundTexture = true; + didSomething = true; + } + } + if (didSomething) + { + pushAttributes(); + 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); + } + } +} + +void WFMtl::pushAttributes() +{ + if (m_attributesPushed) + return; + m_attributesPushed = true; +#ifdef DEBUGGL + cout << " glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT)" << endl; +#endif + glPushAttrib(GL_LIGHTING_BIT | GL_TEXTURE_BIT); } void WFMtl::renderEnd(const string & mtlname) @@ -142,6 +240,12 @@ void WFMtl::renderEnd(const string & mtlname) map< string, vector< vector > >::iterator it = m_data.find(mtlname); if (it == m_data.end()) return; - /* TODO: end the material at it->second */ + if (m_attributesPushed) + { +#ifdef DEBUGGL + cout << " glPopAttrib()" << endl; +#endif + glPopAttrib(); + } } diff --git a/WFMtl.hh b/WFMtl.hh index df58a13..281127a 100644 --- a/WFMtl.hh +++ b/WFMtl.hh @@ -18,8 +18,10 @@ private: int filesize(const char * filename); void processInputLine(const std::string & input); std::string stripFirstToken(std::string & input); + void pushAttributes(); /* variables */ std::map< std::string, std::vector< std::vector > > m_data; - string m_currentMaterialName; + std::string m_currentMaterialName; + bool m_attributesPushed; }; diff --git a/WFObj.cc b/WFObj.cc index 7e44756..f9ecc0a 100644 --- a/WFObj.cc +++ b/WFObj.cc @@ -10,6 +10,7 @@ #include #include #include "WFObj.hh" +#include "WFMtl.hh" using namespace std; #define WHITESPACE " \n\r\t\v"