1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_FrustumProjectionUpdater.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 17185 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 #include <nw/gfx/gfx_FrustumProjectionUpdater.h>
18 #include <nw/os/os_Memory.h>
19 #include <nw/ut/ut_Rect.h>
20 
21 namespace nw
22 {
23 namespace gfx
24 {
25 
26 NW_UT_RUNTIME_TYPEINFO_DEFINITION(FrustumProjectionUpdater,CameraProjectionUpdater);
27 
28 //----------------------------------------
29 FrustumProjectionUpdater*
Create(os::IAllocator * allocator)30 FrustumProjectionUpdater::Create(os::IAllocator* allocator)
31 {
32     NW_NULL_ASSERT(allocator);
33 
34     void* updaterMemory = allocator->Alloc(sizeof(FrustumProjectionUpdater));
35     NW_NULL_ASSERT(updaterMemory);
36 
37     void* dataMemory
38         = allocator->AllocAndFill(sizeof(ResFrustumProjectionUpdaterData), NULL);
39 
40     ResFrustumProjectionUpdaterData* buffer =
41             new(dataMemory) ResFrustumProjectionUpdaterData();
42 
43     buffer->typeInfo = ResFrustumProjectionUpdater::TYPE_INFO;
44     buffer->m_Near = PROJECTION_NEAR_CLIP;
45     buffer->m_Far = PROJECTION_FAR_CLIP;
46     buffer->m_Rect.m_AspectRatio = PROJECTION_ASPECT_RATIO;
47     buffer->m_Rect.m_Center = PROJECTION_CENTER;
48     buffer->m_Rect.m_Height = PROJECTION_HEIGHT;
49 
50     ResFrustumProjectionUpdater resUpdater = ResFrustumProjectionUpdater(buffer);
51 
52     return new(updaterMemory) FrustumProjectionUpdater(allocator, true, resUpdater);
53 }
54 
55 //----------------------------------------
56 FrustumProjectionUpdater*
Create(os::IAllocator * allocator,ResFrustumProjectionUpdater resUpdater)57 FrustumProjectionUpdater::Create(os::IAllocator* allocator, ResFrustumProjectionUpdater resUpdater)
58 {
59     NW_NULL_ASSERT(allocator);
60 
61     void* updaterMemory = allocator->Alloc(sizeof(FrustumProjectionUpdater));
62     NW_NULL_ASSERT(updaterMemory);
63 
64     return new(updaterMemory) FrustumProjectionUpdater(allocator, false, resUpdater);
65 }
66 
67 //----------------------------------------
FrustumProjectionUpdater(os::IAllocator * allocator,bool isDynamic,ResFrustumProjectionUpdater resUpdater)68 FrustumProjectionUpdater::FrustumProjectionUpdater(
69     os::IAllocator* allocator,
70     bool isDynamic,
71     ResFrustumProjectionUpdater resUpdater
72 )
73 : CameraProjectionUpdater(allocator, isDynamic),
74   m_Resource(resUpdater)
75 {
76 }
77 
78 //----------------------------------------
~FrustumProjectionUpdater()79 FrustumProjectionUpdater::~FrustumProjectionUpdater()
80 {
81     if (this->IsDynamic() && this->m_Resource.IsValid())
82     {
83         this->GetAllocator().Free(m_Resource.ptr());
84     }
85 }
86 
87 
88 //----------------------------------------
89 void
Update(math::MTX44 * projectionMatrix,math::MTX34 * textureProjectionMatrix)90 FrustumProjectionUpdater::Update(math::MTX44* projectionMatrix, math::MTX34* textureProjectionMatrix)
91 {
92     NW_ASSERT(m_Resource.IsValid());
93 
94     float halfWidth = m_Resource.GetRect().GetWidth() / 2.0f;
95     float halfHeight = m_Resource.GetRect().GetHeight() / 2.0f;
96 
97     float left = m_Resource.GetRect().GetCenter().x - halfWidth;
98     float right = m_Resource.GetRect().GetCenter().x + halfWidth;
99     float bottom = m_Resource.GetRect().GetCenter().y - halfHeight;
100     float top = m_Resource.GetRect().GetCenter().y + halfHeight;
101     float near = m_Resource.GetNear();
102     float far = m_Resource.GetFar();
103 
104     math::MTX44FrustumPivot(
105         projectionMatrix,
106         left,
107         right,
108         bottom,
109         top,
110         near,
111         far,
112         this->GetPivotDirection());
113 
114     math::MTX34TextureProjectionFrustum(
115         textureProjectionMatrix,
116         left,
117         right,
118         bottom,
119         top,
120         near,
121         this->TextureScale().x,
122         this->TextureScale().y,
123         this->TextureTranslate().x,
124         this->TextureTranslate().y);
125 }
126 
127 } // namespace gfx
128 } // namespace nw
129