Skip to content

Commit

Permalink
Merge pull request #37 from damian-tomczak/dev/damian
Browse files Browse the repository at this point in the history
refactoring
  • Loading branch information
damian-tomczak authored Nov 9, 2023
2 parents 119c1da + e104adb commit 7ac70ee
Show file tree
Hide file tree
Showing 46 changed files with 479 additions and 351 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.26)

# TODO: cmake configuration time weird increased last time - investigate it
# TODO: cmake configuration time weirdly increased last time - investigate it
project(ts VERSION 0.0.1 LANGUAGES CXX)

set(GAME_NAME "Awesome Game" CACHE STRING "Game name")
Expand Down
1 change: 1 addition & 0 deletions assets/shaders/common.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once

#define PI 3.14159265359
#define LIGHTS_N 2
15 changes: 8 additions & 7 deletions assets/shaders/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_multiview : enable
#extension GL_EXT_debug_printf : enable
#extension GL_EXT_nonuniform_qualifier : enable

#include "assets/shaders/common.h"

Expand All @@ -17,7 +18,7 @@ layout (binding = 1) uniform CommonUbo {
} commonUbo;

layout (binding = 2) uniform LightsUbo {
vec3 lights[2];
vec3 positions[LIGHTS_N];
} lightsUbo;

layout(push_constant) uniform Material {
Expand All @@ -38,7 +39,8 @@ float D_GGX(float dotNH, float roughness)
float alpha = roughness * roughness;
float alpha2 = alpha * alpha;
float denom = dotNH * dotNH * (alpha2 - 1.0) + 1.0;
return (alpha2)/(PI * denom*denom);

return alpha2 / (PI * denom*denom);
}

float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness)
Expand All @@ -47,13 +49,15 @@ float G_SchlicksmithGGX(float dotNL, float dotNV, float roughness)
float k = (r*r) / 8.0;
float GL = dotNL / (dotNL * (1.0 - k) + k);
float GV = dotNV / (dotNV * (1.0 - k) + k);

return GL * GV;
}

vec3 F_Schlick(float cosTheta, float metallic)
{
vec3 F0 = mix(vec3(0.04), materialcolor(), metallic);
vec3 F = F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);

return F;
}

Expand Down Expand Up @@ -88,9 +92,6 @@ void main()
{
vec3 N = normalize(inNormal);

// vec3 var = vec4(inverse(commonUbo.viewMats[gl_ViewIndex]) * vec4(-commonUbo.camPos - inWorldPos, 1.0)).xyz;
// vec3 V = normalize(vec3(var.x, var.y + 1.0, var.z));

vec3 temp = vec3(commonUbo.viewMats[gl_ViewIndex][3]);
vec3 v1 = vec3(
commonUbo.camPos.x + temp.x,
Expand All @@ -100,9 +101,9 @@ void main()
vec3 V = normalize(-v1 - inWorldPos);

vec3 Lo = vec3(0.0);
for (int i = 0; i < lightsUbo.lights.length(); i++)
for (int i = 0; i < lightsUbo.positions.length(); i++)
{
vec3 L = normalize(lightsUbo.lights[i] - inWorldPos);
vec3 L = normalize(lightsUbo.positions[i] - inWorldPos);
Lo += BRDF(L, V, N, material.metallic, material.roughness);
};

Expand Down
2 changes: 2 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ if(MSVC)
$<$<CONFIG:Release>: /O2 /MD>
/W3 /WX /wd4710
)
else()
message(FATAL_ERROR "not implemented")
endif()

if(ENABLE_TESTS)
Expand Down
2 changes: 1 addition & 1 deletion engine/include/tsengine/asset_store.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "utils.hpp"

#include "tsengine/ecs/ecs.hpp"
#include "tsengine/ecs/ecs.h"
#include "tsengine/ecs/components/mesh_component.hpp"

namespace ts
Expand Down
6 changes: 3 additions & 3 deletions engine/include/tsengine/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Engine
Engine(const Engine&&) = delete;
Engine& operator=(Engine&&) = delete;

virtual bool init(const char*& gameName, unsigned& width, unsigned& height) = 0;
virtual void loadLvL() {};
virtual bool init(const char*& gameName, unsigned& width, unsigned& height);
virtual void loadLvL() = 0;
virtual bool tick() = 0;
virtual void close() = 0;
virtual void close();
};

int run(Engine* const engine);
Expand Down
5 changes: 4 additions & 1 deletion engine/include/tsengine/ecs/components/asset_component.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "tsengine/utils.hpp"

namespace ts
{
class AssetStore;
Expand All @@ -8,11 +10,12 @@ struct AssetComponent : public Component
{
using Base = AssetComponent;

AssetComponent(const std::string_view assetName_ = "") : assetName{assetName_}
AssetComponent(const std::string_view assetName_ = "") : assetName{assetName_}, assetNameId{std::hash<std::string_view>{}(assetName_)}
{}

protected:
friend AssetStore;
std::string assetName;
size_t assetNameId;
};
}
31 changes: 23 additions & 8 deletions engine/include/tsengine/ecs/components/renderer_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace ts
{
class Renderer;

// TODO: enum reflection
// TODO: implement color

Expand All @@ -27,23 +29,31 @@ enum class PipelineType
struct RendererComponentBase : public Component
{
using Base = RendererComponentBase;
using ZIdxT = int32_t;

RendererComponentBase(const ZIdxT z_ = {}) : z{z_} {}

ZIdxT z;
};

template<PipelineType pipeType>
struct RendererComponent : public RendererComponentBase
{
PipelineType pipeline{pipeType};

RendererComponent(const ZIdxT z_ = {}) : RendererComponentBase{z_}
{}
};

template<>
struct RendererComponent<PipelineType::PBR> : public RendererComponentBase
{
#define TS_MATERIALS_LIST \
MATERIAL(WHITE, .color = { 1.0f, 1.0f, 1.0f}, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(RED, .color = { 1.0f, 0.0f, 0.0f}, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(BLUE, .color = { 0.0f, 0.0f, 1.0f}, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(BLACK, .color = { 0.0f, 0.0f, 0.0f}, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(GOLD, .color = { 1.0f, 0.765557f, 0.336057f}, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(WHITE, .color = {1.0f , 1.0f ,1.0f }, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(RED, .color = {1.0f , 0.0f ,0.0f }, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(BLUE, .color = {0.0f , 0.0f ,1.0f }, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(BLACK, .color = {0.0f , 0.0f ,0.0f }, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(GOLD, .color = {1.0f , 0.765557f, 0.336057f}, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(COPPER, .color = {0.955008f, 0.637427f, 0.538163f}, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(CHROMIUM, .color = {0.549585f, 0.556114f, 0.554256f}, .roughness = 0.5f, .metallic = 1.0f) \
MATERIAL(NICKEL, .color = {0.659777f, 0.608679f, 0.525649f}, .roughness = 0.5f, .metallic = 1.0f) \
Expand All @@ -66,9 +76,10 @@ struct RendererComponent<PipelineType::PBR> : public RendererComponentBase
{
switch (type)
{
#define MATERIAL(type, ...) \
case Material::Type::type: \
#define MATERIAL(type, ...) \
case Material::Type::type: \
return Material{__VA_ARGS__};

TS_MATERIALS_LIST
#undef MATERIAL
default: throw Exception{"Invalid material type"};
Expand All @@ -82,6 +93,10 @@ struct RendererComponent<PipelineType::PBR> : public RendererComponentBase
float metallic;
};

Material material{Material::create(Material::Type::RED)};
Material material;

RendererComponent(const Material material_ = Material::create(Material::Type::RED), const ZIdxT z_ = {})
: RendererComponentBase{z_}, material{material_}
{}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct TransformComponent : public Component
math::Vec3 pos;
math::Mat4 modelMat{1.f};

TransformComponent(const math::Vec3 pos_ = math::Vec3{1.f}) : pos{pos_}
TransformComponent(const math::Vec3 pos_ = math::Vec3{0.f}) : pos{pos_}
{}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Pool : public IPool
const T& operator [](const Id index) const { return data.at(index); }
};

inline class Registry
class Registry
{
std::unordered_map<std::type_index, std::shared_ptr<System>> systems;
std::unordered_map<std::string, Entity> entityPerTag;
Expand Down Expand Up @@ -176,7 +176,9 @@ inline class Registry

void addEntityToSystems(const Entity entity);
void removeEntityFromSystems(const Entity entity);
} gRegistry;
};

Registry& getMainReg();

// Entity

Expand Down Expand Up @@ -367,7 +369,7 @@ inline Entity Registry::createEntity()

const auto [entity, isQueued] = entitiesToBeAdded.emplace(entityId, this);

TS_ASSERT(isQueued, "Entity couldn't be added to the queue of entities to be added.");
TS_ASSERT_MSG(isQueued, "Entity couldn't be added to the queue of entities to be added.");

return *entity;
}
Expand All @@ -380,8 +382,6 @@ inline void Registry::tagEntity(const Entity entity, const std::string& tag)

inline bool Registry::entityHasTag(const Entity entity, const std::string& tag) const
{
TS_ASSERT(tagPerEntity.find(entity.getId()) != tagPerEntity.end(), "Entity doesn't have tag");

return entityPerTag.find(tag)->second == entity;
}

Expand Down Expand Up @@ -447,7 +447,7 @@ void Registry::addSystem(TArgs&& ...args)
{
const auto index = std::type_index(typeid(TSystem));

TS_ASSERT(systems.find(index) == systems.end(), "System is already added");
TS_ASSERT_MSG(systems.find(index) == systems.end(), "System is already added");

const auto newSystem = std::make_shared<TSystem>(std::forward<TArgs>(args)...);
systems.insert(std::make_pair(std::type_index(typeid(TSystem)), newSystem));
Expand Down Expand Up @@ -479,7 +479,7 @@ void Registry::addComponent(const Entity entity, TArgs&& ...args)
{
if constexpr (!is_component_of_v<typename TComponent::Base>)
{
_addComponent<typename TComponent::Base>(entity, std::forward<TArgs>(args)...);
_addComponent<typename TComponent::Base>(entity);
}

_addComponent<TComponent>(entity, std::forward<TArgs>(args)...);
Expand Down
24 changes: 15 additions & 9 deletions engine/include/tsengine/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@

#define NOT_PRINT_LINE_NUMBER -1

#define TS_LOG(message) ts::logger::log(message, __FILE__, FUNCTION_SIGNATURE, __LINE__)
#define TS_WARN(message) ts::logger::warning(message, __FILE__, FUNCTION_SIGNATURE, __LINE__)
#define TS_ERR(message) ts::logger::error(message, __FILE__, FUNCTION_SIGNATURE, __LINE__)
#define TS_LOG(msg) ts::logger::log(msg, __FILE__, FUNCTION_SIGNATURE, __LINE__)
#define TS_WARN(msg) ts::logger::warning(msg, __FILE__, FUNCTION_SIGNATURE, __LINE__)
#define TS_ERR(msg) ts::logger::error(msg, __FILE__, FUNCTION_SIGNATURE, __LINE__)

// TODO: default message
#ifndef NDEBUG
#define TS_ASSERT(condition, ...) \
if (!(condition)) \
{ \
ts::logger::warning(__VA_ARGS__, __FILE__, FUNCTION_SIGNATURE, __LINE__, true); \
#define TS_ASSERT(condition) \
if (!(condition)) \
{ \
ts::logger::warning("Assertion Failed: " #condition, __FILE__, FUNCTION_SIGNATURE, __LINE__, true); \
}

#define TS_ASSERT_MSG(condition, msg) \
if (!(condition)) \
{ \
ts::logger::warning(msg, __FILE__, FUNCTION_SIGNATURE, __LINE__, true); \
}

#else
#define TS_ASSERT(condition, ...)
#define TS_ASSERT(condition)
#define TS_ASSERT_MSG(condition, msg)
#endif

namespace ts
Expand Down
26 changes: 13 additions & 13 deletions engine/include/tsengine/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

// TODO: Remove the need of providing class name - reflection

#define NOT_COPYABLE(ClassName) \
#define TS_NOT_COPYABLE(ClassName) \
ClassName(const ClassName&) = delete; \
ClassName& operator=(const ClassName&) = delete;

#define NOT_MOVEABLE(ClassName) \
#define TS_NOT_MOVEABLE(ClassName) \
ClassName(ClassName&&) = delete; \
ClassName& operator=(ClassName&&) = delete;

#define NOT_COPYABLE_AND_MOVEABLE(ClassName) \
NOT_COPYABLE(ClassName); \
NOT_MOVEABLE(ClassName);
#define TS_NOT_COPYABLE_AND_MOVEABLE(ClassName) \
TS_NOT_COPYABLE(ClassName); \
TS_NOT_MOVEABLE(ClassName);

#define SINGLETON_BODY(ClassName) \
NOT_COPYABLE_AND_MOVEABLE(ClassName); \
private: ClassName() = default; \
#define TS_SINGLETON_BODY(ClassName) \
TS_NOT_COPYABLE_AND_MOVEABLE(ClassName); \
private: ClassName() = default; \
friend Singleton<ClassName>;

#define STR(x) XSTR(x)
#define XSTR(x) #x
#define TS_STR(x) XSTR(x)
#define TS_XSTR(x) #x

template<>
struct std::hash<std::string_view> final
Expand All @@ -44,8 +44,8 @@ enum
{
TS_SUCCESS,
TS_FAILURE,
STL_FAILURE,
UNKNOWN_FAILURE
TS_STL_FAILURE,
TS_UNKNOWN_FAILURE
};

class Exception : public std::exception
Expand All @@ -63,7 +63,7 @@ class Exception : public std::exception
template<typename DerivedClass>
class Singleton
{
NOT_COPYABLE_AND_MOVEABLE(Singleton);
TS_NOT_COPYABLE_AND_MOVEABLE(Singleton);

public:
static auto& getInstance()
Expand Down
5 changes: 3 additions & 2 deletions engine/src/core/asset_store.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include "tsengine/asset_store.h"

#include "globals.hpp"
#include "tsengine/logger.h"

#include "tsengine/ecs/ecs.hpp"
#include "tsengine/ecs/ecs.h"
#include "tsengine/ecs/components/mesh_component.hpp"
#include "tsengine/ecs/components/transform_component.hpp"

Expand All @@ -19,7 +20,7 @@ namespace
// TODO: avoid the same files
void AssetStore::Models::load()
{
for (const auto entity : gRegistry.getSystem<AssetStore>().getSystemEntities())
for (const auto entity : gReg.getSystem<AssetStore>().getSystemEntities())
{
auto& meshComponent = entity.getComponent<MeshComponent>();
const auto& fileName = meshComponent.assetName;
Expand Down
Loading

0 comments on commit 7ac70ee

Please sign in to comment.