Imported OpenGL Shading
Language
functions. All functions are declared in the base library (_G
).
-- Create a zero-initialized vector of specified type and length, e.g., vec3():
v = [i,b]vec[2,3,4]()
-- Create a vector of specified type and length with the given scalar for each
-- component, e.g., vec3(0):
v = [i,b]vec[2,3,4](scalar)
-- Create a vector of specified type and length and value, e.g., vec3(1,2,3):
v = [i,b]vec[2,3,4](scalar, ..., scalar)
-- Generic vector constructor; infers length based on arguments. Rules:
-- 1. A primitive value (number, integer, boolean) will be stored at v[X];
-- 2. A vector, quaternion, or array (of N dimensions) will have its contents
-- unpacked and stored at v[X], v[X + 1], ..., v[X + N] following x, y, z,
-- w ordering;
v = [i,b]vec(...)
> vec(math.pi, math.pi, math.pi)
vec3(3.141593, 3.141593, 3.141593)
> vec3(math.pi)
vec(3.141593, 3.141593, 3.141593)
-- Parse array
> vec(1, {2, 3, 4})
vec4(1.000000, 2.000000, 3.000000, 4.000000)
-- Return the identity quaternion.
q = quat()
-- Generic quaternion constructor.
q = quat(w --[[ number ]], x --[[ number ]], y --[[ number ]], z --[[ number ]])
q = quat(xyz --[[ vec3 ]], w --[[ number ]])
q = quat(xyzw --[[ vec4 ]])
-- Build a quaternion from an angle (in radians) and axis of rotation.
q = quat(angle --[[ number ]], axis --[[ vec3 ]])
-- Create the shortest arc quaternion that rotates 'source' to coincide with
-- 'target'.
q = quat(source --[[ vec3 ]], target --[[ vec3 ]])
-- Create a quaternion from three unit vectors.
q = quat(x --[[ vec3 ]], y --[[ vec3 ]], z --[[ vec3 ]])
-- Matrix to quaternion.
q = quat(m --[[ mat3x3 ]])
q = quat(m --[[ mat4x4 ]])
-- All matrix constructors accept an optional preallocated matrix object that is
-- used as the return value.
-- Create a identity matrix of specified dimension (C x R).
m = matC[R]([matC[R] sink])
-- Create a diagonal matrix with the given scalar.
m = matC[R]([matC[R] sink,] scalar --[[ number ]])
-- Create a diagonal matrix with the given vector. Zero filling if dimensions
-- are inconsistent (i.e., N ~= C)
m = matC[R]([matC[R] sink,] diag --[[ vecN ]])
-- Create a rotation matrix from a quaternion.
m = matC[R]([matC[R] sink,] rot --[[ quat ]])
-- Copy/Cast a matrix (sink required).
m = matC[R](matC[R] sink, m --[[ matrix ]])
> e = math.exp(1)
> mat3x3(vec3(e, e, e), vec3(math.pi, math.pi, math.pi), vec3(1,1,1))
mat3x3((2.718282, 2.718282, 2.718282), (3.141593, 3.141593, 3.141593), (1.000000, 1.000000, 1.000000))
-- Infer matrix dimensions based on arguments
> mat(vec3(e, e, e), vec3(math.pi, math.pi, math.pi), vec3(1,1,1))
mat3x3((2.718282, 2.718282, 2.718282), (3.141593, 3.141593, 3.141593), (1.000000, 1.000000, 1.000000))
> mat4x4(math.pi)
mat4x4((3.141593, 0.000000, 0.000000, 0.000000), (0.000000, 3.141593, 0.000000, 0.000000), (0.000000, 0.000000, 3.141593, 0.000000), (0.000000, 0.000000, 0.000000, 3.141593))
> mat3x3(quat(35.0, vec3(0,0,1)))
mat3x3((0.819152, 0.573576, 0.000000), (-0.573576, 0.819152, 0.000000), (0.000000, 0.000000, 1.000000))
-- Recycling constructor
> m = mat3x3()
> mat3x3(m, quat(35.0, vec3(0,0,1))) == m
true
Converts degrees to radians.
vec|number = radians(vec|number degrees)
Converts radians to degrees.
vec|number = degrees(vec|number radians)
The standard trigonometric sine function.
vec|number = sin(vec|number angle)
The standard trigonometric cosine function.
vec|number = cos(vec|number angle)
The standard trigonometric tangent.
vec|number = tan(vec|number angle)
Arc sine. Returns an angle whose sine is x.
vec|number = asin(vec|number x)
Arc cosine. Returns an angle whose cosine is x.
vec|number = acos(vec|number x)
Arc tangent. Returns an angle whose tangent is y / x. The signs of x and y are used to determine what quadrant the angle is in.
vec = atan(vec y, vec x)
vec = atan(vec y, number x)
number = atan(number y, number x)
vec|number = atan(vec|number y_over_x)
Returns the hyperbolic sine function.
vec|number = sinh(vec|number x)
Returns the hyperbolic cosine function.
vec|number = cosh(vec|number x)
Returns the hyperbolic tangent function.
vec|number = tanh(vec|number x)
Arc hyperbolic sine; returns the inverse of sinh.
vec|number = asinh(vec|number x)
Arc hyperbolic cosine; returns the non-negative inverse of cosh.
vec|number = acosh(vec|number x)
Arc hyperbolic tangent; returns the inverse of tanh.
vec|number = atanh(vec|number x)
Returns x raised to the y power, i.e., x^y
.
vec = pow(vec x, vec y)
vec|quat|number = pow(vec|quat|number x, number y)
Returns the natural exponentiation of x, i.e., e^x
.
vec|quat|number = exp(vec|quat|number x)
Returns the natural logarithm of x.
vec|number = log(vec|number x[, vec|number base])
quat = log(quat x)
Returns 2 raised to the x power.
vec|number = exp2(vec|number x)
Returns the base 2 logarithm of x.
vec|number = log2(vec|number x)
Returns sqrt(x).
vec|quat|number = sqrt(vec|quat|number x)
Returns 1 / sqrt(x)
.
vec|quat|number = inversesqrt(vec|quat|number x)
Returns x if x ≥ 0; otherwise it returns -x.
vec|number = abs(vec|number x)
Returns 1.0 if x > 0, 0.0 if x = 0, or -1.0 if x < 0.
vec|number = sign(vec|number x)
Returns a value equal to the nearest integer that is less than or equal to x.
vec|number = floor(vec|number x)
Returns a value equal to the nearest integer to x whose absolute value is not larger than the absolute value of x.
vec|number = trunc(vec|number x)
Returns a value equal to the nearest integer to x.
vec|number = round(vec|number x)
Returns a value equal to the nearest integer to x. A fractional part of 0.5 will round toward the nearest even integer.
vec|number = roundEven(vec|number x)
Returns a value equal to the nearest integer that is greater than or equal to x.
vec|number = ceil(vec|number x)
Returns x - floor(x).
vec|number = fract(vec|number x)
Modulus. Returns x - y * floor(x / y).
vec = mod(vec x, vec y)
vec = mod(vec x, number y)
number = mod(number x, number y)
Returns the integral and fractional parts of x. The second result is always a
float (return values consistent with math.modf
).
vec,vec = modf(vec x)
integer,number = modf(integer x)
integer,number = modf(number x)
Returns y if y < x; otherwise it returns x.
vec = min(vec x, vec y)
vec = min(vec x, number y)
number = min(number x, number y)
integer = min(integer x, integer y)
Returns y if x < y; otherwise it returns x.
vec = max(vec x, vec y)
vec = max(vec x, number y)
number = max(number x, number y)
integer = max(integer x, integer y)
Returns min(max(x, minVal), maxVal).
vec = clamp(vec x, vec minVal, vec maxVal)
vec = clamp(vec x, number minVal, number maxVal)
number = clamp(number x, number minVal, number maxVal)
integer = clamp(integer x, integer minVal, integer maxVal)
-- Saturate: clamp(X, 0, 1)
vec|number = clamp(vec|number x)
Returns the linear blend of x and y, i.e., x * (1 - a) + y * a
.
vec = mix(vec x, vec y, vec|number a)
number = mix(number x, number y, number a)
-- If a is true, returns y. Otherwise x is returned.
T = mix(T x, T y, bool a)
Returns 0.0 if x < edge; otherwise it returns 1.0.
vec|number = step(vec|number edge, vec|number x)
vec|number = step(number edge, vec|number x)
Returns 0.0 if x <= edge0
and 1.0 if x >= edge1
, and performs smooth Hermite
interpolation between 0 and 1 when edge0 < x < edge1
.
vec|number = smoothstep(vec|number edge0, vec|number edge1, vec|number x)
vec|number = smoothstep(number edge0, number edge1, vec|number x)
Returns true if x holds a NaN. Returns false otherwise.
vec|bool = isnan(vec|number x)
Returns true if x holds a positive infinity or negative infinity.
vec|bool = isinf(vec|number x)
Unimplemented. See Format Strings for Pack and Unpack.
Computes and returns a * b + c
vec|number = fma(vec|number a, vec|number b, vec|number c)
Splits x into a floating-point significand in the range [0.5, 1.0]
, and an
integral exponent of two.
vec|number,vec|number = frexp(vec|number x)
Builds a floating-point number from x and the corresponding integral exponent of
two in exp, returning: significand * 2^exponent
vec|number = frexp(vec|number x, vec|number exp)
Unimplemented. See Format Strings for Pack and Unpack.
Returns the length of vector x.
vec = length(vec)
_ = length(_) -- Generic length operation
Returns the distance between p0 and p1, i.e., length(p0 - p1)
.
number = distance(vec|number p0, vec|number p1)
Returns the dot product of x and y.
vec|number = dot(vec|number p0, vec|number p1)
Returns the cross product of x and y
vec2 = cross(vec2 x, vec2 y)
vec3 = cross(vec3 x, vec3 y)
vec3 = cross(vec3 x, quat y)
quat = cross(quat x, quat y)
quat = cross(quat x, vec3 y)
Returns a vector in the same direction as x but with a length of 1.
vec|quat|number = normalize(vec|quat|number x)
If dot(Nref, I) < 0 return N, otherwise return -N.
vec|number = faceforward(vec N, vec I, vec nRef)
For the incident vector I and surface orientation N, returns the reflection
direction: I - 2 * dot(N, I) * N
.
vec|number = reflect(vec|number I, vec|number N)
For the incident vector I and surface normal N, and the ratio of indices of refraction eta, return the refraction vector.
vec|number = refract(vec|number I, vec|number N, number eta)
Multiply matrix x by matrix y component-wise, i.e., result[i][j]
is the scalar
product of x[i][j]
and y[i][j]
.
mat = transpose(mat x, mat y[, mat recycle])
Treats the first parameter c as a column vector and the second parameter r as a
row vector and does a linear algebraic matrix multiply c * r
.
mat = transpose(vec c, vec r[, mat recycle])
Returns a matrix that is the transpose of m.
mat = transpose(mat m[, mat recycle])
Returns the determinant of m.
number = determinant(mat m)
Returns a value that is the inverse of x.
mat = inverse(mat x[, mat recycle])
quat|vec|number = inverse(quat|vec|number x)
Returns the component-wise compare of x < y.
vec = lessThan(vec x, vec y)
bool = lessThan(number x, number y)
bool = lessThan(integer x, integer y)
Returns the component-wise compare of x <= y.
vec = lessThanEqual(vec x, vec y)
bool = lessThanEqual(number x, number y)
bool = lessThanEqual(integer x, integer y)
Returns the component-wise compare of x > y.
vec = greaterThan(vec x, vec y)
bool = greaterThan(number x, number y)
bool = greaterThan(integer x, integer y)
Returns the component-wise compare of x >= y.
vec = greaterThanEqual(vec x, vec y)
bool = greaterThanEqual(number x, number y)
bool = greaterThanEqual(integer x, integer y)
Returns the component-wise compare of x == y.
vec = equal(mat x, mat y)
vec = equal(vec x, vec|number y)
bool = equal(number x, number y)
bool = equal(integer x, integer y)
Returns the component-wise compare of x != y.
vec = notEqual(mat x, mat y)
vec = notEqual(vec x, vec y)
bool = notEqual(number x, number y)
bool = notEqual(integer x, integer y)
Returns true if any component of x is true (non-zero).
bool = any(vec|number|bool x)
Returns true only if all components of x are true (non-zero).
bool = all(vec|number|bool x)
Returns the component-wise logical complement of x.
vec|bool|bool = not_(vec|number|bool x)
Unimplemented
Unimplemented
Unimplemented
Extracts bits [offset, offset + bits - 1]
from value, returning them in the
least significant bits of the result.
integer = bitfieldExtract(integer value, integer offset, integer bits)
Inserts the bits least significant bits of insert into base.
integer = bitfieldInsert(integer base, integer insert, integer offset, integer bits)
Reverses the bits of value.
integer = bitfieldReverse(integer value)
Returns the number of one bits in the binary representation of value.
integer = bitCount(integer value)
Returns the bit number of the least significant one bit in the binary representation of value. If value is zero, -1 will be returned. Unimplemented for vector types.
integer = findLSB(integer value)
Returns the bit number of the most significant bit in the binary representation of value. Unimplemented for vector types.
integer = findMSB(integer value)
Alias to mix
Alias to inverse
Alias to normalize
Returns the component-wise comparison of |x - y| <= epsilon
.
vec = approx(vec x, vec y[, number epsilon])
vec = approx(mat x, mat y[, number epsilon])
bool = approx(number x, number y[, number epsilon])
bool = approx(integer x, integer y[, integer epsilon])
Return the axis of rotation.
vec3 = axis(quat q)
Return the angle of a quaternion, matrix, or between two vectors.
number = angle(quat r)
number = angle(vec2 x, vec2 y)
number = angle(vec3 x, vec3 y)
Create a quaternion from a forward (direction) and up vector (quatLookAt).
quat = quat_for(vec3 dir, vec3 up)
Returns the spherical blend of x and y.
quat = slerp(quat x, quat y, number t)
vec3 = slerp(vec3 x, vec3 y, number t)
Translate a transformation matrix.
mat4 = translate(mat4 m, vec3 translate[, mat4 recycle])
mat3 = translate(mat3 m, vec2 translate[, mat3 recycle])
Scale a transformation matrix.
mat4 = scale(mat4 m, vec3 scale[, mat4 recycle])
mat3 = scale(mat3 m, vec2 scale[, mat3 recycle])
Rotate a transformation matrix.
quat = rotate(quat q, number angle, vec3 axis)
mat4 = rotate(mat4 m, number angle, vec3 axis[, mat4 recycle])
mat3 = rotate(mat3 m, number angle[, mat3 recycle])
vec2 = rotate(vec2 m, number angle)
Create a perspective projection matrix. Depends upon compilation flags
CGLM_FORCE_DEPTH_ZERO_TO_ONE
and CGLM_FORCE_LEFT_HANDED
.
mat4 = frustum(number left, number right, number bottom, number top, number nearZ, number farZ[, mat4 recycle])
Create an orthographic projection matrix. Depends upon compilation flags
CGLM_FORCE_DEPTH_ZERO_TO_ONE
and CGLM_FORCE_LEFT_HANDED
.
mat4 = ortho(number left, number right, number bottom, number top, number nearZ, number farZ[, mat4 recycle])
Create a perspective projection matrix. Depends upon compilation flags
CGLM_FORCE_DEPTH_ZERO_TO_ONE
and CGLM_FORCE_LEFT_HANDED
.
mat4 = perspective(number fovy, number aspect, number nearZ, number farZ[, mat4 recycle])
Map object coordinates to window coordinates.
vec3 = project(vec3 pos, mat4 modelView, vec4 viewport)
vec3 = project(vec3 x, vec3 y) -- Project X onto Y
Map the specified viewport coordinates into the specified space:
- Projection to View.
- View to World.
- Model to Object.
vec3 = unproject(vec3 pos, mat4 m, vec4 viewport)
Create a view matrix from camera coordinates and direction.
mat4 = lookat(vec3 eye, vec3 center, vec3 up[, mat4 recycle])
mat4 = lookat(vec3 eye, quat rotation[, mat4 recycle])
Create a spherical billboard matrix that rotates around the specified object position.
mat4 = billboard(vec3 obj, vec3 eye, vec3 up, vec3 fwd[, mat4 recycle])
Create an affine transformation matrix from translation, rotation, and scaling components.
mat4 = compose(vec3 translation, quat rotation, vec3 scale[, mat4 recycle])
Decompose an affine transformation matrix into translation, rotation, and scaling components.
vec3,quat,vec3 = decompose(mat4 m)
Build a quaternion from Euler angles. The axis-ordering is specified by
character strings: 'xyz', 'xzy', 'yxz', 'yzx', 'zxy', 'zyx'
.
quat = from_euler(vec3 euler, string order[, bool extrinsic])
Extract the Euler angles from a transformation matrix or quaternion using the specified rotation sequence.
vec3 = to_euler(quat q, string order[, bool extrinsic])
vec3 = to_euler(mat4 m, string order[, bool extrinsic])
vec3 = to_euler(mat3 m, string order[, bool extrinsic])
EPSILON value for vector types