1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/3D_Pol_High_Light
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 for high-light shading
20 //
21 // Display two chairs.
22 // The left one is in modulation mode, and the right one is in high-light mode.
23 //
24 // HOWTO:
25 // 1. Switch to high light shading mode by G3X_SetShading(GX_SHADING_HIGHLIGHT).
26 // 2. Set up the high-light(toon) table by G3X_SetToonTable.
27 // 3. Speciy GX_POLYGON_MODE_TOON by G3X_PolygonAttr.
28 //---------------------------------------------------------------------------
29 
30 #ifdef SDK_TWL
31 #include <twl.h>
32 #else
33 #include <nitro.h>
34 #endif
35 #include "DEMO.h"
36 #include "data.h"
37 
38 GXRgb   Toon_Table[32] = {
39     GX_RGB(0, 0, 0), GX_RGB(0, 0, 0), GX_RGB(0, 0, 0), GX_RGB(0, 0, 0),
40     GX_RGB(0, 0, 0), GX_RGB(0, 0, 0), GX_RGB(0, 0, 0), GX_RGB(0, 0, 0),
41     GX_RGB(0, 0, 0), GX_RGB(0, 0, 0), GX_RGB(0, 0, 0), GX_RGB(0, 0, 0),
42     GX_RGB(4, 4, 4), GX_RGB(4, 4, 4), GX_RGB(4, 4, 4), GX_RGB(4, 4, 4),
43     GX_RGB(16, 16, 16), GX_RGB(16, 16, 16), GX_RGB(16, 16, 16), GX_RGB(16, 16, 16),
44     GX_RGB(19, 19, 19), GX_RGB(22, 22, 22), GX_RGB(25, 25, 25), GX_RGB(28, 28, 28),
45     GX_RGB(29, 29, 29), GX_RGB(30, 30, 30), GX_RGB(31, 31, 31), GX_RGB(31, 31, 31),
46     GX_RGB(31, 31, 31), GX_RGB(31, 31, 31), GX_RGB(31, 31, 31), GX_RGB(31, 31, 31)
47 };
48 
49 
50 #ifdef SDK_TWL
TwlMain(void)51 void TwlMain(void)
52 #else
53 void NitroMain(void)
54 #endif
55 {
56     u16     Rotate = 0;                // for rotating cubes(0-65535)
57     u32     myTexAddr = 0x1000;        // texture image address in texture image slots
58     u32     myTexPlttAddr = 0x1000;    // texture palette address in texture palette slots
59 
60     //---------------------------------------------------------------------------
61     // Initialize:
62     // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
63     //---------------------------------------------------------------------------
64     DEMOInitCommon();
65     DEMOInitVRAM();
66     DEMOInitDisplay3D();
67 
68     //---------------------------------------------------------------------------
69     // Specify high-light mode
70     //---------------------------------------------------------------------------
71     G3X_SetShading(GX_SHADING_HIGHLIGHT);
72 
73     //---------------------------------------------------------------------------
74     // Download the textures:
75     //
76     // Transfer the texture data on the main memory to the texture image slots.
77     //---------------------------------------------------------------------------
78     GX_BeginLoadTex();                 // map the texture image slots onto LCDC address space
79     {
80         GX_LoadTex((void *)&tex_isu1[0],        // a pointer to the texture data on the main memory(4 bytes aligned)
81                    myTexAddr,          // an offset address in the texture image slots
82                    tex_isu1_size       // the size of the texture(s)(in bytes)
83             );
84         GX_LoadTex((void *)&tex_isu2[0],        // a pointer to the texture data on the main memory(4 bytes aligned)
85                    myTexAddr + tex_isu1_size,   // an offset address in the texture image slots
86                    tex_isu2_size       // the size of the texture(s)(in bytes)
87             );
88     }
89     GX_EndLoadTex();                   // restore the texture image slots
90 
91     GX_BeginLoadTexPltt();             // map the texture palette slots onto LCDC address space
92     {
93         GX_LoadTexPltt((void *)&pal_isu1[0],    // a pointer to the texture data on the main memory(4 bytes aligned)
94                        myTexPlttAddr,  // an offset address in the texture palette slots
95                        pal_isu1_size   // the size of the texture palette(s)(in bytes)
96             );
97         GX_LoadTexPltt((void *)&pal_isu2[0],    // a pointer to the texture data on the main memory(4 bytes aligned)
98                        myTexPlttAddr + pal_isu1_size,   // an offset address in the texture palette slots
99                        pal_isu2_size   // the size of the texture palette(s)(in bytes)
100             );
101     }
102     GX_EndLoadTexPltt();               // restore the texture palette slots
103 
104 
105     //---------------------------------------------------------------------------
106     // Set up the high light(toon) table
107     //---------------------------------------------------------------------------
108     G3X_SetToonTable(Toon_Table);
109 
110     DEMOStartDisplay();
111     while (1)
112     {
113         G3X_Reset();
114         Rotate += 256;
115 
116         //---------------------------------------------------------------------------
117         // Set up camera matrix
118         //---------------------------------------------------------------------------
119         {
120             VecFx32 Eye = { 0, 0, FX32_ONE };   // Eye Position
121             VecFx32 at = { 0, 0, 0 };  // Viewpoint
122             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
123 
124             G3_LookAt(&Eye, &vUp, &at, NULL);
125         }
126 
127         //---------------------------------------------------------------------------
128         // Set up light colors and direction.
129         // Notice that light vector is transformed by the current vector matrix
130         // immediately after LightVector command is issued.
131         //
132         // GX_LIGHTID_0: white, downward
133         //---------------------------------------------------------------------------
134         G3_LightVector(GX_LIGHTID_0, FX32_SQRT1_2, -FX32_SQRT1_2, 0);
135         G3_LightColor(GX_LIGHTID_0, GX_RGB(16, 16, 16));
136 
137         G3_Translate(0, 0, -5 * FX32_ONE);
138 
139         G3_PushMtx();
140 
141         //---------------------------------------------------------------------------
142         //
143         // Left chair (in modulation mode)
144         //
145         //---------------------------------------------------------------------------
146         G3_PushMtx();
147 
148         {
149             // Rotate and translate the chair
150             fx16    s = FX_SinIdx(Rotate);
151             fx16    c = FX_CosIdx(Rotate);
152 
153             G3_Translate(-2 * FX32_ONE, 0, 0);
154             G3_RotZ(s, c);
155             G3_RotY(s, c);
156             G3_RotX(s, c);
157         }
158 
159         G3_TexImageParam(GX_TEXFMT_PLTT16,      // 16 colors palette texture
160                          GX_TEXGEN_NONE,        // no texgen
161                          GX_TEXSIZE_S256,       // 256 pixels
162                          GX_TEXSIZE_T64,        // 64 pixels
163                          GX_TEXREPEAT_NONE,     // no repeat
164                          GX_TEXFLIP_NONE,       // no flip
165                          GX_TEXPLTTCOLOR0_USE,  // use color 0 of the palette
166                          myTexAddr     // the offset of the texture image
167             );
168 
169         G3_TexPlttBase(myTexPlttAddr,  // the offset of the texture palette
170                        GX_TEXFMT_PLTT16 // 16 colors palette texture
171             );
172 
173         // Set the material color( diffuse, ambient , specular ) as basic white
174         DEMO_Set3DDefaultMaterial(TRUE, TRUE);
175         DEMO_Set3DDefaultShininessTable();
176 
177         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
178                        GX_POLYGONMODE_MODULATE, // modulation mode
179                        GX_CULL_BACK,   // cull back
180                        0,              // polygon ID(0 - 63)
181                        31,             // alpha(0 - 31)
182                        0               // OR of GXPolygonAttrMisc's value
183             );
184 
185         G3_Begin(GX_BEGIN_TRIANGLES);
186         {
187             MI_SendGXCommand(3, isu1, isu1_size);
188         }
189         G3_End();
190 
191         G3_TexImageParam(GX_TEXFMT_PLTT16,      // 16 colors palette texture
192                          GX_TEXGEN_NONE,        // no texgen
193                          GX_TEXSIZE_S128,       // 128 pixels
194                          GX_TEXSIZE_T128,       // 128 pixels
195                          GX_TEXREPEAT_NONE,     // no repeat
196                          GX_TEXFLIP_NONE,       // no flip
197                          GX_TEXPLTTCOLOR0_USE,  // use color 0 of the palette
198                          myTexAddr + tex_isu1_size      // the offset of the texture image
199             );
200 
201         G3_TexPlttBase(myTexPlttAddr + pal_isu1_size,   // the offset of the texture palette
202                        GX_TEXFMT_PLTT16 // 16 colors palette texture
203             );
204 
205         // Set the material color( diffuse, ambient , specular ) as basic white
206         DEMO_Set3DDefaultMaterial(TRUE, TRUE);
207         DEMO_Set3DDefaultShininessTable();
208 
209 
210         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
211                        GX_POLYGONMODE_MODULATE, // modulation mode
212                        GX_CULL_BACK,   // cull back
213                        0,              // polygon ID(0 - 63)
214                        31,             // alpha(0 - 31)
215                        0               // OR of GXPolygonAttrMisc's value
216             );
217 
218         G3_Begin(GX_BEGIN_TRIANGLES);
219         {
220             MI_SendGXCommand(3, isu2, isu2_size);
221         }
222         G3_End();
223 
224         G3_PopMtx(1);
225 
226         //---------------------------------------------------------------------------
227         //
228         // Right chair (in high-light mode)
229         //
230         //---------------------------------------------------------------------------
231         G3_PushMtx();
232 
233         {
234             fx16    s = FX_SinIdx(Rotate);
235             fx16    c = FX_CosIdx(Rotate);
236 
237             G3_Translate(2 * FX32_ONE, 0, 0);
238             G3_RotZ(s, c);
239             G3_RotY(s, c);
240             G3_RotX(s, c);
241         }
242 
243         G3_TexImageParam(GX_TEXFMT_PLTT16,      // 16 colors palette texture
244                          GX_TEXGEN_NONE,        // no texgen
245                          GX_TEXSIZE_S256,       // 256 pixels
246                          GX_TEXSIZE_T64,        // 64 pixels
247                          GX_TEXREPEAT_NONE,     // no repeat
248                          GX_TEXFLIP_NONE,       // no flip
249                          GX_TEXPLTTCOLOR0_USE,  // use color 0 of the palette
250                          myTexAddr     // the offset of the texture image
251             );
252 
253         G3_TexPlttBase(myTexPlttAddr,  // the offset of the texture palette
254                        GX_TEXFMT_PLTT16 // 16 colors palette texture
255             );
256 
257         // Set the material color( diffuse, ambient , specular ) as basic white
258         DEMO_Set3DDefaultMaterial(TRUE, TRUE);
259         DEMO_Set3DDefaultShininessTable();
260 
261         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
262                        GX_POLYGONMODE_TOON,     // toon/high-light mode
263                        GX_CULL_BACK,   // cull back
264                        0,              // polygon ID(0 - 63)
265                        31,             // alpha(0 - 31)
266                        0               // OR of GXPolygonAttrMisc's value
267             );
268 
269         G3_Begin(GX_BEGIN_TRIANGLES);
270         {
271             MI_SendGXCommand(3, isu1, isu1_size);
272         }
273         G3_End();
274 
275         G3_TexImageParam(GX_TEXFMT_PLTT16,      // 16 colors palette texture
276                          GX_TEXGEN_NONE,        // no texgen
277                          GX_TEXSIZE_S128,       // 128 pixels
278                          GX_TEXSIZE_T128,       // 128 pixels
279                          GX_TEXREPEAT_NONE,     // no repeat
280                          GX_TEXFLIP_NONE,       // no flip
281                          GX_TEXPLTTCOLOR0_USE,  // use color 0 of the palette
282                          myTexAddr + tex_isu1_size      // the offset of the texture image
283             );
284 
285         G3_TexPlttBase(myTexPlttAddr + pal_isu1_size,   // the offset of the texture palette
286                        GX_TEXFMT_PLTT16 // 16 colors palette texture
287             );
288 
289         // Set the material color( diffuse, ambient , specular ) as basic white
290         DEMO_Set3DDefaultMaterial(TRUE, TRUE);
291         DEMO_Set3DDefaultShininessTable();
292 
293         G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
294                        GX_POLYGONMODE_TOON,     // TOON / HIGHLIGHT mode
295                        GX_CULL_BACK,   // cull back
296                        0,              // polygon ID(0 - 63)
297                        31,             // alpha(0 - 31)
298                        0               // OR of GXPolygonAttrMisc's value
299             );
300 
301         G3_Begin(GX_BEGIN_TRIANGLES);
302         {
303             MI_SendGXCommand(3, isu2, isu2_size);
304         }
305         G3_End();
306 
307         G3_PopMtx(1);
308 
309         G3_PopMtx(1);
310 
311         // swapping the polygon list RAM, the vertex RAM, etc.
312         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
313 
314 #ifdef SDK_AUTOTEST
315         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
316         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
317         EXT_TestScreenShot(100, 0x76068B4B);
318         EXT_TestTickCounter();
319 #endif //SDK_AUTOTEST
320 
321         OS_WaitVBlankIntr();           // Waiting the end of VBlank interrupt
322     }
323 }
324 
325 //---------------------------------------------------------------------------
326 // VBlank interrupt function:
327 //
328 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
329 // OS_EnableIrqMask selects IRQ interrupts to enable, and
330 // OS_EnableIrq enables IRQ interrupts.
331 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
332 //---------------------------------------------------------------------------
VBlankIntr(void)333 void VBlankIntr(void)
334 {
335     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
336 }
337