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