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-separated 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 #define SIDE 30
265 #define NORM (sqrt(3.0))/3.0
266 
267 /*---------------------------------------------------------------------------*
268    Global variables
269  *---------------------------------------------------------------------------*/
270 
271 // Display lists *************************************************************
272 
273 // Commands to initialize (most of) texture ID 0 and texcoord 0 scale:
274 
275 u8 InitList[] ATTRIBUTE_ALIGN(32) =
276 {
277     GX_LOAD_BP_REG,
278     //               wps wpt mgf mnf dgl lbi mxa lcl RID
279     WTB(TX_SETMODE0(  1,  1,  1,  6,  0,  0,  0,  0, TX_SETMODE0_I0_ID)),
280 
281     GX_LOAD_BP_REG,
282     //                 mnl   mxl  RID
283     WTB(TX_SETMODE1(  0<<4, 7<<4, TX_SETMODE1_I0_ID)),
284 
285     // TX_SETIMAGE0 (width, height, format) is loaded in the DrawList
286 
287     GX_LOAD_BP_REG,
288     //                tmem       cw ch type RID
289     WTB(TX_SETIMAGE1( 0x00000>>5, 3, 3, 0,  TX_SETIMAGE1_I0_ID)),
290 
291     GX_LOAD_BP_REG,
292     //                tmem       cw ch  RID
293     WTB(TX_SETIMAGE2( 0x80000>>5, 3, 3, TX_SETIMAGE2_I0_ID)),
294 
295     // TX_SETIMAGE3 is loaded in the DrawList
296 
297     GX_LOAD_BP_REG,
298     //              tmem(high)  fmt RID
299     WTB(TX_SETTLUT( 0x40000>>5, 1,  TX_SETTLUT_I0_ID)),
300 
301     // 0x400000 corresponds to GX_TLUT0's location
302     // We don't actually use the CI texture in this demo...
303     // We'd have to modify the texture cache locations, and we're too lazy.
304 
305     GX_LOAD_BP_REG,
306     //          ssz-1 bs ws lf pf RID
307     WTB(SU_TS0(  127, 0, 0, 0, 0, SU_SSIZE0_ID)),
308 
309     GX_LOAD_BP_REG,
310     //          tsz-1 bt wt RID
311     WTB(SU_TS1(  127, 0, 0, SU_TSIZE0_ID)),
312 
313     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // pad to 32B
314     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    // pad to 32B
315 };
316 
317 #define INITSIZE sizeof(InitList)
318 
319 // Commands to select textures and draw the actual geometry:
320 
321 u8 DrawList[] ATTRIBUTE_ALIGN(32) =
322 {
323     // face 1
324 
325     GX_LOAD_BP_REG,
326     //                w-1  h-1  format       RID
327     WTB(TX_SETIMAGE0( 127, 127, GX_TF_RGBA8, TX_SETIMAGE0_I0_ID)),
328 
329     GX_LOAD_BP_REG,
330     //                mem  RID  (will fill in "mem" at run time)
331     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
332 
333     GX_DRAW_QUADS, HTB(4),
334     0, 0, 0,
335     1, 1, 1,
336     2, 2, 2,
337     3, 3, 3,
338 
339     // face 2
340 
341     GX_LOAD_BP_REG,
342     //                w-1  h-1  format       RID
343     WTB(TX_SETIMAGE0( 127, 127, GX_TF_RGB5A3, TX_SETIMAGE0_I0_ID)),
344 
345     GX_LOAD_BP_REG,
346     //                mem  RID  (will fill in "mem" at run time)
347     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
348 
349     GX_DRAW_QUADS, HTB(4),
350     4, 4, 0,
351     5, 5, 1,
352     6, 6, 2,
353     7, 7, 3,
354 
355     // face 3
356 
357     GX_LOAD_BP_REG,
358     //                w-1  h-1  format       RID
359     WTB(TX_SETIMAGE0( 127, 127, GX_TF_CMPR, TX_SETIMAGE0_I0_ID)),
360 
361     GX_LOAD_BP_REG,
362     //                mem  RID  (will fill in "mem" at run time)
363     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
364 
365     GX_DRAW_QUADS, HTB(4),
366     2, 2, 0,
367     6, 6, 1,
368     5, 5, 2,
369     3, 3, 3,
370 
371     // face 4
372 
373     GX_LOAD_BP_REG,
374     //                w-1  h-1  format       RID
375     WTB(TX_SETIMAGE0( 127, 127, GX_TF_RGBA8, TX_SETIMAGE0_I0_ID)),
376 
377     GX_LOAD_BP_REG,
378     //                mem  RID  (will fill in "mem" at run time)
379     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
380 
381     GX_DRAW_QUADS, HTB(4),
382     1, 1, 0,
383     0, 0, 1,
384     4, 4, 2,
385     7, 7, 3,
386 
387     // face 5
388 
389     GX_LOAD_BP_REG,
390     //                w-1  h-1  format       RID
391     WTB(TX_SETIMAGE0( 127, 127, GX_TF_I4, TX_SETIMAGE0_I0_ID)),
392 
393     GX_LOAD_BP_REG,
394     //                mem  RID  (will fill in "mem" at run time)
395     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
396 
397     GX_DRAW_QUADS, HTB(4),
398     5, 5, 0,
399     4, 4, 1,
400     0, 0, 2,
401     3, 3, 3,
402 
403     // face 6
404 
405     GX_LOAD_BP_REG,
406     //                w-1  h-1  format       RID
407     WTB(TX_SETIMAGE0( 127, 127, GX_TF_IA8, TX_SETIMAGE0_I0_ID)),
408 
409     GX_LOAD_BP_REG,
410     //                mem  RID  (will fill in "mem" at run time)
411     WTB(TX_SETIMAGE3( 0x0, TX_SETIMAGE3_I0_ID)),
412 
413     GX_DRAW_QUADS, HTB(4),
414     6, 6, 0,
415     2, 2, 1,
416     1, 1, 2,
417     7, 7, 3,
418 
419     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // pad to 32B
420 };
421 
422 #define DRAWSIZE sizeof(DrawList)
423 
424 // For size of one face, take off padding, divide by # of faces
425 
426 #define FACESIZE ((DRAWSIZE - 10)/6)
427 
428 // This array tells us the offsets for where to set the memory addresses
429 // for the textures in the DrawList.  We don't know this information until
430 // runtime, so we need to know which locations to fix up.
431 
432 u32 DLOffsets[6] =
433 {
434     6+FACESIZE*0,
435     6+FACESIZE*1,
436     6+FACESIZE*2,
437     6+FACESIZE*3,
438     6+FACESIZE*4,
439     6+FACESIZE*5,
440 };
441 
442 // Actual primitive data follows
443 
444 float FloatVert[] ATTRIBUTE_ALIGN(32) =
445 {
446     -SIDE,  SIDE, -SIDE,
447     -SIDE,  SIDE,  SIDE,
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 };
455 
456 float FloatNorm[] ATTRIBUTE_ALIGN(32) =
457 {
458     -1,  1, -1,
459     -1,  1,  1,
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 };
467 
468 float FloatTex[] ATTRIBUTE_ALIGN(32) =
469 {
470     0.0F, 0.0F,
471     1.0F, 0.0F,
472     1.0F, 1.0F,
473     0.0F, 1.0F,
474 };
475 
476 Mtx v;          // view matrix
477 u32 rot;        // current cube rotation
478 
479 TPLPalettePtr tpl = 0;
480 
481 /*---------------------------------------------------------------------------*
482    Application main loop
483  *---------------------------------------------------------------------------*/
main(void)484 void main ( void )
485 {
486     DEMOInit(NULL);
487 
488     DrawInit();         // Define my vertex formats and set array pointers.
489 
490     ParameterInit();
491 
492     DEMOPadRead();      // Read the joystick for this frame
493 
494     // While the quit button is not pressed
495     while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
496     {
497         DEMOPadRead();  // Read the joystick for this frame
498 
499         // Do animation based on input
500         AnimTick();
501 
502         DEMOBeforeRender();
503 
504         DrawTick();     // Draw the model.
505 
506         DEMODoneRender();
507     }
508 
509     OSHalt("End of test");
510 }
511 
512 /*---------------------------------------------------------------------------*
513    Functions
514  *---------------------------------------------------------------------------*/
515 
516 
517 /*---------------------------------------------------------------------------*
518     Name:           CameraInit
519 
520     Description:    Initialize the projection matrix and load into hardware.
521 
522     Arguments:      v   view matrix to be passed to ViewInit
523                     cameraLocScale  scale for the camera's distance from the
524                                     object - to be passed to ViewInit
525 
526     Returns:        none
527  *---------------------------------------------------------------------------*/
CameraInit(void)528 static void CameraInit      ( void )
529 {
530     Mtx44 p;
531     Vec camPt = {0.0F, 0.0F, 650.0F};
532     Vec up = {0.0F, 1.0F, 0.0F};
533     Vec origin = {0.0F, 0.0F, 0.0F};
534 
535     MTXFrustum(p, 112, -112, -160, 160, 500, 2000);
536 
537     GXSetProjection(p, GX_PERSPECTIVE);
538 
539     MTXLookAt(v, &camPt, &up, &origin);
540 }
541 
542 /*---------------------------------------------------------------------------*
543     Name:           DrawInit
544 
545     Description:    Calls the correct initialization function for the current
546                     model.
547 
548     Arguments:      none
549 
550     Returns:        none
551  *---------------------------------------------------------------------------*/
DrawInit(void)552 static void DrawInit( void )
553 {
554     Mtx rz;
555     u32 i;
556     TPLDescriptorPtr tdescp;
557     GXTexObj  to0;
558     GXTlutObj tlo1;
559     u32       data;
560     u32       reg;
561     static u8 ftt[]={0, 1, 2, 5, 10, 13};  // face-to-texture table, from below
562     GXLightObj MyLight;
563     GXColor White = {255, 255, 255, 255};
564 
565     CameraInit();   // Initialize the camera.
566 
567     GXClearVtxDesc();
568 
569     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
570     GXSetVtxDesc(GX_VA_TEX0, GX_INDEX8);
571     GXSetArray(GX_VA_TEX0, FloatTex, 8);
572 
573     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_NRM, GX_NRM_XYZ, GX_F32, 0);
574     GXSetVtxDesc(GX_VA_NRM, GX_INDEX8);
575     GXSetArray(GX_VA_NRM, FloatNorm, 12);
576 
577     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
578     GXSetVtxDesc (GX_VA_POS, GX_INDEX8);
579     GXSetArray(GX_VA_POS, FloatVert, 12);
580 
581     TPLGetPalette(&tpl, "gxTests/tex-06.tpl");
582 
583     // In tex-06.tpl:
584     //
585     // index width height format ws wt mnf mgf mnl mxl lb mm face
586     //   0    128   128   RGBA8  r  r  l/l  l   0   7   0 Y   1
587     //   1    128   128   RGB5A3 r  r  l/l  l   0   7   0 Y   2
588     //   2    128   128   CMPR   r  r  l/l  l   0   7   0 Y   3
589     //   3    128   128   CI8    r  r  l/l  l   0   7   0 N
590     //   4    128   128   RGB565 r  r  l/l  l   0   7   0 Y
591     //   5    128   128   RGBA8  r  r  l/l  l   0   7   0 Y   4
592     //   6    128   128   RGBA8  r  r  l/l  l   0   7   0 Y
593     //   7    128   128   RGB565 r  r  l/l  l   0   7   0 Y
594     //   8    128   128   CMPR   r  r  l/l  l   0   7   0 Y
595     //   9    128   128   RGB5A3 r  r  l/l  l   0   7   0 Y
596     //  10    128   128   I4     r  r  l/l  l   0   7   0 Y   5
597     //  11    128   128   IA4    r  r  l/l  l   0   7   0 Y
598     //  12    128   128   I8     r  r  l/l  l   0   7   0 Y
599     //  13    128   128   IA8    r  r  l/l  l   0   7   0 Y   6
600 
601     // Set up a light
602     GXInitLightPos(&MyLight, 0.0F, 0.0F, 0.0F);
603     GXInitLightColor(&MyLight, White);
604     GXLoadLightObjImm(&MyLight, GX_LIGHT0);
605     GXSetChanCtrl(
606         GX_COLOR0,
607         GX_ENABLE,   // enable channel
608         GX_SRC_REG,  // amb source
609         GX_SRC_REG,  // mat source
610         GX_LIGHT0,   // light mask
611         GX_DF_CLAMP, // diffuse function
612         GX_AF_NONE);
613 
614     // Set up TEV
615     GXSetNumChans(1);
616     GXSetNumTevStages(1);
617     GXSetTevOp   (GX_TEVSTAGE0, GX_MODULATE);
618     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
619 
620     // Fix up normals
621     for(i = 0; i < 24; i++)
622     {
623         FloatNorm[i] *= NORM;
624     }
625 
626     // Set up texcoord gen
627     MTXIdentity(rz);
628     GXLoadTexMtxImm(rz, GX_TEXMTX0, GX_MTX2x4);
629     GXSetNumTexGens(1);
630     GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_TEXMTX0);
631 
632     // Set ALL texture coordinates to scale manually.
633     // This way, GX won't try to do any automatic scaling.
634     GXSetTexCoordScaleManually(GX_TEXCOORD0, GX_ENABLE, 1, 1);
635     GXSetTexCoordScaleManually(GX_TEXCOORD1, GX_ENABLE, 1, 1);
636     GXSetTexCoordScaleManually(GX_TEXCOORD2, GX_ENABLE, 1, 1);
637     GXSetTexCoordScaleManually(GX_TEXCOORD3, GX_ENABLE, 1, 1);
638     GXSetTexCoordScaleManually(GX_TEXCOORD4, GX_ENABLE, 1, 1);
639     GXSetTexCoordScaleManually(GX_TEXCOORD5, GX_ENABLE, 1, 1);
640     GXSetTexCoordScaleManually(GX_TEXCOORD6, GX_ENABLE, 1, 1);
641     GXSetTexCoordScaleManually(GX_TEXCOORD7, GX_ENABLE, 1, 1);
642 
643     // Set up the relevant textures
644     //
645     // We need to insert their memory addresses into the DrawList.
646     //
647     for(i = 0; i < 6; i++)
648     {
649         tdescp = TPLGet(tpl, ftt[i]);
650         if(tdescp->CLUTHeader)
651         {
652             TPLGetGXTexObjFromPaletteCI(tpl, &to0, &tlo1, GX_TLUT0, ftt[i]);
653             GXLoadTlut(&tlo1, 0);
654         }
655         else
656         {
657             TPLGetGXTexObjFromPalette(tpl, &to0, ftt[i]);
658         }
659         data = (u32) GXGetTexObjData(&to0);
660         data = data >> 5; // data is already a physical address
661         reg  = TX_SETIMAGE3( data, TX_SETIMAGE3_I0_ID);
662         *(u32 *)(&DrawList[DLOffsets[i]]) = reg;
663     }
664 
665     // Since we changed it, we need to flush it
666     DCFlushRange((void *)DrawList, DRAWSIZE);
667 
668     // Set up TEX0
669     GXCallDisplayList(InitList, INITSIZE);
670 }
671 
672 /*---------------------------------------------------------------------------*
673     Name:           DrawTick
674 
675     Description:    Draw the current model once.
676 
677     Arguments:      v       view matrix
678                     m       model matrix
679 
680     Returns:        none
681  *---------------------------------------------------------------------------*/
DrawTick(void)682 static void DrawTick( void )
683 {
684     Mtx ry, rz, mv, t;
685     f32 tx, ty;
686 
687     tx = 0.0f;
688     ty = 0.0f;
689 
690     MTXRotDeg(ry, 'Y', (float)rot);
691     MTXRotDeg(rz, 'Z', (float)rot / 2.0F);
692     MTXTrans(t, tx, ty, 0);
693 
694     MTXConcat(rz, ry, mv);
695     MTXConcat(t, mv, mv);
696     MTXConcat(v, mv, mv);
697     GXLoadPosMtxImm(mv, GX_PNMTX0);
698     MTXInverse(mv, mv);
699     MTXTranspose(mv, mv);
700     GXLoadNrmMtxImm(mv, GX_PNMTX0);
701 
702     GXCallDisplayList(DrawList, DRAWSIZE);
703 }
704 
705 /*---------------------------------------------------------------------------*
706     Name:           AnimTick
707 
708     Description:    Animates the camera and object based on the joystick's
709                     state.
710 
711     Arguments:      none
712 
713     Returns:        none
714  *---------------------------------------------------------------------------*/
AnimTick(void)715 static void AnimTick ( void )
716 {
717     u16 buttons = DEMOPadGetButton(0);
718 
719     // Just simple controls right now...
720 
721     if(buttons & PAD_BUTTON_A)
722     {
723         // suspend animation
724     } else {
725 
726         rot ++;
727         if(rot > 1439)
728             rot = 0;
729     }
730 }
731 
732 /*---------------------------------------------------------------------------*
733     Name:           ParameterInit
734 
735     Description:    Initialize parameters for single frame display
736 
737     Arguments:      none
738 
739     Returns:        none
740  *---------------------------------------------------------------------------*/
ParameterInit(void)741 static void ParameterInit ( void )
742 {
743     rot = 45;
744 }
745 
746 /****************************************************************************/
747 
748 
749