1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin
3   File:     gd-init-gc.c
4 
5   Copyright 2001 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: gd-init-gc.c,v $
14   Revision 1.3  2006/08/18 21:50:58  carlmu
15   Added explanatory output.
16 
17   Revision 1.2  02/20/2006 04:13:09  mitu
18   changed include path from dolphin/ to revolution/.
19 
20   Revision 1.1  02/08/2006 11:19:43  mitu
21   1st version.
22 
23 
24     2     02/09/06 11:25:00 Hirose
25     Resolved future time stamp problem.
26 
27     2     10/19/02 6:53p Hirose
28     Changed location of data file.
29 
30     1     9/25/01 6:23p Carl
31     Sources for GD init demo.
32 
33   $NoKeywords: $
34  *---------------------------------------------------------------------------*/
35 
36 #include <demo.h>
37 #include <math.h>
38 
39 #include <revolution/gd.h>
40 
41 /*---------------------------------------------------------------------------*
42    Forward references
43  *---------------------------------------------------------------------------*/
44 
45 void        main            ( void );
46 
47 static void CameraInit      ( void );
48 static void DrawInit        ( void );
49 static void DrawTick        ( void );
50 
51 static void AnimTick        ( void );
52 
53 static void ParameterInit   ( void );
54 
55 #ifdef LOAD_DL_FROM_FILE
56 static void LoadDLs   ( void );
57 #else
58 extern void CreateDLs( void );
59 #endif
60 
61 /*---------------------------------------------------------------------------*
62    Global variables
63  *---------------------------------------------------------------------------*/
64 
65 // Display lists *************************************************************
66 
67 // These are only created during runtime if LOAD_DL_FROM_FILE is not defined.
68 // Otherwise, these are loaded from a file.
69 
70 // This DL is used with the "Draw" display list.
71 // It initializes state that will be used by the Draw DL.
72 
73 GDLObj InitDLO;
74 
75 // This DL draws a colored cube.
76 
77 GDLObj DrawDLO;
78 
79 // This array indicates the offsets to patch memory addresses for the
80 // primitive data arrays (positions and colors) referred to in the Init DL.
81 
82 u32 *setArrayOffsets;
83 
84 /*---------------------------------------------------------------------------*/
85 
86 // Actual primitive data follows
87 
88 #define SIDE 30
89 
90 // Remember:  Alignment of vertex arrays to 32B IS NOT required, but it
91 // may result in a slight performance improvement.
92 
93 float FloatVert[] ATTRIBUTE_ALIGN(32) =
94 {
95     -SIDE,  SIDE, -SIDE,
96     -SIDE,  SIDE,  SIDE,
97     -SIDE, -SIDE,  SIDE,
98     -SIDE, -SIDE, -SIDE,
99      SIDE,  SIDE, -SIDE,
100      SIDE, -SIDE, -SIDE,
101      SIDE, -SIDE,  SIDE,
102      SIDE,  SIDE,  SIDE,
103 };
104 
105 GXColor IntColor[] ATTRIBUTE_ALIGN(32) =
106 {
107     { 255, 0, 0, 255 },   // red
108     { 0, 255, 0, 255 },   // green
109     { 0, 0, 255, 255 },   // blue
110     { 0, 255, 255, 255 }, // cyan
111     { 255, 0, 255, 255 }, // magenta
112     { 255, 255, 0, 255 }, // yellow
113 };
114 
115 // Misc data...
116 
117 Mtx v;          // view matrix
118 u32 rot;        // current cube rotation
119 Mtx mv;         // model view matrix
120 
121 /*---------------------------------------------------------------------------*
122    Application main loop
123  *---------------------------------------------------------------------------*/
124 
main(void)125 void main ( void )
126 {
127     DEMOInit(NULL);
128 
129     OSReport("\n\n");
130     OSReport("**********************************************\n");
131     OSReport("gd-init-gc: GXInit display list demo\n");
132     OSReport("**********************************************\n");
133     OSReport("To quit hit the start button.\n");
134     OSReport("\n");
135     OSReport("A button toggles animation.\n");
136     OSReport("**********************************************\n");
137     OSReport("\n\n");
138 
139     DrawInit();         // Prepare the display lists and such
140 
141     ParameterInit();
142 
143     DEMOPadRead();      // Read the joystick for this frame
144 
145     // While the quit button is not pressed
146     while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
147     {
148         DEMOPadRead();  // Read the joystick for this frame
149 
150         AnimTick();     // Do animation based on input
151 
152         DEMOBeforeRender();
153 
154         DrawTick();     // Draw the model.
155 
156         DEMODoneRender();
157     }
158 
159     OSHalt("End of test");
160 }
161 
162 /*---------------------------------------------------------------------------*
163    Functions
164  *---------------------------------------------------------------------------*/
165 
166 /*---------------------------------------------------------------------------*
167     Name:           CameraInit
168 
169     Description:    Initialize the projection matrix and load into hardware.
170 
171     Arguments:      v   view matrix to be passed to ViewInit
172                     cameraLocScale  scale for the camera's distance from the
173                                     object - to be passed to ViewInit
174 
175     Returns:        none
176  *---------------------------------------------------------------------------*/
CameraInit(void)177 static void CameraInit      ( void )
178 {
179     Mtx44 p;
180     Vec camPt = {0.0F, 0.0F, 650.0F};
181     Vec up = {0.0F, 1.0F, 0.0F};
182     Vec origin = {0.0F, 0.0F, 0.0F};
183 
184     MTXFrustum(p, 112, -112, -160, 160, 500, 2000);
185 
186     GXSetProjection(p, GX_PERSPECTIVE);
187 
188     MTXLookAt(v, &camPt, &up, &origin);
189 }
190 
191 
192 /*---------------------------------------------------------------------------*
193     Name:           LoadDLs
194 
195     Description:    Loads the display lists used by the program from a file.
196                     This routine is only called if LOAD_DL_FROM_FILE is defined.
197 
198     Arguments:      none
199 
200     Returns:        none
201  *---------------------------------------------------------------------------*/
202 #ifdef LOAD_DL_FROM_FILE
LoadDLs(void)203 static void LoadDLs ( void )
204 {
205     s32 err;
206     GDGList *DLDescArray;
207     GDGList *PLDescArray;
208     u32 numDLs, numPLs;
209 
210     err = GDReadDLFile("gddemo/gdInit.gdl", &numDLs, &numPLs,
211                        &DLDescArray, &PLDescArray);
212 
213     OSReport("(%d) Read %d DLs, %d PLs\n", err, numDLs, numPLs);
214 
215     ASSERTMSG(!err, "Error reading GDL file.\n");
216 
217     ASSERTMSG(numDLs == 2 && numPLs == 1, "Incorrect data in GDL file.\n");
218 
219     // Note: We put the DL length into the "offset" field, since that's
220     // where the run-time code expects it.  We do this since the CreateDLs
221     // function uses an oversize "length" field, and the actual valid data
222     // length is saved in the "offset" field in that case.  Thus we do the
223     // same thing here for consistency.
224 
225     GDInitGDLObj( &InitDLO, DLDescArray[0].ptr, DLDescArray[0].byteLength );
226     GDSetCurrent( &InitDLO );
227     GDSetCurrOffset( DLDescArray[0].byteLength );
228 
229     GDInitGDLObj( &DrawDLO, DLDescArray[1].ptr, DLDescArray[1].byteLength );
230     GDSetCurrent( &DrawDLO );
231     GDSetCurrOffset( DLDescArray[1].byteLength );
232 
233     GDSetCurrent( NULL );
234 
235     ASSERTMSG(PLDescArray[0].byteLength == 2 * sizeof(u32),
236               "Incorrect patch list in GDL file.\n");
237 
238     setArrayOffsets = (u32 *) PLDescArray[0].ptr;
239 }
240 #endif
241 
242 
243 /*---------------------------------------------------------------------------*
244     Name:           DrawInit
245 
246     Description:    Calls the correct initialization function for the current
247                     model.
248 
249     Arguments:      none
250 
251     Returns:        none
252  *---------------------------------------------------------------------------*/
DrawInit(void)253 static void DrawInit( void )
254 {
255     u32          i;
256     static void *basePtrs[2] = { FloatVert, IntColor };
257     void        *cp;
258     u32          length;
259 
260     // Load or create all the display lists and patch lists.
261 
262 #ifdef LOAD_DL_FROM_FILE
263     LoadDLs();
264 #else
265     CreateDLs();
266 #endif
267 
268     // Now, we need to patch in the vertex array base pointers
269     GDSetCurrent(&InitDLO);
270     length = GDGetCurrOffset(); // preserve the offset!
271     for(i=0; i<2; i++)
272     {
273         GDSetCurrOffset(setArrayOffsets[i]);
274         cp = GDGetCurrPointer();
275         GDPatchArrayPtr( basePtrs[i] );
276         DCStoreRange(cp, CP_DATA_LENGTH );
277     }
278     GDSetCurrOffset(length);
279     GDSetCurrent(NULL);
280 
281     // Proceed with the usual sort of initialization...
282 
283     CameraInit();   // Initialize the camera.
284 }
285 
286 /*---------------------------------------------------------------------------*
287     Name:           DrawTick
288 
289     Description:    Draw the current model once.
290 
291     Arguments:      v       view matrix
292                     m       model matrix
293 
294     Returns:        none
295  *---------------------------------------------------------------------------*/
DrawTick(void)296 static void DrawTick( void )
297 {
298     // Draw the cube
299 
300     // Call the init DL to set up the parameters for the draw DL.
301     GXCallDisplayList(GDGetGDLObjStart(&InitDLO), GDGetGDLObjOffset(&InitDLO));
302 
303     // Set up the desired model view matrix.
304     // (This was written over by the Init DL.)
305     GXLoadPosMtxImm(mv, GX_PNMTX0);
306 
307     // Call the draw DL to draw the cube.
308     GXCallDisplayList(GDGetGDLObjStart(&DrawDLO), GDGetGDLObjOffset(&DrawDLO));
309 }
310 
311 /*---------------------------------------------------------------------------*
312     Name:           AnimTick
313 
314     Description:    Animates the camera and object based on the joystick's
315                     state.
316 
317     Arguments:      none
318 
319     Returns:        none
320  *---------------------------------------------------------------------------*/
AnimTick(void)321 static void AnimTick ( void )
322 {
323     Mtx ry, rz, tm;
324     f32 tx, ty;
325     u16 buttons = DEMOPadGetButton(0);
326 
327     // Just simple controls right now...
328 
329     if(buttons & PAD_BUTTON_A)
330     {
331         // suspend animation
332     } else {
333 
334         rot ++;
335         if(rot > 2159)
336             rot = 0;
337     }
338 
339     // Set up our transformations...
340 
341     tx = 0.0f;
342     ty = 0.0f;
343 
344     MTXRotDeg(ry, 'X', (float)rot);
345     MTXRotDeg(rz, 'Y', (float)rot / 3.0F);
346     MTXTrans(tm, tx, ty, 0);
347 
348     MTXConcat(rz, ry, mv);
349     MTXConcat(tm, mv, mv);
350     MTXConcat(v, mv, mv);
351 }
352 
353 /*---------------------------------------------------------------------------*
354     Name:           ParameterInit
355 
356     Description:    Initialize parameters for single frame display
357 
358     Arguments:      none
359 
360     Returns:        none
361  *---------------------------------------------------------------------------*/
ParameterInit(void)362 static void ParameterInit ( void )
363 {
364     rot = 45;
365 }
366 
367 /****************************************************************************/
368