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