diff --git a/assimp/glow-extras/assimp/Importer.cc b/assimp/glow-extras/assimp/Importer.cc index 4c242fa46f073263854587ceb96f550f6591ff31..313f99c9de7404715656992fe54ef8f4f5a18d13 100644 --- a/assimp/glow-extras/assimp/Importer.cc +++ b/assimp/glow-extras/assimp/Importer.cc @@ -24,16 +24,16 @@ glow::assimp::Importer::Importer() {} glow::SharedVertexArray glow::assimp::Importer::load(const std::string& filename) { - GLOW_ACTION(); + GLOW_ACTION(); - using namespace assimp; + using namespace assimp; - if (!std::ifstream(filename).good()) - { - error() << "Error loading `" << filename << "' with Assimp."; - error() << " File not found/not readable"; - return nullptr; - } + if (!std::ifstream(filename).good()) + { + error() << "Error loading `" << filename << "' with Assimp."; + error() << " File not found/not readable"; + return nullptr; + } uint32_t flags = aiProcess_SortByPType; @@ -47,187 +47,187 @@ glow::SharedVertexArray glow::assimp::Importer::load(const std::string& filename flags |= aiProcess_GenUVCoords; if (mPreTransformVertices) flags |= aiProcess_PreTransformVertices; - if (mFlipUVCoords) - flags |= aiProcess_FlipUVs; + if (mFlipUVCoords) + flags |= aiProcess_FlipUVs; - return LoadAndCreateMesh(filename, flags); + return LoadAndCreateMesh(filename, flags); } glow::SharedVertexArray glow::assimp::Importer::LoadAndCreateMesh(std::string const & filename, uint32_t flags) { - MeshData data = LoadData(filename, flags); - return CreateMeshFromData(data); + MeshData data = LoadData(filename, flags); + return CreateMeshFromData(data); } glow::SharedVertexArray glow::assimp::Importer::CreateMeshFromData(const MeshData & data) { - std::vector<SharedArrayBuffer> abs; - - if (!data.positions.empty()) - { - auto ab = ArrayBuffer::create(); - ab->defineAttribute<glm::vec3>("aPosition"); - ab->bind().setData(data.positions); - abs.push_back(ab); - } - if (!data.normals.empty()) - { - auto ab = ArrayBuffer::create(); - ab->defineAttribute<glm::vec3>("aNormal"); - ab->bind().setData(data.normals); - abs.push_back(ab); - } - if (!data.tangents.empty()) - { - auto ab = ArrayBuffer::create(); - ab->defineAttribute<glm::vec3>("aTangent"); - ab->bind().setData(data.tangents); - abs.push_back(ab); - } - for (auto i = 0u; i < data.colors.size(); ++i) - { - auto ab = ArrayBuffer::create(); - if (i == 0) - ab->defineAttribute<glm::vec4>("aColor"); - else - ab->defineAttribute<glm::vec4>("aColor" + std::to_string(i + 1)); - ab->bind().setData(data.colors[i]); - abs.push_back(ab); - } - for (auto i = 0u; i < data.texCoords.size(); ++i) - { - auto ab = ArrayBuffer::create(); - if (i == 0) - ab->defineAttribute<glm::vec2>("aTexCoord"); - else - ab->defineAttribute<glm::vec2>("aTexCoord" + std::to_string(i + 1)); - ab->bind().setData(data.texCoords[i]); - abs.push_back(ab); - } - - for (auto const& ab : abs) - ab->setObjectLabel(ab->getAttributes()[0].name + " of " + data.filename); - - auto eab = ElementArrayBuffer::create(data.indices); - eab->setObjectLabel(data.filename); - auto va = VertexArray::create(abs, eab, GL_TRIANGLES); - va->setObjectLabel(data.filename); - return va; + std::vector<SharedArrayBuffer> abs; + + if (!data.positions.empty()) + { + auto ab = ArrayBuffer::create(); + ab->defineAttribute<glm::vec3>("aPosition"); + ab->bind().setData(data.positions); + abs.push_back(ab); + } + if (!data.normals.empty()) + { + auto ab = ArrayBuffer::create(); + ab->defineAttribute<glm::vec3>("aNormal"); + ab->bind().setData(data.normals); + abs.push_back(ab); + } + if (!data.tangents.empty()) + { + auto ab = ArrayBuffer::create(); + ab->defineAttribute<glm::vec3>("aTangent"); + ab->bind().setData(data.tangents); + abs.push_back(ab); + } + for (auto i = 0u; i < data.colors.size(); ++i) + { + auto ab = ArrayBuffer::create(); + if (i == 0) + ab->defineAttribute<glm::vec4>("aColor"); + else + ab->defineAttribute<glm::vec4>("aColor" + std::to_string(i + 1)); + ab->bind().setData(data.colors[i]); + abs.push_back(ab); + } + for (auto i = 0u; i < data.texCoords.size(); ++i) + { + auto ab = ArrayBuffer::create(); + if (i == 0) + ab->defineAttribute<glm::vec2>("aTexCoord"); + else + ab->defineAttribute<glm::vec2>("aTexCoord" + std::to_string(i + 1)); + ab->bind().setData(data.texCoords[i]); + abs.push_back(ab); + } + + for (auto const& ab : abs) + ab->setObjectLabel(ab->getAttributes()[0].name + " of " + data.filename); + + auto eab = ElementArrayBuffer::create(data.indices); + eab->setObjectLabel(data.filename); + auto va = VertexArray::create(abs, eab, GL_TRIANGLES); + va->setObjectLabel(data.filename); + return va; } glow::assimp::Importer::MeshData glow::assimp::Importer::loadDataCustom(std::string const & filename) { - uint32_t flags = aiProcess_SortByPType; - - if (mTriangulate) - flags |= aiProcess_Triangulate; - if (mCalculateTangents) - flags |= aiProcess_CalcTangentSpace; - if (mGenerateSmoothNormal) - flags |= aiProcess_GenSmoothNormals; - if (mGenerateUVCoords) - flags |= aiProcess_GenUVCoords; - if (mPreTransformVertices) - flags |= aiProcess_PreTransformVertices; - if (mFlipUVCoords) - flags |= aiProcess_FlipUVs; - - return LoadData(filename, flags); + uint32_t flags = aiProcess_SortByPType; + + if (mTriangulate) + flags |= aiProcess_Triangulate; + if (mCalculateTangents) + flags |= aiProcess_CalcTangentSpace; + if (mGenerateSmoothNormal) + flags |= aiProcess_GenSmoothNormals; + if (mGenerateUVCoords) + flags |= aiProcess_GenUVCoords; + if (mPreTransformVertices) + flags |= aiProcess_PreTransformVertices; + if (mFlipUVCoords) + flags |= aiProcess_FlipUVs; + + return LoadData(filename, flags); } glow::assimp::Importer::MeshData glow::assimp::Importer::LoadData(std::string const & filename, uint32_t flags) { - MeshData data; - - GLOW_ACTION(); - - using namespace assimp; - - if (!std::ifstream(filename).good()) - { - error() << "Error loading `" << filename << "' with Assimp."; - error() << " File not found/not readable"; - return data; - } - - Assimp::Importer importer; - auto scene = importer.ReadFile(filename, flags); - - if (!scene) - { - error() << "Error loading `" << filename << "' with Assimp."; - error() << " " << importer.GetErrorString(); - return data; - } - - if (!scene->HasMeshes()) - { - error() << "File `" << filename << "' has no meshes."; - return data; - } - - auto baseIdx = 0u; - for (auto i = 0u; i < scene->mNumMeshes; ++i) - { - auto const& mesh = scene->mMeshes[i]; - auto colorsCnt = mesh->GetNumColorChannels(); - auto texCoordsCnt = mesh->GetNumUVChannels(); - - if (data.texCoords.empty()) - data.texCoords.resize(texCoordsCnt); - else if (data.texCoords.size() != texCoordsCnt) - { - error() << "File `" << filename << "':"; - error() << " contains inconsistent texture coordinate counts"; - return data; - } - - if (data.colors.empty()) - data.colors.resize(colorsCnt); - else if (data.colors.size() != colorsCnt) - { - error() << "File `" << filename << "':"; - error() << " contains inconsistent vertex color counts"; - return data; - } - - // add faces - auto fCnt = mesh->mNumFaces; - for (auto f = 0u; f < fCnt; ++f) - { - auto const& face = mesh->mFaces[f]; - if (face.mNumIndices != 3) - { - error() << "File `" << filename << "':."; - error() << " non-3 faces not implemented/supported"; - return data; - } - for (auto fi = 0u; fi < face.mNumIndices; ++fi) - { - data.indices.push_back(baseIdx + face.mIndices[fi]); - } - } - - // add vertices - auto vCnt = mesh->mNumVertices; - for (auto v = 0u; v < vCnt; ++v) - { - data.positions.push_back(aiCast(mesh->mVertices[v])); - - if (mesh->HasNormals()) - data.normals.push_back(aiCast(mesh->mNormals[v])); - if (mesh->HasTangentsAndBitangents()) - data.tangents.push_back(aiCast(mesh->mTangents[v])); - - for (auto t = 0u; t < texCoordsCnt; ++t) - data.texCoords[t].push_back((glm::vec2)aiCast(mesh->mTextureCoords[t][v])); - for (auto t = 0u; t < colorsCnt; ++t) - data.colors[t].push_back(aiCast(mesh->mColors[t][v])); - } - - baseIdx = data.positions.size(); - } - - data.filename = filename; - return data; + MeshData data; + + GLOW_ACTION(); + + using namespace assimp; + + if (!std::ifstream(filename).good()) + { + error() << "Error loading `" << filename << "' with Assimp."; + error() << " File not found/not readable"; + return data; + } + + Assimp::Importer importer; + auto scene = importer.ReadFile(filename, flags); + + if (!scene) + { + error() << "Error loading `" << filename << "' with Assimp."; + error() << " " << importer.GetErrorString(); + return data; + } + + if (!scene->HasMeshes()) + { + error() << "File `" << filename << "' has no meshes."; + return data; + } + + auto baseIdx = 0u; + for (auto i = 0u; i < scene->mNumMeshes; ++i) + { + auto const& mesh = scene->mMeshes[i]; + auto colorsCnt = mesh->GetNumColorChannels(); + auto texCoordsCnt = mesh->GetNumUVChannels(); + + if (data.texCoords.empty()) + data.texCoords.resize(texCoordsCnt); + else if (data.texCoords.size() != texCoordsCnt) + { + error() << "File `" << filename << "':"; + error() << " contains inconsistent texture coordinate counts"; + return data; + } + + if (data.colors.empty()) + data.colors.resize(colorsCnt); + else if (data.colors.size() != colorsCnt) + { + error() << "File `" << filename << "':"; + error() << " contains inconsistent vertex color counts"; + return data; + } + + // add faces + auto fCnt = mesh->mNumFaces; + for (auto f = 0u; f < fCnt; ++f) + { + auto const& face = mesh->mFaces[f]; + if (face.mNumIndices != 3) + { + error() << "File `" << filename << "':."; + error() << " non-3 faces not implemented/supported"; + return data; + } + for (auto fi = 0u; fi < face.mNumIndices; ++fi) + { + data.indices.push_back(baseIdx + face.mIndices[fi]); + } + } + + // add vertices + auto vCnt = mesh->mNumVertices; + for (auto v = 0u; v < vCnt; ++v) + { + data.positions.push_back(aiCast(mesh->mVertices[v])); + + if (mesh->HasNormals()) + data.normals.push_back(aiCast(mesh->mNormals[v])); + if (mesh->HasTangentsAndBitangents()) + data.tangents.push_back(aiCast(mesh->mTangents[v])); + + for (auto t = 0u; t < texCoordsCnt; ++t) + data.texCoords[t].push_back((glm::vec2)aiCast(mesh->mTextureCoords[t][v])); + for (auto t = 0u; t < colorsCnt; ++t) + data.colors[t].push_back(aiCast(mesh->mColors[t][v])); + } + + baseIdx = data.positions.size(); + } + + data.filename = filename; + return data; } diff --git a/assimp/glow-extras/assimp/Importer.hh b/assimp/glow-extras/assimp/Importer.hh index 90ca158d0357d4d40ee43fb46d30a40e3ab0dc80..746ba1537be85ef041c8093718c90b21b588f3e1 100644 --- a/assimp/glow-extras/assimp/Importer.hh +++ b/assimp/glow-extras/assimp/Importer.hh @@ -18,12 +18,12 @@ namespace assimp { constexpr uint32_t importDefaultFlags = - aiProcess_SortByPType | - aiProcess_Triangulate | - aiProcess_CalcTangentSpace | - aiProcess_GenSmoothNormals | - aiProcess_GenUVCoords | - aiProcess_PreTransformVertices; + aiProcess_SortByPType | + aiProcess_Triangulate | + aiProcess_CalcTangentSpace | + aiProcess_GenSmoothNormals | + aiProcess_GenUVCoords | + aiProcess_PreTransformVertices; constexpr uint32_t importFlipUVFlags = importDefaultFlags | aiProcess_FlipUVs; @@ -66,7 +66,7 @@ private: // settings /// vec2 aTexCoord is available (hopefully) bool mGenerateUVCoords = true; - bool mFlipUVCoords = false; + bool mFlipUVCoords = false; public: GLOW_PROPERTY(CalculateTangents); @@ -78,91 +78,91 @@ public: public: - struct MeshData - { - std::vector<glm::vec3> positions; - std::vector<glm::vec3> normals; - std::vector<glm::vec3> tangents; - std::vector<std::vector<glm::vec2>> texCoords; - std::vector<std::vector<glm::vec4>> colors; - std::vector<uint32_t> indices; - std::string filename; - }; + struct MeshData + { + std::vector<glm::vec3> positions; + std::vector<glm::vec3> normals; + std::vector<glm::vec3> tangents; + std::vector<std::vector<glm::vec2>> texCoords; + std::vector<std::vector<glm::vec4>> colors; + std::vector<uint32_t> indices; + std::string filename; + }; Importer(); - /// Importer instance version of LoadAndCreateMesh + /// Importer instance version of LoadAndCreateMesh SharedVertexArray load(std::string const& filename); - /// Importer instance version of LoadData - MeshData loadDataCustom(std::string const& filename); + /// Importer instance version of LoadData + MeshData loadDataCustom(std::string const& filename); public: - /// Loads and creates a VA from a given filename - /// Supported file formats at time of writing this are: - /// - /// COMMON INTERCHANGE FORMATS - /// Autodesk ( .fbx ) - /// Collada ( .dae ) - /// glTF ( .gltf, .glb ) - /// Blender 3D ( .blend ) - /// 3ds Max 3DS ( .3ds ) - /// 3ds Max ASE ( .ase ) - /// Wavefront Object ( .obj ) - /// Industry Foundation Classes (IFC/Step) ( .ifc ) - /// XGL ( .xgl,.zgl ) - /// Stanford Polygon Library ( .ply ) - /// *AutoCAD DXF ( .dxf ) - /// LightWave ( .lwo ) - /// LightWave Scene ( .lws ) - /// Modo ( .lxo ) - /// Stereolithography ( .stl ) - /// DirectX X ( .x ) - /// AC3D ( .ac ) - /// Milkshape 3D ( .ms3d ) - /// * TrueSpace ( .cob,.scn ) - /// - /// MOTION CAPTURE FORMATS - /// Biovision BVH ( .bvh ) - /// * CharacterStudio Motion ( .csm ) - /// GRAPHICS ENGINE FORMATS - /// Ogre XML ( .xml ) - /// Irrlicht Mesh ( .irrmesh ) - /// * Irrlicht Scene ( .irr ) - /// GAME FILE FORMATS - /// Quake I ( .mdl ) - /// Quake II ( .md2 ) - /// Quake III Mesh ( .md3 ) - /// Quake III Map/BSP ( .pk3 ) - /// * Return to Castle Wolfenstein ( .mdc ) - /// Doom 3 ( .md5* ) - /// *Valve Model ( .smd,.vta ) - /// *Open Game Engine Exchange ( .ogex ) - /// *Unreal ( .3d ) - /// - /// OTHER FILE FORMATS - /// BlitzBasic 3D ( .b3d ) - /// Quick3D ( .q3d,.q3s ) - /// Neutral File Format ( .nff ) - /// Sense8 WorldToolKit ( .nff ) - /// Object File Format ( .off ) - /// PovRAY Raw ( .raw ) - /// Terragen Terrain ( .ter ) - /// 3D GameStudio (3DGS) ( .mdl ) - /// 3D GameStudio (3DGS) Terrain ( .hmp ) - /// Izware Nendo ( .ndo ) - /// - /// Each attribute gets their own buffer. - static SharedVertexArray LoadAndCreateMesh(std::string const& filename, uint32_t flags = importDefaultFlags); - - /// Creates a VA from MeshData - /// Requires OGL context and initialized GLOW on the same thread - static SharedVertexArray CreateMeshFromData(const MeshData& data); - - /// Loads MeshData from a given filename with the given importer flags - /// Can be run on any thread - static MeshData LoadData(std::string const& filename, uint32_t flags = importDefaultFlags); + /// Loads and creates a VA from a given filename + /// Supported file formats at time of writing this are: + /// + /// COMMON INTERCHANGE FORMATS + /// Autodesk ( .fbx ) + /// Collada ( .dae ) + /// glTF ( .gltf, .glb ) + /// Blender 3D ( .blend ) + /// 3ds Max 3DS ( .3ds ) + /// 3ds Max ASE ( .ase ) + /// Wavefront Object ( .obj ) + /// Industry Foundation Classes (IFC/Step) ( .ifc ) + /// XGL ( .xgl,.zgl ) + /// Stanford Polygon Library ( .ply ) + /// *AutoCAD DXF ( .dxf ) + /// LightWave ( .lwo ) + /// LightWave Scene ( .lws ) + /// Modo ( .lxo ) + /// Stereolithography ( .stl ) + /// DirectX X ( .x ) + /// AC3D ( .ac ) + /// Milkshape 3D ( .ms3d ) + /// * TrueSpace ( .cob,.scn ) + /// + /// MOTION CAPTURE FORMATS + /// Biovision BVH ( .bvh ) + /// * CharacterStudio Motion ( .csm ) + /// GRAPHICS ENGINE FORMATS + /// Ogre XML ( .xml ) + /// Irrlicht Mesh ( .irrmesh ) + /// * Irrlicht Scene ( .irr ) + /// GAME FILE FORMATS + /// Quake I ( .mdl ) + /// Quake II ( .md2 ) + /// Quake III Mesh ( .md3 ) + /// Quake III Map/BSP ( .pk3 ) + /// * Return to Castle Wolfenstein ( .mdc ) + /// Doom 3 ( .md5* ) + /// *Valve Model ( .smd,.vta ) + /// *Open Game Engine Exchange ( .ogex ) + /// *Unreal ( .3d ) + /// + /// OTHER FILE FORMATS + /// BlitzBasic 3D ( .b3d ) + /// Quick3D ( .q3d,.q3s ) + /// Neutral File Format ( .nff ) + /// Sense8 WorldToolKit ( .nff ) + /// Object File Format ( .off ) + /// PovRAY Raw ( .raw ) + /// Terragen Terrain ( .ter ) + /// 3D GameStudio (3DGS) ( .mdl ) + /// 3D GameStudio (3DGS) Terrain ( .hmp ) + /// Izware Nendo ( .ndo ) + /// + /// Each attribute gets their own buffer. + static SharedVertexArray LoadAndCreateMesh(std::string const& filename, uint32_t flags = importDefaultFlags); + + /// Creates a VA from MeshData + /// Requires OGL context and initialized GLOW on the same thread + static SharedVertexArray CreateMeshFromData(const MeshData& data); + + /// Loads MeshData from a given filename with the given importer flags + /// Can be run on any thread + static MeshData LoadData(std::string const& filename, uint32_t flags = importDefaultFlags); }; } -}; \ No newline at end of file +}