From 53d4bebbc2aa1c258db712bab7e68bd9ea02f3a2 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 24 Apr 2011 23:28:53 -0400 Subject: [PATCH] add draw() and renderMaterial() --- WFObj.cc | 33 +++++++++++++++++++++++++++++++++ WFObj.h | 10 ++++++---- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/WFObj.cc b/WFObj.cc index 47d2a2c..89fd291 100644 --- a/WFObj.cc +++ b/WFObj.cc @@ -379,6 +379,7 @@ void WFObj::loadMaterial(const std::string & name) for (int i = 0; i < 3; i++) m_materials[mat_name].ambient[i] = atof(tokens[i + 1].c_str()); + m_materials[mat_name].ambient[3] = 1.0; m_materials[mat_name].flags |= Material::MAT_AMBIENT; } else @@ -392,6 +393,7 @@ void WFObj::loadMaterial(const std::string & name) for (int i = 0; i < 3; i++) m_materials[mat_name].diffuse[i] = atof(tokens[i + 1].c_str()); + m_materials[mat_name].diffuse[3] = 1.0; m_materials[mat_name].flags |= Material::MAT_DIFFUSE; } else @@ -405,6 +407,7 @@ void WFObj::loadMaterial(const std::string & name) for (int i = 0; i < 3; i++) m_materials[mat_name].specular[i] = atof(tokens[i + 1].c_str()); + m_materials[mat_name].specular[3] = 1.0; m_materials[mat_name].flags |= Material::MAT_SPECULAR; } else @@ -542,6 +545,36 @@ bool WFObj::buildVBO() return true; } +void WFObj::draw() +{ + if (!m_valid) + return; + for (map >::iterator it = m_faces.begin(); + it != m_faces.end(); + it++) + { + glPushAttrib(GL_LIGHTING_BIT); + renderMaterial(m_materials[it->first]); + glPopAttrib(); + } +} + +void WFObj::renderMaterial(const WFObj::Material & m) +{ + if (m.flags & Material::MAT_SHININESS) + glMaterialf(GL_FRONT, GL_SHININESS, m.shininess); + if (m.flags & Material::MAT_AMBIENT) + glMaterialfv(GL_FRONT, GL_AMBIENT, &m.ambient[0]); + if (m.flags & Material::MAT_DIFFUSE) + glMaterialfv(GL_FRONT, GL_DIFFUSE, &m.diffuse[0]); + if (m.flags & Material::MAT_SPECULAR) + glMaterialfv(GL_FRONT, GL_SPECULAR, &m.specular[0]); + if (m.flags & Material::MAT_TEXTURE) + { + cerr << "WFObj: error: textured materials not implemented yet" << endl; + } +} + bool WFObj::VertexRef::operator<(const VertexRef & other) const { if (vertex != other.vertex) diff --git a/WFObj.h b/WFObj.h index 9edecff..18b8f8b 100644 --- a/WFObj.h +++ b/WFObj.h @@ -41,6 +41,7 @@ public: bool load(const char *fname); bool load(const Buffer &buff); const float * const getAABB() { return m_aabb; } + void draw(); protected: /* types */ @@ -81,10 +82,10 @@ protected: MAT_TEXTURE = 0x10 }; Material() : flags(0) {} - float shininess; - float ambient[3]; - float diffuse[3]; - float specular[3]; + GLfloat shininess; + GLfloat ambient[4]; + GLfloat diffuse[4]; + GLfloat specular[4]; std::string texture; int flags; }; @@ -100,6 +101,7 @@ protected: std::string getLine(const Buffer & buff, size_t idx, size_t *update_idx); void loadMaterial(const std::string & name); bool buildVBO(); + void renderMaterial(const Material & m); /* variables */ std::vector m_vertices[VERTEX_TYPES];