Skip to content

Commit d9e0159

Browse files
committed
Add Rgb; inspectors for Rgb, nvec3, vec3 colour
- Split `DirLight` into scene data vs SSBO data. - Prepare for inspecting dir lights in editor.
1 parent ad87774 commit d9e0159

File tree

9 files changed

+98
-7
lines changed

9 files changed

+98
-7
lines changed

lib/engine/include/facade/engine/editor/inspector.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <facade/engine/editor/common.hpp>
33
#include <facade/scene/material.hpp>
44
#include <facade/scene/node.hpp>
5+
#include <facade/util/nvec3.hpp>
6+
#include <facade/util/rgb.hpp>
57
#include <limits>
68

79
namespace facade {
@@ -28,7 +30,10 @@ class Inspector {
2830
bool inspect(char const* label, glm::vec2& out_vec2, float speed = 1.0f, float lo = min_v, float hi = max_v) const;
2931
bool inspect(char const* label, glm::vec3& out_vec3, float speed = 1.0f, float lo = min_v, float hi = max_v) const;
3032
bool inspect(char const* label, glm::vec4& out_vec4, float speed = 1.0f, float lo = min_v, float hi = max_v) const;
33+
bool inspect(char const* label, nvec3& out_vec3, float speed = 0.01f) const;
3134
bool inspect(char const* label, glm::quat& out_quat) const;
35+
bool inspect_rgb(char const* label, glm::vec3& out_rgb) const;
36+
bool inspect(char const* label, Rgb& out_rgb) const;
3237
bool inspect(Transform& out_transform, Bool& out_unified_scaling) const;
3338
bool inspect(std::span<Transform> out_instances, Bool unfied_scaling) const;
3439

lib/engine/src/editor/inspector.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ bool Inspector::inspect(char const* label, glm::vec4& out_vec4, float speed, flo
5050
return false;
5151
}
5252

53+
bool Inspector::inspect(char const* label, nvec3& out_vec3, float speed) const {
54+
auto vec3 = out_vec3.value();
55+
if (inspect(label, vec3, speed, -1.0f, 1.0f)) {
56+
out_vec3 = vec3;
57+
return true;
58+
}
59+
return false;
60+
}
61+
5362
bool Inspector::inspect(char const* label, glm::quat& out_quat) const {
5463
auto euler = to_degree(glm::eulerAngles(out_quat));
5564
auto const org = euler;
@@ -62,6 +71,25 @@ bool Inspector::inspect(char const* label, glm::quat& out_quat) const {
6271
return false;
6372
}
6473

74+
bool Inspector::inspect_rgb(char const* label, glm::vec3& out_rgb) const {
75+
float arr[3] = {out_rgb.x, out_rgb.y, out_rgb.z};
76+
if (ImGui::ColorEdit3(label, arr)) {
77+
out_rgb = {arr[0], arr[1], arr[2]};
78+
return true;
79+
}
80+
return false;
81+
}
82+
83+
bool Inspector::inspect(char const* label, Rgb& out_rgb) const {
84+
auto ret = Modified{};
85+
if (auto tn = TreeNode{label}) {
86+
auto vec3 = out_rgb.to_vec3();
87+
if (inspect_rgb("RGB", vec3)) { ret.value = true; }
88+
ret(ImGui::DragFloat("Intensity", &out_rgb.intensity, 0.05f, 0.1f, 1000.0f));
89+
}
90+
return ret.value;
91+
}
92+
6593
bool Inspector::inspect(Transform& out_transform, Bool& out_unified_scaling) const {
6694
if (ImGui::CollapsingHeader("Transform", ImGuiTreeNodeFlags_DefaultOpen)) { return do_inspect(out_transform, out_unified_scaling, {true}); }
6795
return false;
@@ -111,6 +139,7 @@ bool SceneInspector::inspect([[maybe_unused]] TreeNode const& node, LitMaterial&
111139
auto ret = Modified{};
112140
ret(ImGui::SliderFloat("Metallic", &out_material.metallic, 0.0f, 1.0f));
113141
ret(ImGui::SliderFloat("Roughness", &out_material.roughness, 0.0f, 1.0f));
142+
ret(inspect_rgb("Albedo", out_material.albedo));
114143
if (out_material.base_colour || out_material.roughness_metallic) {
115144
if (auto tn = TreeNode{"Textures"}) {
116145
if (out_material.base_colour) {

lib/engine/src/scene_renderer.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ struct Img1x1 {
2424
};
2525
}
2626
};
27+
28+
struct DirLightSSBO {
29+
alignas(16) glm::vec3 direction{front_v};
30+
alignas(16) glm::vec3 ambient{0.04f};
31+
alignas(16) glm::vec3 diffuse{1.0f};
32+
33+
static DirLightSSBO make(DirLight const& light) {
34+
return {
35+
.direction = light.direction.value(),
36+
.diffuse = light.rgb.to_vec4(),
37+
};
38+
}
39+
};
2740
} // namespace
2841

2942
SceneRenderer::SceneRenderer(Gfx const& gfx)
@@ -51,7 +64,9 @@ void SceneRenderer::write_view(glm::vec2 const extent) {
5164
.pos_v = {cam_node.transform.position(), 1.0f},
5265
};
5366
m_view_proj.write(&vp, sizeof(vp));
54-
m_dir_lights.write(m_scene->dir_lights.data(), m_scene->dir_lights.size() * sizeof(DirLight));
67+
auto dir_lights = FlexArray<DirLightSSBO, 4>{};
68+
for (auto const& light : m_scene->dir_lights.span()) { dir_lights.insert(DirLightSSBO::make(light)); }
69+
m_dir_lights.write(dir_lights.span().data(), dir_lights.span().size_bytes());
5570
}
5671

5772
void SceneRenderer::update_view(Pipeline& out_pipeline) const {
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
#pragma once
22
#include <facade/util/nvec3.hpp>
3+
#include <facade/util/rgb.hpp>
34

45
namespace facade {
56
///
6-
/// \brief GPU data for directional lights.
7+
/// \brief Directional light.
78
///
89
struct DirLight {
9-
alignas(16) glm::vec3 direction{front_v};
10-
alignas(16) glm::vec3 ambient{0.04f};
11-
alignas(16) glm::vec3 diffuse{1.0f};
10+
///
11+
/// \brief Direction.
12+
///
13+
nvec3 direction{front_v};
14+
///
15+
/// \brief Colour and intensity.
16+
///
17+
Rgb rgb{};
1218
};
1319
} // namespace facade

lib/scene/include/facade/scene/scene.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <facade/scene/node.hpp>
99
#include <facade/scene/node_data.hpp>
1010
#include <facade/util/enum_array.hpp>
11+
#include <facade/util/flex_array.hpp>
1112
#include <facade/util/image.hpp>
1213
#include <facade/util/ptr.hpp>
1314
#include <facade/util/transform.hpp>
@@ -245,7 +246,7 @@ class Scene {
245246
///
246247
/// \brief All the directional lights in the scene.
247248
///
248-
std::vector<DirLight> dir_lights{};
249+
FlexArray<DirLight, 4> dir_lights{};
249250

250251
private:
251252
struct TreeBuilder;

lib/util/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ target_sources(${PROJECT_NAME} PRIVATE
7979
include/${target_prefix}/util/nvec3.hpp
8080
include/${target_prefix}/util/pinned.hpp
8181
include/${target_prefix}/util/ptr.hpp
82+
include/${target_prefix}/util/rgb.hpp
8283
include/${target_prefix}/util/time.hpp
8384
include/${target_prefix}/util/transform.hpp
8485
include/${target_prefix}/util/type_id.hpp
@@ -90,5 +91,6 @@ target_sources(${PROJECT_NAME} PRIVATE
9091
src/data_provider.cpp
9192
src/image.cpp
9293
src/logger.cpp
94+
src/rgb.cpp
9395
src/time.cpp
9496
)

lib/util/include/facade/util/rgb.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#pragma once
2+
#include <glm/vec3.hpp>
3+
#include <glm/vec4.hpp>
4+
5+
namespace facade {
6+
struct Rgb {
7+
glm::tvec3<std::uint8_t> channels{0xff, 0xff, 0xff};
8+
float intensity{1.0f};
9+
10+
static constexpr float to_f32(std::uint8_t channel) { return static_cast<float>(channel) / static_cast<float>(0xff); }
11+
static constexpr float to_u8(float normalized) { return static_cast<std::uint8_t>(normalized * static_cast<float>(0xff)); }
12+
static glm::vec4 to_srgb(glm::vec4 const& linear);
13+
static glm::vec4 to_linear(glm::vec4 const& srgb);
14+
15+
static constexpr Rgb make(glm::vec3 const& serialized, float intensity = 1.0f) {
16+
return {
17+
.channels = {to_u8(serialized.x), to_u8(serialized.y), to_u8(serialized.z)},
18+
.intensity = intensity,
19+
};
20+
}
21+
22+
constexpr glm::vec3 to_vec3() const { return intensity * glm::vec3{to_f32(channels.x), to_f32(channels.y), to_f32(channels.z)}; }
23+
constexpr glm::vec4 to_vec4(float alpha = 1.0f) const { return {to_vec3(), alpha}; }
24+
};
25+
} // namespace facade

lib/util/src/rgb.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <facade/util/rgb.hpp>
2+
#include <glm/gtc/color_space.hpp>
3+
#include <glm/mat4x4.hpp>
4+
5+
namespace facade {
6+
glm::vec4 Rgb::to_srgb(glm::vec4 const& linear) { return glm::convertLinearToSRGB(linear); }
7+
glm::vec4 Rgb::to_linear(glm::vec4 const& srgb) { return glm::convertSRGBToLinear(srgb); }
8+
} // namespace facade

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ void run() {
259259
engine->add_shader(shaders::unlit());
260260

261261
auto& scene = engine->scene();
262-
scene.dir_lights.push_back(DirLight{.direction = glm::normalize(glm::vec3{-1.0f, -1.0f, -1.0f}), .diffuse = glm::vec3{5.0f}});
262+
scene.dir_lights.insert(DirLight{.direction = glm::normalize(glm::vec3{-1.0f, -1.0f, -1.0f}), .rgb = {.intensity = 5.0f}});
263263
scene.load_gltf(dj::Json::parse(test_json_v), DummyDataProvider{});
264264
post_scene_load(engine->scene());
265265
engine->show(true);

0 commit comments

Comments
 (0)