|
@@ -11,47 +11,14 @@
|
11
|
11
|
#include "SkeletalModel.h"
|
12
|
12
|
#include "World.h"
|
13
|
13
|
|
14
|
|
-BoneTag::BoneTag(int m, float o[3], float r[3], char f) {
|
15
|
|
- mesh = m;
|
16
|
|
- flag = f;
|
17
|
|
- for (int i = 0; i < 3; i++) {
|
18
|
|
- off[i] = o[i];
|
19
|
|
- rot[i] = r[i];
|
20
|
|
- }
|
21
|
|
-}
|
22
|
|
-
|
23
|
|
-void BoneTag::display() {
|
24
|
|
- /*
|
25
|
|
- if (getWorld().sizeMesh() > 0)
|
26
|
|
- getWorld().getMesh(mesh).drawSolid(); // TODO ?
|
27
|
|
- else
|
28
|
|
- getWorld().getStaticMesh(mesh).display();
|
29
|
|
- */
|
30
|
|
-}
|
31
|
|
-
|
32
|
|
-void BoneTag::getOffset(float o[3]) {
|
33
|
|
- o[0] = off[0];
|
34
|
|
- o[1] = off[1];
|
35
|
|
- o[2] = off[2];
|
36
|
|
-}
|
37
|
|
-
|
38
|
|
-void BoneTag::getRotation(float r[3]) {
|
39
|
|
- r[0] = rot[0];
|
40
|
|
- r[1] = rot[1];
|
41
|
|
- r[2] = rot[2];
|
42
|
|
-}
|
|
14
|
+#include <glm/gtc/matrix_transform.hpp>
|
43
|
15
|
|
44
|
|
-char BoneTag::getFlag() {
|
45
|
|
- return flag;
|
|
16
|
+void BoneTag::display(glm::mat4 MVP) {
|
|
17
|
+ getWorld().getMesh(mesh).display(MVP);
|
46
|
18
|
}
|
47
|
19
|
|
48
|
20
|
// ----------------------------------------------------------------------------
|
49
|
21
|
|
50
|
|
-BoneFrame::BoneFrame(float p[3]) {
|
51
|
|
- for (int i = 0; i < 3; i++)
|
52
|
|
- pos[i] = p[i];
|
53
|
|
-}
|
54
|
|
-
|
55
|
22
|
BoneFrame::~BoneFrame() {
|
56
|
23
|
for (unsigned long i = 0; i < tag.size(); i++)
|
57
|
24
|
delete tag[i];
|
|
@@ -70,18 +37,8 @@ void BoneFrame::add(BoneTag* t) {
|
70
|
37
|
tag.push_back(t);
|
71
|
38
|
}
|
72
|
39
|
|
73
|
|
-void BoneFrame::getPosition(float p[3]) {
|
74
|
|
- p[0] = pos[0];
|
75
|
|
- p[1] = pos[1];
|
76
|
|
- p[2] = pos[2];
|
77
|
|
-}
|
78
|
|
-
|
79
|
40
|
// ----------------------------------------------------------------------------
|
80
|
41
|
|
81
|
|
-AnimationFrame::AnimationFrame(char r) {
|
82
|
|
- rate = r;
|
83
|
|
-}
|
84
|
|
-
|
85
|
42
|
AnimationFrame::~AnimationFrame() {
|
86
|
43
|
for (unsigned long i = 0; i < frame.size(); i++)
|
87
|
44
|
delete frame[i];
|
|
@@ -102,50 +59,113 @@ void AnimationFrame::add(BoneFrame* f) {
|
102
|
59
|
|
103
|
60
|
// ----------------------------------------------------------------------------
|
104
|
61
|
|
|
62
|
+class MatrixStack {
|
|
63
|
+ public:
|
|
64
|
+ MatrixStack(glm::mat4 start) : startVal(start) { stack.push_back(startVal); }
|
|
65
|
+
|
|
66
|
+ void push() {
|
|
67
|
+ //assert(stack.size() > 0);
|
|
68
|
+ if (stack.size() > 0)
|
|
69
|
+ stack.push_back(stack.at(stack.size() - 1));
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ void pop() {
|
|
73
|
+ //assert(stack.size() > 0);
|
|
74
|
+ if (stack.size() > 0)
|
|
75
|
+ stack.pop_back();
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ glm::mat4 get() {
|
|
79
|
+ //assert(stack.size() > 0);
|
|
80
|
+ if (stack.size() > 0)
|
|
81
|
+ return stack.at(stack.size() - 1);
|
|
82
|
+ return startVal;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ private:
|
|
86
|
+ std::vector<glm::mat4> stack;
|
|
87
|
+ glm::mat4 startVal;
|
|
88
|
+};
|
|
89
|
+
|
|
90
|
+// ----------------------------------------------------------------------------
|
|
91
|
+
|
105
|
92
|
SkeletalModel::~SkeletalModel() {
|
106
|
93
|
for (unsigned long i = 0; i < animation.size(); i++)
|
107
|
94
|
delete animation[i];
|
108
|
95
|
}
|
109
|
96
|
|
|
97
|
+//#define DEBUG_MODELS
|
|
98
|
+
|
|
99
|
+#ifdef DEBUG_MODELS
|
|
100
|
+#include <bitset>
|
|
101
|
+#endif
|
|
102
|
+
|
110
|
103
|
void SkeletalModel::display(glm::mat4 MVP, int aframe, int bframe) {
|
111
|
|
- /*
|
112
|
104
|
assert(aframe < size());
|
113
|
105
|
assert(bframe < get(aframe).size());
|
114
|
106
|
|
115
|
107
|
AnimationFrame& anim = get(aframe);
|
116
|
108
|
BoneFrame& boneframe = anim.get(bframe);
|
117
|
109
|
|
118
|
|
- float pos[3];
|
119
|
|
- boneframe.getPosition(pos);
|
120
|
|
- glTranslatef(pos[0], pos[1], pos[2]);
|
|
110
|
+ glm::vec3 pos = boneframe.getPosition();
|
|
111
|
+ glm::mat4 frameTrans = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, -pos.y, pos.z));
|
|
112
|
+
|
|
113
|
+ MatrixStack stack(MVP * frameTrans);
|
|
114
|
+
|
|
115
|
+#ifdef DEBUG_MODELS
|
|
116
|
+ Log::get(LOG_DEBUG) << "Starting SkeletalModel:" << Log::endl;
|
|
117
|
+ int cnt = 0;
|
|
118
|
+#endif
|
121
|
119
|
|
122
|
120
|
for (unsigned int a = 0; a < boneframe.size(); a++) {
|
123
|
121
|
BoneTag& tag = boneframe.get(a);
|
124
|
|
- float rot[3], off[3];
|
125
|
|
-
|
126
|
|
- tag.getRotation(rot);
|
127
|
|
- tag.getOffset(off);
|
128
|
|
-
|
129
|
|
- if (a == 0) {
|
130
|
|
- glRotatef(rot[1], 0, 1, 0);
|
131
|
|
- glRotatef(rot[0], 1, 0, 0);
|
132
|
|
- glRotatef(rot[2], 0, 0, 1);
|
133
|
|
- } else {
|
134
|
|
- if (tag.getFlag() & 0x01)
|
135
|
|
- glPopMatrix();
|
136
|
|
-
|
137
|
|
- if (tag.getFlag() & 0x02)
|
138
|
|
- glPushMatrix();
|
139
|
|
-
|
140
|
|
- glTranslatef(off[0], off[1], off[2]);
|
141
|
|
- glRotatef(rot[1], 0, 1, 0);
|
142
|
|
- glRotatef(rot[0], 1, 0, 0);
|
143
|
|
- glRotatef(rot[2], 0, 0, 1);
|
|
122
|
+
|
|
123
|
+ glm::mat4 translate(1.0f);
|
|
124
|
+
|
|
125
|
+ if (a != 0) {
|
|
126
|
+ if (tag.getFlag() & 0x01) {
|
|
127
|
+ stack.pop();
|
|
128
|
+#ifdef DEBUG_MODELS
|
|
129
|
+ Log::get(LOG_DEBUG) << " --> pop" << Log::endl;
|
|
130
|
+ cnt--;
|
|
131
|
+#endif
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ if (tag.getFlag() & 0x02) {
|
|
135
|
+ stack.push();
|
|
136
|
+#ifdef DEBUG_MODELS
|
|
137
|
+ Log::get(LOG_DEBUG) << " --> push" << Log::endl;
|
|
138
|
+ cnt++;
|
|
139
|
+#endif
|
|
140
|
+ }
|
|
141
|
+
|
|
142
|
+#ifdef DEBUG_MODELS
|
|
143
|
+ if (tag.getFlag() & ~0x03) {
|
|
144
|
+ std::bitset<8> bs(tag.getFlag());
|
|
145
|
+ Log::get(LOG_DEBUG) << " Unexpected flag " << bs << Log::endl;
|
|
146
|
+ }
|
|
147
|
+#endif
|
|
148
|
+
|
|
149
|
+ glm::vec3 off = tag.getOffset();
|
|
150
|
+ translate = glm::translate(glm::mat4(1.0f), glm::vec3(off.x, -off.y, off.z));
|
144
|
151
|
}
|
145
|
152
|
|
146
|
|
- tag.display();
|
|
153
|
+ glm::vec3 rot = tag.getRotation();
|
|
154
|
+ glm::mat4 rotY = glm::rotate(glm::mat4(1.0f), rot[1], glm::vec3(0.0f, 1.0f, 0.0f));
|
|
155
|
+ glm::mat4 rotX = glm::rotate(glm::mat4(1.0f), rot[0], glm::vec3(1.0f, 0.0f, 0.0f));
|
|
156
|
+ glm::mat4 rotZ = glm::rotate(glm::mat4(1.0f), rot[2], glm::vec3(0.0f, 0.0f, 1.0f));
|
|
157
|
+ glm::mat4 rotate = rotZ * rotX * rotY;
|
|
158
|
+
|
|
159
|
+ glm::mat4 mod = translate * rotate;
|
|
160
|
+ tag.display(stack.get() * mod);
|
|
161
|
+#ifdef DEBUG_MODELS
|
|
162
|
+ Log::get(LOG_DEBUG) << " --> get (" << cnt << ")" << Log::endl;
|
|
163
|
+#endif
|
147
|
164
|
}
|
148
|
|
- */
|
|
165
|
+
|
|
166
|
+#ifdef DEBUG_MODELS
|
|
167
|
+ Log::get(LOG_DEBUG) << "Done!" << Log::endl;
|
|
168
|
+#endif
|
149
|
169
|
}
|
150
|
170
|
|
151
|
171
|
unsigned long SkeletalModel::size() {
|