1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     tg-basic.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 
16 #define WATER1_TEX_ID   3
17 
18 /*---------------------------------------------------------------------------*/
19 Mtx v, m, mv;
20 Mtx44 proj;
21 u32 i;
22 
23 u8 done;
24 
25 Vec CamLoc = {0.0F, 0.0F, 8.0F};
26 float CameraLocScale = 100.0F;
27 Vec UP = {0.0F, 1.0F, 0.0F};
28 Vec ObjPt = {0.0F, 0.0F, 0.0F};
29 
30 u8 CurrentTexGenMethod;
31 u8 CurrentCombineMethod;
32 
33 static TPLPalettePtr tpl = 0;
34 
35 /*---------------------------------------------------------------------------*/
36 void        main                ( void );
37 static void InitCamera          ( float scale );
38 static void DrawInit            ( void );
39 static void DrawTick            ( void );
40 static void InitTexGenMethod    ( void );
41 static void AnimTick            ( void );
42 
43 /*---------------------------------------------------------------------------*/
main(void)44 void main ( void )
45 {
46     DEMOInit(NULL);
47 
48     OSReport("\n\n");
49     OSReport("**********************************************\n");
50     OSReport("tg-basic: Texture Coordinate Generation demo\n");
51     OSReport("**********************************************\n");
52     OSReport("To quit hit the start button.\n");
53     OSReport("\n");
54     OSReport("Main stick rotates model.\n");
55     OSReport("A button changes the texgen method.\n");
56     OSReport("B button changes the Tev combine operation.\n");
57     OSReport("**********************************************\n");
58     OSReport("\n\n");
59 
60     //Initialize camera
61     InitCamera(100.0F);
62 
63     DrawInit();
64 
65     while(!(DEMOPadGetButton(0) & PAD_BUTTON_MENU))
66     {
67         DEMOPadRead();
68 
69         AnimTick();
70         DEMOBeforeRender();
71 
72         DrawTick();
73 
74         DEMODoneRender();
75     }
76 
77     OSHalt("End of demo");
78 }
79 
80 /*---------------------------------------------------------------------------*/
InitCamera(float scale)81 static void InitCamera ( float scale )
82 {
83     Mtx44 p;
84 
85     Vec camPt = CamLoc;
86 
87     camPt.x *= CameraLocScale; camPt.y *= CameraLocScale; camPt.z *= CameraLocScale;
88 
89     MTXFrustum(p, .24F * scale,-.24F * scale,-.32F * scale, .32F * scale, .5F * scale, 20.0F * scale);
90 
91     GXSetProjection(p, GX_PERSPECTIVE);
92 
93     MTXLookAt(v, &camPt, &UP, &ObjPt);
94 
95 }
96 
97 /*---------------------------------------------------------------------------*/
DrawInit(void)98 static void DrawInit    ( void )
99 {
100     GXTexObj to;
101     GXLightObj lo;
102     GXColor LightColor;
103 
104     GXSetCullMode(GX_CULL_NONE);
105 
106     GXClearVtxDesc();
107     GXSetVtxDesc(GX_VA_POS, GX_DIRECT);
108     GXSetVtxDesc(GX_VA_TEX0, GX_DIRECT);
109     GXSetVtxDesc(GX_VA_CLR0, GX_DIRECT);
110     GXSetVtxDesc(GX_VA_NRM, GX_DIRECT);
111 
112     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
113     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);
114     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
115     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_NRM, GX_NRM_XYZ, GX_F32, 0);
116 
117     /*
118         Init texture object from gxTextrs.tpl
119      */
120     TPLGetPalette(&tpl, "gxTextrs.tpl");
121     // Initialize a texture object to contain the correct texture
122     TPLGetGXTexObjFromPalette(tpl, &to, WATER1_TEX_ID);
123     // Load the texture object into hardware
124     GXLoadTexObj(&to, GX_TEXMAP0);
125     // Set the tev op to be GX_REPLACE
126     GXSetTevOp(GX_TEVSTAGE0, GX_REPLACE);
127     GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
128 
129     MTXLightPerspective(proj, 40.0F, 1.0F, 2.0F, 2.0F, 0.5F, 0.5F);
130 
131     GXInitLightAttn(&lo, 1.0F, 0.0F, 0.0F, 1.0F, 0.0F, 0.0F);
132     GXInitLightPos(&lo, 0.0F, 0.0F, 0.0F);
133     LightColor.r = LightColor.g = LightColor.b = LightColor.a = 255;
134     GXInitLightColor(&lo, LightColor);
135     GXLoadLightObjImm(&lo, GX_LIGHT0);
136 
137     LightColor.r = LightColor.g = LightColor.b = LightColor.a = 0;
138     GXSetNumChans(1);
139     GXSetChanAmbColor(GX_COLOR0, LightColor);
140     GXSetChanCtrl(GX_COLOR0A0, 1, GX_SRC_REG, GX_SRC_VTX, GX_LIGHT0, GX_DF_CLAMP, GX_AF_SPOT);
141 
142     InitTexGenMethod();
143 }
144 
145 /*---------------------------------------------------------------------------*/
DrawTick(void)146 static void DrawTick    ( void )
147 {
148     GXBegin(GX_QUADS, GX_VTXFMT0, 4);
149 
150         GXPosition3f32(100.0F, 100.0F, 0.0F);
151         GXNormal3f32(0.0F, 0.0F, 1.0F);
152         GXColor4u8(255, 64, 128, 255);
153         GXTexCoord2f32(1.0F, 0.0F);
154 
155         GXPosition3f32(100.0F, -100.0F, 0.0F);
156         GXNormal3f32(0.0F, 0.0F, 1.0F);
157         GXColor4u8(255, 255, 128, 255);
158         GXTexCoord2f32(1.0F, 1.0F);
159 
160         GXPosition3f32(-100.0F, -100.0F, 0.0F);
161         GXNormal3f32(0.0F, 0.0F, 1.0F);
162         GXColor4u8(64, 255, 128, 64);
163         GXTexCoord2f32(0.0F, 1.0F);
164 
165         GXPosition3f32(-100.0F, 100.0F, 0.0F);
166         GXNormal3f32(0.0F, 0.0F, 1.0F);
167         GXColor4u8(64, 64, 128, 64);
168         GXTexCoord2f32(0.0F, 0.0F);
169 
170     GXEnd();
171 }
172 
173 /*---------------------------------------------------------------------------*/
InitTexGenMethod(void)174 static void InitTexGenMethod ( void )
175 {
176     switch(CurrentTexGenMethod)
177     {
178         case 0:
179             GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_TEXMTX0);
180             OSReport("TexGen - Texture coord translate in T\n\n");
181             break;
182         case 1:
183             GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_TEXMTX0);
184             OSReport("TexGen - Texture coord rotate around origin\n\n");
185             break;
186         case 2:
187             GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX3x4, GX_TG_POS, GX_TEXMTX0);
188             OSReport("TexGen - Texture projection\n\n");
189             break;
190         case 3:
191             GXSetTexCoordGen(GX_TEXCOORD0, GX_TG_SRTG, GX_TG_COLOR0, GX_TEXMTX0);
192             OSReport("TexGen - S = R, T = G\n\n");
193             break;
194     }
195     GXSetNumTexGens(1);
196 }
197 
198 /*---------------------------------------------------------------------------*/
AnimTick(void)199 static void AnimTick    ( void )
200 {
201     u16 buttons = DEMOPadGetButtonDown(0);
202 
203     MTXRotDeg(m, 'Y', i);
204     MTXConcat(v, m, mv);
205     GXLoadPosMtxImm(mv, GX_PNMTX0);
206     MTXInverse(mv, m);
207     MTXTranspose(m, m);
208     GXLoadNrmMtxImm(m, GX_PNMTX0);
209 
210     if(buttons & PAD_BUTTON_A)
211     {
212         CurrentTexGenMethod ++;
213         if(CurrentTexGenMethod > 3) CurrentTexGenMethod = 0;
214 
215         InitTexGenMethod();
216     }
217 
218     if(buttons & PAD_BUTTON_B)
219     {
220         CurrentCombineMethod ++;
221         if(CurrentCombineMethod > 1) CurrentCombineMethod = 0;
222 
223         switch(CurrentCombineMethod)
224         {
225             case 0:
226                 GXSetTevOp(GX_TEVSTAGE0, GX_REPLACE);
227                 OSReport("TevOp - REPLACE\n\n");
228                 break;
229             case 1:
230                 GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
231                 OSReport("TevOp - PASSCLR\n\n");
232                 break;
233         }
234     }
235 
236     switch(CurrentTexGenMethod)
237     {
238         case 0:
239             MTXTrans(m, 0.0F, ((float)i)/100.0F, 0.0F);
240             GXLoadTexMtxImm(m, GX_TEXMTX0, GX_MTX2x4);
241             break;
242         case 1:
243             MTXRotDeg(m, 'Z', i);
244             GXLoadTexMtxImm(m, GX_TEXMTX0, GX_MTX2x4);
245             break;
246         case 2:
247             MTXConcat((MtxPtr)proj, mv, mv);
248             GXLoadTexMtxImm(mv, GX_TEXMTX0, GX_MTX3x4);
249             break;
250     }
251 
252     i++;
253 }
254 
255 /*============================================================================*/
256 
257