1 module dlangui.graphics.scene.camera;
2 
3 public import dlangui.core.config;
4 static if (ENABLE_OPENGL):
5 static if (BACKEND_GUI):
6 
7 import dlangui.graphics.scene.node;
8 
9 import dlangui.core.math3d;
10 
11 /// Camera
12 class Camera : Node3d {
13 
14     protected mat4 _projectionMatrix;
15     protected mat4 _viewMatrix;
16     protected mat4 _viewProjectionMatrix;
17     protected bool _dirtyViewProjection = true;
18     protected bool _dirtyView = true;
19 
20     protected bool _enabled;
21 
22     protected float _far = 100;
23 
24     this(string ID = null) {
25         super(ID);
26         _enabled = true;
27         setPerspective(4.0f, 3.0f, 45.0f, 0.1f, 100.0f);
28     }
29 
30     /// returns FAR range of camera
31     @property float farRange() { return _far; }
32 
33     /// returns true if camera is active (enabled)
34     @property bool enabled() { return _enabled; }
35 
36     /// activates / deactivates camera
37     @property void enabled(bool v) {
38         if (scene)
39             scene.activeCamera = null;
40         _enabled = v;
41     }
42 
43     /// returns true if some changes occured in projection or view matrix since last matrix getter call
44     @property bool viewChanged() {
45         return _dirtyTransform || _dirtyViewProjection || _dirtyView;
46     }
47 
48     /// get projection matrix
49     @property ref const(mat4) projectionMatrix() {
50         return _projectionMatrix;
51     }
52 
53     /// set custom projection matrix
54     @property void projectionMatrix(mat4 v) {
55         _projectionMatrix = v;
56         _dirtyViewProjection = true;
57     }
58 
59     override protected void invalidateTransform() {
60         _dirtyTransform = true;
61         _dirtyViewProjection = true;
62         _dirtyView = true;
63     }
64 
65     override @property ref const(mat4) viewMatrix() {
66         if (_dirtyView) {
67             _viewMatrix = matrix.invert();
68             _dirtyView = false;
69         }
70         return _viewMatrix;
71     }
72 
73     /// get projection*view matrix
74     override @property ref const(mat4) projectionViewMatrix() {
75         if (_dirtyTransform || _dirtyViewProjection) {
76             _viewProjectionMatrix = _projectionMatrix * viewMatrix;
77             _dirtyViewProjection = false;
78         }
79         return _viewProjectionMatrix;
80     }
81 
82     /// set orthographic projection
83     void setOrtho(float left, float right, float bottom, float top, float near, float far) {
84         _far = far;
85         _projectionMatrix.setOrtho(left, right, bottom, top, near, far);
86         _dirtyViewProjection = true;
87     }
88 
89     /// set perspective projection
90     void setPerspective(float width, float height, float fov, float near, float far) {
91         _far = far;
92         _projectionMatrix.setPerspective(fov, width / height, near, far);
93         _dirtyViewProjection = true;
94     }
95 
96     ~this() {
97         // disconnect active camera
98         if (scene && scene.activeCamera is this)
99             scene.activeCamera = null;
100     }
101 }