June 18, 2021, 07:38:32 AM

### Author Topic: [bb] Blitz3D Maths Library by Bobysait [ 8 months ago ]  (Read 958 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Blitz3D Maths Library by Bobysait [ 8 months ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Blitz3D Maths Library
Author : Bobysait
Posted : 8 months ago

Description : (full code probably too long, so it's split in 4 parts)

All the maths involved to rebuild a blitz3d like entity system
Originally done for a blitzmax 3d engine, all behaviors reproduice perfectly the Turn/Rotate/Move etc ... of the blitz3d entity
Here is a Blitz3d version of the library.

It also include some color templates to be used as vector.

Note : The Vector type use a 4 th component "W"
originally used for alignement, It's also usefull to store an extra value, like a radius, so a vector can define a sphere.
All class are designed for OOP usage, so they all returns the object passed in the argument of the function.
It allows to imbricate function calls in a single line.
Also, the functions with "Self" in the name happens the transformation on the first object passed in the argument -> it does not create a new object.

Local a.Vector = NewVector()
Local b.Vector = NewVector()
Local c.Vector = VecSub(a,b) -> create a new vector
Local d.Vector = VecSelfSub(a,b) -> this will store the result of a-b in a and will return a, so "d" is "a"

As it's Blitz3D (and not blitzmax), take care of memory leak !
If you create an instance of a maths object, don't forget to release it
-> Vector quand Quaternion can be freed using "Delete" as they don't store internal objects
-> Matrix3 and Matrix4 MUST be freed using Mat3Free(m3) and Mat4Free(m4)
(because they store vectors as axis [and position for the matrix4 object])
; Worklog :

V1.001
- Fix an error in VecSub (y was added instead of substract)

Code :
Code: BlitzBasic
1. ; --------------------------------------------------------------------
2. ; - Maths Library -
3. ; --------------------------------------------------------------------
4. ; - Author  : Bobysait 2016
5. ; - About   : Full compatible with the Blitz3D Entity stuff.
6. ;             the library is a portage of the Bigbang Maths module.
7. ;             whatever, see license aggreement below.
8. ; - license : no restrictions.
9. ;             Feel free to use for any purpose
10. ;             After all, it's just a maths lib.
11. ; --------------------------------------------------------------------

Bobysait(Posted 8 months ago)

The Vector part :
Code: [Select]

; -----------------------------------------
; - Vector 3 - (Also used to store colors)
; -----------------------------------------
Type Vector
Field X#,Y#,Z#, W#
End Type ; [W optional -> required on BigBang for alignement]

Function FreeVector .Vector(v.Vector):Delete v:Return Null: End Function

; Constructors
Function NewVector .Vector(x#=0,y#=0,z#=0):Local v.Vector=New Vector:vX=x:vY=y:v=z:vw=1:Return v:End Function
Function VecUnit .Vector() : Return NewVector(1,1,1) : End Function
Function VecLeft .Vector() : Return NewVector(-1,0,0) : End Function
Function VecRight .Vector() : Return NewVector(1,0,0) : End Function
Function VecUp .Vector() : Return NewVector(0,1,0) : End Function
Function VecDown .Vector() : Return NewVector(0,-1,0) : End Function
Function VecFront .Vector() : Return NewVector(0,0,1) : End Function
Function VecBack .Vector() : Return NewVector(0,0,-1) : End Function
Function VecForward .Vector() : Return NewVector(0,0,1) : End Function
Function VecBackward .Vector() : Return NewVector(0,0,-1) : End Function
Function VecCopy .Vector(v.Vector): Return NewVector( vX,vY,v ) : End Function
Function Vec4Copy .Vector(v.Vector): Local o.Vector=NewVector( vX,vY,v ):oW=vW:Return o : End Function
Function VecFromFArray .Vector(f#[2]) : Return NewVector( f[0],f[1],f[2] ) : End Function

; Set
Function VecSet .Vector(v.Vector,x#,y#,z#) : vX=x:vY=y:v=z:Return v : End Function
Function VecSetX .Vector(v.Vector, x#) : vX=x:Return v : End Function
Function VecSetY .Vector(v.Vector, y#) : vY=y:Return v : End Function
Function VecSetZ .Vector(v.Vector, z#) : v=z:Return v : End Function
Function VecSetW .Vector(v.Vector, w#) : vW=w:Return v : End Function
Function VecSetVector .Vector(v.Vector, a.Vector) : VecSet( v, aX,aY,a):vW = aW:Return v : End Function
Function VecAssign .Vector(v.Vector, a.Vector) : VecSet( v, aX,aY,a):vW = aW:Return v : End Function

; Get
Function VecX# (v.Vector) : Return vX : End Function
Function VecY# (v.Vector) : Return vY : End Function
Function VecZ# (v.Vector) : Return v : End Function
Function VecW# (v.Vector) : Return vW : End Function

; scalars
Function VecScale .Vector (v.Vector, a#    ) : Return NewVector(  vX*a,vY*a,v*a) : End Function
Function VecSelfScale .Vector (v.Vector, a#    ) : Return VecSet(v,vX*a,vY*a,v*a) : End Function
Function VecTranslate .Vector (v.Vector,x#,y#,z#) : vX=vX+x:vY=vY+y:v=vz+z:Return v : End Function
Function VecDot# (v.Vector, a.Vector) : Return vX*aX+vY*aY+v*a : End Function
Function VecLength# (v.Vector) : Return Sqr(vX*vX+vY*vY+v*v) : End Function
Function VecSqLength# (v.Vector) : Return vX*vX+vY*vY+v*v : End Function
Function VecNormalize .Vector (v.Vector)
Local l# = Sqr(vX*vX+vY*vY+v*v) : If (l>0.000001) Then l=1.0/l:Return NewVector(vX*l,vY*l,v*l);
Local x_# = Sgn(vX), y_# = Sgn(vY), z_# = Sgn(v) : If ((x_<>0) or (y_<>0) or (z_<>0)) Then l=1.0/Sqr(x_*x_+y_*y_+z_*z_): Return NewVector(x_*l,y_*l,z_*l);
Return Vecup();
End Function
Function VecSelfNormalize.Vector(v.Vector)
Local l# = Sqr(vX*vX+vY*vY+v*v) : If (l>0.000001) Then l=1.0/l:Return VecSet(v,vX*l,vY*l,v*l);
Local x_# = Sgn(vX), y_# = Sgn(vY), z_# = Sgn(v) : If ((x_<>0) or (y_<>0) or (z_<>0)) Then l=1.0/Sqr(x_*x_+y_*y_+z_*z_): Return VecSet(v,x_*l,y_*l,z_*l);
Return VecSet(v,0,1,0);
End Function

Function VecSetLength .Vector (v.Vector, s#)
Local l# = vX*vX+vY*vY+v*v : If (l>0.000001) Then l=s/Sqr(l) : vX=vX*l:vY=vY*l:v=v*l :Return v;
vX=Sgn(vX) : vY=Sgn(vY) : v=Sgn(v) : If ((vX<>0) Or (vY<>0) Or (v<>0)) Then l=s/Sqr(vX*vX+vY*vY+v*v) : vX=vX*l : vY=vY*l : v=v*l :Return v;
Return VecSet(v, 0,s,0);
End Function

; Transform
; vs Vec
Function VecAdd .Vector(v.Vector, a.Vector) : Return NewVector(vX+aX,vY+aY,v+a) : End Function
Function VecSelfAdd .Vector(v.Vector, a.Vector) : Return VecSet (v,vX+aX,vY+aY,v+a) : End Function
Function VecSub .Vector(v.Vector, a.Vector) : Return NewVector(vX-aX,vY-aY,v-a) : End Function
Function VecSelfSub .Vector(v.Vector, a.Vector) : Return VecSet (v,vX-aX,vY-aY,v-a) : End Function
Function VecMul .Vector(v.Vector, a.Vector) : Return NewVector(vX*aX,vY*aY,v*a) : End Function
Function VecSelfMul .Vector(v.Vector, a.Vector) : Return VecSet (v,vX*aX,vY*aY,v*a) : End Function
Function VecDiv .Vector(v.Vector, a.Vector) : Return NewVector(vX/aX,vY/aY,v/a) : End Function
Function VecSelfDiv .Vector(v.Vector, a.Vector) : Return VecSet (v,vX/aX,vY/aY,v/a) : End Function
Function VecMulV .Vector(v.Vector, a.Vector) : Return NewVector(vX*aX,vY*aY,v*a) : End Function
Function VecSelfMulV .Vector(v.Vector, a.Vector) : Return VecSet (v,vX*aX,vY*aY,v*a) : End Function
Function VecMulQ .Vector(v.Vector, q.Quaternion ): Return QuatMulV(q, v) : End Function
Function VecSelfMulQ .Vector(v.Vector, q.Quaternion ): Return QuatSelfMulV(q, v) : End Function
Function VecMulM3 .Vector(v.Vector, m.Matrix3 ):Return Mat3MulV(m, v) : End Function
Function VecSelfMulM3 .Vector(v.Vector, m.Matrix3 ):Return Mat3SelfMulV(m, v) : End Function
Function VecMulM4 .Vector(v.Vector, m.Matrix4 ):Return Mat4MulV(m, v) : End Function
Function VecSelfMulM4 .Vector(v.Vector, m.Matrix4 ):Return Mat4SelfMulV(m, v) : End Function
Function VecInvert .Vector(v.Vector) : Return NewVector(  1.0/vX,1.0/vY, 1.0/v) : End Function
Function VecSelfInvert .Vector(v.Vector) : Return VecSet(v,1.0/vX,1.0/vY, 1.0/v) : End Function
Function VecNegate .Vector(v.Vector) : Return NewVector(  -vX,-vY, -v) : End Function
Function VecSelfNegate .Vector(v.Vector) : Return VecSet(v,-vX,-vY, -v) : End Function
Function VecCross .Vector(v.Vector, a.Vector) : Return NewVector(  vY*a-v*aY,v*aX-vX*a,vX*aY-vY*aX):End Function
Function VecSelfCross .Vector(v.Vector, a.Vector) : Return VecSet(v,vY*a-v*aY,v*aX-vX*a,vX*aY-vY*aX):End Function
Function VecPerpendicular.Vector(v.Vector)
If (Abs(vX)>Abs(vY)) Then Local l1# = Sqr(vX*vX+v*v):Return NewVector(vz/l1,0, -vX/l1);
Local l2# = Sqr(vY*vY+v*v) : Return VecSet(v, 0,v/l2,-vY/l2);
End Function
Function VecSelfPerpendicular.Vector(v.Vector)
If (Abs(vX)>Abs(vY)) Then Local l1# = Sqr(vX*vX+v*v):Return NewVector(vz/l1,0, -vX/l1);
Local l2# = Sqr(vY*vY+v*v) : Return VecSet(v, 0,v/l2,-vY/l2);
End Function
Local v.Vector = New Vector
vX = aX * a_Scale + bX * b_Scale
vY = aY * a_Scale + bY * b_Scale
v = a * a_Scale + b * b_Scale
vW = 1
Return v
End Function
aX = aX * a_Scale + bX * b_Scale
aY = aY * a_Scale + bY * b_Scale
a = a * a_Scale + b * b_Scale
Return a
End Function
; vs Mat3
Function VecTransformM3.Vector(v.Vector,m.Matrix3)
Local o.Vector = New Vector;
oX = vX*mXX + vY*mYX + v*mX;
oY = vX*mXY + vY*mYY + v*mY;
o = vX*mX + vY*mY + v*m;
oW = 1
Return o;
End Function
Function VecSelfTransformM3.Vector(v.Vector,m.Matrix3)
Local lX# = vX, lY# = vY, lZ# = v;
vX = lX*mXX + lY*mYX + lZ*mX;
vY = lX*mXY + lY*mYY + lZ*mY;
v = lX*mX + lY*mY + lZ*m;
Return v;
End Function
Function VecInvertTransformM3.Vector( v.Vector, m.Matrix3 )
Local m_.Matrix3 = Mat3Invert(m);
v = VecTransformM3(v, m_);
FreeMatrix3(m_);
Return v;
End Function
Function VecSelfInvertTransformM3.Vector( v.Vector, m.Matrix3 )
Local m_.Matrix3 = Mat3Invert(m);
VecSelfTransformM3(v, m_);
FreeMatrix3(m_);
Return v;
End Function

; vs Mat4
Function VecTransformM4.Vector(v.Vector,m.Matrix4)
Local o.Vector = New Vector;
oX = vX*mXX + vY*mYX + v*mX + mPX;
oY = vX*mXY + vY*mYY + v*mY + mPY;
o = vX*mX + vY*mY + v*m + mP;
oW = 1
Return o;
End Function
Function VecSelfTransformM4.Vector(v.Vector,m.Matrix4)
Local lX# = vX, lY# = vY, lZ# = v;
vX = lX*mXX + lY*mYX + lZ*mX + mPX;
vY = lX*mXY + lY*mYY + lZ*mY + mPY;
v = lX*mX + lY*mY + lZ*m + mP;
Return v;
End Function
Function VecInvertTransformM4.Vector( v.Vector, m.Matrix4 )
Local m_.Matrix4 = Mat4Invert(m);
v = VecTransformM4(v, m_);
FreeMatrix4(m_);
Return v;
End Function
Function VecSelfInvertTransformM4.Vector( v.Vector, m.Matrix4 )
Local m_.Matrix4 = Mat4Invert(m);
VecSelfTransformM4(v, m_);
FreeMatrix4(m_);
Return v;
End Function

; some smart functions
; convert to float array
Function VecArray.Vector(v.Vector, f#[2]) : f[0] = vX : f[1] = vY : f[2] = v : Return v : End Function
Function VecArray4.Vector(v.Vector, f#[3]) : f[0] = vX : f[1] = vY : f[2] = v : f[3] = vW : Return v : End Function

; convert to string
Function VecToString\$(v.Vector):Return vX+" "+vY+" "+v: End Function
Function Vec4ToString\$(v.Vector):Return vX+" "+vY+" "+v+" "+vW: End Function
Function ColorToString\$(v.Vector):Return vX+" "+vY+" "+v+" "+vW: End Function

; vector pitch and yaw
Function VecPitch#(v.Vector) : Return -Atan2(vY, Sqr(vX*vX+v*vz)) : End Function
Function VecYaw  #(v.Vector) : Return -Atan2(vX, v) : End Function

; linear interpolation
Function VecLerp.Vector(v.Vector, a.Vector, t#) : Return NewVector(vX+(aX-vX)*t, vY+(aY-vY)*t, v+(a-v)*t) : End Function
Function VecSelfLerp.Vector(v.Vector, a.Vector, t#) : vx=vX+(aX-vX)*t: vY=vY+(aY-vY)*t: v=v+(a-v)*t : Return v: End Function

; Clamp
Function VecClamp.Vector(v.Vector, vMin.Vector,vMax.Vector)
Local v_.Vector = VecCopy(v);
If v_X < vMinX Then v_X=vMinX;
If v_Y < vMinY Then v_Y=vMinY;
If v_ < vMin Then v_=vMin;
If v_X > vMaxX Then v_X=vMaxX;
If v_Y > vMaxY Then v_Y=vMaxY;
If v_ > vMax Then v_=vMax;
Return v_;
End Function
Function VecSelfClamp.Vector(v.Vector, vMin.Vector,vMax.Vector)
If vX < vMinX Then vX=vMinX;
If vY < vMinY Then vY=vMinY;
If v < vMin Then v=vMin;
If vX > vMaxX Then vX=vMaxX;
If vY > vMaxY Then vY=vMaxY;
If v > vMax Then v=vMax;
Return v;
End Function

; center
Function VecCenter.Vector(a.Vector,b.Vector)
Local v.Vector = New Vector
vx = (aX+bX)*0.5 : vY = (aY+bY)*0.5 : v = (a+b)*0.5 : vW = 1 : Return v
End Function
Function VecSelfCenter.Vector(v.Vector,b.Vector)
vx = (vX+bX)*0.5 : vY = (vY+bY)*0.5 : v = (v+b)*0.5 : Return v
End Function

; bounding sphere
Function BoundingSphere.Vector(boundMin.Vector, boundMax.Vector)
Local Center.Vector = New Vector;
CenterX = (boundMinX + boundMaxX) * 0.5;
CenterY = (boundMinY + boundMaxY) * 0.5;
Center = (boundMin + boundMax) * 0.5;
CenterW = Sqr((boundMaxX-boundMinX)*(boundMaxX-boundMinX)+(boundMaxY-boundMinY)*(boundMaxY-boundMinY)+(boundMax-boundMin))*0.5
Return Center;
End Function

; distances
Function VecDistance# (v.Vector, a.Vector): Return Sqr((aX-vX)*(aX-vX)+(aY-vY)*(aY-vY)+(a-v)*(a-v)) : End Function
Function VecSqDistance# (v.Vector, a.Vector): Return (aX-vX)*(aX-vX)+(aY-vY)*(aY-vY)+(a-v)*(a-v) : End Function
Function VecDistanceXY# (v.Vector, a.Vector): Return Sqr((aX-vX)*(aX-vX)+(aY-vY)*(aY-vY)) : End Function
Function VecSqDistanceXY# (v.Vector, a.Vector): Return (aX-vX)*(aX-vX)+(aY-vY)*(aY-vY) : End Function
Function VecDistanceXZ# (v.Vector, a.Vector): Return Sqr((aX-vX)*(aX-vX)+(a-v)*(a-v)) : End Function
Function VecSqDistanceXZ# (v.Vector, a.Vector): Return (aX-vX)*(aX-vX)+(a-v)*(a-v) : End Function
Function VecDistanceYZ# (v.Vector, a.Vector): Return Sqr((aY-vY)*(aY-vY)+(a-v)*(a-v)) : End Function
Function VecSqDistanceYZ# (v.Vector, a.Vector): Return (aY-vY)*(aY-vY)+(a-v)*(a-v) : End Function

Function VecXYZ.Vector(v.Vector): Local a.Vector=New Vector: aX = vX: aY = vY: a = v: aW=1: Return a: End Function
Function VecXZY.Vector(v.Vector): Local a.Vector=New Vector: aX = vX: aY = v: a = vY: aW=1: Return a: End Function
Function VecYXZ.Vector(v.Vector): Local a.Vector=New Vector: aX = vY: aY = vX: a = v: aW=1: Return a: End Function
Function VecYZX.Vector(v.Vector): Local a.Vector=New Vector: aX = vY: aY = v: a = vX: aW=1: Return a: End Function
Function VecZXY.Vector(v.Vector): Local a.Vector=New Vector: aX = v: aY = vX: a = vY: aW=1: Return a: End Function
Function VecZYX.Vector(v.Vector): Local a.Vector=New Vector: aX = v: aY = vY: a = vX: aW=1: Return a: End Function
Function VecXY.Vector(v.Vector) : Local a.Vector=New Vector: aX = vX: aY = vY: a = 0.0: aW=1: Return a: End Function
Function VecXZ.Vector(v.Vector) : Local a.Vector=New Vector: aX = vX: aY = v: a = 0.0: aW=1: Return a: End Function
Function VecYX.Vector(v.Vector) : Local a.Vector=New Vector: aX = vY: aY = vX: a = 0.0: aW=1: Return a: End Function
Function VecYZ.Vector(v.Vector) : Local a.Vector=New Vector: aX = vY: aY = v: a = 0.0: aW=1: Return a: End Function
Function VecZX.Vector(v.Vector) : Local a.Vector=New Vector: aX = v: aY = vX: a = 0.0: aW=1: Return a: End Function
Function VecZY.Vector(v.Vector) : Local a.Vector=New Vector: aX = v: aY = vY: a = 0.0: aW=1: Return a: End Function

; Vec As Colors
Function VecR#(v.Vector) : Return vX : End Function
Function VecG#(v.Vector) : Return vY : End Function
Function VecB#(v.Vector) : Return v : End Function
Function VecA#(v.Vector) : Return vW : End Function

Function VecSetHex.Vector(v.Vector, h%)
vW = Float((h Shr(24)) And \$FF)/255.0;
vX = Float((h Shr(16)) And \$FF)/255.0;
vY = Float((h Shr(8)) And \$FF)/255.0;
v = Float(h And \$FF)/255.0;
Return v;
End Function

Function VecSetColor.Vector(v.Vector,pR#,pG#,pB#,pA#=1.0)
vX = pR : vY = pG : v = pB : vW = pA : Return v;
End Function
Function VecARGB%(v.Vector): Return Int(vW*255.0) Shl(24)+Int(vX*255.0) Shl(16)+Int(vY*255.0)Shl(8) + Int(v*255.0) : End Function
Function VecRGB%(v.Vector) : Return Int(vX*255.0) Shl(16)+Int(vY*255.0)Shl(8) + Int(v*255.0) : End Function

Function VecSetAlpha.Vector(v.Vector, a#): vW = a: Return v : End Function
Function VecSetRed.Vector(v.Vector, r#): vX = r: Return v : End Function
Function VecSetGreen.Vector(v.Vector, g#): vY = g: Return v : End Function
Function VecSetBlue.Vector(v.Vector, b#): v = b: Return v : End Function
; Color Templates -> HTML/CSS colors
.VecColor_Templates
Data 144
Data "black",\$000000,"white",\$FFFFFF,"red",\$FF0000,"lime",\$00FF00,"blue",\$0000FF,"gray",\$808080,"grey",\$808080,"green",\$008000,"cyan",\$00FFFF,"medium violet red",\$C71585 ; 10
Data "dark slate blue",\$483D8B,"pale violet red",\$DB7093,"light steel blue",\$B0C4DE,"dodger blue",\$1E90FF,"linen",\$FAF0E6,"dark slate gray",\$2F4F4F,"dark orange",\$FF8C00 ; 7
Data "white smoke",\$F5F5F5,"sandy brown",\$F4A460,"ghost white",\$F8F8FF,"pale golden rod",\$EEE8AA,"dark magenta",\$8B008B,"spring green",\$00FF7F,"medium sea green",\$3CB371 ; 7
Data "medium spring green",\$00FA9A,"light golden rod yellow",\$FAFAD2,"peru",\$CD853F,"brown",\$A52A2A,"forest green",\$228B22,"dark golden rod",\$B8860B,"cadet blue",\$5F9EA0 ; 7
Data "pale turquoise",\$AFEEEE,"light sky blue",\$87CEFA,"lavender blush",\$FFF0F5,"blanched almond",\$FFEBCD,"light sea green",\$20B2AA,"tan",\$D2B48C,"lemon chiffon",\$FFFACD ; 7
Data "deep sky blue",\$00BFFF,"midnight blue",\$191970,"medium orchid",\$BA55D3,"medium purple",\$9370DB,"antique white",\$FAEBD7,"dark sea green",\$8FBC8F,"dark grey",\$A9A9A9 ; 7
Data "slate gray",\$708090,"alice blue",\$F0F8FF,"peach puff",\$FFDAB9,"misty rose",\$FFE4E1,"rosy brown",\$BC8F8F,"light cyan",\$E0FFFF,"maroon",\$800000,"blue violet",\$8A2BE2 ; 8
Data "dark gray",\$A9A9A9,"gainsboro",\$DCDCDC,"deep pink",\$FF1493,"corn silk",\$FFF8DC,"chocolate",\$D2691E,"sea shell",\$FFF5EE,"dark turquoise",\$00CED1,"firebrick",\$B22222 ; 8
Data "steel blue",\$4682B4,"dark khaki",\$BDB76B,"olive drab",\$6B8E23,"lawn green",\$7CFC00,"indian red",\$CD5C5C,"orange red",\$FF4500,"golden rod",\$DAA520,"magenta",\$FF00FF ; 8
Data "sea green",\$2E8B57,"turquoise",\$40E0D0,"dark blue",\$00008B,"light gray",\$D3D3D3,"light grey",\$D3D3D3,"light pink",\$FFB6C1,"mint cream",\$F5FFFA,"pale green",\$98FB98 ; 8
Data "medium blue",\$0000CD,"aqua marine",\$7FFFD4,"powder blue",\$B0E0E6,"light green",\$90EE90,"chart reuse",\$7FFF00,"indigo",\$4B0082,"silver",\$C0C0C0,"dark green",\$006400 ; 8
Data "lime green",\$32CD32,"slate blue",\$6A5ACD,"light blue",\$ADD8E6,"royal blue",\$4169E1,"dark orchid",\$9932CC,"honeydew",\$F0FFF0,"fuchsia",\$FF00FF,"papaya whip",\$FFEFD5 ; 8
Data "olive",\$808000,"coral",\$FF7F50,"khaki",\$F0E68C,"violet",\$EE82EE,"orchid",\$DA70D6,"purple",\$800080,"dark salmon",\$E9967A,"burly wood",\$DEB887,"yellow green",\$9ACD32 ; 9
Data "sky blue",\$87CEEB,"dark red",\$8B0000,"hot pink",\$FF69B4,"dim gray",\$696969,"dim grey",\$696969,"old lace",\$FDF5E6,"lavender",\$E6E6FA,"orange",\$FFA500,"gold",\$FFD700 ; 9
Data "yellow",\$FFFF00,"tomato",\$FF6347,"salmon",\$FA8072,"bisque",\$FFE4C4,"sienna",\$A0522D,"thistle",\$D8BFD8,"crimson",\$DC143C,"medium aqua marine",\$66CDAA,"teal",\$008080 ; 9
Data "plum",\$DDA0DD,"pink",\$FFC0CB,"snow",\$FFFAFA,"ivory",\$FFFFF0,"azure",\$F0FFFF,"wheat",\$F5DEB3,"beige",\$F5F5DC,"moccasin",\$FFE4B5,"light coral",\$F08080,"Aqua",\$00FFFF ; 10
Data"dark violet",\$9400D3,"medium turquoise",\$48D1CC,"corn flower blue",\$6495ED,"dark olive green",\$556B2F,"medium slate blue",\$7B68EE,"dark cyan",\$008B8B,"navy",\$000080 ; 7

Global G_VecColorTemplateCount% = 0
Global VecColorTemplateNames\$[255]
Global VecColorTemplateValues%[255]
Function VecRestoreColors()
Restore VecColor_Templates
Local ColName\$, Value%, c%
For c = 0 To G_VecColorTemplateCount-1
Next
End Function

Function VecTemplateColor.Vector(name\$)
Local c%, l\$ = Lower(name);
For c = 0 To G_VecColorTemplateCount-1
If (VecColorTemplateNames[c]=l) Then Return VecSetHex(NewVector(), VecColorTemplateValues[c]);
Next
Return NewVector(0,0,0);
End Function

Function VecTemplateColorId%(name\$)
Local c%, l\$ = Lower(name);
For c = 0 To G_VecColorTemplateCount-1
If (VecColorTemplateNames[c]=l) Then Return c;
Next
Return 0;
End Function

Function VecTemplateColorById.Vector(pColorId%)
If ((pColorId>=0) And (pColorId<G_VecColorTemplateCount)) Then Return VecSetHex(NewVector(), VecColorTemplateValues[pColorId]);
Return NewVector(0,0,0);
End Function

Bobysait(Posted 8 months ago)

The Quaternion part :
Code: [Select]

; -----------------------------------------
; - Quaternion -
; -----------------------------------------
Type Quaternion
Field W#, X#, Y#, Z#;
End Type
Function FreeQuaternion.Quaternion(q.Quaternion):Delete q:Return Null: End Function

; Constructors
Function NewQuaternion.Quaternion(w#=1,x#=0,y#=0,z#=0):Local q.Quaternion=New Quaternion: qW=w:qX=x:qY=y:q=z: Return q:End Function
Function QuatIdentity.Quaternion(q.Quaternion): qW=1:qX=0:qY=0:q=0:Return q :End Function
Function QuatCopy.Quaternion(q.Quaternion):Return NewQuaternion(qW,qX,qY,q):End Function
Function QuatFromArray.Quaternion(f#[3]):Return NewQuaternion(f[0],f[1],f[2],f[3]):End Function
Function QuatFromMat3.Quaternion ( m.Matrix3 )
Local t# = mXX+mYY+m, q.Quaternion = New Quaternion;
If( t>0.000001 )
t = Sqr( t+1.0 )*2.0;
qX = (mY-mY)/t;
qY = (mX-mX)/t;
q = (mYX-mXY)/t;
qW = t*.25;
ElseIf( mXX>mYY And mXX>m )
t=Sqr( mXX-mYY-m+1.0 )*2.0;
qX=t*.25;
qY=(mYX+mXY)/t;
q=(mX+mX)/t;
qW=(mY-mY)/t;
ElseIf( mYY>m )
t=Sqr( mYY-m-mXX+1.0 )*2;
qX=(mYX+mXY)/t;
qY=t*.25;
q=(mY+mY)/t;
qW=(mX-mX)/t;
Else
t=Sqr( m-mYY-mXX+1.0 )*2.0;
qX=(mX+mX)/t;
qY=(mY+mY)/t;
q=t*.25;
qW=(mYX-mXY)/t;
EndIf;
Return q;
End Function
Function QuatFromMat4.Quaternion ( m.Matrix4 )
Local t# = mXX+mYY+m, q.Quaternion = New Quaternion;
If( t>0.000001 )
t = Sqr( t+1.0 )*2.0;
qX = (mY-mY)/t;
qY = (mX-mX)/t;
q = (mYX-mXY)/t;
qW = t*.25;
ElseIf( mXX>mYY And mXX>m )
t=Sqr( mXX-mYY-m+1.0 )*2.0;
qX=t*.25;
qY=(mYX+mXY)/t;
q=(mX+mX)/t;
qW=(mY-mY)/t;
ElseIf( mYY>m )
t=Sqr( mYY-m-mXX+1.0 )*2;
qX=(mYX+mXY)/t;
qY=t*.25;
q=(mY+mY)/t;
qW=(mX-mX)/t;
Else
t=Sqr( m-mYY-mXX+1.0 )*2.0;
qX=(mX+mX)/t;
qY=(mY+mY)/t;
q=t*.25;
qW=(mYX-mXY)/t;
EndIf;
Return q;
End Function
Function QuatFromPitch.Quaternion(p#) : Return NewQuaternion( Float(Cos(-p*.5)), Float(Sin(-p*.5)), 0.0,0.0 ) : End Function
Function QuatFromYaw.Quaternion(y#) : Return NewQuaternion( Float(Cos( y*.5)), 0.0, Float(Sin( y*.5)), 0.0 ) : End Function
Function QuatFromRoll.Quaternion(r#) : Return NewQuaternion( Float(Cos(-r*.5)), 0.0, 0.0, Float(Sin(-r*.5)) ) : End Function
Function QuatFromEulerX.Quaternion(p#) : Return NewQuaternion( Float(Cos(-p*.5)), Float(Sin(-p*.5)), 0.0,0.0 ) : End Function
Function QuatFromEulerY.Quaternion(y#) : Return NewQuaternion( Float(Cos( y*.5)), 0.0, Float(Sin( y*.5)), 0.0 ) : End Function
Function QuatFromEulerZ.Quaternion(r#) : Return NewQuaternion( Float(Cos(-r*.5)), 0.0, 0.0, Float(Sin(-r*.5)) ) : End Function
Function QuatFromEuler.Quaternion(v.Vector)
Local lX#=vX*0.5,lY#=vY*0.5,lZ#=v*0.5, cx#=Cos(lX),sx#=Sin(lX),cy#=Cos(lY),sy#=Sin(lY),cz#=Cos(lZ),sz#=Sin(lZ);
Local cxcy#=cx*cy, cxsy#=cx*sy, sxsy#=sx*sy, sxcy#=sx*cy;
Return QuatSelfNormalize(NewQuaternion(+cxcy*cz-sxsy*sz, +cxsy*sz-sxcy*cz, +cxsy*cz+sxcy*sz, -sxsy*cz-cxcy*sz));
End Function
Function QuatFromEulerXYZ.Quaternion(x#,y#,z#)
Local lX#=x*0.5,lY#=y*0.5,lZ#=z*0.5, cx#=Cos(lX),sx#=Sin(lX),cy#=Cos(lY),sy#=Sin(lY),cz#=Cos(lZ),sz#=Sin(lZ);
Local cxcy#=cx*cy, cxsy#=cx*sy, sxsy#=sx*sy, sxcy#=sx*cy;
Return QuatSelfNormalize(NewQuaternion(+cxcy*cz-sxsy*sz, +cxsy*sz-sxcy*cz, +cxsy*cz+sxcy*sz, -sxsy*cz-cxcy*sz));
End Function
; set
Function QuatSet.Quaternion(q.Quaternion, w#=1,x#=0,y#=0,z#=0):qW=w:qX=x:qY=y:q=z:Return q:End Function
Function QuatSetQuaternion.Quaternion(q.Quaternion, a.Quaternion):qW=aW:qX=aX:qY=aY:q=az:Return q:End Function
Function QuatAssign.Quaternion(q.Quaternion, a.Quaternion):qW=aW:qX=aX:qY=aY:q=az:Return q:End Function
Function QuatSetArray.Quaternion(q.Quaternion, a#[3]):qW=a[0]:qX=a[1]:qY=a[2]:q=a[3]:Return q:End Function

Function QuatSetMat3.Quaternion ( q.Quaternion, m.Matrix3 )
Local t# = mXX+mYY+m;
If( t>0.000001 )
t = Sqr( t+1.0 )*2.0;
qX = (mY-mY)/t;
qY = (mX-mX)/t;
q = (mYX-mXY)/t;
qW = t*.25;
ElseIf( mXX>mYY And mXX>m )
t=Sqr( mXX-mYY-m+1.0 )*2.0;
qX=t*.25;
qY=(mYX+mXY)/t;
q=(mX+mX)/t;
qW=(mY-mY)/t;
ElseIf( mYY>m )
t=Sqr( mYY-m-mXX+1.0 )*2;
qX=(mYX+mXY)/t;
qY=t*.25;
q=(mY+mY)/t;
qW=(mX-mX)/t;
Else
t=Sqr( m-mYY-mXX+1.0 )*2.0;
qX=(mX+mX)/t;
qY=(mY+mY)/t;
q=t*.25;
qW=(mYX-mXY)/t;
EndIf;
Return q;
End Function
Function QuatSetMat4.Quaternion ( q.Quaternion, m.Matrix4 )
Local t# = mXX+mYY+m;
If( t>0.000001 )
t = Sqr( t+1.0 )*2.0;
qX = (mY-mY)/t;
qY = (mX-mX)/t;
q = (mYX-mXY)/t;
qW = t*.25;
ElseIf( mXX>mYY And mXX>m )
t=Sqr( mXX-mYY-m+1.0 )*2.0;
qX=t*.25;
qY=(mYX+mXY)/t;
q=(mX+mX)/t;
qW=(mY-mY)/t;
ElseIf( mYY>m )
t=Sqr( mYY-m-mXX+1.0 )*2;
qX=(mYX+mXY)/t;
qY=t*.25;
q=(mY+mY)/t;
qW=(mX-mX)/t;
Else
t=Sqr( m-mYY-mXX+1.0 )*2.0;
qX=(mX+mX)/t;
qY=(mY+mY)/t;
q=t*.25;
qW=(mYX-mXY)/t;
EndIf;
Return q;
End Function
Function QuatSetPitch.Quaternion(q.Quaternion,p#)
qW=Cos(-p*.5): qX=Sin(-p*.5): qY=0: q=0: Return q;
End Function
Function QuatSetEulerX.Quaternion(q.Quaternion,p#)
qW=Cos(-p*.5): qX=Sin(-p*.5): qY=0: q=0: Return q;
End Function
Function QuatSetYaw.Quaternion(q.Quaternion, y#)
qW=Float(Cos( y*.5)): qX=0.0: qY=Float(Sin( y*.5)): q=0.0 : Return q;
End Function
Function QuatSetEulerY.Quaternion(q.Quaternion, y#)
qW=Float(Cos( y*.5)): qX=0.0: qY=Float(Sin( y*.5)): q=0.0 : Return q;
End Function
Function QuatSetRoll.Quaternion(q.Quaternion,r#)
qW=Float(Cos(-r*.5)) : qX=0 : qY=0 : q=Float(Sin(-r*.5)): Return q;
End Function
Function QuatSetEulerZ.Quaternion(q.Quaternion,z#)
qW=Float(Cos(-z*.5)) : qX=0 : qY=0 : q=Float(Sin(-z*.5)): Return q;
End Function

Function QuatSetEuler.Quaternion(q.Quaternion,v.Vector)
Local lX#=vX*0.5,lY#=vY*0.5,lZ#=v*0.5, cx#=Cos(lX),sx#=Sin(lX),cy#=Cos(lY),sy#=Sin(lY),cz#=Cos(lZ),sz#=Sin(lZ);
Local cxcy#=cx*cy, cxsy#=cx*sy, sxsy#=sx*sy, sxcy#=sx*cy;
qW=+cxcy*cz-sxsy*sz;
qX=+cxsy*sz-sxcy*cz;
qY=+cxsy*cz+sxcy*sz;
q=-sxsy*cz-cxcy*sz;
Return QuatSelfNormalize(q);
End Function
Function QuatSetEulerXYZ.Quaternion(q.Quaternion,x#,y#,z#)
Local lX#=x*0.5,lY#=y*0.5,lZ#=z*0.5, cx#=Cos(lX),sx#=Sin(lX),cy#=Cos(lY),sy#=Sin(lY),cz#=Cos(lZ),sz#=Sin(lZ);
Local cxcy#=cx*cy, cxsy#=cx*sy, sxsy#=sx*sy, sxcy#=sx*cy;
qW=+cxcy*cz-sxsy*sz;
qX=+cxsy*sz-sxcy*cz;
qY=+cxsy*cz+sxcy*sz;
q=-sxsy*cz-cxcy*sz;
Return QuatSelfNormalize(q);
End Function
Function QuatInvert.Quaternion(q.Quaternion) : Return NewQuaternion(qW,-qX,-qY,-q) : End Function
Function QuatSelfInvert.Quaternion(q.Quaternion) : qX=-qX:qY=-qY:q=-q :Return q : End Function

; Convert
Function QuatToMat3.Matrix3(q.Quaternion)
Local m.Matrix3 = New Matrix3;
mX = VecSet(New Vector, 1.0-2.0*(qY*qY+q*q),2.0*(qX*qY-qW*q),2.0*(qX*q+qW*qY));
mY = VecSet(New Vector, 2.0*(qX*qY+qW*q),1.0-2.0*(qX*qX+q*q),2.0*(qY*q-qW*qX));
m = VecSet(New Vector, 2.0*(qX*q-qW*qY),2.0*(qY*q+qW*qX),1.0-2.0*(qX*qX+qY*qY));
Return m;
End Function
Function QuatToMat4.Matrix4(q.Quaternion)
Local m.Matrix4 = New Matrix4;
mX = VecSet(New Vector, 1.0-2.0*(qY*qY+q*q),2.0*(qX*qY-qW*q),2.0*(qX*q+qW*qY));
mY = VecSet(New Vector, 2.0*(qX*qY+qW*q),1.0-2.0*(qX*qX+q*q),2.0*(qY*q-qW*qX));
m = VecSet(New Vector, 2.0*(qX*q-qW*qY),2.0*(qY*q+qW*qX),1.0-2.0*(qX*qX+qY*qY));
mP = NewVector();
Return m;
End Function

; Get
Function QuatW#(q.Quaternion):Return qW: End Function
Function QuatX#(q.Quaternion):Return qX: End Function
Function QuatY#(q.Quaternion):Return qY: End Function
Function QuatZ#(q.Quaternion):Return q: End Function
Function QuatLength#(q.Quaternion):Return Sqr(qW*qW+qX*qX+qY*qY+q*q): End Function
Function QuatSqLength#(q.Quaternion):Return qW*qW+qX*qX+qY*qY+q*q: End Function
Function QuatScale.Quaternion(q.Quaternion,s#):Return NewQuaternion(qW*s,qX*s,qY*s,q*s):End Function
Function QuatSelfScale.Quaternion(q.Quaternion,s#):qW=qW*s:qX=qX*s:qY=qY*s:q=q*s:Return q:End Function
Function QuatNormalize.Quaternion(q.Quaternion)
Local l#=1.0/Sqr(qW*qW + qX*qX + qY*qY + q*q);
If (l>0.000001) Then Return NewQuaternion(qW*l,qX*l,qY*l,q*l);
Return NewQuaternion(1);
End Function
Function QuatSelfNormalize.Quaternion(q.Quaternion)
Local l#=1.0/Sqr(qW*qW + qX*qX + qY*qY + q*q);
If (l>0.000001) Then qW=qW*l:qX=qX*l:qY=qY*l:q=q*l:Return q;
Return QuatSet(q,1,0,0,0);
End Function
Local q_.Quaternion=New Quaternion: q_W=qW+vW: q_X=qX+vX: q_Y=qY+vY: q_=q+v: Return q_;
End Function
qW=qW+vW: qX=qX+vX: qY=qY+vY: q=q+v : Return q;
End Function
Function QuatSub.Quaternion(q.Quaternion, v.Quaternion)
Local q_.Quaternion=New Quaternion: q_W=qW-vW: q_X=qX-vX: q_Y=qY-vY: q_=q-v: Return q_;
End Function
Function QuatSelfSub.Quaternion(q.Quaternion, v.Quaternion)
qW=qW-vW: qX=qX-vX: qY=qY-vY: q=q-v : Return q;
End Function
Function QuatMul.Quaternion( q.Quaternion, v.Quaternion)
Local q_.Quaternion = New Quaternion;
q_W = qW*vW - qX*vX - qY*vY - q*v;
q_X = qW*vX + qX*vW - qY*v + q*vY;
q_Y = qW*vY + qX*v + qY*vW - q*vX;
q_ = qW*v - qX*vY + qY*vX + q*vW;
Return q_;
End Function
Function QuatSelfMul.Quaternion(q.Quaternion, v.Quaternion)
Local lw#=qW, lx#=qX, ly#=qY, lz#=q;
qW = lw*vW - lx*vX - ly*vY - lz*v;
qX = lw*vX + lx*vW - ly*v + lz*vY;
qY = lw*vY + lx*v + ly*vW - lz*vX;
q = lw*v - lx*vY + ly*vX + lz*vW;
Return q;
End Function
Function QuatMulSelf.Quaternion(q.Quaternion, v.Quaternion)
Local lw#=qW, lx#=qX, ly#=qY, lz#=q;
qW = vW*lw - vX*lx - vY*ly - v*lz;
qX = vW*lx + vX*lw - vY*lz + v*ly;
qY = vW*ly + vX*lz + vY*lw - v*lx;
q = vW*lz - vX*ly + vY*lx + v*lw;
Return v;
End Function

; Transform
Function QuatMulV.Vector(q.Quaternion, v.Vector)
Local qw# = - qX*vX - qY*vY - q*v;
Local qx# = + qW*vX - qY*v + q*vY;
Local qy# = + qW*vY + qX*v - q*vX;
Local qz# = + qW*v - qX*vY + qY*vX;
Local o.Vector = New Vector;
oX = - qw*qX + qx*qW + qy*q - qz*qY;
oY = - qw*qY - qx*q + qy*qW + qz*qX;
o = - qw*q + qx*qY - qy*qX + qz*qW;
oW = 1
Return o;
End Function
Function QuatMulXYZ.Vector(q.Quaternion, pX#, pY#, pZ#)
Local qw# = - qX*pX - qY*pY - q*pZ;
Local qx# = + qW*pX - qY*pZ + q*pY;
Local qy# = + qW*pY + qX*pZ - q*pX;
Local qz# = + qW*pZ - qX*pY + qY*pX;
Local o.Vector = New Vector;
oX = - qw*qX + qx*qW + qy*q - qz*qY;
oY = - qw*qY - qx*q + qy*qW + qz*qX;
o = - qw*q + qx*qY - qy*qX + qz*qW;
oW = 1
Return o;
End Function
Function QuatSelfMulV.Vector(q.Quaternion,v.Vector)
Local qw# = - qX*vX - qY*vY - q*v;
Local qx# = + qW*vX - qY*v + q*vY;
Local qy# = + qW*vY + qX*v - q*vX;
Local qz# = + qW*v - qX*vY + qY*vX;
vX = - qw*qX + qx*qW + qy*q - qz*qY;
vY = - qw*qY - qx*q + qy*qW + qz*qX;
v = - qw*q + qx*qY - qy*qX + qz*qW;
Return v;
End Function

Function QuatMulVX.Vector(q.Quaternion, x#)
Local v.Vector = New Vector;
vX = qX*x*qX + qW*x*qW - q*x*q - qY*x*qY;
vY = qX*x*qY - qW*x*q - q*x*qW + qY*x*qX;
v = qX*x*q + qW*x*qY + q*x*qX + qY*x*qW;
vW = 1
Return v;
End Function
Function QuatMulVY.Vector(q.Quaternion,y#)
Local v.Vector = New Vector;
vX = qY*y*qX + q*y*qW + qW*y*q + qX*y*qY;
vY = qY*y*qY - q*y*q + qW*y*qW - qX*y*qX;
v = qY*y*q + q*y*qY - qW*y*qX - qX*y*qW;
vW = 1
Return v;
End Function
Function QuatMulVZ.Vector(q.Quaternion,z#)
Local v.Vector = New Vector;
vX = q*z*qX - qY*z*qW + qX*z*q - qW*z*qY;
vY = q*z*qY + qY*z*q + qX*z*qW + qW*z*qX;
v = q*z*q - qY*z*qY - qX*z*qX + qW*z*qW;
vW = 1
Return v;
End Function

Function QuatToEuler.Vector(q.Quaternion): Local v.Vector = New Vector: vX = QuatPitch(q): vY = QuatYaw(q): v = QuatRoll(q): vW = 1: Return v: End Function
Function QuatPitch#(q.Quaternion)
Local vx# = 2.0*(qX*q-qW*qY), vz# = 1.0-2.0*(qX*qX+qY*qY): Return -ATan2( 2.0*(qY*q+qW*qX), Sqr( vx*vx+vz*vz ) );
End Function
Function QuatYaw#(q.Quaternion) : Return -ATan2( qX*q-qW*qY, .5 - qX*qX - qY*qY ): End Function
Function QuatRoll#(q.Quaternion) : Return  ATan2( qX*qY-qW*q, .5 - qX*qX - q*q ): End Function
Function QuatI.Vector(q.Quaternion) : Return NewVector( 1.0-2.0*(qY*qY+q*q), 2.0*(qX*qY-qW*q), 2.0*(qX*q+qW*qY) ) : End Function
Function QuatRight.Vector(q.Quaternion) : Return NewVector( 1.0-2.0*(qY*qY+q*q), 2.0*(qX*qY-qW*q), 2.0*(qX*q+qW*qY) ) : End Function
Function QuatLeft.Vector(q.Quaternion) : Return NewVector(-1.0+2.0*(qY*qY+q*q),-2.0*(qX*qY-qW*q),-2.0*(qX*q+qW*qY) ) : End Function
Function QuatJ.Vector(q.Quaternion) : Return NewVector( 2.0*(qX*qY+qW*q), 1.0-2.0*(qX*qX+q*q), 2.0*(qY*q-qW*qX) ) : End Function
Function QuatUp.Vector(q.Quaternion) : Return NewVector( 2.0*(qX*qY+qW*q), 1.0-2.0*(qX*qX+q*q), 2.0*(qY*q-qW*qX) ) : End Function
Function QuatDown.Vector(q.Quaternion) : Return NewVector(-2.0*(qX*qY+qW*q),-1.0+2.0*(qX*qX+q*q),-2.0*(qY*q-qW*qX) ) : End Function
Function QuatK.Vector(q.Quaternion) : Return NewVector( 2.0*(qX*q-qW*qY), 2.0*(qY*q+qW*qX), 1.0-2.0*(qX*qX+qY*qY) ) : End Function
Function QuatFront.Vector(q.Quaternion) : Return NewVector( 2.0*(qX*q-qW*qY), 2.0*(qY*q+qW*qX), 1.0-2.0*(qX*qX+qY*qY) ) : End Function
Function QuatBack.Vector(q.Quaternion) : Return NewVector(-2.0*(qX*q-qW*qY),-2.0*(qY*q+qW*qX),-1.0+2.0*(qX*qX+qY*qY) ) : End Function
Function QuatArray.Quaternion(q.Quaternion,f#[3]): f[0]=qW : f[1]=qX : f[2]=qY : f[3]=q : Return q: End Function
Function QuatToString\$(q.Quaternion) : Return qW+" "+qX+" "+qY+" "+q : End Function

Bobysait(Posted 8 months ago)

The Matrix3 part :
Code: [Select]

; -----------------------------------------
; - 3*3 Matrix -
; -----------------------------------------
Type Matrix3
Field X.Vector
Field Y.Vector
Field Z.Vector
End Type

Function FreeMatrix3.Matrix3(m.Matrix3):Delete mX:Delete mY:Delete m: Delete m:Return Null:End Function
; Constructors
Function NewMatrix3.Matrix3():Local m.Matrix3=New Matrix3: mX=New Vector: mY=New Vector: m=New Vector: Return Mat3Identity(m):End Function
Function Mat3Copy.Matrix3(Self.Matrix3):Local o.Matrix3=New Matrix3:oX=VecCopy(SelfX):oY=VecCopy(SelfY):o=VecCopy(Self):Return o:End Function
Function Mat3FromVectors.Matrix3(x.Vector,y.Vector,z.Vector):Local m.Matrix3=New Matrix3:mX=VecCopy(x):mY=VecCopy(y):m=VecCopy(z):Return m:End Function
; Set
Function Mat3Identity.Matrix3(Self.Matrix3):VecSet(SelfX,1,0,0): VecSet(SelfY, 0,1,0): VecSet(Self, 0,0,1): Return Self:End Function
Function Mat3Set.Matrix3(Self.Matrix3,xx#,xy#,xz#,yx#,yy#,yz#,zx#,zy#,zz#)
SelfXX=xx:SelfXY=xy:SelfX=xz;
SelfYX=yx:SelfYY=yy:SelfY=yz;
SelfX=zx:SelfY=zy:Self=zz;
Return Self;
End Function
Function Mat3SetMat3.Matrix3(Self.Matrix3,q.Matrix3)
SelfXX=qXX:SelfXY=qXY:SelfX=qX;
SelfYX=qYX:SelfYY=qYY:SelfY=qY;
SelfX=qX:SelfY=qY:Self=q;
Return Self;
End Function
Function Mat3Assign.Matrix3(Self.Matrix3, q.Matrix3)
SelfXX = qXX: SelfXY = qXY: SelfX = qX: SelfXW = qXW;
SelfYX = qYX: SelfYY = qYY: SelfY = qY: SelfYW = qYW;
SelfX = qX: SelfY = qY: Self = q: SelfW = qW;
Return Self;
End Function
Function Mat3SetArray.Matrix3(Self.Matrix3,f#[8])
SelfXX=f[0]:SelfXY=f[1]:SelfX=f[2];
SelfYX=f[3]:SelfYY=f[4]:SelfY=f[5];
SelfX=f[6]:SelfY=f[7]:Self=f[8];
Return Self;
End Function
Function Mat3SetVectors.Matrix3(Self.Matrix3,x.Vector,y.Vector,z.Vector):VecAssign(SelfX,x):VecAssign(SelfY,y):VecAssign(Self,z):Return Self:End Function
Function Mat3Determinant#(m.Matrix3):Return mXX*(mYY*m-mY*mY )-mXY*(mYX*m-mY*mX )+mX*(mYX*mY-mYY*mX ):End Function
Function Mat3Invert.Matrix3 (Self.Matrix3)
Local t# = 1.0 / Mat3Determinant(Self);
Local o.Matrix3 = New Matrix3;
oX= VecSet(New Vector, t*(SelfYY*Self-SelfY*SelfY),-t*(SelfXY*Self-SelfX*SelfY), t*(SelfXY*SelfY-SelfX*SelfYY));
oY= VecSet(New Vector,-t*(SelfYX*Self-SelfY*SelfX), t*(SelfXX*Self-SelfX*SelfX),-t*(SelfXX*SelfY-SelfX*SelfYX));
o= VecSet(New Vector, t*(SelfYX*SelfY-SelfYY*SelfX),-t*(SelfXX*SelfY-SelfXY*SelfX), t*(SelfXX*SelfYY-SelfXY*SelfYX));
Return o;
End Function
Function Mat3SelfInvert.Matrix3 (Self.Matrix3)
Local t# = 1.0 / Mat3Determinant(Self);
Local lxx#=SelfXX, lxy#=SelfXY, lxz#=SelfX;
Local lyx#=SelfYX, lyy#=SelfYY, lyz#=SelfY;
Local lzx#=SelfX, lzy#=SelfY, lzz#=Self;
SelfXX= t*(lyy*lzz-lyz*lzy);
SelfXY=-t*(lxy*lzz-lxz*lzy);
SelfX= t*(lxy*lyz-lxz*lyy);
SelfYX=-t*(lyx*lzz-lyz*lzx);
SelfYY= t*(lxx*lzz-lxz*lzx);
SelfY=-t*(lxx*lyz-lxz*lyx);
SelfX= t*(lyx*lzy-lyy*lzx);
SelfY=-t*(lxx*lzy-lxy*lzx);
Self= t*(lxx*lyy-lxy*lyx);
Return Self;
End Function
Function Mat3Transpose.Matrix3(Self.Matrix3)
Local m.Matrix3= NewMatrix3();
mXX=SelfXX: mXY=SelfYX: mX=SelfX;
mYX=SelfXY: mYY=SelfYY: mY=SelfY;
mX=SelfX: mY=SelfY: m=Self;
Return m;
End Function
Function Mat3SelfTranspose.Matrix3(Self.Matrix3)
Local t#;
t = SelfXY: SelfXY=SelfYX: SelfYX=t;
t = SelfX: SelfX=SelfX: SelfX=t;
t = SelfY: SelfY=SelfY: SelfY=t;
Return Self;
End Function
Function Mat3Mul.Matrix3(Self.Matrix3,m.Matrix3)
Local o.Matrix3 = New Matrix3;
oXX=SelfXX*mXX+SelfYX*mXY+SelfX*mX;
oXY=SelfXY*mXX+SelfYY*mXY+SelfY*mX;
oX=SelfX*mXX+SelfY*mXY+Self*mX;
oYX=SelfXX*mYX+SelfYX*mYY+SelfX*mY;
oYY=SelfXY*mYX+SelfYY*mYY+SelfY*mY;
oY=SelfX*mYX+SelfY*mYY+Self*mY;
oX=SelfXX*mX+SelfYX*mY+SelfX*m;
oY=SelfXY*mX+SelfYY*mY+SelfY*m;
o=SelfX*mX+SelfY*mY+Self*m;
Return o;
End Function
Function Mat3SelfMul.Matrix3(Self.Matrix3,m.Matrix3)
Local lxx#=SelfXX, lxy#=SelfXY, lxz#=SelfX;
Local lyx#=SelfYX, lyy#=SelfYY, lyz#=SelfY;
Local lzx#=SelfX, lzy#=SelfY, lzz#=Self;
SelfXX=lxx*mXX+lyx*mXY+lzx*mX;
SelfXY=lxy*mXX+lyy*mXY+lzy*mX;
SelfX=lxz*mXX+lyz*mXY+lzz*mX;
SelfYX=lxx*mYX+lyx*mYY+lzx*mY;
SelfYY=lxy*mYX+lyy*mYY+lzy*mY;
SelfY=lxz*mYX+lyz*mYY+lzz*mY;
SelfX=lxx*mX+lyx*mY+lzx*m;
SelfY=lxy*mX+lyy*mY+lzy*m;
Self=lxz*mX+lyz*mY+lzz*m;
Return Self;
End Function
Function Mat3Transform.Vector(Self.Matrix3,v.Vector)
Return VecTransformM3(v,Self);
End Function
Function Mat3InvertTransform.Vector(Self.Matrix3,v.Vector)
Local m.Matrix3 = Mat3Invert(Self);
Local o.Vector = VecTransformM3(v,m);
FreeMatrix3(m)
Return o;
End Function
Function Mat3Normalize.Matrix3(Self.Matrix3)
Local tX# = 1.0/Sqr(SelfXX*SelfXX+SelfXY*SelfXY+SelfX*SelfX);
Local tY# = 1.0/Sqr(SelfYX*SelfYX+SelfYY*SelfYY+SelfY*SelfY);
Local tZ# = 1.0/Sqr(SelfX*SelfX+SelfY*SelfY+Self*Self);
Local m.Matrix3 = New Matrix3;
mX = VecSet(New Vector, SelfXX*tX, SelfXY*tX, SelfX*tX);
mY = VecSet(New Vector, SelfYX*tY, SelfYY*tY, SelfY*tY);
m = VecSet(New Vector, SelfX*tZ, SelfY*tZ, Self*tZ);
Return m;
End Function
Function Mat3SelfNormalize.Matrix3(Self.Matrix3)
VecSelfNormalize(SelfX);
VecSelfNormalize(SelfY);
VecSelfNormalize(Self);
Return Self;
End Function
Function Mat3ToQuat.Quaternion(Self.Matrix3)
Return QuatFromMat3(Self);
End Function
Function Mat3ToMat4.Matrix4(Self.Matrix3)
Local m.Matrix4 = New Matrix4;
mX = VecCopy(SelfX): mXW = 0.0;
mY = VecCopy(SelfY): mYW = 0.0;
m = VecCopy(Self): mW = 0.0;
mP = NewVector();
Return m;
End Function
Function Mat3MulV.Vector(Self.Matrix3,v.Vector)
Return NewVector(vX*SelfXX+vY*SelfYX+v*SelfX,vX*SelfXY+vY*SelfYY+v*SelfY,vX*SelfX+vY*SelfY+v*Self);
End Function
Function Mat3SelfMulV.Vector(Self.Matrix3,v.Vector)
Return VecSet(v,vX*SelfXX+vY*SelfYX+v*SelfX,vX*SelfXY+vY*SelfYY+v*SelfY,vX*SelfX+vY*SelfY+v*Self);
End Function
Function Mat3Row.Vector(Self.Matrix3,pCol%)
If pCol=2 Then Return VecCopy(Self)
If pCol=1 Then Return VecCopy(SelfY)
Return VecCopy(SelfX)
End Function
Function Mat3Cell#(Self.Matrix3,j%,i%)
If j=2
If i=2 Then Return Self
If i=1 Then Return SelfY
Return SelfX
ElseIf j=1
If i=2 Then Return SelfY
If i=1 Then Return SelfYY
Return SelfYX
EndIf
If i=2 Then Return SelfX
If i=1 Then Return SelfXY
Return SelfXX
End Function
Function Mat3I.Vector(Self.Matrix3)
Return VecCopy(SelfX);
End Function
Function Mat3J.Vector(Self.Matrix3)
Return VecCopy(SelfY);
End Function
Function Mat3K.Vector(Self.Matrix3)
Return VecCopy(Self);
End Function
Function Mat3Array.Matrix3(Self.Matrix3,f#[8])
f[0]=SelfXX:f[1]=SelfXY:f[2]=SelfX
f[3]=SelfYX:f[4]=SelfYY:f[5]=SelfY
f[6]=SelfX:f[7]=SelfY:f[8]=Self
Return Self;
End Function
Function Mat3ToString\$(Self.Matrix3)
Return VecToString(SelfX)+Chr(13)+VecToString(SelfY)+Chr(13)+VecToString(Self);
End Function
Function Mat3SetRotation.Matrix3(Self.Matrix3,q.Quaternion)
SelfXX = 1.0-2.0*(qY*qY+q*q);
SelfXY =     2.0*(qX*qY-qW*q);
SelfX =     2.0*(qX*q+qW*qY);
SelfYX =     2.0*(qX*qY+qW*q);
SelfYY = 1.0-2.0*(qX*qX+q*q);
SelfY =     2.0*(qY*q-qW*qX);
SelfX =     2.0*(qX*q-qW*qY);
SelfY =     2.0*(qY*q+qW*qX);
Self = 1.0-2.0*(qX*qX+qY*qY);
Return Self;
End Function
Function Mat3SetScale.Matrix3(Self.Matrix3,s.Vector)
VecSelfScale(SelfX, sX);
VecSelfScale(SelfY, sY);
VecSelfScale(Self, s);
Return Self;
End Function

Bobysait(Posted 8 months ago)

The Matrix4 part :[code]

; -----------------------------------------
; - 4*4 Matrix -
; (actually, Vec objects contain 4 components)
; -----------------------------------------
Type Matrix4
Field X.Vector
Field Y.Vector
Field Z.Vector
Field P.Vector
End Type

Function FreeMatrix4.Matrix4(m.Matrix4):Delete mX:Delete mY:Delete m:Delete mP: Delete m: Return Null:End Function
Function NewMatrix4.Matrix4()
Local m.Matrix4=New Matrix4;
mX=VecSet(New Vector, 1,0,0): mXW=0;
mY=VecSet(New Vector, 0,1,0): mYW=0;
m=VecSet(New Vector, 0,0,1): mW=0;
mP=NewVector(0,0,0): mPW=1;
Return m
End Function
Function Mat4Identity.Matrix4(m.Matrix4):mX=VecSet(New Vector, 1,0,0): mY=VecSet(New Vector, 0,1,0): m=VecSet(New Vector,0,0,1): mP=NewVector(0,0,0):Return m:End Function
Function Mat4SelfTranslate.Matrix4(m.Matrix4,x#,y#,z#):VecTranslate(mP,x,y,z):Return m:End Function
Function Mat4FromAxis.Matrix4(x.Vector, y.Vector, z.Vector, p.Vector)
Local m.Matrix4 = New Matrix4;
mX = VecCopy(x): mXW=0;
mY = VecCopy(y): mYW=0;
m = VecCopy(z): mW=0;
If (p<>Null)
mP = VecCopy(p);
Else
mP = NewVector();
EndIf;
mPW=1;
Return m;
End Function
Function Mat4SetMat4.Matrix4(Self.Matrix4, q.Matrix4)
SelfXX = qXX: SelfXY = qXY: SelfX = qX: SelfXW = qXW;
SelfYX = qYX: SelfYY = qYY: SelfY = qY: SelfYW = qYW;
SelfX = qX: SelfY = qY: Self = q: SelfW = qW;
SelfPX = qPX: SelfPY = qPY: SelfP = qP: SelfPW = qPW;
Return Self;
End Function
Function Mat4Assign.Matrix4(Self.Matrix4, q.Matrix4)
SelfXX = qXX: SelfXY = qXY: SelfX = qX: SelfXW = qXW;
SelfYX = qYX: SelfYY = qYY: SelfY = qY: SelfYW = qYW;
SelfX = qX: SelfY = qY: Self = q: SelfW = qW;
SelfPX = qPX: SelfPY = qPY: SelfP = qP: SelfPW = qPW;
Return Self;
End Function
Function Mat4Set.Matrix4( Self.Matrix4, pXX#,pXY#,pXZ#,pXW#, pYX#,pYY#,pYZ#,pYW#, pZX#,pZY#,pZZ#,pZW#, pPX#,pPY#,pPZ#,pWW# )
SelfXX = pXX: SelfXY = pXY: SelfX = pXZ: SelfXW = pXW;
SelfYX = pYX: SelfYY = pYY: SelfY = pYZ: SelfYW = pYW;
SelfX = pZX: SelfY = pZY: Self = pZZ: SelfW = pZW;
SelfPX = pPX: SelfPY = pPY: SelfP = pPZ: SelfPW = pWW;
Return Self;
End Function
Function Mat4SetMat3.Matrix4(Self.Matrix4,m.Matrix4)
SelfXX=mXX:SelfXY=mXY:SelfX=mX:SelfXW=mXW;
SelfYX=mYX:SelfYY=mYY:SelfY=mY:SelfYW=mYW;
SelfX=mX:SelfY=mY:Self=m:SelfW=mW;
SelfPX=mPX:SelfPY=mPY:SelfP=mP:SelfPW=mPW;
Return Self;
End Function
Function Mat4SetArray.Matrix4(Self.Matrix4,pF#[15])
SelfXX = pF[ 0]: SelfXY = pF[ 1]: SelfX = pF[ 2]: SelfXW = pF[ 3];
SelfYX = pF[ 4]: SelfYY = pF[ 5]: SelfY = pF[ 6]: SelfYW = pF[ 7];
SelfX = pF[ 8]: SelfY = pF[ 9]: Self = pF[10]: SelfW = pF[11];
SelfPX = pF[12]: SelfPY = pF[13]: SelfP = pF[14]: SelfPW = pF[15];
Return Self;
End Function
Function Mat4Invert.Matrix4 ( Self.Matrix4 )
Local m.Matrix4 = NewMatrix4();
Local t#;
Local lx# = SelfXY*SelfY - SelfX*SelfYY;
Local ly# = SelfX*SelfYX - SelfXX*SelfY;
Local lz# = SelfXX*SelfYY - SelfXY*SelfYX;
t = 1.0 / (SelfX*lx + SelfY*ly + Self*lz);
mX = t*lx;
mY = t*ly;
m = t*lz;
mP = -(mX*SelfPX + mY*SelfPY + m*SelfP);
mXX = t*(Self*SelfYY-SelfY*SelfY);
mYX = t*(SelfX*SelfY-SelfYX*Self);
mX = t*(SelfY*SelfYX-SelfYY*SelfX);
mPX = -(mXX*SelfPX + mYX*SelfPY + mX*SelfP);
mXY = t*(SelfX*SelfY-SelfXY*Self);
mYY = t*(SelfXX*Self-SelfX*SelfX);
mY = t*(SelfXY*SelfX-SelfXX*SelfY);
mPY = -(mXY*SelfPX + mYY*SelfPY + mY*SelfP);
mXW = 0.0: mYW = 0.0: mW = 0.0: mPW = 1.0;
Return m;
End Function
Function Mat4SelfInvert.Matrix4 ( Self.Matrix4 )
Local t#;
Local mxx#=SelfXX, mxy#=SelfXY, mxz#=SelfX;
Local myx#=SelfYX, myy#=SelfYY, myz#=SelfY;
Local mzx#=SelfX, mzy#=SelfY, mzz#=Self;
Local mpx#=SelfPX, mpy#=SelfPY, mpz#=SelfP;
Local lx# = mxy*myz - mxz*myy;
Local ly# = mxz*myx - mxx*myz;
Local lz# = mxx*myy - mxy*myx;
t = 1.0 / (mzx*lx + mzy*ly + mzz*lz);
SelfX = t*lx;
SelfY = t*ly;
Self = t*lz;
SelfP = -(SelfX*mpx + SelfY*mpy + Self*mpz);
SelfXX = t*(mzz*myy-myz*mzy);
SelfYX = t*(mzx*myz-myx*mzz);
SelfX = t*(mzy*myx-myy*mzx);
SelfPX = -(SelfXX*mpx + SelfYX*mpy + SelfX*mpz);
SelfXY = t*(mxz*mzy-mxy*mzz);
SelfYY = t*(mxx*mzz-mxz*mzx);
SelfY = t*(mxy*mzx-mxx*mzy);
SelfPY = -(SelfXY*mpx + SelfYY*SelfPY + SelfY*mpz);
SelfXW = 0.0: SelfYW = 0.0: SelfW = 0.0: SelfPW = 1.0;
Return Self;
End Function
Function Mat4InvertV .Vector ( Self.Matrix4, v.Vector )
Local x# = SelfXY*SelfY-SelfX*SelfYY;
Local y# = SelfX*SelfYX-SelfXX*SelfY;
Local z# = SelfXX*SelfYY-SelfXY*SelfX;
Local t# = 1.0 / (SelfX*x+SelfY*y+Self*z);
Local m02# = t*x;
Local m06# = t*y;
Local m10# = t*z;
Local m14# = -t*(SelfPX*x+SelfPY*y+SelfP*z);
x = SelfYY*Self-SelfY*SelfY;
y = SelfX*SelfY-SelfYX*Self;
z = SelfYX*SelfY-SelfYY*SelfX;
Local m00# = t*x;
Local m04# = t*y;
Local m08# = t*z;
Local m12# = -t*(SelfPX*x+SelfPY*y+SelfP*z);
x = SelfX*SelfY-SelfXY*Self;
y = SelfXX*Self-SelfX*SelfX;
z = SelfXY*SelfX-SelfXX*SelfY;
Local m01# = t*x;
Local m05# = t*y;
Local m09# = t*z;
Local m13# = -t*(SelfPX*x+SelfPY*y+SelfP*z);
Local o.Vector = New Vector;
oX = vX*m00 + vY*m04 + v*m08 + m12;
oY = vX*m01 + vY*m05 + v*m09 + m13;
o = vX*m02 + vY*m06 + v*m10 + m14;
Return v;
End Function
Function Mat4SelfInvertV.Vector(Self.Matrix4,v.Vector)
Local x# = SelfXY*SelfY-SelfX*SelfYY;
Local y# = SelfX*SelfYX-SelfXX*SelfY;
Local z# = SelfXX*SelfYY-SelfXY*SelfX;
Local t# = 1.0 / (SelfX*x+SelfY*y+Self*z);
Local m02# = t*x;
Local m06# = t*y;
Local m10# = t*z;
Local m14# = -t*(SelfPX*x+SelfPY*y+SelfP*z);
x = SelfYY*Self-SelfY*SelfY;
y = SelfX*SelfY-SelfYX*Self;
z = SelfYX*SelfY-SelfYY*SelfX;
Local m00# = t*x;
Local m04# = t*y;
Local m08# = t*z;
Local m12# = -t*(SelfPX*x+SelfPY*y+SelfP*z);
x = SelfX*SelfY-SelfXY*Self;
y = SelfXX*Self-SelfX*SelfX;
z = SelfXY*SelfX-SelfXX*SelfY;
Local m01# = t*x;
Local m05# = t*y;
Local m09# = t*z;
Local m13# = -t*(SelfPX*x+SelfPY*y+SelfP*z);
Local lx#=vX,ly#=vY,lz#=v;
vX = lx*m00 + ly*m04 + lz*m08 + m12;
vY = lx*m01 + ly*m05 + lz*m09 + m13;
v = lx*m02 + ly*m06 + lz*m10 + m14;
Return v;
End Function
Function Mat4InvertRot.Matrix4(Self.Matrix4)
Local tX# = 1.0 / (SelfXX*SelfXX+SelfXY*SelfXY+SelfX*SelfX);
Local tY# = 1.0 / (SelfYX*SelfYX+SelfYY*SelfYY+SelfY*SelfY);
Local tZ# = 1.0 / (SelfX*SelfX+SelfY*SelfY+Self*Self);
Local m.Matrix4 = NewMatrix4();
mXX = SelfXX*tX: mXY = SelfYX*tY: mX = SelfX*tZ: mXW = 0.0;
mYX = SelfXY*tX: mYY = SelfYY*tY: mY = SelfY*tZ: mYW = 0.0;
mX = SelfX*tX: mY = SelfY*tY: m = Self*tZ: mW = 0.0;
mPX = -tX*(SelfPX*SelfXX+SelfPY*SelfXY+SelfP*SelfX);
mPY = -tY*(SelfPX*SelfYX+SelfPY*SelfYY+SelfP*SelfY);
mP = -tZ*(SelfPX*SelfX+SelfPY*SelfY+SelfP*Self);
mPW = 1.0;
Return m;
End Function
Function Mat4SelfInvertRot.Matrix4(Self.Matrix4)
Local tX# = 1.0 / (SelfXX*SelfXX+SelfXY*SelfXY+SelfX*SelfX);
Local tY# = 1.0 / (SelfYX*SelfYX+SelfYY*SelfYY+SelfY*SelfY);
Local tZ# = 1.0 / (SelfX*SelfX+SelfY*SelfY+Self*Self);
Local mxx#=SelfXX, mxy#=SelfXY, mxz#=SelfX
Local myx#=SelfYX, myy#=SelfYY, myz#=SelfY
Local mzx#=SelfX, mzy#=SelfY, mzz#=Self
Local mpx#=SelfPX, mpy#=SelfPY, mpz#=SelfP
SelfXX = mxx*tX: SelfXY = myx*tY: SelfX = mzx*tZ: SelfXW = 0.0;
SelfYX = mxy*tX: SelfYY = myy*tY: SelfY = mzy*tZ: SelfYW = 0.0;
SelfX = mxz*tX: SelfY = myz*tY: Self = mzz*tZ: SelfW = 0.0;
SelfPX = -tX*(mpx*mxx+mpy*mxy+mpz*mxz);
SelfPY = -tY*(mpx*myx+mpy*myy+mpz*myz);
SelfP = -tZ*(mpx*mzx+mpy*mzy+mpz*mzz);
SelfPW = 1.0;
Return Self;
End Function
Function Mat4Transpose.Matrix4(Self.Matrix4)
Local m.Matrix4 = New Matrix4;
mX = VecSet(New Vector, SelfXX, SelfYX, SelfX) : mXW = SelfPX;
mY = VecSet(New Vector, SelfXY, SelfYY, SelfY) : mYW = SelfPY;
m = VecSet(New Vector, SelfX, SelfY, Self) : mW = SelfP;
mP = NewVector(SelfXW, SelfYW, SelfW) : mPW = SelfPW;
Return m;
End Function
Function Mat4SelfTranspose.Matrix4(Self.Matrix4)
Local t#;
t = SelfXY: SelfXY = SelfYX: SelfYX = t;
t = SelfX: SelfX = SelfX: SelfX = t;
t = SelfY: SelfY = SelfY: SelfY = t;
t = SelfPX: SelfPX = SelfXW: SelfXW = t;
t = SelfPY: SelfPY = SelfYW: SelfYW = t;
t = SelfP: SelfP = SelfW: SelfW = t;
Return Self;
End Function
Function Mat4Determinant#(Self.Matrix4)
Return SelfXX*(SelfYY*Self-SelfY*SelfY) - SelfXY*(SelfYX*Self-SelfY*SelfX) + SelfX*(SelfYX*SelfY-SelfYY*SelfX);
End Function
Function Mat4ToMat3.Matrix3(Self.Matrix4)
Local m.Matrix3 = New Matrix3;
mX = VecSet(New Vector, SelfXX, SelfXY, SelfX);
mY = VecSet(New Vector, SelfYX, SelfYY, SelfY);
m = VecSet(New Vector, SelfX, SelfY, Self);
Return m;
End Function
Function Mat4Mul.Matrix4(Self.Matrix4,m.Matrix4)
Local o.Matrix4 = New Matrix4;
oX = New Vector:oY = New Vector:o = New Vector:oP = New Vector;
oXX = SelfXX*mXX + SelfYX*mXY + SelfX*mX + SelfPX*mXW;
oXY = SelfXY*mXX + SelfYY*mXY + SelfY*mX + SelfPY*mXW;
oX = SelfX*mXX + SelfY*mXY + Self*mX + SelfP*mXW;
oXW = SelfXW*mXX + SelfYW*mXY + SelfW*mX + SelfPW*mXW;
oYX = SelfXX*mYX + SelfYX*mYY + SelfX*mY + SelfPX*mYW;
oYY = SelfXY*mYX + SelfYY*mYY + SelfY*mY + SelfPY*mYW;
oY = SelfX*mYX + SelfY*mYY + Self*mY + SelfP*mYW;
oYW = SelfXW*mYX + SelfYW*mYY + SelfW*mY + SelfPW*mYW;
oX = SelfXX*mX + SelfYX*mY + SelfX*m + SelfPX*mW;
oY = SelfXY*mX + SelfYY*mY + SelfY*m + SelfPY*mW;
o = SelfX*mX + SelfY*mY + Self*m + SelfP*mW;
oW = SelfXW*mX + SelfYW*mY + SelfW*m + SelfPW*mW;
oPX = SelfXX*mPX + SelfYX*mPY + SelfX*mP + SelfPX*mPW;
oPY = SelfXY*mPX + SelfYY*mPY + SelfY*mP + SelfPY*mPW;
oP = SelfX*mPX + SelfY*mPY + Self*mP + SelfP*mPW;
oPW = SelfXW*mPX + SelfYW*mPY + SelfW*mP + SelfPW*mPW;
Return o;
End Function
Function Mat4SelfMul.Matrix4(Self.Matrix4,m.Matrix4)
Local mxx#=SelfXX, mxy#=SelfXY, mxz#=SelfX, mxw#=SelfXW;
Local myx#=SelfYX, myy#=SelfYY, myz#=SelfY, myw#=SelfYW;
Local mzx#=SelfX, mzy#=SelfY, mzz#=Self, mzw#=SelfW;
Local mpx#=SelfPX, mpy#=SelfPY, mpz#=SelfP, mww#=SelfPW;
SelfXX = mxx*mXX + myx*mXY + mzx*mX + mpx*mXW;
SelfXY = mxy*mXX + myy*mXY + mzy*mX + mpy*mXW;
SelfX = mxz*mXX + myz*mXY + mzz*mX + mpz*mXW;
SelfXW = mxw*mXX + myw*mXY + mzw*mX + mww*mXW;
SelfYX = mxx*mYX + myx*mYY + mzx*mY + mpx*mYW;
SelfYY = mxy*mYX + myy*mYY + mzy*mY + mpy*mYW;
SelfY = mxz*mYX + myz*mYY + mzz*mY + mpz*mYW;
SelfYW = mxw*mYX + myw*mYY + mzw*mY + mww*mYW;
SelfX = mxx*mX + myx*mY + mzx*m + mpx*mW;
SelfY = mxy*mX + myy*mY + mzy*m + mpy*mW;
Self = mxz*mX + myz*mY + mzz*m + mpz*mW;
SelfW = mxw*mX + myw*mY + mzw*m + mww*mW;
SelfPX = mxx*mPX + myx*mPY + mzx*mP + mpx*mPW;
SelfPY = mxy*mPX + myy*mPY + mzy*mP + mpy*mPW;
SelfP = mxz*mPX + myz*mPY + mzz*mP + mpz*mPW;
SelfPW = mxw*mPX + myw*mPY + mzw*mP + mww*mPW;
Return Self;
End Function
Function Mat4Ortho.Matrix4( Self.Matrix4,pLeft#, pRight#, pBottom#, pTop#, pNear#, pFar#)
If (pLeft = pRight) Then RuntimeError ("left == right");
If (pBottom = pTop) Then RuntimeError ("bottom == top");
If (pNear = pFar) Then RuntimeError ("near == far");
Local r_width   # = 1.0 / (pRight - pLeft);
Local r_height   # = 1.0 / (pTop - pBottom);
Local r_depth   # = 1.0 / (pFar - pNear);
Local m.Matrix4 = New Matrix4;
mX = NewVector(2.0 * (r_width), 0, 0)  : mXW = 0;
mY = NewVector(0, 2.0 * (r_height), 0) : mYW = 0;
m = NewVector(0, 0, -2.0 * (r_depth)) : mW = 0;
mP = NewVector( -(pRight + pLeft) * r_width, -(pTop + pBottom) * r_height, -(pFar + pNear) * r_depth ) : mPW = 1;
Return m;
End Function
Function Mat4SetOrtho.Matrix4( Self.Matrix4, pLeft#, pRight#, pBottom#, pTop#, pNear#, pFar#)
If (pLeft = pRight) Then RuntimeError ("left == right");
If (pBottom = pTop) Then RuntimeError ("bottom == top");
If (pNear = pFar) Then RuntimeError ("near == far");
Local r_width   # = 1.0 / (pRight - pLeft);
Local r_height   # = 1.0 / (pTop - pBottom);
Local r_depth   # = 1.0 / (pFar - pNear);
VecSet(SelfX, 2.0 * (r_width),0,0) : SelfXW = 0;
VecSet(SelfY, 0,2.0 * (r_height),0): SelfYW = 0;
VecSet(Self, 0,0,-2.0 * (r_depth)): SelfW = 0;
VecSet(SelfP, -(pRight + pLeft) * r_width, -(pTop + pBottom) * r_height, -(pFar + pNear) * r_depth): SelfW = 1;
Return Self;
End Function
Function Mat4Frustum.Matrix4( Self.Matrix4, pLeft#, pRight#, pBottom#, pTop#, pNear#, pFar# )
If (pLeft = pRight) Then RuntimeError ("left == right");
If (ptop = pbottom) Then RuntimeError ("top == bottom");
If (pnear = pfar) Then RuntimeError ("near == far");
If (pnear <= 0.0) Then RuntimeError ("near <= 0.0f");
If (pfar <= 0.0) Then RuntimeError ("far <= 0.0f");
Local r_width   #   =   1.0 / (pRight - pLeft);
Local r_height   #   =   1.0 / (ptop - pbottom);
Local r_depth   #   =   1.0 / (pnear - pfar);
Local m.Matrix4 = New Matrix4;
mX = NewVector(2.0 * (pnear * r_width), 0, 0)  : mXW = 0;
mY = NewVector(0, 2.0 * (pnear * r_height), 0)  : mYW = 0;
m = NewVector(2.0 * ((pRight + pLeft) * r_width), (ptop + pbottom) * r_height, (pfar + pnear) * r_depth)  : mW = -1;
mP = NewVector(0, 0, 2.0 * (pfar * pnear * r_depth))  : mPW = 0;
Return m;
End Function
Function Mat4SetFrustum.Matrix4( Self.Matrix4, pLeft#, pRight#, pBottom#, pTop#, pNear#, pFar# )
If (pLeft = pRight) Then RuntimeError ("left == right");
If (ptop = pbottom) Then RuntimeError ("top == bottom");
If (pnear = pfar) Then RuntimeError ("near == far");
If (pnear <= 0.0) Then RuntimeError ("near <= 0.0f");
If (pfar <= 0.0) Then RuntimeError ("far <= 0.0f");
Local r_width   #   =   1.0 / (pRight - pLeft);
Local r_height   #   =   1.0 / (ptop - pbottom);
Local r_depth   #   =   1.0 / (pnear - pfar);
VecSet(SelfX, 2.0 * (pnear * r_width), 0, 0)  : SelfXW = 0;
VecSet(SelfY, 0, 2.0 * (pnear * r_height), 0) : SelfYW = 0;
VecSet(Self, 2.0 * ((pRight + pLeft) * r_width), (ptop + pbottom) * r_height, (pfar + pnear) * r_depth)  : SelfW = -1;
VecSet(SelfP, 0, 0, 2.0 * (pfar * pnear * r_depth))  : SelfPW = 0;
Return Self;
End Function
Function Mat4Perspective.Matrix4( Self.Matrix4, fovy#, aspect#, zNear#, zFar#)
Local f# = 1.0 / Tan(fovy * 0.5);
Local rangeReciprocal# = 1.0 / (zNear - zFar);
Local m.Matrix4 = New Matrix4;
mX = NewVector(f / aspect,0,0) : mXW = 0;
mY = NewVector(0, f, 0) : mYW = 0;
m = NewVector(0, 0,(zFar + zNear) * rangeReciprocal): mW = -1;
mP = NewVector(0, 0, 2.0 * zFar * zNear * rangeReciprocal): mPW = 0;
Return m;
End Function
Function Mat4SetPerspective.Matrix4( Self.Matrix4, fovy#, aspect#, zNear#, zFar#)
Local f# = 1.0 / Tan(fovy * 0.5);
Local rangeReciprocal# = 1.0 / (zNear - zFar);
VecSet(SelfX, f / aspect,0,0) : SelfXW = 0;
VecSet(SelfY, 0, f, 0) : SelfYW = 0;
VecSet(Self, 0, 0,(zFar + zNear) * rangeReciprocal): SelfW = -1;
VecSet(SelfP, 0, 0, 2.0 * zFar * zNear * rangeReciprocal): SelfPW = 0;
Return Self;
End Function
Function Mat4MulV.Vector(Self.Matrix4, v.Vector)
Local o.Vector = New Vector;
oX = vX*SelfXX + vY*SelfYX + v*SelfX + SelfPX;
oY = vX*SelfXY + vY*SelfYY + v*SelfY + SelfPY;
o = vX*SelfX + vY*SelfY + v*Self + SelfP;
Return o;
End Function
Function Mat4MulV2.Vector(Self.Matrix4, v.Vector,out.Vector)
outX = vX*SelfXX + vY*SelfYX + v*SelfX + SelfPX;
outY = vX*SelfXY + vY*SelfYY + v*SelfY + SelfPY;
out = vX*SelfX + vY*SelfY + v*Self + SelfP;
Return out;
End Function
Function Mat4SelfMulV.Vector(Self.Matrix4, v.Vector)
Local lx#=vX, ly#=vY, lz#=v;
vX = lx*SelfXX + ly*SelfYX + lz*SelfX + SelfPX;
vY = lx*SelfXY + ly*SelfYY + lz*SelfY + SelfPY;
v = lx*SelfX + ly*SelfY + lz*Self + SelfP;
Return v;
End Function
Function Mat4Transform.Vector(Self.Matrix4, v.Vector)
Return VecTransformM4(v,Self);
End Function
Function Mat4InvertTransform.Vector(Self.Matrix4, v.Vector)
Local m.Matrix4 = Mat4Invert(Self);
v = VecTransformM4(v,m);
FreeMatrix4(m);
Return v;
End Function
Function Mat4ToQuaternion.Quaternion(Self.Matrix4)
Return QuatFromMat4(Self);
End Function
Function Mat4Row.Vector(Self.Matrix4, pCol%)
If (pCol = 3) Then Return Vec4Copy(SelfP);
If (pCol = 2) Then Return Vec4Copy(Self);
If (pCol = 1) Then Return Vec4Copy(SelfY);
Return Vec4Copy(SelfX);
End Function
Function Mat4Cell#(Self.Matrix4, j%,i%)
If (j = 3)
If i=3 Then Return SelfPW;
If i=2 Then Return SelfP;
If i=1 Then Return SelfPY;
Return SelfPX;
ElseIf (j = 2)
If i=3 Then Return SelfW;
If i=2 Then Return Self;
If i=1 Then Return SelfY;
Return SelfX;
ElseIf (j = 1)
If i=3 Then Return SelfYW;
If i=2 Then Return SelfY;
If i=1 Then Return SelfYY;
Return SelfYX;
EndIf
If i=3 Then Return SelfXW;
If i=2 Then Return SelfX;
If i=1 Then Return SelfXY;
Return SelfXX;
End Function
Function Mat4X#(Self.Matrix4)
Return SelfPX;
End Function
Function Mat4Y#(Self.Matrix4)
Return SelfPY;
End Function
Function Mat4Z#(Self.Matrix4)
Return SelfP;
End Function
Function Mat4I.Vector(Self.Matrix4):Return VecCopy(SelfX):End Function
Function Mat4J.Vector(Self.Matrix4):Return VecCopy(SelfY):End Function
Function Mat4K.Vector(Self.Matrix4):Return VecCopy(Self):End Function
Function Mat4Left.Vector(Self.Matrix4):Return VecNegate(SelfX):End Function
Function Mat4Right.Vector(Self.Matrix4):Return VecCopy(SelfX):End Function
Function Mat4Down.Vector(Self.Matrix4):Return VecNegate(SelfY):End Function
Function Mat4Up.Vector(Self.Matrix4):Return VecCopy(SelfY):End Function
Function Mat4Back.Vector(Self.Matrix4):Return VecNegate(Self):End Function
Function Mat4Front.Vector(Self.Matrix4):Return VecCopy(Self):End Function
Function Mat4Position.Vector(Self.Matrix4):Return VecCopy(SelfP):End Function
Function Mat4Normalize.Matrix4(Self.Matrix4)
Local m.Matrix4 = New Matrix4;
mX = VecNormal