1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_LookAtTargetViewUpdater.cpp
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 #include "precompiled.h"
19 #include <nw/gfx/gfx_LookAtTargetViewUpdater.h>
20 #include <nw/os/os_Memory.h>
21 
22 namespace nw
23 {
24 namespace gfx
25 {
26 
27 NW_UT_RUNTIME_TYPEINFO_DEFINITION(LookAtTargetViewUpdater, CameraViewUpdater);
28 
29 //----------------------------------------
30 LookAtTargetViewUpdater*
Create(os::IAllocator * allocator)31 LookAtTargetViewUpdater::Create(os::IAllocator* allocator)
32 {
33     NW_NULL_ASSERT(allocator);
34 
35     void* updaterMemory = allocator->Alloc(sizeof(LookAtTargetViewUpdater));
36     NW_NULL_ASSERT(updaterMemory);
37 
38     void* dataMemory
39         = AllocateAndFill<ResLookAtTargetViewUpdaterData>(allocator, 0);
40 
41     ResLookAtTargetViewUpdaterData* buffer =
42             new(dataMemory) ResLookAtTargetViewUpdaterData();
43 
44     buffer->typeInfo         = ResLookAtTargetViewUpdater::TYPE_INFO;
45     buffer->m_TargetPosition = VIEW_TARGET_POSITION;
46     buffer->m_UpwardVector   = VIEW_UPWARD_VECTOR;
47     buffer->m_Flags          = 0x0;
48 
49     ResLookAtTargetViewUpdater resUpdater = ResLookAtTargetViewUpdater(buffer);
50 
51     return new(updaterMemory) LookAtTargetViewUpdater(allocator, true, resUpdater);
52 }
53 
54 //----------------------------------------
55 LookAtTargetViewUpdater*
Create(os::IAllocator * allocator,ResLookAtTargetViewUpdater resUpdater)56 LookAtTargetViewUpdater::Create(os::IAllocator* allocator, ResLookAtTargetViewUpdater resUpdater)
57 {
58     NW_NULL_ASSERT(allocator);
59 
60     void* updaterMemory = allocator->Alloc(sizeof(LookAtTargetViewUpdater));
61     NW_NULL_ASSERT(updaterMemory);
62 
63     return new(updaterMemory) LookAtTargetViewUpdater(allocator, false, resUpdater);
64 }
65 
66 //----------------------------------------
LookAtTargetViewUpdater(os::IAllocator * allocator,bool isDynamic,ResLookAtTargetViewUpdater resUpdater)67 LookAtTargetViewUpdater::LookAtTargetViewUpdater(
68     os::IAllocator* allocator,
69     bool isDynamic,
70     ResLookAtTargetViewUpdater resUpdater)
71 : CameraViewUpdater(allocator, isDynamic),
72   m_Resource(resUpdater)
73 {
74 }
75 
76 //----------------------------------------
~LookAtTargetViewUpdater()77 LookAtTargetViewUpdater::~LookAtTargetViewUpdater()
78 {
79     if (this->IsDynamic() && this->m_Resource.IsValid())
80     {
81         this->GetAllocator().Free(m_Resource.ptr());
82     }
83 }
84 
85 //----------------------------------------
86 void
Update(math::MTX34 * viewMatrix,const math::MTX34 & worldMatrix,const math::VEC3 & cameraPosition)87 LookAtTargetViewUpdater::Update(
88                                 math::MTX34* viewMatrix,
89                                 const math::MTX34& worldMatrix,
90                                 const math::VEC3& cameraPosition)
91 {
92     NW_ASSERT(m_Resource.IsValid());
93 
94     u32 flags = m_Resource.GetFlags();
95     math::VEC3 targetPosition(this->m_Resource.GetTargetPosition());
96     math::VEC3 upwardVector(this->m_Resource.GetUpwardVector());
97 
98     if (ut::CheckFlagOr(
99             flags,
100             ResLookAtTargetViewUpdaterData::FLAG_INHERITING_TARGET_ROTATE |
101             ResLookAtTargetViewUpdaterData::FLAG_INHERITING_TARGET_TRANSLATE |
102             ResLookAtTargetViewUpdaterData::FLAG_INHERITING_UP_ROTATE))
103     {
104         math::MTX33 rotateMatrix;
105         math::MTX34ToMTX33(&rotateMatrix, &worldMatrix);
106 
107         if (ut::CheckFlag(flags, ResLookAtTargetViewUpdaterData::FLAG_INHERITING_UP_ROTATE))
108         {
109             math::VEC3Transform(&upwardVector, &rotateMatrix, &upwardVector);
110         }
111         if (ut::CheckFlag(flags, ResLookAtTargetViewUpdaterData::FLAG_INHERITING_TARGET_ROTATE))
112         {
113             math::VEC3Transform(&targetPosition, &rotateMatrix, &targetPosition);
114         }
115         if (ut::CheckFlag(flags, ResLookAtTargetViewUpdaterData::FLAG_INHERITING_TARGET_TRANSLATE))
116         {
117             math::VEC3Add(&targetPosition, &targetPosition, &cameraPosition);
118         }
119     }
120 
121     NW_ASSERT(cameraPosition != targetPosition);
122     NW_ASSERT(!upwardVector.IsZero());
123 
124     // 視線ベクトルとアップベクトルが平行ではいけない
125     NW_ASSERT(!math::VEC3().Cross(upwardVector, targetPosition - cameraPosition).IsZero());
126 
127     math::MTX34LookAt(
128             viewMatrix,
129             &cameraPosition,
130             &upwardVector,
131             &targetPosition);
132 }
133 
134 } // namespace gfx
135 } // namespace nw
136