1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/PosVecTest
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 do VectorTest and PositionTest
19 //
20 //  One cube is displayed.
21 //  You can try VectorTest and PositionTest, and see the results.
22 //
23 //  HOWTO:
24 //  1. Call G3_PositionTest to start PositionTest
25 //  2. Call G3X_GetPositionTestResult to get the result of the test
26 //  3. Call G3_VectorTest" to start VectorTest
27 //  4. Call G3X_GetVectorTestResult to get the result of the test
28 //
29 //  OPERATION:
30 //  1. Push A button for PositionTest
31 //  2. Push B button for VectorTest
32 //
33 //---------------------------------------------------------------------------
34 #ifdef SDK_TWL
35 #include <twl.h>
36 #else
37 #include <nitro.h>
38 #endif
39 #include "DEMO.h"
40 #include "tex_32768.h"
41 
42 //---------------------------------------------------------------------------
43 //  Cube model data
44 //---------------------------------------------------------------------------
45 // Vertex data
46 static s16 s_Vertex[3 * 8] = {
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     -FX16_ONE, FX16_ONE, -FX16_ONE,
53     -FX16_ONE, -FX16_ONE, FX16_ONE,
54     -FX16_ONE, -FX16_ONE, -FX16_ONE
55 };
56 // Normal data
57 static VecFx10 s_Normal[6] = {
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     GX_VECFX10(0, 0, -FX32_ONE + 1),
62     GX_VECFX10(0, -FX32_ONE + 1, 0),
63     GX_VECFX10(-FX32_ONE + 1, 0, 0)
64 };
65 // Texture coordinate data
66 static GXSt s_TextureCoord[] = {
67     GX_ST(0, 0),
68     GX_ST(0, 64 * FX32_ONE),
69     GX_ST(64 * FX32_ONE, 0),
70     GX_ST(64 * FX32_ONE, 64 * FX32_ONE)
71 };
72 
73 //---------------------------------------------------------------------------
74 //  Set vertex coordinate
75 //  Input:
76 //      idx:  ID of vertex data
77 //---------------------------------------------------------------------------
Vertex(int idx)78 inline void Vertex(int idx)
79 {
80     G3_Vtx(s_Vertex[idx * 3], s_Vertex[idx * 3 + 1], s_Vertex[idx * 3 + 2]);
81 }
82 
83 //---------------------------------------------------------------------------
84 //  Set normal setting
85 //  Input:
86 //      idx:  ID of normal data
87 //---------------------------------------------------------------------------
Normal(int idx)88 inline void Normal(int idx)
89 {
90     G3_Direct1(G3OP_NORMAL, s_Normal[idx]);
91 }
92 
93 //---------------------------------------------------------------------------
94 //  Set texture coordinate
95 //  Input:
96 //      idx:  ID of texture data
97 //---------------------------------------------------------------------------
TextureCoord(int idx)98 inline void TextureCoord(int idx)
99 {
100     G3_Direct1(G3OP_TEXCOORD, s_TextureCoord[idx]);
101 }
102 
103 //---------------------------------------------------------------------------
104 //  Draw a cube and set texture
105 //---------------------------------------------------------------------------
DrawCube(void)106 static void DrawCube(void)
107 {
108     G3_Begin(GX_BEGIN_QUADS);          // Start to set vertices. (Use quadrilateral polygons)
109     {
110         TextureCoord(1);
111         Normal(0);
112         Vertex(2);
113         TextureCoord(0);
114         Normal(0);
115         Vertex(0);
116         TextureCoord(2);
117         Normal(0);
118         Vertex(4);
119         TextureCoord(3);
120         Normal(0);
121         Vertex(6);
122 
123         TextureCoord(1);
124         Normal(3);
125         Vertex(7);
126         TextureCoord(0);
127         Normal(3);
128         Vertex(5);
129         TextureCoord(2);
130         Normal(3);
131         Vertex(1);
132         TextureCoord(3);
133         Normal(3);
134         Vertex(3);
135 
136         TextureCoord(1);
137         Normal(5);
138         Vertex(6);
139         TextureCoord(0);
140         Normal(5);
141         Vertex(4);
142         TextureCoord(2);
143         Normal(5);
144         Vertex(5);
145         TextureCoord(3);
146         Normal(5);
147         Vertex(7);
148 
149         TextureCoord(1);
150         Normal(2);
151         Vertex(3);
152         TextureCoord(0);
153         Normal(2);
154         Vertex(1);
155         TextureCoord(2);
156         Normal(2);
157         Vertex(0);
158         TextureCoord(3);
159         Normal(2);
160         Vertex(2);
161 
162         TextureCoord(1);
163         Normal(1);
164         Vertex(5);
165         TextureCoord(0);
166         Normal(1);
167         Vertex(4);
168         TextureCoord(2);
169         Normal(1);
170         Vertex(0);
171         TextureCoord(3);
172         Normal(1);
173         Vertex(1);
174 
175         TextureCoord(1);
176         Normal(4);
177         Vertex(6);
178         TextureCoord(0);
179         Normal(4);
180         Vertex(7);
181         TextureCoord(2);
182         Normal(4);
183         Vertex(3);
184         TextureCoord(3);
185         Normal(4);
186         Vertex(2);
187     }
188     G3_End();                          // End
189 }
190 
191 //---------------------------------------------------------------------------
192 // V-Blank interrupt function:
193 //
194 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
195 // OS_EnableIrqMask selects IRQ interrupts to enable, and
196 // OS_EnableIrq enables IRQ interrupts.
197 // Notice that you have to call OS_SetIrqCheckFlag to check a V-Blank interrupt.
198 //---------------------------------------------------------------------------
VBlankIntr(void)199 void VBlankIntr(void)
200 {
201     // Set flag that checks for V-Blank interrupt
202     OS_SetIrqCheckFlag(OS_IE_V_BLANK);
203 }
204 
205 //---------------------------------------------------------------------------
206 //  Main
207 //---------------------------------------------------------------------------
208 #ifdef  SDK_CW_WA_CONSTPOOLS
209 #pragma optimization_level 1
210 #endif
211 #ifdef SDK_TWL
TwlMain(void)212 void TwlMain(void)
213 #else
214 void NitroMain(void)
215 #endif
216 {
217     unsigned int count = 0;
218     u32     myTexAddr = 0x00000;       // Address of texture image slot
219     u16     Rotate = 0;
220 
221     // Initialize
222     DEMOInitCommon();
223     DEMOInitVRAM();
224     DEMOInitDisplay3D();
225 
226     // Load texture image into texture image slot.
227     GX_BeginLoadTex();
228     {
229         GX_LoadTex((void *)&tex_32768_64x64[0], // Src address
230                    myTexAddr,          // Destination address
231                    8192);              // Size to load
232     }
233     GX_EndLoadTex();
234 
235     DEMOStartDisplay();
236 
237     // main loop
238     while (1)
239     {
240         G3X_Reset();
241         Rotate += 256;
242 
243         // Read input
244         DEMOReadKey();
245 #ifdef SDK_AUTOTEST                    // Code for auto-test
246         {
247             const EXTKeys keys[8] = { {0, 40}, {PAD_BUTTON_A, 10},
248             {0, 40}, {PAD_BUTTON_B, 10}, {0, 0}
249             };
250             EXT_AutoKeys(keys, &gKeyWork.press, &gKeyWork.trigger);
251         }
252 #endif
253 
254         // Position test
255         if (DEMO_IS_TRIG(PAD_BUTTON_A))
256         {
257             VecFx32 m;
258             fx32    w;
259             fx16    s = FX_SinIdx(Rotate);
260             fx16    c = FX_CosIdx(Rotate);
261 
262             // Set matrix for test
263             G3_MtxMode(GX_MTXMODE_TEXTURE);
264             G3_Identity();
265             G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
266             G3_PushMtx();
267             G3_Translate(0, 0, -5 * FX32_ONE);
268             G3_RotX(s, c);
269             G3_RotY(s, c);
270             G3_RotZ(s, c);
271             //---------------------------------------------------------------------------
272             // Start position test
273             G3_PositionTest(FX16_ONE, FX16_ONE, FX16_ONE);
274             //---------------------------------------------------------------------------
275             G3_PopMtx(1);
276 
277             //---------------------------------------------------------------------------
278             // Get result of position test
279             while (G3X_GetPositionTestResult(&m, &w))
280             {
281             }
282             //---------------------------------------------------------------------------
283 
284             // Print result of position test
285             OS_Printf("mssg%d:Position Test : Pos(%d, %d, %d) W(%d)\n", count++, m.x, m.y, m.z, w);
286 
287 #ifdef SDK_AUTOTEST
288             EXT_CompPrint("Pos(3611, 456, 5817) W(13960)", "Pos(%d, %d, %d) W(%d)", m.x, m.y, m.z,
289                           w);
290 #endif
291         }
292 
293         /* Perform G3_VectorTest() */
294         // Vector test
295         if (DEMO_IS_TRIG(PAD_BUTTON_B))
296         {
297             fx16    vec[3];
298 
299             fx16    s = FX_SinIdx(Rotate);
300             fx16    c = FX_CosIdx(Rotate);
301 
302             // Set matrix for test
303             G3_MtxMode(GX_MTXMODE_TEXTURE);
304             G3_Identity();
305             G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
306             G3_PushMtx();
307             G3_Translate(0, 0, -5 * FX32_ONE);
308             G3_RotX(s, c);
309             G3_RotY(s, c);
310             G3_RotZ(s, c);
311             //---------------------------------------------------------------------------
312             // Start vector test
313             G3_VectorTest(0, 0, FX16_ONE - 1);
314             //---------------------------------------------------------------------------
315             G3_PopMtx(1);
316 
317             //---------------------------------------------------------------------------
318             // Get result of vector test
319             while (G3X_GetVectorTestResult(vec))
320             {
321             }
322             //---------------------------------------------------------------------------
323 
324             // Print result of vector test
325             OS_Printf("mssg%d:Vector Test : Vec(%d, %d, %d)\n", count++, vec[0], vec[1], vec[2]);
326 
327 #ifdef SDK_AUTOTEST
328             EXT_CompPrint("Vec(3222, 1982, 1546)", "Vec(%d, %d, %d)", vec[0], vec[1], vec[2]);
329 #endif
330         }
331 
332         // Camera settings
333         {
334             VecFx32 Eye = { 0, 0, FX32_ONE };   // Sight position
335             VecFx32 at = { 0, 0, 0 };  // viewpoint
336             VecFx32 vUp = { 0, FX32_ONE, 0 };   // up
337             G3_LookAt(&Eye, &vUp, &at, NULL);   // Sight settings
338         }
339 
340         // Light settings
341         G3_LightVector(GX_LIGHTID_0, 0, -FX32_ONE + 1, 0);
342         G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
343 
344         // Matrix settings
345         G3_MtxMode(GX_MTXMODE_TEXTURE);
346         G3_Identity();
347         G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
348         G3_PushMtx();
349 
350         // Rotate and translate cube
351         {
352             fx16    s = FX_SinIdx(Rotate);
353             fx16    c = FX_CosIdx(Rotate);
354 
355             G3_Translate(0, 0, -5 * FX32_ONE);
356 
357             G3_RotX(s, c);
358             G3_RotY(s, c);
359             G3_RotZ(s, c);
360         }
361 
362         // Draw settings
363         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // Diffuse
364                                 GX_RGB(16, 16, 16),     // Ambient
365                                 TRUE); // Color
366         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // Specular
367                                 GX_RGB(0, 0, 0),        // Emission
368                                 FALSE); // Shininess
369         G3_TexImageParam(GX_TEXFMT_DIRECT,      // Texture format
370                          GX_TEXGEN_TEXCOORD,    // Texture generation
371                          GX_TEXSIZE_S64,        // Texture width
372                          GX_TEXSIZE_T64,        // Texture height
373                          GX_TEXREPEAT_NONE,     // Texture repeat
374                          GX_TEXFLIP_NONE,       // Texture flip
375                          GX_TEXPLTTCOLOR0_USE,  // Palette color
376                          myTexAddr);   // Texture address
377         G3_PolygonAttr(GX_LIGHTMASK_0, // Light
378                        GX_POLYGONMODE_MODULATE, // Polygon mode
379                        GX_CULL_NONE,   // Culling
380                        0,              // Polygon ID
381                        31,             // alpha
382                        GX_POLYGON_ATTR_MISC_NONE);      // Misc
383 
384         // Draw cube
385         DrawCube();
386 
387         G3_PopMtx(1);
388 
389         // Swapping the polygon list RAM, the vertex RAM, etc.
390         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
391 
392         // Wait for V-Blank
393         OS_WaitVBlankIntr();
394 #ifdef SDK_AUTOTEST                    // Code for auto-test
395         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
396         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
397         EXT_TestScreenShot(100, 0x8C9F5E42);
398         EXT_TestTickCounter();
399 #endif //SDK_AUTOTEST
400     }
401 }
402