framework2
200 строк · 7.0 Кб
1/****************************************************************************************
2
3Copyright (C) 2013 Autodesk, Inc.
4All rights reserved.
5
6Use of this software is subject to the terms of the Autodesk license agreement
7provided at the time of installation or download, or which otherwise accompanies
8this 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
21using 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.
25FbxAMatrix ofxFBXSource::GetGlobalPosition(FbxNode* pNode, const FbxTime& pTime, FbxPose* pPose, FbxAMatrix* pParentGlobalPosition)
26{
27FbxAMatrix lGlobalPosition;
28bool lPositionFound = false;
29
30if (pPose)
31{
32int lNodeIndex = pPose->Find(pNode);
33
34if (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.
39if (pPose->IsBindPose() || !pPose->IsLocalMatrix(lNodeIndex))
40{
41lGlobalPosition = GetPoseMatrix(pPose, lNodeIndex);
42}
43else
44{
45// We have a local matrix, we need to convert it to
46// a global space matrix.
47FbxAMatrix lParentGlobalPosition;
48
49if (pParentGlobalPosition)
50{
51lParentGlobalPosition = *pParentGlobalPosition;
52}
53else
54{
55if (pNode->GetParent())
56{
57lParentGlobalPosition = GetGlobalPosition(pNode->GetParent(), pTime, pPose);
58}
59}
60
61FbxAMatrix lLocalPosition = GetPoseMatrix(pPose, lNodeIndex);
62lGlobalPosition = lParentGlobalPosition * lLocalPosition;
63}
64
65lPositionFound = true;
66}
67}
68
69if (!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.
78lGlobalPosition = pNode->EvaluateGlobalTransform(pTime);
79}
80
81return 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.
87FbxAMatrix ofxFBXSource::GetLocalPositionForNode(FbxNode* pNode, const FbxTime& pTime, FbxPose* pPose, FbxAMatrix* pParentGlobalPosition) {
88FbxAMatrix lLocalPosition;
89bool lPositionFound = false;
90
91if (pPose) {
92int lNodeIndex = pPose->Find(pNode);
93
94if (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.
98if (pPose->IsBindPose() || !pPose->IsLocalMatrix(lNodeIndex)) {
99// lGlobalPosition = GetPoseMatrix(pPose, lNodeIndex);
100FbxAMatrix lGlobalPosition = GetPoseMatrix( pPose, lNodeIndex );
101FbxAMatrix lParentGlobalPosition;
102
103if (pParentGlobalPosition) {
104lParentGlobalPosition = *pParentGlobalPosition;
105} else {
106if (pNode->GetParent()) {
107lParentGlobalPosition = GetGlobalPosition(pNode->GetParent(), pTime, pPose);
108}
109}
110
111lLocalPosition = lParentGlobalPosition.Inverse() * lGlobalPosition;
112
113} else {
114// We have a local matrix, we need to convert
115lLocalPosition = GetPoseMatrix(pPose, lNodeIndex);
116
117// lGlobalPosition = lParentGlobalPosition * lLocalPosition;
118}
119
120lPositionFound = true;
121}
122}
123
124if (!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 );
135lLocalPosition = pNode->EvaluateLocalTransform( pTime, FbxNode::eSourcePivot, false, false );
136}
137
138return lLocalPosition;
139}
140
141// Get the matrix of the given pose
142FbxAMatrix ofxFBXSource::GetPoseMatrix(FbxPose* pPose, int pNodeIndex)
143{
144FbxAMatrix lPoseMatrix;
145FbxMatrix lMatrix = pPose->GetMatrix(pNodeIndex);
146
147memcpy((double*)lPoseMatrix, (double*)lMatrix, sizeof(lMatrix.mData));
148
149return lPoseMatrix;
150}
151
152// Get the geometry offset to a node. It is never inherited by the children.
153FbxAMatrix ofxFBXSource::GetGeometry(FbxNode* pNode)
154{
155const FbxVector4 lT = pNode->GetGeometricTranslation(FbxNode::eSourcePivot);
156const FbxVector4 lR = pNode->GetGeometricRotation(FbxNode::eSourcePivot);
157const FbxVector4 lS = pNode->GetGeometricScaling(FbxNode::eSourcePivot);
158
159return FbxAMatrix(lT, lR, lS);
160}
161
162
163// Scale all the elements of a matrix.
164void ofxFBXSource::MatrixScale(FbxAMatrix& pMatrix, double pValue)
165{
166int i,j;
167
168for (i = 0; i < 4; i++)
169{
170for (j = 0; j < 4; j++)
171{
172pMatrix[i][j] *= pValue;
173}
174}
175}
176
177
178// Add a value to all the elements in the diagonal of the matrix.
179void ofxFBXSource::MatrixAddToDiagonal(FbxAMatrix& pMatrix, double pValue)
180{
181pMatrix[0][0] += pValue;
182pMatrix[1][1] += pValue;
183pMatrix[2][2] += pValue;
184pMatrix[3][3] += pValue;
185}
186
187
188// Sum two matrices element by element.
189void ofxFBXSource::MatrixAdd(FbxAMatrix& pDstMatrix, FbxAMatrix& pSrcMatrix)
190{
191int i,j;
192
193for (i = 0; i < 4; i++)
194{
195for (j = 0; j < 4; j++)
196{
197pDstMatrix[i][j] += pSrcMatrix[i][j];
198}
199}
200}
201
202
203