1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/3D_PolAttr_FARClip
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 sets GX_POLYGON_ATTR_MISC_FAR_CLIPPING in G3_PolygonAttr
20 //
21 // This sample displays two cubes.
22 // The left one has normal settings and the right one uses far clipping.
23 // If a polygon crosses the far face, the right one is clipped.
24 //
25 // USAGE:
26 // UP, DOWN : Move z position of the object
27 // START : Reset position of the object
28 //
29 //---------------------------------------------------------------------------
30
31 #ifdef SDK_TWL
32 #include <twl.h>
33 #else
34 #include <nitro.h>
35 #endif
36 #include "DEMO.h"
37 #include "data.h"
38
39 //---------------------------------------------------------------------------
40 // Summary:
41 // Cube model data
42 //---------------------------------------------------------------------------
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
56 /* Normal data */
57 static VecFx10 s_Normal[6] = {
58 GX_VECFX10(0, 0, FX32_ONE - 1),
59 GX_VECFX10(0, FX32_ONE - 1, 0),
60 GX_VECFX10(FX32_ONE - 1, 0, 0),
61 GX_VECFX10(0, 0, -FX32_ONE + 1),
62 GX_VECFX10(0, -FX32_ONE + 1, 0),
63 GX_VECFX10(-FX32_ONE + 1, 0, 0)
64 };
65
66 /* Texture coordinates */
67 static GXSt s_TextureCoord[] = {
68 GX_ST(0, 0),
69 GX_ST(0, 64 * FX32_ONE),
70 GX_ST(64 * FX32_ONE, 0),
71 GX_ST(64 * FX32_ONE, 64 * FX32_ONE)
72 };
73
74 //---------------------------------------------------------------------------
75 // Summary:
76 // Set vertex coordinates
77 // Description:
78 // Use specified vertex data to set vertex coordinates
79 // Input:
80 // i_idx: Vertex data ID
81 //---------------------------------------------------------------------------
Vertex(int i_idx)82 inline void Vertex(int i_idx)
83 {
84 G3_Vtx(s_Vertex[i_idx * 3], // Set vertex coordinates
85 s_Vertex[i_idx * 3 + 1], s_Vertex[i_idx * 3 + 2]);
86 }
87
88 //---------------------------------------------------------------------------
89 // Summary:
90 // Set normals
91 // Description:
92 //
93 // Input:
94 // i_idx: Normal data ID
95 //---------------------------------------------------------------------------
Normal(int i_idx)96 inline void Normal(int i_idx)
97 {
98 G3_Direct1(G3OP_NORMAL, s_Normal[i_idx]);
99 }
100
101 //---------------------------------------------------------------------------
102 // Summary:
103 // Texture coordinate settings
104 // Description:
105 //
106 // Input:
107 // i_idx: Texture data ID
108 //---------------------------------------------------------------------------
TextureCoord(int i_idx)109 inline void TextureCoord(int i_idx)
110 {
111 G3_Direct1(G3OP_TEXCOORD, s_TextureCoord[i_idx]);
112 }
113
114 //---------------------------------------------------------------------------
115 // Summary:
116 // Draw cube
117 //---------------------------------------------------------------------------
DrawCube(void)118 static void DrawCube(void)
119 {
120 G3_Begin(GX_BEGIN_QUADS); // Begin vertex list (draw with quadrilateral polygons)
121 {
122 TextureCoord(1);
123 Normal(0);
124 Vertex(2);
125 TextureCoord(0);
126 Normal(0);
127 Vertex(0);
128 TextureCoord(2);
129 Normal(0);
130 Vertex(4);
131 TextureCoord(3);
132 Normal(0);
133 Vertex(6);
134
135 TextureCoord(1);
136 Normal(3);
137 Vertex(7);
138 TextureCoord(0);
139 Normal(3);
140 Vertex(5);
141 TextureCoord(2);
142 Normal(3);
143 Vertex(1);
144 TextureCoord(3);
145 Normal(3);
146 Vertex(3);
147
148 TextureCoord(1);
149 Normal(5);
150 Vertex(6);
151 TextureCoord(0);
152 Normal(5);
153 Vertex(4);
154 TextureCoord(2);
155 Normal(5);
156 Vertex(5);
157 TextureCoord(3);
158 Normal(5);
159 Vertex(7);
160
161 TextureCoord(1);
162 Normal(2);
163 Vertex(3);
164 TextureCoord(0);
165 Normal(2);
166 Vertex(1);
167 TextureCoord(2);
168 Normal(2);
169 Vertex(0);
170 TextureCoord(3);
171 Normal(2);
172 Vertex(2);
173
174 TextureCoord(1);
175 Normal(1);
176 Vertex(5);
177 TextureCoord(0);
178 Normal(1);
179 Vertex(4);
180 TextureCoord(2);
181 Normal(1);
182 Vertex(0);
183 TextureCoord(3);
184 Normal(1);
185 Vertex(1);
186
187 TextureCoord(1);
188 Normal(4);
189 Vertex(6);
190 TextureCoord(0);
191 Normal(4);
192 Vertex(7);
193 TextureCoord(2);
194 Normal(4);
195 Vertex(3);
196 TextureCoord(3);
197 Normal(4);
198 Vertex(2);
199 }
200
201 G3_End(); // End vertex list
202 }
203
204
205 //---------------------------------------------------------------------------
206 // Summary:
207 // V-Blank interrupt process
208 // Description:
209 // Enables a flag that confirms that a V-Blank interrupt has been performed
210 //
211 // The following steps will be performed during common initialization (DEMOInitCommon), causing this function to be called during V-Blanks
212 // * Select IRQ interrupt (OS_SetIrqMask)
213 // * Register this function, which performs IRQ interrupts (OS_SetIRQFunction)
214 // * Enable IRQ interrupts (OS_EnableIrq)
215 //
216 //---------------------------------------------------------------------------
VBlankIntr(void)217 void VBlankIntr(void)
218 {
219 OS_SetIrqCheckFlag(OS_IE_V_BLANK); // Sets a V-Blank interrupt confirmation flag
220 }
221
222 //---------------------------------------------------------------------------
223 // Summary:
224 // Clips polygons that intersect the far plane defined in G3_PolygonAttr
225 // Description:
226 // Far-plane clipping is not applied to the cube displayed on the left, but it is applied to the cube displayed on the right.
227 // The far value of the view volume is significantly smaller than the value normally set for the view volume.
228 // This is so that the programmer can watch the clipping process.
229 //
230 // Controls:
231 // UP, DOWN: Manipulate the object's z coordinate position
232 // START: Return to initial position
233 //---------------------------------------------------------------------------
234 #ifdef SDK_TWL
TwlMain(void)235 void TwlMain(void)
236 #else
237 void NitroMain(void)
238 #endif
239 {
240 int pos_z = -5;
241 u16 rotate = 0;
242 u32 texAddr = 0x01000; // Slot address of the texture image
243
244 //---------------------
245 // Initialize (enable IRQ, initialize VRAM, use BG0 in 3D mode)
246 //---------------------
247 DEMOInitCommon();
248 DEMOInitVRAM();
249 DEMOInitDisplay3D();
250
251 /* Load the texture image to the texture image slot */
252 GX_BeginLoadTex(); // Map the bank allocated to the slot to LCDC memory
253 {
254 GX_LoadTex((void *)&tex_32768_64x64[0], // Load source pointer
255 texAddr, // Load destination slot address
256 8192); // Load size
257 }
258 GX_EndLoadTex(); // Restore the slot mapped to LCDC to its original state
259
260 /* Perspective projection settings */
261 {
262 fx32 right = FX32_ONE;
263 fx32 top = FX32_ONE * 3 / 4;
264 fx32 near = FX32_ONE;
265 fx32 far = FX32_ONE * 6;
266 // Perspective projection settings
267 G3_Perspective(FX32_SIN30, // Sine FOVY
268 FX32_COS30, // Cosine FOVY
269 FX32_ONE * 4 / 3, // Aspect
270 near, // Near
271 far, // Far
272 NULL);
273
274 G3_StoreMtx(0); // Save the matrix in stack number 0
275 }
276
277 DEMOStartDisplay();
278
279 //---------------------
280 // Main loop
281 //---------------------
282 while (1)
283 {
284 G3X_Reset();
285 rotate += 256;
286
287 /* Processes pad input */
288 DEMOReadKey();
289
290 if (DEMO_IS_TRIG(PAD_KEY_DOWN))
291 {
292 if (++pos_z > -3)
293 {
294 pos_z = -3;
295 }
296 OS_Printf("Pos_Z=%d\n", pos_z);
297 }
298 else if (DEMO_IS_TRIG(PAD_KEY_UP))
299 {
300 if (--pos_z < -7)
301 {
302 pos_z = -7;
303 }
304 OS_Printf("Pos_Z=%d\n", pos_z);
305 }
306 if (DEMO_IS_TRIG(PAD_BUTTON_START))
307 {
308 pos_z = -5;
309 OS_Printf("Pos_Z=%d\n", pos_z);
310 }
311
312 /* Camera configuration */
313 {
314 VecFx32 Eye = { 0, 0, FX32_ONE }; // Eye point position
315 VecFx32 at = { 0, 0, 0 }; // Viewpoint
316 VecFx32 vUp = { 0, FX32_ONE, 0 }; // Up
317
318 G3_LookAt(&Eye, &vUp, &at, NULL); // Eye point settings
319 }
320
321 /* Light configuration (configures light color and direction) */
322 {
323 G3_LightVector(GX_LIGHTID_0, 0, 0, -FX32_ONE + 1);
324 G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
325 }
326
327 G3_MtxMode(GX_MTXMODE_TEXTURE); // Set matrix to texture mode
328 G3_Identity(); // Initialize current matrix to a unit matrix
329 // The matrix is set to Position-Vector simultaneous set mode
330 G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
331
332 G3_PushMtx(); // Add matrix to the stack
333
334 /* Calculate the rotation of the cube on the right and set its position */
335 {
336 fx16 s = FX_SinIdx(rotate);
337 fx16 c = FX_CosIdx(rotate);
338
339 G3_Translate(FX32_ONE * 2, 0, pos_z * FX32_ONE);
340 G3_RotX(s, c);
341 G3_RotY(s, c);
342 G3_RotZ(s, c);
343 }
344
345 /* Render configuration for right cube */
346 // Set material's diffuse reflection color and ambient reflection color
347 G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31), // Diffuse reflection color
348 GX_RGB(16, 16, 16), // Ambient reflection color
349 TRUE); // Set vertex color
350 // Configure the specular color and the emission color of the material
351 G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16), // Specular reflection color
352 GX_RGB(0, 0, 0), // The emission color
353 FALSE); // Specular reflection not used
354
355 // Specify texture parameters
356 G3_TexImageParam(GX_TEXFMT_DIRECT, // Use direct texture
357 GX_TEXGEN_TEXCOORD, // Set texture coordinate conversion mode
358 GX_TEXSIZE_S64, // 64 texture s size
359 GX_TEXSIZE_T64, // 64 texture t size
360 GX_TEXREPEAT_NONE, // No repeat
361 GX_TEXFLIP_NONE, // No flip
362 GX_TEXPLTTCOLOR0_USE, // Enable palette color 0 set value
363 texAddr); // Texture address
364 // Polygon attribute settings
365 G3_PolygonAttr(GX_LIGHTMASK_0, // Light 0 ON
366 GX_POLYGONMODE_MODULATE, // Modulation mode
367 GX_CULL_NONE, // No culling
368 0, // Polygon ID 0
369 31, // Alpha value
370 GX_POLYGON_ATTR_MISC_FAR_CLIPPING);
371
372 DrawCube(); // Draw cube
373
374 G3_PopMtx(1);
375
376 G3_PushMtx();
377 /* Calculate the rotation of the cube on the left and set its position */
378 {
379 fx16 s = FX_SinIdx(rotate);
380 fx16 c = FX_CosIdx(rotate);
381
382 G3_Translate(-FX32_ONE * 2, 0, pos_z * FX32_ONE);
383
384 G3_RotX(s, c);
385 G3_RotY(s, c);
386 G3_RotZ(s, c);
387 }
388
389 G3_MtxMode(GX_MTXMODE_TEXTURE); // Set matrix to texture mode
390 G3_Identity(); // Initialize current matrix to a unit matrix
391 // The matrix is set to Position-Vector simultaneous set mode
392 G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
393
394 /* Render configuration for left cube */
395 // Set material's diffuse reflection color and ambient reflection color
396 G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31), // Diffuse reflection color
397 GX_RGB(16, 16, 16), // Ambient reflection color
398 TRUE); // Set vertex color
399 // Configure the specular color and the emission color of the material
400 G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16), // Specular reflection color
401 GX_RGB(0, 0, 0), // The emission color
402 FALSE); // Specular reflection not used
403 // Specify texture parameters
404 G3_TexImageParam(GX_TEXFMT_DIRECT, // Use direct texture
405 GX_TEXGEN_TEXCOORD, // Set texture coordinate conversion mode
406 GX_TEXSIZE_S64, // 64 texture s size
407 GX_TEXSIZE_T64, // 64 texture t size
408 GX_TEXREPEAT_NONE, // No repeat
409 GX_TEXFLIP_NONE, // No flip
410 GX_TEXPLTTCOLOR0_USE, // Enable palette color 0 set value
411 texAddr); // Texture address
412 G3_PolygonAttr(GX_LIGHTMASK_0, // Light 0 ON
413 GX_POLYGONMODE_MODULATE, // Modulation mode
414 GX_CULL_NONE, // Do not perform backface culling
415 0, // Polygon ID 0
416 31, // Alpha value
417 GX_POLYGON_ATTR_MISC_NONE);
418
419 DrawCube(); // Draw cube
420
421 G3_PopMtx(1);
422
423 // Swapping the polygon list RAM, the vertex RAM, etc.
424 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
425
426 /* V-Blank wait */
427 OS_WaitVBlankIntr();
428
429 #ifdef SDK_AUTOTEST // Code for auto-test
430 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
431 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
432 EXT_TestScreenShot(100, 0x954F11C7);
433 EXT_TestTickCounter();
434 #endif //SDK_AUTOTEST
435 }
436 }
437