1 /*
2 Copyright (c) 2017-2018 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.graphics.freeview;
29 
30 import dlib.core.memory;
31 import dlib.math.vector;
32 import dlib.math.matrix;
33 import dlib.math.quaternion;
34 
35 import derelict.sdl2.sdl;
36 
37 import dagon.core.ownership;
38 import dagon.core.event;
39 import dagon.core.keycodes;
40 import dagon.graphics.tbcamera;
41 import dagon.graphics.view;
42 
43 class Freeview: EventListener, View
44 {
45     TrackballCamera camera;
46     int prevMouseX;
47     int prevMouseY;
48 
49     this(EventManager emngr, Owner owner)
50     {
51         super(emngr, owner);
52         camera = New!TrackballCamera();
53         camera.pitch(45.0f);
54         camera.turn(45.0f);
55         camera.setZoom(20.0f);
56     }
57 
58     void reset()
59     {
60         camera.reset();
61         camera.pitch(45.0f);
62         camera.turn(45.0f);
63         camera.setZoom(20.0f);
64         camera.update(1.0 / 60.0);
65     }
66 
67     ~this()
68     {
69         Delete(camera);
70     }
71 
72     override void onMouseButtonDown(int button)
73     {
74         if (button == MB_LEFT)
75         {
76             prevMouseX = eventManager.mouseX;
77             prevMouseY = eventManager.mouseY;
78         }
79     }
80     
81     override void onMouseWheel(int x, int y)
82     {
83         camera.zoom(cast(float)y * 0.2f);
84     }
85 
86     void setZoom(float z)
87     {
88         camera.setZoom(z);
89     }
90 
91     void update(double dt)
92     {
93         processEvents();
94 
95         if (eventManager.mouseButtonPressed[MB_RIGHT])
96         {
97             float shift_x = (eventManager.mouseX - prevMouseX) * 0.1f;
98             float shift_y = -(eventManager.mouseY - prevMouseY) * 0.1f;
99             Vector3f trans = camera.getUpVector * shift_y + camera.getRightVector * shift_x;
100             camera.translateTarget(trans);
101         }
102         else if (eventManager.mouseButtonPressed[MB_LEFT] && eventManager.keyPressed[KEY_LCTRL])
103         {
104             float shift_x = (eventManager.mouseX - prevMouseX);
105             float shift_y = (eventManager.mouseY - prevMouseY);
106             camera.zoom((shift_x + shift_y) * 0.1f);
107         }
108         else if (eventManager.mouseButtonPressed[MB_LEFT])
109         {                
110             float turn_m = (eventManager.mouseX - prevMouseX);
111             float pitch_m = (eventManager.mouseY - prevMouseY);
112             camera.pitch(pitch_m);
113             camera.turn(turn_m);
114         }
115 
116         prevMouseX = eventManager.mouseX;
117         prevMouseY = eventManager.mouseY;
118         
119         camera.update(dt);
120     }
121 
122     Matrix4x4f viewMatrix()
123     {
124         return camera.viewMatrix();
125     }
126     
127     Matrix4x4f invViewMatrix()
128     {
129         return camera.invViewMatrix();
130     }
131     
132     
133     Vector3f cameraPosition()
134     {
135         return camera.getPosition();
136     }
137 }
138