1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/3D_Pol_Simple3
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 // A sample that use GX_VECVTX10(G3_Vtx10):
20 //
21 // You can reduce the amount of the data transferred to the FIFO by G3_Vtx10.
22 // Since it uses a 3D vector in s3.6 format, precision becomes lower than G3_Vtx.
23 //---------------------------------------------------------------------------
24
25 #ifdef SDK_TWL
26 #include <twl.h>
27 #else
28 #include <nitro.h>
29 #endif
30 #include "DEMO.h"
31
32 //---------------------------------------------------------------------------
33 // GX_VECVTX10 takes (x, y, z) in fx32/fx16 format,
34 // and packs them into a 3D vector in s3.6 format.
35 //---------------------------------------------------------------------------
36 VecVtx10 gCubeGeometry[8] = {
37 GX_VECVTX10(FX16_ONE, FX16_ONE, FX16_ONE),
38 GX_VECVTX10(FX16_ONE, FX16_ONE, -FX16_ONE),
39 GX_VECVTX10(FX16_ONE, -FX16_ONE, FX16_ONE),
40 GX_VECVTX10(FX16_ONE, -FX16_ONE, -FX16_ONE),
41 GX_VECVTX10(-FX16_ONE, FX16_ONE, FX16_ONE),
42 GX_VECVTX10(-FX16_ONE, FX16_ONE, -FX16_ONE),
43 GX_VECVTX10(-FX16_ONE, -FX16_ONE, FX16_ONE),
44 GX_VECVTX10(-FX16_ONE, -FX16_ONE, -FX16_ONE)
45 };
46
vtx(int idx)47 static void vtx(int idx)
48 {
49 // use G3_Vtx10(x, y, z) if not packed yet
50 G3_Direct1(G3OP_VTX_10, gCubeGeometry[idx]);
51 }
52
quad(int idx0,int idx1,int idx2,int idx3)53 static void quad(int idx0, int idx1, int idx2, int idx3)
54 {
55 vtx(idx0);
56 vtx(idx1);
57 vtx(idx2);
58 vtx(idx3);
59 }
60
61 #ifdef SDK_TWL
TwlMain(void)62 void TwlMain(void)
63 #else
64 void NitroMain(void)
65 #endif
66 {
67 u16 Rotate = 0; // for rotating cubes(0-65535)
68 int idx = 0;
69
70 //---------------------------------------------------------------------------
71 // Initialize:
72 // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
73 //---------------------------------------------------------------------------
74 DEMOInitCommon();
75 DEMOInitVRAM();
76 DEMOInitDisplay3D();
77
78 DEMOStartDisplay();
79 while (1)
80 {
81 G3X_Reset();
82 Rotate += 256;
83
84 {
85 VecFx32 Eye = { 0, 0, FX32_ONE }; // Eye position
86 VecFx32 at = { 0, 0, 0 }; // Viewpoint
87 VecFx32 vUp = { 0, FX32_ONE, 0 }; // Up
88
89 // Matrix mode is changed to GX_MTXMODE_POSITION_VECTOR internally,
90 // and the camera matrix is loaded to the current matrix.
91 G3_LookAt(&Eye, &vUp, &at, NULL);
92 }
93
94 G3_PushMtx();
95
96 {
97 fx16 s = FX_SinIdx(Rotate);
98 fx16 c = FX_CosIdx(Rotate);
99
100 G3_Translate(0, 0, -5 * FX32_ONE);
101
102 G3_RotX(s, c);
103 G3_RotY(s, c);
104 G3_RotZ(s, c);
105
106 // The current matrix is Rz * Ry * Rx * Trns * Cam.
107 }
108
109 G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31), // diffuse
110 GX_RGB(16, 16, 16), // ambient
111 TRUE // use diffuse as vtx color if TRUE
112 );
113
114 G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16), // specular
115 GX_RGB(0, 0, 0), // emission
116 FALSE // use shininess table if TRUE
117 );
118
119 G3_PolygonAttr(GX_LIGHTMASK_NONE, // no lights
120 GX_POLYGONMODE_MODULATE, // modulation mode
121 GX_CULL_BACK, // cull back
122 0, // polygon ID(0 - 63)
123 31, // alpha(0 - 31)
124 0 // OR of GXPolygonAttrMisc's value
125 );
126
127 G3_Begin(GX_BEGIN_QUADS);
128
129 {
130 quad(2, 0, 4, 6);
131 quad(7, 5, 1, 3);
132 quad(6, 4, 5, 7);
133 quad(3, 1, 0, 2);
134 quad(5, 4, 0, 1);
135 quad(6, 7, 3, 2);
136 }
137
138 G3_End();
139
140 G3_PopMtx(1);
141
142 // swapping the polygon list RAM, the vertex RAM, etc.
143 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
144
145 #ifdef SDK_AUTOTEST
146 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
147 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
148 EXT_TestScreenShot(100, 0xF34A239D);
149 EXT_TestTickCounter();
150 #endif //SDK_AUTOTEST
151
152 OS_WaitVBlankIntr(); // Waiting the end of VBlank interrupt
153 }
154 }
155
156
157 //---------------------------------------------------------------------------
158 // VBlank interrupt function:
159 //
160 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
161 // OS_EnableIrqMask selects IRQ interrupts to enable, and
162 // OS_EnableIrq enables IRQ interrupts.
163 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
164 //---------------------------------------------------------------------------
VBlankIntr(void)165 void VBlankIntr(void)
166 {
167 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
168 }
169