Skip to content

Commit

Permalink
Implemented a bunch of new serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunkTreasure1 committed Mar 11, 2024
1 parent 3cad686 commit a28c0cc
Show file tree
Hide file tree
Showing 61 changed files with 2,217 additions and 218 deletions.
80 changes: 17 additions & 63 deletions Volt/Navigation/src/Navigation/Filesystem/NavMeshImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include <Volt/Asset/AssetManager.h>

#include <Volt/Utility/FileIO/BinaryStreamWriter.h>
#include <Volt/Utility/FileIO/BinaryStreamReader.h>

namespace Volt
{
namespace AI
Expand All @@ -16,23 +19,7 @@ namespace Volt
static const int TILECACHESET_MAGIC = 'T' << 24 | 'S' << 16 | 'E' << 8 | 'T'; //'TSET';
static const int TILECACHESET_VERSION = 2;

struct NavMeshSetHeader
{
int magic;
int version;
int numTiles;
dtNavMeshParams params;
};

struct NavMeshTileHeader
{
dtTileRef tileRef;
int dataSize;
};

VT_OPTIMIZE_OFF

bool NavMeshImporter::SaveNavMesh(std::ostream& output, Ref<NavMesh>& asset)
bool NavMeshImporter::SaveNavMesh(BinaryStreamWriter& output, Ref<NavMesh>& asset)
{
if (asset->GetNavMesh()->GetTileCache())
{
Expand All @@ -44,39 +31,30 @@ namespace Volt
}
}

bool NavMeshImporter::LoadNavMesh(std::ifstream& input, Ref<dtNavMesh>& asset)
bool NavMeshImporter::LoadNavMesh(BinaryStreamReader& input, Ref<dtNavMesh>& asset)
{
std::streampos currentPosition = input.tellg();

// Read header.
NavMeshSetHeader header;
if (!input.read(reinterpret_cast<char*>(&header), sizeof(NavMeshSetHeader)))
{
input.close();
return false;
}

input.seekg(currentPosition);
input.Read(header);

switch (header.magic)
{
case NAVMESHSET_MAGIC:
{
return LoadSingleNavMesh(input, asset);
return LoadSingleNavMesh(input, header, asset);
}
case TILECACHESET_MAGIC:
{
return LoadTiledNavMesh(input, asset);
return LoadTiledNavMesh(input, header, asset);
}
default:
{
input.close();
return false;
}
}
}

bool NavMeshImporter::SaveSingleNavMesh(std::ostream& output, Ref<NavMesh>& asset)
bool NavMeshImporter::SaveSingleNavMesh(BinaryStreamWriter& output, Ref<NavMesh>& asset)
{
const auto* mesh = asset->GetNavMesh()->GetNavMesh().get();

Expand All @@ -92,7 +70,7 @@ namespace Volt
header.numTiles++;
}
memcpy(&header.params, mesh->getParams(), sizeof(dtNavMeshParams));
output.write(reinterpret_cast<char*>(&header), sizeof(NavMeshSetHeader));
output.Write(header);

// Store tiles.
for (int i = 0; i < mesh->getMaxTiles(); ++i)
Expand All @@ -103,50 +81,37 @@ namespace Volt
NavMeshTileHeader tileHeader;
tileHeader.tileRef = mesh->getTileRef(tile);
tileHeader.dataSize = tile->dataSize;
output.write(reinterpret_cast<char*>(&tileHeader), sizeof(NavMeshTileHeader));

output.write(reinterpret_cast<const char*>(tile->data), tile->dataSize);
output.Write(tileHeader);
output.Write(tile->data);
}
return true;
}

bool NavMeshImporter::SaveTiledNavMesh(std::ostream& output, Ref<NavMesh>& asset)
bool NavMeshImporter::SaveTiledNavMesh(BinaryStreamWriter& output, Ref<NavMesh>& asset)
{
return true;
}

bool NavMeshImporter::LoadSingleNavMesh(std::ifstream& input, Ref<dtNavMesh>& asset)
bool NavMeshImporter::LoadSingleNavMesh(BinaryStreamReader& input, const NavMeshSetHeader& header, Ref<dtNavMesh>& asset)
{
// Read header.
NavMeshSetHeader header;
if (!input.read(reinterpret_cast<char*>(&header), sizeof(NavMeshSetHeader)))
{
input.close();
return false;
}

if (header.magic != NAVMESHSET_MAGIC)
{
input.close();
return false;
}
if (header.version != NAVMESHSET_VERSION)
{
input.close();
return false;
}

dtNavMesh* mesh = dtAllocNavMesh();
if (!mesh)
{
input.close();
dtFree(mesh);
return false;
}
dtStatus status = mesh->init(&header.params);
if (dtStatusFailed(status))
{
input.close();
dtFree(mesh);
return false;
}
Expand All @@ -155,27 +120,16 @@ namespace Volt
for (int i = 0; i < header.numTiles; ++i)
{
NavMeshTileHeader tileHeader;
if (!input.read(reinterpret_cast<char*>(&tileHeader), sizeof(NavMeshTileHeader)))
{
input.close();
dtFree(mesh);
return false;
}
input.Read(tileHeader);

if (!tileHeader.tileRef || !tileHeader.dataSize)
break;

unsigned char* data = (unsigned char*)dtAlloc(tileHeader.dataSize, DT_ALLOC_PERM);
if (!data) break;
memset(data, 0, tileHeader.dataSize);
if (!input.read(reinterpret_cast<char*>(data), tileHeader.dataSize))
{
dtFree(data);
dtFree(mesh);
input.close();
return false;
}

input.Read(data);
mesh->addTile(data, tileHeader.dataSize, DT_TILE_FREE_DATA, tileHeader.tileRef, 0);
}

Expand All @@ -184,7 +138,7 @@ namespace Volt
return true;
}

bool NavMeshImporter::LoadTiledNavMesh(std::ifstream& input, Ref<dtNavMesh>& asset)
bool NavMeshImporter::LoadTiledNavMesh(BinaryStreamReader& input, const NavMeshSetHeader& header, Ref<dtNavMesh>& asset)
{
return true;
}
Expand Down
29 changes: 23 additions & 6 deletions Volt/Navigation/src/Navigation/Filesystem/NavMeshImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Volt
{
class BinaryStreamReader;
class BinaryStreamWriter;

namespace AI
{
class NavMeshImporter
Expand All @@ -14,15 +17,29 @@ namespace Volt
NavMeshImporter() = delete;
virtual ~NavMeshImporter() = delete;

static bool SaveNavMesh(std::ostream& output, Ref<NavMesh>& asset);
static bool LoadNavMesh(std::ifstream& input, Ref<dtNavMesh>& asset);
static bool SaveNavMesh(BinaryStreamWriter& output, Ref<NavMesh>& asset);
static bool LoadNavMesh(BinaryStreamReader& input, Ref<dtNavMesh>& asset);

private:
static bool SaveSingleNavMesh(std::ostream& output, Ref<NavMesh>& asset);
static bool SaveTiledNavMesh(std::ostream& output, Ref<NavMesh>& asset);
struct NavMeshSetHeader
{
int magic;
int version;
int numTiles;
dtNavMeshParams params;
};

struct NavMeshTileHeader
{
dtTileRef tileRef;
int dataSize;
};

static bool SaveSingleNavMesh(BinaryStreamWriter& output, Ref<NavMesh>& asset);
static bool SaveTiledNavMesh(BinaryStreamWriter& output, Ref<NavMesh>& asset);

static bool LoadSingleNavMesh(std::ifstream& input, Ref<dtNavMesh>& asset);
static bool LoadTiledNavMesh(std::ifstream& input, Ref<dtNavMesh>& asset);
static bool LoadSingleNavMesh(BinaryStreamReader& input, const NavMeshSetHeader& header, Ref<dtNavMesh>& asset);
static bool LoadTiledNavMesh(BinaryStreamReader& input, const NavMeshSetHeader& header, Ref<dtNavMesh>& asset);
};
}
}
2 changes: 1 addition & 1 deletion Volt/Navigation/src/Navigation/NavMesh/VTNavMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Volt
{
namespace AI
{
NavMesh::NavMesh(Ref<dtNavMesh> navmesh, Ref<dtTileCache> tilecache)
void NavMesh::Initialize(Ref<dtNavMesh> navmesh, Ref<dtTileCache> tilecache)
{
myNavMesh = CreateRef<DtNavMesh>(navmesh, tilecache);
myCrowd = CreateRef<DtCrowd>(myNavMesh);
Expand Down
4 changes: 3 additions & 1 deletion Volt/Navigation/src/Navigation/NavMesh/VTNavMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ namespace Volt
{
public:
NavMesh() = default;
NavMesh(Ref<dtNavMesh> navmesh, Ref<dtTileCache> tilecache = nullptr);
virtual ~NavMesh() = default;

void Initialize(Ref<dtNavMesh> navmesh, Ref<dtTileCache> tilecache = nullptr);

Ref<DtNavMesh>& GetNavMesh() { return myNavMesh; }
Ref<DtCrowd>& GetCrowd() { return myCrowd; }

Expand All @@ -30,6 +31,7 @@ namespace Volt

private:
friend class NavigationSystem;

void Update(float deltaTime);

Ref<DtNavMesh> myNavMesh = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,8 @@ Ref<Volt::AI::NavMesh> RecastBuilder::BuildSingleNavMesh()
return nullptr;
}

result = CreateRef<Volt::AI::NavMesh>(navMesh);
result = CreateRef<Volt::AI::NavMesh>();
result->Initialize(navMesh);

if (result)
{
Expand Down Expand Up @@ -948,7 +949,8 @@ Ref<Volt::AI::NavMesh> RecastBuilder::BuildTiledNavMesh()
}
}

result = CreateRef<Volt::AI::NavMesh>(navMesh, tileCache);
result = CreateRef<Volt::AI::NavMesh>();
result->Initialize(navMesh, tileCache);

auto gridSize = tw * th;
const float compressionRatio = (float)m_cacheCompressedSize / (float)(m_cacheRawSize + 1);
Expand Down
26 changes: 26 additions & 0 deletions Volt/Sandbox/src/Sandbox/Sandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@

#include <imgui.h>

#include "Volt/Asset/ImportersNew/SourceTextureSerializer.h"
#include "Volt/Asset/ImportersNew/TextureSerializer.h"

Sandbox::Sandbox()
{
VT_ASSERT(!myInstance, "Sandbox already exists!");
Expand All @@ -101,6 +104,8 @@ Sandbox::~Sandbox()
myInstance = nullptr;
}

static Ref<Volt::Texture2D> tex;

void Sandbox::OnAttach()
{
SelectionManager::Init();
Expand Down Expand Up @@ -224,6 +229,20 @@ void Sandbox::OnAttach()

myRuntimeScene->InitializeEngineScripts();

Volt::AssetMetadata metadata;
Ref<Volt::SourceTextureSerializer> texSourceImporter = CreateRef<Volt::SourceTextureSerializer>();
metadata.filePath = "Assets/Player Test/CH_Player_01_MAT_C.dds";

Ref<Volt::Texture2D> sourceTex = CreateRef<Volt::Texture2D>();
texSourceImporter->Deserialize(metadata, sourceTex);

Ref<Volt::TextureSerializer> texSerializer = CreateRef<Volt::TextureSerializer>();
metadata.filePath = "Assets/Player Test/CH_Player_01_MAT_C.vtasset";
texSerializer->Serialize(metadata, sourceTex);

tex = CreateRef<Volt::Texture2D>();
texSerializer->Deserialize(metadata, tex);

m_isInitialized = true;
}

Expand Down Expand Up @@ -880,6 +899,13 @@ bool Sandbox::OnImGuiUpdateEvent(Volt::AppImGuiUpdateEvent& e)
RenderProgressBar(buildProgess);
}

if (ImGui::Begin("Test"))
{
ImGui::Image(UI::GetTextureID(tex), { 512, 512 });

ImGui::End();
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1638,7 +1638,9 @@ void AssetBrowserPanel::CreateNewShaderModal()
fout << out.c_str();
fout.close();

Ref<Volt::Shader> newShader = Volt::AssetManager::CreateAsset<Volt::Shader>(Volt::AssetManager::GetRelativePath(myCurrentDirectory->path), tempName + ".vtsdef", tempName, shaderPaths, false);
Ref<Volt::Shader> newShader = Volt::AssetManager::CreateAsset<Volt::Shader>(Volt::AssetManager::GetRelativePath(myCurrentDirectory->path), tempName + ".vtsdef");
newShader->Initialize(tempName, shaderPaths, false);

Volt::ShaderRegistry::Register(tempName, newShader);
}

Expand Down
1 change: 1 addition & 0 deletions Volt/Volt/src/Volt/Asset/Animation/AnimatedCharacter.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace Volt

private:
friend class AnimatedCharacterImporter;
friend class AnimatedCharacterSerializer;

Ref<Skeleton> mySkeleton;
Ref<Mesh> mySkin;
Expand Down
Loading

0 comments on commit a28c0cc

Please sign in to comment.