1 /*---------------------------------------------------------------------------*
2 Project: NintendoWare
3 File: gfx_PerspectiveProjectionUpdater.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_PerspectiveProjectionUpdater.h>
18 #include <nw/os/os_Memory.h>
19
20 namespace nw
21 {
22 namespace gfx
23 {
24
25 NW_UT_RUNTIME_TYPEINFO_DEFINITION(PerspectiveProjectionUpdater,CameraProjectionUpdater);
26
27 //----------------------------------------
28 PerspectiveProjectionUpdater*
Create(os::IAllocator * allocator)29 PerspectiveProjectionUpdater::Create(os::IAllocator* allocator)
30 {
31 NW_NULL_ASSERT(allocator);
32
33 void* updaterMemory = allocator->Alloc(sizeof(PerspectiveProjectionUpdater));
34 NW_NULL_ASSERT(updaterMemory);
35
36 void* dataMemory
37 = AllocateAndFill<ResPerspectiveProjectionUpdaterData>(allocator, 0);
38
39 ResPerspectiveProjectionUpdaterData* buffer =
40 new(dataMemory) ResPerspectiveProjectionUpdaterData();
41
42 buffer->typeInfo = ResPerspectiveProjectionUpdater::TYPE_INFO;
43 buffer->m_Near = PROJECTION_NEAR_CLIP;
44 buffer->m_Far = PROJECTION_FAR_CLIP;
45 buffer->m_Fovy = PROJECTION_FOVY_RADIAN;
46 buffer->m_AspectRatio = PROJECTION_ASPECT_RATIO;
47
48 ResPerspectiveProjectionUpdater resUpdater = ResPerspectiveProjectionUpdater(buffer);
49
50 return new(updaterMemory) PerspectiveProjectionUpdater(allocator, true, resUpdater);
51 }
52
53 //----------------------------------------
54 PerspectiveProjectionUpdater*
Create(os::IAllocator * allocator,ResPerspectiveProjectionUpdater resUpdater)55 PerspectiveProjectionUpdater::Create(
56 os::IAllocator* allocator,
57 ResPerspectiveProjectionUpdater resUpdater)
58 {
59 NW_NULL_ASSERT(allocator);
60
61 void* updaterMemory = allocator->Alloc(sizeof(PerspectiveProjectionUpdater));
62 NW_NULL_ASSERT(updaterMemory);
63
64 return new(updaterMemory) PerspectiveProjectionUpdater(allocator, false, resUpdater);
65 }
66
67 //----------------------------------------
PerspectiveProjectionUpdater(os::IAllocator * allocator,bool isDynamic,ResPerspectiveProjectionUpdater resUpdater)68 PerspectiveProjectionUpdater::PerspectiveProjectionUpdater(
69 os::IAllocator* allocator,
70 bool isDynamic,
71 ResPerspectiveProjectionUpdater resUpdater
72 )
73 : CameraProjectionUpdater(allocator, isDynamic),
74 m_Resource(resUpdater)
75 {
76 }
77
78 //----------------------------------------
~PerspectiveProjectionUpdater()79 PerspectiveProjectionUpdater::~PerspectiveProjectionUpdater()
80 {
81 if (this->IsDynamic() && this->m_Resource.IsValid())
82 {
83 this->GetAllocator().Free(m_Resource.ptr());
84 }
85 }
86
87 //----------------------------------------
88 void
Update(math::MTX44 * projectionMatrix,math::MTX34 * textureProjectionMatrix)89 PerspectiveProjectionUpdater::Update(math::MTX44* projectionMatrix, math::MTX34* textureProjectionMatrix)
90 {
91 NW_ASSERT(m_Resource.IsValid());
92
93 float fovy = this->m_Resource.GetFovy();
94 NW_ASSERT(0 < fovy && fovy < nw::math::F_PI);
95
96 float aspect = this->m_Resource.GetAspectRatio();
97 NW_ASSERT(aspect != 0);
98
99 float near = this->m_Resource.GetNear();
100 float far = this->m_Resource.GetFar();
101 NW_ASSERT(near != far);
102
103 math::MTX44PerspectivePivotRad(
104 projectionMatrix,
105 fovy,
106 aspect,
107 near,
108 far,
109 this->GetPivotDirection());
110
111 math::MTX34TextureProjectionPerspective(
112 textureProjectionMatrix,
113 fovy,
114 aspect,
115 this->TextureScale().x,
116 this->TextureScale().y,
117 this->TextureTranslate().x,
118 this->TextureTranslate().y);
119 }
120
121 } // namespace gfx
122 } // namespace nw
123