1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/LinesOver
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-18#$
14 $Rev: 8573 $
15 $Author: okubata_ryoma $
16 *---------------------------------------------------------------------------*/
17 //---------------------------------------------------------------------------
18 // A sample to confirm linesover.
19 //
20 // 200 cubes is displayed on same position.
21 // When there is over limit of polygons per line, the polygon breaks off
22 // on that line.
23 //
24 // HOWTO:
25 // 1. Draw many cubes over limit.
26 //
27 //---------------------------------------------------------------------------
28 #ifdef SDK_TWL
29 #include <twl.h>
30 #else
31 #include <nitro.h>
32 #endif
33 #include "DEMO.h"
34 #include "tex_32768.h"
35
36 //---------------------------------------------------------------------------
37 // Cube model data
38 //---------------------------------------------------------------------------
39 // Vertex data
40 static s16 s_Vertex[3 * 8] = {
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 -FX16_ONE, -FX16_ONE, FX16_ONE,
48 -FX16_ONE, -FX16_ONE, -FX16_ONE
49 };
50 // Normal data
51 static VecFx10 s_Normal[6] = {
52 GX_VECFX10(0, 0, FX32_ONE - 1),
53 GX_VECFX10(0, FX32_ONE - 1, 0),
54 GX_VECFX10(FX32_ONE - 1, 0, 0),
55 GX_VECFX10(0, 0, -FX32_ONE + 1),
56 GX_VECFX10(0, -FX32_ONE + 1, 0),
57 GX_VECFX10(-FX32_ONE + 1, 0, 0)
58 };
59 // Texture coordinate data
60 static GXSt s_TextureCoord[] = {
61 GX_ST(0, 0),
62 GX_ST(0, 64 * FX32_ONE),
63 GX_ST(64 * FX32_ONE, 0),
64 GX_ST(64 * FX32_ONE, 64 * FX32_ONE)
65 };
66
67 //---------------------------------------------------------------------------
68 // Set vertex coordinate.
69 // input:
70 // i_idx: ID of vertex data
71 //---------------------------------------------------------------------------
Vertex(int i_idx)72 inline void Vertex(int i_idx)
73 {
74 G3_Vtx(s_Vertex[i_idx * 3], s_Vertex[i_idx * 3 + 1], s_Vertex[i_idx * 3 + 2]);
75 }
76
77 //---------------------------------------------------------------------------
78 // Set normal setting.
79 // input:
80 // i_idx: ID of normal data
81 //---------------------------------------------------------------------------
Normal(int i_idx)82 inline void Normal(int i_idx)
83 {
84 G3_Direct1(G3OP_NORMAL, s_Normal[i_idx]);
85 }
86
87 //---------------------------------------------------------------------------
88 // Set texture coordinate.
89 // input:
90 // i_idx: ID of texture data
91 //---------------------------------------------------------------------------
TextureCoord(int i_idx)92 inline void TextureCoord(int i_idx)
93 {
94 G3_Direct1(G3OP_TEXCOORD, s_TextureCoord[i_idx]);
95 }
96
97 //---------------------------------------------------------------------------
98 // Draw cube
99 // input:
100 // i_TexAddr: Address of texture which is pasted on cube
101 //---------------------------------------------------------------------------
Cube(u32 i_TexAddr)102 static void Cube(u32 i_TexAddr)
103 {
104 // Draw setting
105 G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31), // Diffuse
106 GX_RGB(16, 16, 16), // Ambient
107 TRUE); // Color
108 G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16), // Specular
109 GX_RGB(0, 0, 0), // Emission
110 FALSE); // Shininess
111 G3_TexImageParam(GX_TEXFMT_DIRECT, // Texture format
112 GX_TEXGEN_TEXCOORD, // Texture generation
113 GX_TEXSIZE_S64, // Texture width
114 GX_TEXSIZE_T64, // Texture height
115 GX_TEXREPEAT_NONE, // Texture repeat
116 GX_TEXFLIP_NONE, // Texture flip
117 GX_TEXPLTTCOLOR0_USE, // Palette color
118 i_TexAddr); // Texture address
119 G3_PolygonAttr(GX_LIGHTMASK_0, // Light
120 GX_POLYGONMODE_MODULATE, // Polygon mode
121 GX_CULL_NONE, // Culling
122 0, // Polygon ID
123 31, // Alpha
124 0); // Misc
125
126 // Draw cube
127 G3_Begin(GX_BEGIN_QUADS);
128 {
129 TextureCoord(1);
130 Normal(0);
131 Vertex(2);
132 TextureCoord(0);
133 Normal(0);
134 Vertex(0);
135 TextureCoord(2);
136 Normal(0);
137 Vertex(4);
138 TextureCoord(3);
139 Normal(0);
140 Vertex(6);
141
142 TextureCoord(1);
143 Normal(3);
144 Vertex(7);
145 TextureCoord(0);
146 Normal(3);
147 Vertex(5);
148 TextureCoord(2);
149 Normal(3);
150 Vertex(1);
151 TextureCoord(3);
152 Normal(3);
153 Vertex(3);
154
155 TextureCoord(1);
156 Normal(5);
157 Vertex(6);
158 TextureCoord(0);
159 Normal(5);
160 Vertex(4);
161 TextureCoord(2);
162 Normal(5);
163 Vertex(5);
164 TextureCoord(3);
165 Normal(5);
166 Vertex(7);
167
168 TextureCoord(1);
169 Normal(2);
170 Vertex(3);
171 TextureCoord(0);
172 Normal(2);
173 Vertex(1);
174 TextureCoord(2);
175 Normal(2);
176 Vertex(0);
177 TextureCoord(3);
178 Normal(2);
179 Vertex(2);
180
181 TextureCoord(1);
182 Normal(1);
183 Vertex(5);
184 TextureCoord(0);
185 Normal(1);
186 Vertex(4);
187 TextureCoord(2);
188 Normal(1);
189 Vertex(0);
190 TextureCoord(3);
191 Normal(1);
192 Vertex(1);
193
194 TextureCoord(1);
195 Normal(4);
196 Vertex(6);
197 TextureCoord(0);
198 Normal(4);
199 Vertex(7);
200 TextureCoord(2);
201 Normal(4);
202 Vertex(3);
203 TextureCoord(3);
204 Normal(4);
205 Vertex(2);
206 }
207 G3_End();
208 }
209
210 //---------------------------------------------------------------------------
211 // VBlank interrupt function:
212 //
213 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
214 // OS_EnableIrqMask selects IRQ interrupts to enable, and
215 // OS_EnableIrq enables IRQ interrupts.
216 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
217 //---------------------------------------------------------------------------
VBlankIntr(void)218 void VBlankIntr(void)
219 {
220 // Set flag which checks VBlank interrupt.
221 OS_SetIrqCheckFlag(OS_IE_V_BLANK);
222 }
223
224 //---------------------------------------------------------------------------
225 // main
226 //---------------------------------------------------------------------------
227 #ifdef SDK_TWL
TwlMain(void)228 void TwlMain(void)
229 #else
230 void NitroMain(void)
231 #endif
232 {
233 u16 Rotate = 0;
234 u32 texAddr = 0x01000; // Address of texture image slot
235
236 // Initialize
237 DEMOInitCommon();
238 DEMOInitVRAM();
239 DEMOInitDisplay3D();
240
241 // If boot on ensata , print waring message.
242 if (OS_IsRunOnEmulator())
243 {
244 OS_Panic("LinesOver is not emulated on \"ensata\".\n");
245 }
246
247 // Load texture image into texture image slot.
248 GX_BeginLoadTex();
249 {
250 GX_LoadTex((void *)&tex_32768_64x64[0], // Src address
251 texAddr, // Destination address
252 8192); // Size to load
253 }
254 GX_EndLoadTex();
255
256 DEMOStartDisplay();
257
258 // main loop
259 while (1)
260 {
261 int i;
262
263 G3X_Reset();
264 Rotate += 256;
265
266 // Camera setting
267 {
268 VecFx32 Eye = { 0, 0, FX32_ONE }; // Sight position
269 VecFx32 at = { 0, 0, 0 }; // Viewpoint
270 VecFx32 vUp = { 0, FX32_ONE, 0 }; // Up
271 G3_LookAt(&Eye, &vUp, &at, NULL); // Sight setting
272 }
273
274 // Light setting
275 G3_LightVector(GX_LIGHTID_0, 0, 0, -FX32_ONE + 1);
276 G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
277
278 // Matrix setting
279 G3_MtxMode(GX_MTXMODE_TEXTURE);
280 G3_Identity();
281 G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
282
283 // Draw 200 cubes
284 for (i = 0; i < 200; i++)
285 {
286 // Rotate and translate cubes
287 fx16 s = FX_SinIdx(Rotate);
288 fx16 c = FX_CosIdx(Rotate);
289
290 G3_PushMtx();
291
292 G3_Translate(0, 0, -5 * FX32_ONE);
293
294 G3_RotX(s, c);
295 G3_RotY(s, c);
296 G3_RotZ(s, c);
297
298 // Draw cube
299 Cube(texAddr);
300
301 G3_PopMtx(1);
302 }
303
304 while (G3X_IsGeometryBusy())
305 {
306 }
307
308 // Print ListRam information
309 OS_Printf("PolygonListRAM = %d/2048\n", G3X_GetPolygonListRamCount());
310 OS_Printf("VertexListRAM = %d/6144\n", G3X_GetVtxListRamCount());
311
312 // swapping the polygon list RAM, the vertex RAM, etc.
313 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
314
315 // Wait VBlank
316 OS_WaitVBlankIntr();
317
318 // Print Rendering information
319 OS_Printf("LineCnt=%d\n", G3X_GetRenderedLineCount());
320 OS_Printf("LinesOverFlow = %s\n\n", (G3X_IsLineBufferUnderflow()? "TRUE" : "FALSE"));
321
322
323 #ifdef SDK_AUTOTEST // code for auto test
324 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
325 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
326 EXT_TestScreenShot(100, 0x9ECAB1AD);
327 EXT_TestTickCounter();
328 #endif //SDK_AUTOTEST
329 }
330 }
331