1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/3D_Pol_Tex_Alpha
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:: 2009-01-15#$
14   $Rev: 9860 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 //---------------------------------------------------------------------------
19 // A sample that use a alpha texture with a 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_ALPHA, ...).
27 // 4. Specify the palette to use by G3_TexPlttBase(..., GX_TEXFMT_ALPHA).
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_alpha.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, 16 * FX32_ONE),
62     GX_ST(16 * FX32_ONE, 0),
63     GX_ST(16 * FX32_ONE, 16 * 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
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 #ifdef SDK_TWL
TwlMain(void)85 void TwlMain(void)
86 #else
87 void NitroMain(void)
88 #endif
89 {
90     u16     Rotate = 0;                // for rotating cubes(0-65535)
91     u32     myTexAddr = 0x01000;       // a texture image at 0x1000 of the texture image slots
92     u32     myTexPlttAddr = 0x01000;   // a texture palette at 0x1000 of the texture palette slots
93 
94     //---------------------------------------------------------------------------
95     // Initialize:
96     // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
97     //---------------------------------------------------------------------------
98     DEMOInitCommon();
99     DEMOInitVRAM();
100     DEMOInitDisplay3D();
101 
102     //---------------------------------------------------------------------------
103     // Download the texture images:
104     //
105     // Transfer the texture data on the main memory to the texture image slots.
106     //---------------------------------------------------------------------------
107     GX_BeginLoadTex();                 // map the texture image slots onto LCDC address space
108     {
109         GX_LoadTex((void *)&tex_alpha[0],       // a pointer to the texture data on the main memory(4 bytes aligned)
110                    myTexAddr,          // an offset address in the texture image slots
111                    256                 // the size of the texture(s)(in bytes)
112             );
113     }
114     GX_EndLoadTex();                   // restore the texture image slots
115 
116     //---------------------------------------------------------------------------
117     // Download the texture palettes:
118     //
119     // Transfer the texture palette data on the main memory to the texture palette slots.
120     //---------------------------------------------------------------------------
121     GX_BeginLoadTexPltt();             // map the texture palette slots onto LCDC address space
122     {
123         GX_LoadTexPltt((void *)&pal_alpha[0],   // a pointer to the texture data on the main memory(4 bytes aligned)
124                        myTexPlttAddr,  // an offset address in the texture palette slots
125                        16);            // the size of the texture palette(s)(in bytes)
126     }
127     GX_EndLoadTexPltt();               // restore the texture palette slots
128 
129     DEMOStartDisplay();
130     while (1)
131     {
132         G3X_Reset();
133         Rotate += 256;
134 
135         //---------------------------------------------------------------------------
136         // Set up a camera matrix
137         //---------------------------------------------------------------------------
138         {
139             VecFx32 Eye = { 0, 0, FX32_ONE };   // Eye position
140             VecFx32 at = { 0, 0, 0 };  // Viewpoint
141             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
142 
143             G3_LookAt(&Eye, &vUp, &at, NULL);
144         }
145 
146         //---------------------------------------------------------------------------
147         // Set up light colors and direction
148         // Notice that light vector is transformed by the current vector matrix
149         // immediately after LightVector command is issued.
150         //
151         // GX_LIGHTID_0: white, downward
152         //---------------------------------------------------------------------------
153         G3_LightVector(GX_LIGHTID_0, FX16_SQRT1_3, -FX16_SQRT1_3, -FX16_SQRT1_3);
154         G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
155 
156         G3_PushMtx();
157 
158         {
159             fx16    s = FX_SinIdx(Rotate);
160             fx16    c = FX_CosIdx(Rotate);
161 
162             G3_Translate(0, 0, -5 * FX32_ONE);
163 
164             G3_RotX(s, c);
165             G3_RotY(s, c);
166             G3_RotZ(s, c);
167         }
168 
169         {
170             G3_MtxMode(GX_MTXMODE_TEXTURE);
171             G3_Identity();
172             // Use an identity matrix for the texture matrix for simplicity
173             G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
174         }
175 
176         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // diffuse
177                                 GX_RGB(16, 16, 16),     // ambient
178                                 TRUE   // use diffuse as vtx color if TRUE
179             );
180 
181         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
182                                 GX_RGB(0, 0, 0),        // emission
183                                 FALSE  // use shininess table if TRUE
184             );
185 
186         G3_TexImageParam(GX_TEXFMT_ALPHA,       // use alpha texture
187                          GX_TEXGEN_TEXCOORD,    // use texcoord
188                          GX_TEXSIZE_S16,        // 16 pixels
189                          GX_TEXSIZE_T16,        // 16 pixels
190                          GX_TEXREPEAT_NONE,     // no repeat
191                          GX_TEXFLIP_NONE,       // no flip
192                          GX_TEXPLTTCOLOR0_USE,  // use color 0 of the palette
193                          myTexAddr     // the offset of the texture image
194             );
195 
196         G3_TexPlttBase(myTexPlttAddr,  // the offset of the texture palette
197                        GX_TEXFMT_ALPHA // alpha texture
198             );
199 
200         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
201                        GX_POLYGONMODE_MODULATE, // modulation mode
202                        GX_CULL_NONE,   // cull none
203                        0,              // polygon ID(0 - 63)
204                        31,             // alpha(0 - 31)
205                        0               // OR of GXPolygonAttrMisc's value
206             );
207 
208         G3_Begin(GX_BEGIN_QUADS);
209 
210         {
211             tex_coord(1);
212             normal(0);
213             vtx(2);
214             tex_coord(0);
215             normal(0);
216             vtx(0);
217             tex_coord(2);
218             normal(0);
219             vtx(4);
220             tex_coord(3);
221             normal(0);
222             vtx(6);
223 
224             tex_coord(1);
225             normal(3);
226             vtx(7);
227             tex_coord(0);
228             normal(3);
229             vtx(5);
230             tex_coord(2);
231             normal(3);
232             vtx(1);
233             tex_coord(3);
234             normal(3);
235             vtx(3);
236 
237             tex_coord(1);
238             normal(5);
239             vtx(6);
240             tex_coord(0);
241             normal(5);
242             vtx(4);
243             tex_coord(2);
244             normal(5);
245             vtx(5);
246             tex_coord(3);
247             normal(5);
248             vtx(7);
249 
250             tex_coord(1);
251             normal(2);
252             vtx(3);
253             tex_coord(0);
254             normal(2);
255             vtx(1);
256             tex_coord(2);
257             normal(2);
258             vtx(0);
259             tex_coord(3);
260             normal(2);
261             vtx(2);
262 
263             tex_coord(1);
264             normal(1);
265             vtx(5);
266             tex_coord(0);
267             normal(1);
268             vtx(4);
269             tex_coord(2);
270             normal(1);
271             vtx(0);
272             tex_coord(3);
273             normal(1);
274             vtx(1);
275 
276             tex_coord(1);
277             normal(4);
278             vtx(6);
279             tex_coord(0);
280             normal(4);
281             vtx(7);
282             tex_coord(2);
283             normal(4);
284             vtx(3);
285             tex_coord(3);
286             normal(4);
287             vtx(2);
288         }
289         G3_End();
290 
291         G3_PopMtx(1);
292 
293         // swapping the polygon list RAM, the vertex RAM, etc.
294         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
295 
296 #ifdef SDK_AUTOTEST
297         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
298         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
299         EXT_TestScreenShot(100, 0x303A5872);
300         EXT_TestTickCounter();
301 #endif //SDK_AUTOTEST
302 
303         OS_WaitVBlankIntr();           // Waiting the end of VBlank interrupt
304     }
305 }
306 
307 //---------------------------------------------------------------------------
308 // VBlank interrupt function:
309 //
310 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
311 // OS_EnableIrqMask selects IRQ interrupts to enable, and
312 // OS_EnableIrq enables IRQ interrupts.
313 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
314 //---------------------------------------------------------------------------
VBlankIntr(void)315 void VBlankIntr(void)
316 {
317     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
318 }
319