1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - GX - demos - UnitTours/AntiAlias
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 // A sample for anti-alias testing.
19 //
20 // One square is displayed along the setting of anti-alias.
21 //
22 // HOWTO:
23 // 1. Call "G3X_AntiAlias(TRUE)", which switches anti-alias setting to ON.
24 // 2. Call "G3X_AntiAlias(FALSE)", which switches anti-alias setting to OFF.
25 //
26 // OPERATION:
27 // 1. Push start button to change anti-alias setting.
28 //
29 //---------------------------------------------------------------------------
30 #ifdef SDK_TWL
31 #include <twl.h>
32 #else
33 #include <nitro.h>
34 #endif
35 #include "DEMO.h"
36 #include "tex_32768.h"
37
38 //---------------------------------------------------------------------------
39 // Square model data
40 //---------------------------------------------------------------------------
41 // Vertex data
42 static s16 s_Vertex[3 * 8] = {
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 };
48 // Normal data
49 static VecFx10 s_Normal = GX_VECFX10(0, 0, FX32_ONE - 1);
50 // Texture coordinate data
51 static GXSt s_TextureCoord[] = {
52 GX_ST(0, 0),
53 GX_ST(0, 64 * FX32_ONE),
54 GX_ST(64 * FX32_ONE, 0),
55 GX_ST(64 * FX32_ONE, 64 * FX32_ONE)
56 };
57
58 //---------------------------------------------------------------------------
59 // Set vertex coordinate.
60 // input:
61 // i_idx: ID of vertex data
62 //---------------------------------------------------------------------------
Vertex(int i_idx)63 inline void Vertex(int i_idx)
64 {
65 G3_Vtx(s_Vertex[i_idx * 3], s_Vertex[i_idx * 3 + 1], s_Vertex[i_idx * 3 + 2]);
66 }
67
68 //---------------------------------------------------------------------------
69 // Set normal setting.
70 //---------------------------------------------------------------------------
Normal(void)71 inline void Normal(void)
72 {
73 G3_Direct1(G3OP_NORMAL, s_Normal);
74 }
75
76 //---------------------------------------------------------------------------
77 // Set texture coordinate.
78 // input:
79 // i_idx: ID of texture data
80 //---------------------------------------------------------------------------
TextureCoord(int i_idx)81 inline void TextureCoord(int i_idx)
82 {
83 G3_Direct1(G3OP_TEXCOORD, s_TextureCoord[i_idx]);
84 }
85
86 //---------------------------------------------------------------------------
87 // Draw a rectangle and set texture.
88 //---------------------------------------------------------------------------
DrawRectangle(void)89 static void DrawRectangle(void)
90 {
91 G3_Begin(GX_BEGIN_QUADS); // start to set vertexes.(use square polygon)
92 {
93 TextureCoord(1);
94 Normal();
95 Vertex(1);
96 TextureCoord(3);
97 Normal();
98 Vertex(3);
99 TextureCoord(2);
100 Normal();
101 Vertex(2);
102 TextureCoord(0);
103 Normal();
104 Vertex(0);
105 }
106 G3_End(); // end
107 }
108
109 //---------------------------------------------------------------------------
110 // VBlank interrupt function:
111 //
112 // Interrupt handlers are registered on the interrupt table by OS_SetIRQFunction.
113 // OS_EnableIrqMask selects IRQ interrupts to enable, and
114 // OS_EnableIrq enables IRQ interrupts.
115 // Notice that you have to call 'OS_SetIrqCheckFlag' to check a VBlank interrupt.
116 //---------------------------------------------------------------------------
VBlankIntr(void)117 void VBlankIntr(void)
118 {
119 // Set flag which checks VBlank interrupt.
120 OS_SetIrqCheckFlag(OS_IE_V_BLANK);
121 }
122
123 //---------------------------------------------------------------------------
124 // main
125 //---------------------------------------------------------------------------
126 #ifdef SDK_TWL
TwlMain(void)127 void TwlMain(void)
128 #else
129 void NitroMain(void)
130 #endif
131 {
132 unsigned int count = 0;
133 BOOL trg = TRUE;
134 u16 Rotate = 0;
135 u32 myTexAddr = 0x00000; // Address of texture image slot
136
137 // Initialize
138 DEMOInitCommon();
139 DEMOInitVRAM();
140 DEMOInitDisplay3D();
141
142 // Set ClearColor to get effect of AntiAlias
143 G3X_SetClearColor(GX_RGB(0, 0, 0), // Color
144 0x1, // Alpha
145 0x7fff, // Depth
146 0x3f, // Polygon ID
147 FALSE); // Fog setting toward 2D face
148
149 // Load texture image into texture image slot.
150 GX_BeginLoadTex();
151 {
152 GX_LoadTex((void *)&tex_32768_64x64[0], // Src address
153 myTexAddr, // Destination slot address
154 8192); // Size to load
155 }
156 GX_EndLoadTex();
157
158 DEMOStartDisplay();
159
160 OS_Printf("mssg%d:AntiAlias ON\n", count++);
161
162 // main loop
163 while (1)
164 {
165 G3X_Reset();
166 Rotate += 32;
167
168 // Read input
169 DEMOReadKey();
170 #ifdef SDK_AUTOTEST // code for auto test
171 {
172 const EXTKeys keys[8] = { {0, 10}, {PAD_BUTTON_START, 10}, {0, 0} };
173 EXT_AutoKeys(keys, &gKeyWork.press, &gKeyWork.trigger);
174 }
175 #endif
176
177 if (gKeyWork.trigger & PAD_BUTTON_START)
178 {
179 trg = (trg + 1) & 0x01;
180 //---------------------------------------------------------------------------
181 // Anti-alias setting
182 G3X_AntiAlias(trg);
183 //---------------------------------------------------------------------------
184 OS_Printf("mssg%d:AntiAlias %s\n", count++, (trg ? "ON" : "OFF"));
185 }
186
187 // Camera setting
188 {
189 VecFx32 Eye = { 0, 0, FX32_ONE }; // Sight position
190 VecFx32 at = { 0, 0, 0 }; // Viewpoint
191 VecFx32 vUp = { 0, FX32_ONE, 0 }; // Up
192 G3_LookAt(&Eye, &vUp, &at, NULL); // Sight setting
193 }
194
195 // Light setting
196 G3_LightVector(GX_LIGHTID_0, 0, 0, -FX32_ONE + 1);
197 G3_LightColor(GX_LIGHTID_0, GX_RGB(31, 31, 31));
198
199 // Matrix setting
200 G3_MtxMode(GX_MTXMODE_TEXTURE);
201 G3_Identity();
202 G3_MtxMode(GX_MTXMODE_POSITION_VECTOR);
203 G3_PushMtx();
204
205 // Rotate and translate square
206 {
207 fx16 s = FX_SinIdx(Rotate);
208 fx16 c = FX_CosIdx(Rotate);
209
210 G3_Translate(0, 0, -3 * FX32_ONE);
211 G3_RotZ(s, c);
212 }
213
214 // Draw setting
215 G3_MaterialColorDiffAmb(GX_RGB(31, 31, 31), // Diffuse
216 GX_RGB(16, 16, 16), // Ambient
217 TRUE); // Color
218 G3_MaterialColorSpecEmi(GX_RGB(16, 16, 16), // Specular
219 GX_RGB(0, 0, 0), // Emission
220 FALSE); // Shininess
221 G3_TexImageParam(GX_TEXFMT_DIRECT, // Texture format
222 GX_TEXGEN_TEXCOORD, // Texture generation
223 GX_TEXSIZE_S64, // Texture width
224 GX_TEXSIZE_T64, // Texture hight
225 GX_TEXREPEAT_NONE, // Texture repeat
226 GX_TEXFLIP_NONE, // Texture flip
227 GX_TEXPLTTCOLOR0_USE, // Palette color
228 myTexAddr); // Texture address
229 G3_PolygonAttr(GX_LIGHTMASK_0, // Light
230 GX_POLYGONMODE_MODULATE, // Polygon mode
231 GX_CULL_NONE, // Culling
232 0, // Polygon ID
233 31, // Alpha
234 0); // Misc
235
236 // Draw square
237 DrawRectangle();
238
239 G3_PopMtx(1);
240
241 // swapping the polygon list RAM, the vertex RAM, etc.
242 G3_SwapBuffers(GX_SORTMODE_AUTO, GX_BUFFERMODE_W);
243
244 // Wait VBlank
245 OS_WaitVBlankIntr();
246
247 #ifdef SDK_AUTOTEST // code for auto test
248 GX_SetBankForLCDC(GX_VRAM_LCDC_C);
249 EXT_TestSetVRAMForScreenShot(GX_VRAM_LCDC_C);
250 EXT_TestScreenShot(100, 0x7497BC53);
251 EXT_TestTickCounter();
252 #endif //SDK_AUTOTEST
253 }
254 }
255