1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/3D_Pol_Tex_Decal
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 DECAL polygons with an alpha texture:
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. Prepare to use decal polygons by G3_PolygonAttr(...,  GX_POLYGONMODE_DECAL, ...).
29 // 6. The texcoords are specified by G3_TexCoord().
30 //---------------------------------------------------------------------------
31 
32 #ifdef SDK_TWL
33 #include <twl.h>
34 #else
35 #include <nitro.h>
36 #endif
37 #include "DEMO.h"
38 #include "tex_alpha.h"
39 
40 
41 s16     gCubeGeometry[3 * 4] = {
42     FX16_ONE, FX16_ONE, 0,
43     FX16_ONE, -FX16_ONE, 0,
44     -FX16_ONE, FX16_ONE, 0,
45     -FX16_ONE, -FX16_ONE, 0
46 };
47 
48 VecFx10 gCubeNormal[1] = {
49     GX_VECFX10(0, 0, FX32_ONE - 1),
50 };
51 
52 GXSt    gCubeTexCoord[] = {
53     GX_ST(0, 0),
54     GX_ST(0, 16 * FX32_ONE),
55     GX_ST(16 * FX32_ONE, 0),
56     GX_ST(16 * FX32_ONE, 16 * FX32_ONE)
57 };
58 
59 
vtx(int idx)60 inline void vtx(int idx)
61 {
62     G3_Vtx(gCubeGeometry[idx * 3], gCubeGeometry[idx * 3 + 1], gCubeGeometry[idx * 3 + 2]);
63 }
64 
normal(int idx)65 inline void normal(int idx)
66 {
67     G3_Direct1(G3OP_NORMAL, gCubeNormal[idx]);
68     // use G3_Normal(x, y, z) if not packed
69 }
70 
tex_coord(int idx)71 inline void tex_coord(int idx)
72 {
73     G3_Direct1(G3OP_TEXCOORD, gCubeTexCoord[idx]);
74     // use G3_TexCoord if not packed yet
75 }
76 
77 
78 #ifdef SDK_TWL
TwlMain(void)79 void TwlMain(void)
80 #else
81 void NitroMain(void)
82 #endif
83 {
84     u16     Rotate = 0;                // for rotating cubes(0-65535)
85     u32     myTexAddr = 0x01000;       // a texture image at 0x1000 of the texture image slots
86     u32     myTexPlttAddr = 0x01000;   // a texture palette at 0x1000 of the texture palette slots
87 
88     //---------------------------------------------------------------------------
89     // Initialize:
90     // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
91     //---------------------------------------------------------------------------
92     DEMOInitCommon();
93     DEMOInitVRAM();
94     DEMOInitDisplay3D();
95 
96     G3X_AlphaBlend(TRUE);              // enable alpha blending
97 
98 
99     //---------------------------------------------------------------------------
100     // Download the texture images:
101     //
102     // Transfer the texture data on the main memory to the texture image slots.
103     //---------------------------------------------------------------------------
104     GX_BeginLoadTex();                 // map the texture image slots onto LCDC address space
105     {
106         GX_LoadTex((void *)&tex_alpha[0],       // a pointer to the texture data on the main memory(4 bytes aligned)
107                    myTexAddr,          // an offset address in the texture image slots
108                    256                 // the size of the texture(s)(in bytes)
109             );
110     }
111     GX_EndLoadTex();                   // restore the texture image slots
112 
113     //---------------------------------------------------------------------------
114     // Download the texture palettes:
115     //
116     // Transfer the texture palette data on the main memory to the texture palette slots.
117     //---------------------------------------------------------------------------
118     GX_BeginLoadTexPltt();             // map the texture palette slots onto LCDC address space
119     {
120         GX_LoadTexPltt((void *)&pal_alpha[0],   // a pointer to the texture data on the main memory(4 bytes aligned)
121                        myTexPlttAddr,  // an offset address in the texture palette slots
122                        16);            // the size of the texture palette(s)(in bytes)
123     }
124     GX_EndLoadTexPltt();               // restore the texture palette slots
125 
126     DEMOStartDisplay();
127     while (1)
128     {
129         G3X_Reset();
130         Rotate += 256;
131 
132         {
133             G3_MtxMode(GX_MTXMODE_TEXTURE);
134             G3_Identity();
135             // Use an identity matrix for the texture matrix for simplicity
136             G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
137         }
138 
139         //---------------------------------------------------------------------------
140         // Set up a camera matrix
141         //---------------------------------------------------------------------------
142         {
143             VecFx32 Eye = { 0, 0, FX32_ONE };   // Eye position
144             VecFx32 at = { 0, 0, 0 };  // Viewpoint
145             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
146 
147             G3_LookAt(&Eye, &vUp, &at, NULL);
148         }
149 
150         //---------------------------------------------------------------------------
151         // Set up light colors and direction.
152         // Notice that light vector is transformed by the current vector matrix
153         // immediately after LightVector command is issued.
154         //
155         // GX_LIGHTID_0: white, downward
156         //---------------------------------------------------------------------------
157         G3_LightVector(GX_LIGHTID_0, FX16_SQRT1_3, -FX16_SQRT1_3, -FX16_SQRT1_3);
158         G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
159 
160         G3_PushMtx();
161         G3_Translate(0, 0, -3 * FX32_ONE);
162 
163         //---------------------------------------------------------------------------
164         // Modulation polygons with an alpha texture
165         //---------------------------------------------------------------------------
166 
167         G3_PushMtx();
168         {
169             fx16    s = FX_SinIdx(Rotate);
170             fx16    c = FX_CosIdx(Rotate);
171 
172             G3_Translate(-3 * (FX32_ONE >> 1), 0, 0);
173             G3_RotZ(s, c);
174         }
175 
176         // Set the material color( diffuse, ambient , specular ) as basic white
177         DEMO_Set3DDefaultMaterial(TRUE, TRUE);
178         DEMO_Set3DDefaultShininessTable();
179 
180 
181         G3_TexImageParam(GX_TEXFMT_ALPHA,       // use alpha texture
182                          GX_TEXGEN_TEXCOORD,    // use texcoord
183                          GX_TEXSIZE_S16,        // 16 pixels
184                          GX_TEXSIZE_T16,        // 16 pixels
185                          GX_TEXREPEAT_NONE,     // no repeat
186                          GX_TEXFLIP_NONE,       // no flip
187                          GX_TEXPLTTCOLOR0_USE,  // use color 0 of the palette
188                          myTexAddr     // the offset of the texture image
189             );
190 
191         G3_TexPlttBase(myTexPlttAddr,  // the offset of the texture palette
192                        GX_TEXFMT_ALPHA // alpha texture
193             );
194 
195         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
196                        GX_POLYGONMODE_MODULATE, // modulation mode
197                        GX_CULL_NONE,   // cull none
198                        0,              // polygon ID(0 - 63)
199                        16,             // alpha(0 - 31)
200                        0               // OR of GXPolygonAttrMisc's value
201             );
202 
203         G3_Begin(GX_BEGIN_QUADS);
204         {
205             tex_coord(1);
206             normal(0);
207             vtx(1);
208             tex_coord(0);
209             normal(0);
210             vtx(0);
211             tex_coord(2);
212             normal(0);
213             vtx(2);
214             tex_coord(3);
215             normal(0);
216             vtx(3);
217         }
218         G3_End();
219 
220         G3_PopMtx(1);
221 
222         //---------------------------------------------------------------------------
223         // Decal polygons with an alpha texture
224         //---------------------------------------------------------------------------
225 
226         G3_PushMtx();
227         {
228             fx16    s = FX_SinIdx(Rotate);
229             fx16    c = FX_CosIdx(Rotate);
230 
231             G3_Translate(3 * (FX32_ONE >> 1), 0, 0);
232             G3_RotZ(s, c);
233         }
234 
235         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // diffuse
236                                 GX_RGB(16, 16, 16),     // ambient
237                                 TRUE   // use diffuse as vtx color if TRUE
238             );
239 
240         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
241                                 GX_RGB(0, 0, 0),        // emission
242                                 FALSE  // use shininess table if TRUE
243             );
244 
245         G3_TexImageParam(GX_TEXFMT_ALPHA,       // use alpha texture
246                          GX_TEXGEN_TEXCOORD,    // use texcoord
247                          GX_TEXSIZE_S16,        // 16 pixels
248                          GX_TEXSIZE_T16,        // 16 pixels
249                          GX_TEXREPEAT_NONE,     // no repeat
250                          GX_TEXFLIP_NONE,       // no flip
251                          GX_TEXPLTTCOLOR0_USE,  // use color 0 of the palette
252                          myTexAddr     // the offset of the texture image
253             );
254 
255         G3_TexPlttBase(myTexPlttAddr,  // the offset of the texture palette
256                        GX_TEXFMT_ALPHA // alpha texture
257             );
258 
259         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
260                        GX_POLYGONMODE_DECAL,    // decal mode
261                        GX_CULL_NONE,   // cull none
262                        0,              // polygon ID(0 - 63)
263                        16,             // alpha(0 - 31)
264                        0               // OR of GXPolygonAttrMisc's value
265             );
266 
267         G3_Begin(GX_BEGIN_QUADS);
268         {
269             tex_coord(1);
270             normal(0);
271             vtx(1);
272             tex_coord(0);
273             normal(0);
274             vtx(0);
275             tex_coord(2);
276             normal(0);
277             vtx(2);
278             tex_coord(3);
279             normal(0);
280             vtx(3);
281         }
282         G3_End();
283 
284         G3_PopMtx(1);
285 
286         //---------------------------------------------------------------------------
287         // The squares behind modulation/decal polygons
288         //---------------------------------------------------------------------------
289 
290         G3_PushMtx();
291         {
292             fx16    s = FX_SinIdx(Rotate);
293             fx16    c = FX_CosIdx(Rotate);
294 
295             G3_Translate(0, 0, -10 * FX32_ONE);
296             G3_RotZ((fx16)-s, c);
297         }
298 
299         G3_MaterialColorDiffAmb(GX_RGB(31, 0, 0),       // diffuse
300                                 GX_RGB(31, 0, 0),       // ambient
301                                 TRUE   // use diffuse as vtx color if TRUE
302             );
303 
304         G3_MaterialColorSpecEmi(GX_RGB(31, 0, 0),       // specular
305                                 GX_RGB(0, 0, 0),        // emission
306                                 FALSE  // use shininess table if TRUE
307             );
308 
309         G3_TexImageParam(GX_TEXFMT_NONE,        // no texture
310                          (GXTexGen)0,  // dummy
311                          (GXTexSizeS)0, // dummy
312                          (GXTexSizeT)0, // dummy
313                          (GXTexRepeat)0,        // dummy
314                          (GXTexFlip)0, // dummy
315                          (GXTexPlttColor0)0,    // dummy
316                          0             // dummy
317             );
318 
319         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
320                        GX_POLYGONMODE_MODULATE, // modulation mode
321                        GX_CULL_NONE,   // cull none
322                        1,              // polygon ID(0 - 63)
323                        31,             // alpha(0 - 31)
324                        0               // OR of GXPolygonAttrMisc's value
325             );
326 
327         G3_Begin(GX_BEGIN_QUADS);
328         {
329             normal(0);
330             G3_Vtx(4 * FX16_ONE, 0, 0);
331             G3_Vtx(4 * FX16_ONE, 4 * FX16_ONE, 0);
332             G3_Vtx(0, 4 * FX16_ONE, 0);
333             G3_Vtx(0, 0, 0);
334         }
335         G3_End();
336 
337         G3_MaterialColorDiffAmb(GX_RGB(0, 31, 0),       // diffuse
338                                 GX_RGB(0, 31, 0),       // ambient
339                                 TRUE   // use diffuse as vtx color if TRUE
340             );
341 
342         G3_MaterialColorSpecEmi(GX_RGB(0, 31, 0),       // specular
343                                 GX_RGB(0, 0, 0),        // emission
344                                 FALSE  // use shininess table if TRUE
345             );
346 
347         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
348                        GX_POLYGONMODE_MODULATE, // modulation mode
349                        GX_CULL_NONE,   // cull none
350                        2,              // polygon ID(0 - 63)
351                        31,             // alpha(0 - 31)
352                        0               // OR of GXPolygonAttrMisc's value
353             );
354 
355         G3_Begin(GX_BEGIN_QUADS);
356         {
357             normal(0);
358             G3_Vtx(0, 0, 0);
359             G3_Vtx(0, 4 * FX16_ONE, 0);
360             G3_Vtx(-4 * FX16_ONE, 4 * FX16_ONE, 0);
361             G3_Vtx(-4 * FX16_ONE, 0, 0);
362         }
363         G3_End();
364 
365         G3_MaterialColorDiffAmb(GX_RGB(0, 0, 31),       // diffuse
366                                 GX_RGB(0, 0, 31),       // ambient
367                                 TRUE   // use diffuse as vtx color if TRUE
368             );
369 
370         G3_MaterialColorSpecEmi(GX_RGB(0, 0, 31),       // specular
371                                 GX_RGB(0, 0, 0),        // emission
372                                 FALSE  // use shininess table if TRUE
373             );
374 
375         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
376                        GX_POLYGONMODE_MODULATE, // modulation mode
377                        GX_CULL_NONE,   // cull none
378                        3,              // polygon ID(0 - 63)
379                        31,             // alpha(0 - 31)
380                        0               // OR of GXPolygonAttrMisc's value
381             );
382 
383         G3_Begin(GX_BEGIN_QUADS);
384         {
385             normal(0);
386             G3_Vtx(0, -4 * FX16_ONE, 0);
387             G3_Vtx(0, 0, 0);
388             G3_Vtx(-4 * FX16_ONE, 0, 0);
389             G3_Vtx(-4 * FX16_ONE, -4 * FX16_ONE, 0);
390         }
391         G3_End();
392 
393         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 0),      // diffuse
394                                 GX_RGB(31, 31, 0),      // ambient
395                                 TRUE   // use diffuse as vtx color if TRUE
396             );
397 
398         G3_MaterialColorSpecEmi(GX_RGB(31, 31, 0),      // specular
399                                 GX_RGB(0, 0, 0),        // emission
400                                 FALSE  // use shininess table if TRUE
401             );
402 
403         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
404                        GX_POLYGONMODE_MODULATE, // modulation mode
405                        GX_CULL_NONE,   // cull none
406                        4,              // polygon ID(0 - 63)
407                        31,             // alpha(0 - 31)
408                        0               // OR of GXPolygonAttrMisc's value
409             );
410 
411         G3_Begin(GX_BEGIN_QUADS);
412         {
413             normal(0);
414             G3_Vtx(4 * FX16_ONE, -4 * FX16_ONE, 0);
415             G3_Vtx(4 * FX16_ONE, 0, 0);
416             G3_Vtx(0, 0, 0);
417             G3_Vtx(0, -4 * FX16_ONE, 0);
418         }
419         G3_End();
420 
421         G3_PopMtx(1);
422 
423         G3_PopMtx(1);
424 
425         // swapping the polygon list RAM, the vertex RAM, etc.
426         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
427 
428 #ifdef SDK_AUTOTEST
429         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
430         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
431         EXT_TestScreenShot(100, 0x5B9F61B);
432         EXT_TestTickCounter();
433 #endif //SDK_AUTOTEST
434 
435         OS_WaitVBlankIntr();           // Waiting the end of VBlank interrupt
436     }
437 }
438 
439 //---------------------------------------------------------------------------
440 // VBlank interrupt function:
441 //
442 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
443 // OS_EnableIrqMask selects IRQ interrupts to enable, and
444 // OS_EnableIrq enables IRQ interrupts.
445 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
446 //---------------------------------------------------------------------------
VBlankIntr(void)447 void VBlankIntr(void)
448 {
449     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
450 }
451