framework2
210 строк · 7.1 Кб
1//
2// ofxFBXBone.cpp
3// ofxFBX-Example-Importer
4//
5// Created by Nick Hardeman on 11/1/13.
6//
7//
8
9#include "ofxFBXBone.h"
10#include "ofxFBXUtils.h"
11
12//----------------------------------------
13ofxFBXSource::Node::NodeType ofxFBXBone::getType() {
14return ofxFBXSource::Node::OFX_FBX_BONE;
15}
16
17//--------------------------------------------------------------------------------
18void ofxFBXBone::setBoneSource( shared_ptr<ofxFBXSource::Bone> asrc, shared_ptr<ofxFBXNode> aself ) {
19ofxFBXNode::setup( asrc );
20boneSrc = asrc;
21setup( asrc );
22setPosition( boneSrc->getPosition() );
23setOrientation( boneSrc->getOrientationQuat() );
24setScale( boneSrc->getScale() );
25
26map<string, shared_ptr<ofxFBXSource::Bone> >::iterator it;
27for(it = boneSrc->childBones.begin(); it != boneSrc->childBones.end(); ++it ) {
28auto nb = make_shared<ofxFBXBone>();
29nb->setParent( *this );
30nb->setParentNode( aself );
31addChild(nb);
32childBones[ it->first ] = nb;
33nb->setBoneSource( it->second, nb );
34}
35
36}
37
38//--------------------------------------------------------------------------------
39void ofxFBXBone::update() {
40if(bUpdateFromAnimation) {
41ofxFBXNode::setTransform( boneSrc );
42// cout << "ofxFBXBone :: update : " << getName() << " bUpdateFromAnimation: " << bUpdateFromAnimation << " | " << ofGetFrameNum() << endl;
43}
44
45map<string, shared_ptr<ofxFBXBone> >::iterator it;
46for(it = childBones.begin(); it != childBones.end(); ++it ) {
47it->second->update();
48}
49}
50
51//--------------------------------------------------------------------------------
52void ofxFBXBone::lateUpdate() {
53if( bUpdateFromAnimation ) {
54if( boneSrc->isRoot() && getParent() != NULL ) {
55boneSrc->setPosition( getPosition() + getParent()->getPosition() );
56auto invParent = glm::inverse( getParent()->getOrientationQuat() );
57auto m44 = invParent * getOrientationQuat();
58boneSrc->setOrientation(m44);
59// boneSrc->setOrientation( getGlobalOrientation() );
60// boneSrc->setScale( getScale()*getParent()->getGlobalScale() );
61boneSrc->setScale( getScale() );
62} else {
63boneSrc->setPosition( getPosition() );
64boneSrc->setOrientation( getOrientationQuat() );
65boneSrc->setScale( getScale() );
66}
67boneSrc->updateFbxTransform();
68}
69
70map<string, shared_ptr<ofxFBXBone> >::iterator it;
71for(it = childBones.begin(); it != childBones.end(); ++it ) {
72it->second->lateUpdate();
73}
74}
75
76//--------------------------------------------------------------------------------
77void ofxFBXBone::enableAnimation( bool bRecursively ) {
78bUpdateFromAnimation = true;
79if( bRecursively ) {
80map< string, shared_ptr<ofxFBXBone> >::iterator it;
81for(it = childBones.begin(); it != childBones.end(); ++it ) {
82it->second->enableAnimation( bRecursively );
83}
84}
85}
86
87//--------------------------------------------------------------------------------
88void ofxFBXBone::disableAnimation( bool bRecursively) {
89bUpdateFromAnimation = false;
90if( bRecursively ) {
91map< string, shared_ptr<ofxFBXBone> >::iterator it;
92for(it = childBones.begin(); it != childBones.end(); ++it ) {
93it->second->disableAnimation( bRecursively );
94}
95}
96}
97
98//--------------------------------------------------------------------------------
99bool ofxFBXBone::isAnimationEnabled() {
100return bUpdateFromAnimation;
101}
102
103//--------------------------------------------------------------------------------
104void ofxFBXBone::draw( float aLen, bool aBDrawAxes ) {
105if(aBDrawAxes) {
106transformGL(); {
107ofDrawAxis( aLen );
108} restoreTransformGL();
109}
110
111if( boneSrc != NULL && !boneSrc->isRoot() ) {
112glm::vec3 ppos = getParent()->getGlobalPosition();
113ofDrawLine( ppos, getGlobalPosition() );
114}
115
116map< string, shared_ptr<ofxFBXBone> >::iterator it;
117for(it = childBones.begin(); it != childBones.end(); ++it ) {
118it->second->draw( aLen, aBDrawAxes );
119}
120}
121
122//---------------------------------------------------------------
123int ofxFBXBone::getNumBones() {
124int ttotal = childBones.size();
125map<string, shared_ptr<ofxFBXBone> >::iterator it;
126for(it = childBones.begin(); it != childBones.end(); ++it ) {
127ttotal += it->second->getNumBones();
128}
129return ttotal;
130}
131
132//--------------------------------------------------------------------------------
133shared_ptr<ofxFBXBone> ofxFBXBone::getBone( string aName ) {
134shared_ptr<ofxFBXBone> tbone;// = NULL;
135findBoneRecursive( aName, tbone );
136if( tbone ) {
137return tbone;
138}
139return NULL;
140}
141
142//--------------------------------------------------------------------------------
143void ofxFBXBone::findBoneRecursive( string aName, shared_ptr<ofxFBXBone>& returnBone ) {
144
145if( !returnBone ) {
146map< string, shared_ptr<ofxFBXBone> >::iterator it;
147for(it = childBones.begin(); it != childBones.end(); ++it ) {
148if( it->second->name == aName ) {
149returnBone = (it->second);
150break;
151}
152it->second->findBoneRecursive( aName, returnBone );
153}
154}
155}
156
157//--------------------------------------------------------------------------------
158void ofxFBXBone::pointTo( glm::vec3 aTarget ) {
159glm::vec3 axis( 1, 0, 0 );
160pointTo( aTarget, axis );
161}
162
163//--------------------------------------------------------------------------------
164void ofxFBXBone::pointTo( glm::vec3 aTarget, glm::vec3 aAxis ) {
165auto relPosition = (getGlobalPosition() - aTarget);
166glm::quat tq = glm::rotation( aAxis, glm::normalize(relPosition));
167setGlobalOrientation(tq);
168}
169
170//--------------------------------------------------------------
171string ofxFBXBone::getAsString( int aLevel ) {
172
173stringstream oStr;// = "";
174for( int i = 0; i < aLevel; i++ ) {
175oStr << " ";
176}
177if( aLevel > 0 ) {
178oStr <<" '";
179}
180if( childBones.size() ) {
181oStr << "+ ";
182} else {
183oStr << "- ";
184}
185string pname = "";
186if( hasParentNode() ) {
187pname = " parent: " + getParentNode()->getName();
188}
189
190if( mSrcNode ) {
191oStr << mSrcNode->getTypeAsString() << ": " << getName() << pname << " fbx type: " << mSrcNode->getFbxTypeString() << " anim: " << isAnimationEnabled();
192} else {
193oStr << ": " << getName() << pname << " anim: " << isAnimationEnabled();
194}
195if( childBones.size() > 0 ) {
196oStr << " child bones: " << childBones.size();
197}
198// if(usingKeyFrames()) {
199// oStr << " num keys: " << mKeyCollections.size();
200// }
201oStr << endl;
202
203// oStr << getTypeAsString()+": " + getName() + " kids: " +ofToString( childBones.size(), 0) + pname + " anim: " + ofToString( isAnimationEnabled(), 0) + " num keys: " +ofToString(mKeyCollections.size(),0) + "\n";
204
205map<string, shared_ptr<ofxFBXBone> >::iterator it;
206for(it = childBones.begin(); it != childBones.end(); ++it ) {
207oStr << it->second->getAsString( aLevel + 1);
208}
209return oStr.str();
210}
211
212
213
214
215
216
217
218
219
220