-
Notifications
You must be signed in to change notification settings - Fork 0
/
SceneNode.cpp
136 lines (110 loc) · 3.46 KB
/
SceneNode.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
#include "SceneNode.hpp"
#include "cs488-framework/MathUtils.hpp"
#include <iostream>
#include <sstream>
using namespace std;
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include <glm/gtx/transform.hpp>
using namespace glm;
// Static class variable
unsigned int SceneNode::nodeInstanceCount = 0;
//---------------------------------------------------------------------------------------
SceneNode::SceneNode(const std::string& name)
: m_name(name),
m_nodeType(NodeType::SceneNode),
trans(mat4()),
invtrans(mat4()),
m_nodeId(nodeInstanceCount++)
{
}
//---------------------------------------------------------------------------------------
// Deep copy
SceneNode::SceneNode(const SceneNode & other)
: m_nodeType(other.m_nodeType),
m_name(other.m_name),
trans(other.trans),
invtrans(other.invtrans)
{
for(SceneNode * child : other.children) {
this->children.push_front(new SceneNode(*child));
}
}
//---------------------------------------------------------------------------------------
SceneNode::~SceneNode() {
for(SceneNode * child : children) {
delete child;
}
}
//---------------------------------------------------------------------------------------
void SceneNode::set_transform(const glm::mat4& m) {
trans = m;
invtrans = glm::inverse(m);
}
//---------------------------------------------------------------------------------------
const glm::mat4& SceneNode::get_transform() const {
return trans;
}
//---------------------------------------------------------------------------------------
const glm::mat4& SceneNode::get_inverse() const {
return invtrans;
}
//---------------------------------------------------------------------------------------
void SceneNode::add_child(SceneNode* child) {
children.push_back(child);
}
//---------------------------------------------------------------------------------------
void SceneNode::remove_child(SceneNode* child) {
children.remove(child);
}
//---------------------------------------------------------------------------------------
void SceneNode::rotate(char axis, float angle) {
vec3 rot_axis;
switch (axis) {
case 'x':
rot_axis = vec3(1,0,0);
break;
case 'y':
rot_axis = vec3(0,1,0);
break;
case 'z':
rot_axis = vec3(0,0,1);
break;
default:
break;
}
mat4 rot_matrix = glm::rotate(degreesToRadians(angle), rot_axis);
set_transform( rot_matrix * trans );
}
//---------------------------------------------------------------------------------------
void SceneNode::scale(const glm::vec3 & amount) {
set_transform( glm::scale(amount) * trans );
}
//---------------------------------------------------------------------------------------
void SceneNode::translate(const glm::vec3& amount) {
set_transform( glm::translate(amount) * trans );
}
//---------------------------------------------------------------------------------------
int SceneNode::totalSceneNodes() const {
return nodeInstanceCount;
}
//---------------------------------------------------------------------------------------
std::ostream & operator << (std::ostream & os, const SceneNode & node) {
//os << "SceneNode:[NodeType: ___, name: ____, id: ____, isSelected: ____, transform: ____"
switch (node.m_nodeType) {
case NodeType::SceneNode:
os << "SceneNode";
break;
case NodeType::GeometryNode:
os << "GeometryNode";
break;
case NodeType::JointNode:
os << "JointNode";
break;
}
os << ":[";
os << "name:" << node.m_name << ", ";
os << "id:" << node.m_nodeId;
os << "]\n";
return os;
}