cleaned all trailing white space from source files.
[sdk] / samples / 3D / OpenGL / glEcereCamera.ec
1 import "ecere"
2 #include <GL/gl.h>
3
4 class GLDemo : Window
5 {
6    displayDriver = "OpenGL";
7    nativeDecorations = true;
8    text = "GL+Ecere Camera Demo";
9    background = black;
10    hasClose = true;
11    size = { 640, 480 };
12
13    Camera camera { fixed, position = { 50, -50, -300 }, orientation = Euler { 25, 0, 0 }, fov = 53; };
14    Light light { diffuse = white; specular = white; orientation = Euler { pitch = -45, yaw = 0 }; };
15
16    Cube cube { };
17
18    bool moving;
19    Point lastMouse;
20
21    Timer timer
22    {
23       this, 0.05, true;
24
25       bool DelayExpired()
26       {
27          static Time lastTime = 0, time;
28          time = GetTime();
29          if(!lastTime) lastTime = time;
30          UpdateObjects(time - lastTime);
31          lastTime = time;
32          Update(null);
33          return true;
34       }
35    };
36
37    void UpdateObjects(Time diffTime)
38    {
39       Quaternion orientation = cube.transform.orientation;
40       orientation.RotateYawPitch((Degrees)diffTime * 0.5, (Degrees)diffTime * 0.5);
41       orientation.RotateRoll((Degrees)diffTime * 0.5);
42       cube.transform.orientation = orientation;
43       cube.UpdateTransform();
44    }
45
46    bool OnLoadGraphics()
47    {
48       cube.Create(displaySystem);
49       cube.transform.scaling = { 100, 100, 100 };
50       cube.transform.position = { 0, 0, 0 };
51       cube.transform.orientation = Euler { 50, 50 };
52       cube.UpdateTransform();
53       return true;
54    }
55
56    void OnResize(int w, int h)
57    {
58       camera.Setup(w, h, null);
59    }
60
61    void OnRedraw(Surface surface)
62    {
63       surface.Clear(depthBuffer);
64       camera.Update();
65       display.SetLight(0, light);
66       display.fogDensity = 0;
67       display.SetCamera(surface, camera);
68
69       display.DrawObject(cube);
70
71       glPushMatrix();
72       glTranslated(
73          -camera.cPosition.x,
74          -camera.cPosition.y,
75          -camera.cPosition.z);
76
77       glDisable(GL_LIGHTING);
78
79       glBegin(GL_LINES);
80
81       glColor3f(1, 0, 0);
82       glVertex3f(-300, 0, 0);
83       glVertex3f(300, 0, 0);
84
85       glColor3f(0, 1, 0);
86       glVertex3f(0, -300, 0);
87       glVertex3f(0, 300, 0);
88
89       glColor3f(0, 0, 1);
90       glVertex3f(0, 0, -300);
91       glVertex3f(0, 0, 300);
92
93       glEnd();
94
95       glEnable(GL_LIGHTING);
96
97       glPopMatrix();
98
99       display.SetCamera(surface, null);
100    }
101
102    bool OnMouseMove(int x, int y, Modifiers mods)
103    {
104       if(moving)
105       {
106          int dx = (int)(x - lastMouse.x), dy = (int)(lastMouse.y - y);
107          camera.RotateYaw(-dx, 0,0);
108          camera.RotatePitch(dy, 0,0);
109          Update(null);
110          lastMouse.x = x;
111          lastMouse.y = y;
112       }
113       return true;
114    }
115
116    bool OnLeftButtonDown(int x, int y, Modifiers mods)
117    {
118       if(!moving)
119       {
120          bool back;
121          Capture();
122          moving = true;
123          lastMouse = { x, y };
124       }
125       return true;
126    }
127
128    bool OnLeftButtonUp(int x, int y, Modifiers mods)
129    {
130       if(moving)
131       {
132          ReleaseCapture();
133          moving = false;
134       }
135       return true;
136    }
137
138    bool OnKeyHit(Key key, unichar ch)
139    {
140       switch(key)
141       {
142          case wheelUp:
143             camera.Move({ 0,0,100 });
144             Update(null);
145             break;
146          case wheelDown:
147             camera.Move({ 0,0,-100 });
148             Update(null);
149             break;
150       }
151       return true;
152    }
153 }
154
155 GLDemo glDemo { anchor = { horz = 160, vert = 120 } };