1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/3D_Pol_Translucent
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 translucent polygons:
20 //
21 // There are a simple translucent cube and a opaque square behind it.
22 //
23 // HOWTO:
24 // 1. Enable alpha blending by G3X_AlphaBlend(TRUE).
25 // 2. Alpha can be set by PolygonAttr().
26 //---------------------------------------------------------------------------
27 
28 #ifdef SDK_TWL
29 #include <twl.h>
30 #else
31 #include <nitro.h>
32 #endif
33 #include "DEMO.h"
34 
35 #ifdef SDK_TWL
TwlMain(void)36 void TwlMain(void)
37 #else
38 void NitroMain(void)
39 #endif
40 {
41     u16     Rotate = 0;                // for rotating cubes(0-65535)
42 
43     //---------------------------------------------------------------------------
44     // Initialize:
45     // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
46     //---------------------------------------------------------------------------
47     DEMOInitCommon();
48     DEMOInitVRAM();
49     DEMOInitDisplay3D();
50 
51     // Enable alpha blending
52     G3X_AlphaBlend(TRUE);
53 
54     DEMOStartDisplay();
55     while (1)
56     {
57         G3X_Reset();
58         Rotate += 256;
59 
60         //---------------------------------------------------------------------------
61         // Set up camera matrix
62         //---------------------------------------------------------------------------
63         {
64             VecFx32 Eye = { 0, 0, FX32_ONE };   // Eye position
65             VecFx32 at = { 0, 0, 0 };  // Viewpoint
66             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
67 
68             G3_LookAt(&Eye, &vUp, &at, NULL);
69         }
70 
71         G3_PushMtx();
72 
73         //---------------------------------------------------------------------------
74         // Draw a translucent cube
75         //---------------------------------------------------------------------------
76         G3_PushMtx();
77 
78         {
79             fx16    s = FX_SinIdx(Rotate);
80             fx16    c = FX_CosIdx(Rotate);
81             G3_Translate(0, 0, -5 * FX32_ONE);
82 
83             G3_RotX(s, c);
84             G3_RotY(s, c);
85             G3_RotZ(s, c);
86         }
87 
88         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // diffuse
89                                 GX_RGB(16, 16, 16),     // ambient
90                                 TRUE   // use diffuse as vtx color if TRUE
91             );
92 
93         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
94                                 GX_RGB(0, 0, 0),        // emission
95                                 FALSE  // use shininess table if TRUE
96             );
97 
98         G3_PolygonAttr(GX_LIGHTMASK_NONE,       // no lights
99                        GX_POLYGONMODE_MODULATE, // modulation mode
100                        GX_CULL_BACK,   // cull back
101                        0,              // polygon ID(0 - 63)
102                        24,             // alpha(0 - 31)
103                        0               // OR of GXPolygonAttrMisc's value
104             );
105 
106         G3_Begin(GX_BEGIN_QUADS);
107 
108         {
109             G3_Vtx(FX16_ONE, -FX16_ONE, FX16_ONE);
110             G3_Vtx(FX16_ONE, FX16_ONE, FX16_ONE);
111             G3_Vtx(-FX16_ONE, FX16_ONE, FX16_ONE);
112             G3_Vtx(-FX16_ONE, -FX16_ONE, FX16_ONE);
113 
114             G3_Vtx(-FX16_ONE, -FX16_ONE, -FX16_ONE);
115             G3_Vtx(-FX16_ONE, FX16_ONE, -FX16_ONE);
116             G3_Vtx(FX16_ONE, FX16_ONE, -FX16_ONE);
117             G3_Vtx(FX16_ONE, -FX16_ONE, -FX16_ONE);
118 
119             G3_Vtx(-FX16_ONE, -FX16_ONE, FX16_ONE);
120             G3_Vtx(-FX16_ONE, FX16_ONE, FX16_ONE);
121             G3_Vtx(-FX16_ONE, FX16_ONE, -FX16_ONE);
122             G3_Vtx(-FX16_ONE, -FX16_ONE, -FX16_ONE);
123 
124             G3_Vtx(FX16_ONE, -FX16_ONE, -FX16_ONE);
125             G3_Vtx(FX16_ONE, FX16_ONE, -FX16_ONE);
126             G3_Vtx(FX16_ONE, FX16_ONE, FX16_ONE);
127             G3_Vtx(FX16_ONE, -FX16_ONE, FX16_ONE);
128 
129             G3_Vtx(-FX16_ONE, FX16_ONE, -FX16_ONE);
130             G3_Vtx(-FX16_ONE, FX16_ONE, FX16_ONE);
131             G3_Vtx(FX16_ONE, FX16_ONE, FX16_ONE);
132             G3_Vtx(FX16_ONE, FX16_ONE, -FX16_ONE);
133 
134             G3_Vtx(-FX16_ONE, -FX16_ONE, FX16_ONE);
135             G3_Vtx(-FX16_ONE, -FX16_ONE, -FX16_ONE);
136             G3_Vtx(FX16_ONE, -FX16_ONE, -FX16_ONE);
137             G3_Vtx(FX16_ONE, -FX16_ONE, FX16_ONE);
138         }
139 
140         G3_End();
141 
142         G3_PopMtx(1);
143 
144         //---------------------------------------------------------------------------
145         // Draw opaque squares
146         //---------------------------------------------------------------------------
147         G3_PushMtx();
148 
149         // Rotate and translate a square
150         G3_Translate(0, 0, -10 << FX32_SHIFT);
151         {
152             fx16    s = FX_SinIdx(Rotate);
153             fx16    c = FX_CosIdx(Rotate);
154 
155             G3_RotZ(s, c);
156         }
157 
158         G3_MaterialColorDiffAmb(GX_RGB(31, 0, 0),       // diffuse
159                                 GX_RGB(16, 16, 16),     // ambient
160                                 TRUE   // use diffuse as vtx color if TRUE
161             );
162 
163         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
164                                 GX_RGB(0, 0, 0),        // emission
165                                 FALSE  // use shininess table if TRUE
166             );
167 
168         G3_PolygonAttr(GX_LIGHTMASK_NONE,       // no lights
169                        GX_POLYGONMODE_MODULATE, // modulation mode
170                        GX_CULL_BACK,   // cull back
171                        0,              // polygon ID(0 - 63)
172                        31,             // alpha(0 - 31)
173                        0               // OR of GXPolygonAttrMisc's value
174             );
175 
176         G3_Begin(GX_BEGIN_QUADS);
177         {
178             G3_Vtx(FX16_ONE << 1, 0, FX16_ONE << 1);
179             G3_Vtx(FX16_ONE << 1, FX16_ONE << 1, FX16_ONE << 1);
180             G3_Vtx(0, FX16_ONE << 1, FX16_ONE << 1);
181             G3_Vtx(0, 0, FX16_ONE << 1);
182         }
183         G3_End();
184 
185         G3_MaterialColorDiffAmb(GX_RGB(0, 31, 0),       // diffuse
186                                 GX_RGB(16, 16, 16),     // ambient
187                                 TRUE   // use diffuse as vtx color if TRUE
188             );
189 
190         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
191                                 GX_RGB(0, 0, 0),        // emission
192                                 FALSE  // use shininess table if TRUE
193             );
194 
195         G3_PolygonAttr(GX_LIGHTMASK_NONE,       // no lights
196                        GX_POLYGONMODE_MODULATE, // modulation mode
197                        GX_CULL_BACK,   // cull back
198                        0,              // polygon ID(0 - 63)
199                        31,             // alpha(0 - 31)
200                        0               // OR of GXPolygonAttrMisc's value
201             );
202 
203         G3_Begin(GX_BEGIN_QUADS);
204 
205         {
206             G3_Vtx(0, 0, FX16_ONE << 1);
207             G3_Vtx(0, FX16_ONE << 1, FX16_ONE << 1);
208             G3_Vtx(-FX16_ONE << 1, FX16_ONE << 1, FX16_ONE << 1);
209             G3_Vtx(-FX16_ONE << 1, 0, FX16_ONE << 1);
210         }
211 
212         G3_End();
213 
214         G3_MaterialColorDiffAmb(GX_RGB(0, 0, 31),       // diffuse
215                                 GX_RGB(16, 16, 16),     // ambient
216                                 TRUE   // use diffuse as vtx color if TRUE
217             );
218 
219         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
220                                 GX_RGB(0, 0, 0),        // emission
221                                 FALSE  // use shininess table if TRUE
222             );
223 
224         G3_PolygonAttr(GX_LIGHTMASK_NONE,       // no lights
225                        GX_POLYGONMODE_MODULATE, // modulation mode
226                        GX_CULL_BACK,   // cull back
227                        0,              // polygon ID(0 - 63)
228                        31,             // alpha(0 - 31)
229                        0               // OR of GXPolygonAttrMisc's value
230             );
231 
232         G3_Begin(GX_BEGIN_QUADS);
233         {
234             G3_Vtx(0, -FX16_ONE << 1, FX16_ONE << 1);
235             G3_Vtx(0, 0, FX16_ONE << 1);
236             G3_Vtx(-FX16_ONE << 1, 0, FX16_ONE << 1);
237             G3_Vtx(-FX16_ONE << 1, -FX16_ONE << 1, FX16_ONE << 1);
238         }
239         G3_End();
240 
241         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 0),      // diffuse
242                                 GX_RGB(16, 16, 16),     // ambient
243                                 TRUE   // use diffuse as vtx color if TRUE
244             );
245 
246         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
247                                 GX_RGB(0, 0, 0),        // emission
248                                 FALSE  // use shininess table if TRUE
249             );
250 
251         G3_PolygonAttr(GX_LIGHTMASK_NONE,       // no lights
252                        GX_POLYGONMODE_MODULATE, // modulation mode
253                        GX_CULL_BACK,   // cull back
254                        0,              // polygon ID(0 - 63)
255                        31,             // alpha(0 - 31)
256                        0               // OR of GXPolygonAttrMisc's value
257             );
258 
259         G3_Begin(GX_BEGIN_QUADS);
260 
261         {
262             G3_Vtx(FX16_ONE << 1, -FX16_ONE << 1, FX16_ONE << 1);
263             G3_Vtx(FX16_ONE << 1, 0, FX16_ONE << 1);
264             G3_Vtx(0, 0, FX16_ONE << 1);
265             G3_Vtx(0, -FX16_ONE << 1, FX16_ONE << 1);
266         }
267 
268         G3_End();
269 
270         G3_PopMtx(1);
271 
272         G3_PopMtx(1);
273 
274         // swapping the polygon list RAM, the vertex RAM, etc.
275         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
276 
277 #ifdef SDK_AUTOTEST
278         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
279         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
280         EXT_TestScreenShot(100, 0x0BCC31FE);
281         EXT_TestTickCounter();
282 #endif //SDK_AUTOTEST
283 
284         OS_WaitVBlankIntr();           // Waiting the end of VBlank interrupt
285     }
286 }
287 
288 //---------------------------------------------------------------------------
289 // VBlank interrupt function:
290 //
291 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
292 // OS_EnableIrqMask selects IRQ interrupts to enable, and
293 // OS_EnableIrq enables IRQ interrupts.
294 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
295 //---------------------------------------------------------------------------
VBlankIntr(void)296 void VBlankIntr(void)
297 {
298     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
299 }
300