1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     tex-wrap.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-wrap
15         texture wrap mode test
16  *---------------------------------------------------------------------------*/
17 
18 
19 /*---------------------------------------------------------------------------*
20    Header files
21  *---------------------------------------------------------------------------*/
22 #include <demo.h>
23 
24 /*---------------------------------------------------------------------------*
25    Macro definitions
26  *---------------------------------------------------------------------------*/
27 #define Clamp(val,min,max) \
28     ((val) = (((val)<(min)) ? (min) : ((val)>(max)) ? (max) : (val)))
29 
30 /*---------------------------------------------------------------------------*
31    Structure definitions
32  *---------------------------------------------------------------------------*/
33 // for camera
34 typedef struct
35 {
36     Vec    location;
37     Vec    up;
38     Vec    target;
39     f32    left;
40     f32    top;
41     f32    znear;
42     f32    zfar;
43 } CameraConfig;
44 
45 typedef struct
46 {
47     CameraConfig  cfg;
48     Mtx           view;
49     Mtx44         proj;
50 } MyCameraObj;
51 
52 // for entire scene control
53 typedef struct
54 {
55     MyCameraObj       cam;
56     u32               texNumber;
57     f32               tcScaleX;
58     f32               tcScaleY;
59     GXTexFilter       filterMode;
60 } MySceneCtrlObj;
61 
62 /*---------------------------------------------------------------------------*
63    Forward references
64  *---------------------------------------------------------------------------*/
65 void        main           ( void );
66 static void DrawInit       ( MySceneCtrlObj* sc );
67 static void DrawTick       ( MySceneCtrlObj* sc );
68 static void AnimTick       ( MySceneCtrlObj* sc );
69 static void DrawQuad       ( void );
70 static void SetCamera      ( MyCameraObj* cam );
71 static void PrintIntro     ( void );
72 
73 /*---------------------------------------------------------------------------*
74    Parameters
75  *---------------------------------------------------------------------------*/
76 static GXTexWrapMode WrapModeTable[] =
77 {
78     GX_REPEAT,
79     GX_CLAMP,
80     GX_MIRROR
81 };
82 
83 /*---------------------------------------------------------------------------*
84    Camera configuration
85  *---------------------------------------------------------------------------*/
86 static CameraConfig DefaultCamera =
87 {
88     { 0.0F, 0.0F, 500.0F }, // location
89     { 0.0F, 1.0F,   0.0F }, // up
90     { 0.0F, 0.0F,   0.0F }, // target
91     -320.0F, // left
92     224.0F,  // top  (note: was 240, now adjusted for over scan)
93     50.0F,   // near
94     2000.0F  // far
95 };
96 
97 /*---------------------------------------------------------------------------*
98    Global variables
99  *---------------------------------------------------------------------------*/
100 static MySceneCtrlObj   SceneCtrl;          // scene control parameters
101 static TPLPalettePtr    MyTplObj = NULL;    // texture palette
102 static u32              TexNumMax;          // number of textures
103 
104 /*---------------------------------------------------------------------------*
105    Application main loop
106  *---------------------------------------------------------------------------*/
main(void)107 void main ( void )
108 {
109     DEMOInit(NULL);  // Init the OS, game pad, graphics and video.
110 
111     DrawInit(&SceneCtrl); // Initialize vertex formats, array pointers
112                           // and default scene settings.
113 
114     PrintIntro();    // Print demo directions
115 
116     while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
117     {
118 		DEMOBeforeRender();
119         DrawTick(&SceneCtrl);    // Draw the model.
120         DEMODoneRender();
121         DEMOPadRead();           // Read controller
122         AnimTick(&SceneCtrl);    // Do animation
123     }
124 
125     OSHalt("End of demo");
126 }
127 
128 /*---------------------------------------------------------------------------*
129    Functions
130  *---------------------------------------------------------------------------*/
131 /*---------------------------------------------------------------------------*
132     Name:           DrawInit
133 
134     Description:    Initializes the vertex attribute format and sets up
135                     the array pointer for the indexed data.
136                     This function also initializes scene control parameters.
137 
138     Arguments:      sc : pointer to the structure of scene control parameters
139 
140     Returns:        none
141  *---------------------------------------------------------------------------*/
DrawInit(MySceneCtrlObj * sc)142 static void DrawInit( MySceneCtrlObj* sc )
143 {
144     //  Vertex Attribute
145     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
146     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
147 
148     //  Load TPL file
149     TPLGetPalette(&MyTplObj, "gxTests/tex-03.tpl");
150     TexNumMax = MyTplObj->numDescriptors;
151 
152     // Texture coord generation setting
153     GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_TEXMTX0);
154     GXSetNumTexGens(1);
155 
156 
157     // Default scene control parameter settings
158 
159     // camera
160     sc->cam.cfg = DefaultCamera;
161     SetCamera(&sc->cam);   // never changes in this test
162 
163     // texture number
164     sc->texNumber = 0;
165 
166     // texture coord scale and filter
167     sc->tcScaleX = 1.0F;
168     sc->tcScaleY = 1.0F;
169     sc->filterMode = GX_NEAR;
170 }
171 
172 /*---------------------------------------------------------------------------*
173     Name:           DrawTick
174 
175     Description:    Draw the model by using given scene parameters
176 
177     Arguments:      sc : pointer to the structure of scene control parameters
178 
179     Returns:        none
180  *---------------------------------------------------------------------------*/
DrawTick(MySceneCtrlObj * sc)181 static void DrawTick( MySceneCtrlObj* sc )
182 {
183     TPLDescriptorPtr tdp;
184     GXTexObj         texObj;
185     GXTexFilter      minFilter, magFilter;
186     GXBool           mipMapFlag;
187     Mtx              mt, mv;
188     s32              x, y;
189     f32              tcx, tcy;
190 
191     // use texture in Tev
192     GXSetTevOp(GX_TEVSTAGE0, GX_REPLACE);
193     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR_NULL);
194 
195     // get texture descriptor from texture palette
196     tdp = TPLGet(MyTplObj, sc->texNumber);
197 
198     // set up texture filters
199     mipMapFlag =
200         (GXBool)(( tdp->textureHeader->minLOD == tdp->textureHeader->maxLOD ) ?
201                  GX_FALSE : GX_TRUE);
202 
203     magFilter = sc->filterMode;
204     if ( mipMapFlag )
205     {
206         minFilter = ( sc->filterMode == GX_NEAR ) ?
207                     GX_NEAR_MIP_LIN : GX_LIN_MIP_LIN;
208     }
209     else
210     {
211         minFilter = sc->filterMode;
212     }
213 
214 
215     // display 3x3 panels
216     for ( y = 0 ; y < 3 ; ++y )
217     {
218         for ( x = 0 ; x < 3 ; ++x )
219         {
220             GXInitTexObj(
221                 &texObj,
222                 tdp->textureHeader->data,
223                 tdp->textureHeader->width,
224                 tdp->textureHeader->height,
225                 (GXTexFmt)tdp->textureHeader->format,
226                 WrapModeTable[x], // s
227                 WrapModeTable[y], // t
228                 mipMapFlag );     // Mipmap
229 
230             GXInitTexObjLOD(
231                 &texObj,
232                 minFilter,
233                 magFilter,
234                 tdp->textureHeader->minLOD,
235                 tdp->textureHeader->maxLOD,
236                 tdp->textureHeader->LODBias,
237                 GX_FALSE,
238                 tdp->textureHeader->edgeLODEnable,
239                 GX_ANISO_1 );
240 
241             // Load texture obj
242             GXLoadTexObj(&texObj, GX_TEXMAP0);
243 
244             // Set modelview matrix
245             MTXTrans(mt, (f32)(x-1)*180, (f32)(y-1)*136, 0.0F);
246             MTXConcat(sc->cam.view, mt, mv);
247             GXLoadPosMtxImm(mv, GX_PNMTX0);
248 
249             // Set texcoord matrix
250             tcx = (f32)(sc->tcScaleX * 2.0F + 1.0F);
251             tcy = (f32)(sc->tcScaleY * 2.0F + 1.0F);
252             MTXScale(mv, tcx, tcy, 1.0F);
253             MTXTrans(mt, -(sc->tcScaleX), -(sc->tcScaleY), 0.0F);
254             MTXConcat(mt, mv, mv);
255             GXLoadTexMtxImm(mv, GX_TEXMTX0, GX_MTX2x4);
256 
257             // Draw a textured matrix
258             DrawQuad();
259         }
260     }
261 }
262 
263 /*---------------------------------------------------------------------------*
264     Name:           AnimTick
265 
266     Description:    Changes scene parameters according to the pad status.
267 
268     Arguments:      sc : pointer to the structure of scene control parameters
269 
270     Returns:        none
271  *---------------------------------------------------------------------------*/
AnimTick(MySceneCtrlObj * sc)272 static void AnimTick( MySceneCtrlObj* sc )
273 {
274     u16 down = DEMOPadGetButtonDown(0);
275 
276     // Texture coordinate scale control
277     sc->tcScaleX += 0.01F * (f32)(DEMOPadGetStickX(0)) / 32.0F;
278     sc->tcScaleY -= 0.01F * (f32)(DEMOPadGetStickY(0)) / 32.0F;
279     Clamp(sc->tcScaleX, 0.0F, 10.0F);
280     Clamp(sc->tcScaleY, 0.0F, 10.0F);
281 
282     // Reset coordinates
283     if ( down & PAD_BUTTON_X )
284     {
285         sc->tcScaleX = sc->tcScaleY = 1.0F;
286     }
287 
288     // Change the texture
289     if ( down & PAD_BUTTON_A )
290     {
291         sc->texNumber = ( sc->texNumber + 1 ) % TexNumMax;
292     }
293 
294     // Change filter mode
295     if ( down & PAD_BUTTON_B )
296     {
297         sc->filterMode = ( sc->filterMode == GX_NEAR ) ?
298         GX_LINEAR : GX_NEAR;
299     }
300 
301 }
302 
303 /*---------------------------------------------------------------------------*
304     Name:           DrawQuad
305 
306     Description:    Draw a textured quad.
307  *---------------------------------------------------------------------------*/
DrawQuad(void)308 static void DrawQuad( void )
309 {
310     // set vertex descriptor
311     GXClearVtxDesc();
312     GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
313     GXSetVtxDesc(GX_VA_TEX0, GX_DIRECT);
314 
315     // draw the box
316     GXBegin(GX_QUADS, GX_VTXFMT0, 4);
317         GXPosition3s16(-64,  64, 0);
318         GXTexCoord2f32(0.0F, 0.0F);
319         GXPosition3s16( 64,  64, 0);
320         GXTexCoord2f32(1.0F, 0.0F);
321         GXPosition3s16( 64, -64, 0);
322         GXTexCoord2f32(1.0F, 1.0F);
323         GXPosition3s16(-64, -64, 0);
324         GXTexCoord2f32(0.0F, 1.0F);
325     GXEnd();
326 }
327 
328 /*---------------------------------------------------------------------------*
329     Name:           SetCamera
330 
331     Description:    set view matrix and load projection matrix into hardware
332 
333     Arguments:      cam : pointer to the MyCameraObj structure
334 
335     Returns:        none
336  *---------------------------------------------------------------------------*/
SetCamera(MyCameraObj * cam)337 static void SetCamera( MyCameraObj* cam )
338 {
339     MTXLookAt(
340         cam->view,
341         &cam->cfg.location,
342         &cam->cfg.up,
343         &cam->cfg.target );
344 
345     MTXOrtho(
346         cam->proj,
347         cam->cfg.top,
348         - (cam->cfg.top),
349         cam->cfg.left,
350         - (cam->cfg.left),
351         cam->cfg.znear,
352         cam->cfg.zfar );
353     GXSetProjection(cam->proj, GX_ORTHOGRAPHIC);
354 }
355 
356 /*---------------------------------------------------------------------------*
357     Name:           PrintIntro
358 
359     Description:    Prints the directions on how to use this demo.
360 
361     Arguments:      none
362 
363     Returns:        none
364  *---------------------------------------------------------------------------*/
PrintIntro(void)365 static void PrintIntro( void )
366 {
367     OSReport("\n\n");
368     OSReport("************************************************\n");
369     OSReport("tex-wrap: texture wrap mode test\n");
370     OSReport("************************************************\n");
371     OSReport("to quit hit the start button\n");
372     OSReport("\n");
373     OSReport("Main Stick : change scale of texture coordinates\n");
374     OSReport("A button   : change the texture\n");
375     OSReport("B button   : switch NEAR/LINEAR filters\n");
376     OSReport("X button   : reset texture coordinates\n");
377     OSReport("************************************************\n\n");
378 }
379 
380 /*============================================================================*/
381