cleaned all trailing white space from source files.
[sdk] / samples / 3D / TransCube / transCube.ec
1 import "ecere"
2
3 ModelViewer modelViewer { };
4
5 class eModelApp : GuiApplication
6 {
7    driver = "OpenGL";
8    //driver = "Direct3D8";
9    //driver = "Direct3D";
10 }
11
12 class ModelViewer : Window
13 {
14    text = "Transparent Cube Demo";
15    background = black;
16    borderStyle = sizable;
17    hasMaximize = true;
18    hasMinimize = true;
19    hasClose = true;
20    anchor = { left = 0, top = 0, right = 0, bottom = 0 };
21
22    Camera camera
23    {
24       attachedQuaternion,
25       position = Vector3D { 0, 0, -250 },
26       orientation = Euler { 120, 30, 0 },
27       zMin = 0.01f;
28       fov = 53;
29    };
30    Cube model { size = { 100, 100, 100 } };
31
32    bool moving, lightMoving;
33
34    Point startPosition;
35
36    Euler startOrientation;
37    Light light
38    {
39       diffuse = white;
40       specular = white;
41       // orientation = Euler { pitch = 50, yaw = 45 };
42       orientation = Euler { pitch = 10, yaw = 30 };
43    };
44
45    void OnUnloadGraphics()
46    {
47       displaySystem.ClearMaterials();
48       displaySystem.ClearTextures();
49       displaySystem.ClearMeshes();
50    }
51
52    void OnResize(int w, int h)
53    {
54       camera.Setup(w, h, null);
55       Update(null);
56    }
57
58    bool OnLeftButtonDown(int x, int y, Modifiers mods)
59    {
60       if(!moving && !lightMoving)
61       {
62          startPosition.x = x;
63          startPosition.y = y;
64          startOrientation = camera.orientation;
65          Capture();
66          moving = true;
67       }
68       return true;
69    }
70
71    bool OnLeftButtonUp(int x, int y, Modifiers mods)
72    {
73       if(moving)
74       {
75          ReleaseCapture();
76          moving = false;
77       }
78       return true;
79    }
80
81    bool OnRightButtonDown(int x, int y, Modifiers mods)
82    {
83       if(!moving && !lightMoving)
84       {
85          startPosition.x = x;
86          startPosition.y = y;
87          startOrientation = light.orientation;
88          Capture();
89          lightMoving = true;
90       }
91       return true;
92    }
93
94    bool OnRightButtonUp(int x, int y, Modifiers mods)
95    {
96       if(lightMoving)
97       {
98          ReleaseCapture();
99          lightMoving = false;
100       }
101       return true;
102    }
103
104    bool OnMouseMove(int x, int y, Modifiers mods)
105    {
106       if(moving)
107       {
108          Euler euler
109          {
110             startOrientation.yaw   - (x - startPosition.x),
111             startOrientation.pitch + (y - startPosition.y),
112             startOrientation.roll
113          };
114
115          camera.orientation = euler;
116
117          Update(null);
118       }
119       else if(lightMoving)
120       {
121          light.orientation = Euler
122          {
123             startOrientation.yaw   - (x - startPosition.x),
124             startOrientation.pitch + (y - startPosition.y),
125             startOrientation.roll
126          };
127
128          Update(null);
129       }
130       return true;
131    }
132
133    bool OnKeyDown(Key key, unichar ch)
134    {
135       switch(key)
136       {
137          case escape: Destroy(0); return false;
138       }
139       return true;
140    }
141
142    bool OnKeyHit(Key key, unichar ch)
143    {
144       switch((SmartKey) key)
145       {
146          case wheelDown: case minus: camera.position.z *= 1.1111111f; Update(null); break;
147          case wheelUp: case plus: camera.position.z *= 0.9f; Update(null); break;
148       }
149       return true;
150    }
151
152    bool OnLoadGraphics()
153    {
154       if(model.Create(displaySystem))
155       {
156          if(model)
157          {
158             int c;
159             for(c = 0; c<6; c++)
160             {
161                PrimitiveGroup group;
162                Material material;
163                char name[20];
164
165                sprintf(name, "Cube Face %d", c+1);
166                material = displaySystem.GetMaterial(name);
167                if(material)
168                {
169                   material.flags = { noFog = true, doubleSided = true, translucent = true };
170                   material.opacity = 0.5f;
171                   material.diffuse.r = material.diffuse.g = material.diffuse.b = 1;
172                   material.ambient = material.diffuse;
173                   material.baseMap = displaySystem.GetTexture(name);
174                   if(!material.baseMap)
175                   {
176                      material.baseMap = Bitmap { };
177
178                      sprintf(name, "tex%d.bmp", c+1);
179                      if(c == 3) strcpy(name, "glass.bmp");
180
181                      material.baseMap.LoadMipMaps(name, null, displaySystem);
182                      displaySystem.AddTexture(name, material.baseMap);
183                   }
184                }
185             }
186             model.mesh.ApplyTranslucency(model);
187             camera.target = model;
188          }
189          return true;
190       }
191       else
192          return false;
193    }
194
195    void OnRedraw(Surface surface)
196    {
197       display.fogColor = blue;
198       display.fogDensity = 0.000001f;
199
200       surface.Clear(depthBuffer);
201
202       camera.Update();
203
204       display.antiAlias = true;
205       display.SetLight(0, light);
206
207       display.SetCamera(surface, camera);
208
209       display.ambient = ColorRGB { 0.2f, 0.2f, 0.2f };
210
211       display.DrawObject(model);
212
213       display.SetCamera(surface, null);
214    }
215 }