1 /*---------------------------------------------------------------------------*
2 Project: NintendoWare
3 File: gfx_TransformNode.h
4
5 Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain proprietary
8 information of Nintendo and/or its licensed developers and are protected by
9 national and international copyright laws. They may not be disclosed to third
10 parties or copied or duplicated in any form, in whole or in part, without the
11 prior written consent of Nintendo.
12
13 The content herein is highly confidential and should be handled accordingly.
14
15 $Revision: 31311 $
16 *---------------------------------------------------------------------------*/
17
18 #ifndef NW_GFX_TRANSFORMNODE_H_
19 #define NW_GFX_TRANSFORMNODE_H_
20
21 #include <nw/gfx/gfx_SceneNode.h>
22
23 #include <nw/gfx/gfx_CalculatedTransform.h>
24
25 namespace nw
26 {
27 namespace gfx
28 {
29
30 //---------------------------------------------------------------------------
31 //! @brief 変換情報を持つシーンノードを表すクラスです。
32 //---------------------------------------------------------------------------
33 class TransformNode : public SceneNode
34 {
35 private:
36 NW_DISALLOW_COPY_AND_ASSIGN(TransformNode);
37
38 public:
39 NW_UT_RUNTIME_TYPEINFO;
40
41 //! @brief マトリクス計算時に呼ばれるコールバック用シグナルの定義です。
42 //!
43 //! @sa PostCalculateWorldMatrixSignal
44 typedef ut::Signal2<void, TransformNode*, SceneContext*> CalculateMatrixSignal;
45
46 //! @brief マトリクス計算時に呼ばれるコールバック用スロットの定義です。
47 typedef CalculateMatrixSignal::SlotType CalculateMatrixSlot;
48
49 //! @brief 設定内容です。
50 struct Description : public SceneNode::Description
51 {
52 //! @brief コンストラクタです。
DescriptionDescription53 Description(){}
54 };
55
56 //----------------------------------------
57 //! @name 作成/破棄
58 //@{
59
60 //! @brief トランスフォームノードを動的に構築するためのクラスです。
61 //!
62 //! IsFixedSizeMemory の初期値は true です。false に変更すると、各種最大数の設定は無視されます。
63 class DynamicBuilder
64 {
65 public:
66 //! @brief コンストラクタです。
DynamicBuilder()67 DynamicBuilder() {}
68 //! @brief デストラクタです。
~DynamicBuilder()69 ~DynamicBuilder() {}
70
71 //! @brief 生成時以外にもメモリを確保するかどうかのフラグを設定します。
72 //!
73 //! true を指定すると、生成時のみ固定サイズのメモリ確保を行います。
74 //!
75 //! false を指定すると、生成時以外にも必要に応じて動的にメモリ確保が行われます。
IsFixedSizeMemory(bool isFixedSizeMemory)76 DynamicBuilder& IsFixedSizeMemory(bool isFixedSizeMemory)
77 {
78 m_Description.isFixedSizeMemory = isFixedSizeMemory;
79 return *this;
80 }
81
82 //! @brief 子の最大数を設定します。
MaxChildren(int maxChildren)83 DynamicBuilder& MaxChildren(int maxChildren)
84 {
85 m_Description.maxChildren = maxChildren;
86 return *this;
87 }
88
89 //! @brief 管理できるコールバックの最大数を設定します。
MaxCallbacks(int maxCallbacks)90 DynamicBuilder& MaxCallbacks(int maxCallbacks)
91 {
92 m_Description.maxCallbacks = maxCallbacks;
93 return *this;
94 }
95
96 //! @brief トランスフォームノードを生成します。
97 //!
98 //! @param[in] allocator アロケータです。
99 //!
100 //! @return 生成したトランスフォームノードを返します。
101 //!
102 TransformNode* Create(os::IAllocator* allocator);
103
104 //! @brief 生成時に必要なメモリサイズを取得します。
105 //!
106 //! メモリサイズは Builder の設定によって変化します。
107 //! すべての設定が終わった後にこの関数を呼び出してください。
108 //!
109 //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
110 size_t GetMemorySize(size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT) const
111 {
112 os::MemorySizeCalculator size(alignment);
113
114 size += sizeof(TransformNode);
115 GetMemorySizeForInitialize(&size, ResTransformNode(), m_Description);
116
117 return size.GetSizeWithPadding(alignment);
118 }
119
120 private:
121 TransformNode::Description m_Description;
122 };
123
124 //! @brief リソースからトランスフォームノードを生成します。
125 //!
126 //! @param[in] parent 親のノードです。
127 //! @param[in] resource リソースです。
128 //! @param[in] description 設定内容です。
129 //! @param[in] allocator アロケータです。
130 //!
131 //! @return 生成されたトランスフォームノードです。
132 //!
133 static TransformNode* Create(
134 SceneNode* parent,
135 ResSceneObject resource,
136 const TransformNode::Description& description,
137 os::IAllocator* allocator);
138
139 //! @brief 生成時に必要なメモリサイズを取得します。
140 //!
141 //! @param[in] resTransformNode リソースです。
142 //! @param[in] description 設定内容です。
143 //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。
144 static size_t GetMemorySize(
145 ResTransformNode resTransformNode,
146 Description description,
147 size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT)
148 {
149 os::MemorySizeCalculator size(alignment);
150
151 GetMemorySizeInternal(&size, resTransformNode, description);
152
153 return size.GetSizeWithPadding(alignment);
154 }
155
156 //! @details :private
GetMemorySizeInternal(os::MemorySizeCalculator * pSize,ResTransformNode resTransformNode,Description description)157 static void GetMemorySizeInternal(
158 os::MemorySizeCalculator* pSize,
159 ResTransformNode resTransformNode,
160 Description description)
161 {
162 os::MemorySizeCalculator& size = *pSize;
163
164 size += sizeof(TransformNode);
165 GetMemorySizeForInitialize(pSize, resTransformNode, description);
166 }
167
168 //@}
169
170 //----------------------------------------
171 //! @name リソース
172 //@{
173
174 //! @brief トランスフォームノードのリソースを取得します。
GetResTransformNode()175 ResTransformNode GetResTransformNode()
176 {
177 return ResStaticCast<ResTransformNode>( this->GetResSceneObject() );
178 }
179
180 //! @brief トランスフォームノードのリソースを取得します。
GetResTransformNode()181 const ResTransformNode GetResTransformNode() const
182 {
183 return ResStaticCast<ResTransformNode>( this->GetResSceneObject() );
184 }
185
186 //@}
187
188 //----------------------------------------
189 //! @name トランスフォーム
190 //@{
191
192 //! @brief 変換情報を取得します。
Transform()193 CalculatedTransform& Transform() { return m_Transform; }
194
195 //! @brief 変換情報を取得します。
Transform()196 const CalculatedTransform& Transform() const { return m_Transform; }
197
198 //! @brief ワールドマトリクスを取得します。
WorldMatrix()199 math::MTX34& WorldMatrix() { return m_WorldMatrix; }
200
201 //! @brief ワールドマトリクスを取得します。
WorldMatrix()202 const math::MTX34& WorldMatrix() const { return m_WorldMatrix; }
203
204 //! @brief WorldMatrix 計算後の詳細な変換情報を取得します。
205 //!
WorldTransform()206 CalculatedTransform& WorldTransform() { return m_CalculatedTransform; }
207
208 //! @brief WorldMatrix 計算後の詳細な変換情報を取得します。
209 //! TransformMatrix にはこのノードの Rotate と Translate に親ノードまでの Scale が掛かっています。
210 //! Scale は親ノードまでの累積のスケールとなります。
WorldTransform()211 const CalculatedTransform& WorldTransform() const { return m_CalculatedTransform; }
212
213 //! @brief ワールド行列の逆行列を取得します。2回目以降は、キャッシュを利用します。
214 const math::MTX34& InverseWorldMatrix() const;
215
216 //! @brief ワールド行列の逆行列のキャッシュを無効化します。(ワールド行列を更新する際に呼び出します。)
217 void InvalidateInverseWorldMatrix();
218
219 //! @brief 方向情報を更新します。
UpdateDirection()220 virtual void UpdateDirection() {}
221
222 //! @brief 変換情報に関する更新を行います。
223 //!
224 //! @param[in] worldMatrixUpdater ワールドマトリクス更新クラスです。
225 //! @param[in] sceneContext シーンコンテキストです。
226 //!
227 virtual void UpdateTransform(
228 WorldMatrixUpdater* worldMatrixUpdater,
229 SceneContext* sceneContext);
230
231 //! @brief リソースの持つトランスフォーム情報を基準にトランスフォームを設定します。
232 //!
233 //! リソースの持つトランスフォーム情報を表す変換行列に、引数で与えられた変換行列を乗じ、
234 //! その結果をノードに設定します。
235 //! transformMatrix はスケール成分を含むことができません。
236 //!
237 //! この関数は実行毎にリソースのトランスフォーム情報から行列を生成するので
238 //! パフォーマンスに注意してください。
239 //!
240 //! @param[in] transformMatrix 合成する変換行列です。
241 //! @param[in] scale スケールです。
242 void SetResourceBasedTransform(
243 const math::MTX34& transformMatrix,
244 const math::VEC3& scale = math::VEC3(1.0f, 1.0f, 1.0f)
245 )
246 {
247 math::Transform3& resTransform = GetResTransformNode().GetTransform();
248
249 math::MTX34 resultMatrix;
250 nw::math::MTX34RotXYZRad(&resultMatrix,
251 resTransform.rotate.x,
252 resTransform.rotate.y,
253 resTransform.rotate.z);
254 resultMatrix.f._03 = resTransform.translate.x;
255 resultMatrix.f._13 = resTransform.translate.y;
256 resultMatrix.f._23 = resTransform.translate.z;
257
258 math::MTX34Mult(&resultMatrix, &transformMatrix, &resultMatrix);
259
260 math::VEC3 resultScale;
261 math::VEC3Mult(&resultScale, &scale, &resTransform.scale);
262
263 m_Transform.SetTransformMatrix(resultMatrix);
264 m_Transform.SetScale(resultScale);
265 m_Transform.UpdateFlagsStrictly();
266 }
267
268 //! @brief リソースの持つトランスフォーム情報を基準にトランスフォームを設定します。
269 //!
270 //! リソースの持つトランスフォーム情報を表す変換行列に、引数で与えられた変換行列を乗じ、
271 //! その結果をノードに設定します。
272 //!
273 //! SetResourceBasedTransform() と異なり、行列にスケールを含むことができますが、
274 //! 内部でスケールの分離処理を行なうのでパフォーマンスに注意してください。
275 //!
276 //! @param[in] transformMatrix 合成する変換行列です。
SetResourceScaledTransform(const math::MTX34 & transformMatrix)277 void SetResourceScaledTransform(const math::MTX34& transformMatrix)
278 {
279 math::VEC3 scale;
280 math::MTX34 matrix;
281 nw::math::MTX34DecomposeToColumnScale(&scale, &transformMatrix);
282
283 NW_ASSERT(scale.x > 0 && scale.y > 0 && scale.z > 0);
284
285 matrix.f._00 = transformMatrix.f._00 / scale.x;
286 matrix.f._10 = transformMatrix.f._10 / scale.x;
287 matrix.f._20 = transformMatrix.f._20 / scale.x;
288 matrix.f._01 = transformMatrix.f._01 / scale.y;
289 matrix.f._11 = transformMatrix.f._11 / scale.y;
290 matrix.f._21 = transformMatrix.f._21 / scale.y;
291 matrix.f._02 = transformMatrix.f._02 / scale.z;
292 matrix.f._12 = transformMatrix.f._12 / scale.z;
293 matrix.f._22 = transformMatrix.f._22 / scale.z;
294 matrix.f._03 = transformMatrix.f._03;
295 matrix.f._13 = transformMatrix.f._13;
296 matrix.f._23 = transformMatrix.f._23;
297
298 SetResourceBasedTransform(matrix, scale);
299 }
300
301 //@}
302
303 //----------------------------------------
304 //! @name シーンツリー
305 //@{
306
307 //! @brief ビジターを受け付けます。
308 //!
309 //! @param[in] visitor ビジターです。
310 //!
311 virtual void Accept(ISceneVisitor* visitor);
312
313 //! @brief 親ノードをたどりワールドマトリクスを取得します。
TrackbackWorldMatrix()314 virtual const math::MTX34& TrackbackWorldMatrix() const
315 {
316 return this->WorldMatrix();
317 }
318
319 //! @brief 親ノードをたどりワールドトランスフォームを取得します。
TrackbackWorldTransform()320 virtual const CalculatedTransform& TrackbackWorldTransform() const
321 {
322 return this->WorldTransform();
323 }
324
325 //! @brief 親ノードをたどりローカルトランスフォームを取得します。
TrackbackLocalTransform()326 virtual const CalculatedTransform& TrackbackLocalTransform() const
327 {
328 return this->Transform();
329 }
330
331 //! @brief 親ノードから変換情報を継承します。
332 NW_INLINE virtual void InheritTraversalResults();
333
334 //@}
335
336 //----------------------------------------
337 //! @name コールバック
338 //@{
339
340 //! @brief ワールドマトリクス計算後のシグナルを取得します。
341 //!
342 //! @sa CalculateMatrixSignal
PostCalculateWorldMatrixSignal()343 CalculateMatrixSignal& PostCalculateWorldMatrixSignal()
344 {
345 return *m_PostCalculateWorldMatrixSignal;
346 }
347
348 //! @brief ワールドマトリクス計算後のシグナルを取得します。
349 //!
350 //! @sa CalculateMatrixSignal
PostCalculateWorldMatrixSignal()351 const CalculateMatrixSignal& PostCalculateWorldMatrixSignal() const
352 {
353 return *m_PostCalculateWorldMatrixSignal;
354 }
355
356 //@}
357
358 protected:
359 //----------------------------------------
360 //! @name コンストラクタ/デストラクタ
361 //@{
362
363 //! @brief コンストラクタです。
364 TransformNode(
365 os::IAllocator* allocator,
366 ResTransformNode resObj,
367 const TransformNode::Description& description);
368
369 //! @brief デストラクタです。
~TransformNode()370 virtual ~TransformNode()
371 {
372 SafeDestroy(m_PostCalculateWorldMatrixSignal);
373 }
374
375 virtual Result Initialize(os::IAllocator* allocator);
376
377 //@}
378
379 //! @brief 親ノードの変換情報を継承した方向を計算します。
CalcInheritingDiretion(math::VEC3 & inheritingDirection,const math::VEC3 & direction)380 void CalcInheritingDiretion(
381 math::VEC3& inheritingDirection,
382 const math::VEC3 &direction) const
383 {
384 if (this->GetParent() == NULL)
385 {
386 inheritingDirection = direction;
387 }
388 else
389 {
390 const math::MTX34& parentWorldMatrix = this->TrackbackWorldMatrix();
391 inheritingDirection.x =
392 parentWorldMatrix.f._00 * direction.x +
393 parentWorldMatrix.f._01 * direction.y +
394 parentWorldMatrix.f._02 * direction.z;
395
396 inheritingDirection.y =
397 parentWorldMatrix.f._10 * direction.x +
398 parentWorldMatrix.f._11 * direction.y +
399 parentWorldMatrix.f._12 * direction.z;
400
401 inheritingDirection.z =
402 parentWorldMatrix.f._20 * direction.x +
403 parentWorldMatrix.f._21 * direction.y +
404 parentWorldMatrix.f._22 * direction.z;
405 }
406 }
407
408
409 //! @brief Initialize() の実行に必要なメモリサイズを取得します。
410 //!
411 //! @details :private
GetMemorySizeForInitialize(os::MemorySizeCalculator * pSize,ResTransformNode resTransformNode,Description description)412 static void GetMemorySizeForInitialize(
413 os::MemorySizeCalculator* pSize,
414 ResTransformNode resTransformNode,
415 Description description)
416 {
417 NW_ASSERT(description.isFixedSizeMemory);
418
419 SceneNode::GetMemorySizeForInitialize(pSize, resTransformNode, description);
420
421 // TransformNode::CreateCallbacks
422 if (description.maxCallbacks == 0)
423 {
424 CalculateMatrixSignal::GetMemorySizeForInvalidateSignalInternal(pSize);
425 }
426 else
427 {
428 CalculateMatrixSignal::GetMemorySizeForFixedSizedSignalInternal(pSize, description.maxCallbacks);
429 }
430 }
431
432 private:
433 //! @brief コールバック関数を生成します。
434 Result CreateCallbacks(os::IAllocator* allocator);
435
436 CalculatedTransform m_Transform;
437 math::MTX34 m_WorldMatrix;
438 CalculatedTransform m_CalculatedTransform;
439
440 mutable math::MTX34 m_InverseWorldMatrix;
441 mutable bool m_IsInverseWorldMatrixValid;
442
443 CalculateMatrixSignal* m_PostCalculateWorldMatrixSignal;
444 bool m_IsBranchWorldMatrixCalculationEnabled;
445 Description m_Description;
446 };
447
448 //----------------------------------------
449 NW_INLINE void
InheritTraversalResults()450 TransformNode::InheritTraversalResults()
451 {
452 SceneNode::InheritTraversalResults();
453
454 SceneNode* parent = this->GetParent();
455 bit32 results = this->GetTraversalResults();
456
457 if (this->Transform().IsEnabledFlags(CalculatedTransform::FLAG_IS_DIRTY))
458 {
459 results = ut::EnableFlag(results, SceneNode::FLAG_IS_DIRTY);
460 }
461 else if (parent== NULL)
462 {
463 results = ut::DisableFlag(results, SceneNode::FLAG_IS_DIRTY);
464 }
465 else if (parent->IsEnabledResults(SceneNode::FLAG_IS_DIRTY))
466 {
467 results = ut::EnableFlag(results, SceneNode::FLAG_IS_DIRTY);
468 }
469 else
470 {
471 results = ut::DisableFlag(results, SceneNode::FLAG_IS_DIRTY);
472 }
473
474 this->SetTraversalResults(results);
475 }
476
477 } // namespace gfx
478 } // namespace nw
479
480 #endif // NW_GFX_TRANSFORMNODE_H_
481