diff --git a/SHADE_Engine/src/Assets/Asset Types/SHMeshAsset.h b/SHADE_Engine/src/Assets/Asset Types/SHMeshAsset.h index 18674a04..68c0d150 100644 --- a/SHADE_Engine/src/Assets/Asset Types/SHMeshAsset.h +++ b/SHADE_Engine/src/Assets/Asset Types/SHMeshAsset.h @@ -1,3 +1,15 @@ +/*************************************************************************//** + * \file SHMeshAsset.h + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Struct to contain ready data for loading into GPU. Also used for + * compilation into binary files + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #pragma once #include @@ -10,6 +22,7 @@ namespace SHADE { uint32_t vertexCount; uint32_t indexCount; + std::string meshName; }; struct SH_API SHMeshAsset @@ -19,8 +32,6 @@ namespace SHADE SHMeshAssetHeader header; - std::string meshName; - std::vector vertexPosition; std::vector vertexTangent; std::vector vertexNormal; diff --git a/SHADE_Engine/src/Assets/Asset Types/SHTextureAsset.h b/SHADE_Engine/src/Assets/Asset Types/SHTextureAsset.h index d3b69e32..94b1b74c 100644 --- a/SHADE_Engine/src/Assets/Asset Types/SHTextureAsset.h +++ b/SHADE_Engine/src/Assets/Asset Types/SHTextureAsset.h @@ -1,11 +1,8 @@ #pragma once #include "tinyddsloader.h" - #include "Graphics/MiddleEnd/Textures/SHTextureLibrary.h" -#include - namespace SHADE { struct SHTextureAsset diff --git a/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.cpp b/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.cpp index 1a9dede2..6d4b6fcb 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.cpp +++ b/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.cpp @@ -1,5 +1,18 @@ +/*************************************************************************//** + * \file SHMeshCompiler.cpp + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Library to write data in SHMeshAsset into binary file for faster + * loading in the future + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #include "SHpch.h" #include "SHMeshCompiler.h" +#include "Graphics/MiddleEnd/Meshes/SHMeshData.h" #include @@ -21,6 +34,17 @@ void SHADE::SHMeshCompiler::CompileMeshBinary(SHMeshAsset const& asset, AssetPat sizeof(uint32_t) ); + uint32_t charCount{static_cast(asset.header.meshName.size())}; + file.write( + reinterpret_cast(&charCount), + sizeof(uint32_t) + ); + + file.write( + asset.header.meshName.c_str(), + asset.header.meshName.size() + ); + auto const vertexVec3Byte {sizeof(SHVec3) * asset.header.vertexCount}; auto const vertexVec2Byte {sizeof(SHVec2) * asset.header.vertexCount}; @@ -44,5 +68,10 @@ void SHADE::SHMeshCompiler::CompileMeshBinary(SHMeshAsset const& asset, AssetPat vertexVec2Byte ); + file.write( + reinterpret_cast(asset.indices.data()), + sizeof(uint32_t) + ); + file.close(); } diff --git a/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.h b/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.h index 090e241e..6da00525 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.h +++ b/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.h @@ -1,3 +1,15 @@ +/*************************************************************************//** + * \file SHMeshCompiler.h + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Library to write data in SHMeshAsset into binary file for faster + * loading in the future + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #pragma once #include "../Asset Types/SHMeshAsset.h" diff --git a/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.cpp b/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.cpp index fc4ff435..a1a8b29c 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.cpp +++ b/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.cpp @@ -1,6 +1,19 @@ +/*************************************************************************//** + * \file SHMeshLoader.cpp + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Implementation for Mesh loader. Accounts for custom binary format + * as well as GLTF file format. + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #include "SHpch.h" #include "SHMeshLoader.h" #include +#include namespace SHADE { @@ -27,10 +40,11 @@ namespace SHADE SHMeshAsset result { .compiled { false}, - .changed { false }, - .meshName { mesh.mName.C_Str() } + .changed { false } }; + result.header.meshName = mesh.mName.C_Str(); + for (size_t i{0}; i < mesh.mNumVertices; ++i) { // Vertex position @@ -85,15 +99,15 @@ namespace SHADE return result; } - bool SHMeshLoader::LoadMesh(std::vector& meshes, AssetPath path) - { + void SHMeshLoader::LoadExternal(std::vector& meshes, AssetPath path) noexcept + { const aiScene* scene = aiImporter.ReadFile(path.string().c_str(), aiProcess_Triangulate // Make sure we get triangles rather than nvert polygons | aiProcess_GenUVCoords // Convert any type of mapping to uv mapping | aiProcess_TransformUVCoords // preprocess UV transformations (scaling, translation ...) | aiProcess_FindInstances // search for instanced meshes and remove them by references to one master | aiProcess_CalcTangentSpace // calculate tangents and bitangents if possible - | aiProcess_JoinIdenticalVertices // join identical vertices/ optimize indexing + | aiProcess_JoinIdenticalVertices // join identical vertices/ optimize indexing | aiProcess_RemoveRedundantMaterials // remove redundant materials | aiProcess_FindInvalidData // detect invalid model data, such as invalid normal vectors | aiProcess_FlipUVs // flip the V to match the Vulkans way of doing UVs @@ -102,7 +116,7 @@ namespace SHADE if (!scene || !scene->HasMeshes()) { SHLOG_ERROR("ERROR in GLTF::ASSIMP: {}\nFile: {}", aiImporter.GetErrorString(), path.string()); - return false; + return; } //TODO MATERIALS FROM MESHES @@ -112,14 +126,103 @@ namespace SHADE // { // if (scene->mMaterials[i]->mNumProperties > 0) // { - // for (int j{0}; j < scene->mMaterials[i]->mProperties[j].) + // for (int j{0}; j < scene->mMaterials[i]->mProperties[j].) // } - //std::cout << scene->mMaterials[i]->; + //std::cout << scene->mMaterials[i]->; // } //} ProcessNode(*scene->mRootNode, *scene, meshes); + } - return true; + void SHMeshLoader::LoadSHMesh(SHMeshAsset& mesh, AssetPath path) noexcept + { + std::ifstream file{ path.string(), std::ios::in | std::ios::binary }; + if (!file.is_open()) + { + SHLOG_ERROR("Unable to open SHMesh File: {}", path.string()); + } + + uint32_t vertCount, indexCount, charCount; + std::vector vertPos, vertTan, vertNorm; + std::vector texCoord; + std::vector indices; + + std::string name; + + file >> vertCount; + file >> indexCount; + file >> charCount; + + vertPos.resize(vertCount); + vertTan.resize(vertCount); + vertNorm.resize(vertCount); + texCoord.resize(vertCount); + indices.resize(indexCount); + + name.reserve(charCount); + for (auto i{0}; i < charCount; ++i) + { + file >> name[i]; + } + + for (auto i{ 0 }; i < vertCount; ++i) + { + file >> vertPos[i].x; + file >> vertPos[i].y; + file >> vertPos[i].z; + } + + for (auto i{ 0 }; i < vertCount; ++i) + { + file >> vertTan[i].x; + file >> vertTan[i].y; + file >> vertTan[i].z; + } + + for (auto i{ 0 }; i < vertCount; ++i) + { + file >> vertNorm[i].x; + file >> vertNorm[i].y; + file >> vertNorm[i].z; + } + + for (auto i{ 0 }; i < vertCount; ++i) + { + file >> texCoord[i].x; + file >> texCoord[i].y; + } + + for (auto i{ 0 }; i < indexCount; ++i) + { + file >> indices[i]; + } + + mesh.compiled = true; + mesh.changed = false; + + mesh.header.indexCount = indexCount; + mesh.header.vertexCount = vertCount; + mesh.header.meshName = name; + + mesh.vertexPosition = std::move(vertPos); + mesh.vertexTangent = std::move(vertTan); + mesh.vertexNormal = std::move(vertNorm); + mesh.texCoords = std::move(texCoord); + mesh.indices = std::move(indices); + + file.close(); + } + + void SHMeshLoader::LoadMesh(std::vector& meshes, AssetPath path) + { + if (path.extension().string() == GLTF_EXTENSION) + { + LoadExternal(meshes, path); + return; + } + + meshes.emplace_back(); + LoadSHMesh(meshes.back(), path); } } diff --git a/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.h b/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.h index fc8b548a..3e430aca 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.h +++ b/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.h @@ -1,3 +1,14 @@ +/*************************************************************************//** + * \file SHMeshLoader.h + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Library to load gltf mesh files and custom binary format + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #pragma once #include #include @@ -12,10 +23,14 @@ namespace SHADE private: static Assimp::Importer aiImporter; - static void ProcessNode(aiNode const& node, aiScene const& scene, std::vector& meshes); + static void ProcessNode(aiNode const& node, aiScene const& scene, std::vector& meshes) noexcept; - static SHMeshAsset ProcessMesh(aiMesh const& mesh, aiScene const& scene); + static SHMeshAsset ProcessMesh(aiMesh const& mesh, aiScene const& scene) noexcept; + + static void LoadExternal(std::vector& meshes, AssetPath path) noexcept; + + static void LoadSHMesh(SHMeshAsset& meshes, AssetPath path) noexcept; public: - static bool LoadMesh(std::vector& meshes, AssetPath path); + static void LoadMesh(std::vector& meshes, AssetPath path) noexcept; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.cpp b/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.cpp index 342a0aee..a2fc3b77 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.cpp +++ b/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.cpp @@ -1,3 +1,15 @@ +/*************************************************************************//** + * \file SHTextureCompiler.cpp + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Library to write data in SHTextureAsset into binary file for + * faster loading in the future + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #include "SHpch.h" #include "SHTextureCompiler.h" @@ -15,6 +27,8 @@ namespace SHADE auto const intBytes{sizeof(uint32_t)}; + uint32_t mipOffsetCount{ static_cast(asset.mipOffsets.size()) }; + file.write( reinterpret_cast(&asset.numBytes), intBytes @@ -35,6 +49,16 @@ namespace SHADE sizeof(SHTexture::PixelChannel) ); + file.write( + reinterpret_cast(&mipOffsetCount), + intBytes + ); + + file.write( + reinterpret_cast(&asset.numBytes), + intBytes + ); + file.write( reinterpret_cast(asset.mipOffsets.data()), intBytes * asset.mipOffsets.size() diff --git a/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.h b/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.h index 494907f7..d8685795 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.h +++ b/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.h @@ -1,3 +1,15 @@ +/*************************************************************************//** + * \file SHTextureCompiler.h + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Library to write data in SHTextureAsset into binary file for + * faster loading in the future + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #pragma once #include "Assets/Asset Types/SHTextureAsset.h" diff --git a/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.cpp b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.cpp index f70e21d8..68f86030 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.cpp +++ b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.cpp @@ -1,3 +1,14 @@ +/*************************************************************************//** + * \file SHTextureLoader.cpp + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Library to load dds textures and custom binary format + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #include "SHpch.h" #include "SHTextureLoader.h" @@ -98,13 +109,39 @@ namespace SHADE SHLOG_ERROR("Error opening SHTexture file: {}", path.string()); } - VkFormat format; + tinyddsloader::DDSFile::DXGIFormat format; + uint32_t formatCarrier; + uint32_t mipCount; + uint32_t byteCount; file >> asset.numBytes; file >> asset.width; file >> asset.height; - //file >> format; + file >> formatCarrier; + asset.format = ddsLoaderToVkFormat(static_cast(formatCarrier), true); + + file >> mipCount; + file >> byteCount; + + std::vector mips(mipCount); + for (auto i {0}; i < mipCount; ++i) + { + file >> mips[i]; + } + + asset.mipOffsets = std::move(mips); + + auto pixel = new SHTexture::PixelChannel[byteCount]; + auto pixelIt{ pixel }; + + while(!file.eof()) + { + file >> *(pixelIt++); + } + asset.pixelData = std::move( pixel ); + + file.close(); } void SHTextureLoader::LoadImageAsset(AssetPath path, SHTextureAsset& asset) diff --git a/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.h b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.h index ecc35605..e84fe5cf 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.h +++ b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.h @@ -1,3 +1,14 @@ +/*************************************************************************//** + * \file SHTextureLoader.h + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Library to load dds textures and custom binary format + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #pragma once #define TINYDDSLOADER_IMPLEMENTATION diff --git a/SHADE_Engine/src/Assets/SHAsset.h b/SHADE_Engine/src/Assets/SHAsset.h index 0ba2285f..8d7b55d1 100644 --- a/SHADE_Engine/src/Assets/SHAsset.h +++ b/SHADE_Engine/src/Assets/SHAsset.h @@ -1,3 +1,14 @@ +/*************************************************************************//** + * \file SHAsset.h + * \author Loh Xiao Qi + * \date 30 September 2022 + * \brief Struct for asset identification and meta file writing + * + * + * Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or + * disclosure of this file or its contents without the prior written consent + * of DigiPen Institute of Technology is prohibited. + *****************************************************************************/ #pragma once #include "Filesystem/SHFileSystem.h"