1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/3D_Pol_Tex32768
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 //---------------------------------------------------------------------------
19 // A sample that use a direct texture:
20 //
21 // Note that direct textures do not require texture palettes.
22 //
23 // HOWTO:
24 // 1. Load texture images to the texture image slots by
25 //    GX_BeginLoadTex(), GX_LoadTex(), and GX_EndLoadTex().
26 // 2. Specify the texture to use and etc. by G3_TexImageParam(GX_TEXFMT_DIRECT, ...).
27 // 3. The texcoords are specified by G3_TexCoord().
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 s16     gCubeGeometry[3 * 8] = {
39     FX16_ONE, FX16_ONE, FX16_ONE,
40     FX16_ONE, FX16_ONE, -FX16_ONE,
41     FX16_ONE, -FX16_ONE, FX16_ONE,
42     FX16_ONE, -FX16_ONE, -FX16_ONE,
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 };
48 
49 VecFx10 gCubeNormal[6] = {
50     GX_VECFX10(0, 0, FX32_ONE - 1),
51     GX_VECFX10(0, FX32_ONE - 1, 0),
52     GX_VECFX10(FX32_ONE - 1, 0, 0),
53     GX_VECFX10(0, 0, -FX32_ONE + 1),
54     GX_VECFX10(0, -FX32_ONE + 1, 0),
55     GX_VECFX10(-FX32_ONE + 1, 0, 0)
56 };
57 
58 GXSt    gCubeTexCoord[] = {
59     GX_ST(0, 0),
60     GX_ST(0, 64 * FX32_ONE),
61     GX_ST(64 * FX32_ONE, 0),
62     GX_ST(64 * FX32_ONE, 64 * FX32_ONE)
63 };
64 
65 
vtx(int idx)66 inline void vtx(int idx)
67 {
68     G3_Vtx(gCubeGeometry[idx * 3], gCubeGeometry[idx * 3 + 1], gCubeGeometry[idx * 3 + 2]);
69 }
70 
normal(int idx)71 inline void normal(int idx)
72 {
73     G3_Direct1(G3OP_NORMAL, gCubeNormal[idx]);
74     // use G3_Normal(x, y, z) if not packed
75 }
76 
tex_coord(int idx)77 inline void tex_coord(int idx)
78 {
79     G3_Direct1(G3OP_TEXCOORD, gCubeTexCoord[idx]);
80     // use G3_TexCoord if not packed yet
81 }
82 
83 #ifdef SDK_TWL
TwlMain(void)84 void TwlMain(void)
85 #else
86 void NitroMain(void)
87 #endif
88 {
89     u16     Rotate = 0;                // for rotating cubes(0-65535)
90     u32     myTexAddr = 0x01000;       // a texture image at 0x1000 of the texture image slots
91 
92     //---------------------------------------------------------------------------
93     // Initialize:
94     // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
95     //---------------------------------------------------------------------------
96     DEMOInitCommon();
97     DEMOInitVRAM();
98     DEMOInitDisplay3D();
99 
100 
101     //---------------------------------------------------------------------------
102     // Download texture images:
103     //
104     // Transfer the texture data on the main memory to the texture image slots.
105     //---------------------------------------------------------------------------
106     GX_BeginLoadTex();                 // map the texture image slots onto LCDC address space
107     {
108         GX_LoadTex((void *)&tex_32768_64x64[0], // a pointer to the texture data on the main memory(4 bytes aligned)
109                    myTexAddr,          // an offset address in the texture image slots
110                    8192                // the size of the texture(s)(in bytes)
111             );
112     }
113     GX_EndLoadTex();                   // restore the texture image slots
114 
115     // No texture palettes needed
116 
117     DEMOStartDisplay();
118 
119     while (1)
120     {
121         G3X_Reset();
122         Rotate += 256;
123 
124         //---------------------------------------------------------------------------
125         // Set up a camera matrix
126         //---------------------------------------------------------------------------
127         {
128             VecFx32 Eye = { 0, 0, FX32_ONE };   // Eye position
129             VecFx32 at = { 0, 0, 0 };  // Viewpoint
130             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
131 
132             G3_LookAt(&Eye, &vUp, &at, NULL);
133         }
134 
135         //---------------------------------------------------------------------------
136         // Set up light colors and direction.
137         // Notice that light vector is transformed by the current vector matrix
138         // immediately after LightVector command is issued.
139         //
140         // GX_LIGHTID_0: white, downward
141         //---------------------------------------------------------------------------
142         G3_LightVector(GX_LIGHTID_0, FX16_SQRT1_3, -FX16_SQRT1_3, -FX16_SQRT1_3);
143         G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
144 
145         G3_PushMtx();
146 
147         {
148             fx16    s = FX_SinIdx(Rotate);
149             fx16    c = FX_CosIdx(Rotate);
150 
151             G3_Translate(0, 0, -5 * FX32_ONE);
152 
153             G3_RotX(s, c);
154             G3_RotY(s, c);
155             G3_RotZ(s, c);
156         }
157 
158         {
159             G3_MtxMode(GX_MTXMODE_TEXTURE);
160             G3_Identity();
161             // Use an identity matrix for the texture matrix for simplicity
162             G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
163         }
164 
165         // Set the material color( diffuse, ambient , specular ) as basic white
166         DEMO_Set3DDefaultMaterial(TRUE, TRUE);
167         DEMO_Set3DDefaultShininessTable();
168 
169         G3_TexImageParam(GX_TEXFMT_DIRECT,      // use direct texture
170                          GX_TEXGEN_TEXCOORD,    // use texcoord
171                          GX_TEXSIZE_S64,        // 64 pixels
172                          GX_TEXSIZE_T64,        // 64 pixels
173                          GX_TEXREPEAT_NONE,     // no repeat
174                          GX_TEXFLIP_NONE,       // no flip
175                          GX_TEXPLTTCOLOR0_USE,  // use color 0 of the palette
176                          myTexAddr     // the offset of the texture image
177             );
178 
179         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
180                        GX_POLYGONMODE_MODULATE, // modulation mode
181                        GX_CULL_NONE,   // cull none
182                        0,              // polygon ID(0 - 63)
183                        31,             // alpha(0 - 31)
184                        0               // OR of GXPolygonAttrMisc's value
185             );
186 
187         G3_Begin(GX_BEGIN_QUADS);
188 
189         {
190             tex_coord(1);
191             normal(0);
192             vtx(2);
193             tex_coord(0);
194             normal(0);
195             vtx(0);
196             tex_coord(2);
197             normal(0);
198             vtx(4);
199             tex_coord(3);
200             normal(0);
201             vtx(6);
202 
203             tex_coord(1);
204             normal(3);
205             vtx(7);
206             tex_coord(0);
207             normal(3);
208             vtx(5);
209             tex_coord(2);
210             normal(3);
211             vtx(1);
212             tex_coord(3);
213             normal(3);
214             vtx(3);
215 
216             tex_coord(1);
217             normal(5);
218             vtx(6);
219             tex_coord(0);
220             normal(5);
221             vtx(4);
222             tex_coord(2);
223             normal(5);
224             vtx(5);
225             tex_coord(3);
226             normal(5);
227             vtx(7);
228 
229             tex_coord(1);
230             normal(2);
231             vtx(3);
232             tex_coord(0);
233             normal(2);
234             vtx(1);
235             tex_coord(2);
236             normal(2);
237             vtx(0);
238             tex_coord(3);
239             normal(2);
240             vtx(2);
241 
242             tex_coord(1);
243             normal(1);
244             vtx(5);
245             tex_coord(0);
246             normal(1);
247             vtx(4);
248             tex_coord(2);
249             normal(1);
250             vtx(0);
251             tex_coord(3);
252             normal(1);
253             vtx(1);
254 
255             tex_coord(1);
256             normal(4);
257             vtx(6);
258             tex_coord(0);
259             normal(4);
260             vtx(7);
261             tex_coord(2);
262             normal(4);
263             vtx(3);
264             tex_coord(3);
265             normal(4);
266             vtx(2);
267         }
268         G3_End();
269 
270         G3_PopMtx(1);
271 
272         // swapping the polygon list RAM, the vertex RAM, etc.
273         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
274 
275 #ifdef SDK_AUTOTEST
276         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
277         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
278         EXT_TestScreenShot(100, 0xE26F8D9A);
279         EXT_TestTickCounter();
280 #endif //SDK_AUTOTEST
281 
282         OS_WaitVBlankIntr();           // Waiting the end of VBlank interrupt
283     }
284 }
285 
286 //---------------------------------------------------------------------------
287 // VBlank interrupt function:
288 //
289 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
290 // OS_EnableIrqMask selects IRQ interrupts to enable, and
291 // OS_EnableIrq enables IRQ interrupts.
292 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
293 //---------------------------------------------------------------------------
VBlankIntr(void)294 void VBlankIntr(void)
295 {
296     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
297 }
298