1 /*******************************************************************************************
2 *
3 *   rcamera - Basic camera system with support for multiple camera modes
4 *
5 *   CONFIGURATION:
6 *
7 *   #define CAMERA_IMPLEMENTATION
8 *       Generates the implementation of the library into the included file.
9 *       If not defined, the library is in header only mode and can be included in other headers
10 *       or source files without problems. But only ONE file should hold the implementation.
11 *
12 *   #define CAMERA_STANDALONE
13 *       If defined, the library can be used as standalone as a camera system but some
14 *       functions must be redefined to manage inputs accordingly.
15 *
16 *   CONTRIBUTORS:
17 *       Ramon Santamaria:   Supervision, review, update and maintenance
18 *       Christoph Wagner:   Complete redesign, using raymath (2022)
19 *       Marc Palau:         Initial implementation (2014)
20 *
21 *
22 *   LICENSE: zlib/libpng
23 *
24 *   Copyright (c) 2022-2023 Christoph Wagner (@Crydsch) & Ramon Santamaria (@raysan5)
25 *
26 *   This software is provided "as-is", without any express or implied warranty. In no event
27 *   will the authors be held liable for any damages arising from the use of this software.
28 *
29 *   Permission is granted to anyone to use this software for any purpose, including commercial
30 *   applications, and to alter it and redistribute it freely, subject to the following restrictions:
31 *
32 *     1. The origin of this software must not be misrepresented; you must not claim that you
33 *     wrote the original software. If you use this software in a product, an acknowledgment
34 *     in the product documentation would be appreciated but is not required.
35 *
36 *     2. Altered source versions must be plainly marked as such, and must not be misrepresented
37 *     as being the original software.
38 *
39 *     3. This notice may not be removed or altered from any source distribution.
40 *
41 **********************************************************************************************/
42 module raylib.rcamera;
43 
44 import raylib;
45 
46 extern (C) @nogc nothrow:
47 
48 //----------------------------------------------------------------------------------
49 // Defines and Macros
50 //----------------------------------------------------------------------------------
51 // Function specifiers definition // Functions defined as 'extern' by default (implicit specifiers)
52 
53 enum CAMERA_CULL_DISTANCE_NEAR = RL_CULL_DISTANCE_NEAR;
54 enum CAMERA_CULL_DISTANCE_FAR = RL_CULL_DISTANCE_FAR;
55 
56 //----------------------------------------------------------------------------------
57 // Types and Structures Definition
58 // NOTE: Below types are required for CAMERA_STANDALONE usage
59 //----------------------------------------------------------------------------------
60 
61 // Vector2, 2 components
62 
63 // Vector x component
64 // Vector y component
65 
66 // Vector3, 3 components
67 
68 // Vector x component
69 // Vector y component
70 // Vector z component
71 
72 // Camera type, defines a camera position/orientation in 3d space
73 
74 // Camera position
75 // Camera target it looks-at
76 // Camera up vector (rotation over its axis)
77 // Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic
78 // Camera projection type: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
79 
80 // Camera type fallback, defaults to Camera3D
81 
82 // Camera projection
83 
84 // Perspective projection
85 // Orthographic projection
86 
87 // Camera system modes
88 
89 // Camera custom, controlled by user (UpdateCamera() does nothing)
90 // Camera free mode
91 // Camera orbital, around target, zoom supported
92 // Camera first person
93 // Camera third person
94 
95 //----------------------------------------------------------------------------------
96 // Global Variables Definition
97 //----------------------------------------------------------------------------------
98 //...
99 
100 //----------------------------------------------------------------------------------
101 // Module Functions Declaration
102 //----------------------------------------------------------------------------------
103 
104 // Prevents name mangling of functions
105 
106 Vector3 GetCameraForward(Camera* camera);
107 Vector3 GetCameraUp(Camera* camera);
108 Vector3 GetCameraRight(Camera* camera);
109 
110 // Camera movement
111 void CameraMoveForward(Camera* camera, float distance, bool moveInWorldPlane);
112 void CameraMoveUp(Camera* camera, float distance);
113 void CameraMoveRight(Camera* camera, float distance, bool moveInWorldPlane);
114 void CameraMoveToTarget(Camera* camera, float delta);
115 
116 // Camera rotation
117 void CameraYaw(Camera* camera, float angle, bool rotateAroundTarget);
118 void CameraPitch(Camera* camera, float angle, bool lockView, bool rotateAroundTarget, bool rotateUp);
119 void CameraRoll(Camera* camera, float angle);
120 
121 Matrix GetCameraViewMatrix(Camera* camera);
122 Matrix GetCameraProjectionMatrix(Camera* camera, float aspect);
123 
124 // CAMERA_H
125 
126 /***********************************************************************************
127 *
128 *   CAMERA IMPLEMENTATION
129 *
130 ************************************************************************************/
131 
132 // Required for vector maths:
133 // Vector3Add()
134 // Vector3Subtract()
135 // Vector3Scale()
136 // Vector3Normalize()
137 // Vector3Distance()
138 // Vector3CrossProduct()
139 // Vector3RotateByAxisAngle()
140 // Vector3Angle()
141 // Vector3Negate()
142 // MatrixLookAt()
143 // MatrixPerspective()
144 // MatrixOrtho()
145 // MatrixIdentity()
146 
147 // raylib required functionality:
148 // GetMouseDelta()
149 // GetMouseWheelMove()
150 // IsKeyDown()
151 // IsKeyPressed()
152 // GetFrameTime()
153 
154 //----------------------------------------------------------------------------------
155 // Defines and Macros
156 //----------------------------------------------------------------------------------
157 
158 // Camera mouse movement sensitivity
159 // TODO: it should be independant of framerate
160 
161 // Radians per second
162 
163 // PLAYER (used by camera)
164 
165 //----------------------------------------------------------------------------------
166 // Types and Structures Definition
167 //----------------------------------------------------------------------------------
168 //...
169 
170 //----------------------------------------------------------------------------------
171 // Global Variables Definition
172 //----------------------------------------------------------------------------------
173 //...
174 
175 //----------------------------------------------------------------------------------
176 // Module specific Functions Declaration
177 //----------------------------------------------------------------------------------
178 //...
179 
180 //----------------------------------------------------------------------------------
181 // Module Functions Definition
182 //----------------------------------------------------------------------------------
183 // Returns the cameras forward vector (normalized)
184 
185 // Returns the cameras up vector (normalized)
186 // Note: The up vector might not be perpendicular to the forward vector
187 
188 // Returns the cameras right vector (normalized)
189 
190 // Moves the camera in its forward direction
191 
192 // Project vector onto world plane
193 
194 // Scale by distance
195 
196 // Move position and target
197 
198 // Moves the camera in its up direction
199 
200 // Scale by distance
201 
202 // Move position and target
203 
204 // Moves the camera target in its current right direction
205 
206 // Project vector onto world plane
207 
208 // Scale by distance
209 
210 // Move position and target
211 
212 // Moves the camera position closer/farther to/from the camera target
213 
214 // Apply delta
215 
216 // Distance must be greater than 0
217 
218 // Set new distance by moving the position along the forward vector
219 
220 // Rotates the camera around its up vector
221 // Yaw is "looking left and right"
222 // If rotateAroundTarget is false, the camera rotates around its position
223 // Note: angle must be provided in radians
224 
225 // Rotation axis
226 
227 // View vector
228 
229 // Rotate view vector around up axis
230 
231 // Move position relative to target
232 
233 // rotate around camera.position
234 
235 // Move target relative to position
236 
237 // Rotates the camera around its right vector, pitch is "looking up and down"
238 //  - lockView prevents camera overrotation (aka "somersaults")
239 //  - rotateAroundTarget defines if rotation is around target or around its position
240 //  - rotateUp rotates the up direction as well (typically only usefull in CAMERA_FREE)
241 // NOTE: angle must be provided in radians
242 
243 // Up direction
244 
245 // View vector
246 
247 // In these camera modes we clamp the Pitch angle
248 // to allow only viewing straight up or down.
249 
250 // Clamp view up
251 
252 // avoid numerical errors
253 
254 // Clamp view down
255 
256 // downwards angle is negative
257 // avoid numerical errors
258 
259 // Rotation axis
260 
261 // Rotate view vector around right axis
262 
263 // Move position relative to target
264 
265 // rotate around camera.position
266 
267 // Move target relative to position
268 
269 // Rotate up direction around right axis
270 
271 // Rotates the camera around its forward vector
272 // Roll is "turning your head sideways to the left or right"
273 // Note: angle must be provided in radians
274 
275 // Rotation axis
276 
277 // Rotate up direction around forward axis
278 
279 // Returns the camera view matrix
280 
281 // Returns the camera projection matrix
282 
283 // Update camera position for selected mode
284 // Camera mode: CAMERA_FREE, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON, CAMERA_ORBITAL or CUSTOM
285 
286 // Orbital can just orbit
287 
288 // Camera rotation
289 
290 // Camera movement
291 
292 //if (IsKeyDown(KEY_SPACE)) CameraMoveUp(camera, CAMERA_MOVE_SPEED);
293 //if (IsKeyDown(KEY_LEFT_CONTROL)) CameraMoveUp(camera, -CAMERA_MOVE_SPEED);
294 
295 // Zoom target distance
296 
297 // !CAMERA_STANDALONE
298 
299 // Update camera movement, movement/rotation values should be provided by user
300 
301 // Required values
302 // movement.x - Move forward/backward
303 // movement.y - Move right/left
304 // movement.z - Move up/down
305 // rotation.x - yaw
306 // rotation.y - pitch
307 // rotation.z - roll
308 // zoom - Move towards target
309 
310 // Camera rotation
311 
312 // Camera movement
313 
314 // Zoom target distance
315 
316 // CAMERA_IMPLEMENTATION