Skip to content

Commit

Permalink
Merge pull request #48 from beaumanvienna/wip
Browse files Browse the repository at this point in the history
new iceberg scene
  • Loading branch information
beaumanvienna authored Nov 7, 2024
2 parents a7d61a9 + 10ed6c1 commit 594538b
Show file tree
Hide file tree
Showing 58 changed files with 935 additions and 94 deletions.
1 change: 1 addition & 0 deletions application/lucre/animation/easingAnimations.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ namespace LucreApp
}
}
void Stop() { m_IsRunning = false; }
bool IsRunning() const { return m_IsRunning; }
bool Run(float speedXY[dimensions])
{
for (uint index = 0; index < dimensions; ++index)
Expand Down
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
Load(State::SETTINGS);

SetState(State::SPLASH);
SetNextState(State::TERRAIN);
SetNextState(State::VOLCANO);
}

void GameState::Stop()
Expand Down
4 changes: 3 additions & 1 deletion application/lucre/lucre.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace LucreApp
auto position = glm::vec3(0.0f, 0.0f, 1.0f);
auto direction = glm::vec3(0.0f, 0.0f, -1.0f);
camera.SetViewDirection(position, direction);
#define STRESS_TEST
// #define STRESS_TEST
#ifdef STRESS_TEST
{ // stress test scene changing
std::srand(std::time(nullptr));
Expand Down Expand Up @@ -130,7 +130,9 @@ namespace LucreApp
void Lucre::Shutdown()
{
m_GameState.Stop();
#ifdef STRESS_TEST
m_StressTestFuture.get();
#endif
}

void Lucre::OnUpdate(const Timestep& timestep)
Expand Down
Binary file added application/lucre/models/ice/darkWater.glb
Binary file not shown.
Binary file added application/lucre/models/ice/iceberg1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/iceberg1_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/iceberg2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/iceberg2_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/iceberg3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/iceberg3_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/iceberg4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/iceberg4_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/iceberg5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/iceberg5_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/nx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/ny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/nz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/penguin.glb
Binary file not shown.
Binary file added application/lucre/models/ice/px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/py.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/pz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added application/lucre/models/ice/sailboat.glb
Binary file not shown.
Binary file added application/lucre/models/ice/snowflake1.glb
Binary file not shown.
Binary file added application/lucre/models/ice/snowflake2.glb
Binary file not shown.
Binary file added application/lucre/models/ice/snowflake3.glb
Binary file not shown.
Binary file added application/lucre/models/ice/snowflake4.glb
Binary file not shown.
237 changes: 237 additions & 0 deletions application/lucre/particleSystem/snow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
/* Engine Copyright (c) 2024 Engine Development Team
https://github.com/beaumanvienna/vulkan
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#include "simdjson.h"
#include "auxiliary/file.h"
#include "auxiliary/random.h"
#include "particleSystem/snow.h"
#include "renderer/instanceBuffer.h"
#include "renderer/builder/fastgltfBuilder.h"

using namespace simdjson;

namespace GfxRenderEngine
{
Snow::Snow(Scene& scene, std::string const& jsonFile)
: m_Scene{scene}, m_Registry{scene.GetRegistry()}, m_Dictionary{scene.GetDictionary()}
{
// load JSON particle system description
ParseSysDescription(jsonFile);
if (!m_Initialized)
{
LOG_CORE_CRITICAL("Snow::Snow failed to initialize! (ParseSysDescription)");
return;
}

// load model with m_PoolSize instances
std::vector<entt::entity> snowflakeFirstInstances;
{
auto& dictionaryPrefix = m_SysDescription.m_DictionaryPrefix.value();
auto& model = m_SysDescription.m_Model.value();
auto& sceneGraph = m_Scene.GetSceneGraph();
auto& numberOfInstances = m_SysDescription.m_PoolSize.value();

// create group node
auto entity = m_Registry.Create();
auto name = dictionaryPrefix + "::" + model + "::root";
int groupNode = sceneGraph.CreateNode(SceneGraph::ROOT_NODE, entity, name, m_Dictionary);
TransformComponent transform{};
m_Registry.emplace<TransformComponent>(entity, transform);

FastgltfBuilder builder(model, m_Scene, groupNode);
builder.SetDictionaryPrefix(dictionaryPrefix);
m_Initialized = builder.Load(numberOfInstances, snowflakeFirstInstances, NO_SCENE_GRAPH);
}

if (!m_Initialized || !snowflakeFirstInstances.size())
{
LOG_CORE_CRITICAL("Snow::Snow failed to initialize! (load model)");
return;
}

// set up particles
{
m_ParticlePool.resize(m_SysDescription.m_PoolSize.value());
auto prefix = std::string(m_SysDescription.m_DictionaryPrefix.value()) + "::";
auto& vertex1 = m_SysDescription.m_Vertex1.value();
auto& vertex2 = m_SysDescription.m_Vertex2.value();
auto volumeSize = (vertex2 - vertex1) / 2.0f;
auto& snowflake = snowflakeFirstInstances[0];

if (snowflake != entt::null)
{
auto& instances = m_Registry.get<InstanceTag>(snowflake).m_Instances;
uint index{0};
for (auto& particle : m_ParticlePool)
{
particle.m_RotationSpeed = {0.0f, 0.0f, EngineCore::RandomPlusMinusOne()};
particle.m_Velocity = {0.0f, -1.0f + EngineCore::RandomPlusMinusOne(), 0.0f};

auto& transform = m_Registry.get<TransformComponent>(instances[index]);
particle.m_Transform = &transform;
transform.SetRotation(
{glm::half_pi<float>(), 0.0f, glm::pi<float>() * EngineCore::RandomPlusMinusOne()});
transform.SetTranslation(
{(vertex1.x + volumeSize.x) + (volumeSize.x * EngineCore::RandomPlusMinusOne()),
(vertex1.y + volumeSize.y) + (volumeSize.y * EngineCore::RandomPlusMinusOne()),
(vertex1.z + volumeSize.z) + (volumeSize.z * EngineCore::RandomPlusMinusOne())});
transform.SetScale(0.014f);
++index;
}
}
}
}

void Snow::OnUpdate(Timestep timestep, TransformComponent& cameraTransform)
{
ZoneScopedNC("Snow::OnUpdate", 0x00ff00);
auto& vertex1 = m_SysDescription.m_Vertex1.value();
auto& vertex2 = m_SysDescription.m_Vertex2.value();
for (auto& particle : m_ParticlePool)
{
auto& transform = *particle.m_Transform;
float rotationSpeedZ = particle.m_RotationSpeed.z;
transform.AddRotation({0.0f, 0.0f, timestep * rotationSpeedZ});
transform.SetRotationY(cameraTransform.GetRotation().y);

transform.AddTranslation(timestep * particle.m_Velocity);
if (transform.GetTranslation().y <= vertex1.y)
{
transform.AddTranslation({0.0f, vertex2.y + vertex1.y, 0.0f});
}
transform.SetMat4Global();
}
}

void Snow::ParseSysDescription(std::string const& jsonFile)
{
auto path = std::string("application/lucre/particleSystem/") + jsonFile;
if (!EngineCore::FileExists(path))
{
LOG_CORE_CRITICAL("particle system description not found: {0}", path);
return;
}

auto convertToVec3 = [](ondemand::array arrayJSON)
{
glm::vec3 returnVec3{0.0f};
uint componentIndex = 0;
for (auto component : arrayJSON)
{
switch (componentIndex)
{
case 0:
{
returnVec3.x = component.get_double();
break;
}
case 1:
{
returnVec3.y = component.get_double();
break;
}
case 2:
{
returnVec3.z = component.get_double();
break;
}
default:
{
break;
}
}
++componentIndex;
}
CORE_ASSERT(componentIndex == 3, "convertToVec3(...) argument must have 3 components");
return returnVec3;
};

LOG_CORE_INFO("loading particle system: {0}", path);
ondemand::parser parser;
padded_string json = padded_string::load(path);

ondemand::document sceneDocument = parser.iterate(json);
ondemand::object sceneObjects = sceneDocument.get_object();

for (auto sceneObject : sceneObjects)
{
std::string_view sceneObjectKey = sceneObject.unescaped_key();

if (sceneObjectKey == "file format identifier")
{
CORE_ASSERT((sceneObject.value().type() == ondemand::json_type::number), "type must be number");
// only check major version of "file format identifier"
double fileFormatIdentifier = sceneObject.value().get_double();
CORE_ASSERT((std::trunc(fileFormatIdentifier) == std::trunc(SUPPORTED_FILE_FORMAT_VERSION)),
"The scene description major version does not match");
}
else if (sceneObjectKey == "description")
{
CORE_ASSERT((sceneObject.value().type() == ondemand::json_type::string), "type must be string");
std::string_view sceneDescription = sceneObject.value().get_string();
LOG_CORE_INFO("description: {0}", sceneDescription);
}
else if (sceneObjectKey == "author")
{
CORE_ASSERT((sceneObject.value().type() == ondemand::json_type::string), "type must be string");
std::string_view sceneAuthor = sceneObject.value().get_string();
LOG_CORE_INFO("author: {0}", sceneAuthor);
}
else if (sceneObjectKey == "model")
{
CORE_ASSERT((sceneObject.value().type() == ondemand::json_type::string), "type must be string");
std::string_view model = sceneObject.value().get_string();
m_SysDescription.m_Model = model;
LOG_CORE_INFO("author: {0}", model);
}
else if (sceneObjectKey == "pool size")
{
CORE_ASSERT((sceneObject.value().type() == ondemand::json_type::number), "type must be number");
int64 poolSize = sceneObject.value().get_int64();
m_SysDescription.m_PoolSize = poolSize;
}
else if (sceneObjectKey == "prefix dictionary")
{
CORE_ASSERT((sceneObject.value().type() == ondemand::json_type::string), "type must be string");
std::string_view prefix = sceneObject.value().get_string();
m_SysDescription.m_DictionaryPrefix = prefix;
}
else if (sceneObjectKey == "cubic volume vertex 0,0,0")
{
CORE_ASSERT((sceneObject.value().type() == ondemand::json_type::array), "type must be array");
m_SysDescription.m_Vertex1 = convertToVec3(sceneObject.value());
}
else if (sceneObjectKey == "cubic volume vertex 1,1,1")
{
CORE_ASSERT((sceneObject.value().type() == ondemand::json_type::array), "type must be array");
m_SysDescription.m_Vertex2 = convertToVec3(sceneObject.value());
}
}
auto& descr = m_SysDescription;
m_Initialized = descr.m_Model.has_value() && //
descr.m_PoolSize.has_value() && //
descr.m_DictionaryPrefix.has_value() && //
descr.m_Vertex1.has_value() && //
descr.m_Vertex2.has_value(); //
CORE_ASSERT(m_Initialized, "JSON particle system description did not load properly");
}
} // namespace GfxRenderEngine
70 changes: 70 additions & 0 deletions application/lucre/particleSystem/snow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* Engine Copyright (c) 2024 Engine Development Team
https://github.com/beaumanvienna/vulkan
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */

#pragma once

#include "engine.h"
#include "scene/scene.h"
#include "scene/components.h"
#include "auxiliary/timestep.h"

namespace GfxRenderEngine
{
class Snow
{

public:
Snow(Scene& scene, std::string const& jsonFile);
void OnUpdate(Timestep timestep, TransformComponent& cameraTransform);

private:
struct SysDescription
{
std::optional<uint> m_PoolSize{0};
std::optional<std::string> m_Model;
std::optional<std::string> m_DictionaryPrefix;
std::optional<glm::vec3> m_Vertex1; // 0,0,0 coordinate for cubic snow volume
std::optional<glm::vec3> m_Vertex2; // 1,1,1 coordinate for cubic snow volume
};

struct Particle
{
glm::vec3 m_Velocity;
glm::vec3 m_RotationSpeed;
TransformComponent* m_Transform;
};

private:
static constexpr double SUPPORTED_FILE_FORMAT_VERSION = 1.2;
void ParseSysDescription(std::string const& jsonFile);

private:
static constexpr bool NO_SCENE_GRAPH = false;
Scene& m_Scene;
Registry& m_Registry;
Dictionary& m_Dictionary;
SysDescription m_SysDescription;
bool m_Initialized{false};
uint m_PoolIndex{0};
std::vector<Particle> m_ParticlePool;
};
} // namespace GfxRenderEngine
10 changes: 10 additions & 0 deletions application/lucre/particleSystem/snow1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"file format identifier": 1.2,
"description": "snow particle system 1",
"author": "Copyright (c) 2024 Engine Development Team",
"model": "application/lucre/models/ice/snowflake1.glb",
"pool size": 4000,
"prefix dictionary": "snow",
"cubic volume vertex 0,0,0": [-60.0, -1.0, -20.0],
"cubic volume vertex 1,1,1": [40.0, 15.0, 80.0]
}
10 changes: 10 additions & 0 deletions application/lucre/particleSystem/snow2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"file format identifier": 1.2,
"description": "snow particle system 2",
"author": "Copyright (c) 2024 Engine Development Team",
"model": "application/lucre/models/ice/snowflake2.glb",
"pool size": 4000,
"prefix dictionary": "snow",
"cubic volume vertex 0,0,0": [-30.0, -1.0, -10.0],
"cubic volume vertex 1,1,1": [20.0, 15.0, 40.0]
}
10 changes: 10 additions & 0 deletions application/lucre/particleSystem/snow3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"file format identifier": 1.2,
"description": "snow particle system 3",
"author": "Copyright (c) 2024 Engine Development Team",
"model": "application/lucre/models/ice/snowflake3.glb",
"pool size": 4000,
"prefix dictionary": "snow",
"cubic volume vertex 0,0,0": [-15.0, -1.0, -5.0],
"cubic volume vertex 1,1,1": [10.0, 10.0, 20.0]
}
10 changes: 10 additions & 0 deletions application/lucre/particleSystem/snow4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"file format identifier": 1.2,
"description": "snow particle system 4",
"author": "Copyright (c) 2024 Engine Development Team",
"model": "application/lucre/models/ice/snowflake4.glb",
"pool size": 4000,
"prefix dictionary": "snow",
"cubic volume vertex 0,0,0": [-120.0, -1.0, -40.0],
"cubic volume vertex 1,1,1": [80.0, 15.0, 160.0]
}
Loading

0 comments on commit 594538b

Please sign in to comment.