1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_FrustumProjectionUpdater.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_FrustumProjectionUpdater.h>
20 #include <nw/os/os_Memory.h>
21 #include <nw/ut/ut_Rect.h>
22 
23 namespace nw
24 {
25 namespace gfx
26 {
27 
28 NW_UT_RUNTIME_TYPEINFO_DEFINITION(FrustumProjectionUpdater,CameraProjectionUpdater);
29 
30 //----------------------------------------
31 FrustumProjectionUpdater*
Create(os::IAllocator * allocator)32 FrustumProjectionUpdater::Create(os::IAllocator* allocator)
33 {
34     NW_NULL_ASSERT(allocator);
35 
36     void* updaterMemory = allocator->Alloc(sizeof(FrustumProjectionUpdater));
37     NW_NULL_ASSERT(updaterMemory);
38 
39     void* dataMemory
40         = AllocateAndFill<ResFrustumProjectionUpdaterData>(allocator, 0);
41 
42     ResFrustumProjectionUpdaterData* buffer =
43             new(dataMemory) ResFrustumProjectionUpdaterData();
44 
45     buffer->typeInfo = ResFrustumProjectionUpdater::TYPE_INFO;
46     buffer->m_Near = PROJECTION_NEAR_CLIP;
47     buffer->m_Far = PROJECTION_FAR_CLIP;
48     buffer->m_Rect.m_AspectRatio = PROJECTION_ASPECT_RATIO;
49     buffer->m_Rect.m_Center = PROJECTION_CENTER;
50     buffer->m_Rect.m_Height = PROJECTION_HEIGHT;
51 
52     ResFrustumProjectionUpdater resUpdater = ResFrustumProjectionUpdater(buffer);
53 
54     return new(updaterMemory) FrustumProjectionUpdater(allocator, true, resUpdater);
55 }
56 
57 //----------------------------------------
58 FrustumProjectionUpdater*
Create(os::IAllocator * allocator,ResFrustumProjectionUpdater resUpdater)59 FrustumProjectionUpdater::Create(os::IAllocator* allocator, ResFrustumProjectionUpdater resUpdater)
60 {
61     NW_NULL_ASSERT(allocator);
62 
63     void* updaterMemory = allocator->Alloc(sizeof(FrustumProjectionUpdater));
64     NW_NULL_ASSERT(updaterMemory);
65 
66     return new(updaterMemory) FrustumProjectionUpdater(allocator, false, resUpdater);
67 }
68 
69 //----------------------------------------
FrustumProjectionUpdater(os::IAllocator * allocator,bool isDynamic,ResFrustumProjectionUpdater resUpdater)70 FrustumProjectionUpdater::FrustumProjectionUpdater(
71     os::IAllocator* allocator,
72     bool isDynamic,
73     ResFrustumProjectionUpdater resUpdater
74 )
75 : CameraProjectionUpdater(allocator, isDynamic),
76   m_Resource(resUpdater)
77 {
78 }
79 
80 //----------------------------------------
~FrustumProjectionUpdater()81 FrustumProjectionUpdater::~FrustumProjectionUpdater()
82 {
83     if (this->IsDynamic() && this->m_Resource.IsValid())
84     {
85         this->GetAllocator().Free(m_Resource.ptr());
86     }
87 }
88 
89 
90 //----------------------------------------
91 void
Update(math::MTX44 * projectionMatrix,math::MTX34 * textureProjectionMatrix)92 FrustumProjectionUpdater::Update(math::MTX44* projectionMatrix, math::MTX34* textureProjectionMatrix)
93 {
94     NW_ASSERT(m_Resource.IsValid());
95 
96     float halfWidth = m_Resource.GetRect().GetWidth() / 2.0f;
97     float halfHeight = m_Resource.GetRect().GetHeight() / 2.0f;
98 
99     float left = m_Resource.GetRect().GetCenter().x - halfWidth;
100     float right = m_Resource.GetRect().GetCenter().x + halfWidth;
101     float bottom = m_Resource.GetRect().GetCenter().y - halfHeight;
102     float top = m_Resource.GetRect().GetCenter().y + halfHeight;
103     float near = m_Resource.GetNear();
104     float far = m_Resource.GetFar();
105 
106     math::MTX44FrustumPivot(
107         projectionMatrix,
108         left,
109         right,
110         bottom,
111         top,
112         near,
113         far,
114         this->GetPivotDirection());
115 
116     math::MTX34TextureProjectionFrustum(
117         textureProjectionMatrix,
118         left,
119         right,
120         bottom,
121         top,
122         near,
123         this->TextureScale().x,
124         this->TextureScale().y,
125         this->TextureTranslate().x,
126         this->TextureTranslate().y);
127 }
128 
129 } // namespace gfx
130 } // namespace nw
131