1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/ClearDepthOnly
3   File:     main.c
4 
5   Copyright 2003-2008 Nintendo.  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   $Date:: 2008-09-17#$
14   $Rev: 8556 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 //---------------------------------------------------------------------------
18 //      A sample to set only clear depth without clear image.
19 //
20 //      One Cube is displayed on 2D image.
21 //
22 //      HOWTO:
23 //      1.      Call "GX_SetBankForClearImage" to secure VRAM bank for clear depth.
24 //      2.      Call "GX_BeginLoadClearImage" to start clear image setting.
25 //      3.      Call "GX_LoadClearImageColor" to set clear image color.
26 //      4.      Call "GX_LoadClearImageDepth" to set clear image depth.
27 //      5.      Call "GX_EndLoadClearImage" to finish clear image setting.
28 //
29 //---------------------------------------------------------------------------
30 #ifdef SDK_TWL
31 #include <twl.h>
32 #else
33 #include <nitro.h>
34 #endif
35 #include "DEMO.h"
36 #include "tex_32768.h"
37 #include "sea.h"
38 
39 //---------------------------------------------------------------------------
40 //      Cube model data
41 //---------------------------------------------------------------------------
42 // Vertex data
43 static s16 s_Vertex[3 * 8] = {
44     FX16_ONE, FX16_ONE, FX16_ONE,
45     FX16_ONE, FX16_ONE, -FX16_ONE,
46     FX16_ONE, -FX16_ONE, FX16_ONE,
47     FX16_ONE, -FX16_ONE, -FX16_ONE,
48     -FX16_ONE, FX16_ONE, FX16_ONE,
49     -FX16_ONE, FX16_ONE, -FX16_ONE,
50     -FX16_ONE, -FX16_ONE, FX16_ONE,
51     -FX16_ONE, -FX16_ONE, -FX16_ONE
52 };
53 // Normal data
54 static VecFx10 s_Normal[6] = {
55     GX_VECFX10(0, 0, FX32_ONE - 1),
56     GX_VECFX10(0, FX32_ONE - 1, 0),
57     GX_VECFX10(FX32_ONE - 1, 0, 0),
58     GX_VECFX10(0, 0, -FX32_ONE + 1),
59     GX_VECFX10(0, -FX32_ONE + 1, 0),
60     GX_VECFX10(-FX32_ONE + 1, 0, 0)
61 };
62 // Texture coordinate data
63 static GXSt s_TextureCoord[] = {
64     GX_ST(0, 0),
65     GX_ST(0, 64 * FX32_ONE),
66     GX_ST(64 * FX32_ONE, 0),
67     GX_ST(64 * FX32_ONE, 64 * FX32_ONE)
68 };
69 
70 //---------------------------------------------------------------------------
71 //      Set vertex coordinate.
72 //      input:
73 //              idx:  ID of vertex data
74 //---------------------------------------------------------------------------
Vertex(int idx)75 inline void Vertex(int idx)
76 {
77     G3_Vtx(s_Vertex[idx * 3], s_Vertex[idx * 3 + 1], s_Vertex[idx * 3 + 2]);
78 }
79 
80 //---------------------------------------------------------------------------
81 //      Set normal setting.
82 //      input:
83 //              idx:  ID of normal data
84 //---------------------------------------------------------------------------
Normal(int idx)85 inline void Normal(int idx)
86 {
87     G3_Direct1(G3OP_NORMAL, s_Normal[idx]);
88 }
89 
90 //---------------------------------------------------------------------------
91 //      Set texture coordinate.
92 //      input:
93 //              idx:  ID of texture data
94 //---------------------------------------------------------------------------
TextureCoord(int idx)95 inline void TextureCoord(int idx)
96 {
97     G3_Direct1(G3OP_TEXCOORD, s_TextureCoord[idx]);
98 }
99 
100 //---------------------------------------------------------------------------
101 //  Draw a cube and set texture.
102 //---------------------------------------------------------------------------
DrawCube(void)103 static void DrawCube(void)
104 {
105     G3_Begin(GX_BEGIN_QUADS);          // start to set vertex.(use square polygon)
106     {
107         TextureCoord(1);
108         Normal(0);
109         Vertex(2);
110         TextureCoord(0);
111         Normal(0);
112         Vertex(0);
113         TextureCoord(2);
114         Normal(0);
115         Vertex(4);
116         TextureCoord(3);
117         Normal(0);
118         Vertex(6);
119 
120         TextureCoord(1);
121         Normal(3);
122         Vertex(7);
123         TextureCoord(0);
124         Normal(3);
125         Vertex(5);
126         TextureCoord(2);
127         Normal(3);
128         Vertex(1);
129         TextureCoord(3);
130         Normal(3);
131         Vertex(3);
132 
133         TextureCoord(1);
134         Normal(5);
135         Vertex(6);
136         TextureCoord(0);
137         Normal(5);
138         Vertex(4);
139         TextureCoord(2);
140         Normal(5);
141         Vertex(5);
142         TextureCoord(3);
143         Normal(5);
144         Vertex(7);
145 
146         TextureCoord(1);
147         Normal(2);
148         Vertex(3);
149         TextureCoord(0);
150         Normal(2);
151         Vertex(1);
152         TextureCoord(2);
153         Normal(2);
154         Vertex(0);
155         TextureCoord(3);
156         Normal(2);
157         Vertex(2);
158 
159         TextureCoord(1);
160         Normal(1);
161         Vertex(5);
162         TextureCoord(0);
163         Normal(1);
164         Vertex(4);
165         TextureCoord(2);
166         Normal(1);
167         Vertex(0);
168         TextureCoord(3);
169         Normal(1);
170         Vertex(1);
171 
172         TextureCoord(1);
173         Normal(4);
174         Vertex(6);
175         TextureCoord(0);
176         Normal(4);
177         Vertex(7);
178         TextureCoord(2);
179         Normal(4);
180         Vertex(3);
181         TextureCoord(3);
182         Normal(4);
183         Vertex(2);
184     }
185 
186     G3_End();                          // end
187 }
188 
189 //---------------------------------------------------------------------------
190 // VBlank interrupt function:
191 //
192 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
193 // OS_EnableIrqMask selects IRQ interrupts to enable, and
194 // OS_EnableIrq enables IRQ interrupts.
195 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
196 //---------------------------------------------------------------------------
VBlankIntr(void)197 void VBlankIntr(void)
198 {
199     // Set flag which checks VBlank interrupt.
200     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
201 }
202 
203 //---------------------------------------------------------------------------
204 //      main
205 //---------------------------------------------------------------------------
206 #ifdef SDK_TWL
TwlMain(void)207 void TwlMain(void)
208 #else
209 void NitroMain(void)
210 #endif
211 {
212     u32     myTexAddr = 0x0000;        // Address of texture image slot
213     u16     Rotate = 0;
214 
215     // Initialize
216     DEMOInitCommon();
217     DEMOInitVRAM();
218     DEMOInitDisplay3D();
219 
220     // Load texture image into texture image slot.
221     GX_SetBankForTex(GX_VRAM_TEX_0_A); // Set VRAM-A to texture image buffer
222     GX_BeginLoadTex();
223     {
224         GX_LoadTex((void *)&tex_32768_64x64[0], // Src address
225                    myTexAddr,          // Destination slot address
226                    8192);              // Size to load
227     }
228     GX_EndLoadTex();
229 
230     //---------------------------------------------------------------------------
231     // Clear image setting
232     // Assign VRAM-C to clear depth, and don't assign to clear image.
233     GX_SetBankForClearImage(GX_VRAM_CLEARDEPTH_128_C);
234     // Start clear depth setting
235     GX_BeginLoadClearImage();
236     {
237         // Load clear depth depth
238         GX_LoadClearImageDepth((void *)DEPTH_VRAM256x192,       // src
239                                sizeof(DEPTH_VRAM256x192));      // size
240     }
241     // Finish clear depth setting
242     GX_EndLoadClearImage();
243 
244     // Assin VRAM-D for BG
245     GX_SetBankForBG(GX_VRAM_BG_128_D); // VRAM-D for BGs
246     GX_SetVisiblePlane(GX_PLANEMASK_BG0 // BG #0 for 3D
247                        | GX_PLANEMASK_BG1);     // BG #1 for text
248 
249     /* 2D setting */
250     G2_SetBG1Control(GX_BG_SCRSIZE_TEXT_256x256,        // 256pix x 256pix text
251                      GX_BG_COLORMODE_256,       // use 256 colors mode
252                      GX_BG_SCRBASE_0x0000,      // screen base offset + 0x0000 is the address for BG #0 screen
253                      GX_BG_CHARBASE_0x04000,    // character base offset + 0x04000 is the address for BG #0 characters
254                      GX_BG_EXTPLTT_01); // use BGExtPltt slot #0 if BGExtPltt is enabled
255 
256     G2_SetBG0Priority(0);
257     G2_SetBG1Priority(1);
258     G2_BG1Mosaic(FALSE);
259 
260     GX_LoadBG1Char(d_natsunoumi_schDT, 0, sizeof(d_natsunoumi_schDT));
261     GX_LoadBGPltt(d_natsunoumi_sclDT, 0, sizeof(d_natsunoumi_sclDT));
262     GX_LoadBG1Scr(d_natsunoumi_sscDT, 0, sizeof(d_natsunoumi_sscDT));
263 
264     //---------------------------------------------------------------------------
265 
266     DEMOStartDisplay();
267 
268     // main loop
269     while (1)
270     {
271         G3X_Reset();
272         Rotate += 256;
273 
274         // Camera setting
275         {
276             VecFx32 Eye = { 0, 0, FX32_ONE };   // Sight position
277             VecFx32 at = { 0, 0, 0 };  // Viewpoint
278             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
279             G3_LookAt(&Eye, &vUp, &at, NULL);   // Sight setting
280         }
281 
282         // Light setting
283         G3_LightVector(GX_LIGHTID_0, 0, -FX32_ONE + 1, 0);
284         G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
285 
286         // Matrix setting
287         G3_MtxMode(GX_MTXMODE_TEXTURE);
288         G3_Identity();
289         G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
290         G3_PushMtx();
291 
292         // Rotate and translate cube
293         {
294             fx16    s = FX_SinIdx(Rotate);
295             fx16    c = FX_CosIdx(Rotate);
296 
297             G3_Translate(0, 0, -5 * FX32_ONE);
298 
299             G3_RotX(s, c);
300             G3_RotY(s, c);
301             G3_RotZ(s, c);
302         }
303 
304         // Draw setting
305         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // Diffuse
306                                 GX_RGB(16, 16, 16),     // Ambient
307                                 TRUE); // Color
308         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // Specular
309                                 GX_RGB(0, 0, 0),        // Emission
310                                 FALSE); // Shininess
311         G3_TexImageParam(GX_TEXFMT_DIRECT,      // Texture format
312                          GX_TEXGEN_TEXCOORD,    // Texture generation
313                          GX_TEXSIZE_S64,        // Texture width
314                          GX_TEXSIZE_T64,        // Texture height
315                          GX_TEXREPEAT_NONE,     // Texture repeat
316                          GX_TEXFLIP_NONE,       // Texture flip
317                          GX_TEXPLTTCOLOR0_USE,  // Palette color
318                          myTexAddr);   // Texture address
319         G3_PolygonAttr(GX_LIGHTMASK_0, // Light
320                        GX_POLYGONMODE_MODULATE, // Polygon mode
321                        GX_CULL_NONE,   // Culling
322                        0,              // Polygon ID
323                        31,             // Alpha
324                        GX_POLYGON_ATTR_MISC_NONE);      // Misc
325 
326         // Draw cube
327         DrawCube();
328 
329         G3_PopMtx(1);
330 
331         // swapping the polygon list RAM, the vertex RAM, etc.
332         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
333 
334         // Wait VBlank
335         OS_WaitVBlankIntr();
336 
337 #ifdef SDK_AUTOTEST                    // code for auto test
338         GX_SetBankForLCDC(GX_VRAM_LCDC_B);
339         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_B);
340         EXT_TestScreenShot(100, 0xE0E8A3B2);
341         EXT_TestTickCounter();
342 #endif //SDK_AUTOTEST
343     }
344 }
345