1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     main.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 //------------------------------------------------------------------
19 // デモ: texVram
20 //
21 // 概要
22 //   テクスチャをVRAMに配置するデモです。
23 //
24 //------------------------------------------------------------------
25 
26 
27 #include <nw/lyt.h>
28 #include <nw/demo.h>
29 
30 namespace
31 {
32 
33 typedef nw::ut::MoveArray<u8> File;
34 
35 /*---------------------------------------------------------------------------*
36   @brief 描画の初期設定を行います。
37 
38   @param width  画面の幅。
39   @param height  画面の高さ。
40  *---------------------------------------------------------------------------*/
41 void
InitDraw(int width,int height)42 InitDraw(int width, int height)
43 {
44     // カラーバッファ情報
45     // LCDの向きに合わせて、幅と高さを入れ替えています。
46     const nw::font::ColorBufferInfo colBufInfo =
47     {
48         height, width, PICA_DATA_DEPTH24_STENCIL8_EXT
49     };
50 
51     const u32 commands[] =
52     {
53         // ビューポートの設定
54         NW_FONT_CMD_SET_VIEWPORT(0, 0, colBufInfo.width, colBufInfo.height),
55 
56         // シザー処理を無効
57         NW_FONT_CMD_SET_DISABLE_SCISSOR(colBufInfo),
58 
59         // wバッファの無効化
60         // デプスレンジの設定
61         // ポリゴンオフセットの無効化
62         NW_FONT_CMD_SET_WBUFFER_DEPTHRANGE_POLYGONOFFSET(
63             0.0f,           // wScale : 0.0 でWバッファが無効
64             0.0f,           // depth range near
65             1.0f,           // depth range far
66             0,              // polygon offset units : 0.0 で ポリゴンオフセットが無効
67             colBufInfo),
68     };
69 
70     nngxAdd3DCommand(commands, sizeof(commands), true);
71 
72     static const u32 constCommands[] =
73     {
74         // カリングを無効
75         NW_FONT_CMD_SET_CULL_FACE(NW_FONT_CMD_CULL_FACE_DISABLE),
76 
77         // ステンシルテストを無効
78         NW_FONT_CMD_SET_DISABLE_STENCIL_TEST(),
79 
80         // デプステストを無効
81         // カラーバッファの全ての成分を書き込み可
82         NW_FONT_CMD_SET_DEPTH_FUNC_COLOR_MASK(
83             false,  // isDepthTestEnabled
84             0,      // depthFunc
85             true,   // depthMask
86             true,   // red
87             true,   // green
88             true,   // blue
89             true),  // alpha
90 
91         // アーリーデプステストを無効
92         NW_FONT_CMD_SET_ENABLE_EARLY_DEPTH_TEST(false),
93 
94         // フレームバッファアクセス制御
95         NW_FONT_CMD_SET_FBACCESS(
96             true,   // colorRead
97             true,   // colorWrite
98             false,  // depthRead
99             false,  // depthWrite
100             false,  // stencilRead
101             false), // stencilWrite
102     };
103 
104     nngxAdd3DCommand(constCommands, sizeof(constCommands), true);
105 }
106 
107 /*---------------------------------------------------------------------------*
108   @brief モデルビュー行列と射影行列を設定します。
109 
110   @param layout レイアウトです。
111   @param drawInfo 描画情報です。
112  *---------------------------------------------------------------------------*/
113 void
SetupCamera(nw::lyt::DrawInfo & drawInfo,const nw::lyt::Layout & layout)114 SetupCamera(nw::lyt::DrawInfo& drawInfo, const nw::lyt::Layout& layout)
115 {
116     nw::ut::Rect layoutRect = layout.GetLayoutRect();
117 
118     f32 znear = 0.f;
119     f32 zfar = 500.f;
120 
121     // 射影行列を正射影に設定
122     // (Layoutデータは横向きなので向きを変換する)
123     nw::math::MTX44 projMtx;
124     nw::math::MTX44Ortho(
125         &projMtx,
126         layoutRect.bottom,  // left
127         layoutRect.top,     // right
128         -layoutRect.right,  // bottom
129         -layoutRect.left,   // top
130         znear,
131         zfar);
132     drawInfo.SetProjectionMtx(projMtx);
133 
134     // モデルビュー行列を設定
135     // (Layoutデータは横向きなので画面の上方向はレイアウトの-X方向)
136     nw::math::VEC3 pos(0, 0, 1);
137     nw::math::VEC3 up(-1, 0, 0);
138     nw::math::VEC3 target(0, 0, 0);
139 
140     nw::math::MTX34 viewMtx;
141     nw::math::MTX34LookAt(&viewMtx, &pos, &up, &target);
142     drawInfo.SetViewMtx(viewMtx);
143 }
144 
145 /*---------------------------------------------------------------------------*
146   @brief バイナリリソースにテクスチャをVRAMに配置する設定を行います。
147 
148   @param fileLayout  レイアウトのバイナリリソースです。
149   @param toVram  VRAMにテクスチャを配置する場合は true を指定します。
150  *---------------------------------------------------------------------------*/
151 void
SetTexAreaToVRAM(File & fileLayout,bool toVram)152 SetTexAreaToVRAM(File& fileLayout, bool toVram)
153 {
154     nw::lyt::ARCHandle arcHandle;
155     nw::lyt::ARCInitHandle(fileLayout.begin(), &arcHandle);
156 
157     nw::lyt::ARCDir arcDir;
158     if (!nw::lyt::ArcUtil::OpenTextureDir(&arcHandle, L"/", &arcDir))
159     {
160         return;
161     }
162 
163     nw::lyt::ARCDirEntry arcDirEnt;
164     while (nw::lyt::ArcUtil::ReadTextureDir(&arcDir, &arcDirEnt))
165     {
166         nw::lyt::ARCFileInfo arcFileInfo;
167         if (!nw::lyt::ARCOpen(&arcHandle, &arcDirEnt, &arcFileInfo))
168         {
169             continue;
170         }
171 
172         nw::lyt::TexResource texResource;
173         if (!texResource.Set(arcFileInfo))
174         {
175             NW_FATAL_ERROR("unexpected texture resource.");
176             continue;
177         }
178 
179         if (toVram)
180         {
181             if (texResource.Is4bitFormat())
182             {
183                 texResource.SetImageArea(nw::lyt::MEMAREA_VRAMA);
184             }
185             else
186             {
187                 texResource.SetImageArea(nw::lyt::MEMAREA_VRAMB);
188             }
189         }
190         else
191         {
192             texResource.SetImageArea(nw::lyt::MEMAREA_FCRAM);
193         }
194 
195         nw::lyt::ARCClose(&arcFileInfo);
196     }
197 
198     nw::lyt::ARCCloseDir(&arcDir);
199 }
200 
201 }   // namespace
202 
203 /*---------------------------------------------------------------------------*
204   @brief サンプルのメイン関数です。
205  *---------------------------------------------------------------------------*/
206 void
nnMain()207 nnMain()
208 {
209     nw::demo::SimpleApp& demoApp = nw::demo::SimpleApp::GetInstance();
210     demoApp.Initialize();
211 
212     nw::lyt::Initialize(&demoApp.GetAllocator(), &demoApp.GetDeviceAllocator());
213 
214     nw::lyt::Layout* pLayout = new nw::lyt::Layout();
215 
216     // レイアウトのバイナリリソース(アーカイブ)を読み込み。
217     File fileLayout = nw::demo::Utility::LoadFile(
218         &demoApp.GetDeviceAllocator(), NW_DEMO_FILE_PATH(L"layout.arc"), 128);
219 
220     if (fileLayout.empty())
221     {
222         NW_FATAL_ERROR("can not open layout archive.\n");
223     }
224 
225     // バイナリリソースにテクスチャをVRAMに配置する設定を行います。
226     SetTexAreaToVRAM(fileLayout, true);
227 
228     // バイナリリソースのルートディレクトリを指定してリソースアクセサを生成。
229     nw::lyt::ArcResourceAccessor* pResAccessor = new nw::lyt::ArcResourceAccessor;
230     if (!pResAccessor->Attach(fileLayout.begin(), "."))
231     {
232         NW_FATAL_ERROR("can not attach layout archive.\n");
233     }
234 
235     // レイアウトリソースの読み込み
236     {
237         const void* lytRes = pResAccessor->GetResource(0, "texVram.bclyt");
238         NW_NULL_ASSERT(lytRes);
239         pLayout->Build(lytRes, pResAccessor);
240     }
241 
242     nw::lyt::GraphicsResource graphicsResource;
243     // グローバルなリソースファイルを読み込みます。
244     {
245         graphicsResource.StartSetup();
246         const wchar_t* resourcePath = 0;
247         for (int i = 0;
248              (resourcePath = graphicsResource.GetResourcePath(i)) != NULL;
249              ++i)
250         {
251             File file = nw::demo::Utility::LoadFile(&demoApp.GetAllocator(), resourcePath);
252 
253             if (file.empty())
254             {
255                 NW_FATAL_ERROR("can not read lyt resource file.");
256             }
257 
258             graphicsResource.SetResource(i, file.begin(), file.size(), false);
259         }
260 
261         graphicsResource.FinishSetup();
262     }
263 
264     nw::lyt::DrawInfo drawInfo;
265     drawInfo.SetGraphicsResource(&graphicsResource);
266 
267     nw::lyt::Drawer drawer;
268     drawer.Initialize(graphicsResource);
269 
270     const nw::ut::FloatColor clearColor(0.3f, 0.3f, 0.3f, 1.0f);
271     const f32 clearDepth = 0.0f;
272 
273     bool loop = true;
274     while (loop)
275     {
276         demoApp.SetRenderingTarget(demoApp.DISPLAY0);
277         {
278             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
279 
280             InitDraw(demoApp.DISPLAY0_WIDTH, demoApp.DISPLAY0_HEIGHT);
281 
282             SetupCamera(drawInfo, *pLayout);
283 
284             pLayout->CalculateMtx(drawInfo);
285 
286             drawer.DrawBegin(drawInfo);
287             drawer.Draw(pLayout, drawInfo);
288             drawer.DrawEnd(drawInfo);
289         }
290 
291         demoApp.SetRenderingTarget(demoApp.DISPLAY1);
292         {
293             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
294         }
295 
296         demoApp.SwapBuffer(demoApp.DISPLAY_BOTH);
297     }
298 
299     delete pLayout;
300 
301     delete pResAccessor;
302 }
303 
304