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