1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     gfx_OrthoProjectionUpdater.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: 28760 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 #include <nw/gfx/gfx_OrthoProjectionUpdater.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(OrthoProjectionUpdater,CameraProjectionUpdater);
27 
28 //----------------------------------------
29 OrthoProjectionUpdater*
Create(os::IAllocator * allocator)30 OrthoProjectionUpdater::Create(os::IAllocator* allocator)
31 {
32     NW_NULL_ASSERT(allocator);
33 
34     void* updaterMemory = allocator->Alloc(sizeof(OrthoProjectionUpdater));
35     NW_NULL_ASSERT(updaterMemory);
36 
37     void* dataMemory
38         = AllocateAndFill<ResOrthoProjectionUpdaterData>(allocator, 0);
39 
40     ResOrthoProjectionUpdaterData* buffer =
41             new(dataMemory) ResOrthoProjectionUpdaterData();
42 
43     buffer->typeInfo = ResOrthoProjectionUpdater::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     ResOrthoProjectionUpdater resUpdater = ResOrthoProjectionUpdater(buffer);
51 
52     return new(updaterMemory) OrthoProjectionUpdater(allocator, true, resUpdater);
53 }
54 
55 //----------------------------------------
56 OrthoProjectionUpdater*
Create(os::IAllocator * allocator,ResOrthoProjectionUpdater resUpdater)57 OrthoProjectionUpdater::Create(
58     os::IAllocator* allocator,
59     ResOrthoProjectionUpdater resUpdater)
60 {
61     NW_NULL_ASSERT(allocator);
62 
63     void* updaterMemory = allocator->Alloc(sizeof(OrthoProjectionUpdater));
64     NW_NULL_ASSERT(updaterMemory);
65 
66     return new(updaterMemory) OrthoProjectionUpdater(allocator, false, resUpdater);
67 }
68 
69 //----------------------------------------
OrthoProjectionUpdater(os::IAllocator * allocator,bool isDynamic,ResOrthoProjectionUpdater resUpdater)70 OrthoProjectionUpdater::OrthoProjectionUpdater(
71     os::IAllocator* allocator,
72     bool isDynamic,
73     ResOrthoProjectionUpdater resUpdater
74 )
75 : CameraProjectionUpdater(allocator, isDynamic),
76   m_Resource(resUpdater)
77 {
78 }
79 
80 //----------------------------------------
~OrthoProjectionUpdater()81 OrthoProjectionUpdater::~OrthoProjectionUpdater()
82 {
83     if (this->IsDynamic() && this->m_Resource.IsValid())
84     {
85         this->GetAllocator().Free(m_Resource.ptr());
86     }
87 }
88 
89 //----------------------------------------
90 void
Update(math::MTX44 * projectionMatrix,math::MTX34 * textureProjectionMatrix)91 OrthoProjectionUpdater::Update(math::MTX44* projectionMatrix, math::MTX34* textureProjectionMatrix)
92 {
93     NW_ASSERT(m_Resource.IsValid());
94     NW_ASSERT(m_Resource.GetRect().GetAspectRatio() != 0);
95     NW_ASSERT(m_Resource.GetRect().GetHeight() != 0);
96 
97     float halfWidth = m_Resource.GetRect().GetWidth() / 2.0f;
98     float halfHeight = m_Resource.GetRect().GetHeight() / 2.0f;
99 
100     float left = m_Resource.GetRect().GetCenter().x - halfWidth;
101     float right = m_Resource.GetRect().GetCenter().x + halfWidth;
102     float bottom = m_Resource.GetRect().GetCenter().y - halfHeight;
103     float top = m_Resource.GetRect().GetCenter().y + halfHeight;
104 
105     float near = m_Resource.GetNear();
106     float far = m_Resource.GetFar();
107     NW_ASSERT(near != far);
108 
109     math::MTX44OrthoPivot(
110         projectionMatrix,
111         left,
112         right,
113         bottom,
114         top,
115         near,
116         far,
117         this->GetPivotDirection());
118 
119     math::MTX34TextureProjectionOrtho(
120         textureProjectionMatrix,
121         left,
122         right,
123         bottom,
124         top,
125         this->TextureScale().x,
126         this->TextureScale().y,
127         this->TextureTranslate().x,
128         this->TextureTranslate().y);
129 }
130 
131 } // namespace gfx
132 } // namespace nw
133