1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     tf-pn-mtx.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 #include <math.h>
16 
17 /*---------------------------------------------------------------------------*
18    Forward references
19  *---------------------------------------------------------------------------*/
20 
21 void        main            ( void );
22 
23 static void CameraInit      ( void );
24 static void DrawInit        ( void );
25 static void VertexLightInit ( void );
26 static void DrawTick        ( void );
27 
28 static void AnimTick        ( void );
29 
30 /*---------------------------------------------------------------------------*
31    Global variables
32  *---------------------------------------------------------------------------*/
33 Mtx v;
34 u32 rot;
35 
36 /*---------------------------------------------------------------------------*
37    Application main loop
38  *---------------------------------------------------------------------------*/
main(void)39 void main ( void )
40 {
41     DEMOInit(NULL);
42 
43     OSReport("\n\n");
44     OSReport("**********************************************\n");
45     OSReport("tf-pn-mtx: Position/normal matrix demo\n");
46     OSReport("**********************************************\n");
47     OSReport("To quit hit the start button.\n");
48     OSReport("\n");
49     OSReport("Each cube is drawn with a different HW matrix.\n");
50     OSReport("**********************************************\n");
51     OSReport("\n\n");
52 
53     DrawInit();         // Define my vertex formats and set array pointers.
54 
55     DEMOPadRead();      // Read the joystick for this frame
56 
57     // While the quit button is not pressed
58     while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
59     {
60         DEMOPadRead();      // Read the joystick for this frame
61 
62         // Do animation based on input
63         AnimTick();
64         DEMOBeforeRender();
65 
66         DrawTick();     // Draw the model.
67 
68         DEMODoneRender();
69     }
70 
71     OSHalt("End of test");
72 }
73 
74 /*---------------------------------------------------------------------------*
75    Functions
76  *---------------------------------------------------------------------------*/
77 
78 
79 /*---------------------------------------------------------------------------*
80     Name:           CameraInit
81 
82     Description:    Initialize the projection matrix and load into hardware.
83 
84     Arguments:      v   view matrix to be passed to ViewInit
85                     cameraLocScale  scale for the camera's distance from the
86                                     object - to be passed to ViewInit
87 
88     Returns:        none
89  *---------------------------------------------------------------------------*/
CameraInit(void)90 static void CameraInit      ( void )
91 {
92     Mtx44 p;
93     Vec camPt = {0.0F, 0.0F, 650.0F};
94     Vec up = {0.0F, 1.0F, 0.0F};
95     Vec origin = {0.0F, 0.0F, 0.0F};
96 
97     MTXFrustum(p, 240, -240, -320, 320, 500, 2000);
98 
99     GXSetProjection(p, GX_PERSPECTIVE);
100 
101     MTXLookAt(v, &camPt, &up, &origin);
102 }
103 
104 /*---------------------------------------------------------------------------*
105     Name:           DrawInit
106 
107     Description:    Calls the correct initialization function for the current
108                     model.
109 
110     Arguments:      none
111 
112     Returns:        none
113  *---------------------------------------------------------------------------*/
DrawInit(void)114 static void DrawInit( void )
115 {
116     CameraInit();   // Initialize the camera.
117     VertexLightInit();
118 }
119 
120 /*---------------------------------------------------------------------------*/
VertexLightInit(void)121 static void VertexLightInit ( void )
122 {
123     GXLightObj MyLight;
124     GXColor color = {255, 255, 255, 255};
125 
126     GXInitLightPos(&MyLight, 0.0F, 0.0F, 0.0F);
127     GXInitLightColor(&MyLight, color);
128     GXLoadLightObjImm(&MyLight, GX_LIGHT0);
129 
130     GXSetChanMatColor(GX_COLOR0, color);
131 
132     GXSetChanCtrl(  GX_COLOR0,
133                     GX_TRUE, // enable channel
134                     GX_SRC_REG, // amb source
135                     GX_SRC_REG,     // mat source
136                     GX_LIGHT0,  // light mask
137                     GX_DF_CLAMP,// diffuse function
138                     GX_AF_NONE);
139 
140     GXSetNumChans(1);
141     GXSetNumTexGens( 0 );
142     GXSetTevOp( GX_TEVSTAGE0, GX_PASSCLR );
143     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0);
144 }
145 
146 /*---------------------------------------------------------------------------*
147     Name:           DrawTick
148 
149     Description:    Draw the current model once.
150 
151     Arguments:      v       view matrix
152                     m       model matrix
153 
154     Returns:        none
155  *---------------------------------------------------------------------------*/
DrawTick(void)156 static void DrawTick( void )
157 {
158     Mtx rx, ry, rz, s, mv, t;
159 
160 
161     MTXScale(s, 70, 70, 70);
162     MTXRotDeg(rx, 'X', (float)rot);
163     MTXRotDeg(ry, 'Y', (float)rot);
164     MTXRotDeg(rz, 'Z', (float)rot);
165 
166     MTXTrans(t, -240, 150, 0);
167     MTXConcat(rx, s, mv);
168     MTXConcat(t, mv, mv);
169     MTXConcat(v, mv, mv);
170     GXLoadPosMtxImm(mv, GX_PNMTX0);
171     MTXInverse(mv, mv);
172     MTXTranspose(mv, mv);
173     GXLoadNrmMtxImm(mv, GX_PNMTX0);
174 
175     MTXTrans(t, -80, 150, 0);
176     MTXConcat(ry, s, mv);
177     MTXConcat(t, mv, mv);
178     MTXConcat(v, mv, mv);
179     GXLoadPosMtxImm(mv, GX_PNMTX1);
180     MTXInverse(mv, mv);
181     MTXTranspose(mv, mv);
182     GXLoadNrmMtxImm(mv, GX_PNMTX1);
183 
184     MTXTrans(t, 80, 150, 0);
185     MTXConcat(rz, s, mv);
186     MTXConcat(t, mv, mv);
187     MTXConcat(v, mv, mv);
188     GXLoadPosMtxImm(mv, GX_PNMTX2);
189     MTXInverse(mv, mv);
190     MTXTranspose(mv, mv);
191     GXLoadNrmMtxImm(mv, GX_PNMTX2);
192 
193     MTXTrans(t, 240, 150, 0);
194     MTXConcat(rx, s, mv);
195     MTXConcat(ry, mv, mv);
196     MTXConcat(t, mv, mv);
197     MTXConcat(v, mv, mv);
198     GXLoadPosMtxImm(mv, GX_PNMTX3);
199     MTXInverse(mv, mv);
200     MTXTranspose(mv, mv);
201     GXLoadNrmMtxImm(mv, GX_PNMTX3);
202 
203     MTXTrans(t, -240, -150, 0);
204     MTXConcat(rx, s, mv);
205     MTXConcat(rz, mv, mv);
206     MTXConcat(t, mv, mv);
207     MTXConcat(v, mv, mv);
208     GXLoadPosMtxImm(mv, GX_PNMTX4);
209     MTXInverse(mv, mv);
210     MTXTranspose(mv, mv);
211     GXLoadNrmMtxImm(mv, GX_PNMTX4);
212 
213     MTXTrans(t, -80, -150, 0);
214     MTXConcat(ry, s, mv);
215     MTXConcat(rz, mv, mv);
216     MTXConcat(t, mv, mv);
217     MTXConcat(v, mv, mv);
218     GXLoadPosMtxImm(mv, GX_PNMTX5);
219     MTXInverse(mv, mv);
220     MTXTranspose(mv, mv);
221     GXLoadNrmMtxImm(mv, GX_PNMTX5);
222 
223     MTXTrans(t, 80, -150, 0);
224     MTXConcat(rz, s, mv);
225     MTXConcat(ry, mv, mv);
226     MTXConcat(t, mv, mv);
227     MTXConcat(v, mv, mv);
228     GXLoadPosMtxImm(mv, GX_PNMTX6);
229     MTXInverse(mv, mv);
230     MTXTranspose(mv, mv);
231     GXLoadNrmMtxImm(mv, GX_PNMTX6);
232 
233     MTXTrans(t, 240, -150, 0);
234     MTXConcat(rz, s, mv);
235     MTXConcat(rx, mv, mv);
236     MTXConcat(t, mv, mv);
237     MTXConcat(v, mv, mv);
238     GXLoadPosMtxImm(mv, GX_PNMTX7);
239     MTXInverse(mv, mv);
240     MTXTranspose(mv, mv);
241     GXLoadNrmMtxImm(mv, GX_PNMTX7);
242 
243     MTXTrans(t, -80, 0, 0);
244     MTXConcat(rz, s, mv);
245     MTXConcat(rx, mv, mv);
246     MTXConcat(ry, mv, mv);
247     MTXConcat(t, mv, mv);
248     MTXConcat(v, mv, mv);
249     GXLoadPosMtxImm(mv, GX_PNMTX8);
250     MTXInverse(mv, mv);
251     MTXTranspose(mv, mv);
252     GXLoadNrmMtxImm(mv, GX_PNMTX8);
253 
254     MTXTrans(t, 80, 0, 0);
255     MTXConcat(rx, s, mv);
256     MTXConcat(ry, mv, mv);
257     MTXConcat(rz, mv, mv);
258     MTXConcat(t, mv, mv);
259     MTXConcat(v, mv, mv);
260     GXLoadPosMtxImm(mv, GX_PNMTX9);
261     MTXInverse(mv, mv);
262     MTXTranspose(mv, mv);
263     GXLoadNrmMtxImm(mv, GX_PNMTX9);
264 
265     GXSetCurrentMtx(GX_PNMTX0);
266     GXDrawCube();
267 
268     GXSetCurrentMtx(GX_PNMTX1);
269     GXDrawCube();
270 
271     GXSetCurrentMtx(GX_PNMTX2);
272     GXDrawCube();
273 
274     GXSetCurrentMtx(GX_PNMTX3);
275     GXDrawCube();
276 
277     GXSetCurrentMtx(GX_PNMTX4);
278     GXDrawCube();
279 
280     GXSetCurrentMtx(GX_PNMTX5);
281     GXDrawCube();
282 
283     GXSetCurrentMtx(GX_PNMTX6);
284     GXDrawCube();
285 
286     GXSetCurrentMtx(GX_PNMTX7);
287     GXDrawCube();
288 
289     GXSetCurrentMtx(GX_PNMTX8);
290     GXDrawCube();
291 
292     GXSetCurrentMtx(GX_PNMTX9);
293     GXDrawCube();
294 
295 }
296 
297 /*---------------------------------------------------------------------------*
298     Name:           AnimTick
299 
300     Description:    Animates the camera and object based on the joystick's
301                     state.
302 
303     Arguments:      none
304 
305     Returns:        none
306  *---------------------------------------------------------------------------*/
AnimTick(void)307 static void AnimTick ( void )
308 {
309     rot ++;
310     if(rot > 719)
311         rot = 0;
312 }
313 
314 /*---------------------------------------------------------------------------*/
315 
316 
317