2 import "engineSettings"
4 static FileFilter fileFilters[] =
7 "3D Studio Model Files (*.3ds)",
15 filters = fileFilters, sizeFilters = sizeof(fileFilters);
16 text = "Select a 3DS model to display..."
19 ModelViewer modelViewer { };
21 class eModelApp : GuiApplication
27 if(EngineSettings { }.Modal())
29 bool selected = false;
33 fileDialog.filePath = argv[1];
37 fileDialog.filePath = "models/cow/cow.3DS";
40 while(selected || fileDialog.Modal() == ok)
42 modelViewer.modelFile = fileDialog.filePath;
50 class ModelViewer : Window
52 caption = $"Ecere Model Viewer";
54 borderStyle = sizable;
58 anchor = { left = 0, top = 0, right = 0, bottom = 0 };
63 fovDirection = vertical,
65 position = { 0, 0, -10 },
66 eulerOrientation = Euler { 30, 0, 0 },
70 Object cameraTarget { };
75 bool moving, lightMoving;
91 Euler startOrientation;
96 orientation = Euler { pitch = 50, yaw = 45 };
100 this, caption = $"Help", background = red, 0.2f, borderStyle = sizable, font = { "Courier New", 10 }, size = { 420, 130 }, anchor = { top = 0, right = 0 }, visible = false, hasHorzScroll = true, true, readOnly = true, true, noCaret = true
103 FillModeValue fillMode;
104 SkyBox sky { size = { 10000, 10000, 10000 }, folder = ":skycube", extension = "jpg" };
106 const char * modelFile;
110 File f = FileOpen(":help.txt", read);
121 timer.userData = this;
126 void OnUnloadGraphics()
128 sky.Free(displaySystem);
129 model.Free(displaySystem);
130 displaySystem.ClearMaterials();
131 displaySystem.ClearTextures();
132 displaySystem.ClearMeshes();
143 void OnResize(int w, int h)
145 camera.Setup(w, h, null);
149 bool OnLeftButtonDown(int x, int y, Modifiers mods)
151 if(!moving && !lightMoving)
155 startOrientation = camera.eulerOrientation;
162 bool OnLeftButtonUp(int x, int y, Modifiers mods)
172 bool OnRightButtonDown(int x, int y, Modifiers mods)
174 if(!moving && !lightMoving)
178 startOrientation = light.orientation;
185 bool OnRightButtonUp(int x, int y, Modifiers mods)
195 bool OnMouseMove(int x, int y, Modifiers mods)
201 startOrientation.yaw - (x - startPosition.x),
202 startOrientation.pitch + (y - startPosition.y),
206 camera.eulerOrientation = euler;
212 light.orientation = Euler
214 startOrientation.yaw - (x - startPosition.x),
215 startOrientation.pitch + (y - startPosition.y),
224 bool OnKeyDown(Key key, unichar ch)
228 case escape: Destroy(0); return false;
230 fillMode = (fillMode == wireframe) ? solid : wireframe;
233 help.visible ^= true;
239 bool OnKeyHit(Key key, unichar ch)
241 switch((SmartKey) key)
243 case wheelDown: case minus: camera.position.z *= 1.1111111f; Update(null); break;
244 case wheelUp: case plus: camera.position.z *= 0.9f; Update(null); break;
245 case up: camera.position.y += camera.position.z / 20; Update(null); break;
246 case down: camera.position.y -= camera.position.z / 20; Update(null); break;
247 case left: camera.position.x += camera.position.z / 20; Update(null); break;
248 case right: camera.position.x -= camera.position.z / 20; Update(null); break;
250 case pageDown: model.Animate(model.frame + 1); break;
251 case pageUp: model.Animate(model.frame - 1); break;
252 case home: model.Animate(model.startFrame); break;
253 case end: model.Animate(model.endFrame); break;
259 bool OnLoadGraphics()
261 if(model.Load(modelFile, null, displaySystem))
263 float r = model.radius;
264 cameraTarget.transform.position =
266 (model.max.x + model.min.x) / 2,
267 (model.max.y + model.min.y) / 2,
268 (model.max.z + model.min.z) / 2
270 // model.Merge(displaySystem);
273 camera.position = { 0, 0, -r * 2 };
274 camera.eulerOrientation = Euler { 30, 0, 0 };
276 light.orientation = Euler { pitch = 50, yaw = 45 };
278 if(r * 2 < camera.zMax / 10)
280 while(r * 2 < camera.zMax / 100)
287 while(r * 2 > camera.zMax / 10)
293 camera.target = cameraTarget;
295 sky.size = { camera.zMax, camera.zMax, camera.zMax };
296 sky.Create(displaySystem);
302 void OnRedraw(Surface surface)
304 //display.fogColor = blue;
305 display.fogDensity = 0; //0.000001f;
307 surface.SetBackground(lightBlue);
308 surface.Clear(colorAndDepth);
312 // Set Light before setting the camera will set the light in view space
313 display.SetLight(0, &light);
315 display.SetCamera(surface, camera);
317 // Set Light after setting the camera will set the light in world space
318 //display.SetLight(0, &light);
320 display.ambient = { 30, 30, 30 }; //black;
321 // display.SetLights(model);
323 display.fillMode = fillMode;
325 sky.Render(camera, display);
326 display.DrawObject(model);
328 display.fillMode = solid;
330 display.SetCamera(surface, null);
334 surface.SetForeground(Black);
336 surface.WriteTextf(0,0, "%.2f, Frame: %d", frameRate.fps, model.frame++);
338 surface.WriteTextf(0,20, "Press H to toggle help screen");