-
Notifications
You must be signed in to change notification settings - Fork 11
/
ex20_scene_graphs.cpp
290 lines (243 loc) · 10 KB
/
ex20_scene_graphs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <application.hpp>
#include <shader.hpp>
#include <utility>
#include <imgui-utils/utils.hpp>
#include <mesh/mesh.hpp>
#include <mesh/mesh-utils.hpp>
#include <camera/camera.hpp>
#include <camera/controllers/fly_camera_controller.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <json/json.hpp>
#include <fstream>
#include <string>
#include <unordered_map>
#include <optional>
namespace glm {
template<length_t L, typename T, qualifier Q>
void from_json(const nlohmann::json& j, vec<L, T, Q>& v){
for(length_t index = 0; index < L; ++index)
v[index] = j[index].get<T>();
}
}
/****************************** Explaining the JSON ****************************************/
/* SIMPLE.JSON
{
"name": "root",
"mesh": "cube",
"children": {
"child-1": {
"translation": [-2, 2, 0],
"scale": [0.5, 0.5, 0.5],
"mesh": "cube",
"children": {
"grand-child-1": {
"translation": [0, 2, -2],
"scale": [0.5, 0.5, 0.5],
"mesh": "cube"
},
"grand-child-2": {
"translation": [0, 2, 2],
"scale": [0.5, 0.5, 0.5],
"mesh": "cube"
}
}
},
"child-2": {
"translation": [2, 2, 0],
"scale": [0.5, 0.5, 0.5],
"mesh": "cube",
"children": {
"grand-child-1": {
"translation": [0, 2, -2],
"scale": [0.5, 0.5, 0.5],
"mesh": "cube"
},
"grand-child-2": {
"translation": [0, 2, 2],
"scale": [0.5, 0.5, 0.5],
"mesh": "cube"
}
}
}
}
}
*/
// The name of the root is "root" it has cube mesh.
// It has the root has 2 children (child-1) and (child-2).
// child-1 has transform data: translation, scale. It also has a mesh (cube mesh).
// child-1 has 2 children (grand-child-1) and (grand-child-2).
// grand-child-1 also has transform data: translation, scale. It also has a mesh (cube mesh).
// Notice that the transform data (translation,rotation,scale) propogates from parent to child.
/***************************************************************************************************/
// The struct holds the Transform data.
struct Transform {
// This vec4 is multiplied by the original color in the fragment shader.
glm::vec4 tint;
// Vectors to transform data.
glm::vec3 translation, rotation, scale;
std::optional<std::string> mesh;
std::unordered_map<std::string, std::shared_ptr<Transform>> children;
explicit Transform(
const glm::vec4& tint = {1,1,1,1},
const glm::vec3& translation = {0,0,0},
const glm::vec3& rotation = {0,0,0},
const glm::vec3& scale = {1,1,1},
std::optional<std::string> mesh = std::nullopt
): tint(tint), translation(translation), rotation(rotation), scale(scale), mesh(std::move(mesh)) {}
// Create a matrix from the translation, rotation and scale members
// The matrix transforms from this node's local space to the parent's local space
[[nodiscard]] glm::mat4 to_mat4() const {
return glm::translate(glm::mat4(1.0f), translation) *
glm::yawPitchRoll(rotation.y, rotation.x, rotation.z) *
glm::scale(glm::mat4(1.0f), scale);
}
};
class SceneGraphApplication : public our::Application {
our::ShaderProgram program;
// This unordered map keep track of all our meshes and saves them by name.
// meshes["cube"] => contains the mesh data of a cube.
std::unordered_map<std::string, std::unique_ptr<our::Mesh>> meshes;
// This unordered map keep track of all our scene graphs and saves them by name.
std::unordered_map<std::string, std::shared_ptr<Transform>> roots;
// This value stores the name of the current root of the scene graph that we will render.
std::string current_root_name;
// A camera and a camera controller to view the scene
our::Camera camera;
our::FlyCameraController controller;
our::WindowConfiguration getWindowConfiguration() override {
return { "Scene Graphs", {1280, 720}, false };
}
void onInitialize() override {
program.create();
program.attach("assets/shaders/ex11_transformation/transform.vert", GL_VERTEX_SHADER);
program.attach("assets/shaders/ex11_transformation/tint.frag", GL_FRAGMENT_SHADER);
program.link();
// Create meshes for cube, rod (cube shifted by 0.5 in z), and a sphere.
meshes["cube"] = std::make_unique<our::Mesh>();
our::mesh_utils::Cuboid(*(meshes["cube"]), true);
meshes["rod"] = std::make_unique<our::Mesh>();
our::mesh_utils::Cuboid(*(meshes["rod"]), true, {0, 0, 0.5});
meshes["sphere"] = std::make_unique<our::Mesh>();
our::mesh_utils::Sphere(*(meshes["sphere"]), {32, 16}, true);
// Set the camera data.
int width, height;
glfwGetFramebufferSize(window, &width, &height);
camera.setEyePosition({10, 10, 10});
camera.setTarget({0, 0, 0});
camera.setUp({0, 1, 0});
camera.setupPerspective(glm::pi<float>()/2, static_cast<float>(width)/height, 0.1f, 100.0f);
controller.initialize(this, &camera);
// Reading each file saving only the parent of the whole scene in "root".
roots["simple"] = loadSceneGraph("assets/data/ex20_scene_graphs/simple.json");
roots["solar-system"] = loadSceneGraph("assets/data/ex20_scene_graphs/solar-system.json");
roots["human"] = loadSceneGraph("assets/data/ex20_scene_graphs/human.json");
current_root_name = "simple";
// Set the render state
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
glClearColor(0, 0, 0, 1);
}
// Loading node happens
std::shared_ptr<Transform> loadNode(const nlohmann::json& json){
// First read from the JSON the values of tint,translation,rotation,scale from json.
// in Json:
/// "translation": [0, 2, -2]
// then translation = 0, 2, -2
auto node = std::make_shared<Transform>(
json.value<glm::vec4>("tint", {1,1,1,1}),
json.value<glm::vec3>("translation", {0, 0, 0}),
json.value<glm::vec3>("rotation", {0, 0, 0}),
json.value<glm::vec3>("scale", {1, 1, 1})
);
// If it contains mesh name set it.
if(json.contains("mesh")){
node->mesh = json["mesh"].get<std::string>();
}
// If it contains children nodes set them.
// Notice that this function is recursive, it sets all the children data to the set of children,
// also the data of each child.
if(json.contains("children")){
for(auto& [name, child]: json["children"].items()){
node->children[name] = loadNode(child);
}
}
return node;
}
// Read the data of the JSON file and send it to the "loadNode()" to be processed.
std::shared_ptr<Transform> loadSceneGraph(const std::string& scene_file){
std::ifstream file_in(scene_file);
nlohmann::json json;
file_in >> json;
file_in.close();
return loadNode(json);
}
// This function draws the models in a recursive manner.
// It starts with the parent node and draw each child,
// passing the transform matrix to the children to
// cascade the transformation effect.
void drawNode(const std::shared_ptr<Transform>& node, const glm::mat4& parent_transform_matrix){
glm::mat4 transform_matrix = parent_transform_matrix * node->to_mat4();
if(node->mesh.has_value()){
auto it = meshes.find(node->mesh.value());
if(it != meshes.end()) {
program.set("tint", node->tint);
program.set("transform", transform_matrix);
it->second->draw();
}
}
for(auto& [name, child]: node->children){
drawNode(child, transform_matrix);
}
}
void onDraw(double deltaTime) override {
controller.update(deltaTime);
glUseProgram(program);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// To draw, just call "drawNode" and give it the current root
// Note the we give it the camera VP matrix such that every matrix generated in "drawNode" transforms to the homogenous clip space.
drawNode(roots[current_root_name], camera.getVPMatrix());
}
void onDestroy() override {
program.destroy();
for(auto& [name, mesh] : meshes){
mesh->destroy();
}
meshes.clear();
}
void displayNodeGui(const std::shared_ptr<Transform>& node, const std::string& node_name){
if(ImGui::TreeNode(node_name.c_str())){
if(node->mesh.has_value()) {
our::PairIteratorCombo("Mesh", node->mesh.value(), meshes.begin(), meshes.end());
ImGui::ColorEdit4("Tint", glm::value_ptr(node->tint));
}
ImGui::DragFloat3("Translation", glm::value_ptr(node->translation), 0.1f);
ImGui::DragFloat3("Rotation", glm::value_ptr(node->rotation), 0.01f);
ImGui::DragFloat3("Scale", glm::value_ptr(node->scale), 0.1f);
for(auto& [name, child]: node->children){
displayNodeGui(child, name);
}
ImGui::TreePop();
}
}
void onImmediateGui(ImGuiIO &io) override {
ImGui::Begin("Scene Graph");
if(ImGui::BeginCombo("Scene", current_root_name.c_str())){
for(auto& [name, root] : roots){
bool selected = current_root_name == name;
if(ImGui::Selectable(name.c_str(), selected))
current_root_name = name;
if(selected)
ImGui::SetItemDefaultFocus();
}
ImGui::EndCombo();
}
displayNodeGui(roots[current_root_name], current_root_name);
ImGui::End();
}
};
int main(int argc, char** argv) {
return SceneGraphApplication().run();
}