1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/3D_Fog
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:: 2009-01-15#$
14 $Rev: 9860 $
15 $Author: okubata_ryoma $
16 *---------------------------------------------------------------------------*/
17
18 //---------------------------------------------------------------------------
19 // A sample for fog blending
20 //
21 // A cube is rotating on an axis(a quad).
22 // It is fogged when it gets far from the eye.
23 //
24 // HOWTO:
25 // 1. Enable and set up attributes by G3X_SetFog.
26 // 2. Specify the color of fog by G3X_SetFogColor.
27 // 3. Set up the fog table by G3X_SetFogTable.
28 // 4. GX_POLYGON_ATTR_MISC_FOG must be specified when G3_PolygonAttr is called.
29 //---------------------------------------------------------------------------
30
31 #ifdef SDK_TWL
32 #include <twl.h>
33 #else
34 #include <nitro.h>
35 #endif
36 #include "DEMO.h"
37
38 s16 gCubeGeometry[3 * 8] = {
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 -FX16_ONE, -FX16_ONE, -FX16_ONE
47 };
48
49
vtx(int idx)50 static void vtx(int idx)
51 {
52 G3_Vtx(gCubeGeometry[idx * 3], gCubeGeometry[idx * 3 + 1], gCubeGeometry[idx * 3 + 2]);
53 }
54
quad(int idx0,int idx1,int idx2,int idx3)55 static void quad(int idx0, int idx1, int idx2, int idx3)
56 {
57 vtx(idx0);
58 vtx(idx1);
59 vtx(idx2);
60 vtx(idx3);
61 }
62
63 #ifdef SDK_TWL
TwlMain(void)64 void TwlMain(void)
65 #else
66 void NitroMain(void)
67 #endif
68 {
69 u16 Rotate = 0; // for rotating cubes(0-65535)
70
71 //---------------------------------------------------------------------------
72 // Initialize:
73 // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
74 //---------------------------------------------------------------------------
75 DEMOInitCommon();
76 DEMOInitVRAM();
77 DEMOInitDisplay3D();
78
79 //---------------------------------------------------------------------------
80 // Set up fog
81 //---------------------------------------------------------------------------
82 {
83 u32 fog_table[8];
84 int i;
85
86 //---------------------------------------------------------------------------
87 // Enable fog and set up attributes
88 //---------------------------------------------------------------------------
89 G3X_SetFog(TRUE, // Fog enabled if TRUE
90 GX_FOGBLEND_COLOR_ALPHA, // Fog mode(blending to pixel's color and alpha)
91 GX_FOGSLOPE_0x2000, // Fog slope(the small number makes fog steep)
92 0x5800 // Fog offset(use table[0] until depth=0x5800)
93 );
94
95 //---------------------------------------------------------------------------
96 // Set up fog color
97 //---------------------------------------------------------------------------
98 G3X_SetFogColor(GX_RGB(0, 0, 0), 0);
99
100 //---------------------------------------------------------------------------
101 // Set up the fog table:
102 // The value must be from 0 to 127.
103 // The bigger number means a denser fog.
104 //---------------------------------------------------------------------------
105 for (i = 0; i < 8; i++)
106 {
107 fog_table[i] =
108 (u32)(((i * 16) << 0) | ((i * 16 + 4) << 8) | ((i * 16 + 8) << 16) | ((i * 16 +
109 12) << 24));
110 }
111 G3X_SetFogTable(&fog_table[0]);
112 }
113
114 //---------------------------------------------------------------------------
115 // Use Z-Buffering mode:
116 // This sample uses Z-Buffering mode.
117 // Notice that the depth values are different between Z-Buffering mode and W-Buffering mode.
118 //---------------------------------------------------------------------------
119 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_Z);
120
121 DEMOStartDisplay();
122 while (1)
123 {
124 G3X_Reset();
125 Rotate += 256;
126
127 //---------------------------------------------------------------------------
128 // Set up camera matrix
129 //---------------------------------------------------------------------------
130 {
131 VecFx32 Eye = { 0, 0, FX32_ONE * 8 }; // Eye Position
132 VecFx32 at = { 0, 0, 0 }; // Viewpoint
133 VecFx32 vUp = { 0, FX32_ONE, 0 }; // Up
134
135 G3_LookAt(&Eye, &vUp, &at, NULL);
136 }
137
138 G3_PushMtx();
139
140 //---------------------------------------------------------------------------
141 // Rotate on the Y axis
142 //---------------------------------------------------------------------------
143 {
144 fx16 s = FX_SinIdx(Rotate);
145 fx16 c = FX_CosIdx(Rotate);
146
147 G3_RotY(s, c);
148 G3_Translate(0, 0, 5 * FX32_ONE);
149 G3_RotX(s, c);
150 G3_RotZ(s, c);
151 }
152
153 G3_MaterialColorDiffAmb(GX_RGB(31, 0, 0), // diffuse
154 GX_RGB(16, 16, 16), // ambient
155 TRUE // use diffuse as vtx color if TRUE
156 );
157
158 G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16), // specular
159 GX_RGB(0, 0, 0), // emission
160 FALSE // use shininess table if TRUE
161 );
162
163 G3_PolygonAttr(GX_LIGHTMASK_NONE, // disable lights
164 GX_POLYGONMODE_MODULATE, // modulation mode
165 GX_CULL_BACK, // cull back
166 0, // polygon ID(0 - 63)
167 31, // alpha(0 - 31)
168 GX_POLYGON_ATTR_MISC_FOG //!!! you must specify GX_POLYGON_ATTR_MISC_FOG.
169 );
170
171 //---------------------------------------------------------------------------
172 // Draw a cube with fog blending
173 //---------------------------------------------------------------------------
174 G3_Begin(GX_BEGIN_QUADS);
175
176 {
177 quad(2, 0, 4, 6);
178 quad(7, 5, 1, 3);
179 quad(6, 4, 5, 7);
180 quad(3, 1, 0, 2);
181 quad(5, 4, 0, 1);
182 quad(6, 7, 3, 2);
183 }
184
185 G3_End();
186
187 G3_PopMtx(1);
188
189 //---------------------------------------------------------------------------
190 // A quad without fog blending
191 //---------------------------------------------------------------------------
192 G3_PushMtx();
193
194 G3_MaterialColorDiffAmb(GX_RGB(0, 0, 31), // diffuse
195 GX_RGB(16, 16, 16), // ambient
196 TRUE // use diffuse as vtx color if TRUE
197 );
198
199 G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16), // specular
200 GX_RGB(0, 0, 0), // emission
201 FALSE // use shininess table if TRUE
202 );
203
204 G3_PolygonAttr(GX_LIGHTMASK_NONE, // disable lights
205 GX_POLYGONMODE_MODULATE, // modulation mode
206 GX_CULL_BACK, // cull back
207 0, // polygon ID(0 - 63)
208 31, // alpha(0 - 31)
209 0 // OR of GXPolygonAttrMisc's value
210 );
211
212 //---------------------------------------------------------------------------
213 // Draw a quad without fog blending
214 //---------------------------------------------------------------------------
215 G3_Begin(GX_BEGIN_QUADS);
216
217 G3_Vtx(FX32_ONE >> 2, -FX32_ONE << 2, 0);
218 G3_Vtx(FX32_ONE >> 2, FX32_ONE << 2, 0);
219 G3_Vtx(-FX32_ONE >> 2, FX32_ONE << 2, 0);
220 G3_Vtx(-FX32_ONE >> 2, -FX32_ONE << 2, 0);
221
222 G3_End();
223
224 G3_PopMtx(1);
225
226 // swapping the polygon list RAM, the vertex RAM, etc.
227 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_Z);
228
229 #ifdef SDK_AUTOTEST
230 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
231 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
232 EXT_TestScreenShot(100, 0xA39F3A2E);
233 EXT_TestTickCounter();
234 #endif //SDK_AUTOTEST
235
236 OS_WaitVBlankIntr(); // Waiting the end of VBlank interrupt
237 }
238 }
239
240 //---------------------------------------------------------------------------
241 // VBlank interrupt function:
242 //
243 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
244 // OS_EnableIrqMask selects IRQ interrupts to enable, and
245 // OS_EnableIrq enables IRQ interrupts.
246 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
247 //---------------------------------------------------------------------------
VBlankIntr(void)248 void VBlankIntr(void)
249 {
250 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
251 }
252