Skip to content
This repository was archived by the owner on Dec 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ add_dz_test(DZ_ImGuiTest tests/ImGui.cpp)
add_dz_test(DZ_ECSTest tests/ECS.cpp)
file(COPY images/Suzuho-Ueda.bmp DESTINATION ${CMAKE_BINARY_DIR}/images)
file(COPY images/hi.bmp DESTINATION ${CMAKE_BINARY_DIR}/images)
file(COPY models/SaiyanOne.glb DESTINATION ${CMAKE_BINARY_DIR}/models)
file(COPY models/SaiyanOne.glb DESTINATION ${CMAKE_BINARY_DIR}/models)
file(COPY models/GoldenSportsCar.glb DESTINATION ${CMAKE_BINARY_DIR}/models)
219 changes: 184 additions & 35 deletions include/dz/ECS.hpp

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion include/dz/ECS/Camera.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Provider.hpp"
#include "../Reflectable.hpp"
#include "../Framebuffer.hpp"
#include "../Shader.hpp"
#include "../math.hpp"
Expand Down Expand Up @@ -28,6 +29,7 @@ namespace dz::ecs {
int parent_cid = 0;
int transform_dirty = 1;
int is_active = 1;
inline static constexpr bool RequiresBuffer = true;
inline static constexpr bool IsCameraProvider = true;
inline static constexpr size_t PID = 5;
inline static float Priority = 0.5f;
Expand Down Expand Up @@ -126,7 +128,7 @@ void GetCameraModel(int camera_index, out mat4 out_model, out int parent_index,
};


struct CameraMetaReflectable : Reflectable {
struct CameraMetaReflectable : ::Reflectable {

private:
std::function<Camera*()> get_camera_function;
Expand Down
1 change: 1 addition & 0 deletions include/dz/ECS/Entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace dz::ecs {

inline static constexpr size_t PID = 2;
inline static float Priority = 0.5f;
inline static constexpr bool RequiresBuffer = true;
inline static constexpr bool IsEntityProvider = true;
inline static std::string ProviderName = "Entity";
inline static std::string StructName = "Entity";
Expand Down
217 changes: 117 additions & 100 deletions include/dz/ECS/Light.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "Provider.hpp"
#include "../Reflectable.hpp"
#include "../math.hpp"
namespace dz::ecs {
struct Light : Provider<Light> {
Expand All @@ -20,6 +21,7 @@ namespace dz::ecs {
float outerCone; // for spot
inline static constexpr size_t PID = 7;
inline static float Priority = 3.5f;
inline static constexpr bool RequiresBuffer = true;
inline static std::string ProviderName = "Light";
inline static std::string StructName = "Light";
inline static std::string GLSLStruct = R"(
Expand All @@ -36,116 +38,131 @@ struct Light {
float outerCone;
};
)";
inline static std::unordered_map<ShaderModuleType, std::string> GLSLMethods = {
{ShaderModuleType::Fragment, R"(
vec3 CalculateLight(in vec3 normal, vec3 frag_pos, vec3 view_dir, in Light light) {
vec3 light_dir;
float attenuation = 1.0;

if (light.type == 0)
{
light_dir = normalize(-light.direction);
}
else
{
light_dir = normalize(light.position - frag_pos);
float dist = length(light.position - frag_pos);
attenuation = clamp(1.0 - (dist / light.range), 0.0, 1.0);
}

float diff = max(dot(normal, light_dir), 0.0);
vec3 reflect_dir = reflect(-light_dir, normal);
float spec = pow(max(dot(view_dir, reflect_dir), 0.0), 4.0);//shininess);

float spotlight_factor = 1.0;
if (light.type == 2)
{
float theta = dot(light_dir, normalize(-light.direction));
float epsilon = light.innerCone - light.outerCone;
spotlight_factor = clamp((theta - light.outerCone) / epsilon, 0.0, 1.0);
}

vec3 diffuse = diff * light.color * light.intensity;
vec3 specular = spec * light.color * light.intensity;

return (diffuse + specular) * attenuation * spotlight_factor;
}
)" }
};
inline static std::vector<std::tuple<float, std::string, ShaderModuleType>> GLSLMain = {
{3.5f, R"(
vec3 frag_pos = vec3(inPosition);
vec3 frag_pos = inPosition;
vec3 view_dir = normalize(camera.position - frag_pos);
int lights_size = Lights.data.length();
vec3 light_color = vec3(0.0);
for (int light_index = 0; light_index < lights_size; light_index++) {
light_color += CalculateLight(inNormal, frag_pos, view_dir, Lights.data[light_index]);
}
current_color = vec4(light_color, 1.0) * current_color;
)", ShaderModuleType::Fragment}
};
};

struct LightMetaReflectable : Reflectable {

private:
std::function<Light*()> get_light_function;
std::function<void()> reset_reflectables_function;
int uid;
std::string name;
inline static std::unordered_map<std::string, std::pair<int, int>> prop_name_indexes = {
{ "type", {0, 0}},
{ "intensity", {1, 0}},
{ "range", {2, 0}},
{ "innerCone", {3, 0}},
{ "position", {4, 0}},
{ "direction", {5, 0}},
{ "color", {6, 0}},
{ "outerCone", {7, 0}}
};
inline static std::unordered_map<int, std::string> prop_index_names = {
{ 0, "type" },
{ 1, "intensity"},
{ 2, "range"},
{ 3, "innerCone"},
{ 4, "position"},
{ 5, "direction"},
{ 6, "color"},
{ 7, "outerCone"}
};
inline static std::vector<std::string> prop_names = {
"type",
"intensity",
"range",
"innerCone",
"position",
"direction",
"color",
"outerCone"
struct LightMetaReflectable : Reflectable {

private:
std::function<Light*()> get_light_function;
int uid;
std::string name;
inline static std::unordered_map<std::string, std::pair<int, int>> prop_name_indexes = {
{ "type", {0, 0}},
{ "intensity", {1, 0}},
{ "range", {2, 0}},
{ "innerCone", {3, 0}},
{ "position", {4, 0}},
{ "direction", {5, 0}},
{ "color", {6, 0}},
{ "outerCone", {7, 0}}
};
inline static std::unordered_map<int, std::string> prop_index_names = {
{ 0, "type" },
{ 1, "intensity"},
{ 2, "range"},
{ 3, "innerCone"},
{ 4, "position"},
{ 5, "direction"},
{ 6, "color"},
{ 7, "outerCone"}
};
inline static std::vector<std::string> prop_names = {
"type",
"intensity",
"range",
"innerCone",
"position",
"direction",
"color",
"outerCone"
};
inline static const std::vector<const std::type_info*> typeinfos = {
&typeid(Light::LightType),
&typeid(float),
&typeid(float),
&typeid(float),
&typeid(vec<float, 3>),
&typeid(vec<float, 3>),
&typeid(color_vec<float, 3>),
&typeid(float)
};

public:
LightMetaReflectable(
const std::function<Light*()>& get_light_function
);
int GetID() override;
std::string& GetName() override;
DEF_GET_PROPERTY_INDEX_BY_NAME(prop_name_indexes);
DEF_GET_PROPERTY_NAMES(prop_names);
void* GetVoidPropertyByIndex(int prop_index) override;
DEF_GET_VOID_PROPERTY_BY_NAME;
DEF_GET_PROPERTY_TYPEINFOS(typeinfos);
void NotifyChange(int prop_index) override;
};
inline static const std::vector<const std::type_info*> typeinfos = {
&typeid(Light::LightType),
&typeid(float),
&typeid(float),
&typeid(float),
&typeid(vec<float, 3>),
&typeid(vec<float, 3>),
&typeid(color_vec<float, 3>),
&typeid(float)

struct LightReflectableGroup : ReflectableGroup {
BufferGroup* buffer_group = nullptr;
std::string name;
std::vector<Reflectable*> reflectables;
LightReflectableGroup(BufferGroup* buffer_group):
buffer_group(buffer_group),
name("Light")
{}
LightReflectableGroup(BufferGroup* buffer_group, Serial& serial):
buffer_group(buffer_group)
{
restore(serial);
}
~LightReflectableGroup() {
ClearReflectables();
}
GroupType GetGroupType() override {
return ReflectableGroup::Light;
}
std::string& GetName() override {
return name;
}
const std::vector<Reflectable*>& GetReflectables() override {
return reflectables;
}
void UpdateReflectables() { }
void ClearReflectables() {
if (reflectables.empty()) {
return;
}
delete reflectables[0];
reflectables.clear();
}
void UpdateChildren() override {
if (reflectables.empty()) {
reflectables.push_back(new LightMetaReflectable([&]() {
auto buffer = buffer_group_get_buffer_data_ptr(buffer_group, "Lights");
return ((struct Light*)(buffer.get())) + index;
}));
}
}
bool backup(Serial& serial) const override {
if (!backup_internal(serial))
return false;
serial << name;
return true;
}
bool restore(Serial& serial) override {
if (!restore_internal(serial))
return false;
serial >> name;
return true;
}
};

public:
LightMetaReflectable(
const std::function<Light*()>& get_light_function,
const std::function<void()>& reset_reflectables_function
);
int GetID() override;
std::string& GetName() override;
DEF_GET_PROPERTY_INDEX_BY_NAME(prop_name_indexes);
DEF_GET_PROPERTY_NAMES(prop_names);
void* GetVoidPropertyByIndex(int prop_index) override;
DEF_GET_VOID_PROPERTY_BY_NAME;
DEF_GET_PROPERTY_TYPEINFOS(typeinfos);
void NotifyChange(int prop_index) override;
using ReflectableGroup = LightReflectableGroup;
};
}
Loading
Loading