-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject3ddynamic.cpp
141 lines (118 loc) · 3.36 KB
/
object3ddynamic.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
#include "object3ddynamic.h"
Object3DDynamic::Object3DDynamic(){
m_rootBone = 0;
m_nbBone = 0;
m_texture = 0;
}
Object3DDynamic::Object3DDynamic(vector<int> indices,vector<float> vertices,vector<float> normals,vector<float> textCoord, int nbBone, Bone* rootBone):m_rootBone(rootBone)
{
initializeOpenGLFunctions();
m_animator = Animator(m_rootBone);
m_parent = 0;
m_texture = 0;
m_nbBone = nbBone;
m_mesh = new MeshDynamic(indices, vertices,normals,textCoord);
this->m_location = QVector3D(0,0,0);
this->m_pivot = QVector3D(0,0,0);
this->m_scale = QVector3D(1,1,1);
this->m_rotation = QQuaternion();
m_rootBone->calcInverseBindTransform(QMatrix4x4());
}
Object3DDynamic::~Object3DDynamic()
{
for(int i = 0; i < m_child.size(); ++i)
{
delete m_child[i];
}
delete m_mesh;
delete m_rootBone;
delete m_texture;
}
void Object3DDynamic::setTexture(QString textUrl){
if(textUrl!=""){
QImage temp = QImage(QString(textUrl));
m_texture = new QOpenGLTexture(QImage(QString(textUrl)));}
}
void Object3DDynamic::setRotation(QQuaternion rotation){
this->m_rotation = rotation;
}
QQuaternion Object3DDynamic::getRotation(){
return this->m_rotation;
}
void Object3DDynamic::setLocation(QVector3D location){
this->m_location = location;
}
QVector3D Object3DDynamic::getLocation(){
return this->m_location;
}
void Object3DDynamic::setScale(QVector3D scale){
this->m_scale = scale;
}
QVector3D Object3DDynamic::getScale(){
return this->m_scale;
}
void Object3DDynamic::setPivot(QVector3D pivot){
this->m_pivot = pivot;
}
QVector3D Object3DDynamic::getPivot(){
return this->m_pivot;
}
void Object3DDynamic::setParent(Object3DDynamic* parent){
m_parent = parent;
}
QMatrix4x4 Object3DDynamic::getTransform(){
return this->m_transform;
}
void Object3DDynamic::setTransform(QMatrix4x4 transform){
this->m_transform = m_transform;
}
MeshDynamic* Object3DDynamic::getMesh(){
return m_mesh;
}
void Object3DDynamic::drawGeometry(QOpenGLShaderProgram *program,QMatrix4x4 projection)
{
if(m_parent != 0)
{
m_transform = m_parent->getTransform();
}
else
{
m_transform.setToIdentity();
}
m_transform.translate(m_pivot);
m_transform.rotate(m_rotation);
m_transform.translate(-m_pivot);
m_transform.translate(m_location);
m_transform.scale(m_scale);
program->setUniformValue("mvp", projection * m_transform);
vector<QMatrix4x4> temp1 = getBoneTransforms();
QMatrix4x4* temp = temp1.data();
program->setUniformValueArray("bonesTransforms", temp,m_nbBone);
if(m_texture !=0){
m_texture->bind();
}
m_mesh->drawGeometry(program);
}
Bone* Object3DDynamic::getRootBone(){
return m_rootBone;
}
void Object3DDynamic::setAnimationList(Animation anim){
this->m_animation = anim;
this->m_animator.setupAnimation(&this->m_animation);
}
void Object3DDynamic::computeAnimation(float time){
m_animator.update(time);
}
vector<QMatrix4x4> Object3DDynamic::getBoneTransforms(){
vector<QMatrix4x4> listMatrix(m_nbBone);
addBonesToArray(m_rootBone, listMatrix);
return listMatrix;
}
void Object3DDynamic::addBonesToArray(Bone* headBone, vector<QMatrix4x4>& boneMatrix)
{
QMatrix4x4 temp =headBone->getAnimatedTransform();
boneMatrix[headBone->getId()] = temp;
for (Bone* childBone : headBone->getChildren()) {
addBonesToArray(childBone, boneMatrix);
}
}