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