1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/3D_Pol_Tex4_Plett
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 that use a compressed 4x4 texel texture:
20 //
21 // Note that the address of texture indices is decided by that of a corresponding texture image.
22 //
23 // HOWTO:
24 // 1. Load texture images and texture indices to the texture image slots by
25 // GX_BeginLoadTex(), GX_LoadTex(), and GX_EndLoadTex().
26 // 2. Load texture palettes to the texture palette slots by
27 // GX_BeginLoadTexPltt(), GX_LoadTexPltt(), GX_EndLoadTexPltt().
28 // 3. Specify the texture to use and etc. by G3_TexImageParam(GX_TEXFMT_COMP4x4, ...).
29 // 4. Specify the palette to use by G3_TexPlttBase(..., GX_TEXFMT_COMP4x4).
30 // 5. The texcoords are specified by G3_TexCoord().
31 //---------------------------------------------------------------------------
32
33 #ifdef SDK_TWL
34 #include <twl.h>
35 #else
36 #include <nitro.h>
37 #endif
38 #include "DEMO.h"
39 #include "tex_4x4.h"
40
41 s16 gCubeGeometry[3 * 8] = {
42 FX16_ONE, FX16_ONE, FX16_ONE,
43 FX16_ONE, FX16_ONE, -FX16_ONE,
44 FX16_ONE, -FX16_ONE, FX16_ONE,
45 FX16_ONE, -FX16_ONE, -FX16_ONE,
46 -FX16_ONE, FX16_ONE, FX16_ONE,
47 -FX16_ONE, FX16_ONE, -FX16_ONE,
48 -FX16_ONE, -FX16_ONE, FX16_ONE,
49 -FX16_ONE, -FX16_ONE, -FX16_ONE
50 };
51
52 VecFx10 gCubeNormal[6] = {
53 GX_VECFX10(0, 0, FX32_ONE - 1),
54 GX_VECFX10(0, FX32_ONE - 1, 0),
55 GX_VECFX10(FX32_ONE - 1, 0, 0),
56 GX_VECFX10(0, 0, -FX32_ONE + 1),
57 GX_VECFX10(0, -FX32_ONE + 1, 0),
58 GX_VECFX10(-FX32_ONE + 1, 0, 0)
59 };
60
61 GXSt gCubeTexCoord[] = {
62 GX_ST(0, 0),
63 GX_ST(0, 64 * FX32_ONE),
64 GX_ST(64 * FX32_ONE, 0),
65 GX_ST(64 * FX32_ONE, 64 * FX32_ONE)
66 };
67
68
vtx(int idx)69 inline void vtx(int idx)
70 {
71 G3_Vtx(gCubeGeometry[idx * 3], gCubeGeometry[idx * 3 + 1], gCubeGeometry[idx * 3 + 2]);
72 }
73
normal(int idx)74 inline void normal(int idx)
75 {
76 G3_Direct1(G3OP_NORMAL, gCubeNormal[idx]);
77 // use G3_Normal(x, y, z) if not packed
78 }
79
tex_coord(int idx)80 inline void tex_coord(int idx)
81 {
82 G3_Direct1(G3OP_TEXCOORD, gCubeTexCoord[idx]);
83 // use G3_TexCoord if not packed yet
84 }
85
86 #ifdef SDK_TWL
TwlMain(void)87 void TwlMain(void)
88 #else
89 void NitroMain(void)
90 #endif
91 {
92 u16 Rotate = 0; // for rotating cubes(0-65535)
93 u32 myTexAddr = 0x01000; // a texture image at 0x1000 of the texture image slots
94 u32 myTexIdxAddr = 0x20000 + (myTexAddr >> 1); // the address of texture indices is fixed according to the texture image address
95 u32 myTexPlttAddr = 0x01000; // a texture palette at 0x1000 of the texture palette slots
96
97 //---------------------------------------------------------------------------
98 // Initialization:
99 // They set up interrupt, VRAM and BG0 as 3D mode
100 //---------------------------------------------------------------------------
101 DEMOInitCommon();
102 DEMOInitVRAM();
103 DEMOInitDisplay3D();
104
105 // Alter VRAM bank configuration because we use texture slot 0-1.
106 GX_SetBankForTex(GX_VRAM_TEX_01_AB);
107
108 //---------------------------------------------------------------------------
109 // Download the texture images:
110 //
111 // Transfer the texture data on the main memory to the texture image slots.
112 //---------------------------------------------------------------------------
113 GX_BeginLoadTex(); // map the texture image slots onto LCDC address space
114 {
115 GX_LoadTex((void *)&tex_4x4_64x64[0], // a pointer to the texture data on the main memory(4 bytes aligned)
116 myTexAddr, // an offset address in the texture image slots
117 1024 // the size of the texture(s)(in bytes)
118 );
119 GX_LoadTex((void *)&index_4x4index[0], // a pointer to the texture index data on the main memory(4 bytes aligned)
120 myTexIdxAddr, // an offset address in the texture image slots
121 512 // the size of the indices(in bytes)
122 );
123 }
124 GX_EndLoadTex(); // restore the texture image slots
125
126 //---------------------------------------------------------------------------
127 // Download the texture palettes:
128 //
129 // Transfer the texture palette data on the main memory to the texture palette slots.
130 //---------------------------------------------------------------------------
131 GX_BeginLoadTexPltt(); // map the texture palette slots onto LCDC address space
132 {
133 GX_LoadTexPltt((void *)&pal_4x4plett[0], // a pointer to the texture data on the main memory(4 bytes aligned)
134 myTexPlttAddr, // an offset address in the texture palette slots
135 243 * 2 * 2); // the size of the texture palette(s)(in bytes)
136 }
137 GX_EndLoadTexPltt(); // restore the texture palette slots
138
139 DEMOStartDisplay();
140 while (1)
141 {
142 G3X_Reset();
143 Rotate += 256;
144
145 //---------------------------------------------------------------------------
146 // Set up a camera matrix
147 //---------------------------------------------------------------------------
148 {
149 VecFx32 Eye = { 0, 0, FX32_ONE }; // Eye position
150 VecFx32 at = { 0, 0, 0 }; // Viewpoint
151 VecFx32 vUp = { 0, FX32_ONE, 0 }; // Up
152
153 G3_LookAt(&Eye, &vUp, &at, NULL);
154 }
155
156 //---------------------------------------------------------------------------
157 // Set up light colors and direction.
158 // Notice that light vector is transformed by the current vector matrix
159 // immediately after LightVector command is issued.
160 //
161 // GX_LIGHTID_0: white, downward
162 //---------------------------------------------------------------------------
163 G3_LightVector(GX_LIGHTID_0, FX16_SQRT1_3, -FX16_SQRT1_3, -FX16_SQRT1_3);
164 G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
165
166 G3_PushMtx();
167
168 {
169 fx16 s = FX_SinIdx(Rotate);
170 fx16 c = FX_CosIdx(Rotate);
171
172 G3_Translate(0, 0, -5 * FX32_ONE);
173
174 G3_RotX(s, c);
175 G3_RotY(s, c);
176 G3_RotZ(s, c);
177 }
178
179 {
180 G3_MtxMode(GX_MTXMODE_TEXTURE);
181 G3_Identity();
182 // Use an identity matrix for the texture matrix for simplicity
183 G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
184 }
185
186 // Set the material color( diffuse, ambient , specular ) as basic white
187 DEMO_Set3DDefaultMaterial(TRUE, TRUE);
188 DEMO_Set3DDefaultShininessTable();
189
190 G3_TexImageParam(GX_TEXFMT_COMP4x4, // use compressed 4x4 texel texture
191 GX_TEXGEN_TEXCOORD, // use texcoord
192 GX_TEXSIZE_S64, // 64 pixels
193 GX_TEXSIZE_T64, // 64 pixels
194 GX_TEXREPEAT_NONE, // no repeat
195 GX_TEXFLIP_NONE, // no flip
196 GX_TEXPLTTCOLOR0_USE, // use color 0 of the palette
197 myTexAddr // the offset of the texture image
198 );
199
200 G3_TexPlttBase(myTexPlttAddr, // the offset of the texture palette
201 GX_TEXFMT_COMP4x4 // compressed 4x4 texel texture
202 );
203
204 G3_PolygonAttr(GX_LIGHTMASK_0, // Light #0 is on
205 GX_POLYGONMODE_MODULATE, // modulation mode
206 GX_CULL_NONE, // cull none
207 0, // polygon ID(0 - 63)
208 31, // alpha(0 - 31)
209 0 // OR of GXPolygonAttrMisc's value
210 );
211
212 G3_Begin(GX_BEGIN_QUADS);
213
214 {
215 tex_coord(1);
216 normal(0);
217 vtx(2);
218 tex_coord(0);
219 normal(0);
220 vtx(0);
221 tex_coord(2);
222 normal(0);
223 vtx(4);
224 tex_coord(3);
225 normal(0);
226 vtx(6);
227
228 tex_coord(1);
229 normal(3);
230 vtx(7);
231 tex_coord(0);
232 normal(3);
233 vtx(5);
234 tex_coord(2);
235 normal(3);
236 vtx(1);
237 tex_coord(3);
238 normal(3);
239 vtx(3);
240
241 tex_coord(1);
242 normal(5);
243 vtx(6);
244 tex_coord(0);
245 normal(5);
246 vtx(4);
247 tex_coord(2);
248 normal(5);
249 vtx(5);
250 tex_coord(3);
251 normal(5);
252 vtx(7);
253
254 tex_coord(1);
255 normal(2);
256 vtx(3);
257 tex_coord(0);
258 normal(2);
259 vtx(1);
260 tex_coord(2);
261 normal(2);
262 vtx(0);
263 tex_coord(3);
264 normal(2);
265 vtx(2);
266
267 tex_coord(1);
268 normal(1);
269 vtx(5);
270 tex_coord(0);
271 normal(1);
272 vtx(4);
273 tex_coord(2);
274 normal(1);
275 vtx(0);
276 tex_coord(3);
277 normal(1);
278 vtx(1);
279
280 tex_coord(1);
281 normal(4);
282 vtx(6);
283 tex_coord(0);
284 normal(4);
285 vtx(7);
286 tex_coord(2);
287 normal(4);
288 vtx(3);
289 tex_coord(3);
290 normal(4);
291 vtx(2);
292 }
293 G3_End();
294
295 G3_PopMtx(1);
296
297 // swapping the polygon list RAM, the vertex RAM, etc.
298 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
299
300 #ifdef SDK_AUTOTEST
301 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
302 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
303 EXT_TestScreenShot(100, 0xD4651E9D);
304 EXT_TestTickCounter();
305 #endif //SDK_AUTOTEST
306
307 OS_WaitVBlankIntr(); // Waiting the end of VBlank interrupt
308 }
309 }
310
311 //---------------------------------------------------------------------------
312 // VBlank interrupt function:
313 //
314 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
315 // OS_EnableIrqMask selects IRQ interrupts to enable, and
316 // OS_EnableIrq enables IRQ interrupts.
317 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
318 //---------------------------------------------------------------------------
VBlankIntr(void)319 void VBlankIntr(void)
320 {
321 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // checking VBlank interrupt
322 }
323