framework2

Форк
0
329 строк · 11.1 Кб
1
//
2
//  ofxFBXSrcBone.cpp
3
//  ConnectionsWall-Nick
4
//
5
//  Created by Nick Hardeman on 7/11/19.
6
//
7

8
#include "ofxFBXSrcBone.h"
9
#include "ofxFBXUtils.h"
10
using namespace ofxFBXSource;
11

12
//--------------------------------------------------------------
13
Bone::Bone() {
14
    bExists         = false;
15
    //    sourceBone      = NULL;
16
    parentBoneName  = "";
17
    bIsRoot         = false;
18
    enableAnimation();
19
}
20

21
//--------------------------------------------------------------
22
Bone::~Bone() {
23
//    map<string, ofxFBXBone >::iterator it;
24
//    for(it = bones.begin(); it != bones.end(); ++it ) {
25
//        it->second.clearParent();
26
//    }
27
//
28
//    if( getParent() != NULL ) {
29
//        clearParent();
30
//    }
31
}
32

33
//--------------------------------------------------------------
34
bool Bone::doesExist() {
35
    return bExists;
36
}
37

38
//--------------------------------------------------------------
39
void Bone::setAsRoot() {
40
    bIsRoot = true;
41
}
42

43
//--------------------------------------------------------------
44
void Bone::setup( FbxNode* pNode ) {
45
    ofxFBXSource::Node::setup( pNode );
46
    //    fbxNode = pNode;
47
    
48
    //    setTransformMatrix( ofGetGlobalTransform( fbxNode, FBXSDK_TIME_INFINITE, NULL ) );
49
    setLocalTransformMatrix( fbxGetGlobalTransform( mFbxNode, FBXSDK_TIME_INFINITE, NULL ) );
50
    bExists = true;
51
    
52
    updateFbxTransform();
53
}
54

55
//--------------------------------------------------------------
56
void Bone::update( FbxTime& pTime, FbxPose* pPose ) {
57
    
58
//    if(bIsRoot) {
59
//        cout << "ofxFBXSource::Bone :: update : is root : " << getName() << " animations: " << isAnimationEnabled() << endl;
60
//    }
61
    
62
    if( isAnimationEnabled() ) {
63
        if( !bIsRoot ) {
64
            //            setTransformMatrix( ofGetLocalTransform( fbxNode, pTime, pPose, NULL ));
65
            FbxAMatrix& tmatrix = mFbxNode->EvaluateLocalTransform( pTime );
66
            //            setTransformMatrix( fbxToOf(tmatrix) );
67
            //            setTransformMatrix(tmatrix);
68
            setLocalTransformMatrix(tmatrix);
69
            //            setGlobalTransformMatrix(tmatrix);
70
        } else {
71
//            cout << "ofxFBXSource::Bone :: update : is root : " << getName() << endl;
72
            FbxAMatrix& tmatrix = mFbxNode->EvaluateGlobalTransform( pTime );
73
            //            setTransformMatrix(tmatrix);
74
            setLocalTransformMatrix(tmatrix);
75
            //            setTransformMatrix( fbxToOf(tmatrix) );
76
            
77
            //            setTransformMatrix( ofGetLocalTransform( fbxNode, pTime, pPose, NULL ));
78
            //FbxAMatrix& tmatrix = fbxNode->EvaluateLocalTransform( pTime );
79
            //setTransformMatrix( toOf(tmatrix) );
80
        }
81
    }
82
    
83
    map<string, shared_ptr<Bone> >::iterator it;
84
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
85
        it->second->update( pTime, pPose );
86
    }
87
}
88

89
//--------------------------------------------------------------
90
void Bone::update( int aAnimIndex, signed long aMillis ) {
91
    if( isAnimationEnabled() ) {
92
        ofxFBXSource::Node::update( aAnimIndex, aMillis );
93
//        if( ofGetFrameNum() > 300 ) {
94
//            cout << "ofxFBXSource::Bone :: update : " << getName() << " isAnimationEnabled(): " << isAnimationEnabled() << " | " << ofGetFrameNum() << endl;
95
//        }
96
    }
97
    
98
    if( bIsRoot ) {
99
//        cout << "ofxFBXSource::Bone :: update : is root : " << getName() << " animations: " << isAnimationEnabled() << " skel root: " << mFbxNode->GetSkeleton()->IsSkeletonRoot() << " | " << ofGetFrameNum() << endl;
100
//        if( getParent() != NULL ) {
101
//            cout << " getParent position: " << getParent()->getPosition() << endl;
102
//        }
103
        
104
    }
105
    
106
    map<string, shared_ptr<Bone> >::iterator it;
107
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
108
        it->second->update( aAnimIndex, aMillis );
109
    }
110
}
111

112
//--------------------------------------------------------------
113
void Bone::update( int aAnimIndex1, signed long aAnim1Millis, int aAnimIndex2, signed long aAnim2Millis, float aMixPct ) {
114
    if( isAnimationEnabled() ) {
115
        ofxFBXSource::Node::update( aAnimIndex1, aAnim1Millis, aAnimIndex2, aAnim2Millis, aMixPct );
116
    }
117
    
118
    map<string, shared_ptr<Bone> >::iterator it;
119
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
120
        it->second->update( aAnimIndex1, aAnim1Millis, aAnimIndex2, aAnim2Millis, aMixPct );
121
    }
122
}
123

124
//--------------------------------------------------------------
125
void Bone::lateUpdate() {
126
//    ofxFBXBone* sbone   = sourceBone;
127
//    if( sbone != NULL ) {
128
//        setPosition( getPosition() );
129
//        setOrientation( getOrientationQuat() );
130
//        setScale( getScale() );
131
//        sbone->setGlobalPosition( getGlobalPosition() );
132
//        sbone->setGlobalOrientation( getGlobalOrientation() );
133
//        sbone->setScale( getScale() );
134
//        sbone->setGlobalSca( getGlobalScale() );
135

136
//        sbone->setTransformMatrix( getLocalTransformMatrix() );
137
//        sbone->setTransformMatrix( getGlobalTransformMatrix() );
138
    updateFbxTransform();
139
    //    }
140
    
141
    map<string, shared_ptr<Bone> >::iterator it;
142
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
143
        it->second->lateUpdate();
144
    }
145
}
146

147
//--------------------------------------------------------------
148
//void Bone::draw( float aLen, bool aBDrawAxes ) {
149
//    if(aBDrawAxes) {
150
//        transformGL(); {
151
//            ofDrawAxis( aLen );
152
//        } restoreTransformGL();
153
//    }
154
//    //    if(!hasSkeletonParent()) return;
155
//    //    if(!isLimb()) return;
156
//    if( getParent() != NULL && !bIsRoot ) {
157
//        //ofSetColor(255, 0, 130 );
158
//        glm::vec3 ppos = getParent()->getGlobalPosition();
159
//        ofDrawLine( ppos, getGlobalPosition() );
160
//    }
161
//
162
//    map< string, Bone* >::iterator it;
163
//    for(it = childBones.begin(); it != childBones.end(); ++it ) {
164
//        it->second->draw( aLen );
165
//    }
166
//}
167

168
//--------------------------------------------------------------
169
void Bone::updateFbxTransform() {
170
    // we need to convert this back before sending //
171
    fbxTransform = toFbx( getGlobalPosition(), getGlobalOrientation(), getGlobalScale() );
172
}
173

174
//--------------------------------------------------------------
175
void Bone::clearKeyFrames() {
176
    ofxFBXSource::Node::clearKeyFrames();
177
    
178
    map<string, shared_ptr<ofxFBXSource::Bone> >::iterator it;
179
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
180
        it->second->clearKeyFrames();
181
    }
182
}
183

184
//--------------------------------------------------------------
185
bool Bone::isLimb() {
186
    FbxSkeleton* lSkeleton = getFbxSkeleton();
187
    if(lSkeleton) {
188
        lSkeleton->GetSkeletonType() == FbxSkeleton::eLimbNode;
189
        return true;
190
    }
191
    return false;
192
}
193

194
//--------------------------------------------------------------
195
bool Bone::hasSkeletonParent() {
196
    if(!mFbxNode->GetParent()) return false;
197
    if(!mFbxNode->GetParent()->GetNodeAttribute()) return false;
198
    return mFbxNode->GetParent()->GetNodeAttribute()->GetAttributeType() == FbxNodeAttribute::eSkeleton;
199
}
200

201
//--------------------------------------------------------------
202
FbxSkeleton* Bone::getFbxSkeleton() {
203
    return (FbxSkeleton*) mFbxNode->GetNodeAttribute();
204
}
205

206
//--------------------------------------------------------------
207
void Bone::enableAnimation( bool bRecursively ) {
208
    bUpdateFromAnimation = true;
209
    if( bRecursively ) {
210
        map< string, shared_ptr<ofxFBXSource::Bone> >::iterator it;
211
        for(it = childBones.begin(); it != childBones.end(); ++it ) {
212
            it->second->enableAnimation( bRecursively );
213
        }
214
    }
215
}
216

217
//--------------------------------------------------------------
218
void Bone::disableAnimation( bool bRecursively) {
219
    bUpdateFromAnimation = false;
220
    if( bRecursively ) {
221
        map< string, shared_ptr<ofxFBXSource::Bone> >::iterator it;
222
        for(it = childBones.begin(); it != childBones.end(); ++it ) {
223
            it->second->disableAnimation( bRecursively );
224
        }
225
    }
226
}
227

228
//--------------------------------------------------------------
229
bool Bone::isAnimationEnabled() {
230
    return bUpdateFromAnimation;
231
}
232

233
//--------------------------------------------------------------
234
glm::quat& Bone::getOriginalLocalRotation() {
235
    return origLocalRotation;
236
}
237

238
//--------------------------------------------------------------
239
int Bone::getNumBones() {
240
    int ttotal = childBones.size();
241
    map<string, shared_ptr<Bone> >::iterator it;
242
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
243
        ttotal += it->second->getNumBones();
244
    }
245
    return ttotal;
246
}
247

248
//--------------------------------------------------------------
249
//map< string, shared_ptr<Bone> > Bone::getAllBones() {
250
//    map< string, shared_ptr<Bone> > tbones;
251
//    tbones[ getName() ] = this;
252
//    populateBonesRecursive( tbones );
253
//    return tbones;
254
//}
255

256
//--------------------------------------------------------------
257
void Bone::populateBonesRecursive( map< string, shared_ptr<Bone> >& aBoneMap ) {
258
    
259
    map<string, shared_ptr<Bone> >::iterator it;
260
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
261
        if( !aBoneMap.count(it->first) ) {
262
            aBoneMap[ it->first ] = (it->second);
263
        }
264
        it->second->populateBonesRecursive( aBoneMap );
265
    }
266
}
267

268
//--------------------------------------------------------------
269
string Bone::getAsString( int aLevel ) {
270
    stringstream oStr;// = "";
271
    for( int i = 0; i < aLevel; i++ ) {
272
        oStr << "  ";
273
    }
274
    if( aLevel > 0 ) {
275
        oStr <<" '";
276
    }
277
    if( childBones.size() ) {
278
        oStr << "+ ";
279
    } else {
280
        oStr << "- ";
281
    }
282
    string pname = "";
283
    if( getParent() != NULL ) {
284
        pname = " parent: " + parentBoneName;
285
    }
286
    
287
    oStr << getTypeAsString() << ": " << getName() << pname << " fbx type: " << getFbxTypeString() << " anim: " << isAnimationEnabled();
288
    if( childBones.size() > 0 ) {
289
        oStr << " child bones: " << childBones.size();
290
    }
291
    if(usingKeyFrames()) {
292
        oStr << " num keys: " << mKeyCollections.size();
293
    }
294
    oStr << endl;
295
    
296
    //    oStr << getTypeAsString()+": " + getName() + " kids: " +ofToString( childBones.size(), 0) + pname + " anim: " + ofToString( isAnimationEnabled(), 0) + " num keys: " +ofToString(mKeyCollections.size(),0) + "\n";
297
    
298
    map<string, shared_ptr<Bone> >::iterator it;
299
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
300
        oStr << it->second->getAsString( aLevel + 1);
301
    }
302
    return oStr.str();
303
}
304

305
//--------------------------------------------------------------
306
shared_ptr<Bone> Bone::getBone( string aName ) {
307
    shared_ptr<Bone> tbone;// = NULL;
308
    //    map< string, ofxFBXBone >::iterator it;
309
    findBoneRecursive( aName, tbone );
310
    if( tbone ) {
311
        return tbone;
312
    }
313
    return NULL;
314
}
315

316
//--------------------------------------------------------------
317
void Bone::findBoneRecursive( string aName, shared_ptr<Bone>& returnBone ) {
318
    
319
    if( !returnBone ) {
320
        map< string, shared_ptr<Bone> >::iterator it;
321
        for(it = childBones.begin(); it != childBones.end(); ++it ) {
322
            if( it->second->getName() == aName ) {
323
                returnBone = (it->second);
324
                break;
325
            }
326
            it->second->findBoneRecursive( aName, returnBone );
327
        }
328
    }
329
}
330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.