rcamera.d 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. import raylib;
  44. extern (C) @nogc nothrow:
  45. //----------------------------------------------------------------------------------
  46. // Defines and Macros
  47. //----------------------------------------------------------------------------------
  48. // Function specifiers definition // Functions defined as 'extern' by default (implicit specifiers)
  49. enum CAMERA_CULL_DISTANCE_NEAR = RL_CULL_DISTANCE_NEAR;
  50. enum CAMERA_CULL_DISTANCE_FAR = RL_CULL_DISTANCE_FAR;
  51. //----------------------------------------------------------------------------------
  52. // Types and Structures Definition
  53. // NOTE: Below types are required for CAMERA_STANDALONE usage
  54. //----------------------------------------------------------------------------------
  55. // Vector2, 2 components
  56. // Vector x component
  57. // Vector y component
  58. // Vector3, 3 components
  59. // Vector x component
  60. // Vector y component
  61. // Vector z component
  62. // Camera type, defines a camera position/orientation in 3d space
  63. // Camera position
  64. // Camera target it looks-at
  65. // Camera up vector (rotation over its axis)
  66. // Camera field-of-view apperture in Y (degrees) in perspective, used as near plane width in orthographic
  67. // Camera projection type: CAMERA_PERSPECTIVE or CAMERA_ORTHOGRAPHIC
  68. // Camera type fallback, defaults to Camera3D
  69. // Camera projection
  70. // Perspective projection
  71. // Orthographic projection
  72. // Camera system modes
  73. // Camera custom, controlled by user (UpdateCamera() does nothing)
  74. // Camera free mode
  75. // Camera orbital, around target, zoom supported
  76. // Camera first person
  77. // Camera third person
  78. //----------------------------------------------------------------------------------
  79. // Global Variables Definition
  80. //----------------------------------------------------------------------------------
  81. //...
  82. //----------------------------------------------------------------------------------
  83. // Module Functions Declaration
  84. //----------------------------------------------------------------------------------
  85. // Prevents name mangling of functions
  86. Vector3 GetCameraForward(Camera* camera);
  87. Vector3 GetCameraUp(Camera* camera);
  88. Vector3 GetCameraRight(Camera* camera);
  89. // Camera movement
  90. void CameraMoveForward(Camera* camera, float distance, bool moveInWorldPlane);
  91. void CameraMoveUp(Camera* camera, float distance);
  92. void CameraMoveRight(Camera* camera, float distance, bool moveInWorldPlane);
  93. void CameraMoveToTarget(Camera* camera, float delta);
  94. // Camera rotation
  95. void CameraYaw(Camera* camera, float angle, bool rotateAroundTarget);
  96. void CameraPitch(Camera* camera, float angle, bool lockView, bool rotateAroundTarget, bool rotateUp);
  97. void CameraRoll(Camera* camera, float angle);
  98. Matrix GetCameraViewMatrix(Camera* camera);
  99. Matrix GetCameraProjectionMatrix(Camera* camera, float aspect);
  100. // CAMERA_H
  101. /***********************************************************************************
  102. *
  103. * CAMERA IMPLEMENTATION
  104. *
  105. ************************************************************************************/
  106. // Required for vector maths:
  107. // Vector3Add()
  108. // Vector3Subtract()
  109. // Vector3Scale()
  110. // Vector3Normalize()
  111. // Vector3Distance()
  112. // Vector3CrossProduct()
  113. // Vector3RotateByAxisAngle()
  114. // Vector3Angle()
  115. // Vector3Negate()
  116. // MatrixLookAt()
  117. // MatrixPerspective()
  118. // MatrixOrtho()
  119. // MatrixIdentity()
  120. // raylib required functionality:
  121. // GetMouseDelta()
  122. // GetMouseWheelMove()
  123. // IsKeyDown()
  124. // IsKeyPressed()
  125. // GetFrameTime()
  126. //----------------------------------------------------------------------------------
  127. // Defines and Macros
  128. //----------------------------------------------------------------------------------
  129. // Camera mouse movement sensitivity
  130. // TODO: it should be independant of framerate
  131. // Radians per second
  132. // PLAYER (used by camera)
  133. //----------------------------------------------------------------------------------
  134. // Types and Structures Definition
  135. //----------------------------------------------------------------------------------
  136. //...
  137. //----------------------------------------------------------------------------------
  138. // Global Variables Definition
  139. //----------------------------------------------------------------------------------
  140. //...
  141. //----------------------------------------------------------------------------------
  142. // Module specific Functions Declaration
  143. //----------------------------------------------------------------------------------
  144. //...
  145. //----------------------------------------------------------------------------------
  146. // Module Functions Definition
  147. //----------------------------------------------------------------------------------
  148. // Returns the cameras forward vector (normalized)
  149. // Returns the cameras up vector (normalized)
  150. // Note: The up vector might not be perpendicular to the forward vector
  151. // Returns the cameras right vector (normalized)
  152. // Moves the camera in its forward direction
  153. // Project vector onto world plane
  154. // Scale by distance
  155. // Move position and target
  156. // Moves the camera in its up direction
  157. // Scale by distance
  158. // Move position and target
  159. // Moves the camera target in its current right direction
  160. // Project vector onto world plane
  161. // Scale by distance
  162. // Move position and target
  163. // Moves the camera position closer/farther to/from the camera target
  164. // Apply delta
  165. // Distance must be greater than 0
  166. // Set new distance by moving the position along the forward vector
  167. // Rotates the camera around its up vector
  168. // Yaw is "looking left and right"
  169. // If rotateAroundTarget is false, the camera rotates around its position
  170. // Note: angle must be provided in radians
  171. // Rotation axis
  172. // View vector
  173. // Rotate view vector around up axis
  174. // Move position relative to target
  175. // rotate around camera.position
  176. // Move target relative to position
  177. // Rotates the camera around its right vector, pitch is "looking up and down"
  178. // - lockView prevents camera overrotation (aka "somersaults")
  179. // - rotateAroundTarget defines if rotation is around target or around its position
  180. // - rotateUp rotates the up direction as well (typically only usefull in CAMERA_FREE)
  181. // NOTE: angle must be provided in radians
  182. // Up direction
  183. // View vector
  184. // In these camera modes we clamp the Pitch angle
  185. // to allow only viewing straight up or down.
  186. // Clamp view up
  187. // avoid numerical errors
  188. // Clamp view down
  189. // downwards angle is negative
  190. // avoid numerical errors
  191. // Rotation axis
  192. // Rotate view vector around right axis
  193. // Move position relative to target
  194. // rotate around camera.position
  195. // Move target relative to position
  196. // Rotate up direction around right axis
  197. // Rotates the camera around its forward vector
  198. // Roll is "turning your head sideways to the left or right"
  199. // Note: angle must be provided in radians
  200. // Rotation axis
  201. // Rotate up direction around forward axis
  202. // Returns the camera view matrix
  203. // Returns the camera projection matrix
  204. // Update camera position for selected mode
  205. // Camera mode: CAMERA_FREE, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON, CAMERA_ORBITAL or CUSTOM
  206. // Orbital can just orbit
  207. // Camera rotation
  208. // Camera movement
  209. //if (IsKeyDown(KEY_SPACE)) CameraMoveUp(camera, CAMERA_MOVE_SPEED);
  210. //if (IsKeyDown(KEY_LEFT_CONTROL)) CameraMoveUp(camera, -CAMERA_MOVE_SPEED);
  211. // Zoom target distance
  212. // !CAMERA_STANDALONE
  213. // Update camera movement, movement/rotation values should be provided by user
  214. // Required values
  215. // movement.x - Move forward/backward
  216. // movement.y - Move right/left
  217. // movement.z - Move up/down
  218. // rotation.x - yaw
  219. // rotation.y - pitch
  220. // rotation.z - roll
  221. // zoom - Move towards target
  222. // Camera rotation
  223. // Camera movement
  224. // Zoom target distance
  225. // CAMERA_IMPLEMENTATION