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