diff --git a/Assets/Cube.003.shmesh b/Assets/Cube.003.shmesh new file mode 100644 index 00000000..54cfb867 Binary files /dev/null and b/Assets/Cube.003.shmesh differ diff --git a/Assets/Cube.012.shmesh b/Assets/Cube.012.shmesh new file mode 100644 index 00000000..bc26a4e8 Binary files /dev/null and b/Assets/Cube.012.shmesh differ diff --git a/Assets/RaccoonPreTexturedVer1_Base9.shtex b/Assets/RaccoonPreTexturedVer1_Base9.shtex new file mode 100644 index 00000000..38e0cd0f Binary files /dev/null and b/Assets/RaccoonPreTexturedVer1_Base9.shtex differ diff --git a/SHADE_Application/src/Application/SBApplication.cpp b/SHADE_Application/src/Application/SBApplication.cpp index 3e9b0dce..8abc341b 100644 --- a/SHADE_Application/src/Application/SBApplication.cpp +++ b/SHADE_Application/src/Application/SBApplication.cpp @@ -83,16 +83,17 @@ namespace Sandbox SHADE::SHSystemManager::RegisterRoutine(); //TODO: REMOVE AFTER PRESENTATION - SHADE::SHAssetManager::LoadDataTemp("../../Assets/racoon.gltf"); - SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonBag_Color_Ver4.dds"); - SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonPreTexturedVer1_Base9.dds"); - SHADE::SHAssetManager::LoadDataTemp("../../Assets/TD_Checker_Base_Color.dds"); + //SHADE::SHAssetManager::LoadDataTemp("../../Assets/racoon.gltf"); + SHADE::SHAssetManager::LoadDataTemp("../../Assets/Cube.012.shmesh"); + //SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonBag_Color_Ver4.dds"); + //SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonPreTexturedVer1_Base9.dds"); + SHADE::SHAssetManager::LoadDataTemp("../../Assets/RaccoonPreTexturedVer1_Base9.shtex"); + //TODO: REMOVE AFTER PRESENTATION auto id = SHFamilyID::GetID(); auto id2 = SHFamilyID::GetID(); auto id3 = SHFamilyID::GetID(); - //TODO: REMOVE AFTER PRESENTATION //SHADE::SHSystemManager::RegisterRoutine(); diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index 664497df..6daa3645 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -42,7 +42,7 @@ namespace Sandbox std::vector> handles; for (auto const& mesh : meshes) { - if (mesh.meshName == "Cube.012") + if (mesh.header.meshName == "Cube.012") { handles.push_back(graphicsSystem->AddMesh( mesh.header.vertexCount, @@ -71,7 +71,7 @@ namespace Sandbox auto matInst = graphicsSystem->AddOrGetBaseMaterialInstance(); auto customMat = graphicsSystem->AddMaterialInstanceCopy(matInst); customMat->SetProperty("data.color", SHVec4(0.0f, 1.0f, 1.0f, 1.0f)); - customMat->SetProperty("data.textureIndex", 1); + customMat->SetProperty("data.textureIndex", 0); customMat->SetProperty("data.alpha", 0.1f); // Create Stress Test Objects @@ -115,7 +115,7 @@ namespace Sandbox renderable.SetMaterial(customMat); renderable.GetModifiableMaterial()->SetProperty("data.color", SHVec4(0.0f, 0.0f, 0.0f, 0.0f)); renderable.GetModifiableMaterial()->SetProperty("data.alpha", 1.0f); - renderable.GetModifiableMaterial()->SetProperty("data.textureIndex", 1); + renderable.GetModifiableMaterial()->SetProperty("data.textureIndex", 0); transform.SetWorldPosition ({-3.0f, -1.0f, -1.0f}); transform.SetLocalScale({5.0f, 5.0f, 5.0f}); @@ -147,7 +147,7 @@ namespace Sandbox renderableShowcase.SetMaterial(customMat); renderableShowcase.GetModifiableMaterial()->SetProperty("data.color", SHVec4(0.0f, 0.0f, 0.0f, 0.0f)); renderableShowcase.GetModifiableMaterial()->SetProperty("data.alpha", 1.0f); - renderableShowcase.GetModifiableMaterial()->SetProperty("data.textureIndex", 1); + renderableShowcase.GetModifiableMaterial()->SetProperty("data.textureIndex", 0); transformShowcase.SetWorldPosition({ 3.0f, -1.0f, -1.0f }); transformShowcase.SetLocalScale({ 5.0f, 5.0f, 5.0f }); 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..634d9a9a 100644 --- a/SHADE_Engine/src/Assets/Asset Types/SHTextureAsset.h +++ b/SHADE_Engine/src/Assets/Asset Types/SHTextureAsset.h @@ -1,15 +1,14 @@ #pragma once #include "tinyddsloader.h" - #include "Graphics/MiddleEnd/Textures/SHTextureLibrary.h" -#include - namespace SHADE { struct SHTextureAsset { + bool compiled; + uint32_t numBytes; uint32_t width; uint32_t height; diff --git a/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.cpp b/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.cpp new file mode 100644 index 00000000..12b2517e --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.cpp @@ -0,0 +1,70 @@ +/*************************************************************************//** + * \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 + +void SHADE::SHMeshCompiler::CompileMeshBinary(SHMeshAsset const& asset, AssetPath path) noexcept +{ + std::string newPath{ path.string() }; + newPath = newPath.substr(0, newPath.find_last_of('/') + 1); + newPath += asset.header.meshName + MESH_EXTENSION; + + std::ofstream file{ newPath, std::ios::out | std::ios::binary | std::ios::trunc }; + if (!file.is_open()) + { + SHLOG_ERROR("Unable to open file for writing mesh file: {}", path.string()); + } + + file.write( + reinterpret_cast(&(asset.header.vertexCount)), + sizeof(uint32_t) + ); + + file.write( + reinterpret_cast(&(asset.header.indexCount)), + sizeof(uint32_t) + ); + + auto const vertexVec3Byte {sizeof(SHVec3) * asset.header.vertexCount}; + auto const vertexVec2Byte {sizeof(SHVec2) * asset.header.vertexCount}; + + file.write( + reinterpret_cast(asset.vertexPosition.data()), + vertexVec3Byte + ); + + file.write( + reinterpret_cast(asset.vertexTangent.data()), + vertexVec3Byte + ); + + file.write( + reinterpret_cast(asset.vertexNormal.data()), + vertexVec3Byte + ); + + file.write( + reinterpret_cast(asset.texCoords.data()), + vertexVec2Byte + ); + + file.write( + reinterpret_cast(asset.indices.data()), + sizeof(uint32_t) * asset.header.indexCount + ); + + file.close(); +} diff --git a/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.h b/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.h new file mode 100644 index 00000000..6da00525 --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/SHMeshCompiler.h @@ -0,0 +1,26 @@ +/*************************************************************************//** + * \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" +#include "../SHAssetMacros.h" + +namespace SHADE +{ + class SHMeshCompiler + { + private: + public: + static void CompileMeshBinary(SHMeshAsset const& asset, AssetPath path) noexcept; + }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.cpp b/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.cpp index 3ef454a0..b77d429d 100644 --- a/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.cpp +++ b/SHADE_Engine/src/Assets/Libraries/SHMeshLoader.cpp @@ -1,12 +1,25 @@ +/*************************************************************************//** + * \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 { Assimp::Importer SHMeshLoader::aiImporter; - void SHMeshLoader::ProcessNode(aiNode const& node, aiScene const& scene, std::vector& meshes) + void SHMeshLoader::ProcessNode(aiNode const& node, aiScene const& scene, std::vector& meshes) noexcept { for (size_t i {0}; i < node.mNumMeshes; ++i) { @@ -20,15 +33,14 @@ namespace SHADE } } - SHMeshAsset SHMeshLoader::ProcessMesh(aiMesh const& mesh, aiScene const& scene) + SHMeshAsset SHMeshLoader::ProcessMesh(aiMesh const& mesh, aiScene const& scene) noexcept { (void)scene; SHMeshAsset result { .compiled { false}, - .changed { false }, - .meshName { mesh.mName.C_Str() } + .changed { false } }; for (size_t i{0}; i < mesh.mNumVertices; ++i) @@ -81,34 +93,31 @@ namespace SHADE result.header.vertexCount = result.vertexPosition.size(); result.header.indexCount = result.indices.size(); + result.header.meshName = mesh.mName.C_Str(); 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_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 + 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_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 ); if (!scene || !scene->HasMeshes()) { SHLOG_ERROR("ERROR in GLTF::ASSIMP: {}\nFile: {}", aiImporter.GetErrorString(), path.string()); - return false; + return; } + //TODO MATERIALS FROM MESHES //if (scene->HasMaterials()) //{ @@ -116,14 +125,108 @@ 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()); + } + + std::string name{ path.filename().string() }; + name = name.substr(0, name.find_last_of('.')); + + file.seekg(0); + + uint32_t vertCount, indexCount; + std::vector vertPos, vertTan, vertNorm; + std::vector texCoord; + std::vector indices; + + file.read(reinterpret_cast(&vertCount), sizeof(uint32_t)); + file.read(reinterpret_cast(&indexCount), sizeof(uint32_t)); + + auto const vertexVec3Byte{ sizeof(SHVec3) * vertCount }; + auto const vertexVec2Byte{ sizeof(SHVec2) * vertCount }; + + vertPos.resize(vertCount); + vertTan.resize(vertCount); + vertNorm.resize(vertCount); + texCoord.resize(vertCount); + indices.resize(indexCount); + + file.read(reinterpret_cast(vertPos.data()), vertexVec3Byte); + file.read(reinterpret_cast(vertTan.data()), vertexVec3Byte); + file.read(reinterpret_cast(vertNorm.data()), vertexVec3Byte); + file.read(reinterpret_cast(texCoord.data()), vertexVec2Byte); + file.read(reinterpret_cast(indices.data()), sizeof(uint32_t) * indexCount); + + //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) noexcept + { + 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/SHMeshWriter.cpp b/SHADE_Engine/src/Assets/Libraries/SHMeshWriter.cpp deleted file mode 100644 index 965e7d68..00000000 --- a/SHADE_Engine/src/Assets/Libraries/SHMeshWriter.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "SHpch.h" -#include "SHMeshWriter.h" - -#include - -void SHADE::SHMeshWriter::WriteMeshBinary(SHMeshAsset const& asset, AssetPath path) noexcept -{ - std::ofstream file{path, std::ios::out | std::ios::binary}; - if (!file.is_open()) - { - SHLOG_ERROR("Unable to open file for writing mesh file: {}", path.string()); - } - - file.write( - reinterpret_cast(&(asset.header.vertexCount)), - sizeof(uint32_t) - ); - - file.write( - reinterpret_cast(&(asset.header.indexCount)), - sizeof(uint32_t) - ); - - auto const vertexVec3Byte {sizeof(SHVec3) * asset.header.vertexCount}; - auto const vertexVec2Byte {sizeof(SHVec2) * asset.header.vertexCount}; - - file.write( - reinterpret_cast(asset.vertexPosition.data()), - vertexVec3Byte - ); - - file.write( - reinterpret_cast(asset.vertexTangent.data()), - vertexVec3Byte - ); - - file.write( - reinterpret_cast(asset.vertexNormal.data()), - vertexVec3Byte - ); - - file.write( - reinterpret_cast(asset.texCoords.data()), - vertexVec2Byte - ); - - file.close(); -} diff --git a/SHADE_Engine/src/Assets/Libraries/SHMeshWriter.h b/SHADE_Engine/src/Assets/Libraries/SHMeshWriter.h deleted file mode 100644 index 4a1a3e6b..00000000 --- a/SHADE_Engine/src/Assets/Libraries/SHMeshWriter.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#include "../Asset Types/SHMeshAsset.h" -#include "../SHAssetMacros.h" - -namespace SHADE -{ - class SHMeshWriter - { - private: - public: - static void WriteMeshBinary(SHMeshAsset const& asset, 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 new file mode 100644 index 00000000..62af4da6 --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.cpp @@ -0,0 +1,73 @@ +/*************************************************************************//** + * \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" + +#include + +namespace SHADE +{ + void SHTextureCompiler::CompileTextureBinary(SHTextureAsset const& asset, AssetPath path) + { + std::string newPath{ path.string() }; + newPath = newPath.substr(0, newPath.find_last_of('.')); + newPath += TEXTURE_EXTENSION; + + std::ofstream file{ newPath, std::ios::out | std::ios::binary }; + if (!file.is_open()) + { + SHLOG_ERROR("Unable to open file for writing texture file: {}", path.string()); + } + + auto const intBytes{sizeof(uint32_t)}; + + uint32_t mipOffsetCount{ static_cast(asset.mipOffsets.size()) }; + + file.write( + reinterpret_cast(&asset.numBytes), + intBytes + ); + + file.write( + reinterpret_cast(&asset.width), + intBytes + ); + + file.write( + reinterpret_cast(&asset.height), + intBytes + ); + + file.write( + reinterpret_cast(&asset.format), + sizeof(SHTexture::TextureFormat) + ); + + file.write( + reinterpret_cast(&mipOffsetCount), + intBytes + ); + + file.write( + reinterpret_cast(asset.mipOffsets.data()), + intBytes * asset.mipOffsets.size() + ); + + file.write( + reinterpret_cast(asset.pixelData), + asset.numBytes + ); + + file.close(); + } +} diff --git a/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.h b/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.h new file mode 100644 index 00000000..d8685795 --- /dev/null +++ b/SHADE_Engine/src/Assets/Libraries/SHTextureCompiler.h @@ -0,0 +1,24 @@ +/*************************************************************************//** + * \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" +#include "Assets/SHAssetMacros.h" + +namespace SHADE +{ + struct SHTextureCompiler + { + static void CompileTextureBinary(SHTextureAsset const& asset, AssetPath path); + }; +} \ No newline at end of file diff --git a/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.cpp b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.cpp index 4f510c0a..1047cdc6 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" @@ -58,7 +69,7 @@ namespace SHADE } } - void SHTextureLoader::LoadImageAsset(AssetPath path, SHTextureAsset& asset) + void SHTextureLoader::LoadTinyDDS(AssetPath path, SHTextureAsset& asset) noexcept { tinyddsloader::Result loadResult = tinyddsloader::Result::Success; tinyddsloader::DDSFile file; @@ -82,6 +93,7 @@ namespace SHADE std::memcpy(pixel, file.GetImageData()->m_mem, totalBytes); //pixel = std::move(reinterpret_cast(file.GetDDSData())); + asset.compiled = false; asset.numBytes = totalBytes; asset.width = file.GetWidth(); asset.height = file.GetHeight(); @@ -89,4 +101,46 @@ namespace SHADE asset.mipOffsets = std::move(mipOff); asset.pixelData = std::move(pixel); } + + void SHTextureLoader::LoadSHTexture(AssetPath path, SHTextureAsset& asset) noexcept + { + std::ifstream file{path.string(), std::ios::in | std::ios::binary}; + if (!file.is_open()) + { + SHLOG_ERROR("Error opening SHTexture file: {}", path.string()); + } + + auto const intBytes{ sizeof(uint32_t) }; + uint32_t mipCount; + + file.read(reinterpret_cast(&asset.numBytes), intBytes); + file.read(reinterpret_cast(&asset.width), intBytes); + file.read(reinterpret_cast(&asset.height), intBytes); + file.read(reinterpret_cast(&asset.format), sizeof(SHTexture::TextureFormat)); + + file.read(reinterpret_cast(&mipCount), intBytes); + std::vector mips(mipCount); + file.read(reinterpret_cast(mips.data()), intBytes * mipCount); + + auto pixel = new SHTexture::PixelChannel[asset.numBytes]; + file.read(reinterpret_cast(pixel), asset.numBytes); + + asset.mipOffsets = std::move(mips); + asset.pixelData = std::move( pixel ); + + asset.compiled = true; + file.close(); + } + + void SHTextureLoader::LoadImageAsset(AssetPath path, SHTextureAsset& asset) + { + if (path.extension().string() == DDS_EXTENSION) + { + LoadTinyDDS(path, asset); + } + else if (path.extension().string() == TEXTURE_EXTENSION) + { + LoadSHTexture(path, asset); + } + } } diff --git a/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.h b/SHADE_Engine/src/Assets/Libraries/SHTextureLoader.h index 281a9a19..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 @@ -13,6 +24,9 @@ namespace SHADE static std::string TinyDDSResultToString(tinyddsloader::Result value); static vk::Format ddsLoaderToVkFormat(tinyddsloader::DDSFile::DXGIFormat format, bool isLinear); + + static void LoadTinyDDS(AssetPath path, SHTextureAsset& asset) noexcept; + static void LoadSHTexture(AssetPath path, SHTextureAsset& asset) noexcept; public: static void LoadImageAsset(AssetPath paths, SHTextureAsset& image); }; 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" diff --git a/SHADE_Engine/src/Assets/SHAssetManager.cpp b/SHADE_Engine/src/Assets/SHAssetManager.cpp index 60e35ab3..989cd2ad 100644 --- a/SHADE_Engine/src/Assets/SHAssetManager.cpp +++ b/SHADE_Engine/src/Assets/SHAssetManager.cpp @@ -17,6 +17,9 @@ #include "Libraries/SHMeshLoader.h" #include "Libraries/SHTextureLoader.h" +#include "Libraries/SHMeshCompiler.h" +#include "Libraries/SHTextureCompiler.h" + namespace SHADE { FMOD::System* SHAssetManager::audioSystem; @@ -199,7 +202,9 @@ namespace SHADE { AssetPath path{ p }; - if (path.extension().string() == GLTF_EXTENSION) + if (path.extension().string() == FBX_EXTENSION + || path.extension().string() == GLTF_EXTENSION + || path.extension().string() == MESH_EXTENSION) { LoadGLTF( { @@ -211,7 +216,8 @@ namespace SHADE } ); } - else if (path.extension().string() == DDS_EXTENSION) + else if (path.extension().string() == DDS_EXTENSION + || path.extension().string() == TEXTURE_EXTENSION) { LoadDDS( { @@ -300,6 +306,11 @@ namespace SHADE for (auto const& mesh : meshes) { meshCollection.emplace(GenerateAssetID(AssetType::MESH), mesh); + + if (!mesh.compiled) + { + SHMeshCompiler::CompileMeshBinary(mesh, asset.path); + } } } @@ -310,6 +321,11 @@ namespace SHADE SHTextureLoader::LoadImageAsset(asset.path, image); textureCollection.emplace(GenerateAssetID(AssetType::DDS), image); + + if (!image.compiled) + { + SHTextureCompiler::CompileTextureBinary(image, asset.path); + } } /**************************************************************************** diff --git a/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp b/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp index aabb0dc0..35d167d2 100644 --- a/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp +++ b/SHADE_Engine/src/Assets/SHAssetMetaHandler.cpp @@ -99,6 +99,7 @@ namespace SHADE ****************************************************************************/ void SHAssetMetaHandler::WriteMetaData(SHAsset const& meta) noexcept { + //TODO: Write into binary eventually std::string path{ meta.path.string() }; path.append(META_EXTENSION); @@ -109,9 +110,21 @@ namespace SHADE SHLOG_ERROR("Asset write path is invalid: {}", path); return; } - + + metaFile << "Name: " << meta.name << "\n"; metaFile << "ID: " << meta.id << "\n"; metaFile << "Type: " << static_cast(meta.type) << std::endl; + + //TODO Add in information that is specific to types like mesh + switch(meta.type) + { + case AssetType::MESH: + break; + + default: + void; + } + metaFile.close(); }