1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/3D_Pol_Transparent
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 displays transparent polygons(wireframes):
20 //
21 // If alpha test is off and alpha is zero, polygons are displayed as wireframes.
22 // This sample displays wireframes in two manners(culling back/culling none).
23 // Since wireframes are displayed as a part of a plane,
24 // the reverse side of polygons is not displayed in the case of culling back.
25 //
26 // HOWTO:
27 // 1. Speficy 0 for alpha by G3_PolygonAttr.
28 //---------------------------------------------------------------------------
29 
30 #ifdef SDK_TWL
31 #include <twl.h>
32 #else
33 #include <nitro.h>
34 #endif
35 #include "DEMO.h"
36 
37 s16     gCubeGeometry[3 * 8] = {
38     FX16_ONE, FX16_ONE, FX16_ONE,
39     FX16_ONE, FX16_ONE, -FX16_ONE,
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 };
47 
48 
vtx(int idx)49 static void vtx(int idx)
50 {
51     G3_Vtx(gCubeGeometry[idx * 3], gCubeGeometry[idx * 3 + 1], gCubeGeometry[idx * 3 + 2]);
52 }
53 
quad(int idx0,int idx1,int idx2,int idx3)54 static void quad(int idx0, int idx1, int idx2, int idx3)
55 {
56     vtx(idx0);
57     vtx(idx1);
58     vtx(idx2);
59     vtx(idx3);
60 }
61 
62 #ifdef SDK_TWL
TwlMain(void)63 void TwlMain(void)
64 #else
65 void NitroMain(void)
66 #endif
67 {
68     u16     Rotate = 0;                // for rotating cubes(0-65535)
69 
70     //---------------------------------------------------------------------------
71     // Initialize:
72     // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
73     //---------------------------------------------------------------------------
74     DEMOInitCommon();
75     DEMOInitVRAM();
76     DEMOInitDisplay3D();
77 
78     DEMOStartDisplay();
79     while (1)
80     {
81         G3X_Reset();
82         Rotate += 256;
83 
84         //---------------------------------------------------------------------------
85         // Set up camera matrix
86         //---------------------------------------------------------------------------
87         {
88             VecFx32 Eye = { 0, 0, FX32_ONE };   // Eye position
89             VecFx32 at = { 0, 0, 0 };  // Viewpoint
90             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
91 
92             G3_LookAt(&Eye, &vUp, &at, NULL);
93         }
94 
95         G3_PushMtx();
96         G3_Translate(0, 0, -5 * FX32_ONE);
97 
98         //---------------------------------------------------------------------------
99         // Draw the left cube:
100         //
101         // Some wire frames are not visible because of culling back.
102         //---------------------------------------------------------------------------
103         G3_PushMtx();
104         {
105             fx16    s = FX_SinIdx(Rotate);
106             fx16    c = FX_CosIdx(Rotate);
107 
108             G3_Translate(-3 * (FX32_ONE >> 1), 0, 0);
109 
110             G3_RotX(s, c);
111             G3_RotY(s, c);
112             G3_RotZ(s, c);
113 
114         }
115 
116         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // diffuse
117                                 GX_RGB(16, 16, 16),     // ambient
118                                 TRUE   // use diffuse as vtx color if TRUE
119             );
120 
121         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
122                                 GX_RGB(0, 0, 0),        // emission
123                                 FALSE  // use shininess table if TRUE
124             );
125 
126         G3_PolygonAttr(GX_LIGHTMASK_NONE,       // no lights
127                        GX_POLYGONMODE_MODULATE, // modulation mode
128                        GX_CULL_BACK,   // cull back
129                        0,              // polygon ID(0 - 63)
130                        0,              // alpha(0 - 31)
131                        0               // OR of GXPolygonAttrMisc's value
132             );
133 
134         G3_Begin(GX_BEGIN_QUADS);
135 
136         {
137             quad(2, 0, 4, 6);
138             quad(7, 5, 1, 3);
139             quad(6, 4, 5, 7);
140             quad(3, 1, 0, 2);
141             quad(5, 4, 0, 1);
142             quad(6, 7, 3, 2);
143         }
144 
145         G3_End();
146 
147         G3_PopMtx(1);
148 
149         //---------------------------------------------------------------------------
150         // Draw the right cube:
151         //
152         // All wire frames are visible if GX_CULL_NONE specified
153         //---------------------------------------------------------------------------
154         G3_PushMtx();
155 
156         {
157             fx16    s = FX_SinIdx(Rotate);
158             fx16    c = FX_CosIdx(Rotate);
159 
160             G3_Translate(3 * (FX32_ONE >> 1), 0, 0);
161 
162             G3_RotX(s, c);
163             G3_RotY(s, c);
164             G3_RotZ(s, c);
165 
166         }
167 
168         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // diffuse
169                                 GX_RGB(16, 16, 16),     // ambient
170                                 TRUE   // use diffuse as vtx color if TRUE
171             );
172         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // specular
173                                 GX_RGB(0, 0, 0),        // emission
174                                 FALSE  // use shininess table if TRUE
175             );
176 
177         G3_PolygonAttr(GX_LIGHTMASK_NONE,       // no lights
178                        GX_POLYGONMODE_MODULATE, // modulation mode
179                        GX_CULL_NONE,   // cull none
180                        0,              // polygon ID(0 - 63)
181                        0,              // alpha(0 - 31)
182                        0               // OR of GXPolygonAttrMisc's value
183             );
184 
185         G3_Begin(GX_BEGIN_QUADS);
186 
187         {
188             quad(2, 0, 4, 6);
189             quad(7, 5, 1, 3);
190             quad(6, 4, 5, 7);
191             quad(3, 1, 0, 2);
192             quad(5, 4, 0, 1);
193             quad(6, 7, 3, 2);
194         }
195 
196         G3_End();
197 
198         G3_PopMtx(1);
199 
200         G3_PopMtx(1);
201 
202         // swapping the polygon list RAM, the vertex RAM, etc.
203         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
204 
205 #ifdef SDK_AUTOTEST
206         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
207         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
208         EXT_TestScreenShot(100, 0xE07B93A9);
209         EXT_TestTickCounter();
210 #endif //SDK_AUTOTEST
211 
212         OS_WaitVBlankIntr();           // Waiting the end of VBlank interrupt
213     }
214 }
215 
216 //---------------------------------------------------------------------------
217 // VBlank interrupt function:
218 //
219 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
220 // OS_EnableIrqMask selects IRQ interrupts to enable, and
221 // OS_EnableIrq enables IRQ interrupts.
222 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
223 //---------------------------------------------------------------------------
VBlankIntr(void)224 void VBlankIntr(void)
225 {
226     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
227 }
228