1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - GX - demos - UnitTours/3D_PolAttr_DpthTest
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 //---------------------------------------------------------------------------
20 // A sample that sets GX_POLYGON_ATTR_MISC_DEPTHTEST_DECAL in G3_PolygonAttr
21 //
22 // This sample displays two squares. If switch is ON, the right one is drawn
23 // only when the polygon's depth value equals the value in the depth buffer.
24 //
25 // USAGE:
26 //   START      : Switch ON/OFF GX_POLYGON_ATTR_MISC_DEPTHTEST_DECAL
27 //
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 //        Rectangle model data
42 //---------------------------------------------------------------------------
43 /* Vertex data */
44 static s16 s_Vertex[3 * 8] = {
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 };
50 /* Normal data */
51 static VecFx10 s_Normal = GX_VECFX10(0, 0, FX32_ONE - 1);
52 /* Texture coordinates */
53 static GXSt s_TextureCoord[] = {
54     GX_ST(0, 0),
55     GX_ST(0, 64 * FX32_ONE),
56     GX_ST(64 * FX32_ONE, 0),
57     GX_ST(64 * FX32_ONE, 64 * FX32_ONE)
58 };
59 
60 //---------------------------------------------------------------------------
61 //  Summary:
62 //        Set vertex coordinates
63 //  Description:
64 //        Use specified vertex data to set vertex coordinates
65 //  Input:
66 //        i_idx: Vertex data ID
67 //---------------------------------------------------------------------------
Vertex(int i_idx)68 inline void Vertex(int i_idx)
69 {
70     G3_Vtx(s_Vertex[i_idx * 3], s_Vertex[i_idx * 3 + 1], s_Vertex[i_idx * 3 + 2]);
71 }
72 
73 //---------------------------------------------------------------------------
74 //  Summary:
75 //        Set normals
76 //  Description:
77 //
78 //---------------------------------------------------------------------------
Normal(void)79 inline void Normal(void)
80 {
81     G3_Direct1(G3OP_NORMAL, s_Normal);
82 }
83 
84 //---------------------------------------------------------------------------
85 //  Summary:
86 //        Texture coordinate settings
87 //  Description:
88 //
89 //  Input:
90 //        i_idx: Texture data ID
91 //---------------------------------------------------------------------------
TextureCoord(int i_idx)92 inline void TextureCoord(int i_idx)
93 {
94     G3_Direct1(G3OP_TEXCOORD, s_TextureCoord[i_idx]);
95 }
96 
97 
98 //---------------------------------------------------------------------------
99 //  Summary:
100 //        Draw rectangles
101 //  Description:
102 //
103 //---------------------------------------------------------------------------
DrawRectangle(void)104 static void DrawRectangle(void)
105 {
106     G3_Begin(GX_BEGIN_QUADS);          // Begin vertex list (draw with quadrilateral polygons)
107     {
108         TextureCoord(1);
109         Normal();
110         Vertex(1);
111         TextureCoord(3);
112         Normal();
113         Vertex(3);
114         TextureCoord(2);
115         Normal();
116         Vertex(2);
117         TextureCoord(0);
118         Normal();
119         Vertex(0);
120     }
121     G3_End();                          // End vertex list
122 }
123 
124 //---------------------------------------------------------------------------
125 //  Summary:
126 //        V-Blank interrupt process
127 //  Description:
128 //        Enables a flag that confirms that a V-Blank interrupt has been performed.
129 //
130 //        The following steps will be performed during common initialization (DEMOInitCommon), causing this function to be called during V-Blanks
131 //        * Select IRQ interrupt (OS_SetIrqMask)
132 //        * Register this function, which performs IRQ interrupts (OS_SetIRQFunction)
133 //        * Enable IRQ interrupts (OS_EnableIrq)
134 //
135 //---------------------------------------------------------------------------
VBlankIntr(void)136 void VBlankIntr(void)
137 {
138     OS_SetIrqCheckFlag(OS_IE_V_BLANK); // Sets a V-Blank interrupt confirmation flag
139 }
140 
141 //---------------------------------------------------------------------------
142 //  Summary:
143 //        Setting GX_POLYGON_ATTR_MISC_DEPTHTEST_DECAL in G3_PolygonAttr
144 //  Description:
145 //        Draws when the polygon depth value is the same as the depth buffer's depth value
146 //
147 //        The rectangles shown on the left side are displayed normally. The rectangles shown on the right side are depth-targeted polygons.
148 //      When the effect is enabled, parts of the right rectangle will be displayed over the left rectangle where the depth value is the same and the rectangles overlap.
149 //
150 //  Controls:
151 //        START: Toggles the effect ON/OFF
152 //---------------------------------------------------------------------------
153 #ifdef SDK_TWL
TwlMain(void)154 void TwlMain(void)
155 #else
156 void NitroMain(void)
157 #endif
158 {
159     u32     texAddr = 0x00000;         // Slot address of the texture image
160     unsigned int count = 0;
161     BOOL    trg = 0;
162     u16     rotate = 0;
163 
164     //---------------------
165     // Initialize (enable IRQ, initialize VRAM , use BG0 in 3D mode)
166     //---------------------
167     DEMOInitCommon();
168     DEMOInitVRAM();
169     DEMOInitDisplay3D();
170 
171     /* Load the texture image to the texture image slot */
172     GX_BeginLoadTex();                 // Map the bank allocated to the slot to LCDC memory
173     {
174         GX_LoadTex((void *)&tex_32768_64x64[0], // Load source pointer
175                    texAddr,            // Load destination slot address
176                    8192);              // Load size
177     }
178     GX_EndLoadTex();                   // Restore the slot mapped to LCDC to its original state
179 
180     DEMOStartDisplay();
181 
182     OS_Printf("mssg%d:PolygonAttr_DepthTest=%s\n", count++, (trg ? "ON" : "OFF"));
183 
184     //---------------------
185     //  Main loop
186     //---------------------
187     while (1)
188     {
189         G3X_Reset();
190         rotate += 128;
191 
192         /* Pad input */
193         DEMOReadKey();
194 #ifdef SDK_AUTOTEST                    // Code for auto-test
195         {
196             const EXTKeys keys[8] = { {0, 20}, {PAD_BUTTON_START, 20}, {0, 0} };
197             EXT_AutoKeys(keys, &gKeyWork.press, &gKeyWork.trigger);
198         }
199 #endif
200 
201         if (DEMO_IS_TRIG(PAD_BUTTON_START))
202         {
203             trg = (trg + 1) & 0x01;
204             OS_Printf("mssg%d:PolygonAttr_DepthTest=%s\n", count++, (trg ? "ON" : "OFF"));
205         }
206 
207         /* Camera configuration */
208         {
209             VecFx32 Eye = { 0, 0, FX32_ONE };   // Eye point position
210             VecFx32 at = { 0, 0, 0 };  // Viewpoint
211             VecFx32 vUp = { 0, FX32_ONE, 0 };   // Up
212 
213             G3_LookAt(&Eye, &vUp, &at, NULL);   // Eye point settings
214         }
215 
216         /* Light configuration (configures light color and direction) */
217         {
218             G3_LightVector(GX_LIGHTID_0, 0, 0, -FX32_ONE + 1);  // Light direction
219             G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));    // Light color
220         }
221 
222         /* Render settings for the rectangle shown on the left */
223         G3_MtxMode(GX_MTXMODE_TEXTURE);
224         G3_Identity();
225         // The matrix is set to Position-Vector simultaneous set mode
226         G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
227 
228         G3_PushMtx();                  // Add matrix to the stack
229 
230         /* Left rectangle rotation calculation and position configuration */
231         {
232             fx16    s = FX_SinIdx(rotate);
233             fx16    c = FX_CosIdx(rotate);
234 
235             G3_Translate(-FX32_ONE / 2, 0, -3 * FX32_ONE);
236             G3_RotZ(s, c);
237         }
238 
239         // Set material's diffuse reflection color and ambient reflection color
240         G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31),     // Diffuse reflection color
241                                 GX_RGB(16, 16, 16),     // Ambient reflection color
242                                 TRUE); // Set vertex color
243         // Configure the specular color and the emission color of the material
244         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // Specular reflection color
245                                 GX_RGB(0, 0, 0),        // The emission color
246                                 FALSE); // Specular reflection not used
247         // Specify texture parameters
248         G3_TexImageParam(GX_TEXFMT_DIRECT,      // Use direct texture
249                          GX_TEXGEN_TEXCOORD,    // Set texture coordinate conversion mode
250                          GX_TEXSIZE_S64,        // 64 texture s size
251                          GX_TEXSIZE_T64,        // 64 texture t size
252                          GX_TEXREPEAT_NONE,     // No repeat
253                          GX_TEXFLIP_NONE,       // No flip
254                          GX_TEXPLTTCOLOR0_USE,  // Enable palette color 0 set value
255                          texAddr);     // Texture address
256         // Polygon attribute settings
257         G3_PolygonAttr(GX_LIGHTMASK_0, // Reflect light 0
258                        GX_POLYGONMODE_MODULATE, // Modulation mode
259                        GX_CULL_NONE,   // Do not cull
260                        0,              // Polygon ID 0
261                        31,             // Alpha value
262                        GX_POLYGON_ATTR_MISC_NONE);
263 
264         DrawRectangle();               // Draw rectangles
265 
266         G3_PopMtx(1);
267 
268         G3_PushMtx();
269 
270         /* Right rectangle rotation calculation and position configuration */
271         {
272             fx16    s = FX_SinIdx(rotate);
273             fx16    c = FX_CosIdx(rotate);
274 
275             G3_Translate(FX32_ONE / 2, 0, -3 * FX32_ONE);
276             G3_RotZ(s, c);
277         }
278         /* right display rectangle render configuration */
279         G3_MtxMode(GX_MTXMODE_TEXTURE); // Set matrix to texture mode
280         G3_Identity();                 // Initialize current matrix to a unit matrix
281         // The matrix is set to Position-Vector simultaneous set mode
282         G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
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 vertex color
287         // Configure the specular color and the emission color of the material
288         G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16),     // Specular reflection color
289                                 GX_RGB(0, 0, 0),        // The emission color
290                                 FALSE); // Specular reflection not used
291         // Specify texture parameters
292         G3_TexImageParam(GX_TEXFMT_DIRECT,      // Use direct texture
293                          GX_TEXGEN_TEXCOORD,    // Set texture coordinate conversion mode
294                          GX_TEXSIZE_S64,        // 64 texture s size
295                          GX_TEXSIZE_T64,        // 64 texture t size
296                          GX_TEXREPEAT_NONE,     // No repeat
297                          GX_TEXFLIP_NONE,       // No flip
298                          GX_TEXPLTTCOLOR0_USE,  // Enable palette color 0 set value
299                          texAddr);     // Texture address
300 
301         // Depth change settings for the rectangles that are displayed on the right
302         {
303             int     attr;
304 
305             if (trg)
306             {
307                 attr = GX_POLYGON_ATTR_MISC_DEPTHTEST_DECAL;
308             }
309             else
310             {
311                 attr = GX_POLYGON_ATTR_MISC_NONE;
312             }
313             // Polygon attribute settings
314             G3_PolygonAttr(GX_LIGHTMASK_0,      // Light 0 ON
315                            GX_POLYGONMODE_MODULATE,     // Modulation mode
316                            GX_CULL_NONE,        // No culling
317                            0,          // Polygon ID 0
318                            31,         // Alpha value
319                            attr);
320         }
321 
322         DrawRectangle();               // Draw rectangles
323 
324         G3_PopMtx(1);
325 
326         // Swapping the polygon list RAM, the vertex RAM, etc.
327         G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
328 
329         OS_WaitVBlankIntr();           // V-Blank wait
330 
331 #ifdef SDK_AUTOTEST                    // Code for auto-test
332         GX_SetBankForLCDC(GX_VRAM_LCDC_C);
333         EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
334         EXT_TestScreenShot(100, 0x292691D2);
335         EXT_TestTickCounter();
336 #endif //SDK_AUTOTEST
337 
338     }
339 }
340