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