1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/3D_Alpha_Test
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 alpha testing:
20 // Two cubes are send to command FIFO.
21 // The alpha parameters are 31(opaque) and 16(translucent) respectively.
22 // Since the threshold of alpha test is set to 24 by G3X_AlphaTest(TRUE, 24),
23 // The only one cube is displayed.
24 //
25 // HOWTO:
26 // 1. Call G3X_AlphaTest(TRUE, alpha), which enables alpha tests and specifies
27 // the threshold.
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
drawCube(int alpha,u16 Rotate)63 static void drawCube(int alpha, u16 Rotate)
64 {
65 {
66 fx16 s = FX_SinIdx(Rotate);
67 fx16 c = FX_CosIdx(Rotate);
68
69 G3_RotX(s, c);
70 G3_RotY(s, c);
71 G3_RotZ(s, c);
72 }
73 G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31), // diffuse
74 GX_RGB(16, 16, 16), // ambient
75 TRUE // use diffuse as vtx color if TRUE
76 );
77
78 G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16), // specular
79 GX_RGB(0, 0, 0), // emission
80 FALSE // use shininess table if TRUE
81 );
82
83 G3_PolygonAttr(GX_LIGHTMASK_NONE, // no lights
84 GX_POLYGONMODE_MODULATE, // modulation mode
85 GX_CULL_BACK, // cull back
86 0, // polygon ID(0 - 63)
87 alpha, // alpha(0 - 31)
88 0 // OR of GXPolygonAttrMisc's value
89 );
90
91
92 G3_Begin(GX_BEGIN_QUADS);
93 {
94 quad(2, 0, 4, 6);
95 quad(7, 5, 1, 3);
96 quad(6, 4, 5, 7);
97 quad(3, 1, 0, 2);
98 quad(5, 4, 0, 1);
99 quad(6, 7, 3, 2);
100 }
101 G3_End();
102 }
103
104
105 #ifdef SDK_TWL
TwlMain(void)106 void TwlMain(void)
107 #else
108 void NitroMain(void)
109 #endif
110 {
111 u16 Rotate = 0; // for rotating cubes(0-65535)
112
113 //---------------------------------------------------------------------------
114 // Initialize:
115 // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
116 //---------------------------------------------------------------------------
117 DEMOInitCommon();
118 DEMOInitVRAM();
119 DEMOInitDisplay3D();
120
121 // not displayed if alpha is equal or less than 24
122 G3X_AlphaTest(TRUE, 24);
123
124 DEMOStartDisplay();
125 while (1)
126 {
127 G3X_Reset();
128 Rotate += 256;
129
130 //---------------------------------------------------------------------------
131 // Set up the camera matrix
132 //---------------------------------------------------------------------------
133 {
134 VecFx32 Eye = { 0, 0, FX32_ONE }; // Eye Position
135 VecFx32 at = { 0, 0, 0 }; // Viewpoint
136 VecFx32 vUp = { 0, FX32_ONE, 0 }; // Up
137
138 G3_LookAt(&Eye, &vUp, &at, NULL);
139 }
140
141 G3_PushMtx();
142 G3_Translate(0, 0, -5 * FX32_ONE);
143
144 //---------------------------------------------------------------------------
145 // Send Cube #1(visible)
146 //---------------------------------------------------------------------------
147 G3_PushMtx();
148 G3_Translate(-3 * (FX32_ONE >> 1), 0, 0);
149 drawCube(31, Rotate);
150 G3_PopMtx(1);
151
152 //---------------------------------------------------------------------------
153 // Send Cube #2(not visible)
154 //---------------------------------------------------------------------------
155 G3_PushMtx();
156 G3_Translate(3 * (FX32_ONE >> 1), 0, 0);
157 drawCube(24, Rotate);
158 G3_PopMtx(1);
159
160 G3_PopMtx(1);
161
162 // swapping the polygon list RAM, the vertex RAM, etc.
163 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
164
165 #ifdef SDK_AUTOTEST
166 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
167 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
168 EXT_TestScreenShot(100, 0x7DBE54EC);
169 EXT_TestTickCounter();
170 #endif //SDK_AUTOTEST
171
172 OS_WaitVBlankIntr(); // Waiting the end of VBlank interrupt
173 }
174 }
175
176 //---------------------------------------------------------------------------
177 // VBlank interrupt function:
178 //
179 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
180 // OS_EnableIrqMask selects IRQ interrupts to enable, and
181 // OS_EnableIrq enables IRQ interrupts.
182 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
183 //---------------------------------------------------------------------------
VBlankIntr(void)184 void VBlankIntr(void)
185 {
186 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
187 }
188