-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplaySkeleton.cpp
executable file
·332 lines (282 loc) · 9.54 KB
/
displaySkeleton.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
Revision 1 - Steve Lin, Jan. 14, 2002
Revision 2 - Alla and Kiran, Jan 18, 2002
Revision 3 - Jernej Barbic and Yili Zhao, Feb, 2012
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include "types.h"
#include "skeleton.h"
#include "motion.h"
#include "displaySkeleton.h"
////////////////SOFTWARE GL BEGIN///////////////////////
#include <stack>
stack<MATRIX4> matrixStack;
static MATRIX4 currentMatrix = MATRIX4::Identity();
static MATRIX4 toMatrix4(const MATRIX3& A)
{
MATRIX4 result = MATRIX4::Identity();
for (int y = 0; y < 3; y++)
for (int x = 0; x < 3; x++)
result(x,y) = A(x,y);
return result;
}
static void myPushMatrix()
{
matrixStack.push(currentMatrix);
}
static void myPopMatrix()
{
assert(matrixStack.size() != 0);
currentMatrix = matrixStack.top();
matrixStack.pop();
}
static void myTranslatef(const float x, const float y, const float z)
{
MATRIX4 translation = MATRIX4::Identity();
translation(3,0) = x;
translation(3,1) = y;
translation(3,2) = z;
currentMatrix = translation * currentMatrix;
}
static void myRotatef(const float degrees, const float x, const float y, const float z)
{
VEC3 axis(x,y,z);
axis.normalize();
MATRIX3 rotation;
float radians = (degrees / 360.0) * 2.0 * M_PI;
rotation = AngleAxisd(radians, axis);
rotation.transposeInPlace();
currentMatrix = toMatrix4(rotation) * currentMatrix;
}
static void myMultMatrixd(const double* matrix)
{
MATRIX4 A;
int i = 0;
for (int x = 0; x < 4; x++)
for (int y = 0; y < 4; y++, i++)
A(x,y) = matrix[i];
currentMatrix = A * currentMatrix;
}
static void myLoadIdentity()
{
currentMatrix = MATRIX4::Identity();
}
////////////////SOFTWARE GL END///////////////////////
float DisplaySkeleton::jointColors[NUMBER_JOINT_COLORS][3] =
{
{0.0f, 1.0f, 0.0f}, // GREEN
{1.0f, 0.0f, 0.0f}, // RED
{0.0f, 0.0f, 1.0f} // BLUE
};
DisplaySkeleton::DisplaySkeleton(void)
{
m_SpotJoint = -1;
numSkeletons = 0;
for(int skeletonIndex = 0; skeletonIndex < MAX_SKELS; skeletonIndex++)
{
m_pSkeleton[skeletonIndex] = NULL;
m_pMotion[skeletonIndex] = NULL;
}
}
DisplaySkeleton::~DisplaySkeleton(void)
{
Reset();
}
/*
Define M_k = Modelview matrix at the kth node (bone) in the heirarchy
M_k stores the transformation matrix of the kth bone in world coordinates
Our goal is to draw the (k+1)th bone, using its local information and M_k
In the k+1th node, compute the following matrices:
rot_parent_current: this is the rotation matrix that
takes us from k+1 to the kth local coordinate system
R_k+1 : Rotation matrix for the k+1 th node (bone)
using angles specified by the AMC file in local coordinates
T_k+1 : Translation matrix for the k+1th node
The update relation is given by:
M_k+1 = M_k * (rot_parent_current) * R_k+1 + T_k+1
*/
// TODO: Replace GL calls here with Eigen matrices
void DisplaySkeleton::DrawBone(Bone *pBone,int skelNum)
{
static double z_dir[3] = {0.0, 0.0, 1.0};
double r_axis[3], theta;
//currentMatrix = getCurrentModelview();
//Transform (rotate) from the local coordinate system of this bone to it's parent
//This step corresponds to doing: ModelviewMatrix = M_k * (rot_parent_current)
myMultMatrixd((double*)&pBone->rot_parent_current);
//translate AMC (rarely used)
if(pBone->doftz)
myTranslatef(0.0f, 0.0f, float(pBone->tz));
if(pBone->dofty)
myTranslatef(0.0f, float(pBone->ty), 0.0f);
if(pBone->doftx)
myTranslatef(float(pBone->tx), 0.0f, 0.0f);
//rotate AMC
if(pBone->dofrz)
myRotatef(float(pBone->rz), 0.0f, 0.0f, 1.0f);
if(pBone->dofry)
myRotatef(float(pBone->ry), 0.0f, 1.0f, 0.0f);
if(pBone->dofrx)
myRotatef(float(pBone->rx), 1.0f, 0.0f, 0.0f);
//Store the current ModelviewMatrix (before adding the translation part)
myPushMatrix();
//Compute tx, ty, tz : translation from pBone to its child (in local coordinate system of pBone)
double tx = pBone->dir[0] * pBone->length;
double ty = pBone->dir[1] * pBone->length;
double tz = pBone->dir[2] * pBone->length;
// Use the current ModelviewMatrix to display the current bone
// Rotate the bone from its canonical position (elongated sphere
// with its major axis parallel to X axis) to its correct orientation
if(pBone->idx == Skeleton::getRootIndex())
{
// do nothing
}
else
{
//MATRIX4 currentTransform = getCurrentModelview();
MATRIX4 currentTransform = currentMatrix;
//Compute the angle between the canonical pose and the correct orientation
//(specified in pBone->dir) using cross product.
//Using the formula: r_axis = z_dir x pBone->dir
r_axis[0] = z_dir[1]*pBone->dir[2]-z_dir[2]*pBone->dir[1];
r_axis[1] = z_dir[2]*pBone->dir[0]-z_dir[0]*pBone->dir[2];
r_axis[2] = z_dir[0]*pBone->dir[1]-z_dir[1]*pBone->dir[0];
double dot_prod = z_dir[0] * pBone->dir[0] + z_dir[1] * pBone->dir[1] + z_dir[2] * pBone->dir[2] ;
double r_axis_len = sqrt(r_axis[0] * r_axis[0] + r_axis[1] * r_axis[1] + r_axis[2] * r_axis[2]);
theta = atan2(r_axis_len, dot_prod);
VEC3 axis(r_axis[0], r_axis[1], r_axis[2]);
axis.normalize();
MATRIX3 rotation;
rotation = AngleAxisd(theta, axis);
rotation.transposeInPlace();
MATRIX4 scaling = MATRIX4::Identity();
scaling(0,0) = pBone->aspx;
scaling(1,1) = pBone->aspy;
boneScalings[pBone->idx] = scaling;
currentTransform = scaling * toMatrix4(rotation) * currentTransform;
VEC4 currentTranslation;
currentTranslation[0] = currentTransform(3,0);
currentTranslation[1] = currentTransform(3,1);
currentTranslation[2] = currentTransform(3,2);
currentTransform(3,0) = 0;
currentTransform(3,1) = 0;
currentTransform(3,2) = 0;
boneRotations[pBone->idx] = currentTransform.transpose();
boneTranslations[pBone->idx] = currentTranslation;
}
myPopMatrix();
// Finally, translate the bone, depending on its length and direction
// This step corresponds to doing: M_k+1 = ModelviewMatrix += T_k+1
myTranslatef(float(tx), float(ty), float(tz));
}
//Traverse the hierarchy starting from the root
//Every node in the data structure has just one child pointer.
//If there are more than one children for any node, they are stored as sibling pointers
//The algorithm draws the current node (bone), visits its child and then visits siblings
// TODO: Replace GL calls here with Eigen matrices
void DisplaySkeleton::Traverse(Bone *ptr,int skelNum)
{
if(ptr != NULL)
{
myPushMatrix();
DrawBone(ptr,skelNum);
Traverse(ptr->child,skelNum);
myPopMatrix();
Traverse(ptr->sibling,skelNum);
}
}
//Draw the skeleton
// TODO: Replace GL calls here with Eigen matrices
void DisplaySkeleton::ComputeBonePositions(RenderMode renderMode_)
{
unsigned int numbones = m_pSkeleton[0]->numBonesInSkel(*m_pSkeleton[0]->getRoot());
if (boneRotations.size() != numbones)
{
boneRotations.resize(numbones);
boneTranslations.resize(numbones);
boneScalings.resize(numbones);
boneLengths.resize(numbones);
for (unsigned int x = 0; x < numbones; x++)
boneLengths[x] = m_pSkeleton[0]->getBone(x).length;
}
// Set render mode
renderMode = renderMode_;
myPushMatrix();
// load up identity so we just get the pure transforms out at the end,
// not the modelview as well
myLoadIdentity();
//draw the skeleton starting from the root
for (int i = 0; i < numSkeletons; i++)
{
myPushMatrix();
double translation[3];
m_pSkeleton[i]->GetTranslation(translation);
double rotationAngle[3];
m_pSkeleton[i]->GetRotationAngle(rotationAngle);
myTranslatef(float(MOCAP_SCALE * translation[0]), float(MOCAP_SCALE * translation[1]), float(MOCAP_SCALE * translation[2]));
myRotatef(float(rotationAngle[0]), 1.0f, 0.0f, 0.0f);
myRotatef(float(rotationAngle[1]), 0.0f, 1.0f, 0.0f);
myRotatef(float(rotationAngle[2]), 0.0f, 0.0f, 1.0f);
Traverse(m_pSkeleton[i]->getRoot(),i);
myPopMatrix();
}
myPopMatrix();
}
void DisplaySkeleton::LoadMotion(Motion * pMotion)
{
// always load the motion for the latest skeleton
if(m_pMotion[numSkeletons - 1] != NULL)
delete m_pMotion[numSkeletons - 1];
m_pMotion[numSkeletons - 1] = pMotion;
}
//Set skeleton for display
void DisplaySkeleton::LoadSkeleton(Skeleton *pSkeleton)
{
if (numSkeletons >= MAX_SKELS)
return;
m_pSkeleton[numSkeletons] = pSkeleton;
//Create the display list for the skeleton
//All the bones are the elongated spheres centered at (0,0,0).
//The axis of elongation is the X axis.
//SetDisplayList(numSkeletons, m_pSkeleton[numSkeletons]->getRoot(), &m_BoneList[numSkeletons]);
numSkeletons++;
}
Motion * DisplaySkeleton::GetSkeletonMotion(int skeletonIndex)
{
if (skeletonIndex < 0 || skeletonIndex >= MAX_SKELS)
{
printf("Error in DisplaySkeleton::GetSkeletonMotion: index %d is illegal.\n", skeletonIndex);
exit(0);
}
return m_pMotion[skeletonIndex];
}
Skeleton * DisplaySkeleton::GetSkeleton(int skeletonIndex)
{
if (skeletonIndex < 0 || skeletonIndex >= numSkeletons)
{
printf("Error in DisplaySkeleton::GetSkeleton: skeleton index %d is illegal.\n", skeletonIndex);
exit(0);
}
return m_pSkeleton[skeletonIndex];
}
void DisplaySkeleton::Reset(void)
{
for(int skeletonIndex = 0; skeletonIndex < MAX_SKELS; skeletonIndex++)
{
if (m_pSkeleton[skeletonIndex] != NULL)
{
delete (m_pSkeleton[skeletonIndex]);
//glDeleteLists(m_BoneList[skeletonIndex], 1);
m_pSkeleton[skeletonIndex] = NULL;
}
if (m_pMotion[skeletonIndex] != NULL)
{
delete (m_pMotion[skeletonIndex]);
m_pMotion[skeletonIndex] = NULL;
}
}
numSkeletons = 0;
}