1 /*---------------------------------------------------------------------------*
2 Project: The test of switch to Progressive from Interlace
3 File: smp-onetri_Progressive.c
4
5 Copyright 2006 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 $Log: smp-onetri_Progressive.c,v $
14 Revision 1.5 2008/06/26 02:48:01 urata
15 Removed MPAL defines
16
17 Revision 1.4 2006/08/10 13:40:00 urata
18 Revised the rendermode of pal progressive.
19
20 Revision 1.3 2006/07/20 07:35:06 urata
21 Corrected typo "ntsc" to "NTSC".
22
23 Revision 1.2 2006/06/06 23:55:19 urata
24 Renamed VI[Set/Get]ProgressiveTest to VI[Set/Get]VSyncTimingTest.
25
26 Revision 1.1 2006/06/05 00:38:30 urata
27 Initial check-in.
28
29 $NoKeywords: $
30 *---------------------------------------------------------------------------*/
31 #include <demo.h>
32
33 #define NTSC // if you change here, you can see PAL
34
35 /*---------------------------------------------------------------------------*
36 Model Data
37 *---------------------------------------------------------------------------*/
38 #define STRUT_LN 130 // long side of strut
39 #define STRUT_SD 4 // short side of strut
40 #define JOINT_SD 10 // joint is a cube
41
42 /*---------------------------------------------------------------------------*
43 The macro ATTRIBUTE_ALIGN provides a convenient way to align initialized
44 arrays. Alignment of vertex arrays to 32B IS NOT required, but may result
45 in a slight performance improvement.
46 *---------------------------------------------------------------------------*/
47 s16 Verts_s16[] ATTRIBUTE_ALIGN(32) =
48 {
49 // x y z
50 -STRUT_SD, STRUT_SD, -STRUT_SD, // 0
51 STRUT_SD, STRUT_SD, -STRUT_SD, // 1
52 STRUT_SD, STRUT_SD, STRUT_SD, // 2
53 -STRUT_SD, STRUT_SD, STRUT_SD, // 3
54 STRUT_SD, -STRUT_SD, -STRUT_SD, // 4
55 STRUT_SD, -STRUT_SD, STRUT_SD, // 5
56 STRUT_SD, STRUT_LN, -STRUT_SD, // 6
57 STRUT_SD, STRUT_LN, STRUT_SD, // 7
58 -STRUT_SD, STRUT_LN, STRUT_SD, // 8
59 -STRUT_SD, STRUT_SD, -STRUT_LN, // 9
60 STRUT_SD, STRUT_SD, -STRUT_LN, // 10
61 STRUT_SD, -STRUT_SD, -STRUT_LN, // 11
62 STRUT_LN, STRUT_SD, -STRUT_SD, // 12
63 STRUT_LN, STRUT_SD, STRUT_SD, // 13
64 STRUT_LN, -STRUT_SD, STRUT_SD, // 14
65 -JOINT_SD, JOINT_SD, -JOINT_SD, // 15
66 JOINT_SD, JOINT_SD, -JOINT_SD, // 16
67 JOINT_SD, JOINT_SD, JOINT_SD, // 17
68 -JOINT_SD, JOINT_SD, JOINT_SD, // 18
69 JOINT_SD, -JOINT_SD, -JOINT_SD, // 19
70 JOINT_SD, -JOINT_SD, JOINT_SD, // 20
71 -JOINT_SD, -JOINT_SD, JOINT_SD // 21
72 };
73
74 u8 Colors_rgba8[] ATTRIBUTE_ALIGN(32) =
75 {
76 // r, g, b, a
77 42, 42, 50, 255, // 0
78 80, 80, 80, 255, // 1
79 114, 114, 110, 255 // 2
80 };
81
82 /*---------------------------------------------------------------------------*
83 Forward references
84 *---------------------------------------------------------------------------*/
85
86 void main ( void );
87 static void CameraInit ( Mtx v );
88 static void DrawInit ( void );
89 static void DrawTick ( Mtx v );
90 static void AnimTick ( Mtx v );
91 static void PrintIntro ( void );
92
93 /*---------------------------------------------------------------------------*
94 Application main loop
95 *---------------------------------------------------------------------------*/
main(void)96 void main ( void )
97 {
98 Mtx v; // View matrix
99 PADStatus pad[PAD_MAX_CONTROLLERS]; // Game pad state
100 u32 frame = 0;
101
102
103 pad[0].button = 0;
104
105 #if defined(NTSC)
106 DEMOInit(&GXNtsc480Int); // Init os, pad, gx, vi
107 #elif defined(PAL)
108 DEMOInit(&GXPal528Int); // Init os, pad, gx, vi
109 #elif defined(EURGB60)
110 DEMOInit(&GXEurgb60Hz480Int); // Init os, pad, gx, vi
111 #endif
112
113 // Set VISetVSyncTimingTest when the rendermode is Interlace.
114 VISetVSyncTimingTest();
115
116 CameraInit(v); // Initialize the camera.
117 DrawInit(); // Define my vertex formats and set array pointers.
118
119 PrintIntro(); // Print demo directions
120
121 while(!(pad[0].button & PAD_BUTTON_MENU))
122 {
123 DEMOBeforeRender();
124 DrawTick(v); // Draw the model.
125 DEMODoneRender();
126 AnimTick(v); // Update animation.
127 PADRead(pad);
128
129
130 if(frame == 300)
131 {
132 #if defined(NTSC)
133 VIConfigure(&GXNtsc480Prog);
134 #elif defined(PAL)
135 VIConfigure(&GXEurgb60Hz480Prog);
136 #elif defined(EURGB60)
137 VIConfigure(&GXEurgb60Hz480Prog);
138 #endif
139 VIFlush();
140 VIWaitForRetrace();
141 VIWaitForRetrace();
142
143 // Call the function "VIGetVSyncTimingTest" after Progressive mode.
144 OSReport("The test of switch to Progressive from Interlace -> %s\n",
145 VIGetVSyncTimingTest() == 0 ? "OK" : "NG");
146 }
147 frame++;
148 }
149 OSHalt("End of demo");
150 }
151
152
153 /*---------------------------------------------------------------------------*
154 Functions
155 *---------------------------------------------------------------------------*/
156
157 /*---------------------------------------------------------------------------*
158 Name: CameraInit
159
160 Description: Initialize the projection matrix and load into hardware.
161 Initialize the view matrix.
162
163 Arguments: v : view matrix
164
165 Returns: None.
166 *---------------------------------------------------------------------------*/
CameraInit(Mtx v)167 static void CameraInit ( Mtx v )
168 {
169 Mtx44 p; // Projection matrix.
170 Vec up = {0.20F, 0.97F, 0.0F};
171 Vec camLoc = {90.0F, 110.0F, 13.0F};
172 Vec objPt = {-110.0F, -70.0F, -190.0F};
173 f32 left = 24.0F;
174 f32 top = 32.0F;
175 f32 znear = 50.0F;
176 f32 zfar = 2000.0F;
177
178 MTXFrustum(p, left, -left, -top, top, znear, zfar);
179 GXSetProjection(p, GX_PERSPECTIVE);
180
181 MTXLookAt(v, &camLoc, &up, &objPt);
182 }
183
184 /*---------------------------------------------------------------------------*
185 Name: DrawInit
186
187 Description: Initializes the vertex attribute format 0, and sets
188 the array pointers and strides for the indexed data.
189
190 Arguments: None.
191
192 Returns: None.
193 *---------------------------------------------------------------------------*/
DrawInit(void)194 static void DrawInit( void )
195 {
196 GXColor black = {0, 0, 0, 0};
197
198 GXSetCopyClear(black, 0x00FFFFFF);
199
200 // Set current vertex descriptor to enable position and color0.
201 // Both use 8b index to access their data arrays.
202 GXClearVtxDesc();
203 GXSetVtxDesc(GX_VA_POS, GX_INDEX8);
204 GXSetVtxDesc(GX_VA_CLR0, GX_INDEX8);
205
206 // Position has 3 elements (x,y,z), each of type s16,
207 // no fractional bits (integers)
208 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
209
210 // Color 0 has 4 components (r, g, b, a), each component is 8b.
211 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
212
213 // stride = 3 elements (x,y,z) each of type s16
214 GXSetArray(GX_VA_POS, Verts_s16, 3*sizeof(s16));
215 // stride = 4 elements (r,g,b,a) each of type u8
216 GXSetArray(GX_VA_CLR0, Colors_rgba8, 4*sizeof(u8));
217
218 // Initialize lighting, texgen, and tev parameters
219 GXSetNumChans(1); // default, color = vertex color
220 GXSetNumTexGens(0); // no texture in this demo
221 GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
222 GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
223 }
224
225 /*---------------------------------------------------------------------------*
226 Name: Vertex
227
228 Description: Create my vertex format
229
230 Arguments: v 8-bit position index
231 c 8-bit color index
232
233 Returns: None.
234 *---------------------------------------------------------------------------*/
Vertex(u8 v,u8 c)235 static inline void Vertex( u8 v, u8 c )
236 {
237 GXPosition1x8(v);
238 GXColor1x8(c);
239 }
240
241 /*---------------------------------------------------------------------------*
242 Name: DrawFsQuad
243
244 Description: Draw a flat-shaded quad.
245
246 Arguments: v0 8-bit position index 0
247 v1 8-bit position index 1
248 v2 8-bit position index 2
249 v3 8-bit position index 3
250 c 8-bit color index
251
252 Returns: None.
253 *---------------------------------------------------------------------------*/
DrawFsQuad(u8 v0,u8 v1,u8 v2,u8 v3,u8 c)254 static inline void DrawFsQuad(
255 u8 v0,
256 u8 v1,
257 u8 v2,
258 u8 v3,
259 u8 c )
260 {
261 Vertex(v0, c);
262 Vertex(v1, c);
263 Vertex(v2, c);
264 Vertex(v3, c);
265 }
266
267 /*---------------------------------------------------------------------------*
268 Name: DrawTick
269
270 Description: Draw the model once. Replicates a simple strut model
271 many times in the x, y, z directions to create a dense
272 3D grid. GXInit makes GX_PNMTX0 the default matrix.
273
274 Arguments: v : view matrix
275
276 Returns: None.
277 *---------------------------------------------------------------------------*/
DrawTick(Mtx v)278 static void DrawTick( Mtx v )
279 {
280 f32 x; // Translation in x.
281 f32 y; // Translation in y.
282 f32 z; // Translation in z.
283 Mtx m; // Model matrix.
284 Mtx mv; // Modelview matrix.
285
286 GXSetNumTexGens( 0 );
287 GXSetNumTevStages( 1 );
288 GXSetTevOp( GX_TEVSTAGE0, GX_PASSCLR );
289
290 MTXIdentity(m);
291
292 for(x = -10*STRUT_LN; x < 2*STRUT_LN; x += STRUT_LN)
293 {
294 for(y = -10*STRUT_LN; y < STRUT_LN; y += STRUT_LN)
295 {
296 for(z = STRUT_LN; z > -10*STRUT_LN; z -= STRUT_LN)
297 {
298 MTXRowCol(m, 0, 3) = x;
299 MTXRowCol(m, 1, 3) = y;
300 MTXRowCol(m, 2, 3) = z;
301 MTXConcat(v, m, mv);
302 GXLoadPosMtxImm(mv, GX_PNMTX0);
303
304 GXBegin(GX_QUADS, GX_VTXFMT0, 36); //4 vtx/qd x 9 qd = 36 vtx
305 DrawFsQuad(8, 7, 2, 3, 0);
306 DrawFsQuad(1, 2, 7, 6, 1);
307 DrawFsQuad(1, 0, 9, 10, 2);
308 DrawFsQuad(4, 1, 10, 11, 1);
309 DrawFsQuad(1, 12, 13, 2, 2);
310 DrawFsQuad(2, 13, 14, 5, 0);
311 DrawFsQuad(18, 15, 16, 17, 2);
312 DrawFsQuad(20, 17, 16, 19, 1);
313 DrawFsQuad(20, 21, 18, 17, 0);
314 GXEnd();
315 }
316 }
317 }
318 }
319
320 /*---------------------------------------------------------------------------*
321 Name: AnimTick
322
323 Description: Moves viewpoint through the grid. Loops animation so
324 that it appears viewpoint is continuously moving forward.
325
326 Arguments: v : view matrix
327
328 Returns: None.
329 *---------------------------------------------------------------------------*/
AnimTick(Mtx v)330 static void AnimTick( Mtx v )
331 {
332 static u32 ticks = 0; // Counter.
333 Mtx fwd; // Forward stepping translation matrix.
334 Mtx back; // Loop back translation matrix.
335
336 u32 animSteps = 100;
337 f32 animLoopBack = (f32)STRUT_LN;
338 f32 animStepFwd = animLoopBack / animSteps;
339
340 MTXTrans(fwd, 0, 0, animStepFwd);
341 MTXTrans(back, 0, 0, -animLoopBack);
342
343 MTXConcat(v, fwd, v);
344 if((ticks % animSteps) == 0)
345 MTXConcat(v, back, v);
346
347 ticks++;
348 }
349
350 /*---------------------------------------------------------------------------*
351 Name: PrintIntro
352
353 Description: Prints the directions on how to use this demo.
354
355 Arguments: None.
356
357 Returns: None.
358 *---------------------------------------------------------------------------*/
PrintIntro(void)359 static void PrintIntro( void )
360 {
361 OSReport("\n\n****************************************\n");
362 OSReport("to quit:\n");
363 OSReport(" hit the start button\n");
364 OSReport("****************************************\n");
365 }
366