1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     smp-octa.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(NULL);    // 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     while(!(pad[0].button & PAD_BUTTON_MENU))
111     {
112         DEMOBeforeRender();
113         DrawTick(v);        // Draw the model.
114         DEMODoneRender();
115         AnimTick();        // Update animation.
116         PADRead(pad);
117     }
118 
119     OSHalt("End of demo");
120 }
121 
122 
123 /*---------------------------------------------------------------------------*
124    Functions
125  *---------------------------------------------------------------------------*/
126 
127 /*---------------------------------------------------------------------------*
128     Name:           CameraInit
129 
130     Description:    Initialize the projection matrix and load into hardware.
131                     Initialize the view matrix.
132 
133     Arguments:      v      view matrix
134 
135     Returns:        none
136  *---------------------------------------------------------------------------*/
CameraInit(Mtx v)137 static void CameraInit ( Mtx v )
138 {
139     Mtx44   p;      // projection matrix
140     Vec     up      = {0.0F, 0.0F, 1.0F};
141     Vec     camLoc  = {2.0F, 3.0F, 1.0F};
142     Vec     objPt   = {0.0F, 0.0F, 0.0F};
143     f32     top     = 0.0375F;
144     f32     left    = -0.050F;
145     f32     znear   = 0.1F;
146     f32     zfar    = 10.0F;
147 
148     MTXFrustum(p, top, -top, left, -left, znear, zfar);
149     GXSetProjection(p, GX_PERSPECTIVE);
150 
151     MTXLookAt(v, &camLoc, &up, &objPt);
152 }
153 
154 /*---------------------------------------------------------------------------*
155     Name:           DrawInit
156 
157     Description:    Initializes the vertex attribute format 0, and sets
158                     the array pointers and strides for the indexed data.
159 
160     Arguments:      none
161 
162     Returns:        none
163  *---------------------------------------------------------------------------*/
DrawInit(void)164 static void DrawInit( void )
165 {
166     GXColor black = {0, 0, 0, 0};
167 
168     GXSetCopyClear(black, 0x00ffffff);
169 
170     // Set current vertex descriptor to enable position and color0.
171     // Both use 8b index to access their data arrays.
172     GXClearVtxDesc();
173     GXSetVtxDesc(GX_VA_POS, GX_INDEX8);
174     GXSetVtxDesc(GX_VA_CLR0, GX_INDEX8);
175 
176     // Position has 3 elements (x,y,z), each of type f32
177     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
178 
179     // Color 0 has 4 components (r, g, b, a), each component is 8b.
180     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
181 
182     // stride = 3 elements (x,y,z) each of type s16
183     GXSetArray(GX_VA_POS, Verts_f32, 3*sizeof(f32));
184     // stride = 4 elements (r,g,b,a) each of type u8
185     GXSetArray(GX_VA_CLR0, Colors_rgba8, 4*sizeof(u8));
186 
187     // Initialize lighting, texgen, and tev parameters
188     GXSetNumChans(1); // default, color = vertex color
189     GXSetNumTexGens(0); // no texture in this demo
190     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
191     GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
192 }
193 
194 /*---------------------------------------------------------------------------*
195     Name:           Vertex
196 
197     Description:    Create my vertex format
198 
199     Arguments:      t        8-bit triangle index
200             v        8-bit vertex index
201 
202     Returns:        none
203  *---------------------------------------------------------------------------*/
Vertex(u8 t,u8 v)204 static inline void Vertex( u8 t, u8 v )
205 {
206     u8 tv = (u8) (3 * t + v);
207     GXPosition1x8(tv);
208     GXColor1x8(tv);
209 }
210 
211 /*---------------------------------------------------------------------------*
212     Name:           DrawTick
213 
214     Description:    Draw the model once.
215 
216     Arguments:      v        view matrix
217 
218     Returns:        none
219  *---------------------------------------------------------------------------*/
DrawTick(Mtx v)220 static void DrawTick( Mtx v )
221 {
222     Mtx    m;    // Model matrix.
223     Mtx    mv;    // Modelview matrix.
224     u8    iTri;    // index of triangle
225     u8    iVert;    // index of vertex
226 
227     GXSetNumTexGens( 0 );
228     GXSetNumTevStages( 1 );
229     GXSetTevOp( GX_TEVSTAGE0, GX_PASSCLR );
230 
231     // model has a rotation about z axis
232     MTXRotDeg(m, 'z', 8 * ticks);
233     MTXConcat(v, m, mv);
234     GXLoadPosMtxImm(mv, GX_PNMTX0);
235 
236     GXBegin(GX_TRIANGLES, GX_VTXFMT0, 24);
237 
238     // for all triangles of octahedron, ...
239     for (iTri = 0; iTri < 8; ++iTri)
240     {
241         // for all vertices of triangle, ...
242         for (iVert = 0; iVert < 3; ++iVert)
243         {
244             Vertex(iTri, iVert);
245         }
246     }
247 
248     GXEnd();
249 }
250 
251 /*---------------------------------------------------------------------------*
252     Name:           AnimTick
253 
254     Description:    Computes next time step.
255 
256     Arguments:      none
257 
258     Returns:        none
259  *---------------------------------------------------------------------------*/
AnimTick(void)260 static void AnimTick( void )
261 {
262     ticks++;
263 }
264 
265 /*---------------------------------------------------------------------------*
266     Name:            PrintIntro
267 
268     Description:    Prints the directions on how to use this demo.
269 
270     Arguments:        none
271 
272     Returns:        none
273  *---------------------------------------------------------------------------*/
PrintIntro(void)274 static void PrintIntro( void )
275 {
276     OSReport("\n\n********************************\n");
277     OSReport("to quit:\n");
278     OSReport("     hit the start button\n");
279     OSReport("********************************\n");
280 }
281 
282 /*============================================================================*/
283