Skip to content

Commit

Permalink
Improve water rendering to correctly load textures
Browse files Browse the repository at this point in the history
  • Loading branch information
Pursche committed Sep 30, 2023
1 parent 6267d33 commit cd8c253
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 90 deletions.
19 changes: 19 additions & 0 deletions Source/Game/Game/ECS/Singletons/LiquidDB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#include <Base/Types.h>
#include <Base/Container/StringTable.h>

#include <FileFormat/Novus/ClientDB/ClientDB.h>
#include <FileFormat/Novus/ClientDB/Definitions.h>

#include <robinhood/robinhood.h>

namespace ECS::Singletons
{
struct LiquidDB
{
public:
LiquidDB() {}

DB::Client::ClientDB<DB::Client::Definitions::LiquidType> liquidTypes;
};
}
7 changes: 0 additions & 7 deletions Source/Game/Game/Editor/MapEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "Game/ECS/Util/MapDBUtil.h"
#include "Game/Rendering/GameRenderer.h"
#include "Game/Rendering/Terrain/TerrainLoader.h"
#include "Game/Rendering/Model/ModelLoader.h"
#include "Game/Util/ServiceLocator.h"

#include <Base/CVarSystem/CVarSystemPrivate.h>
Expand Down Expand Up @@ -125,12 +124,6 @@ namespace Editor
{
if (DB::Client::Definitions::Map* map = ECS::Util::MapDB::GetMapFromName(*preview))
{
if (_hasLoadedMap)
{
ModelLoader* modelLoader = ServiceLocator::GetGameRenderer()->GetModelLoader();
modelLoader->Clear();
}

const std::string& mapInternalName = mapDB.entries.stringTable.GetString(map->internalName);

TerrainLoader* terrainLoader = ServiceLocator::GetGameRenderer()->GetTerrainLoader();
Expand Down
32 changes: 31 additions & 1 deletion Source/Game/Game/Loaders/ClientDB/ClientDBLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Game/Loaders/LoaderSystem.h"
#include "Game/Util/ServiceLocator.h"
#include "Game/ECS/Singletons/MapDB.h"
#include "Game/ECS/Singletons/LiquidDB.h"
#include "Game/Application/EnttRegistries.h"

#include <Base/Container/ConcurrentQueue.h>
Expand Down Expand Up @@ -97,6 +98,7 @@ class ClientDBLoader : Loader
void SetupSingletons(entt::registry::context& registryCtx)
{
registryCtx.emplace<ECS::Singletons::MapDB>();
registryCtx.emplace<ECS::Singletons::LiquidDB>();
}

bool LoadMapDB(entt::registry::context& registryCtx, std::shared_ptr<Bytebuffer>& buffer, const ClientDBPair& pair)
Expand Down Expand Up @@ -127,6 +129,8 @@ class ClientDBLoader : Loader
continue;
}

mapDB.entries.idToIndexMap[map.id] = i;

const std::string& mapName = stringTable.GetString(map.name);
u32 mapNameHash = StringUtils::fnv1a_32(mapName.c_str(), mapName.length());

Expand All @@ -138,9 +142,35 @@ class ClientDBLoader : Loader
return true;
}

bool LoadLiquidTypeDB(entt::registry::context& registryCtx, std::shared_ptr<Bytebuffer>& buffer, const ClientDBPair& pair)
{
auto& liquidDB = registryCtx.at<ECS::Singletons::LiquidDB>();

// Clear, in case we already filled liquidDB
liquidDB.liquidTypes.data.clear();
liquidDB.liquidTypes.stringTable.Clear();

if (!liquidDB.liquidTypes.Read(buffer))
{
return false;
}

u32 numRecords = static_cast<u32>(liquidDB.liquidTypes.data.size());

for (u32 i = 0; i < numRecords; i++)
{
const DB::Client::Definitions::LiquidType& type = liquidDB.liquidTypes.data[i];

liquidDB.liquidTypes.idToIndexMap[type.id] = i;
}

return true;
}

robin_hood::unordered_map<u32, std::function<bool(entt::registry::context&, std::shared_ptr<Bytebuffer>&, const ClientDBPair&)>> clientDBEntries =
{
{ "Map.cdb"_h, std::bind(&ClientDBLoader::LoadMapDB, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3) }
{ "Map.cdb"_h, std::bind(&ClientDBLoader::LoadMapDB, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3) },
{ "LiquidType.cdb"_h, std::bind(&ClientDBLoader::LoadLiquidTypeDB, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3) }
};
};

Expand Down
4 changes: 2 additions & 2 deletions Source/Game/Game/Rendering/Canvas/Canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ void Canvas::DrawText(Renderer::Font& font, const std::string& text, const vec2&
// Add char draw data
CanvasCharDrawData& charDrawData = charDrawDatas.emplace_back();
charDrawData.data.x = fontChar.textureIndex;
charDrawData.data.y = Color::White.ToU32();
charDrawData.data.z = Color::Black.ToU32();
charDrawData.data.y = Color::White.ToABGR32();
charDrawData.data.z = Color::Black.ToABGR32();

f32 outlineWidth = 0.5f;
charDrawData.data.w = *reinterpret_cast<i32*>(&outlineWidth);
Expand Down
18 changes: 9 additions & 9 deletions Source/Game/Game/Rendering/Debug/DebugRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ void DebugRenderer::DrawLine2D(const glm::vec2& from, const glm::vec2& to, Color
{
auto& vertices = _debugVertices2D.Get();

u32 colorInt = color.ToU32();
u32 colorInt = color.ToABGR32();

vertices.push_back({ from, colorInt });
vertices.push_back({ to, colorInt });
Expand All @@ -410,7 +410,7 @@ void DebugRenderer::DrawLine3D(const glm::vec3& from, const glm::vec3& to, Color
{
auto& vertices = _debugVertices3D.Get();

u32 colorInt = color.ToU32();
u32 colorInt = color.ToABGR32();

vertices.push_back({ from, colorInt });
vertices.push_back({ to, colorInt });
Expand All @@ -423,7 +423,7 @@ void DebugRenderer::DrawAABB3D(const vec3& center, const vec3& extents, Color co
vec3 v0 = center - extents;
vec3 v1 = center + extents;

u32 colorInt = color.ToU32();
u32 colorInt = color.ToABGR32();

// Bottom
vertices.push_back({ { v0.x, v0.y, v0.z }, colorInt });
Expand Down Expand Up @@ -460,7 +460,7 @@ void DebugRenderer::DrawOBB3D(const vec3& center, const vec3& extents, const qua
{
auto& vertices = _debugVertices3D.Get();

u32 colorInt = color.ToU32();
u32 colorInt = color.ToABGR32();

vec3 corners[8] = {
center + rotation * glm::vec3(-extents.x, -extents.y, -extents.z),
Expand Down Expand Up @@ -522,7 +522,7 @@ void DebugRenderer::DrawCircle3D(const vec3& center, f32 radius, i32 resolution,
{
auto& vertices = _debugVertices3D.Get();

u32 colorInt = color.ToU32();
u32 colorInt = color.ToABGR32();

constexpr f32 PI = glm::pi<f32>();
constexpr f32 TAU = PI * 2.0f;
Expand Down Expand Up @@ -587,7 +587,7 @@ void DebugRenderer::DrawMatrix(const mat4x4& matrix, f32 scale)
void DebugRenderer::DrawLineSolid2D(const vec2& from, const vec2& to, Color color, bool shaded)
{
color.a = static_cast<f32>(shaded);
u32 colorInt = color.ToU32();
u32 colorInt = color.ToABGR32();

auto& vertices = _debugVerticesSolid2D.Get();

Expand All @@ -611,7 +611,7 @@ void DebugRenderer::DrawAABBSolid3D(const vec3& center, const vec3& extents, Col
vec3 rightNormal = { 1, 0, 0 };

color.a = static_cast<f32>(shaded);
u32 colorInt = color.ToU32();
u32 colorInt = color.ToABGR32();
f32 colorFloat = *reinterpret_cast<f32*>(&colorInt);

// Bottom
Expand Down Expand Up @@ -693,7 +693,7 @@ void DebugRenderer::DrawOBBSolid3D(const vec3& center, const vec3& extents, cons
vec3 rightNormal = rotation * vec3(1, 0, 0);

color.a = static_cast<f32>(shaded);
u32 colorInt = color.ToU32();
u32 colorInt = color.ToABGR32();
f32 colorFloat = *reinterpret_cast<f32*>(&colorInt);

// Bottom
Expand Down Expand Up @@ -767,7 +767,7 @@ void DebugRenderer::DrawTriangleSolid3D(const vec3& v0, const vec3& v1, const ve
vec3 normal = glm::normalize(glm::cross(v1 - v0, v2 - v0));

color.a = static_cast<f32>(shaded);
u32 colorInt = color.ToU32();
u32 colorInt = color.ToABGR32();
f32 colorFloat = *reinterpret_cast<f32*>(&colorInt);

vertices.push_back({ vec4(v0, 0.0f), vec4(normal, colorFloat ) });
Expand Down
6 changes: 6 additions & 0 deletions Source/Game/Game/Rendering/Terrain/TerrainLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,12 @@ void TerrainLoader::PrepareForChunks(LoadType loadType, u32 numChunks)
{
_terrainRenderer->ClearChunks();

ModelLoader* modelLoader = ServiceLocator::GetGameRenderer()->GetModelLoader();
modelLoader->Clear();

WaterLoader* waterLoader = ServiceLocator::GetGameRenderer()->GetWaterLoader();
waterLoader->Clear();

for (auto& pair : _chunkIDToBodyID)
{
JPH::BodyID id = static_cast<JPH::BodyID>(pair.second);
Expand Down
9 changes: 7 additions & 2 deletions Source/Game/Game/Rendering/Water/WaterLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ void WaterLoader::Init()

void WaterLoader::Clear()
{
LoadRequestInternal dummyRequest;
while (_requests.try_dequeue(dummyRequest))
{
// Just empty the queue
}

_waterRenderer->Clear();
}

void WaterLoader::Update(f32 deltaTime)
Expand Down Expand Up @@ -68,7 +74,7 @@ void WaterLoader::Update(f32 deltaTime)
// Have WaterRenderer prepare all buffers for what we need to load
_waterRenderer->Reserve(reserveInfo);

#if 1
#if 0
for (u32 i = 0; i < numDequeued; i++)
{
LoadRequestInternal& request = _workingRequests[i];
Expand Down Expand Up @@ -209,7 +215,6 @@ void WaterLoader::LoadRequest(LoadRequestInternal& request)

f32* heightMap = nullptr;
u8* bitMap = nullptr;
//Terrain::LiquidUVMapEntry* uvEntries = nullptr;

if (request.vertexData != nullptr && hasVertexData)
{
Expand Down
Loading

0 comments on commit cd8c253

Please sign in to comment.