1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     frb-fld-dbs.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    The macro ATTRIBUTE_ALIGN provides a convenient way to align initialized
18    arrays.  Alignment of vertex arrays to 32B IS NOT required, but may result
19    in a slight performance improvement.
20  *---------------------------------------------------------------------------*/
21 f32 Verts_f32[] ATTRIBUTE_ALIGN(32) =
22 {
23 //      x, y, z
24     0.0f, 0.0f, 1.0f,   // 0:0
25     0.0f, 1.0f, 0.0f,   // 0:1
26     1.0f, 0.0f, 0.0f,   // 0:2
27     0.0f, 0.0f, 1.0f,   // 1:0
28     -1.0f, 0.0f, 0.0f,  // 1:1
29     0.0f, 1.0f, 0.0f,   // 1:2
30     0.0f, 0.0f, 1.0f,   // 2:0
31     0.0f, -1.0f, 0.0f,  // 2:1
32     -1.0f, 0.0f, 0.0f,  // 2:2
33     0.0f, 0.0f, 1.0f,   // 3:0
34     1.0f, 0.0f, 0.0f,   // 3:1
35     0.0f, -1.0f, 0.0f,  // 3:2
36     0.0f, 0.0f, -1.0f,  // 4:0
37     1.0f, 0.0f, 0.0f,   // 4:1
38     0.0f, 1.0f, 0.0f,   // 4:2
39     0.0f, 0.0f, -1.0f,  // 5:0
40     0.0f, 1.0f, 0.0f,   // 5:1
41     -1.0f, 0.0f, 0.0f,  // 5:2
42     0.0f, 0.0f, -1.0f,  // 6:0
43     -1.0f, 0.0f, 0.0f,  // 6:1
44     0.0f, -1.0f, 0.0f,  // 6:2
45     0.0f, 0.0f, -1.0f,  // 7:0
46     0.0f, -1.0f, 0.0f,  // 7:1
47     1.0f, 0.0f, 0.0f    // 7:2
48 };
49 
50 u8 Colors_rgba8[] ATTRIBUTE_ALIGN(32) =
51 {
52     //  r,   g,  b,  a
53     255, 0, 0, 255, // 0:0
54     255, 0, 0, 255, // 0:1
55     255, 0, 0, 255, // 0:2
56     0, 255, 0, 255, // 1:0
57     0, 255, 0, 255, // 1:1
58     0, 255, 0, 255, // 1:2
59     255, 0, 0, 255, // 2:0
60     255, 0, 0, 255, // 2:1
61     255, 0, 0, 255, // 2:2
62     0, 255, 0, 255, // 3:0
63     0, 255, 0, 255, // 3:1
64     0, 255, 0, 255, // 3:2
65     0, 255, 0, 255, // 4:0
66     0, 255, 0, 255, // 4:1
67     0, 255, 0, 255, // 4:2
68     255, 0, 0, 255, // 5:0
69     255, 0, 0, 255, // 5:1
70     255, 0, 0, 255, // 5:2
71     0, 255, 0, 255, // 6:0
72     0, 255, 0, 255, // 6:1
73     0, 255, 0, 255, // 6:2
74     255, 0, 0, 255, // 7:0
75     255, 0, 0, 255, // 7:1
76     255, 0, 0, 255  // 7:2
77 };
78 
79 
80 u32 ticks = 0;  // time counter
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        ( void );
91 static void PrintIntro      ( void );
92 
93 /*---------------------------------------------------------------------------*
94    Application main loop
95  *---------------------------------------------------------------------------*/
96 
main(void)97 void main ( void )
98 {
99     Mtx         v;   // view matrix
100     PADStatus   pad[PAD_MAX_CONTROLLERS]; // game pad state
101 
102     pad[0].button = 0;
103 
104     DEMOInit(&GXNtsc240Ds);    // Init os, pad, gx, vi
105 
106     CameraInit(v); // Initialize the camera.
107     DrawInit();    // Define my vertex formats and set array pointers.
108 
109     PrintIntro();  // Print demo directions
110 
111     while(!(pad[0].button & PAD_BUTTON_MENU))
112     {
113         DEMOBeforeRender();
114         DrawTick(v);        // Draw the model.
115         DEMODoneRender();
116         if (!(pad[0].button & PAD_BUTTON_A))
117         {
118             AnimTick();     // Update animation.
119         }
120         PADRead(pad);
121     }
122 
123     OSHalt("End of test");
124 }
125 
126 
127 /*---------------------------------------------------------------------------*
128    Functions
129  *---------------------------------------------------------------------------*/
130 
131 /*---------------------------------------------------------------------------*
132     Name:           CameraInit
133 
134     Description:    Initialize the projection matrix and load into hardware.
135                     Initialize the view matrix.
136 
137     Arguments:      v       view matrix
138 
139     Returns:        none
140  *---------------------------------------------------------------------------*/
CameraInit(Mtx v)141 static void CameraInit ( Mtx v )
142 {
143     Mtx44   p;      // projection matrix
144     Vec     up      = {0.0F, 0.0F, 1.0F};
145     Vec     camLoc  = {1.0F, 3.0F, 1.0F};
146     Vec     objPt   = {0.0F, 0.0F, 0.0F};
147     f32     left    = 0.0375F;
148     f32     top     = 0.050F;
149     f32     znear   = 0.1F;
150     f32     zfar    = 10.0F;
151 
152     MTXFrustum(p, left, -left, -top, top, znear, zfar);
153     GXSetProjection(p, GX_PERSPECTIVE);
154 
155     MTXLookAt(v, &camLoc, &up, &objPt);
156 }
157 
158 /*---------------------------------------------------------------------------*
159     Name:           DrawInit
160 
161     Description:    Initializes the vertex attribute format 0, and sets
162                     the array pointers and strides for the indexed data.
163 
164     Arguments:      none
165 
166     Returns:        none
167  *---------------------------------------------------------------------------*/
DrawInit(void)168 static void DrawInit( void )
169 {
170     GXColor black = {0, 0, 0, 0};
171 
172     GXSetCopyClear(black, 0x00ffffff);
173 
174     // Set current vertex descriptor to enable position and color0.
175     // Both use 8b index to access their data arrays.
176     GXClearVtxDesc();
177     GXSetVtxDesc(GX_VA_POS, GX_INDEX8);
178     GXSetVtxDesc(GX_VA_CLR0, GX_INDEX8);
179 
180     // Position has 3 elements (x,y,z), each of type f32
181     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
182 
183     // Color 0 has 4 components (r, g, b, a), each component is 8b.
184     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
185 
186     // stride = 3 elements (x,y,z) each of type s16
187     GXSetArray(GX_VA_POS, Verts_f32, 3*sizeof(f32));
188     // stride = 4 elements (r,g,b,a) each of type u8
189     GXSetArray(GX_VA_CLR0, Colors_rgba8, 4*sizeof(u8));
190 
191     // Initialize lighting, texgen, and tev parameters
192     GXSetNumChans(1); // default, color = vertex color
193     GXSetNumTexGens(0); // no texture in this demo
194     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
195     GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
196 }
197 
198 /*---------------------------------------------------------------------------*
199     Name:           Vertex
200 
201     Description:    Create my vertex format
202 
203     Arguments:      t        8-bit triangle index
204                     v        8-bit vertex index
205 
206     Returns:        none
207  *---------------------------------------------------------------------------*/
Vertex(u8 t,u8 v)208 static inline void Vertex( u8 t, u8 v )
209 {
210     u8 tv = (u8) (3 * t + v);
211     GXPosition1x8(tv);
212     GXColor1x8(tv);
213 }
214 
215 /*---------------------------------------------------------------------------*
216     Name:           DrawTick
217 
218     Description:    Draw the model once.
219 
220     Arguments:      v       view matrix
221 
222     Returns:        none
223  *---------------------------------------------------------------------------*/
DrawTick(Mtx v)224 static void DrawTick( Mtx v )
225 {
226     Mtx m;  // Model matrix.
227     Mtx mv; // Modelview matrix.
228     u8  iTri;   // index of triangle
229     u8  iVert;  // index of vertex
230 
231     GXSetNumTexGens( 0 );
232     GXSetNumTevStages( 1 );
233     GXSetTevOp( GX_TEVSTAGE0, GX_PASSCLR );
234 
235     // model has a rotation about z axis
236     MTXRotDeg(m, 'z', 8 * ticks);
237     MTXConcat(v, m, mv);
238     GXLoadPosMtxImm(mv, GX_PNMTX0);
239 
240     GXBegin(GX_TRIANGLES, GX_VTXFMT0, 24);
241 
242     // for all triangles of octahedron, ...
243     for (iTri = 0; iTri < 8; ++iTri)
244     {
245         // for all vertices of triangle, ...
246         for (iVert = 0; iVert < 3; ++iVert)
247         {
248             Vertex(iTri, iVert);
249         }
250     }
251 
252     GXEnd();
253 }
254 
255 /*---------------------------------------------------------------------------*
256     Name:           AnimTick
257 
258     Description:    Computes next time step.
259 
260     Arguments:      none
261 
262     Returns:        none
263  *---------------------------------------------------------------------------*/
AnimTick(void)264 static void AnimTick( void )
265 {
266     ticks++;
267 }
268 
269 /*---------------------------------------------------------------------------*
270     Name:           PrintIntro
271 
272     Description:    Prints the directions on how to use this demo.
273 
274     Arguments:      none
275 
276     Returns:        none
277  *---------------------------------------------------------------------------*/
PrintIntro(void)278 static void PrintIntro( void )
279 {
280     OSReport("\n\n*************************************************\n");
281     OSReport("frb-fld-dbs : field rendering, double-strike mode\n");
282     OSReport("*************************************************\n");
283     OSReport("A Button : pause animation while pressed down\n");
284     OSReport("to quit:\n");
285     OSReport("     hit the start/pause button\n");
286     OSReport("***************************************************\n");
287 }
288