framework2

Форк
0
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
//----------------------------------------
13
ofxFBXSource::Node::NodeType ofxFBXBone::getType() {
14
    return ofxFBXSource::Node::OFX_FBX_BONE;
15
}
16

17
//--------------------------------------------------------------------------------
18
void ofxFBXBone::setBoneSource( shared_ptr<ofxFBXSource::Bone> asrc, shared_ptr<ofxFBXNode> aself ) {
19
    ofxFBXNode::setup( asrc );
20
    boneSrc = asrc;
21
    setup( asrc );
22
    setPosition( boneSrc->getPosition() );
23
    setOrientation( boneSrc->getOrientationQuat() );
24
    setScale( boneSrc->getScale() );
25
    
26
    map<string, shared_ptr<ofxFBXSource::Bone> >::iterator it;
27
    for(it = boneSrc->childBones.begin(); it != boneSrc->childBones.end(); ++it ) {
28
        auto nb = make_shared<ofxFBXBone>();
29
        nb->setParent( *this );
30
        nb->setParentNode( aself );
31
        addChild(nb);
32
        childBones[ it->first ] = nb;
33
        nb->setBoneSource( it->second, nb );
34
    }
35
    
36
}
37

38
//--------------------------------------------------------------------------------
39
void ofxFBXBone::update() {
40
    if(bUpdateFromAnimation) {
41
        ofxFBXNode::setTransform( boneSrc );
42
//        cout << "ofxFBXBone :: update : " << getName() << " bUpdateFromAnimation: " << bUpdateFromAnimation << " | " << ofGetFrameNum() << endl;
43
    }
44
    
45
    map<string, shared_ptr<ofxFBXBone> >::iterator it;
46
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
47
        it->second->update();
48
    }
49
}
50

51
//--------------------------------------------------------------------------------
52
void ofxFBXBone::lateUpdate() {
53
    if( bUpdateFromAnimation ) {
54
        if( boneSrc->isRoot() && getParent() != NULL ) {
55
            boneSrc->setPosition( getPosition() + getParent()->getPosition() );
56
            auto invParent = glm::inverse( getParent()->getOrientationQuat() );
57
            auto m44 = invParent * getOrientationQuat();
58
            boneSrc->setOrientation(m44);
59
    //        boneSrc->setOrientation( getGlobalOrientation() );
60
    //        boneSrc->setScale( getScale()*getParent()->getGlobalScale() );
61
            boneSrc->setScale( getScale() );
62
        } else {
63
            boneSrc->setPosition( getPosition() );
64
            boneSrc->setOrientation( getOrientationQuat() );
65
            boneSrc->setScale( getScale() );
66
        }
67
        boneSrc->updateFbxTransform();
68
    }
69
    
70
    map<string, shared_ptr<ofxFBXBone> >::iterator it;
71
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
72
        it->second->lateUpdate();
73
    }
74
}
75

76
//--------------------------------------------------------------------------------
77
void ofxFBXBone::enableAnimation( bool bRecursively ) {
78
    bUpdateFromAnimation = true;
79
    if( bRecursively ) {
80
        map< string, shared_ptr<ofxFBXBone> >::iterator it;
81
        for(it = childBones.begin(); it != childBones.end(); ++it ) {
82
            it->second->enableAnimation( bRecursively );
83
        }
84
    }
85
}
86

87
//--------------------------------------------------------------------------------
88
void ofxFBXBone::disableAnimation( bool bRecursively) {
89
    bUpdateFromAnimation = false;
90
    if( bRecursively ) {
91
        map< string, shared_ptr<ofxFBXBone> >::iterator it;
92
        for(it = childBones.begin(); it != childBones.end(); ++it ) {
93
            it->second->disableAnimation( bRecursively );
94
        }
95
    }
96
}
97

98
//--------------------------------------------------------------------------------
99
bool ofxFBXBone::isAnimationEnabled() {
100
    return bUpdateFromAnimation;
101
}
102

103
//--------------------------------------------------------------------------------
104
void ofxFBXBone::draw( float aLen, bool aBDrawAxes ) {
105
    if(aBDrawAxes) {
106
        transformGL(); {
107
            ofDrawAxis( aLen );
108
        } restoreTransformGL();
109
    }
110
    
111
    if( boneSrc != NULL && !boneSrc->isRoot() ) {
112
        glm::vec3 ppos = getParent()->getGlobalPosition();
113
        ofDrawLine( ppos, getGlobalPosition() );
114
    }
115
    
116
    map< string, shared_ptr<ofxFBXBone> >::iterator it;
117
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
118
        it->second->draw( aLen, aBDrawAxes );
119
    }
120
}
121

122
//---------------------------------------------------------------
123
int ofxFBXBone::getNumBones() {
124
    int ttotal = childBones.size();
125
    map<string, shared_ptr<ofxFBXBone> >::iterator it;
126
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
127
        ttotal += it->second->getNumBones();
128
    }
129
    return ttotal;
130
}
131

132
//--------------------------------------------------------------------------------
133
shared_ptr<ofxFBXBone> ofxFBXBone::getBone( string aName ) {
134
    shared_ptr<ofxFBXBone> tbone;// = NULL;
135
    findBoneRecursive( aName, tbone );
136
    if( tbone ) {
137
        return tbone;
138
    }
139
    return NULL;
140
}
141

142
//--------------------------------------------------------------------------------
143
void ofxFBXBone::findBoneRecursive( string aName, shared_ptr<ofxFBXBone>& returnBone ) {
144
    
145
    if( !returnBone ) {
146
        map< string, shared_ptr<ofxFBXBone> >::iterator it;
147
        for(it = childBones.begin(); it != childBones.end(); ++it ) {
148
            if( it->second->name == aName ) {
149
                returnBone = (it->second);
150
                break;
151
            }
152
            it->second->findBoneRecursive( aName, returnBone );
153
        }
154
    }
155
}
156

157
//--------------------------------------------------------------------------------
158
void ofxFBXBone::pointTo( glm::vec3 aTarget ) {
159
    glm::vec3 axis( 1, 0, 0 );
160
    pointTo( aTarget, axis );
161
}
162

163
//--------------------------------------------------------------------------------
164
void ofxFBXBone::pointTo( glm::vec3 aTarget, glm::vec3 aAxis ) {
165
    auto relPosition = (getGlobalPosition() - aTarget);
166
    glm::quat tq = glm::rotation( aAxis, glm::normalize(relPosition));
167
    setGlobalOrientation(tq);
168
}
169

170
//--------------------------------------------------------------
171
string ofxFBXBone::getAsString( int aLevel ) {
172
    
173
    stringstream oStr;// = "";
174
    for( int i = 0; i < aLevel; i++ ) {
175
        oStr << "  ";
176
    }
177
    if( aLevel > 0 ) {
178
        oStr <<" '";
179
    }
180
    if( childBones.size() ) {
181
        oStr << "+ ";
182
    } else {
183
        oStr << "- ";
184
    }
185
    string pname = "";
186
    if( hasParentNode() ) {
187
        pname = " parent: " + getParentNode()->getName();
188
    }
189
    
190
    if( mSrcNode ) {
191
        oStr << mSrcNode->getTypeAsString() << ": " << getName() << pname << " fbx type: " << mSrcNode->getFbxTypeString() << " anim: " << isAnimationEnabled();
192
    } else {
193
        oStr << ": " << getName() << pname << " anim: " << isAnimationEnabled();
194
    }
195
    if( childBones.size() > 0 ) {
196
        oStr << " child bones: " << childBones.size();
197
    }
198
//    if(usingKeyFrames()) {
199
//        oStr << " num keys: " << mKeyCollections.size();
200
//    }
201
    oStr << endl;
202
    
203
    //    oStr << getTypeAsString()+": " + getName() + " kids: " +ofToString( childBones.size(), 0) + pname + " anim: " + ofToString( isAnimationEnabled(), 0) + " num keys: " +ofToString(mKeyCollections.size(),0) + "\n";
204
    
205
    map<string, shared_ptr<ofxFBXBone> >::iterator it;
206
    for(it = childBones.begin(); it != childBones.end(); ++it ) {
207
        oStr << it->second->getAsString( aLevel + 1);
208
    }
209
    return oStr.str();
210
}
211

212

213

214

215

216

217

218

219

220

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

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

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

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