1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4     This file is part of COLLADASaxFrameworkLoader.
5 
6     Licensed under the MIT Open Source License,
7     for details please see LICENSE file or the website
8     http://www.opensource.org/licenses/mit-license.php
9 */
10 
11 #include "COLLADASaxFWLStableHeaders.h"
12 #include "COLLADASaxFWLTransformationLoader.h"
13 
14 #include "COLLADAFWTranslate.h"
15 #include "COLLADAFWRotate.h"
16 #include "COLLADAFWScale.h"
17 #include "COLLADAFWSkew.h"
18 #include "COLLADAFWLookat.h"
19 #include "COLLADAFWMatrix.h"
20 
21 namespace COLLADASaxFWL
22 {
23 
24     //------------------------------
TransformationLoader()25 	TransformationLoader::TransformationLoader()
26 		: mCurrentTransformation(0)
27 		, mTransformationNumbersReceived(0)
28 	{
29 	}
30 
31     //------------------------------
~TransformationLoader()32 	TransformationLoader::~TransformationLoader()
33 	{
34 	}
35 
36 	//------------------------------
dataTranslate(const float * data,size_t length)37 	bool TransformationLoader::dataTranslate( const float* data, size_t length )
38 	{
39 		COLLADAFW::Translate* translate = 0;
40 
41 		if (mCurrentTransformation->getTransformationType() == COLLADAFW::Transformation::TRANSLATE)
42 		{
43 			translate = (COLLADAFW::Translate*)(mCurrentTransformation);
44 		}
45 
46 		COLLADABU_ASSERT(translate);
47 		COLLADABU::Math::Vector3& translationVector = translate->getTranslation();
48 		for ( size_t i = 0; i < length; ++i )
49 		{
50 			translationVector[mTransformationNumbersReceived++] = data[i];
51 		}
52 		return true;
53 	}
54 
55 	//------------------------------
dataRotate(const float * data,size_t length)56 	bool TransformationLoader::dataRotate( const float* data, size_t length )
57 	{
58 		COLLADAFW::Rotate* rotate = 0;
59 
60 		if (mCurrentTransformation->getTransformationType() == COLLADAFW::Transformation::ROTATE)
61 		{
62 			rotate = (COLLADAFW::Rotate*)(mCurrentTransformation);
63 		}
64 
65 		COLLADABU_ASSERT(rotate);
66 		COLLADABU::Math::Vector3& axisVector = rotate->getRotationAxis();
67 		for ( size_t i = 0; i < length; ++i )
68 		{
69 			if ( mTransformationNumbersReceived < 3)
70 			{
71 				axisVector[mTransformationNumbersReceived++] = data[i];
72 			}
73 			else
74 			{
75 				rotate->setRotationAngle(data[i]);
76 			}
77 		}
78 		return true;
79 	}
80 
81 	//------------------------------
dataMatrix(const float * data,size_t length)82 	bool TransformationLoader::dataMatrix( const float* data, size_t length )
83 	{
84 		COLLADAFW::Matrix* matrix = 0;
85 
86 		if (mCurrentTransformation->getTransformationType() == COLLADAFW::Transformation::MATRIX)
87 		{
88 			matrix = (COLLADAFW::Matrix*)(mCurrentTransformation);
89 		}
90 
91 		COLLADABU_ASSERT(matrix);
92 		COLLADABU::Math::Matrix4& transformationMatrix = matrix->getMatrix();
93 		for ( size_t i = 0; i < length; ++i )
94 		{
95 			size_t row = mTransformationNumbersReceived / 4;
96 			size_t column = mTransformationNumbersReceived % 4;
97 			transformationMatrix.setElement(row, column, data[i]);
98 			mTransformationNumbersReceived++;
99 		}
100 		return true;
101 	}
102 
103 	//------------------------------
dataScale(const float * data,size_t length)104 	bool TransformationLoader::dataScale( const float* data, size_t length )
105 	{
106 		COLLADAFW::Scale* scale = 0;
107 
108 		if (mCurrentTransformation->getTransformationType() == COLLADAFW::Transformation::SCALE)
109 		{
110 			scale = (COLLADAFW::Scale*)(mCurrentTransformation);
111 		}
112 
113 		COLLADABU_ASSERT(scale);
114 		COLLADABU::Math::Vector3& scaleVector = scale->getScale();
115 		for ( size_t i = 0; i < length; ++i )
116 		{
117 			scaleVector[mTransformationNumbersReceived++] = data[i];
118 		}
119 		return true;
120 	}
121 
122 	//------------------------------
dataSkew(const float * data,size_t length)123 	bool TransformationLoader::dataSkew( const float* data, size_t length )
124 	{
125 		COLLADAFW::Skew* skew = 0;
126 
127 		if (mCurrentTransformation->getTransformationType() == COLLADAFW::Transformation::SKEW)
128 		{
129 			skew = (COLLADAFW::Skew*)(mCurrentTransformation);
130 		}
131 
132 		COLLADABU_ASSERT(skew);
133 
134 		COLLADABU::Math::Vector3& rotateAxis = skew->getRotateAxis ();
135 		COLLADABU::Math::Vector3& aroundAxis = skew->getTranslateAxis ();
136 
137 		size_t i = 0;
138 		if ( i < length && mTransformationNumbersReceived == 0 )
139 		{
140 			float angle = skew->getAngle ();
141 			skew->setAngle ( angle + data [mTransformationNumbersReceived++] );
142 			++i;
143 		}
144 		if ( i < length && mTransformationNumbersReceived > 0 && mTransformationNumbersReceived < 4 )
145 		{
146 			for ( size_t j=0; j<3 && i<length; ++j, ++i )
147 			{
148 				rotateAxis[j] = data[i];
149 				mTransformationNumbersReceived++;
150 			}
151 		}
152 		if (  i < length && mTransformationNumbersReceived >= 4 )
153 		{
154 			for ( size_t j=0; j<3 && i<length; ++j, ++i )
155 			{
156 				aroundAxis[j] = data[i];
157 				mTransformationNumbersReceived++;
158 			}
159 		}
160 		return true;
161 	}
162 
163 	//------------------------------
dataLookat(const float * data,size_t length)164 	bool TransformationLoader::dataLookat( const float* data, size_t length )
165 	{
166 		COLLADAFW::Lookat* lookat = 0;
167 
168 		if (mCurrentTransformation->getTransformationType() == COLLADAFW::Transformation::LOOKAT)
169 		{
170 			lookat = (COLLADAFW::Lookat*)(mCurrentTransformation);
171 		}
172 
173 		COLLADABU_ASSERT(lookat);
174 
175 		COLLADABU::Math::Vector3& eyePosition = lookat->getEyePosition ();
176 		COLLADABU::Math::Vector3& interestPointPosition = lookat->getInterestPointPosition ();
177 		COLLADABU::Math::Vector3& upAxisDirection = lookat->getUpAxisDirection ();
178 
179 		size_t i = 0;
180 		if ( i < length && mTransformationNumbersReceived < 3 )
181 		{
182 			for ( size_t j=mTransformationNumbersReceived; j<3 && i<length; ++j, ++i )
183 			{
184 				eyePosition[j] = data[i];
185 				mTransformationNumbersReceived++;
186 			}
187 		}
188 		if ( i < length && mTransformationNumbersReceived >= 3 && mTransformationNumbersReceived < 6 )
189 		{
190 			for ( size_t j=mTransformationNumbersReceived-3; j<3 && i<length; ++j, ++i )
191 			{
192 				interestPointPosition[j] = data[i];
193 				mTransformationNumbersReceived++;
194 			}
195 		}
196 		if (  i < length && mTransformationNumbersReceived >= 6 )
197 		{
198 			for ( size_t j=mTransformationNumbersReceived-6; j<3 && i<length; ++j, ++i )
199 			{
200 				upAxisDirection[j] = data[i];
201 				mTransformationNumbersReceived++;
202 			}
203 		}
204 		return true;
205 	}
206 
207 	//------------------------------
endTransformation()208 	void TransformationLoader::endTransformation()
209 	{
210 		mTransformationNumbersReceived = 0;
211 		mCurrentTransformation = 0;
212 	}
213 
214 } // namespace COLLADASaxFWL
215