1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/3D_Pol_Vertex_Source
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-17#$
14 $Rev: 8556 $
15 $Author: okubata_ryoma $
16 *---------------------------------------------------------------------------*/
17
18 //---------------------------------------------------------------------------
19 // A sample that calls G3_TexImageParam() with GX_TEXGEN_VERTEX mode.
20 //
21 // This sample displays a cube with a constant viewpoint texture
22 //---------------------------------------------------------------------------
23
24
25 #ifdef SDK_TWL
26 #include <twl.h>
27 #else
28 #include <nitro.h>
29 #endif
30 #include "DEMO.h"
31 #include "data.h"
32
33 //---------------------------------------------------------------------------
34 // Summary:
35 // Cube model data
36 //---------------------------------------------------------------------------
37 /* Texture matrix */
38 static MtxFx44 s_TextureMtx = {
39 32 * FX32_ONE, 0, 0, 0,
40 0, -32 * FX32_ONE, 0, 0,
41 0, 0, FX32_ONE, 0,
42 0, 0, 0, FX32_ONE
43 };
44 /* Vertex data */
45 static s16 s_Vertex[3 * 8] = {
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 -FX16_ONE, FX16_ONE, FX16_ONE,
51 -FX16_ONE, FX16_ONE, -FX16_ONE,
52 -FX16_ONE, -FX16_ONE, FX16_ONE,
53 -FX16_ONE, -FX16_ONE, -FX16_ONE
54 };
55 /* Normal data */
56 static VecFx10 s_Normal[8] = {
57 GX_VECFX10(FX32_SQRT1_3, FX32_SQRT1_3, FX32_SQRT1_3),
58 GX_VECFX10(FX32_SQRT1_3, FX32_SQRT1_3, -FX32_SQRT1_3),
59 GX_VECFX10(FX32_SQRT1_3, -FX32_SQRT1_3, FX32_SQRT1_3),
60 GX_VECFX10(FX32_SQRT1_3, -FX32_SQRT1_3, -FX32_SQRT1_3),
61 GX_VECFX10(-FX32_SQRT1_3, FX32_SQRT1_3, FX32_SQRT1_3),
62 GX_VECFX10(-FX32_SQRT1_3, FX32_SQRT1_3, -FX32_SQRT1_3),
63 GX_VECFX10(-FX32_SQRT1_3, -FX32_SQRT1_3, FX32_SQRT1_3),
64 GX_VECFX10(-FX32_SQRT1_3, -FX32_SQRT1_3, -FX32_SQRT1_3)
65 };
66
67 //---------------------------------------------------------------------------
68 // Summary:
69 // Set vertex coordinates
70 // Description:
71 // Use specified vertex data to set vertex coordinates
72 // Input:
73 // idx: Vertex data ID
74 //---------------------------------------------------------------------------
Vertex(int idx)75 inline void Vertex(int idx)
76 {
77 G3_Vtx(s_Vertex[idx * 3], s_Vertex[idx * 3 + 1], s_Vertex[idx * 3 + 2]);
78 }
79
80 //---------------------------------------------------------------------------
81 // Summary:
82 // �@�@ Set normals
83 // Description:
84 // �@�@
85 // Input:
86 // idx: Normal data ID
87 //---------------------------------------------------------------------------
Normal(int idx)88 inline void Normal(int idx)
89 {
90 G3_Direct1(G3OP_NORMAL, s_Normal[idx]);
91 }
92
93 //---------------------------------------------------------------------------
94 // Summary:
95 // Generate a cube surface
96 // Description:
97 // Generate one cube surface
98 // Input:
99 // idx0 - i_id3 : Structural vertex data ID
100 //---------------------------------------------------------------------------
Quad(int idx0,int idx1,int idx2,int idx3)101 static void Quad(int idx0, int idx1, int idx2, int idx3)
102 {
103 Normal(idx0);
104 Vertex(idx0);
105 Normal(idx1);
106 Vertex(idx1);
107 Normal(idx2);
108 Vertex(idx2);
109 Normal(idx3);
110 Vertex(idx3);
111 }
112
113 //---------------------------------------------------------------------------
114 // Summary:
115 // Cube rendering (w/o normals)
116 // Description:
117 //
118 //---------------------------------------------------------------------------
DrawCube(void)119 static void DrawCube(void)
120 {
121 G3_Begin(GX_BEGIN_QUADS); // Beginning of vertex list (draw with quadrilateral polygons)
122 {
123 Quad(2, 0, 4, 6);
124 Quad(7, 5, 1, 3);
125 Quad(6, 4, 5, 7);
126 Quad(3, 1, 0, 2);
127 Quad(5, 4, 0, 1);
128 Quad(6, 7, 3, 2);
129 }
130 G3_End(); // End vertex list
131 }
132
133
134 //---------------------------------------------------------------------------
135 // Summary:
136 // V-Blank interrupt process
137 // Description:
138 // Enables a flag that confirms that a V-Blank interrupt has been performed
139 //
140 // The following steps will be performed during common initialization (DEMOInitCommon), causing this function to be called during V-Blanks
141 // * Select IRQ interrupt (OS_SetIrqMask)
142 // * Register this function, which performs IRQ interrupts (OS_SetIRQFunction)
143 // * Enable IRQ interrupts (OS_EnableIrq)
144 //
145 //---------------------------------------------------------------------------
VBlankIntr(void)146 void VBlankIntr(void)
147 {
148 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // Checking V-Blank interrupt
149 }
150
151 //---------------------------------------------------------------------------
152 // Summary:
153 // Setting GX_TEXGEN_VERTEX in G3_TexImageParam (Fixed eye point texture expression)
154 // Description:
155 // Sets GX_TEXGEN_VERTEX with G3_TexImageParam and pastes a texture onto a cube.
156 // The result is that the texture applied to the cube appears fixed when viewed from the viewpoint, despite the rotation of the cube.
157 //
158 //
159 // Note that the vertex vectors sent to the texture matrix are local.
160 // Therefore, you must increase the appropriate rotation matrixes ahead of time.
161 //---------------------------------------------------------------------------
162 #ifdef SDK_TWL
TwlMain(void)163 void TwlMain(void)
164 #else
165 void NitroMain(void)
166 #endif
167 {
168 u32 myTexAddr = 0x1000; // Slot address of the texture image
169 u32 myTexPlttAddr = 0x1000; // Texture palette address, slot address
170 u16 Rotate = 0;
171
172 //---------------------
173 // Initialize (enable IRQ, initialize VRAM, use BG0 in 3D mode)
174 //---------------------
175 DEMOInitCommon();
176 DEMOInitVRAM();
177 DEMOInitDisplay3D();
178
179 /* Load the texture image to the texture image slot */
180 GX_BeginLoadTex(); // Map the bank allocated to the slot to LCDC memory.
181 {
182 GX_LoadTex((void *)&tex_4plett64x64[0], // Load source pointer
183 myTexAddr, // Address of the slot that is the load destination
184 1024); // Load size
185 }
186 GX_EndLoadTex(); // Restore the slot mapped to LCDC to its original state
187
188 /* Load to the texture palette slot of the texture palette */
189 GX_BeginLoadTexPltt(); // Map the bank allocated to the slot to LCDC memory.
190 {
191 GX_LoadTexPltt((void *)&pal_4plett[0], // Load source pointer
192 myTexPlttAddr, // Load destination palette slot address
193 8); // Load size
194 }
195 GX_EndLoadTexPltt(); // Restore the slot mapped to LCDC to its original state
196
197 DEMOStartDisplay();
198
199 //---------------------
200 // Main loop
201 //---------------------
202 while (1)
203 {
204 MtxFx43 camera;
205
206 G3X_Reset();
207 Rotate += 256;
208
209 /* Camera configuration */
210 {
211 VecFx32 Eye = { 0, 0, 10 * FX32_ONE }; // Eye position
212 VecFx32 at = { 0, 0, 0 }; // Viewpoint
213 VecFx32 vUp = { 0, FX32_ONE, 0 }; // Up
214 G3_LookAt(&Eye, &vUp, &at, &camera);
215 }
216
217 /* Light settings
218 (Note that immediately after the LightVector command, the light matrix will be transformed by the current matrix)
219 */
220 {
221 G3_LightVector(GX_LIGHTID_0, 0, -FX32_ONE + 1, 0);
222 G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
223 }
224
225 G3_PushMtx();
226
227 /* Calculate the cube rotation and set its position */
228 {
229 fx16 s = FX_SinIdx(Rotate);
230 fx16 c = FX_CosIdx(Rotate);
231
232 G3_Scale(FX32_ONE * 3, FX32_ONE * 3, FX32_ONE * 3);
233 G3_RotZ(s, c);
234 G3_RotY(s, c);
235 G3_RotX(s, c);
236 }
237
238 /* Cube rendering configuration */
239 {
240 fx16 s = FX_SinIdx(Rotate);
241 fx16 c = FX_CosIdx(Rotate);
242
243 /* Setting for environment mapping onto cube
244 The argument matrix is scaled to (16,16,16) before the current texture matrix is configured
245
246 If G3_MtxMode(GX_MTXMODE_TEXTURE) or G3_LoadMtx44 are used, the matrix must scale itself
247 */
248
249 // Set the texture matrix by setting the matrix to texture mode
250 G3_LoadTexMtxEnv(&s_TextureMtx);
251
252 /* If you increase the camera matrixes here, you can see the texture with the same method.
253 Normally, this matrix specifies the direction of ambient light. */
254 // Multiply the current matrix with a 3x3 matrix
255 G3_MultMtx33((MtxFx33 *)&camera); // Multiply the current matrix by the camera matrix
256
257 G3_RotZ(s, c);
258 G3_RotY(s, c);
259 G3_RotX(s, c);
260
261 /* Configure the texture to be applied to the cube */
262 // The matrix is set to Position-Vector simultaneous set mode
263 G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
264
265 // Texture coordinate settings
266 G3_TexCoord(32 * FX32_ONE, 32 * FX32_ONE); // Specify center of 64x64 texture
267 }
268
269 // Specify texture parameters
270 G3_TexImageParam(GX_TEXFMT_PLTT4, // 4-color palette texture
271 GX_TEXGEN_VERTEX, // Vertex source
272 GX_TEXSIZE_S64, // 64 texture s size
273 GX_TEXSIZE_T64, // 64 texture t size
274 GX_TEXREPEAT_NONE, // No repeat
275 GX_TEXFLIP_NONE, // No flip
276 GX_TEXPLTTCOLOR0_USE, // Enable palette color 0 set value
277 myTexAddr); // Texture address
278
279 // Set the texture palette address
280 G3_TexPlttBase(myTexPlttAddr, // Texture palette address
281 GX_TEXFMT_PLTT4); // Apply to 4-color palette texture
282
283 // Set material's diffuse reflection color and ambient reflection color.
284 G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31), // Diffuse reflection color
285 GX_RGB(16, 16, 16), // Ambient reflection color
286 TRUE); // Set diffuse reflection color as vertex color
287
288 // Configure the specular color and the emission color of the material
289 G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16), // Specular reflection color
290 GX_RGB(0, 0, 0), // The emission color
291 FALSE); // Specular reflection not used
292
293 // Polygon attribute settings
294 G3_PolygonAttr(GX_LIGHTMASK_0, // Reflect light 0
295 GX_POLYGONMODE_MODULATE, // Modulation mode
296 GX_CULL_BACK, // Perform backface culling
297 0, // Polygon ID 0
298 31, // Alpha value
299 0);
300
301 DrawCube(); // Draw cube
302
303 G3_PopMtx(1);
304
305 // Swapping the polygon list RAM, the vertex RAM, etc.
306 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
307 /* Waiting for the V-Blank */
308 OS_WaitVBlankIntr();
309
310 #ifdef SDK_AUTOTEST // Code for auto-test
311 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
312 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
313 EXT_TestScreenShot(100, 0x7E85CB3E);
314 EXT_TestTickCounter();
315 #endif //SDK_AUTOTEST
316 }
317 }
318