-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.cpp
214 lines (177 loc) · 4.33 KB
/
node.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
#include <node.hpp>
#include <scene.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <string>
#include <iostream>
Node::Node(std::string name) :
tx(0), rot(0), sc(1), parent(NULL),
ctr(0),
geometry(NULL),
mat(NULL),
name(name),
tex(NULL)
{
updateLocalModel();
}
Node::Node(std::string name,Object *m) :
tx(0), rot(0), sc(1), parent(NULL),
ctr(0),
geometry(m),
mat(NULL),
name(name),
tex(NULL)
{
updateLocalModel();
}
Node::Node(const NodeConf& conf) :
tx(conf.tx), rot(conf.rot), sc(conf.scale), parent(NULL),
ctr(conf.ctr),
geometry(NULL),
mat(NULL),
name(conf.name),
tex(NULL)
{
updateLocalModel();
}
Node::Node(const NodeConf& conf, Object *m) :
tx(conf.tx), rot(conf.rot), sc(conf.scale), parent(NULL),
ctr(conf.ctr),
geometry(m),
mat(NULL),
name(conf.name),
tex(NULL)
{
updateLocalModel();
}
Node::~Node() {
if(parent)
parent->delChild(this);
for(unsigned int i = 0; i < children.size(); i++)
delete children[i];
}
void Node::center(const glm::vec3& v) {
ctr = v;
}
void Node::updateLocalModel() {
glm::mat4 ctrMat = glm::translate(-ctr);
glm::mat4 ctrMatInv = glm::translate(ctr);
glm::mat4 txMat = glm::translate(tx);
glm::quat rotX = glm::quat(rot.x*glm::vec3(1,0,0));
glm::quat rotY = glm::quat(rot.y*glm::vec3(0,1,0));
glm::quat rotZ = glm::quat(rot.z*glm::vec3(0,0,1));
glm::mat4 rotMat = glm::mat4_cast(rotZ*rotY*rotX);
glm::mat4 scMat = glm::scale(sc);
localModel = ctrMatInv*txMat*rotMat*scMat*ctrMat;
if (parent)
localModel = parent->localModel*localModel;
localModelInv = glm::inverse(localModel);
for(unsigned int i = 0; i < children.size(); i++)
children[i]->updateLocalModel();
}
glm::mat4 Node::getLocalModel() const {
return localModel;
}
void Node::draw(Shader *shader) {
if(shaders.count(shader) && geometry) {
// Set the 4x4 model transformation matrices
// Also upload the inverse transpose for normal transformation
shader->setUniform("u_Model", localModel);
shader->setUniform("u_Color", mat->diffCol);
glm::mat4 modelInvTr = glm::transpose(localModelInv);
shader->setUniform("u_ModelInvTr", modelInvTr);
// bind our texture
if(tex) {
shader->setUniform("u_hasTexture", true);
shader->bind(tex);
} else {
shader->setUniform("u_hasTexture", false);
}
// draw our geometry
shader->bind(geometry);
shader->draw(geometry);
}
for(unsigned int i = 0; i < children.size(); i++)
children[i]->draw(shader);
}
void Node::addShader(Shader *s) {
shaders.insert(s);
}
void Node::delShader(Shader *s) {
shaders.erase(s);
}
void Node::buildPreOrder(std::deque<std::string>& vec) {
if(geometry)
vec.push_back(name);
for(unsigned int i = 0; i < children.size(); i++)
children[i]->buildPreOrder(vec);
}
void Node::translate(glm::vec3 dv) {
tx += dv;
updateLocalModel();
}
void Node::rotate(glm::vec3 dv) {
rot += dv;
updateLocalModel();
}
void Node::scale(glm::vec3 dv) {
sc += dv;
updateLocalModel();
}
void Node::addChild(Node *n) {
children.push_back(n);
n->parent = this;
n->updateLocalModel();
}
void Node::delChild(Node *n) {
unsigned int i;
for(i = 0; i < children.size(); i++) {
if(children[i] == n)
break;
}
if(i < children.size())
children.erase(children.begin()+i);
}
void Node::bindTexture(Texture *t) {
if(tex)
unbindTexture();
tex = t;
}
void Node::bindMaterial(MatConf *m) {
mat = m;
}
void Node::unbindTexture() {
tex = NULL;
}
Intersection Node::raytrace(const Ray& ray) {
Intersection ix;
raytrace(ray, ix);
if(ix.t > 0)
ix.normal = glm::normalize(ix.normal);
if(glm::dot(ray.dir,ix.normal) > 0)
ix.normal *= -1;
return ix;
}
bool Node::raytrace(const Ray& ray, Intersection& best) {
Intersection ix;
if(geometry) {
geometry->intersect(localModel, localModelInv, ray, best);
best.mat = mat;
} else {
best.mat = NULL;
}
for(unsigned int i = 0; i < children.size(); i++) {
children[i]->raytrace(ray, ix);
if(ix.t > .001 && (best.t < 0 || ix.t < best.t))
best = ix;
}
return best.t > .001;
}
void Node::sample(Intersection& ix) const {
if(geometry)
ix.pos = glm::vec3(localModel*glm::vec4(geometry->sample(),1));
else
ix.pos = glm::vec3(localModel*glm::vec4(tx,1));
ix.mat = mat;
}