raymath.d 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. module raylib.raymath;
  2. import raylib;
  3. /**********************************************************************************************
  4. *
  5. * raymath v1.5 - Math functions to work with Vector2, Vector3, Matrix and Quaternions
  6. *
  7. * CONFIGURATION:
  8. *
  9. * #define RAYMATH_IMPLEMENTATION
  10. * Generates the implementation of the library into the included file.
  11. * If not defined, the library is in header only mode and can be included in other headers
  12. * or source files without problems. But only ONE file should hold the implementation.
  13. *
  14. * #define RAYMATH_STATIC_INLINE
  15. * Define static inline functions code, so #include header suffices for use.
  16. * This may use up lots of memory.
  17. *
  18. * CONVENTIONS:
  19. *
  20. * - Functions are always self-contained, no function use another raymath function inside,
  21. * required code is directly re-implemented inside
  22. * - Functions input parameters are always received by value (2 unavoidable exceptions)
  23. * - Functions use always a "result" variable for return
  24. * - Functions are always defined inline
  25. * - Angles are always in radians (DEG2RAD/RAD2DEG macros provided for convenience)
  26. *
  27. *
  28. * LICENSE: zlib/libpng
  29. *
  30. * Copyright (c) 2015-2023 Ramon Santamaria (@raysan5)
  31. *
  32. * This software is provided "as-is", without any express or implied warranty. In no event
  33. * will the authors be held liable for any damages arising from the use of this software.
  34. *
  35. * Permission is granted to anyone to use this software for any purpose, including commercial
  36. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  37. *
  38. * 1. The origin of this software must not be misrepresented; you must not claim that you
  39. * wrote the original software. If you use this software in a product, an acknowledgment
  40. * in the product documentation would be appreciated but is not required.
  41. *
  42. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  43. * as being the original software.
  44. *
  45. * 3. This notice may not be removed or altered from any source distribution.
  46. *
  47. **********************************************************************************************/
  48. extern (C) @nogc nothrow:
  49. // Function specifiers definition
  50. // We are building raylib as a Win32 shared library (.dll).
  51. // We are using raylib as a Win32 shared library (.dll)
  52. // Provide external definition
  53. // Functions may be inlined, no external out-of-line definition
  54. // plain inline not supported by tinycc (See issue #435) // Functions may be inlined or external definition used
  55. //----------------------------------------------------------------------------------
  56. // Defines and Macros
  57. //----------------------------------------------------------------------------------
  58. enum PI = 3.14159265358979323846f;
  59. enum EPSILON = 0.000001f;
  60. enum DEG2RAD = PI / 180.0f;
  61. enum RAD2DEG = 180.0f / PI;
  62. // Get float vector for Matrix
  63. extern (D) auto MatrixToFloat(T)(auto ref T mat)
  64. {
  65. return MatrixToFloatV(mat).v;
  66. }
  67. // Get float vector for Vector3
  68. extern (D) auto Vector3ToFloat(T)(auto ref T vec)
  69. {
  70. return Vector3ToFloatV(vec).v;
  71. }
  72. //----------------------------------------------------------------------------------
  73. // Types and Structures Definition
  74. //----------------------------------------------------------------------------------
  75. // Vector2 type
  76. // Vector3 type
  77. // Vector4 type
  78. // Quaternion type
  79. // Matrix type (OpenGL style 4x4 - right handed, column major)
  80. // Matrix first row (4 components)
  81. // Matrix second row (4 components)
  82. // Matrix third row (4 components)
  83. // Matrix fourth row (4 components)
  84. // NOTE: Helper types to be used instead of array return types for *ToFloat functions
  85. struct float3
  86. {
  87. float[3] v;
  88. }
  89. struct float16
  90. {
  91. float[16] v;
  92. }
  93. // Required for: sinf(), cosf(), tan(), atan2f(), sqrtf(), floor(), fminf(), fmaxf(), fabs()
  94. //----------------------------------------------------------------------------------
  95. // Module Functions Definition - Utils math
  96. //----------------------------------------------------------------------------------
  97. // Clamp float value
  98. float Clamp(float value, float min, float max);
  99. // Calculate linear interpolation between two floats
  100. float Lerp(float start, float end, float amount);
  101. // Normalize input value within input range
  102. float Normalize(float value, float start, float end);
  103. // Remap input value within input range to output range
  104. float Remap(
  105. float value,
  106. float inputStart,
  107. float inputEnd,
  108. float outputStart,
  109. float outputEnd);
  110. // Wrap input value from min to max
  111. float Wrap(float value, float min, float max);
  112. // Check whether two given floats are almost equal
  113. int FloatEquals(float x, float y);
  114. //----------------------------------------------------------------------------------
  115. // Module Functions Definition - Vector2 math
  116. //----------------------------------------------------------------------------------
  117. // Vector with components value 0.0f
  118. Vector2 Vector2Zero();
  119. // Vector with components value 1.0f
  120. Vector2 Vector2One();
  121. // Add two vectors (v1 + v2)
  122. Vector2 Vector2Add(Vector2 v1, Vector2 v2);
  123. // Add vector and float value
  124. Vector2 Vector2AddValue(Vector2 v, float add);
  125. // Subtract two vectors (v1 - v2)
  126. Vector2 Vector2Subtract(Vector2 v1, Vector2 v2);
  127. // Subtract vector by float value
  128. Vector2 Vector2SubtractValue(Vector2 v, float sub);
  129. // Calculate vector length
  130. float Vector2Length(Vector2 v);
  131. // Calculate vector square length
  132. float Vector2LengthSqr(Vector2 v);
  133. // Calculate two vectors dot product
  134. float Vector2DotProduct(Vector2 v1, Vector2 v2);
  135. // Calculate distance between two vectors
  136. float Vector2Distance(Vector2 v1, Vector2 v2);
  137. // Calculate square distance between two vectors
  138. float Vector2DistanceSqr(Vector2 v1, Vector2 v2);
  139. // Calculate angle between two vectors
  140. // NOTE: Angle is calculated from origin point (0, 0)
  141. float Vector2Angle(Vector2 v1, Vector2 v2);
  142. // Calculate angle defined by a two vectors line
  143. // NOTE: Parameters need to be normalized
  144. // Current implementation should be aligned with glm::angle
  145. // Dot product
  146. // Clamp
  147. // Alternative implementation, more costly
  148. //float v1Length = sqrtf((start.x*start.x) + (start.y*start.y));
  149. //float v2Length = sqrtf((end.x*end.x) + (end.y*end.y));
  150. //float result = -acosf((start.x*end.x + start.y*end.y)/(v1Length*v2Length));
  151. float Vector2LineAngle(Vector2 start, Vector2 end);
  152. // Scale vector (multiply by value)
  153. Vector2 Vector2Scale(Vector2 v, float scale);
  154. // Multiply vector by vector
  155. Vector2 Vector2Multiply(Vector2 v1, Vector2 v2);
  156. // Negate vector
  157. Vector2 Vector2Negate(Vector2 v);
  158. // Divide vector by vector
  159. Vector2 Vector2Divide(Vector2 v1, Vector2 v2);
  160. // Normalize provided vector
  161. Vector2 Vector2Normalize(Vector2 v);
  162. // Transforms a Vector2 by a given Matrix
  163. Vector2 Vector2Transform(Vector2 v, Matrix mat);
  164. // Calculate linear interpolation between two vectors
  165. Vector2 Vector2Lerp(Vector2 v1, Vector2 v2, float amount);
  166. // Calculate reflected vector to normal
  167. // Dot product
  168. Vector2 Vector2Reflect(Vector2 v, Vector2 normal);
  169. // Rotate vector by angle
  170. Vector2 Vector2Rotate(Vector2 v, float angle);
  171. // Move Vector towards target
  172. Vector2 Vector2MoveTowards(Vector2 v, Vector2 target, float maxDistance);
  173. // Invert the given vector
  174. Vector2 Vector2Invert(Vector2 v);
  175. // Clamp the components of the vector between
  176. // min and max values specified by the given vectors
  177. Vector2 Vector2Clamp(Vector2 v, Vector2 min, Vector2 max);
  178. // Clamp the magnitude of the vector between two min and max values
  179. Vector2 Vector2ClampValue(Vector2 v, float min, float max);
  180. // Check whether two given vectors are almost equal
  181. int Vector2Equals(Vector2 p, Vector2 q);
  182. //----------------------------------------------------------------------------------
  183. // Module Functions Definition - Vector3 math
  184. //----------------------------------------------------------------------------------
  185. // Vector with components value 0.0f
  186. Vector3 Vector3Zero();
  187. // Vector with components value 1.0f
  188. Vector3 Vector3One();
  189. // Add two vectors
  190. Vector3 Vector3Add(Vector3 v1, Vector3 v2);
  191. // Add vector and float value
  192. Vector3 Vector3AddValue(Vector3 v, float add);
  193. // Subtract two vectors
  194. Vector3 Vector3Subtract(Vector3 v1, Vector3 v2);
  195. // Subtract vector by float value
  196. Vector3 Vector3SubtractValue(Vector3 v, float sub);
  197. // Multiply vector by scalar
  198. Vector3 Vector3Scale(Vector3 v, float scalar);
  199. // Multiply vector by vector
  200. Vector3 Vector3Multiply(Vector3 v1, Vector3 v2);
  201. // Calculate two vectors cross product
  202. Vector3 Vector3CrossProduct(Vector3 v1, Vector3 v2);
  203. // Calculate one vector perpendicular vector
  204. // Cross product between vectors
  205. Vector3 Vector3Perpendicular(Vector3 v);
  206. // Calculate vector length
  207. float Vector3Length(const Vector3 v);
  208. // Calculate vector square length
  209. float Vector3LengthSqr(const Vector3 v);
  210. // Calculate two vectors dot product
  211. float Vector3DotProduct(Vector3 v1, Vector3 v2);
  212. // Calculate distance between two vectors
  213. float Vector3Distance(Vector3 v1, Vector3 v2);
  214. // Calculate square distance between two vectors
  215. float Vector3DistanceSqr(Vector3 v1, Vector3 v2);
  216. // Calculate angle between two vectors
  217. float Vector3Angle(Vector3 v1, Vector3 v2);
  218. // Negate provided vector (invert direction)
  219. Vector3 Vector3Negate(Vector3 v);
  220. // Divide vector by vector
  221. Vector3 Vector3Divide(Vector3 v1, Vector3 v2);
  222. // Normalize provided vector
  223. Vector3 Vector3Normalize(Vector3 v);
  224. // Orthonormalize provided vectors
  225. // Makes vectors normalized and orthogonal to each other
  226. // Gram-Schmidt function implementation
  227. // Vector3Normalize(*v1);
  228. // Vector3CrossProduct(*v1, *v2)
  229. // Vector3Normalize(vn1);
  230. // Vector3CrossProduct(vn1, *v1)
  231. void Vector3OrthoNormalize(Vector3* v1, Vector3* v2);
  232. // Transforms a Vector3 by a given Matrix
  233. Vector3 Vector3Transform(Vector3 v, Matrix mat);
  234. // Transform a vector by quaternion rotation
  235. Vector3 Vector3RotateByQuaternion(Vector3 v, Quaternion q);
  236. // Rotates a vector around an axis
  237. // Using Euler-Rodrigues Formula
  238. // Ref.: https://en.wikipedia.org/w/index.php?title=Euler%E2%80%93Rodrigues_formula
  239. // Vector3Normalize(axis);
  240. // Vector3CrossProduct(w, v)
  241. // Vector3CrossProduct(w, wv)
  242. // Vector3Scale(wv, 2 * a)
  243. // Vector3Scale(wwv, 2)
  244. Vector3 Vector3RotateByAxisAngle(Vector3 v, Vector3 axis, float angle);
  245. // Calculate linear interpolation between two vectors
  246. Vector3 Vector3Lerp(Vector3 v1, Vector3 v2, float amount);
  247. // Calculate reflected vector to normal
  248. // I is the original vector
  249. // N is the normal of the incident plane
  250. // R = I - (2*N*(DotProduct[I, N]))
  251. Vector3 Vector3Reflect(Vector3 v, Vector3 normal);
  252. // Get min value for each pair of components
  253. Vector3 Vector3Min(Vector3 v1, Vector3 v2);
  254. // Get max value for each pair of components
  255. Vector3 Vector3Max(Vector3 v1, Vector3 v2);
  256. // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c)
  257. // NOTE: Assumes P is on the plane of the triangle
  258. // Vector3Subtract(b, a)
  259. // Vector3Subtract(c, a)
  260. // Vector3Subtract(p, a)
  261. // Vector3DotProduct(v0, v0)
  262. // Vector3DotProduct(v0, v1)
  263. // Vector3DotProduct(v1, v1)
  264. // Vector3DotProduct(v2, v0)
  265. // Vector3DotProduct(v2, v1)
  266. Vector3 Vector3Barycenter(Vector3 p, Vector3 a, Vector3 b, Vector3 c);
  267. // Projects a Vector3 from screen space into object space
  268. // NOTE: We are avoiding calling other raymath functions despite available
  269. // Calculate unprojected matrix (multiply view matrix by projection matrix) and invert it
  270. // MatrixMultiply(view, projection);
  271. // Calculate inverted matrix -> MatrixInvert(matViewProj);
  272. // Cache the matrix values (speed optimization)
  273. // Calculate the invert determinant (inlined to avoid double-caching)
  274. // Create quaternion from source point
  275. // Multiply quat point by unprojecte matrix
  276. // QuaternionTransform(quat, matViewProjInv)
  277. // Normalized world points in vectors
  278. Vector3 Vector3Unproject(Vector3 source, Matrix projection, Matrix view);
  279. // Get Vector3 as float array
  280. float3 Vector3ToFloatV(Vector3 v);
  281. // Invert the given vector
  282. Vector3 Vector3Invert(Vector3 v);
  283. // Clamp the components of the vector between
  284. // min and max values specified by the given vectors
  285. Vector3 Vector3Clamp(Vector3 v, Vector3 min, Vector3 max);
  286. // Clamp the magnitude of the vector between two values
  287. Vector3 Vector3ClampValue(Vector3 v, float min, float max);
  288. // Check whether two given vectors are almost equal
  289. int Vector3Equals(Vector3 p, Vector3 q);
  290. // Compute the direction of a refracted ray where v specifies the
  291. // normalized direction of the incoming ray, n specifies the
  292. // normalized normal vector of the interface of two optical media,
  293. // and r specifies the ratio of the refractive index of the medium
  294. // from where the ray comes to the refractive index of the medium
  295. // on the other side of the surface
  296. Vector3 Vector3Refract(Vector3 v, Vector3 n, float r);
  297. //----------------------------------------------------------------------------------
  298. // Module Functions Definition - Matrix math
  299. //----------------------------------------------------------------------------------
  300. // Compute matrix determinant
  301. // Cache the matrix values (speed optimization)
  302. float MatrixDeterminant(Matrix mat);
  303. // Get the trace of the matrix (sum of the values along the diagonal)
  304. float MatrixTrace(Matrix mat);
  305. // Transposes provided matrix
  306. Matrix MatrixTranspose(Matrix mat);
  307. // Invert provided matrix
  308. // Cache the matrix values (speed optimization)
  309. // Calculate the invert determinant (inlined to avoid double-caching)
  310. Matrix MatrixInvert(Matrix mat);
  311. // Get identity matrix
  312. Matrix MatrixIdentity();
  313. // Add two matrices
  314. Matrix MatrixAdd(Matrix left, Matrix right);
  315. // Subtract two matrices (left - right)
  316. Matrix MatrixSubtract(Matrix left, Matrix right);
  317. // Get two matrix multiplication
  318. // NOTE: When multiplying matrices... the order matters!
  319. Matrix MatrixMultiply(Matrix left, Matrix right);
  320. // Get translation matrix
  321. Matrix MatrixTranslate(float x, float y, float z);
  322. // Create rotation matrix from axis and angle
  323. // NOTE: Angle should be provided in radians
  324. Matrix MatrixRotate(Vector3 axis, float angle);
  325. // Get x-rotation matrix
  326. // NOTE: Angle must be provided in radians
  327. // MatrixIdentity()
  328. Matrix MatrixRotateX(float angle);
  329. // Get y-rotation matrix
  330. // NOTE: Angle must be provided in radians
  331. // MatrixIdentity()
  332. Matrix MatrixRotateY(float angle);
  333. // Get z-rotation matrix
  334. // NOTE: Angle must be provided in radians
  335. // MatrixIdentity()
  336. Matrix MatrixRotateZ(float angle);
  337. // Get xyz-rotation matrix
  338. // NOTE: Angle must be provided in radians
  339. // MatrixIdentity()
  340. Matrix MatrixRotateXYZ(Vector3 angle);
  341. // Get zyx-rotation matrix
  342. // NOTE: Angle must be provided in radians
  343. Matrix MatrixRotateZYX(Vector3 angle);
  344. // Get scaling matrix
  345. Matrix MatrixScale(float x, float y, float z);
  346. // Get perspective projection matrix
  347. Matrix MatrixFrustum(
  348. double left,
  349. double right,
  350. double bottom,
  351. double top,
  352. double near,
  353. double far);
  354. // Get perspective projection matrix
  355. // NOTE: Fovy angle must be provided in radians
  356. // MatrixFrustum(-right, right, -top, top, near, far);
  357. Matrix MatrixPerspective(double fovy, double aspect, double near, double far);
  358. // Get orthographic projection matrix
  359. Matrix MatrixOrtho(
  360. double left,
  361. double right,
  362. double bottom,
  363. double top,
  364. double near,
  365. double far);
  366. // Get camera look-at matrix (view matrix)
  367. // Vector3Subtract(eye, target)
  368. // Vector3Normalize(vz)
  369. // Vector3CrossProduct(up, vz)
  370. // Vector3Normalize(x)
  371. // Vector3CrossProduct(vz, vx)
  372. // Vector3DotProduct(vx, eye)
  373. // Vector3DotProduct(vy, eye)
  374. // Vector3DotProduct(vz, eye)
  375. Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up);
  376. // Get float array of matrix data
  377. float16 MatrixToFloatV(Matrix mat);
  378. //----------------------------------------------------------------------------------
  379. // Module Functions Definition - Quaternion math
  380. //----------------------------------------------------------------------------------
  381. // Add two quaternions
  382. Quaternion QuaternionAdd(Quaternion q1, Quaternion q2);
  383. // Add quaternion and float value
  384. Quaternion QuaternionAddValue(Quaternion q, float add);
  385. // Subtract two quaternions
  386. Quaternion QuaternionSubtract(Quaternion q1, Quaternion q2);
  387. // Subtract quaternion and float value
  388. Quaternion QuaternionSubtractValue(Quaternion q, float sub);
  389. // Get identity quaternion
  390. Quaternion QuaternionIdentity();
  391. // Computes the length of a quaternion
  392. float QuaternionLength(Quaternion q);
  393. // Normalize provided quaternion
  394. Quaternion QuaternionNormalize(Quaternion q);
  395. // Invert provided quaternion
  396. Quaternion QuaternionInvert(Quaternion q);
  397. // Calculate two quaternion multiplication
  398. Quaternion QuaternionMultiply(Quaternion q1, Quaternion q2);
  399. // Scale quaternion by float value
  400. Quaternion QuaternionScale(Quaternion q, float mul);
  401. // Divide two quaternions
  402. Quaternion QuaternionDivide(Quaternion q1, Quaternion q2);
  403. // Calculate linear interpolation between two quaternions
  404. Quaternion QuaternionLerp(Quaternion q1, Quaternion q2, float amount);
  405. // Calculate slerp-optimized interpolation between two quaternions
  406. // QuaternionLerp(q1, q2, amount)
  407. // QuaternionNormalize(q);
  408. Quaternion QuaternionNlerp(Quaternion q1, Quaternion q2, float amount);
  409. // Calculates spherical linear interpolation between two quaternions
  410. Quaternion QuaternionSlerp(Quaternion q1, Quaternion q2, float amount);
  411. // Calculate quaternion based on the rotation from one vector to another
  412. // Vector3DotProduct(from, to)
  413. // Vector3CrossProduct(from, to)
  414. // QuaternionNormalize(q);
  415. // NOTE: Normalize to essentially nlerp the original and identity to 0.5
  416. Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to);
  417. // Get a quaternion for a given rotation matrix
  418. Quaternion QuaternionFromMatrix(Matrix mat);
  419. // Get a matrix for a given quaternion
  420. // MatrixIdentity()
  421. Matrix QuaternionToMatrix(Quaternion q);
  422. // Get rotation quaternion for an angle and axis
  423. // NOTE: Angle must be provided in radians
  424. // Vector3Normalize(axis)
  425. // QuaternionNormalize(q);
  426. Quaternion QuaternionFromAxisAngle(Vector3 axis, float angle);
  427. // Get the rotation angle and axis for a given quaternion
  428. // QuaternionNormalize(q);
  429. // This occurs when the angle is zero.
  430. // Not a problem: just set an arbitrary normalized axis.
  431. void QuaternionToAxisAngle(Quaternion q, Vector3* outAxis, float* outAngle);
  432. // Get the quaternion equivalent to Euler angles
  433. // NOTE: Rotation order is ZYX
  434. Quaternion QuaternionFromEuler(float pitch, float yaw, float roll);
  435. // Get the Euler angles equivalent to quaternion (roll, pitch, yaw)
  436. // NOTE: Angles are returned in a Vector3 struct in radians
  437. // Roll (x-axis rotation)
  438. // Pitch (y-axis rotation)
  439. // Yaw (z-axis rotation)
  440. Vector3 QuaternionToEuler(Quaternion q);
  441. // Transform a quaternion given a transformation matrix
  442. Quaternion QuaternionTransform(Quaternion q, Matrix mat);
  443. // Check whether two given quaternions are almost equal
  444. int QuaternionEquals(Quaternion p, Quaternion q);
  445. // RAYMATH_H