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