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