/*---------------------------------------------------------------------------* Project: NintendoWare File: gfx_AimTargetViewUpdater.cpp 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: 28760 $ *---------------------------------------------------------------------------*/ #include "precompiled.h" #include #include namespace nw { namespace gfx { NW_UT_RUNTIME_TYPEINFO_DEFINITION(AimTargetViewUpdater, CameraViewUpdater); //---------------------------------------- AimTargetViewUpdater* AimTargetViewUpdater::Create(os::IAllocator* allocator) { NW_NULL_ASSERT(allocator); void* updaterMemory = allocator->Alloc(sizeof(AimTargetViewUpdater)); NW_NULL_ASSERT(updaterMemory); void* dataMemory = AllocateAndFill(allocator, 0); ResAimTargetViewUpdaterData* buffer = new(dataMemory) ResAimTargetViewUpdaterData(); buffer->typeInfo = ResAimTargetViewUpdater::TYPE_INFO; buffer->m_TargetPosition = VIEW_TARGET_POSITION; buffer->m_Twist = VIEW_TWIST; buffer->m_Flags = 0x0; ResAimTargetViewUpdater resUpdater = ResAimTargetViewUpdater(buffer); return new(updaterMemory) AimTargetViewUpdater(allocator, true, resUpdater); } //---------------------------------------- AimTargetViewUpdater* AimTargetViewUpdater::Create( os::IAllocator* allocator, ResAimTargetViewUpdater resUpdater) { NW_NULL_ASSERT(allocator); void* updaterMemory = allocator->Alloc(sizeof(AimTargetViewUpdater)); NW_NULL_ASSERT(updaterMemory); return new(updaterMemory) AimTargetViewUpdater(allocator, false, resUpdater); } //---------------------------------------- AimTargetViewUpdater::AimTargetViewUpdater( os::IAllocator* allocator, bool isDynamic, ResAimTargetViewUpdater resUpdater ) : CameraViewUpdater(allocator, isDynamic), m_Resource(resUpdater) { } //---------------------------------------- AimTargetViewUpdater::~AimTargetViewUpdater() { if (this->IsDynamic() && this->m_Resource.IsValid()) { this->GetAllocator().Free(m_Resource.ptr()); } } //---------------------------------------- void AimTargetViewUpdater::Update( math::MTX34* viewMatrix, const math::MTX34& worldMatrix, const math::VEC3& cameraPosition) { NW_ASSERT(m_Resource.IsValid()); u32 flags = m_Resource.GetFlags(); if (ut::CheckFlagOr( flags, ResAimTargetViewUpdaterData::FLAG_INHERITING_TARGET_TRANSLATE | ResAimTargetViewUpdaterData::FLAG_INHERITING_TARGET_ROTATE)) { // カメラ座標系のZ方向 math::VEC3 lookReverse( cameraPosition.x - this->m_Resource.GetTargetPosition().x, cameraPosition.y - this->m_Resource.GetTargetPosition().y, cameraPosition.z - this->m_Resource.GetTargetPosition().z); if ((lookReverse.x == 0.0f) && (lookReverse.z == 0.0f)) { // こちらはcameraPosition == targetPositionでも問題ない math::MTX34LookAtRad(viewMatrix, &cameraPosition, this->m_Resource.GetTwist(), &this->m_Resource.GetTargetPosition()); } else { math::MTX33 rotateMatrix; math::MTX34ToMTX33(&rotateMatrix, &worldMatrix); // カメラ座標系のX方向 math::VEC3 r(lookReverse.z, 0.0f, -lookReverse.x); math::VEC3Normalize(&lookReverse, &lookReverse); math::VEC3Normalize(&r, &r); // カメラ座標系のY方向 math::VEC3 u; math::VEC3Cross(&u, &lookReverse, &r); f32 st, ct; math::SinCosRad(&st, &ct, this->m_Resource.GetTwist()); math::VEC3 up; up.x = ct * u.x - st * r.x; up.y = ct * u.y; up.z = ct * u.z - st * r.z; math::VEC3 targetPosition(this->m_Resource.GetTargetPosition()); if (ut::CheckFlag(flags, ResAimTargetViewUpdaterData::FLAG_INHERITING_TARGET_ROTATE)) { math::VEC3Transform(&targetPosition, &rotateMatrix, &targetPosition); math::VEC3Transform(&up, &rotateMatrix, &up); } if (ut::CheckFlag(flags, ResAimTargetViewUpdaterData::FLAG_INHERITING_TARGET_TRANSLATE)) { math::VEC3Add(&targetPosition, &targetPosition, &cameraPosition); } NW_ASSERT(cameraPosition != targetPosition); math::MTX34LookAt( viewMatrix, &cameraPosition, &up, &targetPosition); } } else { // こちらはcameraPosition == targetPositionでも問題ない math::MTX34LookAtRad(viewMatrix, &cameraPosition, this->m_Resource.GetTwist(), &this->m_Resource.GetTargetPosition()); } } } // namespace gfx } // namespace nw