611726728e0b29828578dd948f7b28ee8867d8f9
[sdk] / samples / 3D / ModelViewer / eModel.ec
1 import "ecere"
2 import "engineSettings"
3
4 static FileFilter fileFilters[] =
5 {
6    {
7       "3D Studio Model Files (*.3ds)",
8       "3ds"
9    },
10    { "All files", null }
11 };
12
13 FileDialog fileDialog
14 {
15    filters = fileFilters, sizeFilters = sizeof(fileFilters);
16    text = "Select a 3DS model to display..."
17 };
18
19 ModelViewer modelViewer { };
20
21 class eModelApp : GuiApplication
22 {
23    // driver = "OpenGL";
24
25    void Main()
26    {
27       if(EngineSettings { }.Modal())
28       {
29          bool selected = false;
30          if(argc > 1)
31          {
32             selected = true;         
33             strcpy(fileDialog.filePath, argv[1]);
34          }
35          else
36          {
37             strcpy(fileDialog.filePath, "toyota.3ds");
38             //selected = true;         
39          }
40          while(selected || fileDialog.Modal() == ok)
41          {
42             modelViewer.modelFile = fileDialog.filePath;
43             modelViewer.Modal();
44             selected = false;
45          }
46       }
47    }
48 }
49
50 class ModelViewer : Window
51 {
52    Camera camera
53    {
54       attached,
55       fov = 53,
56       position = { 0, 0,-100 }, 
57       orientation = Euler { 0, 30, 0 },
58       zMin = 0.01f;
59
60    };
61    Object model {};
62
63    //FPS frameRate;
64    bool moving, lightMoving;
65    Timer timer
66    {
67       this, delay = 0.01;
68
69       bool DelayExpired()
70       {
71          if(model)
72          {
73             /*
74             Object rotor = model.Find("propmain");
75             Object rrotor = model.Find("propback");
76
77             if(rotor)
78                rotor.transform.angle.RotateY(0.01 * 30);
79             if(rrotor)
80                rrotor->transform.angle.RotateX(0.01 * 30);
81             */
82             model.frame++;
83             // cameraObject.frame++;
84          }
85             
86          Update(null);
87          return true;
88       }
89    };
90    Point startPosition;
91    Euler startOrientation;
92    Light light
93    {
94       diffuse = white;
95       specular = white;
96       orientation = Euler { pitch = 50, yaw = 45 };
97    };
98    EditBox help
99    {
100       this, visible = false, noCaret = true, multiLine = true, readOnly = true, hasVertScroll = true, hasHorzScroll = true, borderStyle = sizable,
101       text = "Help", anchor = Anchor { right = 0, top = 0 }, size = Size { 400, 120 }, background = red, opacity = 0.2f,
102       font = { "Courier New", 10 }
103    };
104    FillModeValue fillMode;
105    Object cameraObject;
106    SkyBox sky { size = { 10000, 10000, 10000 }, folder = ":skycube", extension = "pcx" };
107    char * modelFile;
108
109    background = blue;
110    borderStyle = sizable;
111    hasMaximize = true;
112    hasMinimize = true;
113    hasClose = true;
114    text = "ECERE Model Viewer";
115    anchor = Anchor { 0,0,0,0 };
116
117    ModelViewer()
118    {
119       File f = FileOpen(":help.txt", read);
120       if(f)
121       {
122          help.Load(f);
123          delete f;
124       }
125    }
126
127    bool OnCreate()
128    {
129       fillMode = solid;
130       timer.userData = this;
131       // timer.Start();
132       return true;
133    }
134
135    void OnUnloadGraphics()
136    {
137       displaySystem.ClearMaterials();
138       displaySystem.ClearTextures();
139       displaySystem.ClearMeshes();
140    }
141
142    void OnDestroy()
143    {
144       //sky.Free(null);
145       //model.Free(null);
146    }
147
148    void OnResize(int w, int h)
149    {
150       camera.Setup(w, h, null);
151       Update(null);
152    }
153
154    bool OnLeftButtonDown(int x, int y, Modifiers mods)
155    {
156       if(!moving && !lightMoving)
157       {
158          startPosition.x = x;
159          startPosition.y = y;
160          startOrientation = camera.orientation;
161          Capture();
162          moving = true;
163       }
164       return true;
165    }
166
167    bool OnLeftButtonUp(int x, int y, Modifiers mods)
168    {
169       if(moving)
170       {
171          ReleaseCapture();
172          moving = false;
173       }
174       return true;
175    }
176
177    bool OnRightButtonDown(int x, int y, Modifiers mods)
178    {
179       if(!moving && !lightMoving)
180       {
181          startPosition.x = x;
182          startPosition.y = y;
183          startOrientation = light.orientation;
184          Capture();
185          lightMoving = true;
186       }
187       return true;
188    }
189
190    bool OnRightButtonUp(int x, int y, Modifiers mods)
191    {
192       if(lightMoving)
193       {
194          ReleaseCapture();
195          lightMoving = false;
196       }
197       return true;
198    }
199
200    bool OnMouseMove(int x, int y, Modifiers mods)
201    {
202       if(moving)
203       {
204          Euler euler
205          {
206             startOrientation.yaw   - (x - startPosition.x),
207             startOrientation.pitch + (y - startPosition.y),
208             0
209          };
210          //if(euler.pitch > 90) euler.pitch = 90;
211          //if(euler.pitch < 1) euler.pitch = 1;
212
213          camera.orientation = euler;
214
215          Update(null);
216       }
217       else if(lightMoving)
218       {
219          light.orientation = Euler
220          {
221             startOrientation.yaw   - (x - startPosition.x),
222             startOrientation.pitch + (y - startPosition.y),
223             0
224          };
225
226          Update(null);
227       }
228       return true;
229    }
230
231    bool OnKeyDown(Key key, unichar ch)
232    {
233       switch(key)
234       {
235          case escape: Destroy(0); return false;
236          case g:
237             fillMode = (fillMode == wireframe) ? solid : wireframe;
238             break;
239          case h: 
240             help.visible ^= true;
241             break;
242          case k1: case k2: case k3: case k4:
243             if(key == k1)        cameraObject = model.Find("Camera01");
244             else if(key == k2)   cameraObject = model.Find("Camera02");
245             else if(key == k3)   cameraObject = model.Find("Camera03");
246             else if(key == k4)   cameraObject = model.Find("Camera04");
247             
248             camera = cameraObject.camera;
249             camera.Setup(clientSize.w, clientSize.h, null);
250             Update(null);
251             break;
252       }
253       return true;
254    }
255
256    bool OnKeyHit(Key key, unichar ch)
257    {
258       switch((SmartKey) key)
259       {
260          case wheelDown: case minus: camera.position.z *= 1.1111111f; Update(null); break;
261          case wheelUp: case plus: camera.position.z *= 0.9f; Update(null); break;
262          //case minus: camera.position.z += 10; break;
263          //case plus: camera.position.z -= 10; break;
264    /*
265          case pageDown:
266             model.Animate(model.frame + 1);
267             break;
268          case pageUp:
269             model.Animate(model.frame - 1);
270             break;
271          case home:
272             model.Animate(model.startFrame);
273             break;
274          case end:
275             model.Animate(model.endFrame);
276             break;
277    */
278       }
279       return true;
280    }
281
282    bool OnLoadGraphics()
283    {
284       if(model.Load(modelFile, null, displaySystem))
285       {
286          if(model)
287          {
288             // model.Merge(displaySystem);
289
290             // camera.position.z = - model.radius * 5;
291
292             //cameraObject = model.Find("Camera01");
293             //cameraObject = model.Find("Camera03");
294             //cameraObject = model.Find("Camera02");
295             //cameraObject = model.Find("Camera04");
296             //cameraObject = model.Find("FullscreenCam01");
297             //cameraObject = model.Find("Full Scre1");
298             //cameraObject = model.Find("Full Scree");
299             cameraObject = model.Find("Plasma Cam");
300             if(cameraObject)
301                camera = cameraObject.camera;
302             else
303                camera.target = model;
304          }
305
306          sky.Create(displaySystem);
307          return true;
308       }
309       else
310          return false;
311    }
312
313    void OnRedraw(Surface surface)
314    {
315       display.fogColor = blue;
316       display.fogDensity = 0.000001f;
317
318       surface.SetBackground(lightBlue);
319       surface.Clear(colorAndDepth);
320
321       camera.Update();
322
323       display.SetCamera(surface, camera);
324
325       display.SetLight(0, &light);
326       display.ambient = black;
327       // display.SetLights(model);
328
329       display.fillMode = fillMode;
330
331       sky.Render(camera, display);
332       display.DrawObject(model);
333       /*
334       display.DrawObject(model);
335       display.DrawObject(model);
336       display.DrawObject(model);
337       display.DrawObject(model);
338       */
339
340       display.fillMode = solid;
341
342       display.SetCamera(surface, null);
343
344       /*
345       frameRate.Step();
346       surface.SetForeground(Black);
347       if(model)
348          surface.WriteTextf(0,0, "%.2f, Frame: %d",  frameRate.fps, model.frame++);
349       */
350       surface.WriteTextf(0,20, "Press H to toggle help screen");
351    }
352 }