/*---------------------------------------------------------------------------* Project: NintendoWare File: gfx_WorldMatrixUpdater.h Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. 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. $Revision: 24186 $ *---------------------------------------------------------------------------*/ #ifndef NW_GFX_WORLD_MATRIX_UPDATER_H_ #define NW_GFX_WORLD_MATRIX_UPDATER_H_ #include #include #include namespace nw { namespace os { class IAllocator; } // os namespace gfx { class CalculatedTransform; //--------------------------------------------------------------------------- //! @brief ワールドマトリクスを更新するためのクラスです。 //--------------------------------------------------------------------------- class WorldMatrixUpdater : public GfxObject { private: NW_DISALLOW_COPY_AND_ASSIGN(WorldMatrixUpdater); public: //--------------------------------------------------------------------------- //! @brief ボーンのスケーリング方式の定義です。 //--------------------------------------------------------------------------- enum ScalingRule { SCALING_RULE_STANDARD, //!< 標準的なスケーリング方式です。 SCALING_RULE_MAYA, //!< Mayaのスケーリング方式です。 SCALING_RULE_SOFTIMAGE //!< Softimageのスケーリング方式です。 }; public: //! ワールドマトリクスアップデータを構築するクラスです。 class Builder { public: //! コンストラクタです。 Builder() {} //! @brief ワールドマトリクスアップデータを生成します。 //! //! @param[in] allocator アロケータです。 //! //! @return 生成されたワールドマトリクスアップデータです。 //! WorldMatrixUpdater* Create(os::IAllocator* allocator); }; //! @brief Maya方式でワールドマトリクスを更新します。 //! //! @param[out] worldMatrix 更新するワールドマトリクスです。 //! @param[out] worldTransform 更新する計算済みワールドトランスフォームです。 //! @param[in] localTransform 計算済みローカルトランスフォームです。 //! @param[in] parentWorldTransform 親の計算済みワールドトランスフォームです。 //! @param[in] parentLocalTransform 親の計算済みローカルトランスフォームです。 //! @param[in] isSSC MAYAのSSCによる計算を行うかどうかのフラグです。 //! NW_INLINE void UpdateMaya( math::MTX34* worldMatrix, CalculatedTransform* worldTransform, const CalculatedTransform& localTransform, const CalculatedTransform& parentWorldTransform, const CalculatedTransform& parentLocalTransform, bool isSSC) const; //! @brief XSI方式でワールドマトリクスを更新します。 //! //! @param[out] worldMatrix 更新するワールドマトリクスです。 //! @param[out] worldTransform 更新する計算済みワールドトランスフォームです。 //! @param[in] localTransform 計算済みローカルトランスフォームです。 //! @param[in] parentWorldTransform 親の計算済みワールドトランスフォームです。 //! @param[in] parentLocalTransform 親の計算済みローカルトランスフォームです。 //! NW_INLINE void UpdateXsi( math::MTX34* worldMatrix, CalculatedTransform* worldTransform, const CalculatedTransform& localTransform, const CalculatedTransform& parentWorldTransform, const CalculatedTransform& parentLocalTransform) const; //! @brief 通常の方式でワールドマトリクスを更新します。 //! //! @param[out] worldMatrix 更新するワールドマトリクスです。 //! @param[out] worldTransform 更新する計算済みワールドトランスフォームです。 //! @param[in] localTransform 計算済みローカルトランスフォームです。 //! @param[in] parentWorldTransform 親の計算済みワールドトランスフォームです。 //! @param[in] parentLocalTransform 親の計算済みローカルトランスフォームです。 //! NW_INLINE void UpdateBasic( math::MTX34* worldMatrix, CalculatedTransform* worldTransform, const CalculatedTransform& localTransform, const CalculatedTransform& parentWorldTransform, const CalculatedTransform& parentLocalTransform) const; private: //! コンストラクタです。 WorldMatrixUpdater( os::IAllocator* allocator); //! デストラクタです。 virtual ~WorldMatrixUpdater(); void CalculateWorldXsi( math::MTX34* transformMatrix, math::VEC3* scale, const CalculatedTransform& localTransform, const CalculatedTransform& parentWorldTransform, const CalculatedTransform& parentLocalTransform) const; void CalculateWorldMayaSsc( math::MTX34* transformMatrix, math::VEC3* scale, const CalculatedTransform& localTransform, const CalculatedTransform& parentWorldTransform, const CalculatedTransform& parentLocalTransform) const; void CalculateWorldBasic( math::MTX34* transformMatrix, math::VEC3* scale, const CalculatedTransform& localTransform, const CalculatedTransform& parentWorldTransform, const CalculatedTransform& parentLocalTransform) const; template void MultScale(TMatrix* dstMatrix, const UMatrix* srcMatrix, const math::VEC3& scale) const { dstMatrix->f._00 = srcMatrix->f._00 * scale.x; dstMatrix->f._10 = srcMatrix->f._10 * scale.x; dstMatrix->f._20 = srcMatrix->f._20 * scale.x; dstMatrix->f._01 = srcMatrix->f._01 * scale.y; dstMatrix->f._11 = srcMatrix->f._11 * scale.y; dstMatrix->f._21 = srcMatrix->f._21 * scale.y; dstMatrix->f._02 = srcMatrix->f._02 * scale.z; dstMatrix->f._12 = srcMatrix->f._12 * scale.z; dstMatrix->f._22 = srcMatrix->f._22 * scale.z; } template void ScaleMatrix(TMatrix* dstMatrix, const math::VEC3& scale) const { dstMatrix->f._00 *= scale.x; dstMatrix->f._10 *= scale.x; dstMatrix->f._20 *= scale.x; dstMatrix->f._01 *= scale.y; dstMatrix->f._11 *= scale.y; dstMatrix->f._21 *= scale.y; dstMatrix->f._02 *= scale.z; dstMatrix->f._12 *= scale.z; dstMatrix->f._22 *= scale.z; } void CopyTranslate(math::MTX34* dstMatrix, const math::MTX34& srcMatrix) const { dstMatrix->f._03 = srcMatrix.f._03; dstMatrix->f._13 = srcMatrix.f._13; dstMatrix->f._23 = srcMatrix.f._23; } void AddTranslate(math::MTX34* dstMatrix, const math::VEC3& translate) const { dstMatrix->f._03 += translate.x; dstMatrix->f._13 += translate.y; dstMatrix->f._23 += translate.z; } void CompensateScale(math::VEC3& scale) const { // Restrict scale to over 1 / 1000000 const float MinimumScale = 0.001f * 0.001f; float mag = (scale.x * scale.x + scale.y * scale.y + scale.z * scale.z); if (mag < MinimumScale) { scale.x = (scale.x < 0.0f) ? -MinimumScale : MinimumScale; scale.y = (scale.y < 0.0f) ? -MinimumScale : MinimumScale; scale.z = (scale.z < 0.0f) ? -MinimumScale : MinimumScale; } } }; //---------------------------------------- NW_INLINE void WorldMatrixUpdater::UpdateMaya( math::MTX34* worldMatrix, CalculatedTransform* worldTransform, const CalculatedTransform& localTransform, const CalculatedTransform& parentWorldTransform, const CalculatedTransform& parentLocalTransform, bool isSSC) const { NW_NULL_ASSERT(worldMatrix); NW_NULL_ASSERT(worldTransform); math::VEC3 scale; // Update WorldTransform if (isSSC) { CalculateWorldMayaSsc( &worldTransform->m_TransformMatrix, &scale, localTransform, parentWorldTransform, parentLocalTransform ); } else { CalculateWorldBasic( &worldTransform->m_TransformMatrix, &scale, localTransform, parentWorldTransform, parentLocalTransform ); } this->CompensateScale(scale); worldTransform->m_Scale = scale; // Update WorldMatrix math::MTX34MultScale(worldMatrix, worldTransform->m_TransformMatrix, localTransform.m_Scale); #if defined(NW_GFX_WORLD_MATRIX_FLAG_UPDATE_ENABLED) worldTransform->UpdateFlags(); #else worldTransform->ResetTransformFlags(); worldTransform->UpdateScaleFlags(); #endif } //---------------------------------------- NW_INLINE void WorldMatrixUpdater::UpdateBasic( math::MTX34* worldMatrix, CalculatedTransform* worldTransform, const CalculatedTransform& localTransform, const CalculatedTransform& parentWorldTransform, const CalculatedTransform& parentLocalTransform) const { NW_NULL_ASSERT(worldMatrix); NW_NULL_ASSERT(worldTransform); math::VEC3 scale; // Update WorldTransform CalculateWorldBasic( &worldTransform->m_TransformMatrix, &scale, localTransform, parentWorldTransform, parentLocalTransform ); this->CompensateScale(scale); worldTransform->m_Scale = scale; // Update WorldMatrix math::MTX34MultScale(worldMatrix, worldTransform->m_TransformMatrix, localTransform.m_Scale); #if defined(NW_GFX_WORLD_MATRIX_FLAG_UPDATE_ENABLED) worldTransform->UpdateFlags(); #else worldTransform->ResetTransformFlags(); worldTransform->UpdateScaleFlags(); #endif } //---------------------------------------- NW_INLINE void WorldMatrixUpdater::UpdateXsi( math::MTX34* worldMatrix, CalculatedTransform* worldTransform, const CalculatedTransform& localTransform, const CalculatedTransform& parentWorldTransform, const CalculatedTransform& parentLocalTransform) const { NW_NULL_ASSERT(worldMatrix); NW_NULL_ASSERT(worldTransform); math::VEC3 scale; // Update WorldTransform CalculateWorldXsi( &worldTransform->m_TransformMatrix, &scale, localTransform, parentWorldTransform, parentLocalTransform ); this->CompensateScale(scale); worldTransform->m_Scale = scale; // Update WorldMatrix math::MTX34MultScale(worldMatrix, worldTransform->m_TransformMatrix, worldTransform->m_Scale); #if defined(NW_GFX_WORLD_MATRIX_FLAG_UPDATE_ENABLED) worldTransform->UpdateFlags(); #else worldTransform->ResetTransformFlags(); worldTransform->UpdateScaleFlags(); #endif } } // gfx } // nw #endif // NW_GFX_WORLD_MATRIX_UPDATER_H_