Apply (some) FBX material properties to rendered meshes.

This commit is contained in:
Andrzej Kapolka 2013-10-03 18:12:58 -07:00
parent c99f1646db
commit 51c868fd28
3 changed files with 63 additions and 8 deletions

View file

@ -88,8 +88,7 @@ bool BlendFace::render(float alpha) {
// enable normalization under the expectation that the GPU can do it faster // enable normalization under the expectation that the GPU can do it faster
glEnable(GL_NORMALIZE); glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glDisable(GL_COLOR_MATERIAL);
glColor4f(_owningHead->getSkinColor().r, _owningHead->getSkinColor().g, _owningHead->getSkinColor().b, alpha);
for (int i = 0; i < networkMeshes.size(); i++) { for (int i = 0; i < networkMeshes.size(); i++) {
const NetworkMesh& networkMesh = networkMeshes.at(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 glMultMatrixf((const GLfloat*)&mesh.transform);
if (i == 1) {
glColor4f(1.0f, 1.0f, 1.0f, alpha);
}
glBindTexture(GL_TEXTURE_2D, texture == NULL ? 0 : texture->getID()); glBindTexture(GL_TEXTURE_2D, texture == NULL ? 0 : texture->getID());
@ -191,6 +190,9 @@ bool BlendFace::render(float alpha) {
glPopMatrix(); glPopMatrix();
// restore all the default material settings
Application::getInstance()->setupWorldLight(*Application::getInstance()->getCamera());
return true; return true;
} }

View file

@ -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) { FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping) {
QHash<qint64, FBXMesh> meshes; QHash<qint64, FBXMesh> meshes;
QVector<ExtractedBlendshape> blendshapes; QVector<ExtractedBlendshape> blendshapes;
@ -354,6 +362,7 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping)
QHash<qint64, Transform> localTransforms; QHash<qint64, Transform> localTransforms;
QHash<qint64, glm::mat4> transformLinkMatrices; QHash<qint64, glm::mat4> transformLinkMatrices;
QHash<qint64, QByteArray> textureFilenames; QHash<qint64, QByteArray> textureFilenames;
QHash<qint64, Material> materials;
QHash<qint64, qint64> diffuseTextures; QHash<qint64, qint64> diffuseTextures;
QHash<qint64, qint64> bumpTextures; QHash<qint64, qint64> bumpTextures;
@ -578,6 +587,37 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping)
textureFilenames.insert(object.properties.at(0).value<qint64>(), filename); textureFilenames.insert(object.properties.at(0).value<qint64>(), 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<double>(),
property.properties.at(5).value<double>(),
property.properties.at(6).value<double>(), 1.0f);
} else if (property.properties.at(0) == "DiffuseColor") {
material.diffuse = glm::vec4(property.properties.at(4).value<double>(),
property.properties.at(5).value<double>(),
property.properties.at(6).value<double>(), 1.0f);
} else if (property.properties.at(0) == "SpecularColor") {
material.specular = glm::vec4(property.properties.at(4).value<double>(),
property.properties.at(5).value<double>(),
property.properties.at(6).value<double>(), 1.0f);
} else if (property.properties.at(0) == "Shininess") {
material.shininess = property.properties.at(4).value<double>();
}
}
}
}
}
materials.insert(object.properties.at(0).value<qint64>(), material);
} else if (object.name == "Deformer") { } else if (object.name == "Deformer") {
if (object.properties.at(2) == "Cluster") { if (object.properties.at(2) == "Cluster") {
foreach (const FBXNode& subobject, object.children) { foreach (const FBXNode& subobject, object.children) {
@ -641,8 +681,16 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping)
qint64 modelID = parentMap.value(it.key()); qint64 modelID = parentMap.value(it.key());
glm::mat4 modelTransform = getGlobalTransform(parentMap, localTransforms, modelID); glm::mat4 modelTransform = getGlobalTransform(parentMap, localTransforms, modelID);
// look for textures // look for textures, material properties
foreach (qint64 childID, childMap.values(modelID)) { 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); qint64 diffuseTextureID = diffuseTextures.value(childID);
if (diffuseTextureID != 0) { if (diffuseTextureID != 0) {
mesh.diffuseFilename = textureFilenames.value(diffuseTextureID); mesh.diffuseFilename = textureFilenames.value(diffuseTextureID);

View file

@ -51,6 +51,11 @@ public:
bool isEye; bool isEye;
glm::vec4 ambientColor;
glm::vec4 diffuseColor;
glm::vec4 specularColor;
float shininess;
QByteArray diffuseFilename; QByteArray diffuseFilename;
QByteArray normalFilename; QByteArray normalFilename;