| 1 | // Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "fbxgeometryloader.h" |
| 5 | |
| 6 | #include <QtCore/QFileDevice> |
| 7 | |
| 8 | #include <Qt3DCore/QAttribute> |
| 9 | #include <Qt3DCore/QBuffer> |
| 10 | #include <Qt3DCore/QGeometry> |
| 11 | #include <Qt3DRender/private/renderlogging_p.h> |
| 12 | |
| 13 | #include <fbxsdk.h> |
| 14 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | namespace Qt3DRender { |
| 18 | |
| 19 | Q_LOGGING_CATEGORY(FbxGeometryLoaderLog, "Qt3D.FbxGeometryLoader", QtWarningMsg) |
| 20 | |
| 21 | class FbxStreamWrapper : public FbxStream |
| 22 | { |
| 23 | FbxManager *m_manager; |
| 24 | QIODevice *m_device; |
| 25 | public: |
| 26 | FbxStreamWrapper(FbxManager *manager) |
| 27 | : m_manager(manager) |
| 28 | , m_device(nullptr) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | EState GetState() |
| 33 | { |
| 34 | if (!m_device) |
| 35 | return eEmpty; |
| 36 | return m_device->isOpen() ? eOpen : eClosed; |
| 37 | } |
| 38 | |
| 39 | bool Open(void *pStreamData) |
| 40 | { |
| 41 | m_device = reinterpret_cast<QIODevice *>(pStreamData); |
| 42 | |
| 43 | if (!m_device->isOpen()) |
| 44 | m_device->open(QIODevice::ReadOnly); |
| 45 | |
| 46 | return m_device && m_device->isOpen(); |
| 47 | } |
| 48 | |
| 49 | bool Close() |
| 50 | { |
| 51 | Q_ASSERT(m_device); |
| 52 | |
| 53 | if (Q_UNLIKELY(!m_device)) |
| 54 | return false; |
| 55 | |
| 56 | m_device->close(); |
| 57 | |
| 58 | return !m_device->isOpen(); |
| 59 | } |
| 60 | |
| 61 | bool Flush() |
| 62 | { |
| 63 | Q_ASSERT(m_device); |
| 64 | |
| 65 | if (Q_UNLIKELY(!m_device)) |
| 66 | return false; |
| 67 | |
| 68 | QFileDevice *device = qobject_cast<QFileDevice*>(m_device); |
| 69 | if (Q_LIKELY(device)) |
| 70 | return device->flush(); |
| 71 | |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | int Write(const void *pData, int pSize) |
| 76 | { |
| 77 | Q_ASSERT(m_device); |
| 78 | |
| 79 | if (Q_UNLIKELY(!m_device)) |
| 80 | return -1; |
| 81 | |
| 82 | return m_device->write(reinterpret_cast<const char *>(pData), pSize); |
| 83 | } |
| 84 | |
| 85 | int Read(void *pData, int pSize) const |
| 86 | { |
| 87 | Q_ASSERT(m_device); |
| 88 | |
| 89 | if (Q_UNLIKELY(!m_device)) |
| 90 | return -1; |
| 91 | |
| 92 | return m_device->read(reinterpret_cast<char *>(pData), pSize); |
| 93 | } |
| 94 | |
| 95 | char *ReadString(char *pBuffer, int pMaxSize, bool pStopAtFirstWhiteSpace = false) |
| 96 | { |
| 97 | Q_UNUSED(pBuffer); |
| 98 | Q_UNUSED(pMaxSize); |
| 99 | Q_UNUSED(pStopAtFirstWhiteSpace); |
| 100 | return nullptr; |
| 101 | } |
| 102 | |
| 103 | int GetReaderID() const |
| 104 | { |
| 105 | return m_manager->GetIOPluginRegistry()-> |
| 106 | FindReaderIDByExtension(qPrintable(FBXGEOMETRYLOADER_EXT)); |
| 107 | } |
| 108 | |
| 109 | int GetWriterID() const |
| 110 | { |
| 111 | return m_manager->GetIOPluginRegistry()-> |
| 112 | FindReaderIDByExtension(qPrintable(FBXGEOMETRYLOADER_EXT)); |
| 113 | } |
| 114 | |
| 115 | void Seek(const FbxInt64 &pOffset, const FbxFile::ESeekPos &pSeekPos) |
| 116 | { |
| 117 | Q_ASSERT(m_device); |
| 118 | |
| 119 | if (Q_UNLIKELY(!m_device)) |
| 120 | return; |
| 121 | |
| 122 | switch (pSeekPos) { |
| 123 | case FbxFile::eBegin: |
| 124 | m_device->seek(pOffset); |
| 125 | break; |
| 126 | case FbxFile::eCurrent: |
| 127 | m_device->seek(m_device->pos() + pOffset); |
| 128 | break; |
| 129 | case FbxFile::eEnd: |
| 130 | m_device->seek(m_device->size() - pOffset); |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | long GetPosition() const |
| 136 | { |
| 137 | Q_ASSERT(m_device); |
| 138 | |
| 139 | if (Q_UNLIKELY(!m_device)) |
| 140 | return -1; |
| 141 | |
| 142 | return m_device->pos(); |
| 143 | } |
| 144 | |
| 145 | void SetPosition(long pPosition) |
| 146 | { |
| 147 | Q_ASSERT(m_device); |
| 148 | |
| 149 | if (Q_UNLIKELY(!m_device)) |
| 150 | return; |
| 151 | |
| 152 | m_device->seek(pPosition); |
| 153 | } |
| 154 | |
| 155 | int GetError() const |
| 156 | { |
| 157 | Q_ASSERT(m_device); |
| 158 | |
| 159 | if (Q_UNLIKELY(!m_device)) |
| 160 | return 1; |
| 161 | |
| 162 | QFileDevice *device = qobject_cast<QFileDevice *>(m_device); |
| 163 | if (Q_LIKELY(device)) |
| 164 | return device->error() == QFileDevice::NoError ? 0 : 1; |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | void ClearError() |
| 170 | { |
| 171 | Q_ASSERT(m_device); |
| 172 | |
| 173 | if (Q_UNLIKELY(!m_device)) |
| 174 | return; |
| 175 | |
| 176 | QFileDevice *device = qobject_cast<QFileDevice *>(m_device); |
| 177 | if (Q_LIKELY(device)) |
| 178 | device->unsetError(); |
| 179 | } |
| 180 | }; |
| 181 | |
| 182 | FbxGeometryLoader::FbxGeometryLoader() |
| 183 | : m_manager(nullptr) |
| 184 | , m_scene(nullptr) |
| 185 | , m_geometry(nullptr) |
| 186 | { |
| 187 | m_manager = FbxManager::Create(); |
| 188 | |
| 189 | if (Q_LIKELY(m_manager)) |
| 190 | qInfo(FbxGeometryLoaderLog, "Autodesk FBX SDK version %s", m_manager->GetVersion()); |
| 191 | else |
| 192 | qWarning(FbxGeometryLoaderLog, "Failed to create FBX Manager."); |
| 193 | } |
| 194 | |
| 195 | FbxGeometryLoader::~FbxGeometryLoader() |
| 196 | { |
| 197 | if (m_manager) |
| 198 | m_manager->Destroy(); |
| 199 | } |
| 200 | |
| 201 | /*! |
| 202 | Gives the list of attributes that can be used to render |
| 203 | a 3D form. |
| 204 | Returns a pointer to the geometry object. |
| 205 | */ |
| 206 | QGeometry *Qt3DRender::FbxGeometryLoader::geometry() const |
| 207 | { |
| 208 | return m_geometry; |
| 209 | } |
| 210 | |
| 211 | /*! |
| 212 | Loads the specified \a subMesh using a device \a ioDev. Returns \c true |
| 213 | on success, \c false otherwise. |
| 214 | */ |
| 215 | bool Qt3DRender::FbxGeometryLoader::load(QIODevice *ioDev, const QString &subMesh) |
| 216 | { |
| 217 | if (m_scene) |
| 218 | m_scene->Destroy(); |
| 219 | |
| 220 | m_scene = FbxScene::Create(m_manager, "scene"); |
| 221 | if (!m_scene) |
| 222 | qWarning(FbxGeometryLoaderLog, "Unable to create FBX scene!"); |
| 223 | |
| 224 | QScopedPointer<FbxStreamWrapper> fbxStream(new FbxStreamWrapper(m_manager)); |
| 225 | |
| 226 | FbxImporter *importer = FbxImporter::Create(m_manager, ""); |
| 227 | |
| 228 | const bool hasInitialized = importer->Initialize(fbxStream.data(), ioDev); |
| 229 | |
| 230 | int fileVersion[3]; |
| 231 | |
| 232 | importer->GetFileVersion(fileVersion[0], fileVersion[1], fileVersion[2]); |
| 233 | |
| 234 | if (!hasInitialized) { |
| 235 | FbxString error = importer->GetStatus().GetErrorString(); |
| 236 | qWarning(FbxGeometryLoaderLog, "Call to FbxImporter::Initialize() failed."); |
| 237 | qWarning(FbxGeometryLoaderLog, "Error returned: %s", error.Buffer()); |
| 238 | |
| 239 | if (importer->GetStatus().GetCode() == FbxStatus::eInvalidFileVersion) { |
| 240 | int SdkVersion[3]; |
| 241 | FbxManager::GetFileFormatVersion(SdkVersion[0], SdkVersion[1], SdkVersion[2]); |
| 242 | qWarning(FbxGeometryLoaderLog, "FBX file format version for this FBX SDK is %d.%d.%d", |
| 243 | SdkVersion[0], SdkVersion[1], SdkVersion[2]); |
| 244 | qWarning(FbxGeometryLoaderLog, "FBX file format version for the file is %d.%d.%d", |
| 245 | fileVersion[0], fileVersion[1], fileVersion[2]); |
| 246 | } |
| 247 | |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | if (importer->IsFBX()) { |
| 252 | qInfo(FbxGeometryLoaderLog, "FBX file format version for file is %d.%d.%d", |
| 253 | fileVersion[0], fileVersion[1], fileVersion[2]); |
| 254 | |
| 255 | const int stackCount = importer->GetAnimStackCount(); |
| 256 | for (int i = 0; i < stackCount; ++i) { |
| 257 | FbxTakeInfo *lTakeInfo = importer->GetTakeInfo(i); |
| 258 | qInfo(FbxGeometryLoaderLog, " Animation Stack %d", i); |
| 259 | qInfo(FbxGeometryLoaderLog, " Name: \"%s\"", lTakeInfo->mName.Buffer()); |
| 260 | qInfo(FbxGeometryLoaderLog, " Description: \"%s\"", lTakeInfo->mDescription.Buffer()); |
| 261 | qInfo(FbxGeometryLoaderLog, " Import Name: \"%s\"", lTakeInfo->mImportName.Buffer()); |
| 262 | qInfo(FbxGeometryLoaderLog, " Import State: %s", lTakeInfo->mSelect ? "true" : "false"); |
| 263 | } |
| 264 | |
| 265 | auto settings = importer->GetIOSettings(); |
| 266 | settings->SetBoolProp(IMP_FBX_MATERIAL, false); |
| 267 | settings->SetBoolProp(IMP_FBX_TEXTURE, false); |
| 268 | settings->SetBoolProp(IMP_FBX_LINK, false); |
| 269 | settings->SetBoolProp(IMP_FBX_SHAPE, false); |
| 270 | settings->SetBoolProp(IMP_FBX_GOBO, false); |
| 271 | settings->SetBoolProp(IMP_FBX_ANIMATION, false); |
| 272 | settings->SetBoolProp(IMP_FBX_GLOBAL_SETTINGS, false); |
| 273 | } |
| 274 | |
| 275 | const bool wasImported = importer->Import(m_scene); |
| 276 | importer->Destroy(); |
| 277 | |
| 278 | m_mesh = subMesh; |
| 279 | |
| 280 | if (wasImported) |
| 281 | recurseNodes(); |
| 282 | |
| 283 | return wasImported; |
| 284 | } |
| 285 | |
| 286 | /*! |
| 287 | Traverse the node hierarchy and process the children of each |
| 288 | node. |
| 289 | */ |
| 290 | void FbxGeometryLoader::recurseNodes() |
| 291 | { |
| 292 | Q_ASSERT(m_scene); |
| 293 | |
| 294 | if (!m_scene) |
| 295 | return; |
| 296 | |
| 297 | FbxNode *node = m_scene->GetRootNode(); |
| 298 | if (node) { |
| 299 | for (int i = 0; i < node->GetChildCount() && !m_geometry; ++i) |
| 300 | processNode(node->GetChild(i)); |
| 301 | } |
| 302 | } |
| 303 | /*! |
| 304 | If the parameter \a node has the attribute eMesh, |
| 305 | process the Mesh. If not, process the children |
| 306 | of that node. |
| 307 | */ |
| 308 | void Qt3DRender::FbxGeometryLoader::processNode(FbxNode *node) |
| 309 | { |
| 310 | auto attr = node->GetNodeAttribute(); |
| 311 | if (!attr) |
| 312 | return; |
| 313 | |
| 314 | switch (attr->GetAttributeType()) { |
| 315 | case FbxNodeAttribute::eMesh: |
| 316 | if (m_mesh.isEmpty() || m_mesh.compare(node->GetName(), Qt::CaseInsensitive) == 0) { |
| 317 | qDebug(FbxGeometryLoaderLog, "Found mesh: %s", node->GetName()); |
| 318 | processMesh(node->GetMesh()); |
| 319 | } |
| 320 | break; |
| 321 | default: |
| 322 | break; |
| 323 | } |
| 324 | |
| 325 | if (m_geometry) |
| 326 | return; |
| 327 | |
| 328 | for (int i = 0; i < node->GetChildCount(); ++i) |
| 329 | processNode(node->GetChild(i)); |
| 330 | } |
| 331 | /*! |
| 332 | Process all vertices of the specified \a mesh. |
| 333 | */ |
| 334 | void Qt3DRender::FbxGeometryLoader::processMesh(FbxMesh *mesh) |
| 335 | { |
| 336 | const int normalCount = mesh->GetElementNormalCount(); |
| 337 | const int polygonCount = mesh->GetPolygonCount(); |
| 338 | const int tangentCount = mesh->GetElementTangentCount(); |
| 339 | const int uvCount = mesh->GetElementUVCount(); |
| 340 | |
| 341 | const bool hasNormal = (normalCount > 0); |
| 342 | const bool hasTangent = (tangentCount > 0); |
| 343 | const bool hasUV = (uvCount > 0); |
| 344 | |
| 345 | const unsigned int elementSize = 3 + (hasUV ? 2 : 0) + (hasNormal ? 3 : 0) + (hasTangent ? 4 : 0); |
| 346 | const unsigned int elementBytes = elementSize * sizeof(double); |
| 347 | |
| 348 | int vertexCount = 0; |
| 349 | for (int polygonIndex = 0; polygonIndex < polygonCount; ++polygonIndex) |
| 350 | vertexCount += mesh->GetPolygonSize(polygonIndex); |
| 351 | const int indexCount = (polygonCount * 3) + ((vertexCount - (polygonCount * 3)) * 3); |
| 352 | |
| 353 | QByteArray indexPayload; |
| 354 | indexPayload.resize(indexCount * sizeof(quint32)); |
| 355 | quint32 *indexData = reinterpret_cast<quint32 *>(indexPayload.data()); |
| 356 | |
| 357 | QByteArray vertexPayload; |
| 358 | vertexPayload.resize(vertexCount * elementBytes); |
| 359 | double *vertexData = reinterpret_cast<double *>(vertexPayload.data()); |
| 360 | |
| 361 | const FbxVector4 *controlPoints = mesh->GetControlPoints(); |
| 362 | |
| 363 | int vertexIndex = 0; |
| 364 | for (int polygonIndex = 0; polygonIndex < polygonCount; ++polygonIndex) { |
| 365 | const int polygonSize = mesh->GetPolygonSize(polygonIndex); |
| 366 | for (int pVertexIndex = 0; pVertexIndex < polygonSize; ++pVertexIndex) { |
| 367 | const int controlPointIndex = mesh->GetPolygonVertex(polygonIndex, pVertexIndex); |
| 368 | const FbxVector4 *vertex = (controlPoints + controlPointIndex); |
| 369 | *vertexData++ = (*vertex)[0]; |
| 370 | *vertexData++ = (*vertex)[1]; |
| 371 | *vertexData++ = (*vertex)[2]; |
| 372 | |
| 373 | if (pVertexIndex >= 1 && pVertexIndex < (polygonSize - 1)) { |
| 374 | *indexData++ = vertexIndex - pVertexIndex; |
| 375 | *indexData++ = vertexIndex; |
| 376 | *indexData++ = vertexIndex + 1; |
| 377 | } |
| 378 | |
| 379 | if (hasUV) { |
| 380 | FbxVector2 vector; |
| 381 | bool unmapped; |
| 382 | |
| 383 | if (mesh->GetPolygonVertexUV(polygonIndex, pVertexIndex, NULL, vector, unmapped)) { |
| 384 | *vertexData++ = unmapped ? 0 : vector[0]; |
| 385 | *vertexData++ = unmapped ? 0 : vector[1]; |
| 386 | } else { |
| 387 | qWarning(FbxGeometryLoaderLog, |
| 388 | "Irregularity encountered while parsing UV element."); |
| 389 | *vertexData++ = 0; |
| 390 | *vertexData++ = 0; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (hasNormal) { |
| 395 | FbxVector4 vector; |
| 396 | if (mesh->GetPolygonVertexNormal(polygonIndex, pVertexIndex, vector)) { |
| 397 | *vertexData++ = vector[0]; |
| 398 | *vertexData++ = vector[1]; |
| 399 | *vertexData++ = vector[2]; |
| 400 | } else { |
| 401 | qWarning(FbxGeometryLoaderLog, |
| 402 | "Irregularity encountered while parsing Normal element."); |
| 403 | *vertexData++ = 0; |
| 404 | *vertexData++ = 0; |
| 405 | *vertexData++ = 0; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (hasTangent) { |
| 410 | int index = -1; |
| 411 | |
| 412 | for (int tangentIndex = 0; tangentIndex < tangentCount && index == -1; ++tangentIndex) { |
| 413 | const FbxGeometryElementTangent *tangent = mesh->GetElementTangent(tangentIndex); |
| 414 | |
| 415 | if (tangent->GetMappingMode() == FbxGeometryElement::eByPolygonVertex) { |
| 416 | switch (tangent->GetReferenceMode()) { |
| 417 | case FbxGeometryElement::eDirect: |
| 418 | index = vertexIndex; |
| 419 | break; |
| 420 | case FbxGeometryElement::eIndexToDirect: |
| 421 | index = tangent->GetIndexArray().GetAt(vertexIndex); |
| 422 | break; |
| 423 | default: break; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if (index != -1) { |
| 428 | const FbxVector4 vector = tangent->GetDirectArray().GetAt(index); |
| 429 | *vertexData++ = vector[0]; |
| 430 | *vertexData++ = vector[1]; |
| 431 | *vertexData++ = vector[2]; |
| 432 | *vertexData++ = vector[3]; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if (index == -1) { |
| 437 | qWarning(FbxGeometryLoaderLog, |
| 438 | "Irregularity encountered while parsing Tangent element."); |
| 439 | *vertexData++ = 0; |
| 440 | *vertexData++ = 0; |
| 441 | *vertexData++ = 0; |
| 442 | *vertexData++ = 0; |
| 443 | } |
| 444 | } |
| 445 | ++vertexIndex; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * QGeometry Generation |
| 451 | */ |
| 452 | m_geometry = new QGeometry(); |
| 453 | |
| 454 | unsigned int offset = 0; |
| 455 | |
| 456 | QBuffer *vertexBuffer = new QBuffer(QBuffer::VertexBuffer); |
| 457 | vertexBuffer->setData(vertexPayload); |
| 458 | |
| 459 | QBuffer *indexBuffer = new QBuffer(QBuffer::IndexBuffer); |
| 460 | indexBuffer->setData(indexPayload); |
| 461 | |
| 462 | QAttribute *positionAttribute = new QAttribute(vertexBuffer, QAttribute::defaultPositionAttributeName(), QAttribute::Double, 3, vertexCount, offset, elementBytes); |
| 463 | m_geometry->addAttribute(positionAttribute); |
| 464 | offset += sizeof(double) * 3; |
| 465 | |
| 466 | if (hasUV) { |
| 467 | QAttribute *attribute = new QAttribute(vertexBuffer, QAttribute::defaultTextureCoordinateAttributeName(), QAttribute::Double, 2, vertexCount, offset, elementBytes); |
| 468 | m_geometry->addAttribute(attribute); |
| 469 | offset += sizeof(double) * 2; |
| 470 | } |
| 471 | |
| 472 | if (hasNormal) { |
| 473 | QAttribute *attribute = new QAttribute(vertexBuffer, QAttribute::defaultNormalAttributeName(), QAttribute::Double, 3, vertexCount, offset, elementBytes); |
| 474 | m_geometry->addAttribute(attribute); |
| 475 | offset += sizeof(double) * 3; |
| 476 | } |
| 477 | |
| 478 | if (hasTangent) { |
| 479 | QAttribute *attribute = new QAttribute(vertexBuffer, QAttribute::defaultTangentAttributeName(),QAttribute::Double, 4, vertexCount, offset, elementBytes); |
| 480 | m_geometry->addAttribute(attribute); |
| 481 | } |
| 482 | |
| 483 | QAttribute *indexAttribute = new QAttribute(indexBuffer, QAttribute::UnsignedInt, 1, indexCount); |
| 484 | indexAttribute->setAttributeType(QAttribute::IndexAttribute); |
| 485 | m_geometry->addAttribute(indexAttribute); |
| 486 | } |
| 487 | |
| 488 | } // namespace Qt3DRender |
| 489 | |
| 490 | QT_END_NAMESPACE |