/*---------------------------------------------------------------------------* Project: NintendoWare File: gfx_LookAtTargetViewUpdater.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(LookAtTargetViewUpdater, CameraViewUpdater); //---------------------------------------- LookAtTargetViewUpdater* LookAtTargetViewUpdater::Create(os::IAllocator* allocator) { NW_NULL_ASSERT(allocator); void* updaterMemory = allocator->Alloc(sizeof(LookAtTargetViewUpdater)); NW_NULL_ASSERT(updaterMemory); void* dataMemory = AllocateAndFill(allocator, 0); ResLookAtTargetViewUpdaterData* buffer = new(dataMemory) ResLookAtTargetViewUpdaterData(); buffer->typeInfo = ResLookAtTargetViewUpdater::TYPE_INFO; buffer->m_TargetPosition = VIEW_TARGET_POSITION; buffer->m_UpwardVector = VIEW_UPWARD_VECTOR; buffer->m_Flags = 0x0; ResLookAtTargetViewUpdater resUpdater = ResLookAtTargetViewUpdater(buffer); return new(updaterMemory) LookAtTargetViewUpdater(allocator, true, resUpdater); } //---------------------------------------- LookAtTargetViewUpdater* LookAtTargetViewUpdater::Create(os::IAllocator* allocator, ResLookAtTargetViewUpdater resUpdater) { NW_NULL_ASSERT(allocator); void* updaterMemory = allocator->Alloc(sizeof(LookAtTargetViewUpdater)); NW_NULL_ASSERT(updaterMemory); return new(updaterMemory) LookAtTargetViewUpdater(allocator, false, resUpdater); } //---------------------------------------- LookAtTargetViewUpdater::LookAtTargetViewUpdater( os::IAllocator* allocator, bool isDynamic, ResLookAtTargetViewUpdater resUpdater) : CameraViewUpdater(allocator, isDynamic), m_Resource(resUpdater) { } //---------------------------------------- LookAtTargetViewUpdater::~LookAtTargetViewUpdater() { if (this->IsDynamic() && this->m_Resource.IsValid()) { this->GetAllocator().Free(m_Resource.ptr()); } } //---------------------------------------- void LookAtTargetViewUpdater::Update( math::MTX34* viewMatrix, const math::MTX34& worldMatrix, const math::VEC3& cameraPosition) { NW_ASSERT(m_Resource.IsValid()); u32 flags = m_Resource.GetFlags(); math::VEC3 targetPosition(this->m_Resource.GetTargetPosition()); math::VEC3 upwardVector(this->m_Resource.GetUpwardVector()); if (ut::CheckFlagOr( flags, ResLookAtTargetViewUpdaterData::FLAG_INHERITING_TARGET_ROTATE | ResLookAtTargetViewUpdaterData::FLAG_INHERITING_TARGET_TRANSLATE | ResLookAtTargetViewUpdaterData::FLAG_INHERITING_UP_ROTATE)) { math::MTX33 rotateMatrix; math::MTX34ToMTX33(&rotateMatrix, &worldMatrix); if (ut::CheckFlag(flags, ResLookAtTargetViewUpdaterData::FLAG_INHERITING_UP_ROTATE)) { math::VEC3Transform(&upwardVector, &rotateMatrix, &upwardVector); } if (ut::CheckFlag(flags, ResLookAtTargetViewUpdaterData::FLAG_INHERITING_TARGET_ROTATE)) { math::VEC3Transform(&targetPosition, &rotateMatrix, &targetPosition); } if (ut::CheckFlag(flags, ResLookAtTargetViewUpdaterData::FLAG_INHERITING_TARGET_TRANSLATE)) { math::VEC3Add(&targetPosition, &targetPosition, &cameraPosition); } } NW_ASSERT(cameraPosition != targetPosition); NW_ASSERT(!upwardVector.IsZero()); // 視線ベクトルとアップベクトルが平行ではいけない NW_ASSERT(!math::VEC3().Cross(upwardVector, targetPosition - cameraPosition).IsZero()); math::MTX34LookAt( viewMatrix, &cameraPosition, &upwardVector, &targetPosition); } } // namespace gfx } // namespace nw