1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/3D_Pol_TexSRT
3 File: main.c
4
5 Copyright 2003-2008 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 $Date:: 2008-09-18#$
14 $Rev: 8573 $
15 $Author: okubata_ryoma $
16 *---------------------------------------------------------------------------*/
17
18 //---------------------------------------------------------------------------
19 // A sample for texture SRT animation:
20 //
21 // USAGE:
22 // Up, Down, Left, Right: move the texture
23 // A, B: change the scale of the texture
24 // L, R: rotate the texture
25 //
26 // HOWTO:
27 // 1. Set up the texture matrix.
28 // 2. Specify GX_TEXGEN_TEXCOORD as a parameter of G3_TexImageParam.
29 //
30 // Notice that the translation must be multiplied by 16 in advance,
31 // or use G3_LoadTexMtxTexCoord() instead.
32 //---------------------------------------------------------------------------
33
34 #ifdef SDK_TWL
35 #include <twl.h>
36 #else
37 #include <nitro.h>
38 #endif
39 #include "DEMO.h"
40 #include "tex_4plett.h"
41
42 #ifdef SDK_TWL
TwlMain(void)43 void TwlMain(void)
44 #else
45 void NitroMain(void)
46 #endif
47 {
48 u32 myTexAddr = 0x1000; // texture image address in texture image slots
49 u32 myTexPlttAddr = 0x1000; // texture palette address in texture palette slots
50
51 fx32 texScale = FX32_ONE >> 1;
52 fx32 texX = 0;
53 fx32 texY = 0;
54 u16 texRotate = 0;
55
56 //---------------------------------------------------------------------------
57 // Initialize:
58 // They enable IRQ interrupts, initialize VRAM, and set BG #0 for 3D mode.
59 //---------------------------------------------------------------------------
60 DEMOInitCommon();
61 DEMOInitVRAM();
62 DEMOInitDisplay3D();
63
64
65 //---------------------------------------------------------------------------
66 // Download the texture images:
67 //
68 // Transfer the texture data on the main memory to the texture image slots.
69 //---------------------------------------------------------------------------
70 GX_BeginLoadTex(); // map the texture image slots onto LCDC address space
71 {
72 GX_LoadTex((void *)&tex_4plett64x64[0], // a pointer to the texture data on the main memory(4 bytes aligned)
73 myTexAddr, // an offset address in the texture image slots
74 1024 // the size of the texture(s)(in bytes)
75 );
76 }
77 GX_EndLoadTex(); // restore the texture image slots
78
79 //---------------------------------------------------------------------------
80 // Download the texture palettes:
81 //
82 // Transfer the texture palette data on the main memory to the texture palette slots.
83 //---------------------------------------------------------------------------
84 GX_BeginLoadTexPltt(); // map the texture palette slots onto LCDC address space
85 {
86 GX_LoadTexPltt((void *)&pal_4plett[0], // a pointer to the texture data on the main memory(4 bytes aligned)
87 myTexPlttAddr, // an offset address in the texture palette slots
88 8 // the size of the texture palette(s)(in bytes)
89 );
90 }
91 GX_EndLoadTexPltt(); // restore the texture palette slots
92
93 DEMOStartDisplay();
94 while (1)
95 {
96 MtxFx43 camera;
97
98 DEMOReadKey();
99 if (DEMO_IS_PRESS(PAD_KEY_UP))
100 texY += FX32_ONE >> 2;
101 if (DEMO_IS_PRESS(PAD_KEY_DOWN))
102 texY -= FX32_ONE >> 2;
103 if (DEMO_IS_PRESS(PAD_KEY_RIGHT))
104 texX -= FX32_ONE >> 2;
105 if (DEMO_IS_PRESS(PAD_KEY_LEFT))
106 texX += FX32_ONE >> 2;
107 if (DEMO_IS_PRESS(PAD_BUTTON_L))
108 texRotate += 256;
109 if (DEMO_IS_PRESS(PAD_BUTTON_R))
110 texRotate -= 256;
111 if (DEMO_IS_PRESS(PAD_BUTTON_A))
112 texScale += FX32_ONE >> 4;
113 if (DEMO_IS_PRESS(PAD_BUTTON_B))
114 texScale -= FX32_ONE >> 4;
115 if (texScale <= 0)
116 texScale = FX32_ONE >> 4;
117
118 G3X_Reset();
119
120 //---------------------------------------------------------------------------
121 // Set up a camera matrix
122 //---------------------------------------------------------------------------
123 {
124 VecFx32 Eye = { 0, 0, 3 * FX32_ONE }; // Eye position
125 VecFx32 at = { 0, 0, 0 }; // Viewpoint
126 VecFx32 vUp = { 0, FX32_ONE, 0 }; // Up
127 G3_LookAt(&Eye, &vUp, &at, &camera);
128 }
129
130 //---------------------------------------------------------------------------n
131 // Set up light colors and direction.
132 // Notice that light vector is transformed by the current vector matrix
133 // immediately after LightVector command is issued.
134 //
135 // GX_LIGHTID_0: white, downward
136 //---------------------------------------------------------------------------
137 G3_LightVector(GX_LIGHTID_0, FX16_SQRT1_3, -FX16_SQRT1_3, -FX16_SQRT1_3);
138 G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
139
140 G3_PushMtx();
141
142 //---------------------------------------------------------------------------
143 // Set up the texture matrix for SRT animation
144 //---------------------------------------------------------------------------
145 {
146 fx16 s = FX_SinIdx(texRotate);
147 fx16 c = FX_CosIdx(texRotate);
148 fx32 rScale = FX_Inv(texScale);
149
150 //---------------------------------------------------------------------------
151 // Initialize a texture matrix
152 //---------------------------------------------------------------------------
153 G3_MtxMode(GX_MTXMODE_TEXTURE);
154 G3_Identity();
155 #if 1
156 //---------------------------------------------------------------------------
157 // Scale, Rotation, and Translation on the texture matrix:
158 //
159 // Notice that 3rd row and 4th row must be multiplied by 16.
160 //---------------------------------------------------------------------------
161 G3_Translate(32 * FX32_ONE, 32 * FX32_ONE, 0); // 5. move the origin to the center of the texture
162 G3_Translate(texX, texY, 0); // 4. translate
163 G3_RotZ(s, c); // 3. rotate counterclockwise
164 G3_Scale(rScale, rScale, FX32_ONE); // 2. scale
165 G3_Translate(-32 * FX32_ONE, -32 * FX32_ONE, 0); // 1. move the center of the texture to the origin
166 {
167 MtxFx44 mtx;
168 MTX_Identity44(&mtx);
169 mtx._22 = 16 * FX32_ONE;
170 mtx._33 = 16 * FX32_ONE;
171 G3_MultMtx44(&mtx);
172 }
173 #else
174 //---------------------------------------------------------------------------
175 // CPU version
176 //---------------------------------------------------------------------------
177 {
178
179 MtxFx44 mtx, tmp;
180 MTX_Identity44(&mtx);
181
182 mtx._30 = 32 * FX32_ONE + texX;
183 mtx._31 = 32 * FX32_ONE + texY;
184
185 MTX_RotZ44(&tmp, s, c);
186 MTX_Concat44(&tmp, &mtx, &mtx);
187 MTX_ScaleApply44(&mtx, &mtx, rScale, rScale, FX32_ONE);
188
189 MTX_TransApply44(&mtx, &mtx, -32 * FX32_ONE, -32 * FX32_ONE, 0);
190
191 //---------------------------------------------------------------------------
192 // Load a texture matrix(in the case of GX_TEXGEN_TEXCOORD):
193 //
194 // Internaly, 3rd and 4th row are multiplied by 16 before
195 // the argument matrix is set to the current texture matrix.
196 //---------------------------------------------------------------------------
197 G3_LoadTexMtxTexCoord(&mtx); // Translation is internally multiplied by 16.
198 }
199 #endif
200 G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
201
202 }
203
204 G3_TexImageParam(GX_TEXFMT_PLTT4, // use 4 colors palette texture
205 GX_TEXGEN_TEXCOORD, // use normal source
206 GX_TEXSIZE_S64, // 64 pixels
207 GX_TEXSIZE_T64, // 64 pixels
208 GX_TEXREPEAT_ST, // repeat st
209 GX_TEXFLIP_NONE, // no flip
210 GX_TEXPLTTCOLOR0_USE, // use color 0 of the palette
211 myTexAddr // the offset of the texture image
212 );
213
214 G3_TexPlttBase(myTexPlttAddr, // the offset of the texture palette
215 GX_TEXFMT_PLTT4 // 4 colors palette texture
216 );
217
218 // Set the material color( diffuse, ambient , specular ) as basic white
219 DEMO_Set3DDefaultMaterial(TRUE, TRUE);
220 DEMO_Set3DDefaultShininessTable();
221
222 G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
223 GX_POLYGONMODE_MODULATE, // modulation mode
224 GX_CULL_BACK, // cull back
225 0, // polygon ID(0 - 63)
226 31, // alpha(0 - 31)
227 0 // OR of GXPolygonAttrMisc's value
228 );
229
230 G3_Begin(GX_BEGIN_QUADS);
231
232 {
233 G3_TexCoord(0, 0);
234 G3_Vtx(-FX16_ONE, FX16_ONE, 0);
235 G3_TexCoord(0, 64 * FX32_ONE);
236 G3_Vtx(-FX16_ONE, -FX16_ONE, 0);
237 G3_TexCoord(64 * FX32_ONE, 64 * FX32_ONE);
238 G3_Vtx(FX16_ONE, -FX16_ONE, 0);
239 G3_TexCoord(64 * FX32_ONE, 0);
240 G3_Vtx(FX16_ONE, FX16_ONE, 0);
241 }
242 G3_End();
243
244 G3_PopMtx(1);
245
246 // swapping the polygon list RAM, the vertex RAM, etc.
247 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
248
249 #ifdef SDK_AUTOTEST
250 // default value for testing.
251 texX = FX32_ONE >> 2 * 15;
252 texY = FX32_ONE >> 2 * 25;
253 texRotate = 256 * 8;
254 texScale = FX32_ONE - (FX32_ONE >> 4) * 3;
255
256 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
257 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
258 EXT_TestScreenShot(100, 0xB17E279F);
259 EXT_TestTickCounter();
260 #endif //SDK_AUTOTEST
261
262 OS_WaitVBlankIntr(); // Waiting the end of VBlank interrupt
263 }
264 }
265
266 //---------------------------------------------------------------------------
267 // VBlank interrupt function:
268 //
269 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
270 // OS_EnableIrqMask selects IRQ interrupts to enable, and
271 // OS_EnableIrq enables IRQ interrupts.
272 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
273 //---------------------------------------------------------------------------
VBlankIntr(void)274 void VBlankIntr(void)
275 {
276 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
277 }
278