1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/3D_Command_Pack
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 //---------------------------------------------------------------------------
19 // Use the command pack data generated in advance:
20 //
21 // Send the data by DMA/CPU transfer, and display a sphere rotating and lighted.
22 //---------------------------------------------------------------------------
23 
24 #ifdef SDK_TWL
25 #include <twl.h>
26 #else
27 #include <nitro.h>
28 #endif
29 #include "DEMO.h"
30 #include "commandList.h"
31 
32 #ifdef SDK_TWL
TwlMain(void)33 void TwlMain(void)
34 #else
35 void NitroMain(void)
36 #endif
37 {
38     u16     count = 0;
39     u16     Rotate = 0;                // for rotating spheresa(0-65535)
40 
41     //---------------------------------------------------------------------------
42     // Initialize:
43     // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
44     //---------------------------------------------------------------------------
45     DEMOInitCommon();
46     DEMOInitVRAM();
47     DEMOInitDisplay3D();
48 
49     DEMOStartDisplay();
50     while (1)
51     {
52         G3X_Reset();
53         count = (u16)((count + 1) & 0x00ff);
54         Rotate += 256;
55 
56         //---------------------------------------------------------------------------
57         // Set up camera matrix
58         //---------------------------------------------------------------------------
59         {
60             VecFx32 Eye = { 0, 0, FX32_ONE * 5 };       // Eye Position
61             VecFx32 at = { 0, 0, 0 };  // Viewpoint
62             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
63 
64             G3_LookAt(&Eye, &vUp, &at, NULL);
65         }
66 
67         //---------------------------------------------------------------------------
68         // Set up light colors and direction.
69         // Notice that light vector is transformed by the current vector matrix
70         // immediately after LightVector command is issued.
71         //
72         // GX_LIGHTID_0: white, downward
73         // GX_LIGHTID_1: red, leftward
74         // GX_LIGHTID_2: green, upward
75         // GX_LIGHTID_3: blue, rightward
76         //---------------------------------------------------------------------------
77         G3_LightVector(GX_LIGHTID_0, 0, (fx16)-FX32_SQRT1_2, (fx16)-FX32_SQRT1_2);
78         G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
79 
80         G3_LightVector(GX_LIGHTID_1, (fx16)-FX32_SQRT1_2, 0, (fx16)-FX32_SQRT1_2);
81         G3_LightColor(GX_LIGHTID_1, GX_RGB(31, 0, 0));
82 
83         G3_LightVector(GX_LIGHTID_2, 0, (fx16)FX32_SQRT1_2, (fx16)-FX32_SQRT1_2);
84         G3_LightColor(GX_LIGHTID_2, GX_RGB(0, 31, 0));
85 
86         G3_LightVector(GX_LIGHTID_3, (fx16)FX32_SQRT1_2, 0, (fx16)-FX32_SQRT1_2);
87         G3_LightColor(GX_LIGHTID_3, GX_RGB(0, 0, 31));
88 
89         G3_PushMtx();
90         G3_Translate(0, 0, -2 * FX32_ONE);
91 
92         //---------------------------------------------------------------------------
93         // Rotate a sphere
94         //---------------------------------------------------------------------------
95         G3_PushMtx();
96         {
97             fx16    s = FX_SinIdx(Rotate);
98             fx16    c = FX_CosIdx(Rotate);
99 
100             G3_RotX(s, c);
101             G3_RotY(s, c);
102             G3_RotZ(s, c);
103         }
104 
105         // Set the material color( diffuse, ambient , specular ) as basic white
106         DEMO_Set3DDefaultMaterial(TRUE, TRUE);
107         DEMO_Set3DDefaultShininessTable();
108 
109         G3_PolygonAttr((GXLightMask)(count >> 4),       // no lights
110                        GX_POLYGONMODE_MODULATE, // modulation mode
111                        GX_CULL_BACK,   // cull back
112                        0,              // polygon ID(0 - 63)
113                        31,             // alpha(0 - 31)
114                        0               // OR of GXPolygonAttrMisc's value
115             );
116 
117         //---------------------------------------------------------------------------
118         // Draw a sphere:
119         // The geometry data for a sphere is generated in advance(commandList.c).
120         // Send them to the geometry FIFO by DMA/CPU, and display a sphere.
121         //---------------------------------------------------------------------------
122         G3_Begin(GX_BEGIN_TRIANGLES);
123 
124 #if 1
125         // Transfer by DMA
126         MI_SendGXCommand(3, sphere1, sphere_size);
127 
128 #else
129         // Transfer by CPU
130         {
131             int     i;
132             for (i = 0; i < sphere_size / 4; ++i)
133             {
134                 reg_G3X_GXFIFO = sphere1[i];
135             }
136         }
137 #endif
138         G3_End();
139 
140         G3_PopMtx(1);
141 
142         G3_PopMtx(1);
143 
144         // swapping the polygon list RAM, the vertex RAM, etc.
145         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
146 
147 #ifdef SDK_AUTOTEST
148         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
149         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
150         EXT_TestScreenShot(100, 0xE405BA9B);
151         EXT_TestTickCounter();
152 #endif //SDK_AUTOTEST
153 
154         OS_WaitVBlankIntr();           // Waiting the end of VBlank interrupt
155     }
156 }
157 
158 
159 //---------------------------------------------------------------------------
160 // VBlank interrupt function:
161 //
162 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
163 // OS_EnableIrqMask selects IRQ interrupts to enable, and
164 // OS_EnableIrq enables IRQ interrupts.
165 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
166 //---------------------------------------------------------------------------
VBlankIntr(void)167 void VBlankIntr(void)
168 {
169     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
170 }
171