1 module raymath; 2 3 import raylib; 4 5 /********************************************************************************************** 6 * 7 * raymath v1.2 - Math functions to work with Vector3, Matrix and Quaternions 8 * 9 * CONFIGURATION: 10 * 11 * #define RAYMATH_IMPLEMENTATION 12 * Generates the implementation of the library into the included file. 13 * If not defined, the library is in header only mode and can be included in other headers 14 * or source files without problems. But only ONE file should hold the implementation. 15 * 16 * #define RAYMATH_HEADER_ONLY 17 * Define static inline functions code, so #include header suffices for use. 18 * This may use up lots of memory. 19 * 20 * #define RAYMATH_STANDALONE 21 * Avoid raylib.h header inclusion in this file. 22 * Vector3 and Matrix data types are defined internally in raymath module. 23 * 24 * 25 * LICENSE: zlib/libpng 26 * 27 * Copyright (c) 2015-2020 Ramon Santamaria (@raysan5) 28 * 29 * This software is provided "as-is", without any express or implied warranty. In no event 30 * will the authors be held liable for any damages arising from the use of this software. 31 * 32 * Permission is granted to anyone to use this software for any purpose, including commercial 33 * applications, and to alter it and redistribute it freely, subject to the following restrictions: 34 * 35 * 1. The origin of this software must not be misrepresented; you must not claim that you 36 * wrote the original software. If you use this software in a product, an acknowledgment 37 * in the product documentation would be appreciated but is not required. 38 * 39 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented 40 * as being the original software. 41 * 42 * 3. This notice may not be removed or altered from any source distribution. 43 * 44 **********************************************************************************************/ 45 46 extern (C): 47 48 //#define RAYMATH_STANDALONE // NOTE: To use raymath as standalone lib, just uncomment this line 49 //#define RAYMATH_HEADER_ONLY // NOTE: To compile functions as static inline, uncomment this line 50 51 // Required for structs: Vector3, Matrix 52 53 // We are building raylib as a Win32 shared library (.dll). 54 55 // We are using raylib as a Win32 shared library (.dll) 56 57 // Provide external definition 58 59 // Functions may be inlined, no external out-of-line definition 60 61 // plain inline not supported by tinycc (See issue #435) // Functions may be inlined or external definition used 62 63 //---------------------------------------------------------------------------------- 64 // Defines and Macros 65 //---------------------------------------------------------------------------------- 66 67 // Return float vector for Matrix 68 69 extern (D) auto MatrixToFloat(T)(auto ref T mat) 70 { 71 return MatrixToFloatV(mat).v; 72 } 73 74 // Return float vector for Vector3 75 76 extern (D) auto Vector3ToFloat(T)(auto ref T vec) 77 { 78 return Vector3ToFloatV(vec).v; 79 } 80 81 //---------------------------------------------------------------------------------- 82 // Types and Structures Definition 83 //---------------------------------------------------------------------------------- 84 85 // Vector2 type 86 87 // Vector3 type 88 89 // Quaternion type 90 91 // Matrix type (OpenGL style 4x4 - right handed, column major) 92 93 // NOTE: Helper types to be used instead of array return types for *ToFloat functions 94 struct float3 95 { 96 float[3] v; 97 } 98 99 struct float16 100 { 101 float[16] v; 102 } 103 104 // Required for: sinf(), cosf(), sqrtf(), tan(), fabs() 105 106 //---------------------------------------------------------------------------------- 107 // Module Functions Definition - Utils math 108 //---------------------------------------------------------------------------------- 109 110 // Clamp float value 111 float Clamp (float value, float min, float max); 112 113 // Calculate linear interpolation between two floats 114 float Lerp (float start, float end, float amount); 115 116 //---------------------------------------------------------------------------------- 117 // Module Functions Definition - Vector2 math 118 //---------------------------------------------------------------------------------- 119 120 // Vector with components value 0.0f 121 Vector2 Vector2Zero (); 122 123 // Vector with components value 1.0f 124 Vector2 Vector2One (); 125 126 // Add two vectors (v1 + v2) 127 Vector2 Vector2Add (Vector2 v1, Vector2 v2); 128 129 // Subtract two vectors (v1 - v2) 130 Vector2 Vector2Subtract (Vector2 v1, Vector2 v2); 131 132 // Calculate vector length 133 float Vector2Length (Vector2 v); 134 135 // Calculate two vectors dot product 136 float Vector2DotProduct (Vector2 v1, Vector2 v2); 137 138 // Calculate distance between two vectors 139 float Vector2Distance (Vector2 v1, Vector2 v2); 140 141 // Calculate angle from two vectors in X-axis 142 float Vector2Angle (Vector2 v1, Vector2 v2); 143 144 // Scale vector (multiply by value) 145 Vector2 Vector2Scale (Vector2 v, float scale); 146 147 // Multiply vector by vector 148 Vector2 Vector2MultiplyV (Vector2 v1, Vector2 v2); 149 150 // Negate vector 151 Vector2 Vector2Negate (Vector2 v); 152 153 // Divide vector by a float value 154 Vector2 Vector2Divide (Vector2 v, float div); 155 156 // Divide vector by vector 157 Vector2 Vector2DivideV (Vector2 v1, Vector2 v2); 158 159 // Normalize provided vector 160 Vector2 Vector2Normalize (Vector2 v); 161 162 // Calculate linear interpolation between two vectors 163 Vector2 Vector2Lerp (Vector2 v1, Vector2 v2, float amount); 164 165 // Rotate Vector by float in Degrees. 166 Vector2 Vector2Rotate (Vector2 v, float degs); 167 168 //---------------------------------------------------------------------------------- 169 // Module Functions Definition - Vector3 math 170 //---------------------------------------------------------------------------------- 171 172 // Vector with components value 0.0f 173 Vector3 Vector3Zero (); 174 175 // Vector with components value 1.0f 176 Vector3 Vector3One (); 177 178 // Add two vectors 179 Vector3 Vector3Add (Vector3 v1, Vector3 v2); 180 181 // Subtract two vectors 182 Vector3 Vector3Subtract (Vector3 v1, Vector3 v2); 183 184 // Multiply vector by scalar 185 Vector3 Vector3Scale (Vector3 v, float scalar); 186 187 // Multiply vector by vector 188 Vector3 Vector3Multiply (Vector3 v1, Vector3 v2); 189 190 // Calculate two vectors cross product 191 Vector3 Vector3CrossProduct (Vector3 v1, Vector3 v2); 192 193 // Calculate one vector perpendicular vector 194 Vector3 Vector3Perpendicular (Vector3 v); 195 196 // Calculate vector length 197 float Vector3Length (const Vector3 v); 198 199 // Calculate two vectors dot product 200 float Vector3DotProduct (Vector3 v1, Vector3 v2); 201 202 // Calculate distance between two vectors 203 float Vector3Distance (Vector3 v1, Vector3 v2); 204 205 // Negate provided vector (invert direction) 206 Vector3 Vector3Negate (Vector3 v); 207 208 // Divide vector by a float value 209 Vector3 Vector3Divide (Vector3 v, float div); 210 211 // Divide vector by vector 212 Vector3 Vector3DivideV (Vector3 v1, Vector3 v2); 213 214 // Normalize provided vector 215 Vector3 Vector3Normalize (Vector3 v); 216 217 // Orthonormalize provided vectors 218 // Makes vectors normalized and orthogonal to each other 219 // Gram-Schmidt function implementation 220 void Vector3OrthoNormalize (Vector3* v1, Vector3* v2); 221 222 // Transforms a Vector3 by a given Matrix 223 Vector3 Vector3Transform (Vector3 v, Matrix mat); 224 225 // Transform a vector by quaternion rotation 226 Vector3 Vector3RotateByQuaternion (Vector3 v, Quaternion q); 227 228 // Calculate linear interpolation between two vectors 229 Vector3 Vector3Lerp (Vector3 v1, Vector3 v2, float amount); 230 231 // Calculate reflected vector to normal 232 233 // I is the original vector 234 // N is the normal of the incident plane 235 // R = I - (2*N*( DotProduct[ I,N] )) 236 Vector3 Vector3Reflect (Vector3 v, Vector3 normal); 237 238 // Return min value for each pair of components 239 Vector3 Vector3Min (Vector3 v1, Vector3 v2); 240 241 // Return max value for each pair of components 242 Vector3 Vector3Max (Vector3 v1, Vector3 v2); 243 244 // Compute barycenter coordinates (u, v, w) for point p with respect to triangle (a, b, c) 245 // NOTE: Assumes P is on the plane of the triangle 246 247 //Vector v0 = b - a, v1 = c - a, v2 = p - a; 248 Vector3 Vector3Barycenter (Vector3 p, Vector3 a, Vector3 b, Vector3 c); 249 250 // Returns Vector3 as float array 251 float3 Vector3ToFloatV (Vector3 v); 252 253 //---------------------------------------------------------------------------------- 254 // Module Functions Definition - Matrix math 255 //---------------------------------------------------------------------------------- 256 257 // Compute matrix determinant 258 259 // Cache the matrix values (speed optimization) 260 float MatrixDeterminant (Matrix mat); 261 262 // Returns the trace of the matrix (sum of the values along the diagonal) 263 float MatrixTrace (Matrix mat); 264 265 // Transposes provided matrix 266 Matrix MatrixTranspose (Matrix mat); 267 268 // Invert provided matrix 269 270 // Cache the matrix values (speed optimization) 271 272 // Calculate the invert determinant (inlined to avoid double-caching) 273 Matrix MatrixInvert (Matrix mat); 274 275 // Normalize provided matrix 276 Matrix MatrixNormalize (Matrix mat); 277 278 // Returns identity matrix 279 Matrix MatrixIdentity (); 280 281 // Add two matrices 282 Matrix MatrixAdd (Matrix left, Matrix right); 283 284 // Subtract two matrices (left - right) 285 Matrix MatrixSubtract (Matrix left, Matrix right); 286 287 // Returns translation matrix 288 Matrix MatrixTranslate (float x, float y, float z); 289 290 // Create rotation matrix from axis and angle 291 // NOTE: Angle should be provided in radians 292 Matrix MatrixRotate (Vector3 axis, float angle); 293 294 // Returns xyz-rotation matrix (angles in radians) 295 Matrix MatrixRotateXYZ (Vector3 ang); 296 297 // Returns x-rotation matrix (angle in radians) 298 Matrix MatrixRotateX (float angle); 299 300 // Returns y-rotation matrix (angle in radians) 301 Matrix MatrixRotateY (float angle); 302 303 // Returns z-rotation matrix (angle in radians) 304 Matrix MatrixRotateZ (float angle); 305 306 // Returns scaling matrix 307 Matrix MatrixScale (float x, float y, float z); 308 309 // Returns two matrix multiplication 310 // NOTE: When multiplying matrices... the order matters! 311 Matrix MatrixMultiply (Matrix left, Matrix right); 312 313 // Returns perspective projection matrix 314 Matrix MatrixFrustum ( 315 double left, 316 double right, 317 double bottom, 318 double top, 319 double near, 320 double far); 321 322 // Returns perspective projection matrix 323 // NOTE: Angle should be provided in radians 324 Matrix MatrixPerspective (double fovy, double aspect, double near, double far); 325 326 // Returns orthographic projection matrix 327 Matrix MatrixOrtho ( 328 double left, 329 double right, 330 double bottom, 331 double top, 332 double near, 333 double far); 334 335 // Returns camera look-at matrix (view matrix) 336 Matrix MatrixLookAt (Vector3 eye, Vector3 target, Vector3 up); 337 338 // Returns float array of matrix data 339 float16 MatrixToFloatV (Matrix mat); 340 341 //---------------------------------------------------------------------------------- 342 // Module Functions Definition - Quaternion math 343 //---------------------------------------------------------------------------------- 344 345 // Returns identity quaternion 346 Quaternion QuaternionIdentity (); 347 348 // Computes the length of a quaternion 349 float QuaternionLength (Quaternion q); 350 351 // Normalize provided quaternion 352 Quaternion QuaternionNormalize (Quaternion q); 353 354 // Invert provided quaternion 355 Quaternion QuaternionInvert (Quaternion q); 356 357 // Calculate two quaternion multiplication 358 Quaternion QuaternionMultiply (Quaternion q1, Quaternion q2); 359 360 // Calculate linear interpolation between two quaternions 361 Quaternion QuaternionLerp (Quaternion q1, Quaternion q2, float amount); 362 363 // Calculate slerp-optimized interpolation between two quaternions 364 Quaternion QuaternionNlerp (Quaternion q1, Quaternion q2, float amount); 365 366 // Calculates spherical linear interpolation between two quaternions 367 Quaternion QuaternionSlerp (Quaternion q1, Quaternion q2, float amount); 368 369 // Calculate quaternion based on the rotation from one vector to another 370 371 // NOTE: Added QuaternioIdentity() 372 373 // Normalize to essentially nlerp the original and identity to 0.5 374 375 // Above lines are equivalent to: 376 //Quaternion result = QuaternionNlerp(q, QuaternionIdentity(), 0.5f); 377 Quaternion QuaternionFromVector3ToVector3 (Vector3 from, Vector3 to); 378 379 // Returns a quaternion for a given rotation matrix 380 Quaternion QuaternionFromMatrix (Matrix mat); 381 382 // Returns a matrix for a given quaternion 383 Matrix QuaternionToMatrix (Quaternion q); 384 385 // Returns rotation quaternion for an angle and axis 386 // NOTE: angle must be provided in radians 387 Quaternion QuaternionFromAxisAngle (Vector3 axis, float angle); 388 389 // Returns the rotation angle and axis for a given quaternion 390 391 // This occurs when the angle is zero. 392 // Not a problem: just set an arbitrary normalized axis. 393 void QuaternionToAxisAngle (Quaternion q, Vector3* outAxis, float* outAngle); 394 395 // Returns he quaternion equivalent to Euler angles 396 Quaternion QuaternionFromEuler (float roll, float pitch, float yaw); 397 398 // Return the Euler angles equivalent to quaternion (roll, pitch, yaw) 399 // NOTE: Angles are returned in a Vector3 struct in degrees 400 401 // roll (x-axis rotation) 402 403 // pitch (y-axis rotation) 404 405 // yaw (z-axis rotation) 406 Vector3 QuaternionToEuler (Quaternion q); 407 408 // Transform a quaternion given a transformation matrix 409 Quaternion QuaternionTransform (Quaternion q, Matrix mat); 410 411 // RAYMATH_H