3 class WalkAroundApp : GuiApplication
6 // driver = "Direct3D";
8 appName = "3D Walkaround Sample";
10 bool wasProcessingMoves;
14 bool processMoves = form && form.acquiredInput;
17 Time time = GetTime(), diffTime;
20 if(wasProcessingMoves != processMoves)
23 GetMouseState(&buttons, &xd, &yd);
25 diffTime = time - lastTime;
27 GetMouseState(&buttons, &xd, &yd);
30 bool updateCamera = false;
32 float cameraSpeed = 0.5;
33 Vector3D movement { };
34 Euler orientation = camera.orientation;
35 Euler euler = orientation;
39 euler.yaw -= xd * cameraSpeed;
40 euler.pitch -= yd * cameraSpeed;
41 if(GetKeyState(right)) euler.yaw -= diffTime * cameraSpeed;
42 if(GetKeyState(left)) euler.yaw += diffTime * cameraSpeed;
43 if(GetKeyState(up)) euler.pitch -= diffTime * cameraSpeed;
44 if(GetKeyState(down)) euler.pitch += diffTime * cameraSpeed;
45 euler.pitch = Min(Max(euler.pitch, Degrees { -89.9 }), Degrees { 89.9 });
46 if(orientation.yaw != euler.yaw || orientation.pitch != euler.pitch || orientation.roll != euler.roll)
48 camera.orientation = euler;
52 if(GetKeyState(e)) movement.y -= diffTime * speed;
53 if(GetKeyState(c)) movement.y += diffTime * speed;
54 if(GetKeyState(s) || buttons.right) movement.z -= diffTime * speed;
55 if(GetKeyState(w) || buttons.left) movement.z += diffTime * speed;
56 if(GetKeyState(a)) movement.x -= diffTime * speed;
57 if(GetKeyState(d)) movement.x += diffTime * speed;
58 if(movement.x || movement.y || movement.z)
60 camera.Move(movement);
71 wasProcessingMoves = processMoves;
76 define app = (WalkAroundApp)__thisModule.application;
81 position = Vector3D { 0, -100, -200 },
82 orientation = Euler { 0, 0, 0 },
90 orientation = Euler { 0, 70 };
98 class WalkAroundForm : Window
100 caption = "3D Walkaround Sample";
103 borderStyle = sizable;
107 clientSize = { 632, 438 };
109 Array<Material> materials { size = 8 };
110 Array<int> map { size = width * height };
111 Array<int> elv { size = width * height };
112 Array<Color> colors { [ black, red, green, blue, cyan, magenta, white, gray, orange, yellow, brown, aquamarine, goldenrod ] };
113 Array<Cube> cubes { };
115 Material sideMat { opacity = 0.5f, diffuse = teal, ambient = teal, flags = { doubleSided = true, translucent = true } };
116 Material dolphin1Mat { opacity = 1.0f, diffuse = skyBlue, ambient = skyBlue };
117 Material dolphin2Mat { opacity = 1.0f, diffuse = lightGray, ambient = lightGray };
118 Object dolphinModel { };
122 Array<Bitmap> textures { };
134 for(i = 0; i < map.count; i++)
136 int num = map[i] = GetRandom(0, 7);
137 elv[i] = GetRandom(1, 100);
141 bool OnLoadGraphics()
143 Bitmap textureFile { };
145 PrimitiveGroup group;
147 dolphinModel.Load(":dolphin.3ds", null, displaySystem);
148 dolphinModel.Merge(displaySystem);
149 dolphinModel.mesh.ApplyMaterial(null);
151 dolphin1.Duplicate(dolphinModel);
152 dolphin1.transform.position = { -20, -50, 10 };
153 dolphin1.transform.scaling = { 0.2, 0.2, 0.2 };
154 dolphin1.UpdateTransform();
155 dolphin1.material = dolphin1Mat;
157 dolphin2.Duplicate(dolphinModel);
158 dolphin2.transform.position = { 60, -30, -5 };
159 dolphin2.transform.scaling = { 0.2, 0.2, 0.2 };
160 dolphin2.UpdateTransform();
161 dolphin2.material = dolphin2Mat;
163 if(textureFile.Load(":texture1.pcx", null, null))
166 int height = textureFile.height / count;
167 for(i = 0; i < count; i++)
170 tex.Allocate(null, textureFile.width, height, 0, pixelFormat888, false);
171 tex.Grab(textureFile, 0, i * height);
172 tex.MakeMipMaps(displaySystem);
186 for(y = 0; y < height; y++)
188 for(x = 0; x < width; x++)
190 int ix = y * width + x;
192 int xs = -((tileSize+gap) * width) / 2;
193 int ys = -((tileSize+gap) * height) / 2;
199 cube.Create(displaySystem);
200 cube.flags.translucent = true;
202 for(face = cube.mesh.groups.first; face; face = face.next)
203 face.material = face.prev ? sideMat : materials[num];
204 cube.mesh.ApplyTranslucency(cube);
205 cube.transform.position = { x * (tileSize + gap), -h/2, y * (tileSize + gap) };
206 cube.transform.scaling = { tileSize, h, tileSize };
207 cube.transform.orientation = Euler { 0, 0, 0 };
208 cube.UpdateTransform();
214 void OnUnloadGraphics()
217 c.Free(displaySystem);
218 dolphinModel.Free(displaySystem);
219 dolphin1.Free(displaySystem);
220 dolphin2.Free(displaySystem);
224 void OnResize(int w, int h)
226 camera.Setup(w, h, null);
230 void OnRedraw(Surface surface)
232 surface.Clear(depthBuffer);
233 display.SetCamera(surface, camera);
234 display.SetLight(0, light);
235 display.ambient = Color { 100, 100, 100 };
238 display.DrawObject(c);
240 display.DrawObject(dolphin1);
241 display.DrawObject(dolphin2);
243 display.SetCamera(surface, null);
247 surface.foreground = white;
248 surface.WriteTextf(50, 50, "Press I to acquire mouse Input, F for Full screen.");
252 bool OnKeyHit(Key key, unichar ch)
256 case escape: Destroy(0); break;
258 camera.Move({ 0, -4, 0 });
263 camera.Move({ 0, 4, 0 });
269 acquiredInput ^= true;
270 AcquireInput(acquiredInput);
276 bool fullScreen = !app.fullScreen;
280 anchor = { 0, 0, 0, 0 };
285 borderStyle = sizable;
286 nativeDecorations = true;
292 clientSize = { 632, 438 };
295 app.fullScreen = fullScreen;
296 AcquireInput(acquiredInput);
304 WalkAroundForm form {};