1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     LytCtrl.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: 1 $
14  *---------------------------------------------------------------------------*/
15 #include <nn/os.h>
16 
17 #include <nw/types.h>
18 //#include <nw/demo.h>
19 #include <nw/dev.h>
20 #include <nw/gfx.h>
21 #include <nw/ut.h>
22 
23 #include "../include/LytCtrl.h"
24 
25 
26 namespace
27 {
28 
29 
30 /*---------------------------------------------------------------------------*
31   @brief グラフィックスの初期設定を行います。
32  *---------------------------------------------------------------------------*/
33 void
InitGX()34 InitGX()
35 {
36 #ifdef NW_PLATFORM_CTR
37     GLenum curCmdGenMode = 0;
38     nngxGetCommandGenerationMode(&curCmdGenMode);
39 
40     if (curCmdGenMode != NN_GX_CMDGEN_MODE_UNCONDITIONAL)
41     {
42         // NN_GX_STATE_OTHER ステートのコマンドをただちに発行させます。
43         nngxSetCommandGenerationMode(NN_GX_CMDGEN_MODE_UNCONDITIONAL);
44     }
45 #endif
46 
47     glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
48     glDisable(GL_CULL_FACE);
49     glDisable(GL_DEPTH_TEST);
50     glDisable(GL_SCISSOR_TEST);
51     glDisable(GL_STENCIL_TEST);
52     glDisable(GL_POLYGON_OFFSET_FILL);
53     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
54     glDepthMask(GL_TRUE);
55 #ifdef NW_PLATFORM_CTR
56     glDisable(GL_EARLY_DEPTH_TEST_DMP);
57 #endif
58 
59 #ifdef NW_PLATFORM_CTR
60     // コマンド発行モードを元に戻します。
61     if (curCmdGenMode != NN_GX_CMDGEN_MODE_UNCONDITIONAL)
62     {
63         nngxSetCommandGenerationMode(curCmdGenMode);
64     }
65 
66     // NN_GX_STATE_OTHERS 以外のステートはバリデートによりコマンドを発行します。
67     const GLbitfield stateMask =
68         NN_GX_STATE_TRIOFFSET |
69         NN_GX_STATE_SCISSOR |
70         NN_GX_STATE_FBACCESS;
71 
72     nngxUpdateState(stateMask);
73     NW_LYT_DRAWER_VALIDATE(stateMask);
74 #endif
75 
76     NW_GL_ASSERT();
77 }
78 
79 
80 /*!--------------------------------------------------------------------------*
81   @brief        コンストラクタ。
82  *---------------------------------------------------------------------------*/
LytCtrl()83 LytCtrl::LytCtrl()
84 {
85     m_ResourcesArray.clear();
86 
87     // シェーダファイル等のロード
88     {
89         m_GraphicsResource.StartSetup();
90         const wchar_t* resourcePath = 0;
91         for (int i = 0;
92              (resourcePath = m_GraphicsResource.GetResourcePath(i)) != NULL;
93              ++i)
94         {
95             // todo:ここでロードされたメモリの行方
96             SmFile file;
97             if (!file.Read(resourcePath, m_Allocator))
98             {
99                 NW_FATAL_ERROR("can not read lyt resource file.");
100             }
101             m_GraphicsResource.SetResource(i, file.Buffer(), file.Size());
102         }
103         m_GraphicsResource.FinishSetup();
104 
105         m_DrawInfo.SetGraphicsResource( &m_GraphicsResource );
106     }
107 
108     // Drawerの初期化
109     m_Drawer.Initialize( m_GraphicsResource );
110 }
111 
112 
113 /*!--------------------------------------------------------------------------*
114   @brief        デストラクタ。
115  *---------------------------------------------------------------------------*/
~LytCtrl()116 LytCtrl::~LytCtrl()
117 {
118     uint i = 0;
119 
120     // SmLayout インスタンスを破棄します。
121     for ( i=0; i<m_ResourcesArray.size(); i++ )
122     {
123         SM_SAFE_DELETE( m_SmLytInstanceArray[i] );
124     }
125 
126     // SmLytResource インスタンスを破棄します。
127     for ( i=0; i<m_SmLytResourceArray.size(); i++ )
128     {
129         SM_SAFE_DELETE( m_SmLytResourceArray[i] );
130     }
131 
132     // 読み込んだレイアウトリソースの解放を行います。
133     for ( i=0; i<m_ResourcesArray.size(); i++ )
134     {
135         SM_SAFE_DELETE( m_ResourcesArray[i].resAccessor );
136         m_ResourcesArray[i].lytFile.Release();
137     }
138 }
139 
140 
141 /*!--------------------------------------------------------------------------*
142   @brief        レイアウトリソースをセットアップします。
143  *---------------------------------------------------------------------------*/
144 SmLayoutResource*
CreateSmLayoutResource(const wchar_t * archivePath,LytResourceFile & lytResFile)145 LytCtrl::CreateSmLayoutResource( const wchar_t* archivePath, LytResourceFile& lytResFile )
146 {
147     LytResourceSet      resSet;
148     SmLayoutResource*   smLytResource = NULL;
149     bool                isPushed = false;
150     NW_UNUSED_VARIABLE(isPushed);
151 
152     // レイアウトのバイナリリソース(アーカイブ)を読み込みます。
153     if (!resSet.lytFile.Read( archivePath, m_DeviceAllocator ) )
154     {
155         NW_FATAL_ERROR("can not open layout archive.\n");
156     }
157 
158     // バイナリリソースのルートディレクトリを指定してリソースアクセサを生成します。
159     resSet.resAccessor = new nw::lyt::ArcResourceAccessor;
160     if (!resSet.resAccessor->Attach(resSet.lytFile.Buffer(), "."))
161     {
162         NW_FATAL_ERROR("can not attach layout archive.\n");
163     }
164 
165     // レイアウトリソースクラスを生成します。
166     smLytResource = new SmLayoutResource();
167 
168     // nw::lyt::Layoutを生成します。
169     const void* lytRes = resSet.resAccessor->GetResource( 0, lytResFile.clyt.c_str() );
170     NW_NULL_ASSERT( lytRes );
171      if ( !smLytResource->InitLayout( lytRes, resSet.resAccessor ) )
172     {
173         NW_FATAL_ERROR("can not setup layout archive.\n");
174     }
175 
176     // アニメーションリソースの読み込みます。
177     for ( uint i = 0; i < lytResFile.clan.size(); i++ )
178     {
179         const void* lpaRes = resSet.resAccessor->GetResource( 0, lytResFile.clan[i].c_str() );
180          if ( lpaRes )
181         {
182              if ( !smLytResource->AddAnimation( lpaRes, resSet.resAccessor ) )
183             {
184                 NW_FATAL_ERROR("can not setup layout animation archive.\n");
185             }
186         }
187     }
188 
189     isPushed = m_SmLytResourceArray.push_back( smLytResource );
190     NW_ASSERT(isPushed);
191 
192     isPushed = m_ResourcesArray.push_back( resSet );
193     NW_ASSERT(isPushed);
194 
195     // カメラのセットアップ
196     setupCamera( smLytResource->GetLayout()->GetLayoutRect() );
197 
198     return smLytResource;
199 }
200 
201 
202 /*!--------------------------------------------------------------------------*
203   @brief        レイアウトリソースクラスのインスタンスを引数に、SmLayoutクラスのインスタンスを生成します。
204  *---------------------------------------------------------------------------*/
205 SmLayout*
CreateSmLayout(SmLayoutResource * layoutResource)206 LytCtrl::CreateSmLayout( SmLayoutResource* layoutResource )
207 {
208     SmLayout* smLyt = new SmLayout( layoutResource );
209     NW_NULL_ASSERT( smLyt );
210 
211     // 生成したインスタンスを保持します。
212     bool isPushed = m_SmLytInstanceArray.push_back( smLyt );
213     NW_ASSERT(isPushed);
214 
215     return smLyt;
216 }
217 
218 
219 /*!--------------------------------------------------------------------------*
220   @brief        保持するSmLayoutクラスの描画を行います。
221  *---------------------------------------------------------------------------*/
222 void
Render()223 LytCtrl::Render()
224 {
225     if ( m_SmLytInstanceArray.empty() ) return;
226 
227     InitGX();
228     NW_LYT_DRAWER_VALIDATE(NN_GX_STATE_ALL);
229 
230     // 保持しているSmLaytouの配列分だけエントリを行います。
231     for ( uint i = 0; i < m_SmLytInstanceArray.size(); i++ )
232     {
233         SmLayout* pLayout = m_SmLytInstanceArray[i];
234 
235         if ( pLayout->GetVisible() )
236         {
237             m_Drawer.DrawBegin( m_DrawInfo );
238 
239             // レイアウトへデータを反映する
240              if ( pLayout->SetupLayout() )
241             {
242                 SmLayoutResource* smLayoutRes = pLayout->GetLayoutResource();
243                 nw::lyt::Layout* layout = smLayoutRes->GetLayout();
244                 layout->Animate();
245                 layout->CalculateMtx( m_DrawInfo );
246                 m_Drawer.Draw( layout, m_DrawInfo );
247                 m_Drawer.DrawEnd( m_DrawInfo );
248 
249                 pLayout->ResetLayout();
250             }
251         }
252     }
253 
254     nngxUpdateState(NN_GX_STATE_ALL);
255 }
256 
257 
258 /*!--------------------------------------------------------------------------*
259   @brief        m_DrawInfoの初期化を行います。
260  *---------------------------------------------------------------------------*/
261 void
setupCamera(nw::ut::Rect rect)262 LytCtrl::setupCamera( nw::ut::Rect rect )
263 {
264     f32 znear = 0.f;
265     f32 zfar = 500.f;
266 
267     // 射影行列を正射影に設定
268     // (Layoutデータは横向きなので向きを変換する)
269     nw::math::MTX44 projMtx;
270     nw::math::MTX44OrthoPivot(
271         &projMtx,
272         rect.left,   // left
273         rect.right,  // right
274         rect.bottom, // bottom
275         rect.top,    // top
276         znear,
277         zfar,
278         nw::math::PIVOT_UPSIDE_TO_TOP);
279     m_DrawInfo.SetProjectionMtx(projMtx);
280 
281     // モデルビュー行列を設定
282     // (Layoutデータは横向きなので画面の上方向はレイアウトの-X方向)
283     nw::math::VEC3 pos(0, 0, 1);
284     nw::math::VEC3 up(0, 1, 0);
285     nw::math::VEC3 target(0, 0, 0);
286 
287     nw::math::MTX34 viewMtx;
288     nw::math::MTX34LookAt(&viewMtx, &pos, &up, &target);
289     m_DrawInfo.SetViewMtx(viewMtx);
290 }
291 
292 } // namespace
293 
294 
295