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