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