1 /*---------------------------------------------------------------------------*
2 Project: Dolphin/Revolution gx demo
3 File: tex-cube.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 tex-fmt-cube
15 display various format textures ( non-tpl version )
16 *---------------------------------------------------------------------------*/
17
18
19 /*---------------------------------------------------------------------------*
20 Header files
21 *---------------------------------------------------------------------------*/
22 #include <demo.h>
23 #include "tex-fmt-cube.h"
24
25 /*---------------------------------------------------------------------------*
26 Macro definitions
27 *---------------------------------------------------------------------------*/
28 #define MAX_SCOORD 0x4000 // for 16b, 2.14 fixed point format
29 #define MAX_TCOORD 0x4000 // for 16b, 2.14 fixed point format
30 #define STEP_SCOORD 8
31 #define STEP_TCOORD 24
32
33 #define NON_CI_TEXTURES 8
34 #define CI_TEXTURES 2
35 #define MAX_TEXTURES (NON_CI_TEXTURES + CI_TEXTURES)
36
37 #define TEST_TLUT00_NUM_ENTRIES 16
38 #define TEST_TLUT01_NUM_ENTRIES 256
39
40 #define Clamp(val,min,max) \
41 ((val) = (((val) < (min)) ? (min) : ((val) > (max)) ? (max) : (val)))
42
43 /*---------------------------------------------------------------------------*
44 Structure definitions
45 *---------------------------------------------------------------------------*/
46 // for camera
47 typedef struct
48 {
49 Vec location;
50 Vec up;
51 Vec target;
52 f32 left;
53 f32 top;
54 f32 znear;
55 f32 zfar;
56 } CameraConfig;
57
58 typedef struct
59 {
60 CameraConfig cfg;
61 Mtx view;
62 Mtx44 proj;
63 } MyCameraObj;
64
65 // for entire scene control
66 typedef struct
67 {
68 MyCameraObj cam;
69 Mtx modelCtrl;
70 f32 modelScale;
71 u32 texNumber;
72 GXTevMode tevMode;
73 } MySceneCtrlObj;
74
75 /*---------------------------------------------------------------------------*
76 Forward references
77 *---------------------------------------------------------------------------*/
78 void main ( void );
79 static void DrawInit ( MySceneCtrlObj* sc );
80 static void DrawTick ( MySceneCtrlObj* sc );
81 static void AnimTick ( MySceneCtrlObj* sc );
82 static void DrawBox ( void );
83 static void SetCamera ( MyCameraObj* cam );
84 static void Message ( u32 num );
85 static void PrintIntro ( void );
86
87 /*---------------------------------------------------------------------------*
88 Model Data
89 *---------------------------------------------------------------------------*/
90 /*---------------------------------------------------------------------------*
91 Data arrays for indexed primitives must be 32B aligned. Normally, memory
92 for the arrays would be OSAlloc'd (which returns 32B aligned pointers) and
93 the data would be loaded from ROM. The pragma variable_align provides a
94 convenient way to align initialized arrays.
95 *---------------------------------------------------------------------------*/
96 static s16 TopVerts[] ATTRIBUTE_ALIGN(32) =
97 {
98 -40, -40, 40, // 0
99 40, -40, 40, // 1
100 40, 40, 40, // 2
101 -40, 40, 40, // 3
102 -40, -40, -40, // 4
103 40, -40, -40, // 5
104 40, 40, -40, // 6
105 -40, 40, -40 // 7
106 };
107
108 static u8 Colors[] ATTRIBUTE_ALIGN(32) =
109 {
110 0x80, 0x00, 0x00, 0x80,
111 0x00, 0x80, 0x00, 0x80,
112 0x00, 0x00, 0x80, 0x80,
113 0x80, 0x80, 0x00, 0x80,
114 0x80, 0x00, 0x80, 0x80,
115 0x00, 0x80, 0x80, 0x80
116 };
117
118 /*---------------------------------------------------------------------------*
119 Texture settings
120 *---------------------------------------------------------------------------*/
121 static void* TexDataPtr[MAX_TEXTURES] =
122 {
123 TestI4TexImageData,
124 TestI8TexImageData,
125 TestIA4TexImageData,
126 TestIA8TexImageData,
127 TestRGB565TexImageData,
128 TestRGB5A3TexImageData,
129 TestRGBA8TexImageData,
130 TestCMPRTexImageData,
131 TestC4TexImageData,
132 TestC8TexImageData
133 };
134
135 static u32 TexAttr[3*MAX_TEXTURES] =
136 {
137 TEST_I4_TEX_IMAGE_WIDTH, TEST_I4_TEX_IMAGE_HEIGHT, TEST_I4_TEX_IMAGE_FORMAT,
138 TEST_I8_TEX_IMAGE_WIDTH, TEST_I8_TEX_IMAGE_HEIGHT, TEST_I8_TEX_IMAGE_FORMAT,
139 TEST_IA4_TEX_IMAGE_WIDTH, TEST_IA4_TEX_IMAGE_HEIGHT, TEST_IA4_TEX_IMAGE_FORMAT,
140 TEST_IA8_TEX_IMAGE_WIDTH, TEST_IA8_TEX_IMAGE_HEIGHT, TEST_IA8_TEX_IMAGE_FORMAT,
141 TEST_RGB565_TEX_IMAGE_WIDTH, TEST_RGB565_TEX_IMAGE_HEIGHT, TEST_RGB565_TEX_IMAGE_FORMAT,
142 TEST_RGB5A3_TEX_IMAGE_WIDTH, TEST_RGB5A3_TEX_IMAGE_HEIGHT, TEST_RGB5A3_TEX_IMAGE_FORMAT,
143 TEST_RGBA8_TEX_IMAGE_WIDTH, TEST_RGBA8_TEX_IMAGE_HEIGHT, TEST_RGBA8_TEX_IMAGE_FORMAT,
144 TEST_CMPR_TEX_IMAGE_WIDTH, TEST_CMPR_TEX_IMAGE_HEIGHT, TEST_CMPR_TEX_IMAGE_FORMAT,
145 TEST_C4_TEX_IMAGE_WIDTH, TEST_C4_TEX_IMAGE_HEIGHT, TEST_C4_TEX_IMAGE_FORMAT,
146 TEST_C8_TEX_IMAGE_WIDTH, TEST_C8_TEX_IMAGE_HEIGHT, TEST_C8_TEX_IMAGE_FORMAT
147 };
148
149 static GXTlut TexTlutAttr[CI_TEXTURES] =
150 {
151 GX_TLUT0, GX_TLUT1
152 };
153
154 /*---------------------------------------------------------------------------*
155 Camera configuration
156 *---------------------------------------------------------------------------*/
157 static CameraConfig DefaultCamera =
158 {
159 { 0.0F, 0.0F, 300.0F }, // location
160 { 0.0F, 1.0F, 0.0F }, // up
161 { 0.0F, 0.0F, 0.0F }, // target
162 -32.0F, // left
163 24.0F, // top
164 50.0F, // near
165 2000.0F // far
166 };
167
168 /*---------------------------------------------------------------------------*
169 Global variables
170 *---------------------------------------------------------------------------*/
171 static MySceneCtrlObj SceneCtrl; // scene control parameters
172 static GXTexObj MyTexObj[MAX_TEXTURES];
173 static GXTlutObj MyTlutObj0, MyTlutObj1;
174
175 /*---------------------------------------------------------------------------*
176 Application main loop
177 *---------------------------------------------------------------------------*/
main(void)178 void main ( void )
179 {
180 DEMOInit(NULL); // Init the OS, game pad, graphics and video.
181
182 DrawInit(&SceneCtrl); // Initialize vertex formats, array pointers
183 // and default scene settings.
184
185 PrintIntro(); // Print demo directions
186
187 Message(SceneCtrl.texNumber);
188 while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
189 {
190 DEMOBeforeRender();
191 DrawTick(&SceneCtrl); // Draw the model.
192 DEMODoneRender();
193 DEMOPadRead(); // Read controller
194 AnimTick(&SceneCtrl); // Do animation
195 }
196
197 OSHalt("End of demo");
198 }
199
200 /*---------------------------------------------------------------------------*
201 Functions
202 *---------------------------------------------------------------------------*/
203 /*---------------------------------------------------------------------------*
204 Name: DrawInit
205
206 Description: Initializes the vertex attribute format and sets up
207 the array pointer for the indexed data.
208 This function also initializes scene control parameters.
209
210 Arguments: sc : pointer to the structure of scene control parameters
211
212 Returns: none
213 *---------------------------------------------------------------------------*/
DrawInit(MySceneCtrlObj * sc)214 static void DrawInit( MySceneCtrlObj* sc )
215 {
216 u32 i;
217
218 // Vertex Attribute
219 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
220 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
221 GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_U16, 14);
222
223 // Array Pointers and Strides
224 GXInvalidateVtxCache();
225 GXSetArray(GX_VA_POS, TopVerts, 3 * sizeof(s16));
226 GXSetArray(GX_VA_CLR0, Colors, 4 * sizeof(u8));
227
228 GXSetNumChans(1);
229
230 // Set texture coordinate generation.
231 GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);
232 GXSetNumTexGens(1);
233
234 // Init texture objects, existing in tex-data00.c
235 GXInvalidateTexAll();
236
237 for ( i = 0 ; i < NON_CI_TEXTURES ; ++i )
238 {
239 GXInitTexObj(
240 &MyTexObj[i],
241 TexDataPtr[i],
242 (u16)TexAttr[i*3],
243 (u16)TexAttr[i*3+1],
244 (GXTexFmt)TexAttr[i*3+2],
245 GX_REPEAT, // s
246 GX_REPEAT, // t
247 GX_FALSE );
248 }
249
250 for ( i = NON_CI_TEXTURES ; i < MAX_TEXTURES ; ++i )
251 {
252 GXInitTexObjCI(
253 &MyTexObj[i],
254 TexDataPtr[i],
255 (u16)TexAttr[i*3],
256 (u16)TexAttr[i*3+1],
257 (GXCITexFmt)TexAttr[i*3+2],
258 GX_REPEAT, // s
259 GX_REPEAT, // t
260 GX_FALSE,
261 (u32)(TexTlutAttr[i - NON_CI_TEXTURES]) );
262 }
263
264 // Init TLUTs
265 GXInitTlutObj(
266 &MyTlutObj0,
267 TestTlutData00,
268 (GXTlutFmt)TEST_TLUT00_FORMAT,
269 TEST_TLUT00_NUM_ENTRIES );
270 GXLoadTlut(&MyTlutObj0, GX_TLUT0);
271
272 GXInitTlutObj(
273 &MyTlutObj1,
274 TestTlutData01,
275 (GXTlutFmt)TEST_TLUT01_FORMAT,
276 TEST_TLUT01_NUM_ENTRIES );
277 GXLoadTlut(&MyTlutObj1, GX_TLUT1);
278
279
280 // Default scene control parameter settings
281
282 // camera
283 sc->cam.cfg = DefaultCamera;
284 SetCamera(&sc->cam); // never changes in this test
285
286 // model control matrix
287 MTXScale(sc->modelCtrl, 1.0F, 1.0F, 1.0F);
288 sc->modelScale = 1.0F;
289
290 // texture number
291 sc->texNumber = 0;
292
293 // tev mode
294 sc->tevMode = GX_REPLACE;
295 }
296
297 /*---------------------------------------------------------------------------*
298 Name: DrawTick
299
300 Description: Draw the model by using given scene parameters
301
302 Arguments: sc : pointer to the structure of scene control parameters
303
304 Returns: none
305 *---------------------------------------------------------------------------*/
DrawTick(MySceneCtrlObj * sc)306 static void DrawTick( MySceneCtrlObj* sc )
307 {
308 Mtx ms; // Scaling matrix.
309 Mtx mv; // Modelview matrix.
310
311 // set tev operation (Decal/Replace)
312 GXSetTevOp(GX_TEVSTAGE0, sc->tevMode);
313 GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
314
315 // set texture
316 GXLoadTexObj(&MyTexObj[sc->texNumber], GX_TEXMAP0);
317
318 // set modelview matrix
319 MTXScale(ms, sc->modelScale, sc->modelScale, sc->modelScale);
320 MTXConcat(sc->cam.view, ms, mv);
321 MTXConcat(mv, sc->modelCtrl, mv);
322 GXLoadPosMtxImm(mv, GX_PNMTX0);
323
324 DrawBox();
325 }
326
327 /*---------------------------------------------------------------------------*
328 Name: AnimTick
329
330 Description: Changes scene parameters according to the pad status.
331
332 Arguments: sc : pointer to the structure of scene control parameters
333
334 Returns: none
335 *---------------------------------------------------------------------------*/
AnimTick(MySceneCtrlObj * sc)336 static void AnimTick( MySceneCtrlObj* sc )
337 {
338 u16 down;
339 Mtx mrx, mry;
340
341 // PAD
342 down = DEMOPadGetButtonDown(0);
343
344 // Model rotation calculation
345 MTXRotDeg(mry, 'x', -(f32)(DEMOPadGetStickY(0))/32.0F);
346 MTXRotDeg(mrx, 'y', (f32)(DEMOPadGetStickX(0))/32.0F);
347 MTXConcat(mry, sc->modelCtrl, sc->modelCtrl);
348 MTXConcat(mrx, sc->modelCtrl, sc->modelCtrl);
349
350 // Model scale control
351 sc->modelScale += ( DEMOPadGetSubStickY(0) / 32 ) * 0.005F;
352 Clamp(sc->modelScale, 0.1F, 2.0F);
353
354
355 // Texture select
356 if ( down & PAD_BUTTON_A )
357 {
358 sc->texNumber = ( sc->texNumber + 1 ) % MAX_TEXTURES;
359 Message(sc->texNumber);
360 }
361
362 // Tev mode control
363 if ( down & PAD_BUTTON_B )
364 {
365 sc->tevMode = ( sc->tevMode == GX_REPLACE ) ?
366 GX_DECAL : GX_REPLACE;
367 }
368 }
369
370 /*---------------------------------------------------------------------------*
371 Name: VertexT
372
373 Description: Draw a vertex with texture, direct data
374
375 Arguments: v 8-bit position index
376 s, t 16-bit tex coord
377
378 Returns: none
379 *---------------------------------------------------------------------------*/
VertexT(u8 v,u32 s,u32 t,u8 c)380 static inline void VertexT( u8 v, u32 s, u32 t, u8 c )
381 {
382 GXPosition1x8(v);
383 GXColor1x8(c);
384 GXTexCoord2u16((u16)s, (u16)t);
385 }
386
387 /*---------------------------------------------------------------------------*
388 Name: DrawTexQuad
389
390 Description: Draw a textured quad. Map extends to corners of the quad.
391 MAX_SCOORD is the value of 1.0 in the fixed point format.
392
393 Arguments: v0 8-bit position
394 v1 8-bit position
395 v2 8-bit position
396 v3 8-bit position
397 s0 s tex coord at v0
398 t0 t tex coord at v0
399
400 Returns: none
401 *---------------------------------------------------------------------------*/
DrawTexQuad(u8 v0,u8 v1,u8 v2,u8 v3,u32 s0,u32 t0,u8 c)402 static inline void DrawTexQuad(
403 u8 v0,
404 u8 v1,
405 u8 v2,
406 u8 v3,
407 u32 s0,
408 u32 t0,
409 u8 c )
410 {
411 VertexT(v0, s0+MAX_SCOORD, t0, c);
412 VertexT(v1, s0+MAX_SCOORD, t0+MAX_TCOORD, c);
413 VertexT(v2, s0, t0+MAX_TCOORD, c);
414 VertexT(v3, s0, t0, c);
415 }
416
417 /*---------------------------------------------------------------------------*
418 Name: DrawBox
419
420 Description: Draw box model.
421
422 Arguments: none
423
424 Returns: none
425 *---------------------------------------------------------------------------*/
DrawBox(void)426 static void DrawBox( void )
427 {
428 static u32 s = 0;
429 static u32 t = 0;
430
431 // set vertex descriptor
432 GXClearVtxDesc();
433 GXSetVtxDesc(GX_VA_POS, GX_INDEX8);
434 GXSetVtxDesc(GX_VA_CLR0, GX_INDEX8);
435 GXSetVtxDesc(GX_VA_TEX0, GX_DIRECT);
436
437 // draw the box
438 GXBegin(GX_QUADS, GX_VTXFMT0, 4*6);
439 DrawTexQuad(3, 2, 1, 0, s, t, 0);
440 DrawTexQuad(4, 5, 6, 7, s, s, 1);
441 DrawTexQuad(4, 7, 3, 0, 0, 0, 2);
442 DrawTexQuad(5, 4, 0, 1, t, t, 3);
443 DrawTexQuad(6, 5, 1, 2, t, 0, 4);
444 DrawTexQuad(7, 6, 2, 3, 0, s, 5);
445 GXEnd();
446
447 // translate s, t coordinates
448 s += STEP_SCOORD;
449 t += STEP_TCOORD;
450 if (s > MAX_SCOORD) s = 0;
451 if (t > MAX_TCOORD) t = 0;
452
453 }
454
455 /*---------------------------------------------------------------------------*
456 Name: SetCamera
457
458 Description: set view matrix and load projection matrix into hardware
459
460 Arguments: cam : pointer to the MyCameraObj structure
461
462 Returns: none
463 *---------------------------------------------------------------------------*/
SetCamera(MyCameraObj * cam)464 static void SetCamera( MyCameraObj* cam )
465 {
466 MTXLookAt(
467 cam->view,
468 &cam->cfg.location,
469 &cam->cfg.up,
470 &cam->cfg.target );
471
472 MTXFrustum(
473 cam->proj,
474 cam->cfg.top,
475 - (cam->cfg.top),
476 cam->cfg.left,
477 - (cam->cfg.left),
478 cam->cfg.znear,
479 cam->cfg.zfar );
480 GXSetProjection(cam->proj, GX_PERSPECTIVE);
481 }
482
483 /*---------------------------------------------------------------------------*
484 Name: Message
485
486 Description: Output message for debugging
487
488 Arguments: num : current texture number
489
490 Returns: none
491 *---------------------------------------------------------------------------*/
Message(u32 num)492 void Message( u32 num )
493 {
494 switch(num)
495 {
496 case 0 : OSReport("I4 Format\n"); break;
497 case 1 : OSReport("I8 Format\n"); break;
498 case 2 : OSReport("IA4 Format\n"); break;
499 case 3 : OSReport("IA8 Format\n"); break;
500 case 4 : OSReport("RGB565 Format\n"); break;
501 case 5 : OSReport("RGB5A3 Format\n"); break;
502 case 6 : OSReport("RGBA8 Format\n"); break;
503 case 7 : OSReport("Compressed Format\n"); break;
504 case 8 : OSReport("C4 Format\n"); break;
505 case 9 : OSReport("C8 Format\n"); break;
506 case 10 : OSReport("C14X2 Format\n"); break;
507 }
508 }
509
510 /*---------------------------------------------------------------------------*
511 Name: PrintIntro
512
513 Description: Prints the directions on how to use this demo.
514
515 Arguments: none
516
517 Returns: none
518 *---------------------------------------------------------------------------*/
PrintIntro(void)519 static void PrintIntro( void )
520 {
521 OSReport("\n\n");
522 OSReport("******************************************************\n");
523 OSReport("tex-fmt-cube: various format test (non-tpl version)\n");
524 OSReport("******************************************************\n");
525 OSReport("to quit hit the start button\n");
526 OSReport("\n");
527 OSReport("Main Stick : rotate the box.\n");
528 OSReport("Sub Stick Y : change scale of the box.\n");
529 OSReport("A button : change the texture.\n");
530 OSReport("B button : change tev mode.\n");
531 OSReport("******************************************************\n");
532 OSReport("\n");
533 }
534
535 /*============================================================================*/
536