diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2017-05-05 17:05:34 +0200 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2017-05-05 19:53:04 +0000 |
commit | 8b45682dfb5bff49c6a7e9ed7b1bbe56e5801585 (patch) | |
tree | ae9c983cc147b2df3ace009ecc8c6a560b76cc3d /src | |
parent | 46e4a33fe90b7ff21baffcb08194ef4c7b346df7 (diff) |
AssimpImporter: properly handle texture wrapping
By default the wrap mode is Repeat. If specified it is changed to the
appropriate value.
Note: This changes also shows that each material could be definining the same
texture with different wrap modes. Therefore we cannot store textures only
based on their file path like we used to.
Change-Id: Ic543c466d7c5cc7cd7e26d1738006952a8ce746b
Task-number: QTBUG-59531
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/plugins/sceneparsers/assimp/assimpimporter.cpp | 47 | ||||
-rw-r--r-- | src/plugins/sceneparsers/assimp/assimpimporter.h | 1 |
2 files changed, 36 insertions, 12 deletions
diff --git a/src/plugins/sceneparsers/assimp/assimpimporter.cpp b/src/plugins/sceneparsers/assimp/assimpimporter.cpp index 5d5593585..196e1d22f 100644 --- a/src/plugins/sceneparsers/assimp/assimpimporter.cpp +++ b/src/plugins/sceneparsers/assimp/assimpimporter.cpp @@ -142,7 +142,7 @@ QMatrix4x4 aiMatrix4x4ToQMatrix4x4(const aiMatrix4x4 &matrix) Q_DECL_NOTHROW /*! * Returns a QString from \a str; */ -static inline QString aiStringToQString(const aiString &str) +inline QString aiStringToQString(const aiString &str) { return QString::fromUtf8(str.data, int(str.length)); } @@ -246,6 +246,21 @@ QAttribute *createAttribute(QBuffer *buffer, return attribute; } +QTextureWrapMode::WrapMode wrapModeFromaiTextureMapMode(int mode) +{ + switch (mode) { + case aiTextureMapMode_Wrap: + return QTextureWrapMode::Repeat; + case aiTextureMapMode_Mirror: + return QTextureWrapMode::MirroredRepeat; + case aiTextureMapMode_Decal: + return QTextureWrapMode::ClampToBorder; + case aiTextureMapMode_Clamp: + default: + return QTextureWrapMode::ClampToEdge; + } +} + } // anonymous QStringList AssimpImporter::assimpSupportedFormatsList = AssimpImporter::assimpSupportedFormats(); @@ -1237,18 +1252,28 @@ void AssimpImporter::copyMaterialTextures(QMaterial *material, aiMaterial *assim for (unsigned int i = 0; i < sizeof(textureType)/sizeof(textureType[0]); i++) { aiString path; if (assimpMaterial->GetTexture(textureType[i], 0, &path) == AI_SUCCESS) { - QString fullPath = m_sceneDir.absoluteFilePath(texturePath(path)); + const QString fullPath = m_sceneDir.absoluteFilePath(texturePath(path)); // Load texture if not already loaded - if (!m_scene->m_materialTextures.contains(fullPath)) { - QAbstractTexture *tex = QAbstractNodeFactory::createNode<QTexture2D>("QTexture2D"); - QTextureImage *texImage = QAbstractNodeFactory::createNode<QTextureImage>("QTextureImage"); - texImage->setSource(QUrl::fromLocalFile(fullPath)); - tex->addTextureImage(texImage); - m_scene->m_materialTextures.insert(fullPath, tex); - qCDebug(AssimpImporterLog) << Q_FUNC_INFO << " Loaded Texture " << fullPath; - } + QAbstractTexture *tex = QAbstractNodeFactory::createNode<QTexture2D>("QTexture2D"); + QTextureImage *texImage = QAbstractNodeFactory::createNode<QTextureImage>("QTextureImage"); + texImage->setSource(QUrl::fromLocalFile(fullPath)); + tex->addTextureImage(texImage); + + // Set proper wrapping mode + QTextureWrapMode wrapMode(QTextureWrapMode::Repeat); + int xMode = 0; + int yMode = 0; + + if (assimpMaterial->Get(AI_MATKEY_MAPPINGMODE_U(textureType[i], 0), xMode) == aiReturn_SUCCESS) + wrapMode.setX(wrapModeFromaiTextureMapMode(xMode)); + if (assimpMaterial->Get(AI_MATKEY_MAPPINGMODE_V(textureType[i], 0), yMode) == aiReturn_SUCCESS) + wrapMode.setY(wrapModeFromaiTextureMapMode(yMode)); + + tex->setWrapMode(wrapMode); + + qCDebug(AssimpImporterLog) << Q_FUNC_INFO << " Loaded Texture " << fullPath; setParameterValue(m_scene->m_textureToParameterName[textureType[i]], - material, QVariant::fromValue(m_scene->m_materialTextures[fullPath])); + material, QVariant::fromValue(tex)); } } } diff --git a/src/plugins/sceneparsers/assimp/assimpimporter.h b/src/plugins/sceneparsers/assimp/assimpimporter.h index 7335830f5..e37950981 100644 --- a/src/plugins/sceneparsers/assimp/assimpimporter.h +++ b/src/plugins/sceneparsers/assimp/assimpimporter.h @@ -143,7 +143,6 @@ private: QMap<uint, QMaterial*> m_materials; QMap<uint, QEffect *> m_effects; QMap<uint, QAbstractTexture *> m_embeddedTextures; - QMap<QString, QAbstractTexture *> m_materialTextures; QMap<aiNode*, Qt3DCore::QEntity*> m_cameras; QHash<aiTextureType, QString> m_textureToParameterName; QVector<Qt3DAnimation::QKeyframeAnimation *> m_animations; |