forked from openframeworks/openFrameworks
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathofxAssimpSrcAnimKeyCollection.cpp
More file actions
153 lines (135 loc) · 4.25 KB
/
ofxAssimpSrcAnimKeyCollection.cpp
File metadata and controls
153 lines (135 loc) · 4.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
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
#include "ofxAssimpSrcAnimKeyCollection.h"
#include "ofxAssimpUtils.h"
using namespace ofxAssimp;
//--------------------------------------------------------------
void SrcAnimKeyCollection::setup( aiNodeAnim* aNodeAnim, float aDurationInTicks ) {
mNodeAnim = aNodeAnim;
mDurationInTicks = aDurationInTicks;
}
//--------------------------------------------------------------
bool SrcAnimKeyCollection::hasKeys() {
return mNodeAnim != nullptr;
}
//--------------------------------------------------------------
glm::vec3 SrcAnimKeyCollection::getVec3ForTime( const float& atime, const std::vector<ofxAssimp::AnimVectorKey>& akeys ) {
size_t numKeys = akeys.size();
for( size_t i = 0; i < numKeys; i++ ) {
if( akeys[i].time == atime ) {
return akeys[i].value;
} else if( akeys[i].time > atime ) {
if( i > 0 ) {
float keyDiff = akeys[i].time - akeys[i-1].time;
return glm::mix( akeys[i-1].value, akeys[i].value, (atime-akeys[i-1].time) / keyDiff );
} else {
return akeys[i].value;
}
}
}
if( numKeys > 1 ) {
return akeys.back().value;
}
return glm::vec3(0.f, 0.f, 0.f);
}
//--------------------------------------------------------------
glm::vec3 SrcAnimKeyCollection::getPosition( const float& atime ) {
auto rpos = glm::vec3(0.f, 0.f, 0.f);
if( positionKeys.size() < 1 ) {
} else if( positionKeys.size() == 1 ) {
rpos = positionKeys[0].value;
} else {
rpos = getVec3ForTime( atime, positionKeys );
}
return rpos;
}
//--------------------------------------------------------------
glm::vec3 SrcAnimKeyCollection::getScale( const float& atime ) {
auto rscale = glm::vec3(1.f, 1.f, 1.f);
if( scaleKeys.size() < 1 ) {
} else if(scaleKeys.size() == 1 ) {
rscale = scaleKeys[0].value;
} else {
rscale = getVec3ForTime( atime, scaleKeys );
}
return rscale;
}
//--------------------------------------------------------------
glm::quat SrcAnimKeyCollection::getRotation( const float& atime ) {
size_t numKeys = rotationKeys.size();
auto rq = glm::quat(1.f, 0.f, 0.f, 0.f);
if(numKeys < 2) {
if( numKeys == 1 ) {
return rotationKeys[0].value;
} else {
return rq;
}
} else {
for( size_t i = 0; i < numKeys; i++ ) {
if( rotationKeys[i].time == atime ) {
return rotationKeys[i].value;
} else if( rotationKeys[i].time > atime ) {
if( i > 0 ) {
float keyDiff = rotationKeys[i].time - rotationKeys[i-1].time;
return glm::slerp( rotationKeys[i-1].value, rotationKeys[i].value, ((atime-rotationKeys[i-1].time) / keyDiff) );
} else {
return rotationKeys[i].value;
}
}
}
}
if( numKeys > 1 ) {
return rotationKeys.back().value;
}
return rq;
}
//--------------------------------------------------------------
std::vector<AnimVectorKey> SrcAnimKeyCollection::getAnimVectorKeysForTime(const float& aStartTime, const float& aEndTime, unsigned int aNumKeys, aiVectorKey* aAiKeys) {
std::vector<AnimVectorKey> rkeys;
if( aNumKeys < 1 ) {
return rkeys;
}
if(aNumKeys == 1) {
AnimVectorKey vkey;
vkey.time = aStartTime;
vkey.value = ofxAssimp::Utils::aiVecToOfVec(aAiKeys[0].mValue);
vkey.valueAi = aAiKeys[0].mValue;
rkeys.push_back( vkey );
return rkeys;
}
double currTime = aStartTime;
for( unsigned int i = 0; i < aNumKeys; i++ ) {
auto& key1 = aAiKeys[i];
auto v1 = ofxAssimp::Utils::aiVecToOfVec(key1.mValue);
AnimVectorKey vkey;
vkey.time = key1.mTime;
vkey.value = v1;
vkey.valueAi = key1.mValue;
rkeys.push_back( vkey );
}
return rkeys;
}
//--------------------------------------------------------------
std::vector<AnimRotationKey> SrcAnimKeyCollection::getAnimRotationKeysForTime(const float& aStartTime, const float& aEndTime, unsigned int aNumKeys, aiQuatKey* aAiKeys) {
std::vector<AnimRotationKey> rkeys;
if( aNumKeys < 1 ) {
return rkeys;
}
if(aNumKeys == 1) {
AnimRotationKey vkey;
vkey.time = aStartTime;
vkey.value = ofxAssimp::Utils::aiQuatToOfQuat(aAiKeys[0].mValue);
vkey.valueAi = aAiKeys[0].mValue;
rkeys.push_back( vkey );
return rkeys;
}
double currTime = aStartTime;
for( unsigned int i = 0; i < aNumKeys; i++ ) {
auto& key1 = aAiKeys[i];
auto v1 = ofxAssimp::Utils::aiQuatToOfQuat(key1.mValue);
AnimRotationKey vkey;
vkey.time = key1.mTime;
vkey.value = v1;
vkey.valueAi = key1.mValue;
rkeys.push_back( vkey );
}
return rkeys;
}