framework2
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"10using namespace ofxFBXSource;11
12//--------------------------------------------------------------
13Bone::Bone() {14bExists = false;15// sourceBone = NULL;16parentBoneName = "";17bIsRoot = false;18enableAnimation();19}
20
21//--------------------------------------------------------------
22Bone::~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//--------------------------------------------------------------
34bool Bone::doesExist() {35return bExists;36}
37
38//--------------------------------------------------------------
39void Bone::setAsRoot() {40bIsRoot = true;41}
42
43//--------------------------------------------------------------
44void Bone::setup( FbxNode* pNode ) {45ofxFBXSource::Node::setup( pNode );46// fbxNode = pNode;47
48// setTransformMatrix( ofGetGlobalTransform( fbxNode, FBXSDK_TIME_INFINITE, NULL ) );49setLocalTransformMatrix( fbxGetGlobalTransform( mFbxNode, FBXSDK_TIME_INFINITE, NULL ) );50bExists = true;51
52updateFbxTransform();53}
54
55//--------------------------------------------------------------
56void Bone::update( FbxTime& pTime, FbxPose* pPose ) {57
58// if(bIsRoot) {
59// cout << "ofxFBXSource::Bone :: update : is root : " << getName() << " animations: " << isAnimationEnabled() << endl;
60// }
61
62if( isAnimationEnabled() ) {63if( !bIsRoot ) {64// setTransformMatrix( ofGetLocalTransform( fbxNode, pTime, pPose, NULL ));65FbxAMatrix& tmatrix = mFbxNode->EvaluateLocalTransform( pTime );66// setTransformMatrix( fbxToOf(tmatrix) );67// setTransformMatrix(tmatrix);68setLocalTransformMatrix(tmatrix);69// setGlobalTransformMatrix(tmatrix);70} else {71// cout << "ofxFBXSource::Bone :: update : is root : " << getName() << endl;
72FbxAMatrix& tmatrix = mFbxNode->EvaluateGlobalTransform( pTime );73// setTransformMatrix(tmatrix);74setLocalTransformMatrix(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
83map<string, shared_ptr<Bone> >::iterator it;84for(it = childBones.begin(); it != childBones.end(); ++it ) {85it->second->update( pTime, pPose );86}87}
88
89//--------------------------------------------------------------
90void Bone::update( int aAnimIndex, signed long aMillis ) {91if( isAnimationEnabled() ) {92ofxFBXSource::Node::update( aAnimIndex, aMillis );93// if( ofGetFrameNum() > 300 ) {
94// cout << "ofxFBXSource::Bone :: update : " << getName() << " isAnimationEnabled(): " << isAnimationEnabled() << " | " << ofGetFrameNum() << endl;
95// }
96}97
98if( 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
106map<string, shared_ptr<Bone> >::iterator it;107for(it = childBones.begin(); it != childBones.end(); ++it ) {108it->second->update( aAnimIndex, aMillis );109}110}
111
112//--------------------------------------------------------------
113void Bone::update( int aAnimIndex1, signed long aAnim1Millis, int aAnimIndex2, signed long aAnim2Millis, float aMixPct ) {114if( isAnimationEnabled() ) {115ofxFBXSource::Node::update( aAnimIndex1, aAnim1Millis, aAnimIndex2, aAnim2Millis, aMixPct );116}117
118map<string, shared_ptr<Bone> >::iterator it;119for(it = childBones.begin(); it != childBones.end(); ++it ) {120it->second->update( aAnimIndex1, aAnim1Millis, aAnimIndex2, aAnim2Millis, aMixPct );121}122}
123
124//--------------------------------------------------------------
125void 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() );
138updateFbxTransform();139// }140
141map<string, shared_ptr<Bone> >::iterator it;142for(it = childBones.begin(); it != childBones.end(); ++it ) {143it->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//--------------------------------------------------------------
169void Bone::updateFbxTransform() {170// we need to convert this back before sending //171fbxTransform = toFbx( getGlobalPosition(), getGlobalOrientation(), getGlobalScale() );172}
173
174//--------------------------------------------------------------
175void Bone::clearKeyFrames() {176ofxFBXSource::Node::clearKeyFrames();177
178map<string, shared_ptr<ofxFBXSource::Bone> >::iterator it;179for(it = childBones.begin(); it != childBones.end(); ++it ) {180it->second->clearKeyFrames();181}182}
183
184//--------------------------------------------------------------
185bool Bone::isLimb() {186FbxSkeleton* lSkeleton = getFbxSkeleton();187if(lSkeleton) {188lSkeleton->GetSkeletonType() == FbxSkeleton::eLimbNode;189return true;190}191return false;192}
193
194//--------------------------------------------------------------
195bool Bone::hasSkeletonParent() {196if(!mFbxNode->GetParent()) return false;197if(!mFbxNode->GetParent()->GetNodeAttribute()) return false;198return mFbxNode->GetParent()->GetNodeAttribute()->GetAttributeType() == FbxNodeAttribute::eSkeleton;199}
200
201//--------------------------------------------------------------
202FbxSkeleton* Bone::getFbxSkeleton() {203return (FbxSkeleton*) mFbxNode->GetNodeAttribute();204}
205
206//--------------------------------------------------------------
207void Bone::enableAnimation( bool bRecursively ) {208bUpdateFromAnimation = true;209if( bRecursively ) {210map< string, shared_ptr<ofxFBXSource::Bone> >::iterator it;211for(it = childBones.begin(); it != childBones.end(); ++it ) {212it->second->enableAnimation( bRecursively );213}214}215}
216
217//--------------------------------------------------------------
218void Bone::disableAnimation( bool bRecursively) {219bUpdateFromAnimation = false;220if( bRecursively ) {221map< string, shared_ptr<ofxFBXSource::Bone> >::iterator it;222for(it = childBones.begin(); it != childBones.end(); ++it ) {223it->second->disableAnimation( bRecursively );224}225}226}
227
228//--------------------------------------------------------------
229bool Bone::isAnimationEnabled() {230return bUpdateFromAnimation;231}
232
233//--------------------------------------------------------------
234glm::quat& Bone::getOriginalLocalRotation() {235return origLocalRotation;236}
237
238//--------------------------------------------------------------
239int Bone::getNumBones() {240int ttotal = childBones.size();241map<string, shared_ptr<Bone> >::iterator it;242for(it = childBones.begin(); it != childBones.end(); ++it ) {243ttotal += it->second->getNumBones();244}245return 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//--------------------------------------------------------------
257void Bone::populateBonesRecursive( map< string, shared_ptr<Bone> >& aBoneMap ) {258
259map<string, shared_ptr<Bone> >::iterator it;260for(it = childBones.begin(); it != childBones.end(); ++it ) {261if( !aBoneMap.count(it->first) ) {262aBoneMap[ it->first ] = (it->second);263}264it->second->populateBonesRecursive( aBoneMap );265}266}
267
268//--------------------------------------------------------------
269string Bone::getAsString( int aLevel ) {270stringstream oStr;// = "";271for( int i = 0; i < aLevel; i++ ) {272oStr << " ";273}274if( aLevel > 0 ) {275oStr <<" '";276}277if( childBones.size() ) {278oStr << "+ ";279} else {280oStr << "- ";281}282string pname = "";283if( getParent() != NULL ) {284pname = " parent: " + parentBoneName;285}286
287oStr << getTypeAsString() << ": " << getName() << pname << " fbx type: " << getFbxTypeString() << " anim: " << isAnimationEnabled();288if( childBones.size() > 0 ) {289oStr << " child bones: " << childBones.size();290}291if(usingKeyFrames()) {292oStr << " num keys: " << mKeyCollections.size();293}294oStr << endl;295
296// oStr << getTypeAsString()+": " + getName() + " kids: " +ofToString( childBones.size(), 0) + pname + " anim: " + ofToString( isAnimationEnabled(), 0) + " num keys: " +ofToString(mKeyCollections.size(),0) + "\n";297
298map<string, shared_ptr<Bone> >::iterator it;299for(it = childBones.begin(); it != childBones.end(); ++it ) {300oStr << it->second->getAsString( aLevel + 1);301}302return oStr.str();303}
304
305//--------------------------------------------------------------
306shared_ptr<Bone> Bone::getBone( string aName ) {307shared_ptr<Bone> tbone;// = NULL;308// map< string, ofxFBXBone >::iterator it;309findBoneRecursive( aName, tbone );310if( tbone ) {311return tbone;312}313return NULL;314}
315
316//--------------------------------------------------------------
317void Bone::findBoneRecursive( string aName, shared_ptr<Bone>& returnBone ) {318
319if( !returnBone ) {320map< string, shared_ptr<Bone> >::iterator it;321for(it = childBones.begin(); it != childBones.end(); ++it ) {322if( it->second->getName() == aName ) {323returnBone = (it->second);324break;325}326it->second->findBoneRecursive( aName, returnBone );327}328}329}
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346