framework2

Форк
0
200 строк · 7.0 Кб
1
/****************************************************************************************
2

3
   Copyright (C) 2013 Autodesk, Inc.
4
   All rights reserved.
5

6
   Use of this software is subject to the terms of the Autodesk license agreement
7
   provided at the time of installation or download, or which otherwise accompanies
8
   this software in either electronic or hard copy form.
9
 
10
****************************************************************************************/
11

12
/////////////////////////////////////////////////////////////////////////
13
//
14
// This file contains the functions to get the global 
15
// position of a node for a given time in the current animation stack.
16
//
17
/////////////////////////////////////////////////////////////////////////
18

19
#include "GetPosition.h"
20

21
using namespace ofxFBXSource;
22
// Get the global position of the node for the current pose.
23
// If the specified node is not part of the pose or no pose is specified, get its
24
// global position at the current time.
25
FbxAMatrix ofxFBXSource::GetGlobalPosition(FbxNode* pNode, const FbxTime& pTime, FbxPose* pPose, FbxAMatrix* pParentGlobalPosition)
26
{
27
    FbxAMatrix lGlobalPosition;
28
    bool        lPositionFound = false;
29

30
    if (pPose)
31
    {
32
        int lNodeIndex = pPose->Find(pNode);
33

34
        if (lNodeIndex > -1)
35
        {
36
            // The bind pose is always a global matrix.
37
            // If we have a rest pose, we need to check if it is
38
            // stored in global or local space.
39
            if (pPose->IsBindPose() || !pPose->IsLocalMatrix(lNodeIndex))
40
            {
41
                lGlobalPosition = GetPoseMatrix(pPose, lNodeIndex);
42
            }
43
            else
44
            {
45
                // We have a local matrix, we need to convert it to
46
                // a global space matrix.
47
                FbxAMatrix lParentGlobalPosition;
48

49
                if (pParentGlobalPosition)
50
                {
51
                    lParentGlobalPosition = *pParentGlobalPosition;
52
                }
53
                else
54
                {
55
                    if (pNode->GetParent())
56
                    {
57
                        lParentGlobalPosition = GetGlobalPosition(pNode->GetParent(), pTime, pPose);
58
                    }
59
                }
60

61
                FbxAMatrix lLocalPosition = GetPoseMatrix(pPose, lNodeIndex);
62
                lGlobalPosition = lParentGlobalPosition * lLocalPosition;
63
            }
64

65
            lPositionFound = true;
66
        }
67
    }
68

69
    if (!lPositionFound)
70
    {
71
        // There is no pose entry for that node, get the current global position instead.
72

73
        // Ideally this would use parent global position and local position to compute the global position.
74
        // Unfortunately the equation 
75
        //    lGlobalPosition = pParentGlobalPosition * lLocalPosition
76
        // does not hold when inheritance type is other than "Parent" (RSrs).
77
        // To compute the parent rotation and scaling is tricky in the RrSs and Rrs cases.
78
        lGlobalPosition = pNode->EvaluateGlobalTransform(pTime);
79
    }
80

81
    return lGlobalPosition;
82
}
83

84
// Get the global position of the node for the current pose.
85
// If the specified node is not part of the pose or no pose is specified, get its
86
// global position at the current time.
87
FbxAMatrix ofxFBXSource::GetLocalPositionForNode(FbxNode* pNode, const FbxTime& pTime, FbxPose* pPose, FbxAMatrix* pParentGlobalPosition) {
88
    FbxAMatrix lLocalPosition;
89
    bool lPositionFound = false;
90
    
91
    if (pPose) {
92
        int lNodeIndex = pPose->Find(pNode);
93
        
94
        if (lNodeIndex > -1) {
95
            // The bind pose is always a global matrix.
96
            // If we have a rest pose, we need to check if it is
97
            // stored in global or local space.
98
            if (pPose->IsBindPose() || !pPose->IsLocalMatrix(lNodeIndex)) {
99
//                lGlobalPosition = GetPoseMatrix(pPose, lNodeIndex);
100
                FbxAMatrix lGlobalPosition = GetPoseMatrix( pPose, lNodeIndex );
101
                FbxAMatrix lParentGlobalPosition;
102
                
103
                if (pParentGlobalPosition) {
104
                    lParentGlobalPosition = *pParentGlobalPosition;
105
                } else {
106
                    if (pNode->GetParent()) {
107
                        lParentGlobalPosition = GetGlobalPosition(pNode->GetParent(), pTime, pPose);
108
                    }
109
                }
110
                
111
                lLocalPosition = lParentGlobalPosition.Inverse() * lGlobalPosition;
112
                
113
            } else {
114
                // We have a local matrix, we need to convert
115
                lLocalPosition = GetPoseMatrix(pPose, lNodeIndex);
116
                
117
//                lGlobalPosition = lParentGlobalPosition * lLocalPosition;
118
            }
119
            
120
            lPositionFound = true;
121
        }
122
    }
123
    
124
    if (!lPositionFound) {
125
        // There is no pose entry for that node, get the current global position instead.
126
        
127
        // Ideally this would use parent global position and local position to compute the global position.
128
        // Unfortunately the equation
129
        //    lGlobalPosition = pParentGlobalPosition * lLocalPosition
130
        // does not hold when inheritance type is other than "Parent" (RSrs).
131
        // To compute the parent rotation and scaling is tricky in the RrSs and Rrs cases.
132
//        lGlobalPosition = pNode->EvaluateGlobalTransform(pTime);
133
//        adfasdfasdfasdiufhasdkfjhasdlkfjhasdf
134
//        fbxNode->EvaluateLocalTransform( pTime, FbxNode::eSourcePivot, false, false );
135
        lLocalPosition = pNode->EvaluateLocalTransform( pTime, FbxNode::eSourcePivot, false, false );
136
    }
137
    
138
    return lLocalPosition;
139
}
140

141
// Get the matrix of the given pose
142
FbxAMatrix ofxFBXSource::GetPoseMatrix(FbxPose* pPose, int pNodeIndex)
143
{
144
    FbxAMatrix lPoseMatrix;
145
    FbxMatrix lMatrix = pPose->GetMatrix(pNodeIndex);
146

147
    memcpy((double*)lPoseMatrix, (double*)lMatrix, sizeof(lMatrix.mData));
148

149
    return lPoseMatrix;
150
}
151

152
// Get the geometry offset to a node. It is never inherited by the children.
153
FbxAMatrix ofxFBXSource::GetGeometry(FbxNode* pNode)
154
{
155
    const FbxVector4 lT = pNode->GetGeometricTranslation(FbxNode::eSourcePivot);
156
    const FbxVector4 lR = pNode->GetGeometricRotation(FbxNode::eSourcePivot);
157
    const FbxVector4 lS = pNode->GetGeometricScaling(FbxNode::eSourcePivot);
158

159
    return FbxAMatrix(lT, lR, lS);
160
}
161

162

163
// Scale all the elements of a matrix.
164
void ofxFBXSource::MatrixScale(FbxAMatrix& pMatrix, double pValue)
165
{
166
    int i,j;
167
    
168
    for (i = 0; i < 4; i++)
169
    {
170
        for (j = 0; j < 4; j++)
171
        {
172
            pMatrix[i][j] *= pValue;
173
        }
174
    }
175
}
176

177

178
// Add a value to all the elements in the diagonal of the matrix.
179
void ofxFBXSource::MatrixAddToDiagonal(FbxAMatrix& pMatrix, double pValue)
180
{
181
    pMatrix[0][0] += pValue;
182
    pMatrix[1][1] += pValue;
183
    pMatrix[2][2] += pValue;
184
    pMatrix[3][3] += pValue;
185
}
186

187

188
// Sum two matrices element by element.
189
void ofxFBXSource::MatrixAdd(FbxAMatrix& pDstMatrix, FbxAMatrix& pSrcMatrix)
190
{
191
    int i,j;
192
    
193
    for (i = 0; i < 4; i++)
194
    {
195
        for (j = 0; j < 4; j++)
196
        {
197
            pDstMatrix[i][j] += pSrcMatrix[i][j];
198
        }
199
    }
200
}
201

202

203

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

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

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

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