diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index 64eac6790..4fc042966 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -210,22 +210,60 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene ai_assert(false); } - // Create nodes for the whole scene - std::vector MeshArray; - for (size_t index = 0; index < pModel->m_Objects.size(); ++index ) { - createNodes(pModel, pModel->m_Objects[ index ], pScene->mRootNode, pScene, MeshArray); - } - - // Create mesh pointer buffer for this scene - if (pScene->mNumMeshes > 0) { - pScene->mMeshes = new aiMesh*[ MeshArray.size() ]; - for (size_t index =0; index < MeshArray.size(); ++index ) { - pScene->mMeshes[ index ] = MeshArray[ index ]; + if (pModel->m_Objects.size() > 0) { + // Create nodes for the whole scene + std::vector MeshArray; + for (size_t index = 0; index < pModel->m_Objects.size(); ++index) { + createNodes(pModel, pModel->m_Objects[index], pScene->mRootNode, pScene, MeshArray); } - } - // Create all materials - createMaterials( pModel, pScene ); + // Create mesh pointer buffer for this scene + if (pScene->mNumMeshes > 0) { + pScene->mMeshes = new aiMesh*[MeshArray.size()]; + for (size_t index = 0; index < MeshArray.size(); ++index) { + pScene->mMeshes[index] = MeshArray[index]; + } + } + + // Create all materials + createMaterials(pModel, pScene); + }else { + if (pModel->m_Vertices.empty()) + return; + + aiMesh* mesh = new aiMesh(); + mesh->mPrimitiveTypes = aiPrimitiveType_POINT; + pScene->mRootNode->mNumMeshes = 1; + pScene->mRootNode->mMeshes = new unsigned int[1]; + pScene->mRootNode->mMeshes[0] = 0; + pScene->mMeshes = new aiMesh*[1]; + pScene->mNumMeshes = 1; + pScene->mMeshes[0] = mesh; + + unsigned int n = pModel->m_Vertices.size(); + mesh->mNumVertices = n; + + mesh->mVertices = new aiVector3D[n]; + memcpy_s(mesh->mVertices, n*sizeof(aiVector3D), pModel->m_Vertices.data(), pModel->m_Vertices.size()*sizeof(aiVector3D) ); + + // Allocate memory for attributes + if ( !pModel->m_Normals.empty() ) { + mesh->mNormals = new aiVector3D[n]; + memcpy_s(mesh->mNormals, n*sizeof(aiVector3D), pModel->m_Normals.data(), pModel->m_Normals.size()*sizeof(aiVector3D)); + } + + if ( !pModel->m_VertexColors.empty() ){ + mesh->mColors[0] = new aiColor4D[mesh->mNumVertices]; + for (unsigned int i = 0; i < n; ++i) { + if (i < pModel->m_VertexColors.size() ) { + const aiVector3D& color = pModel->m_VertexColors[i]; + mesh->mColors[0][i] = aiColor4D(color.x, color.y, color.z, 1.0); + }else { + mesh->mColors[0][i] = aiColor4D( 1.0, 1.0, 1.0, 1.0 ); // Any other ideas what to use as default color? + } + } + } + } } // ------------------------------------------------------------------------------------------------ @@ -452,7 +490,7 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel, // Copy all vertex colors if ( !pModel->m_VertexColors.empty()) { - const aiVector3D color = pModel->m_VertexColors[ vertex ]; + const aiVector3D& color = pModel->m_VertexColors[ vertex ]; pMesh->mColors[0][ newIndex ] = aiColor4D(color.x, color.y, color.z, 1.0); }