1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     smp-onetri_dl.c
4 
5   Copyright 1998-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 
14 #include <demo.h>
15 
16 /*---------------------------------------------------------------------------*
17   Model Data
18  *---------------------------------------------------------------------------*/
19 #define STRUT_LN        130     // long side of strut
20 #define STRUT_SD        4       // short side of strut
21 #define JOINT_SD        10      // joint is a cube
22 
23 /*---------------------------------------------------------------------------*
24    Display list pointers must be 32B aligned.  Attribute arrays ARE NOT
25    required to be 32B aligned, but doing so may result in a slight
26    performance improvement.
27    The macro ATTRIBUTE_ALIGN provides a convenient way to align initialized
28    arrays.
29  *---------------------------------------------------------------------------*/
30 /*---------------------------------------------------------------------------*
31    Connectivity display list.  Normally a display list would be created by a
32    an external tool and loaded from ROM. We define this one in ASCII to make
33    the format readable.
34  *---------------------------------------------------------------------------*/
35 u8 OneTriDL[] ATTRIBUTE_ALIGN(32) =
36 {
37     (GX_DRAW_QUADS | GX_VTXFMT0),      // command, primitive type | vat idx
38     0, 36,                             // number of verts, 16b
39     8,  0,  7, 0,  2, 0,  3, 0,        // quad 0
40     1,  1,  2, 1,  7, 1,  6, 1,        // quad 1
41     1,  2,  0, 2,  9, 2, 10, 2,        // quad 2
42     4,  1,  1, 1, 10, 1, 11, 1,        // quad 3
43     1,  2, 12, 2, 13, 2,  2, 2,        // quad 4
44     2,  0, 13, 0, 14, 0,  5, 0,        // quad 5
45     18, 2, 15, 2, 16, 2, 17, 2,        // quad 6
46     20, 1, 17, 1, 16, 1, 19, 1,        // quad 7
47     20, 0, 21, 0, 18, 0, 17, 0,        // quad 8
48     GX_NOP, GX_NOP, GX_NOP, GX_NOP, GX_NOP, GX_NOP, GX_NOP, // pad
49     GX_NOP, GX_NOP, GX_NOP, GX_NOP, GX_NOP, GX_NOP, GX_NOP, // pad
50     GX_NOP, GX_NOP, GX_NOP, GX_NOP, GX_NOP, GX_NOP, GX_NOP  // pad to 32B
51 };
52 
53 s16 Verts_s16[] ATTRIBUTE_ALIGN(32) =
54 {
55 //      x             y             z
56     -STRUT_SD,     STRUT_SD,    -STRUT_SD,   // 0
57      STRUT_SD,     STRUT_SD,    -STRUT_SD,   // 1
58      STRUT_SD,     STRUT_SD,     STRUT_SD,   // 2
59     -STRUT_SD,     STRUT_SD,     STRUT_SD,   // 3
60      STRUT_SD,    -STRUT_SD,    -STRUT_SD,   // 4
61      STRUT_SD,    -STRUT_SD,     STRUT_SD,   // 5
62      STRUT_SD,     STRUT_LN,    -STRUT_SD,   // 6
63      STRUT_SD,     STRUT_LN,     STRUT_SD,   // 7
64     -STRUT_SD,     STRUT_LN,     STRUT_SD,   // 8
65     -STRUT_SD,     STRUT_SD,    -STRUT_LN,   // 9
66      STRUT_SD,     STRUT_SD,    -STRUT_LN,   // 10
67      STRUT_SD,    -STRUT_SD,    -STRUT_LN,   // 11
68      STRUT_LN,     STRUT_SD,    -STRUT_SD,   // 12
69      STRUT_LN,     STRUT_SD,     STRUT_SD,   // 13
70      STRUT_LN,    -STRUT_SD,     STRUT_SD,   // 14
71     -JOINT_SD,     JOINT_SD,    -JOINT_SD,   // 15
72      JOINT_SD,     JOINT_SD,    -JOINT_SD,   // 16
73      JOINT_SD,     JOINT_SD,     JOINT_SD,   // 17
74     -JOINT_SD,     JOINT_SD,     JOINT_SD,   // 18
75      JOINT_SD,    -JOINT_SD,    -JOINT_SD,   // 19
76      JOINT_SD,    -JOINT_SD,     JOINT_SD,   // 20
77     -JOINT_SD,    -JOINT_SD,     JOINT_SD    // 21
78 };
79 
80 u8 Colors_rgba8[] ATTRIBUTE_ALIGN(32) =
81 {
82 //   r,   g,   b,   a
83     42,  42,  50, 255,   // 0
84     80,  80,  80, 255,   // 1
85    114, 114, 110, 255    // 2
86 };
87 
88 
89 /*---------------------------------------------------------------------------*
90    Forward references
91  *---------------------------------------------------------------------------*/
92 
93 void        main            ( void );
94 static void CameraInit      ( Mtx v );
95 static void DrawInit        ( void );
96 static void DrawTick        ( Mtx v );
97 static void AnimTick        ( Mtx v );
98 static void PrintIntro      ( void );
99 
100 /*---------------------------------------------------------------------------*
101    Application main loop
102  *---------------------------------------------------------------------------*/
103 
main(void)104 void main ( void )
105 {
106     Mtx         v;   // view matrix
107     PADStatus   pad[PAD_MAX_CONTROLLERS]; // game pad state
108 
109     pad[0].button = 0;
110 
111     DEMOInit(NULL);    // init os, pad, gx, vi
112 
113     CameraInit(v); // Initialize the camera.
114     DrawInit();    // Define my vertex formats and set array pointers.
115 
116     PrintIntro(); // Print demo directions
117 
118     while(!(pad[0].button & PAD_BUTTON_MENU)) // any button quits
119     {
120         DEMOBeforeRender();
121         DrawTick(v);         // Draw the model.
122         DEMODoneRender();
123         AnimTick(v);         // Update animation.
124         PADRead(pad);        // read the game pad
125     }
126 
127     OSHalt("End of demo");
128 }
129 
130 
131 /*---------------------------------------------------------------------------*
132    Functions
133  *---------------------------------------------------------------------------*/
134 
135 /*---------------------------------------------------------------------------*
136     Name:           CameraInit
137 
138     Description:    Initialize the projection matrix and load into hardware.
139                     Initialize the view matrix.
140 
141     Arguments:      v      view matrix
142 
143     Returns:        none
144  *---------------------------------------------------------------------------*/
CameraInit(Mtx v)145 static void CameraInit ( Mtx v )
146 {
147     Mtx44   p;      // projection matrix
148     Vec     up      = {0.20F, 0.97F, 0.0F};
149     Vec     camLoc  = {90.0F, 110.0F, 13.0F};
150     Vec     objPt   = {-110.0F, -70.0F, -190.0F};
151     f32     left    = 24.0F;
152     f32     top     = 32.0F;
153     f32     znear   = 50.0F;
154     f32     zfar    = 2000.0F;
155 
156     MTXFrustum(p, left, -left, -top, top, znear, zfar);
157     GXSetProjection(p, GX_PERSPECTIVE);
158 
159     MTXLookAt(v, &camLoc, &up, &objPt);
160 }
161 
162 /*---------------------------------------------------------------------------*
163     Name:           DrawInit
164 
165     Description:    Initializes the vertex attribute format, and sets
166                     the array pointers and strides for the indexed data.
167 
168     Arguments:      none
169 
170     Returns:        none
171  *---------------------------------------------------------------------------*/
DrawInit(void)172 static void DrawInit( void )
173 {
174     GXColor black = {0, 0, 0, 0};
175 
176     GXSetCopyClear(black, 0x00FFFFFF);
177 
178     // Set current vertex descriptor to enable position and color0.
179     // Both use 8b index to access their data arrays.
180     GXClearVtxDesc();
181     GXSetVtxDesc(GX_VA_POS, GX_INDEX8);
182     GXSetVtxDesc(GX_VA_CLR0, GX_INDEX8);
183 
184     // Position has 3 elements (x,y,z), each of type s16,
185     // no fractional bits (integers)
186     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
187     // Color 0 has 4 components (r, g, b, a), each component is 8b.
188     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
189 
190     // stride = 3 elements (x,y,z) of type s16
191     GXSetArray(GX_VA_POS, Verts_s16, 3*sizeof(s16));
192     // stride = 4 elements (r,g,b,a) of type u8
193     GXSetArray(GX_VA_CLR0, Colors_rgba8, 4*sizeof(u8));
194 
195     // Initialize lighting, texgen, and tev parameters
196     GXSetNumChans(1); // default, color = vertex color
197     GXSetNumTexGens(0); // no texture in this demo
198     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
199     GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
200 }
201 
202 /*---------------------------------------------------------------------------*
203     Name:           DrawTick
204 
205     Description:    Draw the model once.  Replicates a simple strut model
206                     many times in the x, y, z directions to create a dense
207                     3D grid.  GXInit makes GX_PNMTX0 the default matrix.
208 
209     Arguments:      v       view matrix
210 
211     Returns:        none
212  *---------------------------------------------------------------------------*/
DrawTick(Mtx v)213 static void DrawTick(Mtx v)
214 {
215     f32  x;  // Translation in x.
216     f32  y;  // Translation in y.
217     f32  z;  // Translation in z.
218     Mtx  m;  // Model matrix.
219     Mtx  mv; // Modelview matrix.
220 
221     GXSetNumTexGens( 0 );
222     GXSetNumTevStages( 1 );
223     GXSetTevOp( GX_TEVSTAGE0, GX_PASSCLR );
224 
225     MTXIdentity(m);
226 
227     for(x = -10*STRUT_LN; x < 2*STRUT_LN; x += STRUT_LN)
228     {
229         for(y = -10*STRUT_LN; y < STRUT_LN; y += STRUT_LN)
230         {
231             for(z = STRUT_LN; z > -10*STRUT_LN; z -= STRUT_LN)
232             {
233                 MTXRowCol(m, 0, 3) = x;
234                 MTXRowCol(m, 1, 3) = y;
235                 MTXRowCol(m, 2, 3) = z;
236                 MTXConcat(v, m, mv);
237                 GXLoadPosMtxImm(mv, GX_PNMTX0);
238                 GXCallDisplayList(OneTriDL, 3*32);
239             }
240         }
241     }
242 }
243 
244 /*---------------------------------------------------------------------------*
245     Name:           AnimTick
246 
247     Description:    Moves viewpoint through the grid.  Loops animation so
248                     that it appears viewpoint is continuously moving forward.
249 
250     Arguments:      v       view matrix
251 
252     Returns:        none
253  *---------------------------------------------------------------------------*/
AnimTick(Mtx v)254 static void AnimTick( Mtx v )
255 {
256     static u32  ticks = 0; // Counter.
257     Mtx         fwd;       // Forward stepping translation matrix.
258     Mtx         back;      // Loop back translation matrix.
259 
260     u32    animSteps    = 100;
261     f32    animLoopBack = (f32)STRUT_LN;
262     f32    animStepFwd  = animLoopBack / animSteps;
263 
264     MTXTrans(fwd, 0, 0, animStepFwd);
265     MTXTrans(back, 0, 0, -animLoopBack);
266 
267     MTXConcat(v, fwd, v);
268     if((ticks % animSteps) == 0)
269         MTXConcat(v, back, v);
270 
271     ticks++;
272 }
273 
274 /*---------------------------------------------------------------------------*
275     Name:           PrintIntro
276 
277     Description:    Prints the directions on how to use this demo.
278 
279     Arguments:      none
280 
281     Returns:        none
282  *---------------------------------------------------------------------------*/
PrintIntro(void)283 static void PrintIntro( void )
284 {
285 
286     OSReport("\n\n****************************************\n");
287     OSReport("to quit:\n");
288     OSReport("     hit the start button\n");
289     OSReport("****************************************\n");
290 }
291