1 /*---------------------------------------------------------------------------*
2 Project: Dolphin/Revolution gx demo
3 File: tex-layer.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 DrawTick ( void );
26
27 static void AnimTick ( void );
28
29 static void DrawSmoothCube ( float tx, float ty );
30 static void SendVertex ( u16 posIndex, u16 texCoordIndex );
31
32 static void PrintIntro ( void );
33
34 #define SIDE 105
35 #define NORM (sqrt(3.0))/3.0
36
37 /*---------------------------------------------------------------------------*
38 Global variables
39 *---------------------------------------------------------------------------*/
40 Mtx v;
41 u32 rot;
42
43 u8 CurrentControl;
44 u8 DoRotation = 1;
45
46 GXTexObj to1, to2;
47
48 float FloatVert[] ATTRIBUTE_ALIGN(32) =
49 { -SIDE, SIDE, -SIDE,
50 -SIDE, SIDE, SIDE,
51 -SIDE, -SIDE, SIDE,
52 -SIDE, -SIDE, -SIDE,
53 SIDE, SIDE, -SIDE,
54 SIDE, -SIDE, -SIDE,
55 SIDE, -SIDE, SIDE,
56 SIDE, SIDE, SIDE
57 };
58
59 float FloatNorm[] ATTRIBUTE_ALIGN(32) =
60 { -1, 1, -1,
61 -1, 1, 1,
62 -1, -1, 1,
63 -1, -1, -1,
64 1, 1, -1,
65 1, -1, -1,
66 1, -1, 1,
67 1, 1, 1
68 };
69
70 float FloatTex[] ATTRIBUTE_ALIGN(32) =
71 { 0.0F, 0.0F,
72 1.0F, 0.0F,
73 1.0F, 1.0F,
74 0.0F, 1.0F
75 };
76
77 u8 ColorRGBA8[] ATTRIBUTE_ALIGN(32) = {255, 255, 255, 255}; //GX_RGBA8
78
79 /*---------------------------------------------------------------------------*
80 Application main loop
81 *---------------------------------------------------------------------------*/
main(void)82 void main ( void )
83 {
84 DEMOInit(NULL);
85
86 DrawInit(); // Define my vertex formats and set array pointers.
87
88 PrintIntro();
89
90 DEMOPadRead(); // Read the joystick for this frame
91
92 // While the quit button is not pressed
93 while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
94 {
95 DEMOPadRead(); // Read the joystick for this frame
96
97 // Do animation based on input
98 AnimTick();
99 DEMOBeforeRender();
100
101 DrawTick(); // Draw the model.
102
103 DEMODoneRender();
104 }
105 OSHalt("End of demo.\n");
106 }
107
108 /*---------------------------------------------------------------------------*
109 Functions
110 *---------------------------------------------------------------------------*/
111
112
113 /*---------------------------------------------------------------------------*
114 Name: CameraInit
115
116 Description: Initialize the projection matrix and load into hardware.
117
118 Arguments: v view matrix to be passed to ViewInit
119 cameraLocScale scale for the camera's distance from the
120 object - to be passed to ViewInit
121
122 Returns: none
123 *---------------------------------------------------------------------------*/
CameraInit(void)124 static void CameraInit ( void )
125 {
126 Mtx44 p;
127 Vec camPt = {0.0F, 0.0F, 650.0F};
128 Vec up = {0.0F, 1.0F, 0.0F};
129 Vec origin = {0.0F, 0.0F, 0.0F};
130
131 MTXFrustum(p, 240, -240, -320, 320, 500, 2000);
132
133 GXSetProjection(p, GX_PERSPECTIVE);
134
135 MTXLookAt(v, &camPt, &up, &origin);
136 }
137
138 /*---------------------------------------------------------------------------*
139 Name: DrawInit
140
141 Description: Calls the correct initialization function for the current
142 model.
143
144 Arguments: none
145
146 Returns: none
147 *---------------------------------------------------------------------------*/
DrawInit(void)148 static void DrawInit( void )
149 {
150 Mtx rz;
151 TPLPalettePtr tpl = 0;
152 u32 i;
153
154 CameraInit(); // Initialize the camera.
155
156 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
157 GXSetVtxDesc(GX_VA_TEX0, GX_INDEX16);
158 GXSetArray(GX_VA_TEX0, FloatTex, 8);
159
160 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX1, GX_TEX_ST, GX_F32, 0);
161 GXSetVtxDesc(GX_VA_TEX1, GX_INDEX16);
162 GXSetArray(GX_VA_TEX1, FloatTex, 8);
163
164 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_NRM, GX_NRM_XYZ, GX_F32, 0);
165 GXSetVtxDesc(GX_VA_NRM, GX_INDEX16);
166 GXSetArray(GX_VA_NRM, FloatNorm, 12);
167
168 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
169 GXSetVtxDesc (GX_VA_POS, GX_INDEX16);
170 GXSetArray(GX_VA_POS, FloatVert, 12);
171
172 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
173 GXSetVtxDesc(GX_VA_CLR0, GX_INDEX16);
174 GXSetArray(GX_VA_CLR0, ColorRGBA8, 4);
175
176 TPLGetPalette(&tpl, "gxTests/tex-05.tpl");
177
178 TPLGetGXTexObjFromPalette(tpl, &to1, 1);
179 TPLGetGXTexObjFromPalette(tpl, &to2, 0);
180
181 GXLoadTexObj(&to1, GX_TEXMAP0);
182 GXLoadTexObj(&to2, GX_TEXMAP1);
183
184 GXSetNumTevStages(2);
185 GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR_NULL);
186 GXSetTevOrder(GX_TEVSTAGE1, GX_TEXCOORD1, GX_TEXMAP1, GX_COLOR_NULL);
187 GXSetTevOp(GX_TEVSTAGE0, GX_REPLACE);
188 GXSetTevOp(GX_TEVSTAGE1, GX_MODULATE);
189
190 for(i = 0; i < 24; i++)
191 {
192 FloatNorm[i] *= NORM;
193 }
194
195 GXSetNumTexGens(2);
196 MTXScale(rz, 3.0F, 3.0F, 3.0F);
197 GXLoadTexMtxImm(rz, GX_TEXMTX0, GX_MTX2x4);
198 GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_TEXMTX0);
199 }
200
201 /*---------------------------------------------------------------------------*
202 Name: DrawTick
203
204 Description: Draw the current model once.
205
206 Arguments: v view matrix
207 m model matrix
208
209 Returns: none
210 *---------------------------------------------------------------------------*/
DrawTick(void)211 static void DrawTick( void )
212 {
213 DrawSmoothCube(0, 0);
214 }
215
216 /*---------------------------------------------------------------------------*
217 Name: AnimTick
218
219 Description: Animates the camera and object based on the joystick's
220 state.
221
222 Arguments: none
223
224 Returns: none
225 *---------------------------------------------------------------------------*/
AnimTick(void)226 static void AnimTick ( void )
227 {
228 u16 buttons = DEMOPadGetButtonDown(0);
229
230 if(buttons & PAD_BUTTON_X)
231 {
232 CurrentControl ++;
233 if(CurrentControl > 2)
234 CurrentControl = 0;
235
236 switch(CurrentControl)
237 {
238 case 0:
239 GXSetTevOp(GX_TEVSTAGE1, GX_MODULATE);
240 OSReport("\n\nTev Mode - modulate\n\n");
241 break;
242
243 case 1:
244 GXSetTevOp(GX_TEVSTAGE1, GX_DECAL);
245 OSReport("\n\nTev Mode - decal\n\n");
246 break;
247
248 case 2:
249 GXSetTevOp(GX_TEVSTAGE1, GX_PASSCLR);
250 OSReport("\n\nTexture 2 off\n\n");
251 break;
252 }
253 }
254
255 if(buttons & PAD_BUTTON_B)
256 {
257 if(DoRotation)
258 DoRotation = 0;
259 else
260 DoRotation = 1;
261 }
262
263 if(DoRotation)
264 {
265 rot ++;
266 if(rot > 1439)
267 rot = 0;
268 }
269 }
270
271 /*---------------------------------------------------------------------------*/
DrawSmoothCube(float tx,float ty)272 static void DrawSmoothCube ( float tx, float ty )
273 {
274 Mtx ry, rz, mv, t;
275
276 MTXRotDeg(ry, 'Y', (float)rot);
277 MTXRotDeg(rz, 'Z', (float)rot / 2.0F);
278 MTXTrans(t, tx, ty, 0);
279
280 MTXConcat(rz, ry, mv);
281 MTXConcat(t, mv, mv);
282 MTXConcat(v, mv, mv);
283 GXLoadPosMtxImm(mv, GX_PNMTX0);
284 MTXInverse(mv, mv);
285 MTXTranspose(mv, mv);
286 GXLoadNrmMtxImm(mv, GX_PNMTX0);
287
288 GXBegin(GX_QUADS, GX_VTXFMT0, 4*6);
289
290 SendVertex(0, 0);
291 SendVertex(1, 1);
292 SendVertex(2, 2);
293 SendVertex(3, 3);
294
295 SendVertex(4, 0);
296 SendVertex(5, 1);
297 SendVertex(6, 2);
298 SendVertex(7, 3);
299
300 SendVertex(2, 0);
301 SendVertex(6, 1);
302 SendVertex(5, 2);
303 SendVertex(3, 3);
304
305 SendVertex(1, 0);
306 SendVertex(0, 1);
307 SendVertex(4, 2);
308 SendVertex(7, 3);
309
310 SendVertex(5, 0);
311 SendVertex(4, 1);
312 SendVertex(0, 2);
313 SendVertex(3, 3);
314
315 SendVertex(6, 0);
316 SendVertex(2, 1);
317 SendVertex(1, 2);
318 SendVertex(7, 3);
319
320 GXEnd();
321 }
322
323 /*---------------------------------------------------------------------------*/
SendVertex(u16 posIndex,u16 texCoordIndex)324 static void SendVertex ( u16 posIndex, u16 texCoordIndex )
325 {
326 GXPosition1x16(posIndex);
327 GXNormal1x16(posIndex);
328 GXColor1x16(0);
329 GXTexCoord1x16(texCoordIndex);
330 GXTexCoord1x16(texCoordIndex);
331 }
332
333 /*---------------------------------------------------------------------------*
334 Name: PrintIntro
335 Description: Prints the directions on how to use this demo.
336 *---------------------------------------------------------------------------*/
PrintIntro(void)337 static void PrintIntro( void )
338 {
339 OSReport("\n\n");
340 OSReport("************************************************\n");
341 OSReport("tex-layer: simple multitexture demo\n");
342 OSReport("************************************************\n");
343 OSReport("to quit hit the start button\n");
344 OSReport("\n");
345 OSReport("X button : toggle combine mode.\n");
346 OSReport("B button : start/stop cube rotation.\n");
347 OSReport("************************************************\n\n");
348 }
349
350 /*============================================================================*/
351