chickadee » gl-math

gl-math

A small math library aimed at gamedev that provides 4x4 float matrix, vector, and quaternion operations. Uses the hypermath library.

Requirements

Documentation

gl-math provides a number of functions for working with 4x4 matrices (plus a handful of others). The functionality is similar to what can be found in the glm egg, but with some notable differences:

Additionally, gl-math is one fifth the compiled size of glm, has a more straight-forward code-base, and complete documentation.

gl-math expects matrices, vectors, and quaternions to be f32vectors or pointers. f32vectors must be 16 elements long, 3 elements long, or 4 elements long for matrices, vectors, or quaternions, respectively. The memory pointed to should likewise be an array of 16, 3, or 4 floats . If a function accepts more than one matrix, vector, or quaternion, all must be of the same type.

gl-math operates on matrices in a column-major fashion in correspondence with OpenGL (e.g. translation components are at indices 12, 13, and 14). Vectors are arranged as ((x y z)), and quaternions as ((x y z w)).

Matrix operations

(print-mat4 MATRIX) procedure

Prints the given MATRIX to (current-output-port).

(copy-mat4 MATRIX [RESULT]) procedure

Make a copy of MATRIX. If the matrix RESULT is given, it will be modified to contain the contents of MATRIX. If RESULT is #t, MATRIX must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, MATRIX must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory.

(m* A B [RESULT]) procedure

Multiply matrix A by matrix B. If the matrix RESULT is given, it will be modified to contain the results of the multiplication. If RESULT is #t, A and B must be f32vectors and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, A and B must be f32vectors and the returned value will be an f32vector located in normal garbage collected memory.

(m*s A S [RESULT]) procedure

Multiply matrix A by scalar S. If the matrix RESULT is given, it will be modified to contain the results of the multiplication. If RESULT is #t, A must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, A must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory.

(m+ A B [RESULT]) procedure

Add matrix A with matrix B. If the matrix RESULT is given, it will be modified to contain the results of the multiplication. If RESULT is #t, A and B must be f32vectors and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, A and B must be f32vectors and the returned value will be an f32vector located in normal garbage collected memory.

(m- A B [RESULT]) procedure

Subtract matrix B from matrix A. If the matrix RESULT is given, it will be modified to contain the results of the multiplication. If RESULT is #t, A and B must be f32vectors and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, A and B must be f32vectors and the returned value will be an f32vector located in normal garbage collected memory.

(mat4-identity [RESULT]) procedure

Return an identity matrix. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(translation VECTOR [RESULT]) procedure

Return the translation matrix given by VECTOR. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(translate VECTOR MATRIX) procedure

Translate MATRIX by VECTOR.

(x-rotation ANGLE [RESULT]) procedure

Return the rotation matrix given by a rotation of ANGLE radians around the x-axis. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(rotate-x ANGLE MATRIX) procedure

Rotate MATRIX around the x-axis by ANGLE radians.

(y-rotation ANGLE [RESULT]) procedure

Return the rotation matrix given by a rotation of ANGLE radians around the y-axis. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(rotate-y ANGLE MATRIX) procedure

Rotate MATRIX around the y-axis by ANGLE radians.

(z-rotation ANGLE [RESULT]) procedure

Return the rotation matrix given by a rotation of ANGLE radians around the z-axis. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(rotate-z ANGLE MATRIX) procedure

Rotate MATRIX around the z-axis by ANGLE radians.

(axis-angle-rotation AXIS ANGLE [RESULT]) procedure

Return the rotation matrix given by a rotation of ANGLE radians around the vector AXIS. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(rotate-axis-angle AXIS ANGLE MATRIX) procedure

Rotate MATRIX around the vector AXIS by ANGLE radians.

(quaternion-rotation Q [RESULT]) procedure

Return the rotation matrix given by the quaternion Q. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(rotate-quaternion Q MATRIX) procedure

Rotate MATRIX by the quaternion Q.

(ypr-rotation YAW PITCH ROLL [RESULT]) procedure

Return the rotation matrix given by rotating by ROLL radians around the z-axis followed by PITCH radians around the x-axis followed by YAW radians around the y-axis. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(rotate-ypr YAW PITCH ROLL MATRIX) procedure

Rotate MATRIX by ROLL radians around the z-axis followed by PITCH radians around the x-axis followed by YAW radians around the y-axis.

(2d-scaling SCALE-X SCALE-Y [RESULT]) procedure

Return the matrix created by scaling the x and y axes by SCALE-X and SCALE-Y. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(scale-2d SCALE-X SCALE-Y MATRIX) procedure

Scale the x and y axis of MATRIX by SCALE-X and SCALE-Y.

(3d-scaling SCALE-X SCALE-Y SCALE-Z [RESULT]) procedure

Return the matrix created by scaling the x, y and z axes by SCALE-X, SCALE-Y, and SCALE-Z. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(scale-3d SCALE-X SCALE-Y SCALE-Z MATRIX) procedure

Scale the x, y, and z axis of MATRIX by SCALE-X, SCALE-Y, and SCALE-Z.

(scaling SCALE [RESULT]) procedure

Return the matrix created by scaling the x, y and z axes by SCALE. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(scale SCALE MATRIX) procedure

Scale the x, y, and z axis of MATRIX by SCALE.

(flip-x MATRIX) procedure

Flip (mirror) MATRIX along the x-axis.

(flip-y MATRIX) procedure

Flip (mirror) MATRIX along the y-axis.

(flip-z MATRIX) procedure

Flip (mirror) MATRIX along the z-axis.

(translate-rotate-scale-2d VECTOR ANGLE SCALE [RESULT]) procedure

Efficiently create a matrix translated by VECTOR, rotated around the z-axis by ANGLE radians, then scaled by SCALE. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(transpose MATRIX [RESULT]) procedure

Transpose MATRIX. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, MATRIX must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, MATRIX must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory.

(inverse MATRIX [RESULT]) procedure

Invert MATRIX. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, MATRIX must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, MATRIX must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory.

(fast-inverse-transpose MATRIX [RESULT]) procedure

Efficiently inverse the transpose the unscaled MATRIX. If MATRIX has been scaled, this will produce incorrect results: inverse then transpose should be used instead. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, MATRIX must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, MATRIX must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory.

Projection matrices

(ortho WIDTH HEIGHT NEAR FAR [RESULT]) procedure

Create an orthographic projection matrix. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(ortho-viewport LEFT RIGHT NEAR FAR VIEWPORT-LEFT VIEWPORT-RIGHT VIEWPORT-BOTTOM VIEWPORT-TOP [RESULT]) procedure

Create an orthographic projection matrix mapping the LEFT, RIGHT, TOP, BOTTOM, NEAR, FAR cube to a viewport of VIEWPORT-LEFT, VIEWPORT-RIGHT, VIEWPORT-TOP, VIEWPORT-BOTTOM. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(perspective WIDTH HEIGHT NEAR FAR ANGLE [RESULT]) procedure

Create an perspective projection matrix with a field of view of ANGLE degrees. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(frustum LEFT RIGHT BOTTOM TOP NEAR FAR [RESULT]) procedure

Create a perspective projection matrix defined by a frustum with a near side of LEFT, RIGHT, TOP, BOTTOM, NEAR, and a far side at FAR. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(frustum-viewport LEFT RIGHT BOTTOM TOP NEAR FAR VIEWPORT-LEFT VIEWPORT-RIGHT VIEWPORT-BOTTOM VIEWPORT-TOP [RESULT]) procedure

Create a perspective projection matrix mapping the LEFT, RIGHT, TOP, BOTTOM, NEAR, FAR frustum to a viewport of VIEWPORT-LEFT, VIEWPORT-RIGHT, VIEWPORT-BOTTOM, VIEWPORT-TOP. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

Camera functions

(look-at EYE OBJ UP [RESULT]) procedure

Create a “look-at” style camera matrix. The camera is positioned at point EYE, pointing towards the point OBJ. UP defines the camera’s up vector. If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(camera-inverse CAMERA [RESULT]) procedure

Invert CAMERA in an efficient fashion. This allows the camera to be constructed in an intuitive fashion by translating and rotating before inverting in order to position the scene properly. This function is far faster than the general inverse function, but the matrix CAMERA must only be a matrix representing a translation and a rotation (no scaling). If the matrix RESULT is given, it will be modified to contain the result. If RESULT is #t, CAMERA must be an f32vector and the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, CAMERA must be an f32vector and the returned value will be an f32vector located in normal garbage collected memory.

Vector operations

(make-point X Y Z [NON-GC?]) procedure
(point-x POINT) procedure
(point-y POINT) procedure
(point-z POINT) procedure
(point-x-set! POINT) procedure
(point-y-set! POINT) procedure
(point-z-set! POINT) procedure

Vector constructor, getters, and setters. If NON-GC? is #t, the point is created in a non-garbage-collected area (the memory will still be freed when there are no more references to the vector).

(v+ A B [RESULT]) procedure

Return the result of the addition of vectors A and B. If the vector RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(v- A B [RESULT]) procedure

Return the result of the subtraction of vector B from A. If the vector RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(v* V S [RESULT]) procedure

Return the result of the multiplication of vector A with scalar S. If the vector RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(cross-product A B [RESULT]) procedure

Return the result of the cross product between the vectors A and B. If the vector RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(dot-product A B) procedure

Return the result of the dot product between the vectors A and B.

(vector-magnitude V) procedure

Return the magnitude of vector V.

(normalize! V) procedure

Destructively normalize the vector V.

(m*vector! MATRIX VECTOR) procedure

Destructively multiply VECTOR by MATRIX.

(m*vector-array! MATRIX VECTORS stride: [STRIDE] length: [LENGTH]) procedure

Destructively multiply the array of 3 element floats VECTORS by MATRIX. VECTORS may be given as an f32vector, a u8vector or a pointer. The keyword STRIDE specifies the number of elements between consecutive vectors, given in number of floats (which must be at least 3) when VECTORS is an f32vector and in bytes when VECTORS is a u8vector or a pointer. When VECTORS is given as a pointer, the keyword LENGTH must be provided, specifying the number of vectors in VECTORS.

(lerp A B T [RESULT]) procedure

Linear interpolation between the points A and B with the interpolation parameter T which must be between 0 and 1. If the vector RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

Quaternion operations

Quaternions are expected to be normalized before they are used in certain functions (quaternion-normalize! may be used to do so). All the provided functions that create quaternions, create unit quaternions.

The order of quaternion cross-multiplication is the inverse of the “standard” order, so a quaternion that has undergone a series or rotations will represent the same rotation as a marix that has gone through the same series, in the same order.

(make-quaternion X Y Z W [NON-GC?]) procedure
(quaternion-x POINT) procedure
(quaternion-y POINT) procedure
(quaternion-z POINT) procedure
(quaternion-w POINT) procedure
(quaternion-x-set! POINT) procedure
(quaternion-y-set! POINT) procedure
(quaternion-z-set! POINT) procedure
(quaternion-w-set! POINT) procedure

Quaternion constructor, getters, and setters. If NON-GC is #t, the quaternion is created in a non-garbage-collected area (the memory will still be freed when there are no more references to the quaternion).

(quaternion-normalize! Q) procedure

Destructively normalize the quaternion Q.

(quaternion-inverse Q [RESULT]) procedure

Return the inverse of the unit quaternion Q. If the quaternion RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(quaternion-cross-product A B [RESULT]) procedure

Return the cross-product of the quaternions A and B. If the quaternion RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(quaternion-axis-angle-rotation AXIS ANGLE [RESULT]) procedure

Return the quaternion corresponding to a rotation of ANGLE radians around the vector AXIS. If the quaternion RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(quaternion-rotate-axis-angle AXIS ANGLE Q) procedure

Rotate the quaternion Q by a rotation of ANGLE radians around the vector AXIS.

(quaternion-x-rotation ANGLE [RESULT]) procedure

Return the quaternion corresponding to a rotation of ANGLE radians around the x-axis. If the quaternion RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(quaternion-rotate-x ANGLE Q) procedure

Rotate the quaternion Q by a rotation of ANGLE radians around the x-axis.

(quaternion-y-rotation ANGLE [RESULT]) procedure

Return the quaternion corresponding to a rotation of ANGLE radians around the y-axis. If the quaternion RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(quaternion-rotate-y ANGLE Q) procedure

Rotate the quaternion Q by a rotation of ANGLE radians around the y-axis.

(quaternion-z-rotation ANGLE [RESULT]) procedure

Return the quaternion corresponding to a rotation of ANGLE radians around the z-axis. If the quaternion RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(quaternion-rotate-z ANGLE Q) procedure

Rotate the quaternion Q by a rotation of ANGLE radians around the z-axis.

(quaternion-ypr-rotation YAW PITCH ROLL [RESULT]) procedure

Return the quaternion corresponding to a rotation of ROLL radians around the z-axis followed by PITCH radians around the x-axis followed by YAW radians around the y-axis. If the quaternion RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

(quaternion-rotate-ypr YAW PITCH ROLL Q) procedure

Rotate the quaternion Q by ROLL radians around the z-axis followed by PITCH radians around the x-axis followed by YAW radians around the y-axis.

(quaternion-rotate-point! Q P) procedure

Destructively rotate the point P by the unit quaternion Q.

(slerp A B T [RESULT]) procedure

Spherical linear interpolation between the quaternions A and B with the interpolation parameter T which must be between 0 and 1. If the quaternion RESULT is given, it will be modified to contain the result. If RESULT is #t the returned value will be an f32vector located in non-garbage-collected memory (the memory will still be freed when there are no more references to the matrix). If RESULT is not provided, the returned value will be an f32vector located in normal garbage collected memory.

Angle operations

(degrees->radians ANGLE) procedure

Converts ANGLE from degrees to radians.

(radians->degrees ANGLE) procedure

Converts ANGLE from radians to degrees.

pi constant
pi/2 constant

Example

    
(import chicken scheme)
(use gl-math)

(define projection-matrix
  (perspective 640 480 0.1 100 70))

(define view-matrix
  (look-at (make-point 1 0 3)
           (make-point 0 0 0)
           (make-point 0 1 0)))

(define model-matrix (mat4-identity))

(print-mat4 (m* projection-matrix
                (m* view-matrix model-matrix)))

Version history

Version 0.8.0

7 August 2014

Version 0.7.0

Version 0.6.0

8 October 2014

Version 0.5.2

10 September 2014

Version 0.5.0

2 September 2014

Version 0.4.1

30 August 2014

Version 0.4.0

27 July 2014

Version 0.3.2

21 July 2014

Version 0.3.1

23 June 2014

Version 0.2.0

21 June 2014

Version 0.1.0

17 June 2014

Source repository

Source available on GitHub.

Bug reports and patches welcome! Bugs can be reported via GitHub or to alex.n.charlton at gmail.

Author

Alex Charlton

Licence

BSD

Contents »