From 51c868fd28bff8a7673307a5b1e1ecd681c22b31 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 3 Oct 2013 18:12:58 -0700 Subject: [PATCH] Apply (some) FBX material properties to rendered meshes. --- interface/src/avatar/BlendFace.cpp | 16 +++++---- interface/src/renderer/FBXReader.cpp | 50 +++++++++++++++++++++++++++- interface/src/renderer/FBXReader.h | 5 +++ 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/interface/src/avatar/BlendFace.cpp b/interface/src/avatar/BlendFace.cpp index 321019b4a0..c88221c25b 100644 --- a/interface/src/avatar/BlendFace.cpp +++ b/interface/src/avatar/BlendFace.cpp @@ -88,8 +88,7 @@ bool BlendFace::render(float alpha) { // enable normalization under the expectation that the GPU can do it faster glEnable(GL_NORMALIZE); glEnable(GL_TEXTURE_2D); - - glColor4f(_owningHead->getSkinColor().r, _owningHead->getSkinColor().g, _owningHead->getSkinColor().b, alpha); + glDisable(GL_COLOR_MATERIAL); for (int i = 0; i < networkMeshes.size(); i++) { const NetworkMesh& networkMesh = networkMeshes.at(i); @@ -118,12 +117,12 @@ bool BlendFace::render(float alpha) { } } - glMultMatrixf((const GLfloat*)&mesh.transform); + // apply material properties + glMaterialfv(GL_FRONT, GL_DIFFUSE, (const float*)&mesh.diffuseColor); + glMaterialfv(GL_FRONT, GL_SPECULAR, (const float*)&mesh.specularColor); + glMaterialf(GL_FRONT, GL_SHININESS, mesh.shininess); - // all meshes after the first are white - if (i == 1) { - glColor4f(1.0f, 1.0f, 1.0f, alpha); - } + glMultMatrixf((const GLfloat*)&mesh.transform); glBindTexture(GL_TEXTURE_2D, texture == NULL ? 0 : texture->getID()); @@ -191,6 +190,9 @@ bool BlendFace::render(float alpha) { glPopMatrix(); + // restore all the default material settings + Application::getInstance()->setupWorldLight(*Application::getInstance()->getCamera()); + return true; } diff --git a/interface/src/renderer/FBXReader.cpp b/interface/src/renderer/FBXReader.cpp index 24644866cd..2a048b4d3b 100644 --- a/interface/src/renderer/FBXReader.cpp +++ b/interface/src/renderer/FBXReader.cpp @@ -346,6 +346,14 @@ void printNode(const FBXNode& node, int indent) { } } +class Material { +public: + glm::vec4 ambient; + glm::vec4 diffuse; + glm::vec4 specular; + float shininess; +}; + FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) { QHash meshes; QVector blendshapes; @@ -354,6 +362,7 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) QHash localTransforms; QHash transformLinkMatrices; QHash textureFilenames; + QHash materials; QHash diffuseTextures; QHash bumpTextures; @@ -578,6 +587,37 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) textureFilenames.insert(object.properties.at(0).value(), filename); } } + } else if (object.name == "Material") { + Material material = { glm::vec4(1.0f, 1.0f, 1.0f, 1.0f), glm::vec4(1.0f, 1.0f, 1.0f, 1.0f), + glm::vec4(1.0f, 1.0f, 1.0f, 1.0f), 96.0f }; + foreach (const FBXNode& subobject, object.children) { + if (subobject.name == "Properties70") { + foreach (const FBXNode& property, subobject.children) { + if (property.name == "P") { + if (property.properties.at(0) == "AmbientColor") { + material.ambient = glm::vec4(property.properties.at(4).value(), + property.properties.at(5).value(), + property.properties.at(6).value(), 1.0f); + + } else if (property.properties.at(0) == "DiffuseColor") { + material.diffuse = glm::vec4(property.properties.at(4).value(), + property.properties.at(5).value(), + property.properties.at(6).value(), 1.0f); + + } else if (property.properties.at(0) == "SpecularColor") { + material.specular = glm::vec4(property.properties.at(4).value(), + property.properties.at(5).value(), + property.properties.at(6).value(), 1.0f); + + } else if (property.properties.at(0) == "Shininess") { + material.shininess = property.properties.at(4).value(); + } + } + } + } + } + materials.insert(object.properties.at(0).value(), material); + } else if (object.name == "Deformer") { if (object.properties.at(2) == "Cluster") { foreach (const FBXNode& subobject, object.children) { @@ -641,8 +681,16 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) qint64 modelID = parentMap.value(it.key()); glm::mat4 modelTransform = getGlobalTransform(parentMap, localTransforms, modelID); - // look for textures + // look for textures, material properties foreach (qint64 childID, childMap.values(modelID)) { + if (!materials.contains(childID)) { + continue; + } + Material material = materials.value(childID); + mesh.ambientColor = material.ambient; + mesh.diffuseColor = material.diffuse; + mesh.specularColor = material.specular; + mesh.shininess = material.shininess; qint64 diffuseTextureID = diffuseTextures.value(childID); if (diffuseTextureID != 0) { mesh.diffuseFilename = textureFilenames.value(diffuseTextureID); diff --git a/interface/src/renderer/FBXReader.h b/interface/src/renderer/FBXReader.h index f84a06468e..77568072c4 100644 --- a/interface/src/renderer/FBXReader.h +++ b/interface/src/renderer/FBXReader.h @@ -51,6 +51,11 @@ public: bool isEye; + glm::vec4 ambientColor; + glm::vec4 diffuseColor; + glm::vec4 specularColor; + float shininess; + QByteArray diffuseFilename; QByteArray normalFilename;