1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin
3   File:     smp-onetri.c
4 
5   Copyright 1998, 1999 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_PAL.c,v $
14     Revision 1.2  2009/05/19 01:31:37  urata
15     Modified the function "ChangeMode" to initialize correctly with DEMOInitGX.
16 
17     Revision 1.1  2005/12/16 08:34:25  urata
18     Initial checkin.
19 
20 
21     4     12/17/01 6:08p Hashida
22     Changed to use GXGetYScaleFactor.
23 
24     3     01/11/13 15:41 Hashida
25     Reverted the change.
26 
27     1     11/04/01 10:37a Hashida
28     initial revision.
29 
30     6     10/18/01 10:31p Hashida
31     Modified so that you can switch NTSC/PAL with A button.
32 
33     5     2/14/01 11:09a Hirose
34     Changed to accept start button only to quit. Deleted obsolete ifdefs
35 
36     4     10/25/00 8:59p Hirose
37     A flag fix MAC -> MACOS
38 
39     3     5/19/00 3:20a Hirose
40     made objects brighter
41 
42     2     3/23/00 8:01p Hirose
43     fixed instruction message
44 
45     1     3/16/00 7:24p Alligator
46     rename to match other demos conventions
47 
48     1     3/06/00 12:11p Alligator
49     move from gxdemos to gxdemos/Simple
50 
51     4     1/19/00 6:15p Alligator
52 
53     4     6/07/99 8:32p Alligator
54     removed cast to void *
55 
56     3     6/07/99 4:35p Alligator
57     fix VI function name
58 
59     2     6/07/99 3:44p Alligator
60     added heap and framebuffer allocation
61 
62     1     6/04/99 11:25p Alligator
63     First Immediate mode demo
64 
65 
66   $NoKeywords: $
67  *---------------------------------------------------------------------------*/
68 
69 #include <demo.h>
70 
71 /*---------------------------------------------------------------------------*
72   Model Data
73  *---------------------------------------------------------------------------*/
74 #define STRUT_LN        130     // long side of strut
75 #define STRUT_SD        4       // short side of strut
76 #define JOINT_SD        10      // joint is a cube
77 
78 /*---------------------------------------------------------------------------*
79    The macro ATTRIBUTE_ALIGN provides a convenient way to align initialized
80    arrays.  Alignment of vertex arrays to 32B IS NOT required, but may result
81    in a slight performance improvement.
82  *---------------------------------------------------------------------------*/
83 s16 Verts_s16[] ATTRIBUTE_ALIGN(32) =
84 {
85 //      x             y             z
86     -STRUT_SD,     STRUT_SD,    -STRUT_SD,   // 0
87      STRUT_SD,     STRUT_SD,    -STRUT_SD,   // 1
88      STRUT_SD,     STRUT_SD,     STRUT_SD,   // 2
89     -STRUT_SD,     STRUT_SD,     STRUT_SD,   // 3
90      STRUT_SD,    -STRUT_SD,    -STRUT_SD,   // 4
91      STRUT_SD,    -STRUT_SD,     STRUT_SD,   // 5
92      STRUT_SD,     STRUT_LN,    -STRUT_SD,   // 6
93      STRUT_SD,     STRUT_LN,     STRUT_SD,   // 7
94     -STRUT_SD,     STRUT_LN,     STRUT_SD,   // 8
95     -STRUT_SD,     STRUT_SD,    -STRUT_LN,   // 9
96      STRUT_SD,     STRUT_SD,    -STRUT_LN,   // 10
97      STRUT_SD,    -STRUT_SD,    -STRUT_LN,   // 11
98      STRUT_LN,     STRUT_SD,    -STRUT_SD,   // 12
99      STRUT_LN,     STRUT_SD,     STRUT_SD,   // 13
100      STRUT_LN,    -STRUT_SD,     STRUT_SD,   // 14
101     -JOINT_SD,     JOINT_SD,    -JOINT_SD,   // 15
102      JOINT_SD,     JOINT_SD,    -JOINT_SD,   // 16
103      JOINT_SD,     JOINT_SD,     JOINT_SD,   // 17
104     -JOINT_SD,     JOINT_SD,     JOINT_SD,   // 18
105      JOINT_SD,    -JOINT_SD,    -JOINT_SD,   // 19
106      JOINT_SD,    -JOINT_SD,     JOINT_SD,   // 20
107     -JOINT_SD,    -JOINT_SD,     JOINT_SD    // 21
108 };
109 
110 u8 Colors_rgba8[] ATTRIBUTE_ALIGN(32) =
111 {
112 //   r,   g,   b,   a
113     42,  42,  50, 255,   // 0
114     80,  80,  80, 255,   // 1
115    114, 114, 110, 255    // 2
116 };
117 
118 GXRenderModeObj Pal574IntDfScale =
119 {
120     VI_TVMODE_PAL_INT,      // viDisplayMode
121     640,             // fbWidth
122     480,             // efbHeight
123     574,             // xfbHeight
124     (VI_MAX_WIDTH_PAL - 640)/2,         // viXOrigin
125     (VI_MAX_HEIGHT_PAL - 574)/2,        // viYOrigin
126     640,             // viWidth
127     574,             // viHeight
128     VI_XFBMODE_DF,   // xFBmode
129     GX_FALSE,        // field_rendering
130     GX_FALSE,        // aa
131 
132     // sample points arranged in increasing Y order
133      6,  6,  6,  6,  6,  6,  // pix 0, 3 sample points, 1/12 units, 4 bits each
134      6,  6,  6,  6,  6,  6,  // pix 1
135      6,  6,  6,  6,  6,  6,  // pix 2
136      6,  6,  6,  6,  6,  6,  // pix 3
137 
138     // vertical filter[7], 1/64 units, 6 bits each
139      8,         // line n-1
140      8,         // line n-1
141     10,         // line n
142     12,         // line n
143     10,         // line n
144      8,         // line n+1
145      8          // line n+1
146 };
147 
148 GXRenderModeObj Pal480IntDfNoScale =
149 {
150     VI_TVMODE_PAL_INT,      // viDisplayMode
151     640,             // fbWidth
152     480,             // efbHeight
153     480,             // xfbHeight
154     (VI_MAX_WIDTH_PAL - 640)/2,         // viXOrigin
155     (VI_MAX_HEIGHT_PAL - 480)/2,        // viYOrigin
156     640,             // viWidth
157     480,             // viHeight
158     VI_XFBMODE_DF,   // xFBmode
159     GX_FALSE,        // field_rendering
160     GX_FALSE,        // aa
161 
162     // sample points arranged in increasing Y order
163      6,  6,  6,  6,  6,  6,  // pix 0, 3 sample points, 1/12 units, 4 bits each
164      6,  6,  6,  6,  6,  6,  // pix 1
165      6,  6,  6,  6,  6,  6,  // pix 2
166      6,  6,  6,  6,  6,  6,  // pix 3
167 
168     // vertical filter[7], 1/64 units, 6 bits each
169      8,         // line n-1
170      8,         // line n-1
171     10,         // line n
172     12,         // line n
173     10,         // line n
174      8,         // line n+1
175      8          // line n+1
176 };
177 
178 
179 
180 enum {
181     MODE_PAL_NOSCALE = 0,
182     MODE_PAL_SCALE,
183     MODE_EURGB60,
184     MODE_MAX
185 };
186 
187 /*---------------------------------------------------------------------------*
188    Forward references
189  *---------------------------------------------------------------------------*/
190 
191 void        main            ( void );
192 static void CameraInit      ( Mtx v );
193 static void DrawInit        ( void );
194 static void DrawTick        ( Mtx v );
195 static void AnimTick        ( Mtx v );
196 static void PrintIntro      ( void );
197 
198 GXRenderModeObj*            CurrentMode;
199 
200 /*---------------------------------------------------------------------------*
201    Application main loop
202  *---------------------------------------------------------------------------*/
203 
ChangeMode(GXRenderModeObj * mode)204 static void ChangeMode(GXRenderModeObj* mode)
205 {
206     DEMOSetRenderMode(mode);
207 
208     // including GXSetDispCopyYScale.
209     DEMOInitGX();
210     DEMOStartVI();
211 }
212 
main(void)213 void main ( void )
214 {
215     Mtx         v;   // view matrix
216     PADStatus   pad[PAD_MAX_CONTROLLERS]; // game pad state
217     u32         oldButtons;
218     u32         changedButtons;
219     u32         mode;
220 
221     pad[0].button = 0;
222     oldButtons = 0;
223 
224     OSReport("\n");
225     OSReport("======================================\n");
226     OSReport("PAL version of smp-onetri\n");
227     OSReport("Press A button to change mode\n");
228     OSReport("======================================\n");
229     OSReport("\n");
230 
231     // Allocate the maximum size of XFB at DEMOInit.
232     DEMOInit(&Pal574IntDfScale);    // Init os, pad, gx, vi
233     mode = MODE_PAL_SCALE;
234     OSReport("PAL (Y scaling)\n");
235 
236     CameraInit(v); // Initialize the camera.
237     DrawInit();    // Define my vertex formats and set array pointers.
238 
239     PrintIntro(); // Print demo directions
240 
241     while(!(pad[0].button & PAD_BUTTON_MENU))
242     {
243         DEMOBeforeRender();
244         DrawTick(v);        // Draw the model.
245         DEMODoneRender();
246         AnimTick(v);        // Update animation.
247         PADRead(pad);
248 
249         changedButtons = (oldButtons ^ pad[0].button) & pad[0].button;
250 
251         if (changedButtons & PAD_BUTTON_A)
252         {
253             mode++;
254             if (mode == MODE_MAX)
255             {
256                 mode = 0;
257             }
258 
259             switch (mode)
260             {
261               case MODE_PAL_NOSCALE:
262                 CurrentMode = &Pal480IntDfNoScale;
263                 ChangeMode(CurrentMode);
264                 OSReport("PAL (black bars)\n");
265                 break;
266 
267               case MODE_PAL_SCALE:
268                 CurrentMode = &Pal574IntDfScale;
269                 ChangeMode(CurrentMode);
270                 OSReport("PAL (Y scaling)\n");
271                 break;
272 
273               case MODE_EURGB60:
274                 CurrentMode = &GXEurgb60Hz480IntDf;
275                 ChangeMode(CurrentMode);
276                 OSReport("EU RGB 60Hz\n");
277                 break;
278             }
279         }
280 
281         oldButtons = pad[0].button;
282     }
283 
284     OSHalt("End of demo");
285 }
286 
287 
288 /*---------------------------------------------------------------------------*
289    Functions
290  *---------------------------------------------------------------------------*/
291 
292 /*---------------------------------------------------------------------------*
293     Name:           CameraInit
294 
295     Description:    Initialize the projection matrix and load into hardware.
296                     Initialize the view matrix.
297 
298     Arguments:      v      view matrix
299 
300     Returns:        none
301  *---------------------------------------------------------------------------*/
CameraInit(Mtx v)302 static void CameraInit ( Mtx v )
303 {
304     Mtx44   p;      // projection matrix
305     Vec     up      = {0.20F, 0.97F, 0.0F};
306     Vec     camLoc  = {90.0F, 110.0F, 13.0F};
307     Vec     objPt   = {-110.0F, -70.0F, -190.0F};
308     f32     left    = 24.0F;
309     f32     top     = 32.0F;
310     f32     znear   = 50.0F;
311     f32     zfar    = 2000.0F;
312 
313     MTXFrustum(p, left, -left, -top, top, znear, zfar);
314     GXSetProjection(p, GX_PERSPECTIVE);
315 
316     MTXLookAt(v, &camLoc, &up, &objPt);
317 }
318 
319 /*---------------------------------------------------------------------------*
320     Name:           DrawInit
321 
322     Description:    Initializes the vertex attribute format 0, and sets
323                     the array pointers and strides for the indexed data.
324 
325     Arguments:      none
326 
327     Returns:        none
328  *---------------------------------------------------------------------------*/
DrawInit(void)329 static void DrawInit( void )
330 {
331     GXColor black = {0, 0, 0, 0};
332 
333     GXSetCopyClear(black, 0x00FFFFFF);
334 
335     // Set current vertex descriptor to enable position and color0.
336     // Both use 8b index to access their data arrays.
337     GXClearVtxDesc();
338     GXSetVtxDesc(GX_VA_POS, GX_INDEX8);
339     GXSetVtxDesc(GX_VA_CLR0, GX_INDEX8);
340 
341     // Position has 3 elements (x,y,z), each of type s16,
342     // no fractional bits (integers)
343     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
344 
345     // Color 0 has 4 components (r, g, b, a), each component is 8b.
346     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
347 
348     // stride = 3 elements (x,y,z) each of type s16
349     GXSetArray(GX_VA_POS, Verts_s16, 3*sizeof(s16));
350     // stride = 4 elements (r,g,b,a) each of type u8
351     GXSetArray(GX_VA_CLR0, Colors_rgba8, 4*sizeof(u8));
352 
353     // Initialize lighting, texgen, and tev parameters
354     GXSetNumChans(1); // default, color = vertex color
355     GXSetNumTexGens(0); // no texture in this demo
356     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
357     GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
358 }
359 
360 /*---------------------------------------------------------------------------*
361     Name:           Vertex
362 
363     Description:    Create my vertex format
364 
365     Arguments:      v        8-bit position index
366                     c        8-bit color index
367 
368     Returns:        none
369  *---------------------------------------------------------------------------*/
Vertex(u8 v,u8 c)370 static inline void Vertex( u8 v, u8 c )
371 {
372     GXPosition1x8(v);
373     GXColor1x8(c);
374 }
375 
376 /*---------------------------------------------------------------------------*
377     Name:           DrawFsQuad
378 
379     Description:    Draw a flat-shaded quad.
380 
381     Arguments:      v0    8-bit position index 0
382                     v1    8-bit position index 1
383                     v2    8-bit position index 2
384                     v3    8-bit position index 3
385                     c     8-bit color index
386 
387     Returns:        none
388  *---------------------------------------------------------------------------*/
DrawFsQuad(u8 v0,u8 v1,u8 v2,u8 v3,u8 c)389 static inline void DrawFsQuad(
390     u8 v0,
391     u8 v1,
392     u8 v2,
393     u8 v3,
394     u8 c )
395 {
396     Vertex(v0, c);
397     Vertex(v1, c);
398     Vertex(v2, c);
399     Vertex(v3, c);
400 }
401 
402 /*---------------------------------------------------------------------------*
403     Name:           DrawTick
404 
405     Description:    Draw the model once.  Replicates a simple strut model
406                     many times in the x, y, z directions to create a dense
407                     3D grid.  GXInit makes GX_PNMTX0 the default matrix.
408 
409     Arguments:      v        view matrix
410 
411     Returns:        none
412  *---------------------------------------------------------------------------*/
DrawTick(Mtx v)413 static void DrawTick( Mtx v )
414 {
415     f32  x;  // Translation in x.
416     f32  y;  // Translation in y.
417     f32  z;  // Translation in z.
418     Mtx  m;  // Model matrix.
419     Mtx  mv; // Modelview matrix.
420 
421     GXSetNumTexGens( 0 );
422     GXSetNumTevStages( 1 );
423     GXSetTevOp( GX_TEVSTAGE0, GX_PASSCLR );
424 
425     MTXIdentity(m);
426 
427     for(x = -10*STRUT_LN; x < 2*STRUT_LN; x += STRUT_LN)
428     {
429         for(y = -10*STRUT_LN; y < STRUT_LN; y += STRUT_LN)
430         {
431             for(z = STRUT_LN; z > -10*STRUT_LN; z -= STRUT_LN)
432             {
433                 MTXRowCol(m, 0, 3) = x;
434                 MTXRowCol(m, 1, 3) = y;
435                 MTXRowCol(m, 2, 3) = z;
436                 MTXConcat(v, m, mv);
437                 GXLoadPosMtxImm(mv, GX_PNMTX0);
438 
439                 GXBegin(GX_QUADS, GX_VTXFMT0, 36); //4 vtx/qd x 9 qd = 36 vtx
440                     DrawFsQuad(8, 7, 2, 3, 0);
441                     DrawFsQuad(1, 2, 7, 6, 1);
442                     DrawFsQuad(1, 0, 9, 10, 2);
443                     DrawFsQuad(4, 1, 10, 11, 1);
444                     DrawFsQuad(1, 12, 13, 2, 2);
445                     DrawFsQuad(2, 13, 14, 5, 0);
446                     DrawFsQuad(18, 15, 16, 17, 2);
447                     DrawFsQuad(20, 17, 16, 19, 1);
448                     DrawFsQuad(20, 21, 18, 17, 0);
449                 GXEnd();
450             }
451         }
452     }
453 }
454 
455 /*---------------------------------------------------------------------------*
456     Name:           AnimTick
457 
458     Description:    Moves viewpoint through the grid.  Loops animation so
459                     that it appears viewpoint is continously moving forward.
460 
461     Arguments:      v        view matrix
462 
463     Returns:        none
464  *---------------------------------------------------------------------------*/
AnimTick(Mtx v)465 static void AnimTick( Mtx v )
466 {
467     static u32  ticks = 0; // Counter.
468     Mtx         fwd;       // Forward stepping translation matrix.
469     Mtx         back;      // Loop back translation matrix.
470 
471     u32    animSteps    = 100;
472     f32    animLoopBack = (f32)STRUT_LN;
473     f32    animStepFwd  = animLoopBack / animSteps;
474 
475     MTXTrans(fwd, 0, 0, animStepFwd);
476     MTXTrans(back, 0, 0, -animLoopBack);
477 
478     MTXConcat(v, fwd, v);
479     if((ticks % animSteps) == 0)
480         MTXConcat(v, back, v);
481 
482     ticks++;
483 }
484 
485 /*---------------------------------------------------------------------------*
486     Name:           PrintIntro
487 
488     Description:    Prints the directions on how to use this demo.
489 
490     Arguments:      none
491 
492     Returns:        none
493  *---------------------------------------------------------------------------*/
PrintIntro(void)494 static void PrintIntro( void )
495 {
496     OSReport("\n\n****************************************\n");
497     OSReport("to quit:\n");
498     OSReport("     hit the start button\n");
499 #ifdef MACOS
500     OSReport("     click on the text output window\n");
501     OSReport("     select quit from the menu or hit 'command q'\n");
502     OSReport("     select 'don't save'\n");
503 #endif // MACOS
504     OSReport("****************************************\n");
505 }
506