framework2

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

8
#include "ofxFBXSrcNode.h"
9
using namespace ofxFBXSource;
10

11
//----------------------------------------
12
Node::Node() {
13
    //    parent          = NULL;
14
    //    globalParent    = NULL;
15
    origScale = glm::vec3(1,1,1);
16
}
17

18
//----------------------------------------
19
Node::~Node() {
20
    if( getParent() != NULL ) {
21
        clearParent();
22
    }
23
}
24

25
//----------------------------------------
26
// this is painful :( -- probably should make an array of names
27
string Node::getFbxTypeStringFromNode( FbxNode* aNode ) {
28
    if( aNode == NULL ) { return "Node is NULL";}
29
    FbxNodeAttribute* lNodeAttribute = aNode->GetNodeAttribute();
30
    if( lNodeAttribute ) {
31
        
32
        //        -eUnknown,
33
        //        -eNull,
34
        //        -eMarker,
35
        //        -eSkeleton,
36
        //        -eMesh,
37
        //        -eNurbs,
38
        //        -ePatch,
39
        //        -eCamera,
40
        //        -eCameraStereo,
41
        //        -eCameraSwitcher,
42
        //        -eLight,
43
        //        -eOpticalReference,
44
        //        -eOpticalMarker,
45
        //        -eNurbsCurve,
46
        //        -eTrimNurbsSurface,
47
        //        -eBoundary,
48
        //        -eNurbsSurface,
49
        //        -eShape,
50
        //        -eLODGroup,
51
        //        -eSubDiv,
52
        //        -eCachedEffect,
53
        //        -eLine
54
        
55
        FbxNodeAttribute::EType et = lNodeAttribute->GetAttributeType();
56
        if( et == FbxNodeAttribute::eUnknown ) {
57
            return "eUnknown";
58
        }
59
        if( et == FbxNodeAttribute::eNull ) {
60
            return "eNull";
61
        }
62
        if( et == FbxNodeAttribute::eMarker ) {
63
            return "eMarker";
64
        }
65
        if( et == FbxNodeAttribute::eSkeleton ) {
66
            return "eSkeleton";
67
        }
68
        if (et == FbxNodeAttribute::eMesh ) {
69
            return "eMesh";
70
        }
71
        if( et == FbxNodeAttribute::eNurbs ) {
72
            return "eNurbs";
73
        }
74
        if(et == FbxNodeAttribute::ePatch) {
75
            return "ePatch";
76
        }
77
        if(et == FbxNodeAttribute::eCamera) {
78
            return "eCamera";
79
        }
80
        if(et == FbxNodeAttribute::eCameraStereo) {
81
            return "eCameraStereo";
82
        }
83
        if(et == FbxNodeAttribute::eCameraSwitcher) {
84
            return "eCameraSwitcher";
85
        }
86
        if(et == FbxNodeAttribute::eLight) {
87
            return "eLight";
88
        }
89
        if( et == FbxNodeAttribute::eOpticalReference ) {
90
            return "eOpticalReference";
91
        }
92
        if( et == FbxNodeAttribute::eOpticalMarker ) {
93
            return "eOpticalMarker";
94
        }
95
        if( et == FbxNodeAttribute::eNurbsCurve ) {
96
            return "eNurbsCurve";
97
        }
98
        if( et == FbxNodeAttribute::eTrimNurbsSurface ) {
99
            return "eTrimNurbsSurface";
100
        }
101
        if( et == FbxNodeAttribute::eBoundary ) {
102
            return "eBoundary";
103
        }
104
        if(et == FbxNodeAttribute::eNurbsSurface) {
105
            return "eNurbsSurface";
106
        }
107
        if( et == FbxNodeAttribute::eShape ) {
108
            return "eShape";
109
        }
110
        if( et == FbxNodeAttribute::eLODGroup ) {
111
            return "eLODGroup";
112
        }
113
        if( et == FbxNodeAttribute::eSubDiv ) {
114
            return "eSubDiv";
115
        }
116
        if( et == FbxNodeAttribute::eCachedEffect ) {
117
            return "eCachedEffect";
118
        }
119
        if( et == FbxNodeAttribute::eLine ) {
120
            return "eLine";
121
        }
122
        return ("default - "+ofToString((int)et));
123
    }
124
    return "default - no attribute";
125
}
126

127
//----------------------------------------
128
string Node::getFbxTypeString() {
129
    return Node::getFbxTypeStringFromNode( mFbxNode );
130
}
131

132
//----------------------------------------
133
string Node::getNodeTypeAsString( NodeType atype ) {
134
    if( atype == OFX_FBX_NULL ) {
135
        return "Null";
136
    } else if( atype == OFX_FBX_MESH ) {
137
        return "Mesh";
138
    } else if( atype == OFX_FBX_SKELETON ) {
139
        return "Skeleton";
140
    } else if( atype == OFX_FBX_BONE ) {
141
        return "Bone";
142
    } else if( atype == OFX_FBX_NURBS_CURVE ) {
143
        return "Nurbs Curve";
144
    }
145
    //OFX_FBX_UNKNOWN
146
    return "Unknown";
147
}
148

149
//----------------------------------------
150
string Node::getTypeAsString() {
151
    return Node::getNodeTypeAsString(getType());
152
}
153

154
//----------------------------------------
155
void Node::setup( FbxNode *pNode ) {
156
    setName( pNode->GetNameOnly() );
157
    mFbxNode = pNode;
158
}
159

160
//----------------------------------------
161
string Node::getName() {
162
    return name;
163
}
164

165
//----------------------------------------
166
FbxString Node::getFbxName() {
167
    return FbxString( name.c_str() );
168
}
169

170
//----------------------------------------
171
void Node::setName( FbxString aName ) {
172
    name = aName;
173
}
174

175
//--------------------------------------------------------------
176
void Node::setUseKeyFrames( bool ab ) {
177
    bUseKeyFrames = ab;
178
}
179

180
//--------------------------------------------------------------
181
bool Node::usingKeyFrames() {
182
    return bUseKeyFrames;
183
}
184

185
//--------------------------------------------------------------
186
void Node::clearKeyFrames() {
187
    mKeyCollections.clear();
188
}
189

190
//--------------------------------------------------------------
191
void Node::cacheStartTransforms() {
192
    // cache the orientations for use later //
193
    origGlobalRotation  = getGlobalOrientation();
194
    origLocalRotation   = getOrientationQuat();
195
    origGlobalTransform = getGlobalTransformMatrix();
196
    origLocalTransform  = getLocalTransformMatrix();
197
    origPos             = getPosition();
198
    origScale           = getScale();
199
}
200

201
//----------------------------------------
202
void Node::update( FbxTime& pTime, FbxPose* pPose ) {
203
    if( mFbxNode ) {
204
        if( !mFbxNode->GetParent() ) {
205
            FbxAMatrix lGlobalPosition = GetGlobalPosition(mFbxNode, pTime, NULL );
206
            setLocalTransformMatrix(lGlobalPosition);
207
        } else {
208
            FbxAMatrix lLocalPosition = GetLocalPositionForNode(mFbxNode, pTime, NULL );
209
            setLocalTransformMatrix(lLocalPosition);
210
        }
211
    }
212
}
213

214
//----------------------------------------
215
void Node::update( int aAnimIndex, signed long aMillis ) {
216
    if( aAnimIndex < 0 ) return;
217
    if( mKeyCollections.size() == 0 ) return;
218
    if( aAnimIndex >= mKeyCollections.size() ) return;
219
    
220
    glm::vec3 tpos = getKeyTranslation( aAnimIndex, aMillis );
221
    glm::vec3 cpos = getPosition();
222
    if( cpos.x != tpos.x || cpos.y != tpos.y || cpos.z != tpos.z ) {
223
        setPosition( tpos );
224
    }
225
    
226
    glm::vec3 tscale = getKeyScale( aAnimIndex, aMillis );
227
    glm::vec3 cscale = getScale();
228
    if( cscale.x != tscale.x || cscale.y != tscale.y || cscale.z != tscale.z ) {
229
        setScale( tscale );
230
    }
231
    
232
    ofQuaternion tquat = getKeyRotation( aAnimIndex, aMillis );
233
    ofQuaternion cquat = getOrientationQuat();
234
    if( cquat.x() != tquat.x() || cquat.y() != tquat.y() || cquat.z() != tquat.z() || cquat.w() != tquat.w() ) {
235
        setOrientation( tquat );
236
    }
237
}
238

239
//----------------------------------------
240
void Node::update( int aAnimIndex1, signed long aAnim1Millis, int aAnimIndex2, signed long aAnim2Millis, float aMixPct ) {
241
    if( mKeyCollections.size() == 0 ) return;
242
    if( aAnimIndex1 < 0 ) return;
243
    if( aAnimIndex1 >= mKeyCollections.size() ) return;
244
    if( aAnimIndex2 < 0 ) return;
245
    if( aAnimIndex2 >= mKeyCollections.size() ) return;
246
    
247
    aMixPct = ofClamp(aMixPct, 0.0, 1.0);
248
    float invpct = 1.0 - aMixPct;
249
    
250
    glm::vec3 tpos1 = getKeyTranslation( aAnimIndex1, aAnim1Millis );
251
    glm::vec3 tpos2 = getKeyTranslation( aAnimIndex2, aAnim2Millis);
252
    
253
    glm::vec3 tscale1 = getKeyScale( aAnimIndex1, aAnim1Millis );
254
    glm::vec3 tscale2 = getKeyScale( aAnimIndex2, aAnim2Millis );
255
    
256
    ofQuaternion tquat1 = getKeyRotation( aAnimIndex1, aAnim1Millis );
257
    ofQuaternion tquat2 = getKeyRotation( aAnimIndex2, aAnim2Millis );
258
    
259
    
260
    if( aMixPct <= 0.0f ) {
261
        setPosition( tpos1 );
262
        setScale(tscale1);
263
        setOrientation(tquat1);
264
    } else if( aMixPct >= 1.0 ) {
265
        setPosition( tpos2 );
266
        setScale(tscale2);
267
        setOrientation(tquat2);
268
    } else {
269
        setPosition( tpos1 * invpct + tpos2 * aMixPct );
270
        setScale( tscale1 * invpct + tscale2 * aMixPct );
271
        tquat1.slerp( aMixPct, tquat1, tquat2 );
272
        setOrientation( tquat1 );
273
    }
274
}
275

276
//----------------------------------------
277
glm::vec3 Node::getKeyTranslation( int aAnimIndex, signed long aMillis ) {
278
    if( aAnimIndex < 0 ) return origPos;
279
    if( mKeyCollections.size() == 0 ) return origPos;
280
    if( aAnimIndex >= mKeyCollections.size() ) return origPos;
281
    
282
    AnimKeyCollection& tcollection = mKeyCollections[aAnimIndex];
283
    mAnimIndex = aAnimIndex;
284
    glm::vec3 tpos = origPos;
285
    if(tcollection.posKeysX.size() > 0) tpos.x = getKeyValue( tcollection.posKeysX, aMillis );
286
    if(tcollection.posKeysY.size() > 0) tpos.y = getKeyValue( tcollection.posKeysY, aMillis );
287
    if(tcollection.posKeysZ.size() > 0) tpos.z = getKeyValue( tcollection.posKeysZ, aMillis );
288
    return tpos;
289
}
290

291
//----------------------------------------
292
ofQuaternion Node::getKeyRotation( int aAnimIndex, signed long aMillis ) {
293
    if( aAnimIndex < 0 ) return origLocalRotation;
294
    if( mKeyCollections.size() == 0 ) return origLocalRotation;
295
    if( aAnimIndex >= mKeyCollections.size() ) return origLocalRotation;
296
    
297
    AnimKeyCollection& tcollection = mKeyCollections[aAnimIndex];
298
    mAnimIndex = aAnimIndex;
299
    
300
    return getKeyRotation( tcollection.rotKeys, aMillis );
301
}
302

303
//----------------------------------------
304
glm::vec3 Node::getKeyScale( int aAnimIndex, signed long aMillis ) {
305
    if( aAnimIndex < 0 ) return origScale;
306
    if( mKeyCollections.size() == 0 ) return origScale;
307
    if( aAnimIndex >= mKeyCollections.size() ) return origScale;
308
    
309
    AnimKeyCollection& tcollection = mKeyCollections[aAnimIndex];
310
    mAnimIndex = aAnimIndex;
311
    
312
    glm::vec3 tscale = origScale;
313
    if(tcollection.scaleKeysX.size() > 0) tscale.x = getKeyValue( tcollection.scaleKeysX, aMillis );
314
    if(tcollection.scaleKeysY.size() > 0) tscale.y = getKeyValue( tcollection.scaleKeysY, aMillis );
315
    if(tcollection.scaleKeysZ.size() > 0) tscale.z = getKeyValue( tcollection.scaleKeysZ, aMillis );
316
    
317
    return tscale;
318
}
319

320
//----------------------------------------
321
// from Arturo Castro's ofxFBX ///
322
float Node::getKeyValue( vector<AnimKey<float> >& keys, signed long ms ) {
323
    for(int i=0;i<keys.size();i++){
324
        if(keys[i].millis==ms){
325
            return keys[i].value;
326
        }else if(keys[i].millis>ms){
327
            if(i>0){
328
                signed long delta = ms - keys[i-1].millis;
329
                float pct = double(delta) / double(keys[i].millis - keys[i-1].millis);
330
                return ofLerp(keys[i-1].value,keys[i].value,pct);
331
            }else{
332
                return keys[0].value;
333
                //u_long delta = ms;
334
                //float pct = double(delta) / double(keys[i].millis);
335
                //return ofLerp(0.0f,keys[i].value,pct);
336
            }
337
        }
338
    }
339
    if(keys.empty()){
340
        return 0.0f;
341
    }else{
342
        return keys.back().value;
343
    }
344
    return 0.0f;
345
}
346

347
//----------------------------------------
348
// from Arturo Castro's ofxFBX ///
349
ofQuaternion Node::getKeyRotation(vector<AnimKey<ofQuaternion> >& keys, signed long ms) {
350
    for(int i=0;i<keys.size();i++){
351
        if(keys[i].millis==ms){
352
            return keys[i].value;
353
        }else if(keys[i].millis>ms){
354
            if(i>0){
355
                signed long delta = ms - keys[i-1].millis;
356
                float pct = double(delta) / double(keys[i].millis - keys[i-1].millis);
357
                ofQuaternion q;
358
                q.slerp(pct,keys[i-1].value,keys[i].value);
359
                return q;
360
            }else{
361
                signed long delta = ms;
362
                float pct = double(delta) / double(keys[i].millis);
363
                ofQuaternion q = keys[i].value;
364
                q.slerp(pct,origLocalRotation,keys[i].value);
365
                return q;
366
            }
367
        }
368
    }
369
    if(keys.empty()){
370
        return origLocalRotation;
371
    }else{
372
        return keys.back().value;
373
    }
374
    return origLocalRotation;
375
}
376

377
//----------------------------------------
378
AnimKeyCollection& Node::getKeyCollection( int aAnimIndex ) {
379
    if( mKeyCollections.count(aAnimIndex) < 1 ) {
380
        AnimKeyCollection temp;
381
        mKeyCollections[ aAnimIndex ] = temp;
382
    }
383
    return mKeyCollections[aAnimIndex];
384
}
385

386
//----------------------------------------
387
//void Node::setGlobalTransformMatrix( FbxAMatrix ainput ) {
388
//    glm::vec3 tpos, tscale;
389
//    glm::quat trot;
390
//    
391
//    fbxToGlmComponents( ainput, tpos, trot, tscale );
392
//    
393
//    setScale( tscale );
394
//    setGlobalPosition( tpos );
395
//    setGlobalOrientation( trot);
396
//}
397

398
//----------------------------------------
399
void Node::setLocalTransformMatrix( FbxAMatrix ainput ) {
400
    glm::vec3 tpos, tscale;
401
    glm::quat trot;
402
    
403
    fbxToGlmComponents( ainput, tpos, trot, tscale );
404
    
405
    setScale( tscale );
406
    setPosition( tpos );
407
    setOrientation( trot);
408
}
409

410
//----------------------------------------
411
void Node::clearChildren() {
412
    mKids.clear();
413
}
414

415
//----------------------------------------
416
void Node::addChild( shared_ptr<Node> akiddo ) {
417
    mKids.push_back( akiddo );
418
}
419

420
//----------------------------------------
421
int Node::getNumChildren() {
422
    return mKids.size();
423
}
424

425
//----------------------------------------
426
vector< shared_ptr<Node> >& Node::getChildren() {
427
    return mKids;
428
}
429

430
//--------------------------------------------------------------
431
string Node::getAsString( int aLevel ) {
432
    stringstream oStr;// = "";
433
    for( int i = 0; i < aLevel; i++ ) {
434
        oStr << "  ";
435
    }
436
    if( aLevel > 0 ) {
437
        oStr <<" '";
438
    }
439
    if( mKids.size() ) {
440
        oStr << "+ ";
441
    } else {
442
        oStr << "- ";
443
    }
444
    //    string pname = "";
445
    //    if( getParent() != NULL ) {
446
    //        pname = " parent: " + parentBoneName;
447
    //    }
448
    oStr << getTypeAsString() << ": " << getName() << " fbx type: " << getFbxTypeString();
449
    if( getNumChildren() > 0 ) {
450
        oStr << " kids: " << mKids.size();
451
    }
452
    if(usingKeyFrames()) {
453
        oStr << " num keys collections: " << mKeyCollections.size();
454
    }
455
    oStr << endl;// "\n";
456
    
457
    for( auto& kid : mKids ) {
458
        oStr << kid->getAsString( aLevel + 1);
459
    }
460
    
461
    return oStr.str();
462
}
463

464
//#pragma mark - Search
465
//--------------------------------------------------------------
466
//shared_ptr<Node> Node::getNodeforName( shared_ptr<Node>& aBSelf, string aPath, bool bStrict ) {
467
//    vector< string > tsearches;
468
//    if( ofIsStringInString( aPath, ":" ) ) {
469
//        tsearches = ofSplitString( aPath, ":" );
470
//    } else {
471
//        tsearches.push_back( aPath );
472
//        if(aBSelf) {
473
//            if(bStrict) {
474
//                if( aBSelf->getName() == aPath ) {
475
//                    //                    cout << "FOUND SELF" << endl;
476
//                    return aBSelf;
477
//                }
478
//            } else {
479
//                if( ofIsStringInString( aBSelf->getName(), aPath )) {
480
//                    //                    cout << "FOUND SELF" << endl;
481
//                    return aBSelf;
482
//                }
483
//            }
484
//        }
485
//    }
486
//
487
//    shared_ptr<Node> temp;// = aBSelf;
488
//    _getNodeForNameRecursive( tsearches, temp, mKids, bStrict );
489
//    return temp;
490
//}
491
//
492
////--------------------------------------------------------------
493
//shared_ptr<Node> Node::getKidforName( string aPath, bool bStrict ) {
494
//
495
//    vector< string > tsearches;
496
//    if( ofIsStringInString( aPath, ":" ) ) {
497
//        tsearches = ofSplitString( aPath, ":" );
498
//    } else {
499
//        tsearches.push_back( aPath );
500
//    }
501
//
502
//    shared_ptr<Node> temp;
503
//    _getNodeForNameRecursive( tsearches, temp, mKids, bStrict );
504
//    return temp;
505
//}
506
//
507
////--------------------------------------------------------------
508
//void Node::_getNodeForNameRecursive( vector<string>& aNamesToFind, shared_ptr<Node>& aTarget, vector< shared_ptr<Node> >& aElements, bool bStrict ) {
509
//
510
//    for( int i = 0; i < aElements.size(); i++ ) {
511
//        bool bFound = false;
512
//        if(bStrict) {
513
//            if( aElements[i]->getName() == aNamesToFind[0] ) {
514
//                bFound = true;
515
//            }
516
//        } else {
517
//            if( ofIsStringInString( aElements[i]->getName(), aNamesToFind[0] )) {
518
//                //                cout << "Found--- " << aNamesToFind[0] << " num names: " << aNamesToFind.size() << endl;
519
//                bFound = true;
520
//            }
521
//        }
522
//
523
//        if( bFound == true ) {
524
//            aNamesToFind.erase( aNamesToFind.begin() );
525
//            if( aNamesToFind.size() == 0 ) {//}|| aElements[i]->getNumChildren() < 1 ) {
526
//                bool bgood = false;
527
//                if( aElements[i] ) {
528
//                    bgood = true;
529
//                }
530
//                //                cout << "going to return one of the elements " << aNamesToFind.size() << " good: " << bgood << " " << endl;
531
//                aTarget = aElements[i];
532
//                break;
533
//            } else {
534
//                if( aElements[i]->getNumChildren() > 0 ) {
535
////                    shared_ptr<Node> tgroup = dynamic_pointer_cast< ofxSvgGroup >( aElements[i] );
536
//                    _getNodeForNameRecursive( aNamesToFind, aTarget, aElements[i]->getChildren(), bStrict );
537
//                    break;
538
//                }
539
//            }
540
//        }
541
//    }
542
//
543
//
544
//}
545

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

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

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

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