/*---------------------------------------------------------------------------* Project: NintendoWare File: gfx_TransformNode.h Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo and/or its licensed developers and are protected by national and international copyright laws. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. The content herein is highly confidential and should be handled accordingly. $Revision: 31311 $ *---------------------------------------------------------------------------*/ #ifndef NW_GFX_TRANSFORMNODE_H_ #define NW_GFX_TRANSFORMNODE_H_ #include #include namespace nw { namespace gfx { //--------------------------------------------------------------------------- //! @brief 変換情報を持つシーンノードを表すクラスです。 //--------------------------------------------------------------------------- class TransformNode : public SceneNode { private: NW_DISALLOW_COPY_AND_ASSIGN(TransformNode); public: NW_UT_RUNTIME_TYPEINFO; //! @brief マトリクス計算時に呼ばれるコールバック用シグナルの定義です。 //! //! @sa PostCalculateWorldMatrixSignal typedef ut::Signal2 CalculateMatrixSignal; //! @brief マトリクス計算時に呼ばれるコールバック用スロットの定義です。 typedef CalculateMatrixSignal::SlotType CalculateMatrixSlot; //! @brief 設定内容です。 struct Description : public SceneNode::Description { //! @brief コンストラクタです。 Description(){} }; //---------------------------------------- //! @name 作成/破棄 //@{ //! @brief トランスフォームノードを動的に構築するためのクラスです。 //! //! IsFixedSizeMemory の初期値は true です。false に変更すると、各種最大数の設定は無視されます。 class DynamicBuilder { public: //! @brief コンストラクタです。 DynamicBuilder() {} //! @brief デストラクタです。 ~DynamicBuilder() {} //! @brief 生成時以外にもメモリを確保するかどうかのフラグを設定します。 //! //! true を指定すると、生成時のみ固定サイズのメモリ確保を行います。 //! //! false を指定すると、生成時以外にも必要に応じて動的にメモリ確保が行われます。 DynamicBuilder& IsFixedSizeMemory(bool isFixedSizeMemory) { m_Description.isFixedSizeMemory = isFixedSizeMemory; return *this; } //! @brief 子の最大数を設定します。 DynamicBuilder& MaxChildren(int maxChildren) { m_Description.maxChildren = maxChildren; return *this; } //! @brief 管理できるコールバックの最大数を設定します。 DynamicBuilder& MaxCallbacks(int maxCallbacks) { m_Description.maxCallbacks = maxCallbacks; return *this; } //! @brief トランスフォームノードを生成します。 //! //! @param[in] allocator アロケータです。 //! //! @return 生成したトランスフォームノードを返します。 //! TransformNode* Create(os::IAllocator* allocator); //! @brief 生成時に必要なメモリサイズを取得します。 //! //! メモリサイズは Builder の設定によって変化します。 //! すべての設定が終わった後にこの関数を呼び出してください。 //! //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。 size_t GetMemorySize(size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT) const { os::MemorySizeCalculator size(alignment); size += sizeof(TransformNode); GetMemorySizeForInitialize(&size, ResTransformNode(), m_Description); return size.GetSizeWithPadding(alignment); } private: TransformNode::Description m_Description; }; //! @brief リソースからトランスフォームノードを生成します。 //! //! @param[in] parent 親のノードです。 //! @param[in] resource リソースです。 //! @param[in] description 設定内容です。 //! @param[in] allocator アロケータです。 //! //! @return 生成されたトランスフォームノードです。 //! static TransformNode* Create( SceneNode* parent, ResSceneObject resource, const TransformNode::Description& description, os::IAllocator* allocator); //! @brief 生成時に必要なメモリサイズを取得します。 //! //! @param[in] resTransformNode リソースです。 //! @param[in] description 設定内容です。 //! @param[in] alignment 計算に用いるアライメントです。2 のべき乗である必要があります。 static size_t GetMemorySize( ResTransformNode resTransformNode, Description description, size_t alignment = os::IAllocator::DEFAULT_ALIGNMENT) { os::MemorySizeCalculator size(alignment); GetMemorySizeInternal(&size, resTransformNode, description); return size.GetSizeWithPadding(alignment); } //! @details :private static void GetMemorySizeInternal( os::MemorySizeCalculator* pSize, ResTransformNode resTransformNode, Description description) { os::MemorySizeCalculator& size = *pSize; size += sizeof(TransformNode); GetMemorySizeForInitialize(pSize, resTransformNode, description); } //@} //---------------------------------------- //! @name リソース //@{ //! @brief トランスフォームノードのリソースを取得します。 ResTransformNode GetResTransformNode() { return ResStaticCast( this->GetResSceneObject() ); } //! @brief トランスフォームノードのリソースを取得します。 const ResTransformNode GetResTransformNode() const { return ResStaticCast( this->GetResSceneObject() ); } //@} //---------------------------------------- //! @name トランスフォーム //@{ //! @brief 変換情報を取得します。 CalculatedTransform& Transform() { return m_Transform; } //! @brief 変換情報を取得します。 const CalculatedTransform& Transform() const { return m_Transform; } //! @brief ワールドマトリクスを取得します。 math::MTX34& WorldMatrix() { return m_WorldMatrix; } //! @brief ワールドマトリクスを取得します。 const math::MTX34& WorldMatrix() const { return m_WorldMatrix; } //! @brief WorldMatrix 計算後の詳細な変換情報を取得します。 //! CalculatedTransform& WorldTransform() { return m_CalculatedTransform; } //! @brief WorldMatrix 計算後の詳細な変換情報を取得します。 //! TransformMatrix にはこのノードの Rotate と Translate に親ノードまでの Scale が掛かっています。 //! Scale は親ノードまでの累積のスケールとなります。 const CalculatedTransform& WorldTransform() const { return m_CalculatedTransform; } //! @brief ワールド行列の逆行列を取得します。2回目以降は、キャッシュを利用します。 const math::MTX34& InverseWorldMatrix() const; //! @brief ワールド行列の逆行列のキャッシュを無効化します。(ワールド行列を更新する際に呼び出します。) void InvalidateInverseWorldMatrix(); //! @brief 方向情報を更新します。 virtual void UpdateDirection() {} //! @brief 変換情報に関する更新を行います。 //! //! @param[in] worldMatrixUpdater ワールドマトリクス更新クラスです。 //! @param[in] sceneContext シーンコンテキストです。 //! virtual void UpdateTransform( WorldMatrixUpdater* worldMatrixUpdater, SceneContext* sceneContext); //! @brief リソースの持つトランスフォーム情報を基準にトランスフォームを設定します。 //! //! リソースの持つトランスフォーム情報を表す変換行列に、引数で与えられた変換行列を乗じ、 //! その結果をノードに設定します。 //! transformMatrix はスケール成分を含むことができません。 //! //! この関数は実行毎にリソースのトランスフォーム情報から行列を生成するので //! パフォーマンスに注意してください。 //! //! @param[in] transformMatrix 合成する変換行列です。 //! @param[in] scale スケールです。 void SetResourceBasedTransform( const math::MTX34& transformMatrix, const math::VEC3& scale = math::VEC3(1.0f, 1.0f, 1.0f) ) { math::Transform3& resTransform = GetResTransformNode().GetTransform(); math::MTX34 resultMatrix; nw::math::MTX34RotXYZRad(&resultMatrix, resTransform.rotate.x, resTransform.rotate.y, resTransform.rotate.z); resultMatrix.f._03 = resTransform.translate.x; resultMatrix.f._13 = resTransform.translate.y; resultMatrix.f._23 = resTransform.translate.z; math::MTX34Mult(&resultMatrix, &transformMatrix, &resultMatrix); math::VEC3 resultScale; math::VEC3Mult(&resultScale, &scale, &resTransform.scale); m_Transform.SetTransformMatrix(resultMatrix); m_Transform.SetScale(resultScale); m_Transform.UpdateFlagsStrictly(); } //! @brief リソースの持つトランスフォーム情報を基準にトランスフォームを設定します。 //! //! リソースの持つトランスフォーム情報を表す変換行列に、引数で与えられた変換行列を乗じ、 //! その結果をノードに設定します。 //! //! SetResourceBasedTransform() と異なり、行列にスケールを含むことができますが、 //! 内部でスケールの分離処理を行なうのでパフォーマンスに注意してください。 //! //! @param[in] transformMatrix 合成する変換行列です。 void SetResourceScaledTransform(const math::MTX34& transformMatrix) { math::VEC3 scale; math::MTX34 matrix; nw::math::MTX34DecomposeToColumnScale(&scale, &transformMatrix); NW_ASSERT(scale.x > 0 && scale.y > 0 && scale.z > 0); matrix.f._00 = transformMatrix.f._00 / scale.x; matrix.f._10 = transformMatrix.f._10 / scale.x; matrix.f._20 = transformMatrix.f._20 / scale.x; matrix.f._01 = transformMatrix.f._01 / scale.y; matrix.f._11 = transformMatrix.f._11 / scale.y; matrix.f._21 = transformMatrix.f._21 / scale.y; matrix.f._02 = transformMatrix.f._02 / scale.z; matrix.f._12 = transformMatrix.f._12 / scale.z; matrix.f._22 = transformMatrix.f._22 / scale.z; matrix.f._03 = transformMatrix.f._03; matrix.f._13 = transformMatrix.f._13; matrix.f._23 = transformMatrix.f._23; SetResourceBasedTransform(matrix, scale); } //@} //---------------------------------------- //! @name シーンツリー //@{ //! @brief ビジターを受け付けます。 //! //! @param[in] visitor ビジターです。 //! virtual void Accept(ISceneVisitor* visitor); //! @brief 親ノードをたどりワールドマトリクスを取得します。 virtual const math::MTX34& TrackbackWorldMatrix() const { return this->WorldMatrix(); } //! @brief 親ノードをたどりワールドトランスフォームを取得します。 virtual const CalculatedTransform& TrackbackWorldTransform() const { return this->WorldTransform(); } //! @brief 親ノードをたどりローカルトランスフォームを取得します。 virtual const CalculatedTransform& TrackbackLocalTransform() const { return this->Transform(); } //! @brief 親ノードから変換情報を継承します。 NW_INLINE virtual void InheritTraversalResults(); //@} //---------------------------------------- //! @name コールバック //@{ //! @brief ワールドマトリクス計算後のシグナルを取得します。 //! //! @sa CalculateMatrixSignal CalculateMatrixSignal& PostCalculateWorldMatrixSignal() { return *m_PostCalculateWorldMatrixSignal; } //! @brief ワールドマトリクス計算後のシグナルを取得します。 //! //! @sa CalculateMatrixSignal const CalculateMatrixSignal& PostCalculateWorldMatrixSignal() const { return *m_PostCalculateWorldMatrixSignal; } //@} protected: //---------------------------------------- //! @name コンストラクタ/デストラクタ //@{ //! @brief コンストラクタです。 TransformNode( os::IAllocator* allocator, ResTransformNode resObj, const TransformNode::Description& description); //! @brief デストラクタです。 virtual ~TransformNode() { SafeDestroy(m_PostCalculateWorldMatrixSignal); } virtual Result Initialize(os::IAllocator* allocator); //@} //! @brief 親ノードの変換情報を継承した方向を計算します。 void CalcInheritingDiretion( math::VEC3& inheritingDirection, const math::VEC3 &direction) const { if (this->GetParent() == NULL) { inheritingDirection = direction; } else { const math::MTX34& parentWorldMatrix = this->TrackbackWorldMatrix(); inheritingDirection.x = parentWorldMatrix.f._00 * direction.x + parentWorldMatrix.f._01 * direction.y + parentWorldMatrix.f._02 * direction.z; inheritingDirection.y = parentWorldMatrix.f._10 * direction.x + parentWorldMatrix.f._11 * direction.y + parentWorldMatrix.f._12 * direction.z; inheritingDirection.z = parentWorldMatrix.f._20 * direction.x + parentWorldMatrix.f._21 * direction.y + parentWorldMatrix.f._22 * direction.z; } } //! @brief Initialize() の実行に必要なメモリサイズを取得します。 //! //! @details :private static void GetMemorySizeForInitialize( os::MemorySizeCalculator* pSize, ResTransformNode resTransformNode, Description description) { NW_ASSERT(description.isFixedSizeMemory); SceneNode::GetMemorySizeForInitialize(pSize, resTransformNode, description); // TransformNode::CreateCallbacks if (description.maxCallbacks == 0) { CalculateMatrixSignal::GetMemorySizeForInvalidateSignalInternal(pSize); } else { CalculateMatrixSignal::GetMemorySizeForFixedSizedSignalInternal(pSize, description.maxCallbacks); } } private: //! @brief コールバック関数を生成します。 Result CreateCallbacks(os::IAllocator* allocator); CalculatedTransform m_Transform; math::MTX34 m_WorldMatrix; CalculatedTransform m_CalculatedTransform; mutable math::MTX34 m_InverseWorldMatrix; mutable bool m_IsInverseWorldMatrixValid; CalculateMatrixSignal* m_PostCalculateWorldMatrixSignal; bool m_IsBranchWorldMatrixCalculationEnabled; Description m_Description; }; //---------------------------------------- NW_INLINE void TransformNode::InheritTraversalResults() { SceneNode::InheritTraversalResults(); SceneNode* parent = this->GetParent(); bit32 results = this->GetTraversalResults(); if (this->Transform().IsEnabledFlags(CalculatedTransform::FLAG_IS_DIRTY)) { results = ut::EnableFlag(results, SceneNode::FLAG_IS_DIRTY); } else if (parent== NULL) { results = ut::DisableFlag(results, SceneNode::FLAG_IS_DIRTY); } else if (parent->IsEnabledResults(SceneNode::FLAG_IS_DIRTY)) { results = ut::EnableFlag(results, SceneNode::FLAG_IS_DIRTY); } else { results = ut::DisableFlag(results, SceneNode::FLAG_IS_DIRTY); } this->SetTraversalResults(results); } } // namespace gfx } // namespace nw #endif // NW_GFX_TRANSFORMNODE_H_