Skip to content

Commit

Permalink
terrain description files added
Browse files Browse the repository at this point in the history
  • Loading branch information
beaumanvienna committed Jun 8, 2024
1 parent ea39e15 commit 5394ee5
Show file tree
Hide file tree
Showing 20 changed files with 767 additions and 204 deletions.
2 changes: 1 addition & 1 deletion application/lucre/gameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace LucreApp
#ifdef MACOSX
SetNextState(State::CUTSCENE);
#else
SetNextState(State::DESSERT);
SetNextState(State::TERRAIN);
#endif
}

Expand Down
44 changes: 43 additions & 1 deletion application/lucre/sceneDescriptions/terrain.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,49 @@
"file format identifier": 1.2,
"description": "terrain scene",
"author": "Copyright (c) 2024 Engine Development Team",
"terrainPngPath": "application/lucre/models/assets/terrain/heightmap2.png",
"terrain":
[
{
"filename": "application/lucre/terrainDescriptions/heightmap2.json",
"instances":
[
{
"transform":
{
"scale":
[
0.141, 0.141, 0.141
],
"rotation":
[
3.14159, 1.07215, 3.14159
],
"translation":
[
-0.779968, 2.25822, -4
]
}
},
{
"transform":
{
"scale":
[
0.140998, 0.140998, 0.140998
],
"rotation":
[
3.01614, 1.00771, 2.99355
],
"translation":
[
-0.494322, 2.28222, -3.408
]
}
}
]
}
],
"fastgltf files":
[
{
Expand Down
10 changes: 1 addition & 9 deletions application/lucre/scenes/terrainScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,7 @@ namespace LucreApp

void TerrainScene::LoadTerrain()
{
Builder builder;
m_Terrain = builder.LoadTerrainHeightMapPNG(m_SceneLoaderJSON.GetTerrainPath(), *this);
if (m_Terrain != entt::null)
{
auto view = m_Registry.view<TransformComponent>();
auto& terrainTransform = view.get<TransformComponent>(m_Terrain);
terrainTransform.SetScale(0.054f);
terrainTransform.SetTranslation({-0.576f, 2.33f, -0.117f});
}
m_Terrain = m_Dictionary.Retrieve("application/lucre/terrainDescriptions/heightmap2.json::0");
}
void TerrainScene::LoadModels()
{
Expand Down
6 changes: 6 additions & 0 deletions application/lucre/terrainDescriptions/heightmap2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"file format identifier": 1.2,
"description": "height map 2",
"author": "Copyright (c) 2024 Engine Development Team",
"terrainPngPath": "application/lucre/models/assets/terrain/heightmap2.png"
}
1 change: 1 addition & 0 deletions engine/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ namespace GfxRenderEngine
float GetDesktopHeight() const { return static_cast<float>(m_Window->GetDesktopHeight()); }

std::shared_ptr<Model> LoadModel(const Builder& builder) { return m_GraphicsContext->LoadModel(builder); }
std::shared_ptr<Model> LoadModel(const TerrainBuilder& builder) { return m_GraphicsContext->LoadModel(builder); }
std::shared_ptr<Model> LoadModel(const GltfBuilder& builder) { return m_GraphicsContext->LoadModel(builder); }
std::shared_ptr<Model> LoadModel(const FastgltfBuilder& builder) { return m_GraphicsContext->LoadModel(builder); }
std::shared_ptr<Model> LoadModel(const FbxBuilder& builder) { return m_GraphicsContext->LoadModel(builder); }
Expand Down
7 changes: 7 additions & 0 deletions engine/platform/Vulkan/VKgraphicsContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ namespace GfxRenderEngine
return model;
}

std::shared_ptr<Model> VK_Context::LoadModel(const TerrainBuilder& builder)
{
ASSERT(VK_Core::m_Device != nullptr);
auto model = std::make_shared<VK_Model>(VK_Core::m_Device, builder);
return model;
}

std::shared_ptr<Model> VK_Context::LoadModel(const GltfBuilder& builder)
{
ASSERT(VK_Core::m_Device != nullptr);
Expand Down
1 change: 1 addition & 0 deletions engine/platform/Vulkan/VKgraphicsContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace GfxRenderEngine

virtual std::shared_ptr<Renderer> GetRenderer() const override { return m_Renderer; }
virtual std::shared_ptr<Model> LoadModel(const Builder& builder) override;
virtual std::shared_ptr<Model> LoadModel(const TerrainBuilder& builder) override;
virtual std::shared_ptr<Model> LoadModel(const GltfBuilder& builder) override;
virtual std::shared_ptr<Model> LoadModel(const FastgltfBuilder& builder) override;
virtual std::shared_ptr<Model> LoadModel(const FbxBuilder& builder) override;
Expand Down
9 changes: 9 additions & 0 deletions engine/platform/Vulkan/VKmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ namespace GfxRenderEngine
CreateIndexBuffer(std::move(builder.m_Indices));
}

VK_Model::VK_Model(std::shared_ptr<VK_Device> device, const TerrainBuilder& builder)
: m_Device(device), m_HasIndexBuffer{false}
{
CopySubmeshes(builder.m_Submeshes);

CreateVertexBuffer(std::move(builder.m_Vertices));
CreateIndexBuffer(std::move(builder.m_Indices));
}

VK_Model::~VK_Model() {}

VK_Submesh::VK_Submesh(Submesh const& submesh)
Expand Down
2 changes: 2 additions & 0 deletions engine/platform/Vulkan/VKmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "renderer/buffer.h"
#include "renderer/builder/builder.h"
#include "renderer/builder/gltfBuilder.h"
#include "renderer/builder/terrainBuilder.h"
#include "renderer/builder/fastgltfBuilder.h"
#include "renderer/builder/ufbxBuilder.h"
#include "renderer/builder/fbxBuilder.h"
Expand Down Expand Up @@ -90,6 +91,7 @@ namespace GfxRenderEngine
VK_Model(std::shared_ptr<VK_Device> device, const FastgltfBuilder& builder);
VK_Model(std::shared_ptr<VK_Device> device, const FbxBuilder& builder);
VK_Model(std::shared_ptr<VK_Device> device, const UFbxBuilder& builder);
VK_Model(std::shared_ptr<VK_Device> device, const TerrainBuilder& builder);
virtual ~VK_Model() override;

VK_Model(const VK_Model&) = delete;
Expand Down
180 changes: 0 additions & 180 deletions engine/renderer/builder/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,186 +46,6 @@ namespace std
namespace GfxRenderEngine
{

void Builder::PopulateTerrainData(std::vector<std::vector<float>> const& heightMap)
{
float scale = 0.1f; // Scale for the grid spacing
float heightScale = 1.f; // Scale for the height values
size_t rows = heightMap.size();
size_t cols = heightMap.empty() ? 0 : heightMap[0].size();
m_Vertices.resize(rows * cols);
size_t vertexCounter = 0;

for (size_t z = 0; z < rows; ++z)
{
for (size_t x = 0; x < cols; ++x)
{
Vertex& vertex = m_Vertices[vertexCounter];
++vertexCounter;

float originY = heightMap[z][x] * heightScale;

vertex.m_Position = glm::vec3(x * scale, originY, z * scale);
vertex.m_Color = glm::vec4(0.f, 0.f, heightMap[z][x] / 3, 1.0f);
vertex.m_UV = glm::vec2(0.f, 0.f);
vertex.m_Tangent = glm::vec3(1.0f);
vertex.m_JointIds = glm::ivec4(0);
vertex.m_Weights = glm::vec4(0.0f);

// compute normals via neighbors
// up
// left O right
// down
glm::vec3 sumNormals(0.0f);
float leftY = x > 0 ? heightMap[z][x - 1] * heightScale : 0.0f;
float rightY = x < cols - 1 ? heightMap[z][x + 1] * heightScale : 0.0f;
float upY = z < rows - 1 ? heightMap[z + 1][x] * heightScale : 0.0f;
float downY = z > 0 ? heightMap[z - 1][x] * heightScale : 0.0f;

float dx = scale;
float dz = scale;

glm::vec3 left = glm::vec3(-dx, leftY - originY, 0.0f);
glm::vec3 right = glm::vec3(dx, rightY - originY, 0.0f);
glm::vec3 up = glm::vec3(0.0f, upY - originY, dz);
glm::vec3 down = glm::vec3(0.0f, downY - originY, -dz);

auto normalComponent = [&](glm::vec3 a, glm::vec3 b)
{
glm::vec3 normal;
if (x > 0 && z > 0 && x < cols - 1 && z < rows - 1)
{
// Cross products to compute normals
normal = glm::cross(a, b);
}
else
{
normal = glm::vec3(0.0f, 1.0f, 0.0f);
}
return normal;
};

// smoothshading
sumNormals = normalComponent(left, -down);
sumNormals += normalComponent(-down, right);
sumNormals += normalComponent(right, -up);
sumNormals += normalComponent(-up, left);

vertex.m_Normal = glm::normalize(sumNormals);
}
}
for (size_t z = 0; z < rows - 1; ++z)
{
for (size_t x = 0; x < cols - 1; ++x)
{

uint32_t topLeft = z * cols + x;

uint32_t topRight = topLeft + 1;
uint32_t bottomLeft = (z + 1) * cols + x;
uint32_t bottomRight = bottomLeft + 1;

m_Indices.push_back(topLeft);
m_Indices.push_back(bottomLeft);
m_Indices.push_back(topRight);
m_Indices.push_back(topRight);
m_Indices.push_back(bottomLeft);
m_Indices.push_back(bottomRight);
}
}
}
entt::entity Builder::LoadTerrainHeightMapPNG(std::string const& filepath, Scene& scene)
{
m_Vertices.clear();
m_Indices.clear();
int width, height, bytesPerPixel;
uchar* localBuffer = stbi_load(filepath.c_str(), &width, &height, &bytesPerPixel, 0);
if (localBuffer)
{
std::vector<std::vector<float>> terrainData(height, std::vector<float>(width));
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
terrainData[i][j] = static_cast<float>(localBuffer[i * width + j]) / 127.0f;
}
}
stbi_image_free(localBuffer);

PopulateTerrainData(terrainData);

// create game object
entt::entity entity;
std::shared_ptr<InstanceBuffer> instanceBuffer;
{
auto& registry = scene.GetRegistry();
auto& sceneGraph = scene.GetSceneGraph();
auto& dictionary = scene.GetDictionary();

// create game object
entity = registry.create();
TransformComponent transform{};
PbrMaterialTag pbrMaterialTag{};
InstanceTag instanceTag;

// create instance buffer
instanceTag.m_Instances.push_back(entity);
const uint numberOfInstances = 1;
const uint indexOfFirstInstance = 0;
instanceBuffer = InstanceBuffer::Create(numberOfInstances);
instanceTag.m_InstanceBuffer = instanceBuffer;
instanceTag.m_InstanceBuffer->SetInstanceData(indexOfFirstInstance, transform.GetMat4Global(),
transform.GetNormalMatrix());
transform.SetInstance(instanceTag.m_InstanceBuffer, indexOfFirstInstance);

// push into ECS
registry.emplace<TransformComponent>(entity, transform);
registry.emplace<PbrMaterialTag>(entity, pbrMaterialTag);
registry.emplace<InstanceTag>(entity, instanceTag);

// add to scene graph
uint newNode = sceneGraph.CreateNode(entity, "terrain", "terrain", dictionary);
sceneGraph.GetRoot().AddChild(newNode);

Submesh submesh{};
submesh.m_FirstIndex = 0;
submesh.m_FirstVertex = 0;
submesh.m_IndexCount = m_Indices.size();
submesh.m_VertexCount = m_Vertices.size();
submesh.m_InstanceCount = 1;

submesh.m_Material.m_PbrMaterial.m_Roughness = 0.1f;
submesh.m_Material.m_PbrMaterial.m_Metallic = 0.9f;
submesh.m_Material.m_PbrMaterial.m_NormalMapIntensity = 1.0f;

{ // create material descriptor
Material::MaterialTextures materialTextures;

auto materialDescriptor = MaterialDescriptor::Create(MaterialDescriptor::MtPbr, materialTextures);
submesh.m_Material.m_MaterialDescriptor = materialDescriptor;
}
{ // create resource descriptor
Resources::ResourceBuffers resourceBuffers;

resourceBuffers[Resources::INSTANCE_BUFFER_INDEX] = instanceBuffer->GetBuffer();
auto resourceDescriptor = ResourceDescriptor::Create(resourceBuffers);
submesh.m_Resources.m_ResourceDescriptor = resourceDescriptor;
}
m_Submeshes.push_back(submesh);
auto model = Engine::m_Engine->LoadModel(*this);
MeshComponent mesh{"terrain", model};
registry.emplace<MeshComponent>(entity, mesh);
}

return entity;
}
else
{
LOG_CORE_CRITICAL("Builder::LoadTerrainHeightMapPNG: Couldn't load file {0}", filepath);
}

return entt::null;
}

void Builder::LoadSprite(Sprite const& sprite, float const amplification, int const unlit, glm::vec4 const& color)
{
m_Vertices.clear();
Expand Down
4 changes: 1 addition & 3 deletions engine/renderer/builder/builder.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Engine Copyright (c) 2023 Engine Development Team
/* Engine Copyright (c) 2024 Engine Development Team
https://github.com/beaumanvienna/vulkan
Permission is hereby granted, free of charge, to any person
Expand Down Expand Up @@ -33,15 +33,13 @@ namespace GfxRenderEngine
public:
Builder() = default;

entt::entity LoadTerrainHeightMapPNG(const std::string& filepath, Scene& scene);
void LoadParticle(glm::vec4 const& color);
void LoadSprite(Sprite const& sprite, float const amplification = 0.0f, int const unlit = 0,
glm::vec4 const& color = glm::vec4(1.0f));
entt::entity LoadCubemap(std::vector<std::string> const& faces, entt::registry& registry);

private:
void CalculateTangents();
void PopulateTerrainData(std::vector<std::vector<float>> const& heightMap);
void CalculateTangentsFromIndexBuffer(std::vector<uint> const& indices);

public:
Expand Down
Loading

0 comments on commit 5394ee5

Please sign in to comment.