-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCObject.cpp
More file actions
63 lines (54 loc) · 1.25 KB
/
CObject.cpp
File metadata and controls
63 lines (54 loc) · 1.25 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
#include "CObject.h"
CObject::CObject(CObject* parent, const vec3& pos, const vec3& rot) :
Parent(0), Position(pos), Rotation(rot)
{
for (int i=0; i<10; i++)
Textures[i] = 0;
setParent(Parent);
updateTransformation();
}
void CObject::setParent(CObject* parent)
{
if (Parent)
Parent->Children.remove(this);
Parent = parent;
if (Parent)
Parent->Children.push_back(this);
}
CObject::~CObject()
{
setParent(0);
for (ObjectIter it = Children.begin(); it != Children.end(); it++)
delete *it;
}
mat4 CObject::getAbsoluteTransformation() const
{
if (Parent)
return Transformation * Parent->getAbsoluteTransformation();
return Transformation;
}
vec3 CObject::getAbsolutePosition() const
{
mat4 tr = getAbsoluteTransformation();
return vec3(tr.M[12], tr.M[13], tr.M[14]);
}
void CObject::render()
{
for (ObjectIter it = Children.begin(); it != Children.end(); it++)
(*it)->render();
}
void CObject::animate(float dt)
{
if (dt > 0)
for (ObjectIter it = Children.begin(); it != Children.end(); it++)
(*it)->animate(dt);
}
void CObject::transform()
{
glMultMatrixf(Transformation.M);
}
void CObject::updateTransformation()
{
Transformation.makeIdentity();
(Transformation.makeTranslate(Position) *= mat4().makeRotate(Rotation)).makeTransposed();
}