1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     DL-tex.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    Defines
19  *---------------------------------------------------------------------------*/
20 
21 // Defines for setting texture registers...
22 
23 //
24 //  tx_setmode0 register
25 //
26 #define TX_SETMODE0_WRAP_S_SHIFT        0
27 #define TX_SETMODE0_WRAP_T_SHIFT        2
28 #define TX_SETMODE0_MAG_FILTER_SHIFT    4
29 #define TX_SETMODE0_MIN_FILTER_SHIFT    5
30 #define TX_SETMODE0_DIAGLOD_SHIFT       8
31 #define TX_SETMODE0_LODBIAS_SHIFT       9
32 #define TX_SETMODE0_MAXANISO_SHIFT      19
33 #define TX_SETMODE0_LODCLAMP_SHIFT      21
34 #define TX_SETMODE0_RID_SHIFT           24
35 #define TX_SETMODE0(wrap_s, wrap_t, mag_filter, min_filter, diaglod, lodbias, maxaniso, lodclamp, rid) \
36         ((((u32)(wrap_s)) << TX_SETMODE0_WRAP_S_SHIFT) | \
37          (((u32)(wrap_t)) << TX_SETMODE0_WRAP_T_SHIFT) | \
38          (((u32)(mag_filter)) << TX_SETMODE0_MAG_FILTER_SHIFT) | \
39          (((u32)(min_filter)) << TX_SETMODE0_MIN_FILTER_SHIFT) | \
40          (((u32)(diaglod)) << TX_SETMODE0_DIAGLOD_SHIFT) | \
41          (((u32)(lodbias)) << TX_SETMODE0_LODBIAS_SHIFT) | \
42          (((u32)(maxaniso)) << TX_SETMODE0_MAXANISO_SHIFT) | \
43          (((u32)(lodclamp)) << TX_SETMODE0_LODCLAMP_SHIFT) | \
44          (((u32)(rid)) << TX_SETMODE0_RID_SHIFT))
45 
46 //
47 //  tx_setmode1 register
48 //
49 #define TX_SETMODE1_MINLOD_SHIFT        0
50 #define TX_SETMODE1_MAXLOD_SHIFT        8
51 #define TX_SETMODE1_RID_SHIFT           24
52 #define TX_SETMODE1(minlod, maxlod, rid) \
53         ((((u32)(minlod)) << TX_SETMODE1_MINLOD_SHIFT) | \
54          (((u32)(maxlod)) << TX_SETMODE1_MAXLOD_SHIFT) | \
55          (((u32)(rid)) << TX_SETMODE1_RID_SHIFT))
56 
57 //
58 //  tx_setimage0 register
59 //
60 #define TX_SETIMAGE0_WIDTH_SHIFT        0
61 #define TX_SETIMAGE0_HEIGHT_SHIFT       10
62 #define TX_SETIMAGE0_FORMAT_SHIFT       20
63 #define TX_SETIMAGE0_RID_SHIFT          24
64 #define TX_SETIMAGE0(width, height, format, rid) \
65         ((((u32)(width)) << TX_SETIMAGE0_WIDTH_SHIFT) | \
66          (((u32)(height)) << TX_SETIMAGE0_HEIGHT_SHIFT) | \
67          (((u32)(format)) << TX_SETIMAGE0_FORMAT_SHIFT) | \
68          (((u32)(rid)) << TX_SETIMAGE0_RID_SHIFT))
69 
70 //
71 //  tx_setimage1 register
72 //
73 #define TX_SETIMAGE1_TMEM_OFFSET_SHIFT  0
74 #define TX_SETIMAGE1_CACHE_WIDTH_SHIFT  15
75 #define TX_SETIMAGE1_CACHE_HEIGHT_SHIFT 18
76 #define TX_SETIMAGE1_IMAGE_TYPE_SHIFT   21
77 #define TX_SETIMAGE1_RID_SHIFT          24
78 #define TX_SETIMAGE1(tmem_offset, cache_width, cache_height, image_type, rid) \
79         ((((u32)(tmem_offset)) << TX_SETIMAGE1_TMEM_OFFSET_SHIFT) | \
80          (((u32)(cache_width)) << TX_SETIMAGE1_CACHE_WIDTH_SHIFT) | \
81          (((u32)(cache_height)) << TX_SETIMAGE1_CACHE_HEIGHT_SHIFT) | \
82          (((u32)(image_type)) << TX_SETIMAGE1_IMAGE_TYPE_SHIFT) | \
83          (((u32)(rid)) << TX_SETIMAGE1_RID_SHIFT))
84 
85 //
86 //  tx_setimage2 register
87 //
88 #define TX_SETIMAGE2_TMEM_OFFSET_SHIFT  0
89 #define TX_SETIMAGE2_CACHE_WIDTH_SHIFT  15
90 #define TX_SETIMAGE2_CACHE_HEIGHT_SHIFT 18
91 #define TX_SETIMAGE2_RID_SHIFT          24
92 #define TX_SETIMAGE2(tmem_offset, cache_width, cache_height, rid) \
93         ((((u32)(tmem_offset)) << TX_SETIMAGE2_TMEM_OFFSET_SHIFT) | \
94          (((u32)(cache_width)) << TX_SETIMAGE2_CACHE_WIDTH_SHIFT) | \
95          (((u32)(cache_height)) << TX_SETIMAGE2_CACHE_HEIGHT_SHIFT) | \
96          (((u32)(rid)) << TX_SETIMAGE2_RID_SHIFT))
97 
98 //
99 //  tx_setimage3 register
100 //
101 #define TX_SETIMAGE3_IMAGE_BASE_SHIFT   0
102 #define TX_SETIMAGE3_RID_SHIFT          24
103 #define TX_SETIMAGE3(image_base, rid) \
104         ((((u32)(image_base)) << TX_SETIMAGE3_IMAGE_BASE_SHIFT) | \
105          (((u32)(rid)) << TX_SETIMAGE3_RID_SHIFT))
106 
107 //
108 //  tx_settlut register
109 //
110 #define TX_SETTLUT_TMEM_OFFSET_SHIFT    0
111 #define TX_SETTLUT_FORMAT_SHIFT         10
112 #define TX_SETTLUT_RID_SHIFT            24
113 #define TX_SETTLUT(tmem_offset, format, rid) \
114         ((((u32)(tmem_offset)) << TX_SETTLUT_TMEM_OFFSET_SHIFT) | \
115          (((u32)(format)) << TX_SETTLUT_FORMAT_SHIFT) | \
116          (((u32)(rid)) << TX_SETTLUT_RID_SHIFT))
117 
118 //
119 //  su_ts0 struct
120 //
121 #define SU_TS0_SSIZE_SHIFT              0
122 #define SU_TS0_BS_SHIFT                 16
123 #define SU_TS0_WS_SHIFT                 17
124 #define SU_TS0_LF_SHIFT                 18
125 #define SU_TS0_PF_SHIFT                 19
126 #define SU_TS0_RID_SHIFT                24
127 #define SU_TS0(ssize, bs, ws, lf, pf, rid) \
128         ((((u32)(ssize)) << SU_TS0_SSIZE_SHIFT) | \
129          (((u32)(bs)) << SU_TS0_BS_SHIFT) | \
130          (((u32)(ws)) << SU_TS0_WS_SHIFT) | \
131          (((u32)(lf)) << SU_TS0_LF_SHIFT) | \
132          (((u32)(pf)) << SU_TS0_PF_SHIFT) | \
133          (((u32)(rid)) << SU_TS0_RID_SHIFT))
134 
135 //
136 //  su_ts1 struct
137 //
138 #define SU_TS1_TSIZE_SHIFT              0
139 #define SU_TS1_BT_SHIFT                 16
140 #define SU_TS1_WT_SHIFT                 17
141 #define SU_TS1_RID_SHIFT                24
142 #define SU_TS1(tsize, bt, wt, rid) \
143         ((((u32)(tsize)) << SU_TS1_TSIZE_SHIFT) | \
144          (((u32)(bt)) << SU_TS1_BT_SHIFT) | \
145          (((u32)(wt)) << SU_TS1_WT_SHIFT) | \
146          (((u32)(rid)) << SU_TS1_RID_SHIFT))
147 
148 
149 // These register ID's can also be found in GX run-time arrays.
150 // However, we need these #define's here for compile-time access.
151 //
152 // The following are defined per texture ID
153 //
154 #define TX_SETMODE0_I0_ID       0x80
155 #define TX_SETMODE0_I1_ID       0x81
156 #define TX_SETMODE0_I2_ID       0x82
157 #define TX_SETMODE0_I3_ID       0x83
158 #define TX_SETMODE0_I4_ID       0xa0
159 #define TX_SETMODE0_I5_ID       0xa1
160 #define TX_SETMODE0_I6_ID       0xa2
161 #define TX_SETMODE0_I7_ID       0xa3
162 
163 #define TX_SETMODE1_I0_ID       0x84
164 #define TX_SETMODE1_I1_ID       0x85
165 #define TX_SETMODE1_I2_ID       0x86
166 #define TX_SETMODE1_I3_ID       0x87
167 #define TX_SETMODE1_I4_ID       0xa4
168 #define TX_SETMODE1_I5_ID       0xa5
169 #define TX_SETMODE1_I6_ID       0xa6
170 #define TX_SETMODE1_I7_ID       0xa7
171 
172 #define TX_SETIMAGE0_I0_ID      0x88
173 #define TX_SETIMAGE0_I1_ID      0x89
174 #define TX_SETIMAGE0_I2_ID      0x8a
175 #define TX_SETIMAGE0_I3_ID      0x8b
176 #define TX_SETIMAGE0_I4_ID      0xa8
177 #define TX_SETIMAGE0_I5_ID      0xa9
178 #define TX_SETIMAGE0_I6_ID      0xaa
179 #define TX_SETIMAGE0_I7_ID      0xab
180 
181 #define TX_SETIMAGE1_I0_ID      0x8c
182 #define TX_SETIMAGE1_I1_ID      0x8d
183 #define TX_SETIMAGE1_I2_ID      0x8e
184 #define TX_SETIMAGE1_I3_ID      0x8f
185 #define TX_SETIMAGE1_I4_ID      0xac
186 #define TX_SETIMAGE1_I5_ID      0xad
187 #define TX_SETIMAGE1_I6_ID      0xae
188 #define TX_SETIMAGE1_I7_ID      0xaf
189 
190 #define TX_SETIMAGE2_I0_ID      0x90
191 #define TX_SETIMAGE2_I1_ID      0x91
192 #define TX_SETIMAGE2_I2_ID      0x92
193 #define TX_SETIMAGE2_I3_ID      0x93
194 #define TX_SETIMAGE2_I4_ID      0xb0
195 #define TX_SETIMAGE2_I5_ID      0xb1
196 #define TX_SETIMAGE2_I6_ID      0xb2
197 #define TX_SETIMAGE2_I7_ID      0xb3
198 
199 #define TX_SETIMAGE3_I0_ID      0x94
200 #define TX_SETIMAGE3_I1_ID      0x95
201 #define TX_SETIMAGE3_I2_ID      0x96
202 #define TX_SETIMAGE3_I3_ID      0x97
203 #define TX_SETIMAGE3_I4_ID      0xb4
204 #define TX_SETIMAGE3_I5_ID      0xb5
205 #define TX_SETIMAGE3_I6_ID      0xb6
206 #define TX_SETIMAGE3_I7_ID      0xb7
207 
208 #define TX_SETTLUT_I0_ID        0x98
209 #define TX_SETTLUT_I1_ID        0x99
210 #define TX_SETTLUT_I2_ID        0x9a
211 #define TX_SETTLUT_I3_ID        0x9b
212 #define TX_SETTLUT_I4_ID        0xb8
213 #define TX_SETTLUT_I5_ID        0xb9
214 #define TX_SETTLUT_I6_ID        0xba
215 #define TX_SETTLUT_I7_ID        0xbb
216 
217 // The following are defined per texcoord
218 
219 #define SU_SSIZE0_ID            0x30
220 #define SU_TSIZE0_ID            0x31
221 #define SU_SSIZE1_ID            0x32
222 #define SU_TSIZE1_ID            0x33
223 #define SU_SSIZE2_ID            0x34
224 #define SU_TSIZE2_ID            0x35
225 #define SU_SSIZE3_ID            0x36
226 #define SU_TSIZE3_ID            0x37
227 #define SU_SSIZE4_ID            0x38
228 #define SU_TSIZE4_ID            0x39
229 #define SU_SSIZE5_ID            0x3a
230 #define SU_TSIZE5_ID            0x3b
231 #define SU_SSIZE6_ID            0x3c
232 #define SU_TSIZE6_ID            0x3d
233 #define SU_SSIZE7_ID            0x3e
234 #define SU_TSIZE7_ID            0x3f
235 
236 // Break a word into its constituent bytes.
237 //
238 #define B3(word) (u8)(((word) >> 24) & 0xff)
239 #define B2(word) (u8)(((word) >> 16) & 0xff)
240 #define B1(word) (u8)(((word) >>  8) & 0xff)
241 #define B0(word) (u8)(((word) >>  0) & 0xff)
242 
243 // Break a word or half-word into a comma-seperated byte sequence
244 //
245 #define WTB(word) B3(word), B2(word), B1(word), B0(word)
246 #define HTB(half) B1(half), B0(half)
247 
248 /*---------------------------------------------------------------------------*
249    Forward references
250  *---------------------------------------------------------------------------*/
251 
252 void        main            ( void );
253 
254 static void CameraInit      ( void );
255 static void DrawInit        ( void );
256 static void DrawTick        ( void );
257 
258 static void AnimTick        ( void );
259 
260 static void ParameterInit   ( void );
261 
262 static void SendVertex      ( u16 posIndex, u16 texCoordIndex );
263 
264 static void PrintIntro      ( void );
265 
266 #define SIDE 30
267 #define NORM (sqrt(3.0))/3.0
268 
269 /*---------------------------------------------------------------------------*
270    Global variables
271  *---------------------------------------------------------------------------*/
272 
273 // Display lists *************************************************************
274 
275 // Commands to initialize (most of) texture ID 0 and texcoord 0 scale:
276 
277 u8 InitList[] ATTRIBUTE_ALIGN(32) =
278 {
279     GX_LOAD_BP_REG,
280     //               wps wpt mgf mnf dgl lbi mxa lcl RID
281     WTB(TX_SETMODE0(  1,  1,  1,  6,  0,  0,  0,  0, TX_SETMODE0_I0_ID)),
282 
283     GX_LOAD_BP_REG,
284     //                 mnl   mxl  RID
285     WTB(TX_SETMODE1(  0<<4, 7<<4, TX_SETMODE1_I0_ID)),
286 
287     // TX_SETIMAGE0 (width, height, format) is loaded in the DrawList
288 
289     GX_LOAD_BP_REG,
290     //                tmem       cw ch type RID
291     WTB(TX_SETIMAGE1( 0x00000>>5, 3, 3, 0,  TX_SETIMAGE1_I0_ID)),
292 
293     GX_LOAD_BP_REG,
294     //                tmem       cw ch  RID
295     WTB(TX_SETIMAGE2( 0x80000>>5, 3, 3, TX_SETIMAGE2_I0_ID)),
296 
297     // TX_SETIMAGE3 is loaded in the DrawList
298 
299     GX_LOAD_BP_REG,
300     //              tmem(high)  fmt RID
301     WTB(TX_SETTLUT( 0x40000>>5, 1,  TX_SETTLUT_I0_ID)),
302 
303     // 0x400000 corresponds to GX_TLUT0's location
304     // We don't actually use the CI texture in this demo...
305     // We'd have to modify the texture cache locations, and we're too lazy.
306 
307     GX_LOAD_BP_REG,
308     //          ssz-1 bs ws lf pf RID
309     WTB(SU_TS0(  127, 0, 0, 0, 0, SU_SSIZE0_ID)),
310 
311     GX_LOAD_BP_REG,
312     //          tsz-1 bt wt RID
313     WTB(SU_TS1(  127, 0, 0, SU_TSIZE0_ID)),
314 
315     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // pad to 32B
316     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    // pad to 32B
317 };
318 
319 #define INITSIZE sizeof(InitList)
320 
321 // Commands to select textures and draw the actual geometry:
322 
323 u8 DrawList[] ATTRIBUTE_ALIGN(32) =
324 {
325     // face 1
326 
327     GX_LOAD_BP_REG,
328     //                w-1  h-1  format       RID
329     WTB(TX_SETIMAGE0( 127, 127, GX_TF_RGBA8, TX_SETIMAGE0_I0_ID)),
330 
331     GX_LOAD_BP_REG,
332     //                mem  RID  (will fill in "mem" at run time)
333     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
334 
335     GX_DRAW_QUADS, HTB(4),
336     0, 0, 0,
337     1, 1, 1,
338     2, 2, 2,
339     3, 3, 3,
340 
341     // face 2
342 
343     GX_LOAD_BP_REG,
344     //                w-1  h-1  format       RID
345     WTB(TX_SETIMAGE0( 127, 127, GX_TF_RGB5A3, TX_SETIMAGE0_I0_ID)),
346 
347     GX_LOAD_BP_REG,
348     //                mem  RID  (will fill in "mem" at run time)
349     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
350 
351     GX_DRAW_QUADS, HTB(4),
352     4, 4, 0,
353     5, 5, 1,
354     6, 6, 2,
355     7, 7, 3,
356 
357     // face 3
358 
359     GX_LOAD_BP_REG,
360     //                w-1  h-1  format       RID
361     WTB(TX_SETIMAGE0( 127, 127, GX_TF_CMPR, TX_SETIMAGE0_I0_ID)),
362 
363     GX_LOAD_BP_REG,
364     //                mem  RID  (will fill in "mem" at run time)
365     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
366 
367     GX_DRAW_QUADS, HTB(4),
368     2, 2, 0,
369     6, 6, 1,
370     5, 5, 2,
371     3, 3, 3,
372 
373     // face 4
374 
375     GX_LOAD_BP_REG,
376     //                w-1  h-1  format       RID
377     WTB(TX_SETIMAGE0( 127, 127, GX_TF_RGBA8, TX_SETIMAGE0_I0_ID)),
378 
379     GX_LOAD_BP_REG,
380     //                mem  RID  (will fill in "mem" at run time)
381     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
382 
383     GX_DRAW_QUADS, HTB(4),
384     1, 1, 0,
385     0, 0, 1,
386     4, 4, 2,
387     7, 7, 3,
388 
389     // face 5
390 
391     GX_LOAD_BP_REG,
392     //                w-1  h-1  format       RID
393     WTB(TX_SETIMAGE0( 127, 127, GX_TF_I4, TX_SETIMAGE0_I0_ID)),
394 
395     GX_LOAD_BP_REG,
396     //                mem  RID  (will fill in "mem" at run time)
397     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
398 
399     GX_DRAW_QUADS, HTB(4),
400     5, 5, 0,
401     4, 4, 1,
402     0, 0, 2,
403     3, 3, 3,
404 
405     // face 6
406 
407     GX_LOAD_BP_REG,
408     //                w-1  h-1  format       RID
409     WTB(TX_SETIMAGE0( 127, 127, GX_TF_IA8, TX_SETIMAGE0_I0_ID)),
410 
411     GX_LOAD_BP_REG,
412     //                mem  RID  (will fill in "mem" at run time)
413     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
414 
415     GX_DRAW_QUADS, HTB(4),
416     6, 6, 0,
417     2, 2, 1,
418     1, 1, 2,
419     7, 7, 3,
420 
421     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // pad to 32B
422 };
423 
424 #define DRAWSIZE sizeof(DrawList)
425 
426 // For size of one face, take off padding, divide by # of faces
427 
428 #define FACESIZE ((DRAWSIZE - 10)/6)
429 
430 // This array tells us the offsets for where to set the memory addresses
431 // for the textures in the DrawList.  We don't know this information until
432 // runtime, so we need to know which locations to fix up.
433 
434 u32 DLOffsets[6] =
435 {
436     6+FACESIZE*0,
437     6+FACESIZE*1,
438     6+FACESIZE*2,
439     6+FACESIZE*3,
440     6+FACESIZE*4,
441     6+FACESIZE*5,
442 };
443 
444 // Actual primitive data follows
445 
446 float FloatVert[] ATTRIBUTE_ALIGN(32) =
447 {
448     -SIDE,  SIDE, -SIDE,
449     -SIDE,  SIDE,  SIDE,
450     -SIDE, -SIDE,  SIDE,
451     -SIDE, -SIDE, -SIDE,
452      SIDE,  SIDE, -SIDE,
453      SIDE, -SIDE, -SIDE,
454      SIDE, -SIDE,  SIDE,
455      SIDE,  SIDE,  SIDE,
456 };
457 
458 float FloatNorm[] ATTRIBUTE_ALIGN(32) =
459 {
460     -1,  1, -1,
461     -1,  1,  1,
462     -1, -1,  1,
463     -1, -1, -1,
464      1,  1, -1,
465      1, -1, -1,
466      1, -1,  1,
467      1,  1,  1,
468 };
469 
470 float FloatTex[] ATTRIBUTE_ALIGN(32) =
471 {
472     0.0F, 0.0F,
473     1.0F, 0.0F,
474     1.0F, 1.0F,
475     0.0F, 1.0F,
476 };
477 
478 Mtx v;          // view matrix
479 u32 rot;        // current cube rotation
480 
481 TPLPalettePtr tpl = 0;
482 
483 /*---------------------------------------------------------------------------*
484    Application main loop
485  *---------------------------------------------------------------------------*/
main(void)486 void main ( void )
487 {
488     DEMOInit(NULL);
489 
490     DrawInit();         // Define my vertex formats and set array pointers.
491 
492     ParameterInit();
493 
494     PrintIntro();
495     DEMOPadRead();      // Read the joystick for this frame
496 
497     // While the quit button is not pressed
498     while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
499     {
500         DEMOPadRead();  // Read the joystick for this frame
501 
502         // Do animation based on input
503         AnimTick();
504 
505         DEMOBeforeRender();
506 
507         DrawTick();     // Draw the model.
508 
509         DEMODoneRender();
510     }
511 
512     OSHalt("End of test");
513 }
514 
515 /*---------------------------------------------------------------------------*
516    Functions
517  *---------------------------------------------------------------------------*/
518 
519 
520 /*---------------------------------------------------------------------------*
521     Name:           CameraInit
522 
523     Description:    Initialize the projection matrix and load into hardware.
524 
525     Arguments:      v   view matrix to be passed to ViewInit
526                     cameraLocScale  scale for the camera's distance from the
527                                     object - to be passed to ViewInit
528 
529     Returns:        none
530  *---------------------------------------------------------------------------*/
CameraInit(void)531 static void CameraInit      ( void )
532 {
533     Mtx44 p;
534     Vec camPt = {0.0F, 0.0F, 650.0F};
535     Vec up = {0.0F, 1.0F, 0.0F};
536     Vec origin = {0.0F, 0.0F, 0.0F};
537 
538     MTXFrustum(p, 112, -112, -160, 160, 500, 2000);
539 
540     GXSetProjection(p, GX_PERSPECTIVE);
541 
542     MTXLookAt(v, &camPt, &up, &origin);
543 }
544 
545 /*---------------------------------------------------------------------------*
546     Name:           DrawInit
547 
548     Description:    Calls the correct initialization function for the current
549                     model.
550 
551     Arguments:      none
552 
553     Returns:        none
554  *---------------------------------------------------------------------------*/
DrawInit(void)555 static void DrawInit( void )
556 {
557     Mtx rz;
558     u32 i;
559     TPLDescriptorPtr tdescp;
560     GXTexObj  to0;
561     GXTlutObj tlo1;
562     u32       data;
563     u32       reg;
564     static u8 ftt[]={0, 1, 2, 5, 10, 13};  // face-to-texture table, from below
565     GXLightObj MyLight;
566     GXColor White = {255, 255, 255, 255};
567 
568     CameraInit();   // Initialize the camera.
569 
570     GXClearVtxDesc();
571 
572     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
573     GXSetVtxDesc(GX_VA_TEX0, GX_INDEX8);
574     GXSetArray(GX_VA_TEX0, FloatTex, 8);
575 
576     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_NRM, GX_NRM_XYZ, GX_F32, 0);
577     GXSetVtxDesc(GX_VA_NRM, GX_INDEX8);
578     GXSetArray(GX_VA_NRM, FloatNorm, 12);
579 
580     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
581     GXSetVtxDesc (GX_VA_POS, GX_INDEX8);
582     GXSetArray(GX_VA_POS, FloatVert, 12);
583 
584     TPLGetPalette(&tpl, "gxTests/tex-06.tpl");
585 
586     // In tex-06.tpl:
587     //
588     // index width height format ws wt mnf mgf mnl mxl lb mm face
589     //   0    128   128   RGBA8  r  r  l/l  l   0   7   0 Y   1
590     //   1    128   128   RGB5A3 r  r  l/l  l   0   7   0 Y   2
591     //   2    128   128   CMPR   r  r  l/l  l   0   7   0 Y   3
592     //   3    128   128   CI8    r  r  l/l  l   0   7   0 N
593     //   4    128   128   RGB565 r  r  l/l  l   0   7   0 Y
594     //   5    128   128   RGBA8  r  r  l/l  l   0   7   0 Y   4
595     //   6    128   128   RGBA8  r  r  l/l  l   0   7   0 Y
596     //   7    128   128   RGB565 r  r  l/l  l   0   7   0 Y
597     //   8    128   128   CMPR   r  r  l/l  l   0   7   0 Y
598     //   9    128   128   RGB5A3 r  r  l/l  l   0   7   0 Y
599     //  10    128   128   I4     r  r  l/l  l   0   7   0 Y   5
600     //  11    128   128   IA4    r  r  l/l  l   0   7   0 Y
601     //  12    128   128   I8     r  r  l/l  l   0   7   0 Y
602     //  13    128   128   IA8    r  r  l/l  l   0   7   0 Y   6
603 
604     // Set up a light
605     GXInitLightPos(&MyLight, 0.0F, 0.0F, 0.0F);
606     GXInitLightColor(&MyLight, White);
607     GXLoadLightObjImm(&MyLight, GX_LIGHT0);
608     GXSetChanCtrl(
609         GX_COLOR0,
610         GX_ENABLE,   // enable channel
611         GX_SRC_REG,  // amb source
612         GX_SRC_REG,  // mat source
613         GX_LIGHT0,   // light mask
614         GX_DF_CLAMP, // diffuse function
615         GX_AF_NONE);
616 
617     // Set up TEV
618     GXSetNumChans(1);
619     GXSetNumTevStages(1);
620     GXSetTevOp   (GX_TEVSTAGE0, GX_MODULATE);
621     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
622 
623     // Fix up normals
624     for(i = 0; i < 24; i++)
625     {
626         FloatNorm[i] *= NORM;
627     }
628 
629     // Set up texcoord gen
630     MTXIdentity(rz);
631     GXLoadTexMtxImm(rz, GX_TEXMTX0, GX_MTX2x4);
632     GXSetNumTexGens(1);
633     GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_TEXMTX0);
634 
635     // Set ALL texture coordinates to scale manually.
636     // This way, GX won't try to do any automatic scaling.
637     GXSetTexCoordScaleManually(GX_TEXCOORD0, GX_ENABLE, 1, 1);
638     GXSetTexCoordScaleManually(GX_TEXCOORD1, GX_ENABLE, 1, 1);
639     GXSetTexCoordScaleManually(GX_TEXCOORD2, GX_ENABLE, 1, 1);
640     GXSetTexCoordScaleManually(GX_TEXCOORD3, GX_ENABLE, 1, 1);
641     GXSetTexCoordScaleManually(GX_TEXCOORD4, GX_ENABLE, 1, 1);
642     GXSetTexCoordScaleManually(GX_TEXCOORD5, GX_ENABLE, 1, 1);
643     GXSetTexCoordScaleManually(GX_TEXCOORD6, GX_ENABLE, 1, 1);
644     GXSetTexCoordScaleManually(GX_TEXCOORD7, GX_ENABLE, 1, 1);
645 
646     // Set up the relevant textures
647     //
648     // We need to insert their memory addresses into the DrawList.
649     //
650     for(i = 0; i < 6; i++)
651     {
652         tdescp = TPLGet(tpl, ftt[i]);
653         if(tdescp->CLUTHeader)
654         {
655             TPLGetGXTexObjFromPaletteCI(tpl, &to0, &tlo1, GX_TLUT0, ftt[i]);
656             GXLoadTlut(&tlo1, 0);
657         }
658         else
659         {
660             TPLGetGXTexObjFromPalette(tpl, &to0, ftt[i]);
661         }
662         data = (u32) GXGetTexObjData(&to0);
663         data = data >> 5; // data is already a physical address
664         reg  = TX_SETIMAGE3( data, TX_SETIMAGE3_I0_ID);
665         *(u32 *)(&DrawList[DLOffsets[i]]) = reg;
666     }
667 
668     // Since we changed it, we need to flush it
669     DCFlushRange((void *)DrawList, DRAWSIZE);
670 
671     // Set up TEX0
672     GXCallDisplayList(InitList, INITSIZE);
673 }
674 
675 /*---------------------------------------------------------------------------*
676     Name:           DrawTick
677 
678     Description:    Draw the current model once.
679 
680     Arguments:      v       view matrix
681                     m       model matrix
682 
683     Returns:        none
684  *---------------------------------------------------------------------------*/
DrawTick(void)685 static void DrawTick( void )
686 {
687     Mtx ry, rz, mv, t;
688     f32 tx, ty;
689 
690     tx = 0.0f;
691     ty = 0.0f;
692 
693     MTXRotDeg(ry, 'Y', (float)rot);
694     MTXRotDeg(rz, 'Z', (float)rot / 2.0F);
695     MTXTrans(t, tx, ty, 0);
696 
697     MTXConcat(rz, ry, mv);
698     MTXConcat(t, mv, mv);
699     MTXConcat(v, mv, mv);
700     GXLoadPosMtxImm(mv, GX_PNMTX0);
701     MTXInverse(mv, mv);
702     MTXTranspose(mv, mv);
703     GXLoadNrmMtxImm(mv, GX_PNMTX0);
704 
705     GXCallDisplayList(DrawList, DRAWSIZE);
706 }
707 
708 /*---------------------------------------------------------------------------*
709     Name:           AnimTick
710 
711     Description:    Animates the camera and object based on the joystick's
712                     state.
713 
714     Arguments:      none
715 
716     Returns:        none
717  *---------------------------------------------------------------------------*/
AnimTick(void)718 static void AnimTick ( void )
719 {
720     u16 buttons = DEMOPadGetButton(0);
721 
722     // Just simple controls right now...
723 
724     if(buttons & PAD_BUTTON_A)
725     {
726         // suspend animation
727     } else {
728 
729         rot ++;
730         if(rot > 1439)
731             rot = 0;
732     }
733 }
734 
735 /*---------------------------------------------------------------------------*
736     Name:           ParameterInit
737 
738     Description:    Initialize parameters for single frame display
739 
740     Arguments:      none
741 
742     Returns:        none
743  *---------------------------------------------------------------------------*/
ParameterInit(void)744 static void ParameterInit ( void )
745 {
746     rot = 45;
747 }
748 
749 /*---------------------------------------------------------------------------*
750     Name:           PrintIntro
751 
752     Description:    Prints the directions on how to use this demo.
753 
754     Arguments:      none
755 
756     Returns:        none
757  *---------------------------------------------------------------------------*/
PrintIntro(void)758 static void PrintIntro( void )
759 {
760     OSReport("\n\n");
761     OSReport("************************************************\n");
762     OSReport("DL-tex \n");
763     OSReport("************************************************\n");
764     OSReport("to quit hit the start button\n");
765     OSReport("\n");
766     OSReport("  A button     : stop rotation\n");
767     OSReport("************************************************\n\n");
768 }
769 
770 /****************************************************************************/
771 
772 
773