-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl.d
228 lines (197 loc) · 5.42 KB
/
impl.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Copyright: Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved
* Authors: $(Gabriel Gheorghe)
* License: $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007)
* Source: $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/camera/impl.d)
* Documentation:
* Coverage:
**/
module liberty.camera.impl;
import liberty.core.engine;
import liberty.math.functions;
import liberty.math.transform;
import liberty.math.vector;
import liberty.math.matrix;
import liberty.core.platform;
import liberty.time;
import liberty.camera.constants;
import liberty.camera.preset;
import liberty.scene.meta;
import liberty.scene.entity;
import liberty.scene.impl;
/**
* Represents the view of the observer.
* Everything that is rendered to the screen is processed within the projection matrix and view matrix of a camera.
* Inheriths $(D Entity) class and encapsulates $(D NodeBody) macro.
* It has a custom constructor that calls: $(D updateCameraVectors) and adds default $(D CameraPreset).
**/
final class Camera : Entity {
mixin NodeBody;
private {
float _yaw = -90.0f;
float _pitch = -30.0f;
float _zNear = 0.01f;
float _zFar = 1000.0f;
}
///
Vector3F frontVector = Vector3F.forward;
///
Vector3F upVector = Vector3F.up;
///
Vector3F rightVector = Vector3F.zero;
///
Vector3F worldUpVector = Vector3F.up;
///
float mouseSensitivity = 0.1f;
///
float fieldOfView = 45.0f;
///
float movementSpeed = 10.0f;
///
bool mouseMoveLocked;
///
bool mouseScrollLocked;
///
bool keyboardLocked;
///
bool constrainPitch = true;
///
CameraPreset preset;
/// Default camera constructor.
this(string id) {
super(id);
register;
updateCameraVectors;
preset = CameraPreset.getDefault;
component!Transform
.setLocation(0.0f, 3.0f, 4.0f);
}
/// Get camera yaw.
@property float yaw() { return _yaw; }
/// Get camera pitch.
@property float pitch() { return _pitch; }
/// Get camera zNear.
@property float zNear() { return _zNear; }
/// Get camera zFar.
@property float zFar() { return _zFar; }
/// Set camera yaw.
@property void yaw(float value) {
updateCameraVectors;
_yaw = value;
}
/// Set camera pitch.
@property void pitch(float value) {
checkPitchLimits;
updateCameraVectors;
_pitch = value;
}
/// Set camera zNear.
@property void zNear(float value) {
checkZNearLimits;
_zNear = value;
}
/// Set camera zFar.
@property void zFar(float value) {
checkZFarLimits;
_zFar = value;
}
/**
* Set keyboard listener using a camera movement direction.
* Works only if camera keyboard listener isn't locked.
* Returns reference to this so it can be used in a stream.
**/
typeof(this) processKeyboard(CameraMovement direction) {
if (!keyboardLocked) {
const float velocity = movementSpeed * Time.getDelta();
preset.runKeyboardProcess(this, direction, velocity);
}
return this;
}
/**
* Set mouse move listener using x and y offsets.
* Works only if camera mouse move listener isn't locked.
* If it works then it updates camera vectors at the end.
* Returns reference to this so it can be used in a stream.
**/
typeof(this) processMouseMovement(float xOffset, float yOffset) {
if (!mouseMoveLocked) {
xOffset *= mouseSensitivity;
yOffset *= mouseSensitivity;
_yaw += xOffset;
_pitch += yOffset;
checkPitchLimits;
updateCameraVectors;
}
return this;
}
/**
* Set mouse scroll listener using y offset.
* Works only if camera mouse scroll listener isn't locked.
* Returns reference to this so it can be used in a stream.
**/
typeof(this) processMouseScroll(float yOffset) {
if (!mouseScrollLocked) {
if (fieldOfView >= 1.0f && fieldOfView <= 45.0f)
fieldOfView -= yOffset;
if (fieldOfView <= 1.0f)
fieldOfView = 1.0f;
if (fieldOfView >= 45.0f)
fieldOfView = 45.0f;
}
return this;
}
/**
* Returns camera view matrix.
**/
Matrix4F viewMatrix() {
return Matrix4F.lookAt(
component!Transform.getLocation,
component!Transform.getLocation + frontVector,
upVector
);
}
/**
* Returns camera projection matrix.
**/
Matrix4F projectionMatrix() {
return Matrix4F.perspective(
fieldOfView.radians,
cast(float)Platform.getWindow.getWidth,
cast(float)Platform.getWindow.getHeight,
_zNear,
_zFar
);
}
private void updateCameraVectors() {
Vector3F front;
front.x = cos(radians(_yaw)) * cos(radians(_pitch));
front.y = sin(radians(_pitch));
front.z = sin(radians(_yaw)) * cos(radians(_pitch));
frontVector = front.normalized;
rightVector = cross(frontVector, worldUpVector).normalized;
upVector = cross(rightVector, frontVector).normalized;
}
pragma (inline, true)
private void checkPitchLimits() {
if (constrainPitch) {
if (_pitch > 89.0f)
_pitch = 89.0f;
if (_pitch < -89.0f)
_pitch = -89.0f;
}
}
pragma (inline, true)
private void checkZNearLimits() {
if (_zNear < 0.001f)
_zNear = 0.001f;
if (_zNear > 10_000.0f)
_zNear = 10_000.0f;
}
pragma (inline, true)
private void checkZFarLimits() {
if (_zFar < 0.001f)
_zFar = 0.0001f;
if (_zFar > 10_000.0f)
_zFar = 10_000.0f;
}
}