Fixing buggy hash map look up and cleaning of name

This commit is contained in:
samcake 2015-09-20 14:17:55 -07:00
parent c9cb768945
commit a41c20a1b7
3 changed files with 63 additions and 34 deletions

View file

@ -817,12 +817,12 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS
if (subobject.name == "RelativeFilename") {
QByteArray filename = subobject.properties.at(0).toByteArray();
filename = fileOnUrl(filename, url);
textureFilenames.insert(getID(object.properties), filename);
_textureFilenames.insert(getID(object.properties), filename);
} else if (subobject.name == "TextureName") {
// trim the name from the timestamp
QString name = QString(subobject.properties.at(0).toByteArray());
name = name.left(name.indexOf('['));
textureNames.insert(getID(object.properties), name);
_textureNames.insert(getID(object.properties), name);
} else if (subobject.name == "Texture_Alpha_Source") {
tex.assign<uint8_t>(tex.alphaSource, subobject.properties.at(0).value<int>());
} else if (subobject.name == "ModelUVTranslation") {
@ -831,6 +831,12 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS
} else if (subobject.name == "ModelUVScaling") {
tex.assign(tex.UVScaling, glm::vec2(subobject.properties.at(0).value<double>(),
subobject.properties.at(1).value<double>()));
if (tex.UVScaling.x == 0.0f) {
tex.UVScaling.x = 1.0f;
}
if (tex.UVScaling.y == 0.0f) {
tex.UVScaling.y = 1.0f;
}
} else if (subobject.name == "Cropping") {
tex.assign(tex.cropping, glm::vec4(subobject.properties.at(0).value<int>(),
subobject.properties.at(1).value<int>(),
@ -857,6 +863,15 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS
tex.assign(tex.rotation, getVec3(property.properties, index));
} else if (property.properties.at(0) == "Scaling") {
tex.assign(tex.scaling, getVec3(property.properties, index));
if (tex.scaling.x == 0.0f) {
tex.scaling.x = 1.0f;
}
if (tex.scaling.y == 0.0f) {
tex.scaling.y = 1.0f;
}
if (tex.scaling.z == 0.0f) {
tex.scaling.z = 1.0f;
}
}
#if defined(DEBUG_FBXREADER)
else {
@ -882,7 +897,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS
}
if (!tex.isDefault) {
textureParams.insert(getID(object.properties), tex);
_textureParams.insert(getID(object.properties), tex);
}
} else if (object.name == "Video") {
QByteArray filename;
@ -897,7 +912,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS
}
}
if (!content.isEmpty()) {
textureContent.insert(filename, content);
_textureContent.insert(filename, content);
}
} else if (object.name == "Material") {
FBXMaterial material = { glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(), glm::vec2(0.f, 1.0f), 96.0f, 1.0f,
@ -1299,7 +1314,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS
geometry.materials = _fbxMaterials;
// see if any materials have texture children
bool materialsHaveTextures = checkMaterialsHaveTextures(_fbxMaterials, textureFilenames, _connectionChildMap);
bool materialsHaveTextures = checkMaterialsHaveTextures(_fbxMaterials, _textureFilenames, _connectionChildMap);
for (QHash<QString, ExtractedMesh>::iterator it = meshes.begin(); it != meshes.end(); it++) {
ExtractedMesh& extracted = it.value();
@ -1344,7 +1359,7 @@ FBXGeometry* FBXReader::extractFBXGeometry(const QVariantHash& mapping, const QS
materialIndex++;
} else if (textureFilenames.contains(childID)) {
} else if (_textureFilenames.contains(childID)) {
FBXTexture texture = getTexture(childID);
for (int j = 0; j < extracted.partMaterialTextures.size(); j++) {
int partTexture = extracted.partMaterialTextures.at(j).second;

View file

@ -312,7 +312,8 @@ FBXGeometry* readFBX(const QByteArray& model, const QVariantHash& mapping, const
/// \exception QString if an error occurs in parsing
FBXGeometry* readFBX(QIODevice* device, const QVariantHash& mapping, const QString& url = "", bool loadLightmaps = true, float lightmapLevel = 1.0f);
struct TextureParam {
class TextureParam {
public:
glm::vec2 UVTranslation;
glm::vec2 UVScaling;
glm::vec4 cropping;
@ -351,6 +352,21 @@ struct TextureParam {
useMaterial(true),
isDefault(true)
{}
TextureParam(const TextureParam& src) :
UVTranslation(src.UVTranslation),
UVScaling(src.UVScaling),
cropping(src.cropping),
UVSet(src.UVSet),
translation(src.translation),
rotation(src.rotation),
scaling(src.scaling),
alphaSource(src.alphaSource),
currentTextureBlendMode(src.currentTextureBlendMode),
useMaterial(src.useMaterial),
isDefault(src.isDefault)
{}
};
class ExtractedMesh;
@ -370,10 +386,10 @@ public:
FBXTexture getTexture(const QString& textureID);
QHash<QString, QString> textureNames;
QHash<QString, QByteArray> textureFilenames;
QHash<QByteArray, QByteArray> textureContent;
QHash<QString, TextureParam> textureParams;
QHash<QString, QString> _textureNames;
QHash<QString, QByteArray> _textureFilenames;
QHash<QByteArray, QByteArray> _textureContent;
QHash<QString, TextureParam> _textureParams;
QHash<QString, QString> diffuseTextures;

View file

@ -28,17 +28,30 @@ bool FBXMaterial::needTangentSpace() const {
FBXTexture FBXReader::getTexture(const QString& textureID) {
FBXTexture texture;
texture.filename = textureFilenames.value(textureID);
texture.name = textureNames.value(textureID);
texture.content = textureContent.value(texture.filename);
texture.filename = _textureFilenames.value(textureID);
texture.name = _textureNames.value(textureID);
texture.content = _textureContent.value(texture.filename);
texture.transform.setIdentity();
texture.texcoordSet = 0;
QHash<QString, TextureParam>::const_iterator it = textureParams.constFind(textureID);
if (it != textureParams.end()) {
const TextureParam& p = (*it);
if (_textureParams.contains(textureID)) {
auto p = _textureParams.value(textureID);
texture.transform.setTranslation(p.translation);
texture.transform.setRotation(glm::quat(glm::radians(p.rotation)));
texture.transform.setScale(p.scaling);
auto scaling = p.scaling;
// Protect from bad scaling which should never happen
if (scaling.x == 0.0f) {
scaling.x = 1.0f;
}
if (scaling.y == 0.0f) {
scaling.y = 1.0f;
}
if (scaling.z == 0.0f) {
scaling.z = 1.0f;
}
texture.transform.setScale(scaling);
if ((p.UVSet != "map1") && (p.UVSet != "UVSet0")) {
texture.texcoordSet = 1;
}
@ -61,14 +74,11 @@ void FBXReader::consolidateFBXMaterials() {
// FBX files generated by 3DSMax have an intermediate texture parent, apparently
foreach (const QString& childTextureID, _connectionChildMap.values(diffuseTextureID)) {
if (textureFilenames.contains(childTextureID)) {
if (_textureFilenames.contains(childTextureID)) {
diffuseTexture = getTexture(diffuseTextureID);
}
}
// TODO associate this per part
//diffuseTexture.texcoordSet = matchTextureUVSetToAttributeChannel(diffuseTexture.texcoordSetName, extracted.texcoordSetMap);
material.diffuseTexture = diffuseTexture;
detectDifferentUVs = (diffuseTexture.texcoordSet != 0) || (!diffuseTexture.transform.isIdentity());
@ -78,12 +88,6 @@ void FBXReader::consolidateFBXMaterials() {
QString bumpTextureID = bumpTextures.value(material.materialID);
if (!bumpTextureID.isNull()) {
normalTexture = getTexture(bumpTextureID);
// TODO Need to generate tangent space at association per part
//generateTangents = true;
// TODO at per part association time
// normalTexture.texcoordSet = matchTextureUVSetToAttributeChannel(normalTexture.texcoordSetName, extracted.texcoordSetMap);
material.normalTexture = normalTexture;
@ -94,8 +98,6 @@ void FBXReader::consolidateFBXMaterials() {
QString specularTextureID = specularTextures.value(material.materialID);
if (!specularTextureID.isNull()) {
specularTexture = getTexture(specularTextureID);
// TODO at per part association time
// specularTexture.texcoordSet = matchTextureUVSetToAttributeChannel(specularTexture.texcoordSetName, extracted.texcoordSetMap);
detectDifferentUVs |= (specularTexture.texcoordSet != 0) || (!specularTexture.transform.isIdentity());
}
@ -115,13 +117,9 @@ void FBXReader::consolidateFBXMaterials() {
emissiveTexture = getTexture(ambientTextureID);
}
// TODO : do this at per part association
//emissiveTexture.texcoordSet = matchTextureUVSetToAttributeChannel(emissiveTexture.texcoordSetName, extracted.texcoordSetMap);
material.emissiveParams = emissiveParams;
material.emissiveTexture = emissiveTexture;
detectDifferentUVs |= (emissiveTexture.texcoordSet != 0) || (!emissiveTexture.transform.isIdentity());
}