Skip to content

Commit bc417d7

Browse files
authored
Merge pull request #86 from pixelsandpointers/various-fixes
Various fixes, cleanup, performance improvements, QoL
2 parents e6d0a86 + 92d2be0 commit bc417d7

28 files changed

+408
-267
lines changed

include/core/AsyncTaskQueue.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ class AsyncTaskQueue {
2222
void push_task(std::function<void()>);
2323
void close();
2424
bool is_open();
25+
std::size_t num_queued_tasks();
26+
std::size_t num_total_queued_tasks();
2527

2628
private:
2729
std::queue<std::function<void()>> m_queue;
2830
std::mutex m_queue_mutex;
2931
std::condition_variable m_convar;
3032
bool m_is_open{true};
33+
std::size_t m_total_queued_tasks{0};
3134
};

include/core/Config.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct Config {
1515
CameraController::Type camera_controller_type{CameraController::Type::UNITY};
1616
float movement_speed{1000.0f}; // Movement speed in distance / second
1717
float rotation_speed{0.2f}; // Mouse sensitivity for looking around in radians / second
18-
float zoom_speed{1.0f};
18+
float zoom_speed{5.0f};
1919

2020
// FIXME: The CameraController breaks if position and target are equal
2121
glm::vec3 camera_position{0.0f};
@@ -24,5 +24,5 @@ struct Config {
2424
bool gizmo_use_snap{true};
2525
float gizmo_snap_translation{100.0f};
2626
float gizmo_snap_rotation{10.0f};
27-
float gizmo_snap_scale{1.0f};
27+
float gizmo_snap_scale{0.1f};
2828
};

include/core/Project.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Project {
5151
Texture const* white_texture() const;
5252

5353
private:
54+
friend struct Performance;
5455
static std::unique_ptr<Project> current;
5556

5657
ColorTexture m_fallback_texture{ColorTexture::single_color(glm::vec4{1.0f})};

include/core/Scene.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ struct InstancedNode {
3333
std::vector<std::unique_ptr<InstancedNode>> children;
3434
std::string name;
3535

36+
static unsigned int counter;
37+
unsigned int const id{counter++};
38+
3639
void traverse(std::function<void(glm::mat4, Node const&)>) const;
3740
void compute_transforms(glm::mat4 = glm::mat4{1.0f});
3841

include/core/Serializer.hpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,19 @@ class Serializer {
2222
Project& m_project;
2323

2424
[[nodiscard]] nlohmann::json serialize(InstancedNode const&) const;
25-
[[nodiscard]] nlohmann::json serialize(glm::vec3 const&) const;
26-
[[nodiscard]] nlohmann::json serialize(glm::vec4 const&) const;
27-
[[nodiscard]] nlohmann::json serialize(glm::quat const&) const;
2825
[[nodiscard]] nlohmann::json serialize(Config const&) const;
2926
[[nodiscard]] nlohmann::json serialize(Uniforms const&) const;
3027

31-
template <class T>
32-
T deserialize(nlohmann::json&) const;
33-
template <>
34-
std::unique_ptr<InstancedNode> deserialize(nlohmann::json& source) const;
35-
template <>
36-
glm::vec3 deserialize(nlohmann::json& source) const;
37-
template <>
38-
glm::vec4 deserialize(nlohmann::json& source) const;
39-
template <>
40-
glm::quat deserialize(nlohmann::json& source) const;
41-
template <>
42-
Config deserialize(nlohmann::json& source) const;
43-
template <>
44-
Uniforms deserialize(nlohmann::json& source) const;
28+
std::unique_ptr<InstancedNode> deserialize_unique_ptr_instancednode(nlohmann::json& source) const;
29+
Config deserialize_config(nlohmann::json& source) const;
30+
Uniforms deserialize_uniforms(nlohmann::json& source) const;
4531
};
32+
33+
namespace glm {
34+
void to_json(nlohmann::json& j, vec3 const& v);
35+
void from_json(nlohmann::json const& j, vec3& v);
36+
void to_json(nlohmann::json& j, vec4 const& v);
37+
void from_json(nlohmann::json const& j, vec4& v);
38+
void to_json(nlohmann::json& j, quat const& q);
39+
void from_json(nlohmann::json const& j, quat& v);
40+
}

include/renderer/Mesh.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class Mesh {
3737
void draw() const;
3838
void draw(ViewingMode) const;
3939
[[nodiscard]] bool is_fully_loaded() const;
40+
void setup_mesh();
4041

4142
private:
42-
unsigned int m_vao, m_vbo, m_ebo;
43-
void setup_mesh();
43+
unsigned int m_vao{0}, m_vbo{0}, m_ebo{0};
4444
};

include/renderer/Shader.hpp

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <functional>
34
#include <glad/glad.h>
45
#include <glm/glm.hpp>
56
#include <glm/gtc/type_ptr.hpp>
@@ -46,6 +47,29 @@ struct Uniforms {
4647
} light;
4748
};
4849

50+
struct UniformLocations {
51+
// Vertex
52+
int model{-1};
53+
int view{-1};
54+
int projection{-1};
55+
56+
// Fragment
57+
int texture_diffuse{-1};
58+
int texture_opacity{-1};
59+
int light_direction{-1};
60+
int light_color{-1};
61+
int light_power{-1};
62+
int camera_pos{-1};
63+
int ambient_strength{-1};
64+
int specularity_factor{-1};
65+
int shininess{-1};
66+
int gamma{-1};
67+
68+
int tex{-1};
69+
int color{-1};
70+
int id{-1};
71+
};
72+
4973
/**
5074
* @brief Defines the types of shading techniques available in rendering.
5175
*
@@ -85,6 +109,7 @@ enum class ViewingMode {
85109
struct ShaderSource {
86110
char const* vertex_shader;
87111
char const* fragment_shader;
112+
std::function<void(UniformLocations&, std::function<void(int&, char const*)>)> uniform_caching_function;
88113
};
89114

90115
/**
@@ -121,24 +146,38 @@ class Shader {
121146
static void init();
122147

123148
Shader() = default;
124-
Shader(char const* vertex_path, char const* fragment_path);
125149
Shader(ShaderSource);
126150
Shader(Shader const&) = delete;
127151
Shader(Shader&&);
128152
Shader& operator=(Shader&&);
129153

130154
unsigned int m_id;
155+
UniformLocations uniform_locations;
131156
void use() const;
132-
void set_bool(char const* name, bool value) const;
133-
void set_int(char const* name, int value) const;
134-
void set_uint(char const* name, unsigned int value) const;
135-
void set_float(char const* name, float value) const;
136-
void set_mat2(char const* name, glm::mat2 const& matrix) const;
137-
void set_mat3(char const* name, glm::mat3 const& matrix) const;
138-
void set_mat4(char const* name, glm::mat4 const& matrix) const;
139-
void set_vec2(char const* name, glm::vec2 const& vector) const;
140-
void set_vec3(char const* name, glm::vec3 const& vector) const;
141-
void set_vec4(char const* name, glm::vec4 const& vector) const;
157+
158+
template <typename T>
159+
void set_uniform(int location, T const& value) const
160+
{
161+
if (location < 0) {
162+
return;
163+
}
164+
165+
if constexpr (std::is_same_v<T, bool>) {
166+
glUniform1i(location, static_cast<int>(value));
167+
} else if constexpr (std::is_same_v<T, int>) {
168+
glUniform1i(location, value);
169+
} else if constexpr (std::is_same_v<T, unsigned int>) {
170+
glUniform1ui(location, value);
171+
} else if constexpr (std::is_same_v<T, float>) {
172+
glUniform1f(location, value);
173+
} else if constexpr (std::is_same_v<T, glm::mat4>) {
174+
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(value));
175+
} else if constexpr (std::is_same_v<T, glm::vec3>) {
176+
glUniform3fv(location, 1, glm::value_ptr(value));
177+
} else if constexpr (std::is_same_v<T, glm::vec4>) {
178+
glUniform4fv(location, 1, glm::value_ptr(value));
179+
}
180+
}
142181

143182
private:
144183
void check_compile_errors(unsigned int shader, ShadingStage stage) const;

include/renderer/Texture.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct Image {
1919
struct ColorTexture;
2020

2121
struct Texture {
22-
unsigned int m_id;
22+
unsigned int id;
2323
int width;
2424
int height;
2525
int channels;

include/ui/ObjectSelectionTree.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ struct ObjectSelectionTree {
1010

1111
private:
1212
std::optional<ImRect> m_prev_rect;
13-
int m_imgui_treenode_id;
1413
};

include/ui/Performance.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <array>
4+
#include <cstddef>
5+
6+
struct Performance {
7+
void render(double delta_time);
8+
9+
private:
10+
double const m_update_interval = 5.0;
11+
double m_current_time{0};
12+
double m_last_updated{-m_update_interval};
13+
std::size_t m_last_total_background_tasks{0};
14+
std::size_t m_total_background_tasks{0};
15+
std::array<float, 100> m_frametimes;
16+
};

0 commit comments

Comments
 (0)