Created
September 22, 2020 10:54
-
-
Save chriscamacho/63a9427deca505634f162e882e369172 to your computer and use it in GitHub Desktop.
port of raymath to python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# just a few functions from raymath | |
from raylib.static import rl, ffi | |
import math | |
PI = 3.14159265358979323846 | |
DEG2RAD = (PI/180.0) | |
RAD2DEG = (180.0/PI) | |
def Clamp(value: float, minv: float, maxv: float): | |
#res = value < minv ? minv : value | |
res = minv if value < minv else value | |
#return res > maxv ? maxv : res | |
return maxv if res > maxv else res | |
def Lerp(start: float, end: float, amount: float): | |
return start + amount * (end - start) | |
def Normalize(value:float , start:float, end:float): | |
return (value - start) / (end - start) | |
def Remap( value:float, inputStart:float, inputEnd:float, outputStart:float, outputEnd:float) : | |
return (value - inputStart) / (inputEnd - inputStart) * (outputEnd - outputStart) + outputStart | |
def Vector2Zero(): | |
return ffi.new("struct Vector2 *",[ 0, 0]) | |
def Vector2One(): | |
return ffi.new("struct Vector2 *",[ 1, 1]) | |
def Vector2(x, y): | |
return ffi.new("struct Vector2 *",[ x, y]) | |
def Vector2Add( v1, v2): | |
return ffi.new("struct Vector2 *",[ v1.x + v2.x, v1.y + v2.y]) | |
def Vector2AddValue(v, add:float): | |
return ffi.new("struct Vector2 *",[ v.x + add, v.y + add]) | |
def Vector2Subtract(v1, v2): | |
return ffi.new("struct Vector2 *",[ v1.x - v2.x, v1.y - v2.y]) | |
def Vector2SubtractValue(v, sub:float): | |
return ffi.new("struct Vector2 *",[ v.x - sub, v.y - sub]) | |
def Vector2Length(v): | |
result = math.sqrt((v.x*v.x) + (v.y*v.y)) | |
return result | |
def Vector2LengthSqr(v): | |
result = (v.x*v.x) + (v.y*v.y) | |
return result | |
def Vector2DotProduct(v1, v2): | |
result = (v1.x*v2.x + v1.y*v2.y) | |
return result | |
def Vector2Distance(v1, v2): | |
result = sqrt((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y)) | |
return result | |
def Vector2Angle(v1, v2): | |
result = atan2(v2.y - v1.y, v2.x - v1.x)*(180.0/PI) | |
if (result < 0): result += 360.0 | |
return result | |
def Vector2Scale(v, scale:float): | |
result = ffi.new("struct Vector2 *",[ v.x*scale, v.y*scale ]) | |
return result | |
def Vector2Multiply(v1, v2): | |
result = ffi.new("struct Vector2 *",[ v1.x*v2.x, v1.y*v2.y ]) | |
return result | |
def Vector2Negate(v): | |
result = ffi.new("struct Vector2 *",[ -v.x, -v.y ]) | |
return result | |
def Vector2Divide(v1, v2): | |
result = ffi.new("struct Vector2 *",[ v1.x/v2.x, v1.y/v2.y ]) | |
return result | |
def Vector2Normalize(v): | |
result = Vector2Scale(v, 1/Vector2Length(v)) | |
return result | |
def Vector2Multiply(v1, v2): | |
result = ffi.new("struct Vector2 *",[ v1.x * v2.x, v1.y * v2.y ]) | |
return result | |
def Vector2Lerp(v1, v2, amount:float): | |
result = ffi.new("struct Vector2 *",[ v1.x + amount*(v2.x - v1.x), v1.y + amount*(v2.y - v1.y) ]) | |
return result | |
def Vector2Rotate(v, degs: float): | |
rads = degs*DEG2RAD | |
#Vector2 result = {v.x * cosf(rads) - v.y * sinf(rads) , v.x * sinf(rads) + v.y * cosf(rads) } | |
result = ffi.new("struct Vector2 *",[v.x * math.cos(rads) - v.y * sin(rads) , v.x * sin(rads) + v.y * cos(rads)]) | |
return result | |
def Vector2MoveTowards( v, target, maxDistance): | |
result = ffi.new("struct Vector2 *",[0,0]) | |
dx = target.x - v.x | |
dy = target.y - v.y | |
value = (dx*dx) + (dy*dy) | |
if ( value == 0 or (maxDistance >= 0 and value <= maxDistance*maxDistance)): | |
result.x = target.x | |
result.y = target.y | |
dist = sqrt(value) | |
result.x = v.x + dx/dist*maxDistance | |
result.y = v.y + dy/dist*maxDistance | |
return result | |
def Vector3Zero(): | |
return ffi.new("struct Vector3 *",[ 0, 0, 0]) | |
def Vector3One(): | |
return ffi.new("struct Vector3 *",[ 1, 1, 1]) | |
def Vector3(x, y, z): | |
return ffi.new("struct Vector3 *",[ x, y, z]) | |
def Vector3Add( v1, v2): | |
result = ffi.new("struct Vector3 *",[ v1.x + v2.x, v1.y + v2.y, v1.z + v2.z ]) | |
return result | |
def Vector3AddValue( v, add:float): | |
result = ffi.new("struct Vector3 *",[ v.x + add, v.y + add, v.z + add ]) | |
return result | |
def Vector3Subtract(v1, v2): | |
result = ffi.new("struct Vector3 *",[ v1.x - v2.x, v1.y - v2.y, v1.z - v2.z ]) | |
return result | |
def Vector3SubtractValue(v, sub:float): | |
result = ffi.new("struct Vector3 *",[ v.x - sub, v.y - sub, v.z - sub ]) | |
return result | |
def Vector3Scale(v, scalar:float): | |
result = ffi.new("struct Vector3 *",[ v.x*scalar, v.y*scalar, v.z*scalar ]) | |
return result | |
def Vector3Multiply(v1, v2): | |
result = ffi.new("struct Vector3 *",[ v1.x*v2.x, v1.y*v2.y, v1.z*v2.z ]) | |
return result | |
def Vector3CrossProduct( v1, v2): | |
result = ffi.new("struct Vector3 *",[ v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x ]) | |
return result | |
def Vector3Perpendicular(v): | |
result = ffi.new("struct Vector3 *",[0.0, 0.0, 0.0]) | |
m = abs(v.x) | |
cardinalAxis = ffi.new("struct Vector3 *",[1.0, 0.0, 0.0]) | |
if (abs(v.y) < m): | |
m = fabs(v.y) | |
tmp = ffi.new("struct Vector3 *",[0.0, 1.0, 0.0]) | |
cardinalAxis = tmp | |
if (abs(v.z) < n): | |
tmp = ffi.new("struct Vector3 *",[0.0, 0.0, 1.0]) | |
cardinalAxis = tmp | |
result = Vector3CrossProduct(v, cardinalAxis) | |
return result | |
def Vector3Length(v): | |
result = math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z) | |
return result | |
def Vector3LengthSqr(v): | |
result = v.x*v.x + v.y*v.y + v.z*v.z | |
return result | |
def Vector3DotProduct(v1, v2): | |
result = v1.x*v2.x + v1.y*v2.y + v1.z*v2.z | |
return result | |
def Vector3Distance(v1, v2): | |
dx = v2.x - v1.x | |
dy = v2.y - v1.y | |
dz = v2.z - v1.z | |
result = sqrt(dx*dx + dy*dy + dz*dz) | |
return result | |
def Vector3Negate(v): | |
result = ffi.new("struct Vector3 *",[-v.x, -v.y, -v.z ]) | |
return result | |
def Vector3Divide(v1, v2): | |
result = ffi.new("struct Vector3 *",[ v1.x/v2.x, v1.y/v2.y, v1.z/v2.z ]) | |
return result | |
def Vector3Normalize(v): | |
result = ffi.new("struct Vector3 *",[ v.x, v.y, v.z ]) | |
length = Vector3Length(v) | |
if (length == 0.0): length = 1.0 | |
ilength = 1.0/length | |
result.x *= ilength | |
result.y *= ilength | |
result.z *= ilength | |
return result | |
def Vector3Transform(v, mat): | |
result = ffi.new("struct Vector3 *",[ 0, 0, 0 ]) | |
x = v.x | |
y = v.y | |
z = v.z | |
result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12 | |
result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13 | |
result.z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14 | |
return result | |
def Vector3RotateByQuaternion(v, q): | |
result = ffi.new("struct Vector3 *",[ 0, 0, 0 ]) | |
result.x = v.x*(q.x*q.x + q.w*q.w - q.y*q.y - q.z*q.z) + v.y*(2*q.x*q.y - 2*q.w*q.z) + v.z*(2*q.x*q.z + 2*q.w*q.y) | |
result.y = v.x*(2*q.w*q.z + 2*q.x*q.y) + v.y*(q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z) + v.z*(-2*q.w*q.x + 2*q.y*q.z) | |
result.z = v.x*(-2*q.w*q.y + 2*q.x*q.z) + v.y*(2*q.w*q.x + 2*q.y*q.z)+ v.z*(q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z) | |
return result | |
def Vector3Lerp(v1, v2, amount): | |
result = ffi.new("struct Vector3 *",[ 0, 0, 0 ]) | |
result.x = v1.x + amount*(v2.x - v1.x) | |
result.y = v1.y + amount*(v2.y - v1.y) | |
result.z = v1.z + amount*(v2.z - v1.z) | |
return result | |
def Vector3Reflect(v, normal): | |
#// I is the original vector | |
#// N is the normal of the incident plane | |
#// R = I - (2*N*( DotProduct[ I,N] )) | |
result = ffi.new("struct Vector3 *",[ 0, 0, 0 ]) | |
dotProduct = Vector3DotProduct(v, normal) | |
result.x = v.x - (2.0*normal.x)*dotProduct | |
result.y = v.y - (2.0*normal.y)*dotProduct | |
result.z = v.z - (2.0*normal.z)*dotProduct | |
return result | |
def Vector3Min( v1, v2): | |
result = ffi.new("struct Vector3 *",[ 0, 0, 0 ]) | |
result.x = min(v1.x, v2.x) | |
result.y = min(v1.y, v2.y) | |
result.z = min(v1.z, v2.z) | |
return result | |
def Vector3Max(v1, v2): | |
result = ffi.new("struct Vector3 *",[ 0, 0, 0 ]) | |
result.x = max(v1.x, v2.x) | |
result.y = max(v1.y, v2.y) | |
result.z = max(v1.z, v2.z) | |
return result | |
def Vector3Barycenter(p, a, b, c): | |
#//Vector v0 = b - a, v1 = c - a, v2 = p - a | |
v0 = Vector3Subtract(b, a) | |
v1 = Vector3Subtract(c, a) | |
v2 = Vector3Subtract(p, a) | |
d00 = Vector3DotProduct(v0, v0) | |
d01 = Vector3DotProduct(v0, v1) | |
d11 = Vector3DotProduct(v1, v1) | |
d20 = Vector3DotProduct(v2, v0) | |
d21 = Vector3DotProduct(v2, v1) | |
denom = d00*d11 - d01*d01 | |
result = ffi.new("struct Vector3 *",[ 0, 0, 0 ]) | |
result.y = (d11*d20 - d01*d21)/denom | |
result.z = (d00*d21 - d01*d20)/denom | |
result.x = 1.0 - (result.z + result.y) | |
return result | |
def MatrixAdd(left, right): | |
result = MatrixIdentity() | |
result.m0 = left.m0 + right.m0 | |
result.m1 = left.m1 + right.m1 | |
result.m2 = left.m2 + right.m2 | |
result.m3 = left.m3 + right.m3 | |
result.m4 = left.m4 + right.m4 | |
result.m5 = left.m5 + right.m5 | |
result.m6 = left.m6 + right.m6 | |
result.m7 = left.m7 + right.m7 | |
result.m8 = left.m8 + right.m8 | |
result.m9 = left.m9 + right.m9 | |
result.m10 = left.m10 + right.m10 | |
result.m11 = left.m11 + right.m11 | |
result.m12 = left.m12 + right.m12 | |
result.m13 = left.m13 + right.m13 | |
result.m14 = left.m14 + right.m14 | |
result.m15 = left.m15 + right.m15 | |
return result | |
def MatrixDeterminant(mat): | |
#// Cache the matrix values (speed optimization) | |
a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3 | |
a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7 | |
a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11 | |
a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15 | |
result = a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 | |
result += a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 | |
result += a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 | |
result += a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 | |
result += a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 | |
result += a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33 | |
return result | |
def MatrixFrustum(left, right, bottom, top, near, far): | |
result = MatrixIdentity() | |
rl = right - left | |
tb = top - bottom | |
fn = far - near | |
result.m0 = (near*2.0) / rl | |
result.m1 = 0.0 | |
result.m2 = 0.0 | |
result.m3 = 0.0 | |
result.m4 = 0.0 | |
result.m5 = (near*2.0) / tb | |
result.m6 = 0.0 | |
result.m7 = 0.0 | |
result.m8 = (right + left) / rl | |
result.m9 = (top + bottom) / tb | |
result.m10 = -(far + near) / fn | |
result.m11 = -1.0 | |
result.m12 = 0.0 | |
result.m13 = 0.0 | |
result.m14 = -(far * near * 2.0) / fn | |
result.m15 = 0.0 | |
return result | |
def MatrixIdentity(): | |
result = ffi.new("struct Matrix *",[ 1.0, 0.0, 0.0, 0.0,0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ]) | |
return result | |
def MatrixInvert(mat): | |
result = MatrixIdentity() | |
#// Cache the matrix values (speed optimization) | |
a00 = mat.m0 | |
a01 = mat.m1 | |
a02 = mat.m2 | |
a03 = mat.m3 | |
a10 = mat.m4 | |
a11 = mat.m5 | |
a12 = mat.m6 | |
a13 = mat.m7 | |
a20 = mat.m8 | |
a21 = mat.m9 | |
a22 = mat.m10 | |
a23 = mat.m11 | |
a30 = mat.m12 | |
a31 = mat.m13 | |
a32 = mat.m14 | |
a33 = mat.m15 | |
b00 = a00*a11 - a01*a10 | |
b01 = a00*a12 - a02*a10 | |
b02 = a00*a13 - a03*a10 | |
b03 = a01*a12 - a02*a11 | |
b04 = a01*a13 - a03*a11 | |
b05 = a02*a13 - a03*a12 | |
b06 = a20*a31 - a21*a30 | |
b07 = a20*a32 - a22*a30 | |
b08 = a20*a33 - a23*a30 | |
b09 = a21*a32 - a22*a31 | |
b10 = a21*a33 - a23*a31 | |
b11 = a22*a33 - a23*a32 | |
#// Calculate the invert determinant (inlined to avoid double-caching) | |
invDet = 1.0/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06) | |
result.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet | |
result.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet | |
result.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet | |
result.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet | |
result.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet | |
result.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet | |
result.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet | |
result.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet | |
result.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet | |
result.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet | |
result.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet | |
result.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet | |
result.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet | |
result.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet | |
result.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet | |
result.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet | |
return result | |
def MatrixLookAt(eye, target, up): | |
print(eye.x, eye.y, eye.z) | |
result = MatrixIdentity() | |
z = Vector3Subtract(eye, target) | |
z = Vector3Normalize(z) | |
x = Vector3CrossProduct(up, z) | |
x = Vector3Normalize(x) | |
y = Vector3CrossProduct(z, x) | |
y = Vector3Normalize(y) | |
result.m0 = x.x | |
result.m1 = x.y | |
result.m2 = x.z | |
result.m3 = 0.0 | |
result.m4 = y.x | |
result.m5 = y.y | |
result.m6 = y.z | |
result.m7 = 0.0 | |
result.m8 = z.x | |
result.m9 = z.y | |
result.m10 = z.z | |
result.m11 = 0.0 | |
result.m12 = eye.x | |
result.m13 = eye.y | |
result.m14 = eye.z | |
result.m15 = 1.0 | |
result = MatrixInvert(result) | |
return result | |
def MatrixTrace(mat): | |
result = (mat.m0 + mat.m5 + mat.m10 + mat.m15) | |
return result | |
def MatrixTranspose(mat): | |
result = MatrixIdentity() | |
result.m0 = mat.m0 | |
result.m1 = mat.m4 | |
result.m2 = mat.m8 | |
result.m3 = mat.m12 | |
result.m4 = mat.m1 | |
result.m5 = mat.m5 | |
result.m6 = mat.m9 | |
result.m7 = mat.m13 | |
result.m8 = mat.m2 | |
result.m9 = mat.m6 | |
result.m10 = mat.m10 | |
result.m11 = mat.m14 | |
result.m12 = mat.m3 | |
result.m13 = mat.m7 | |
result.m14 = mat.m11 | |
result.m15 = mat.m15 | |
return result | |
def MatrixRotate(axis, angle): | |
result = MatrixIdentity() | |
x = axis.x | |
y = axis.y | |
z = axis.z | |
length = math.sqrt(x*x + y*y + z*z) | |
if length != 1.0 and length != 0.0: | |
length = 1.0/length | |
x *= length | |
y *= length | |
z *= length | |
sinres = math.sin(angle) | |
cosres = math.cos(angle) | |
t = 1.0 - cosres | |
result.m0 = x*x*t + cosres | |
result.m1 = y*x*t + z*sinres | |
result.m2 = z*x*t - y*sinres | |
result.m3 = 0.0 | |
result.m4 = x*y*t - z*sinres | |
result.m5 = y*y*t + cosres | |
result.m6 = z*y*t + x*sinres | |
result.m7 = 0.0 | |
result.m8 = x*z*t + y*sinres | |
result.m9 = y*z*t - x*sinres | |
result.m10 = z*z*t + cosres | |
result.m11 = 0.0 | |
result.m12 = 0.0 | |
result.m13 = 0.0 | |
result.m14 = 0.0 | |
result.m15 = 1.0 | |
return result | |
def MatrixRotateX(angle): | |
result = MatrixIdentity() | |
cosres = math.cos(angle) | |
sinres = math.sin(angle) | |
result.m5 = cosres | |
result.m6 = -sinres | |
result.m9 = sinres | |
result.m10 = cosres | |
return result | |
def MatrixRotateY(angle): | |
result = MatrixIdentity() | |
cosres = math.cos(angle) | |
sinres = math.sin(angle) | |
result.m0 = cosres | |
result.m2 = sinres | |
result.m8 = -sinres | |
result.m10 = cosres | |
return result | |
def MatrixRotateZ(angle): | |
result = MatrixIdentity() | |
cosres = math.cos(angle) | |
sinres = math.sin(angle) | |
result.m0 = cosres | |
result.m1 = -sinres | |
result.m4 = sinres | |
result.m5 = cosres | |
return result | |
def MatrixRotateXYZ(ang): | |
result = MatrixIdentity() | |
cosz = math.cos(-ang.z) | |
sinz = math.sin(-ang.z) | |
cosy = math.cos(-ang.y) | |
siny = math.sin(-ang.y) | |
cosx = math.cos(-ang.x) | |
sinx = math.sin(-ang.x) | |
result.m0 = cosz * cosy | |
result.m4 = (cosz * siny * sinx) - (sinz * cosx) | |
result.m8 = (cosz * siny * cosx) + (sinz * sinx) | |
result.m1 = sinz * cosy; | |
result.m5 = (sinz * siny * sinx) + (cosz * cosx) | |
result.m9 = (sinz * siny * cosx) - (cosz * sinx) | |
result.m2 = -siny | |
result.m6 = cosy * sinx | |
result.m10= cosy * cosx | |
return result; | |
def MatrixRotateZYX(ang): | |
result = MatrixRotateZ(ang.z) | |
result = MatrixMultiply(result, MatrixRotateY(ang.y)) | |
result = MatrixMultiply(result, MatrixRotateX(ang.x)) | |
return result | |
def MatrixScale(x, y, z): | |
return ffi.new("struct Matrix *",[ x, 0.0, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 0.0,z, 0.0, 0.0, 0.0, 0.0, 1.0 ]) | |
def MatrixMultiply(left, right): | |
result = ffi.new("struct Matrix *") | |
result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12 | |
result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13 | |
result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14 | |
result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15 | |
result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12 | |
result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13 | |
result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14 | |
result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15 | |
result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12 | |
result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13 | |
result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14 | |
result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15 | |
result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12 | |
result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13 | |
result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14 | |
result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15 | |
return result | |
def MatrixNormalize(mat): | |
result = MatrixIdentity() | |
det = MatrixDeterminant(mat) | |
result.m0 = mat.m0/det | |
result.m1 = mat.m1/det | |
result.m2 = mat.m2/det | |
result.m3 = mat.m3/det | |
result.m4 = mat.m4/det | |
result.m5 = mat.m5/det | |
result.m6 = mat.m6/det | |
result.m7 = mat.m7/det | |
result.m8 = mat.m8/det | |
result.m9 = mat.m9/det | |
result.m10 = mat.m10/det | |
result.m11 = mat.m11/det | |
result.m12 = mat.m12/det | |
result.m13 = mat.m13/det | |
result.m14 = mat.m14/det | |
result.m15 = mat.m15/det | |
return result | |
def MatrixOrtho(left, right, bottom, top, near, far): | |
result = MatrixIdentity() | |
rl = right - left | |
tb = top - bottom | |
fn = far - near | |
result.m0 = 2.0/rl | |
result.m1 = 0.0 | |
result.m2 = 0.0 | |
result.m3 = 0.0 | |
result.m4 = 0.0 | |
result.m5 = 2.0/tb | |
result.m6 = 0.0 | |
result.m7 = 0.0 | |
result.m8 = 0.0 | |
result.m9 = 0.0 | |
result.m10 = -2.0/fn | |
result.m11 = 0.0 | |
result.m12 = -(left + right)/rl | |
result.m13 = -(top + bottom)/tb | |
result.m14 = -(far + near)/fn | |
result.m15 = 1.0 | |
return result | |
def MatrixPerspective(fovy, aspect, near, far): | |
top = near*math.tan(fovy*0.5) | |
right = top*aspect | |
result = MatrixFrustum(-right, right, -top, top, near, far) | |
return result | |
def MatrixToFloatV(mat): | |
buffer = [mat.m0, mat.m1, mat.m2, mat.m3, mat.m4, mat.m5, mat.m6, mat.m7, mat.m8, mat.m9, mat.m10, mat.m11, mat.m12, mat.m13, mat.m14, mat.m15] | |
return buffer | |
def MatrixSubtract(left, right): | |
result = MatrixIdentity() | |
result.m0 = left.m0 - right.m0 | |
result.m1 = left.m1 - right.m1 | |
result.m2 = left.m2 - right.m2 | |
result.m3 = left.m3 - right.m3 | |
result.m4 = left.m4 - right.m4 | |
result.m5 = left.m5 - right.m5 | |
result.m6 = left.m6 - right.m6 | |
result.m7 = left.m7 - right.m7 | |
result.m8 = left.m8 - right.m8 | |
result.m9 = left.m9 - right.m9 | |
result.m10 = left.m10 - right.m10 | |
result.m11 = left.m11 - right.m11 | |
result.m12 = left.m12 - right.m12 | |
result.m13 = left.m13 - right.m13 | |
result.m14 = left.m14 - right.m14 | |
result.m15 = left.m15 - right.m15 | |
return result | |
def MatrixTranslate(x, y, z): | |
return ffi.new("struct Matrix *",[ 1.0, 0.0, 0.0, x, | |
0.0, 1.0, 0.0, y, | |
0.0, 0.0, 1.0, z, | |
0.0, 0.0, 0.0, 1.0 ]) | |
def MatrixTranspose(mat): | |
result = MatrixIdentity() | |
result.m0 = mat.m0 | |
result.m1 = mat.m4 | |
result.m2 = mat.m8 | |
result.m3 = mat.m12 | |
result.m4 = mat.m1 | |
result.m5 = mat.m5 | |
result.m6 = mat.m9 | |
result.m7 = mat.m13 | |
result.m8 = mat.m2 | |
result.m9 = mat.m6 | |
result.m10 = mat.m10 | |
result.m11 = mat.m14 | |
result.m12 = mat.m3 | |
result.m13 = mat.m7 | |
result.m14 = mat.m11 | |
result.m15 = mat.m15 | |
return result | |
def Vector4Zero(): | |
return ffi.new("struct Vector4 *",[ 0, 0, 0, 0]) | |
def Vector4Zero(x, y, z, w): | |
return ffi.new("struct Vector4 *",[ x, y, z, w ]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment