-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathscene-graph.cpp
More file actions
158 lines (132 loc) · 4.69 KB
/
Copy pathscene-graph.cpp
File metadata and controls
158 lines (132 loc) · 4.69 KB
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
#include <iostream>
#include <vector>
#include <memory>
#include <fstream>
#include "sre/Texture.hpp"
#include "sre/Renderer.hpp"
#include "sre/Material.hpp"
#include "sre/SDLRenderer.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/euler_angles.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/rotate_vector.hpp>
using namespace sre;
class Node;
std::shared_ptr<Node> cameraNode;
class Node : public std::enable_shared_from_this<Node> {
public:
Node() {}
Node(Node *parent) : parent(parent) {}
glm::vec3 position = glm::vec3(0,0,0);
glm::vec3 rotationEuler = glm::vec3(0,0,0);
glm::vec3 scale = glm::vec3(1,1,1);
glm::mat4 localToWorld(){
// compute trs
auto rotationEulerRadian = glm::radians(rotationEuler);
auto res = glm::translate(position)
* glm::eulerAngleXYZ(rotationEulerRadian.x,rotationEulerRadian.y,rotationEulerRadian.z)
* glm::scale(scale);
if (parent){
res = parent->localToWorld() * res;
}
return res;
}
std::vector<std::shared_ptr<Node>> children;
Node* parent = nullptr;
void gui(int level = 0){
ImGui::PushID(this);
ImGui::Indent(level*20);
if (ImGui::CollapsingHeader(this == cameraNode.get()?"Camera": "Node")){
ImGui::DragFloat3("Local Position",&position.x);
ImGui::DragFloat3("Local Rotation",&rotationEuler.x);
ImGui::DragFloat3("Local Scale",&scale.x);
auto globalPos = localToWorld() * glm::vec4(0,0,0,1); // transform 0,0,0 (pivot point) from local coordinate frame to global coordinate frame
bool changed = ImGui::DragFloat3("Global Position",&globalPos.x);
if (changed){
if (parent){
position = glm::inverse(parent->localToWorld()) * globalPos;
}
else {
position = globalPos;
}
}
if (ImGui::Button("Add child")){
children.push_back(std::make_shared<Node>(this));
}
if (ImGui::Button("Make camera")){
cameraNode = this->shared_from_this();
}
for (auto & n : children){
n->gui(level+1);
}
}
ImGui::Unindent(level*20);
ImGui::PopID();
}
};
class SceneGraphExample {
public:
SceneGraphExample(){
r.init();
camera.setPerspectiveProjection(60,0.1,100);
material = Shader::getStandardBlinnPhong()->createMaterial();
material->setColor({1.0f,1.0f,1.0f,1.0f});
material->setSpecularity({0.5,0.5,0.5,20.0f});
mesh = Mesh::create().withCube().build();
worldLights.setAmbientLight({0.5,0.5,0.5});
worldLights.addLight(Light::create().withPointLight({0, 3,0}).withColor({1,0,0}).withRange(20).build());
worldLights.addLight(Light::create().withPointLight({3, 0,0}).withColor({0,1,0}).withRange(20).build());
worldLights.addLight(Light::create().withPointLight({0,-3,0}).withColor({0,0,1}).withRange(20).build());
worldLights.addLight(Light::create().withPointLight({-3,0,0}).withColor({1,1,1}).withRange(20).build());
root.children.push_back(std::make_shared<Node>(nullptr));
root.children.push_back(std::make_shared<Node>(nullptr));
cameraNode = root.children[0];
cameraNode->position = {0,0,30};
r.frameRender = [&](){
auto localToWorld = cameraNode->localToWorld();
camera.setViewTransform(glm::inverse(localToWorld));
render();
};
r.startEventLoop();
}
void render(std::shared_ptr<Node>& node, RenderPass& rp){
rp.draw(mesh, node->localToWorld(), material);
// draw children
for (auto & n : node->children){
render(n, rp);
}
}
void render(){
auto renderPass = RenderPass::create()
.withCamera(camera)
.withWorldLights(&worldLights)
.withClearColor(true, {1, 0, 0, 1})
.build();
for (auto & n : root.children){
render(n, renderPass);
}
// draw gui
ImGui::Begin("SceneGraph");
if (ImGui::Button("Add object")){
root.children.push_back(std::make_shared<Node>(nullptr));
}
for (auto & n : root.children){
n->gui();
}
ImGui::End();
i++;
}
private:
SDLRenderer r;
Camera camera;
WorldLights worldLights;
std::shared_ptr<Mesh> mesh;
std::shared_ptr<Material> material;
Node root;
int i=0;
};
int main() {
new SceneGraphExample();
return 0;
}