Serialization work

This commit is contained in:
Antoine Pilote
2021-05-15 23:57:26 -04:00
parent b53d2e729e
commit 5f364dabb2
27 changed files with 368 additions and 305 deletions

View File

@@ -1,7 +1,8 @@
#pragma once
#include "TransformComponent.h"
#include "../Core/Core.h"
class __declspec(dllexport) CameraComponent {
#include "../Resource/Serializable.h"
class CameraComponent {
public:
Ref<Camera> CameraInstance;
TransformComponent* transformComponent;
@@ -9,4 +10,11 @@ public:
CameraComponent();
void DrawEditor();
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_OBJECT(CameraInstance);
END_SERIALIZE();
}
};

View File

@@ -5,13 +5,13 @@
#include "../Rendering/Camera.h"
#include "../../../Rendering/Framebuffer.h"
#include "BaseComponent.h";
#include "../Resource/Serializable.h"
enum LightType {
Directional, Point, Spot
};
class __declspec(dllexport) LightComponent {
class LightComponent {
public:
glm::vec2 yes = glm::vec2(2, 2);
@@ -201,4 +201,17 @@ public:
void SetType(LightType type);
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(Type);
SERIALIZE_VEC3(Direction);
SERIALIZE_VEC3(Color);
SERIALIZE_VAL(IsVolumetric);
SERIALIZE_VAL(Strength);
SERIALIZE_VAL(SyncDirectionWithSky);
SERIALIZE_VAL(CastShadows);
END_SERIALIZE();
}
};

View File

@@ -3,7 +3,7 @@
#include <glm\ext\matrix_float4x4.hpp>
#include "BaseComponent.h";
class __declspec(dllexport) MeshComponent {
class MeshComponent {
private:
unsigned int VAO;

View File

@@ -1,5 +1,14 @@
#pragma once
#include "../Resource/Serializable.h"
class NameComponent {
public:
std::string Name = "Entity";
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(Name);
END_SERIALIZE();
}
};

View File

@@ -2,10 +2,18 @@
#include "../Entity.h"
struct ParentComponent
{
Entity Parent;
bool HasParent = false;
std::vector<Entity> Children = std::vector<Entity>();
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(HasParent);
if(HasParent)
SERIALIZE_VAL_LBL("Parent", Parent.GetHandle());
END_SERIALIZE();
}
};

View File

@@ -3,7 +3,9 @@
#include <vector>
#include "../Rendering/Mesh/Mesh.h"
#include "../Resource/TrenchbroomMap.h"
class __declspec(dllexport) QuakeMap {
#include "../Resource/Serializable.h"
class QuakeMap {
private:
@@ -18,4 +20,11 @@ public:
void Draw();
void DrawEditor();
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(HasCollisions);
SERIALIZE_VAL(Path);
END_SERIALIZE();
}
};

View File

@@ -32,7 +32,6 @@ float RigidBodyComponent::GetMass() {
return 0.0f;
}
void RigidBodyComponent::SetMass(float m)
{
if (!m_Rigidbody)

View File

@@ -1,7 +1,8 @@
#pragma once
#include <glm\ext\matrix_float4x4.hpp>
#include "BaseComponent.h";
class __declspec(dllexport) TransformComponent {
#include "../Resource/Serializable.h"
class TransformComponent {
public:
glm::vec3 Translation;
glm::vec3 Rotation;
@@ -11,4 +12,14 @@ public:
glm::mat4 GetTransform();
void DrawEditor();
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL_LBL("Type", "TransformComponent");
SERIALIZE_VEC3(Translation);
SERIALIZE_VEC3(Rotation);
SERIALIZE_VEC3(Scale);
END_SERIALIZE();
}
};

View File

@@ -2,6 +2,11 @@
#include "Components/ParentComponent.h"
#include "Entity.h"
#include "Components/NameComponent.h"
#include "Components/TransformComponent.h"
#include "Components/CameraComponent.h"
#include "Components/QuakeMap.h"
#include "Components/LightComponent.h"
void Entity::AddChild(Entity ent)
{
if ((int)m_EntityHandle != ent.GetHandle())
@@ -13,6 +18,26 @@ void Entity::AddChild(Entity ent)
}
}
json Entity::Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_OBJECT_REF_LBL("NameComponent", GetComponent<NameComponent>());
SERIALIZE_OBJECT_REF_LBL("ParentComponent", GetComponent<ParentComponent>());
SERIALIZE_OBJECT_REF_LBL("TransformComponent", GetComponent<TransformComponent>());
if(HasComponent<CameraComponent>())
SERIALIZE_OBJECT_REF_LBL("CameraComponent", GetComponent<CameraComponent>());
if(HasComponent<QuakeMap>())
SERIALIZE_OBJECT_REF_LBL("QuakemapComponent", GetComponent<QuakeMap>());
if (HasComponent<LightComponent>())
SERIALIZE_OBJECT_REF_LBL("LightComponent", GetComponent<LightComponent>());
END_SERIALIZE();
}
bool Entity::Deserialize(const std::string& str)
{
return false;
}
Entity::Entity(entt::entity handle, Scene* scene)
{
m_EntityHandle = handle;

View File

@@ -3,7 +3,9 @@
#include <glm\ext\matrix_float4x4.hpp>
#include "../Scene.h"
#include "Components/BaseComponent.h"
class __declspec(dllexport) Entity
#include "../Resource/Serializable.h"
class __declspec(dllexport) Entity : public ISerializable
{
public:
Entity(entt::entity handle, Scene* scene);
@@ -49,6 +51,10 @@ public:
{
return !(*this == other);
}
json Serialize() override;
bool Deserialize(const std::string& str);
private:
entt::entity m_EntityHandle;
Scene* m_Scene;

View File

@@ -24,4 +24,19 @@ void Environment::Push() {
//Renderer::m_Shader->SetUniform4f("u_AmbientColor", 1.0f, 1.0f, 1.0f, 1.0f);
Renderer::m_Shader->SetUniform1f("u_FogAmount", VolumetricFog);
Renderer::m_Shader->SetUniform1f("u_FogStepCount", VolumetricStepCount);
}
}
json Environment::Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(VolumetricFog);
SERIALIZE_VAL(VolumetricStepCount);
SERIALIZE_VEC4(AmbientColor);
SERIALIZE_OBJECT(ProceduralSkybox);
END_SERIALIZE();
}
bool Environment::Deserialize(const std::string& str)
{
return false;
}

View File

@@ -4,7 +4,8 @@
#include "../Core/Core.h"
#include "../Rendering/ProceduralSky.h"
class __declspec(dllexport) Environment
#include "../Resource/Serializable.h"
class Environment : public ISerializable
{
public:
Environment();
@@ -15,10 +16,11 @@ public:
Ref<ProceduralSky> ProceduralSkybox;
glm::vec4 m_AmbientColor;
glm::vec4 GetAmbientColor();
void SetAmbientColor(glm::vec4 color);
void Push();
json Serialize() override;
bool Deserialize(const std::string& str) override;
};

View File

@@ -9,29 +9,40 @@
#include <GL/glew.h>
#include "Entities/Components/BoxCollider.h"
#include "../../Engine.h"
#include "../Core/FileSystem.h"
#include <fstream>
#include <streambuf>
Ref<Scene> Scene::New()
{
return CreateRef<Scene>();
}
Scene::Scene()
{
m_Environement = CreateRef<Environment>();
auto camEntity = CreateEntity("Camera");
camEntity.AddComponent<CameraComponent>().transformComponent = &camEntity.GetComponent<TransformComponent>();
m_EditorCamera = CreateRef<EditorCamera>();
}
Scene::~Scene() {
}
void Scene::Init() {
//m_Skybox = new SkyboxHDR("Res/Textures/Skyboxes/HDR/lilienstein_4k.hdr");
m_Environement = CreateRef<Environment>();
}
Scene::~Scene() {}
std::string Scene::GetName()
{
return this->Name;
}
bool Scene::SetName(std::string& newName)
{
if (newName == "")
return false;
this->Name = newName;
return true;
}
void Scene::OnInit()
{
@@ -145,7 +156,6 @@ void Scene::Update(Timestep ts)
void Scene::EditorUpdate(Timestep ts)
{
m_EditorCamera->Update(ts);
}
@@ -206,72 +216,6 @@ void Scene::DrawShadows()
}
}
void Scene::DrawGBuffer()
{
Renderer::m_GBufferShader->Bind();
Ref<Camera> cam = nullptr;
{
auto view = m_Registry.view<TransformComponent, CameraComponent>();
for (auto e : view) {
auto [transform, camera] = view.get<TransformComponent, CameraComponent>(e);
cam = camera.CameraInstance;
cam->Translation = transform.Translation;
break;
}
}
if (cam)
{
auto view = m_Registry.view<TransformComponent, ModelComponent>();
for (auto e : view) {
auto [transform, model] = view.get<TransformComponent, ModelComponent>(e);
Renderer::m_GBufferShader->SetUniformMat4f("u_View", cam->GetTransform());
Renderer::m_GBufferShader->SetUniformMat4f("u_Projection", cam->GetPerspective());
Renderer::m_GBufferShader->SetUniformMat4f("u_Model", transform.GetTransform());
model.Draw();
}
auto view2 = m_Registry.view<TransformComponent, QuakeMap>();
for (auto e : view2) {
auto [transform, model] = view2.get<TransformComponent, QuakeMap>(e);
Renderer::m_GBufferShader->SetUniformMat4f("u_View", cam->GetTransform());
Renderer::m_GBufferShader->SetUniformMat4f("u_Projection", cam->GetPerspective());
Renderer::m_GBufferShader->SetUniformMat4f("u_Model", transform.GetTransform());
model.Draw();
}
}
}
void Scene::DrawDeferred()
{
// Find the camera of the scene.
Ref<Camera> cam = nullptr;
{
auto view = m_Registry.view<TransformComponent, CameraComponent>();
for (auto e : view) {
auto [transform, camera] = view.get<TransformComponent, CameraComponent>(e);
cam = camera.CameraInstance;
break;
}
}
{
auto view = m_Registry.view<TransformComponent, LightComponent>();
for (auto l : view) {
auto [transform, light] = view.get<TransformComponent, LightComponent>(l);
//light.DrawDeferred(transform, cam);
}
}
//m_Skybox->Draw(cam->GetPerspective(), cam->GetTransform());
if (m_Skybox != nullptr)
m_Skybox->Push();
glm::vec3 camPos = cam->GetTranslation();
Renderer::m_DeferredShader->SetUniform1f("u_Exposure", cam->Exposure);
Renderer::m_DeferredShader->SetUniform3f("u_EyePosition", camPos.x, camPos.y, camPos.z);
Renderer::m_DeferredShader->SetUniformMat4f("u_View", cam->GetTransform());
Renderer::m_DeferredShader->SetUniformMat4f("u_Projection", cam->GetPerspective());
}
void Scene::Draw()
{
@@ -433,7 +377,6 @@ void Scene::EditorDraw()
{
currentParent = currentParent.GetComponent<ParentComponent>().Parent;
globalOffset += currentParent.GetComponent<TransformComponent>().Translation;
}
copyT.Translation = globalOffset;
@@ -589,7 +532,7 @@ glm::vec3 Scene::GetGlobalPosition(Entity ent)
}
Entity Scene::GetEntity(const std::string name)
Entity Scene::GetEntity(const std::string& name)
{
std::vector<Entity> allEntities;
auto view = m_Registry.view<TransformComponent, NameComponent>();
@@ -600,7 +543,7 @@ Entity Scene::GetEntity(const std::string name)
}
}
Entity Scene::CreateEntity(const std::string name) {
Entity Scene::CreateEntity(const std::string& name) {
Entity entity = { m_Registry.create(), this };
// Must have transform
@@ -635,7 +578,6 @@ Ref<Camera> Scene::GetCurrentCamera()
}
}
return cam;
}
return m_EditorCamera;
@@ -644,3 +586,39 @@ Ref<Camera> Scene::GetCurrentCamera()
Ref<Environment> Scene::GetEnvironment() {
return m_Environement;
}
bool Scene::Save()
{
if (Path == "")
Path = FileDialog::SaveFile("*.scene") + ".scene";
return SaveAs(Path);
}
bool Scene::SaveAs(const std::string& path)
{
std::ofstream sceneFile;
sceneFile.open(path);
sceneFile << Serialize().dump(4);
sceneFile.close();
return true;
}
json Scene::Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(Name);
SERIALIZE_OBJECT(m_Environement)
std::vector<json> entities = std::vector<json>();
for (Entity e : GetAllEntities())
entities.push_back(e.Serialize());
SERIALIZE_VAL_LBL("Entities", entities);
END_SERIALIZE();
}
bool Scene::Deserialize(const std::string& str)
{
return false;
}

View File

@@ -8,9 +8,10 @@
#include "../Rendering/ProceduralSky.h"
#include "../Core/Core.h"
#include "EditorCamera.h"
#include "../Resource/Serializable.h"
class Entity;
class __declspec(dllexport) Scene
class Scene : public ISerializable
{
friend Entity;
@@ -18,13 +19,17 @@ private:
Ref<Environment> m_Environement;
entt::registry m_Registry;
Ref<EditorCamera> m_EditorCamera;
std::string Name;
std::string Path = "";
bool has_changed = true;
public:
SkyboxHDR* m_Skybox;
static Ref<Scene> New();
Scene();
~Scene();
std::string GetName();
bool SetName(std::string& newName);
void Init();
void OnInit();
void OnExit();
@@ -39,10 +44,17 @@ public:
std::vector<Entity> GetAllEntities();
glm::vec3 GetGlobalPosition(Entity ent);
Entity GetEntity(const std::string name);
Entity CreateEntity(const std::string name);
Entity GetEntity(const std::string& name);
Entity CreateEntity(const std::string& name);
void DestroyEntity(Entity entity);
Ref<Camera> GetCurrentCamera();
Ref<Environment> GetEnvironment();
bool Save();
bool SaveAs(const std::string& path);
json Serialize() override;
bool Deserialize(const std::string& str) override;
};