Hello pals,
sorry for the late reply, I had work to be done very quick.
I returned with the newest update of the code.
WickedX, its more your snippet, because you did the brain stuff already =)
Thank you for your contribution! I hope we could get the last missing commands done,
then a quick debug round and we have advanced vector and matrix math in AppGameKit!
0.14:
// ==================================================================================
// ====== Extended math function for AGK ======================================
// ========================================= 2016================ 0.14 ============
// ==================================================================================
// ==== Jack('the baby stuff), WickedX('the hard stuff) =============================
// ==================================================================================
/* Changelog
Version 0.1
===========
Vector 3 (already existing, but incomplete commandset)
- added normalize vector 3 command
Vector 4 (new command set)
- added vector4 create command
- added vector4 delete command
- added vector4 add command
- added vector4 substract command
- added vector4 set command
- added vector4 get x,y,z,w commands
Matrix 4 (new command set)
- added matrix 4 create command
- added matrix 4 delete command
- added matrix 4 add command
- added matrix 4 multiply command
- added matrix 4 divide command
- added matrix 4 get element command
- added matrix 4 set command
Version 0.12
============
Bug Fixes
- Fixed a major bug at deleting matrix4 and vector4 arrays (deleting caused missing links)
Matrix 4
- added matrix 4 Projection Matrix 4 command
- added matrix 4 Translate Matrix 4 command
Version 0.14
============
Bug Fixes
- Matrix multiplication is corrected (was wronger than wrong)
Vector 3
- added TransformCoordsVector3 command
Matrix 4
- added matrix 4 rotation Matrix 4 commands (x,y,z)
- added Set Matrix 4 Element command (0 - 15)
Work in Progress (Not finished, but have to be done to get really serious with 3d math):
- transform normals vector3 command
- Inverse Matrix4 command
- Build Look At LhMatrix4 command
- set identity matrix4 comnmand [?]
- Intense debugging
Forum thread: https://forum.thegamecreators.com/thread/217064#msg2582798
========================================================================
*/
// types
/*
type Vec4_def
x as float
y as float
z as float
w as float
endtype
type Vector4_def
used as integer
x as float
y as float
z as float
w as float
endtype
type Matrix4_def
used as integer
x as Vec4_def
y as Vec4_def
z as Vec4_def
w as Vec4_def
endtype
*/
// Math Constants
// ==============
#constant Math_PI 3.14159
// extended vector3 commands Library
// =================================
// normalize vector (from source to result)
function normalizeVector3(vsource)
d# = getVector3Length(vsource)
x# = GetVector3X(vsource) / d#
y# = GetVector3Y(vsource) / d#
z# = GetVector3Z(vsource) / d#
SetVector3(vsource,x#,y#,z#) // update source vector
endfunction
// Transform Coords Vector 3
function TransformCoordsVector3(VecID, MatID)
norm as float
x as float
y as float
z as float
norm = GetMatrix4Element(MatID, 3)*GetVector3X(VecID)+GetMatrix4Element(MatID, 7)*GetVector3Y(VecID)+GetMatrix4Element(MatID, 11)+GetMatrix4Element(MatID, 15)
if (norm)>0.0
x = (GetMatrix4Element(MatID, 0)*GetVector3X(VecID)+GetMatrix4Element(MatID, 4)*GetVector3Y(VecID)+GetMatrix4Element(MatID, 8)*GetVector3Z(VecID)+GetMatrix4Element(MatID, 12))/norm
y = (GetMatrix4Element(MatID, 1)*GetVector3X(VecID)+GetMatrix4Element(MatID, 5)*GetVector3Y(VecID)+GetMatrix4Element(MatID, 9)*GetVector3Z(VecID)+GetMatrix4Element(MatID, 13))/norm
z = (GetMatrix4Element(MatID, 2)*GetVector3X(VecID)+GetMatrix4Element(MatID, 6)*GetVector3Y(VecID)+GetMatrix4Element(MatID, 10)*GetVector3Z(VecID)+GetMatrix4Element(MatID, 14))/norm
else
x = 0.0
y = 0.0
z = 0.0
endif
setvector3(VecID, x, y, z)
endfunction
remstart this should be a good feature :
// transform normals vector3
function TransformNormalsVector3(vsource, msource)
endfunction
remend
// vector4 commands Library
// ========================
// create vector4
function CreateVector4()
if Vector4.length=-1
dim Vector4[] as Vector4_def
endif
// find free slot
x = -1
repeat
inc x
if x>Vector4.length // if no slot exists, create a new one
Vector4.length=Vector4.length+1
exit
endif
until Vector4[x].used=0
out=x
Vector4[out].used=1 // set used to 1
endfunction out
// delete vector4 (in1)
function DeleteVector4(num)
// Vector4.remove(num)
Vector4[num].used=0
Vector4[num].x=0.0
Vector4[num].y=0.0
Vector4[num].z=0.0
Vector4[num].w=0.0
endfunction
// Vector4 add (result,in1,in2)
function AddVector4(resultvec,vec1,vec2)
Vector4[resultvec].x=Vector4[vec1].x + Vector4[vec2].x
Vector4[resultvec].y=Vector4[vec1].y + Vector4[vec2].y
Vector4[resultvec].z=Vector4[vec1].z + Vector4[vec2].z
Vector4[resultvec].w=Vector4[vec1].w + Vector4[vec2].w
endfunction
// Vector4 substract substract vec2 from vec1 into resultvec
function SubstractVector4(resultvec,vec1,vec2)
Vector4[resultvec].x=Vector4[vec1].x - Vector4[vec2].x
Vector4[resultvec].y=Vector4[vec1].y - Vector4[vec2].y
Vector4[resultvec].z=Vector4[vec1].z - Vector4[vec2].z
Vector4[resultvec].w=Vector4[vec1].w - Vector4[vec2].w
endfunction
// Vector4 set (vec,x,y,z,w)
function SetVector4(resultvec,x as float,y as float,z as float,w as float)
Vector4[resultvec].x=x
Vector4[resultvec].y=y
Vector4[resultvec].z=z
Vector4[resultvec].w=w
endfunction
// get vector4 x
function GetVector4X(vec)
out# = Vector4[vec].x
endfunction out#
// get vector4 y
function GetVector4Y(vec)
out# = Vector4[vec].y
endfunction out#
// get vector4 z
function GetVector4Z(vec)
out# = Vector4[vec].z
endfunction out#
// get vector4 w
function GetVector4W(vec)
out# = Vector4[vec].w
endfunction out#
// matrix4 commands Library
// ========================
// create matrix4 (creates a new slot in the matrix4 4x4 array)
function CreateMatrix4()
if Matrix4.length=-1 // if the matrix4 array is empty, create a new one
dim Matrix4[] as Matrix4_def
endif
// find free slot
x = -1
repeat
inc x
if x>Matrix4.length // if no slot exists, create a new one
Matrix4.length=Matrix4.length+1
exit
endif
until Matrix4[x].used=0
out=x
Matrix4[out].used=1 // set used to 1
endfunction out
// deletes matrix4 from the matrix4 array
function DeleteMatrix4(num)
// Vector4.remove(num)
Matrix4[num].used=0
Matrix4[num].x.x=0.0 : Matrix4[num].y.x=0.0 : Matrix4[num].z.x=0.0 : Matrix4[num].w.x=0.0
Matrix4[num].x.y=0.0 : Matrix4[num].y.y=0.0 : Matrix4[num].z.y=0.0 : Matrix4[num].w.y=0.0
Matrix4[num].x.z=0.0 : Matrix4[num].y.z=0.0 : Matrix4[num].z.z=0.0 : Matrix4[num].w.z=0.0
Matrix4[num].x.w=0.0 : Matrix4[num].y.w=0.0 : Matrix4[num].z.w=0.0 : Matrix4[num].w.w=0.0
endfunction
// add matrix4 1 and matrix4 2 to resultmatrix4
function AddMatrix4(resultmat,mat1,mat2)
// x
Matrix4[resultmat].x.x=Matrix4[mat1].x.x + Matrix4[mat2].x.x
Matrix4[resultmat].x.y=Matrix4[mat1].x.y + Matrix4[mat2].x.y
Matrix4[resultmat].x.z=Matrix4[mat1].x.z + Matrix4[mat2].x.z
Matrix4[resultmat].x.w=Matrix4[mat1].x.w + Matrix4[mat2].x.w
// y
Matrix4[resultmat].y.x=Matrix4[mat1].y.x + Matrix4[mat2].y.x
Matrix4[resultmat].y.y=Matrix4[mat1].y.y + Matrix4[mat2].y.y
Matrix4[resultmat].y.z=Matrix4[mat1].y.z + Matrix4[mat2].y.z
Matrix4[resultmat].y.w=Matrix4[mat1].y.w + Matrix4[mat2].y.w
// z
Matrix4[resultmat].z.x=Matrix4[mat1].z.x + Matrix4[mat2].z.x
Matrix4[resultmat].z.y=Matrix4[mat1].z.y + Matrix4[mat2].z.y
Matrix4[resultmat].z.z=Matrix4[mat1].z.z + Matrix4[mat2].z.z
Matrix4[resultmat].z.w=Matrix4[mat1].z.w + Matrix4[mat2].z.w
// w
Matrix4[resultmat].w.x=Matrix4[mat1].w.x + Matrix4[mat2].w.x
Matrix4[resultmat].w.y=Matrix4[mat1].w.y + Matrix4[mat2].w.y
Matrix4[resultmat].w.z=Matrix4[mat1].w.z + Matrix4[mat2].w.z
Matrix4[resultmat].w.w=Matrix4[mat1].w.w + Matrix4[mat2].w.w
endfunction
// substract matrix4 2 from matrix4 1 to resultmatrix4
function SubstractMatrix4(resultmat,mat1,mat2)
// x
Matrix4[resultmat].x.x=Matrix4[mat1].x.x - Matrix4[mat2].x.x
Matrix4[resultmat].x.y=Matrix4[mat1].x.y - Matrix4[mat2].x.y
Matrix4[resultmat].x.z=Matrix4[mat1].x.z - Matrix4[mat2].x.z
Matrix4[resultmat].x.w=Matrix4[mat1].x.w - Matrix4[mat2].x.w
// y
Matrix4[resultmat].y.x=Matrix4[mat1].y.x - Matrix4[mat2].y.x
Matrix4[resultmat].y.y=Matrix4[mat1].y.y - Matrix4[mat2].y.y
Matrix4[resultmat].y.z=Matrix4[mat1].y.z - Matrix4[mat2].y.z
Matrix4[resultmat].y.w=Matrix4[mat1].y.w - Matrix4[mat2].y.w
// z
Matrix4[resultmat].z.x=Matrix4[mat1].z.x - Matrix4[mat2].z.x
Matrix4[resultmat].z.y=Matrix4[mat1].z.y - Matrix4[mat2].z.y
Matrix4[resultmat].z.z=Matrix4[mat1].z.z - Matrix4[mat2].z.z
Matrix4[resultmat].z.w=Matrix4[mat1].z.w - Matrix4[mat2].z.w
// w
Matrix4[resultmat].w.x=Matrix4[mat1].w.x - Matrix4[mat2].w.x
Matrix4[resultmat].w.y=Matrix4[mat1].w.y - Matrix4[mat2].w.y
Matrix4[resultmat].w.z=Matrix4[mat1].w.z - Matrix4[mat2].w.z
Matrix4[resultmat].w.w=Matrix4[mat1].w.w - Matrix4[mat2].w.w
endfunction
// multiply matrix4 2 from matrix4 1 to resultmatrix4 or multiply resultmatrix4 with mat1 value
function MultiplyMatrix4(resultmat,mat1 as float,mat2)
if mat2=-1 // if mat2 is entered with -1, mat1 will become the multiply value
// multiply matrix by value
// x
Matrix4[resultmat].x.x=Matrix4[resultmat].x.x * mat1
Matrix4[resultmat].x.y=Matrix4[resultmat].x.y * mat1
Matrix4[resultmat].x.z=Matrix4[resultmat].x.z * mat1
Matrix4[resultmat].x.w=Matrix4[resultmat].x.w * mat1
// y
Matrix4[resultmat].y.x=Matrix4[resultmat].y.x * mat1
Matrix4[resultmat].y.y=Matrix4[resultmat].y.y * mat1
Matrix4[resultmat].y.z=Matrix4[resultmat].y.z * mat1
Matrix4[resultmat].y.w=Matrix4[resultmat].y.w * mat1
// z
Matrix4[resultmat].z.x=Matrix4[resultmat].z.x * mat1
Matrix4[resultmat].z.y=Matrix4[resultmat].z.y * mat1
Matrix4[resultmat].z.z=Matrix4[resultmat].z.z * mat1
Matrix4[resultmat].z.w=Matrix4[resultmat].z.w * mat1
// w
Matrix4[resultmat].w.x=Matrix4[resultmat].w.x * mat1
Matrix4[resultmat].w.y=Matrix4[resultmat].w.y * mat1
Matrix4[resultmat].w.z=Matrix4[resultmat].w.z * mat1
Matrix4[resultmat].w.w=Matrix4[resultmat].w.w * mat1
else
// multiply 2 matrices
// x
Matrix4[resultmat].x.x=Matrix4[val(str(mat1))].x.x * Matrix4[mat2].x.x
Matrix4[resultmat].x.y=Matrix4[val(str(mat1))].x.y * Matrix4[mat2].x.y
Matrix4[resultmat].x.z=Matrix4[val(str(mat1))].x.z * Matrix4[mat2].x.z
Matrix4[resultmat].x.w=Matrix4[val(str(mat1))].x.w * Matrix4[mat2].x.w
// y
Matrix4[resultmat].y.x=Matrix4[val(str(mat1))].y.x * Matrix4[mat2].y.x
Matrix4[resultmat].y.y=Matrix4[val(str(mat1))].y.y * Matrix4[mat2].y.y
Matrix4[resultmat].y.z=Matrix4[val(str(mat1))].y.z * Matrix4[mat2].y.z
Matrix4[resultmat].y.w=Matrix4[val(str(mat1))].y.w * Matrix4[mat2].y.w
// z
Matrix4[resultmat].z.x=Matrix4[val(str(mat1))].z.x * Matrix4[mat2].z.x
Matrix4[resultmat].z.y=Matrix4[val(str(mat1))].z.y * Matrix4[mat2].z.y
Matrix4[resultmat].z.z=Matrix4[val(str(mat1))].z.z * Matrix4[mat2].z.z
Matrix4[resultmat].z.w=Matrix4[val(str(mat1))].z.w * Matrix4[mat2].z.w
// w
Matrix4[resultmat].w.x=Matrix4[val(str(mat1))].w.x * Matrix4[mat2].w.x
Matrix4[resultmat].w.y=Matrix4[val(str(mat1))].w.y * Matrix4[mat2].w.y
Matrix4[resultmat].w.z=Matrix4[val(str(mat1))].w.z * Matrix4[mat2].w.z
Matrix4[resultmat].w.w=Matrix4[val(str(mat1))].w.w * Matrix4[mat2].w.w
endif
endfunction
// divide matrix4 2 from matrix4 1 to resultmatrix4 or divide matrix4 1 by the mat1 value
function DivideMatrix4(resultmat,mat1 as float,mat2)
if mat2=-1 // if mat2 is entered with -1, mat1 will become the multiply value
// multiply matrix by value
// x
Matrix4[resultmat].x.x=Matrix4[resultmat].x.x / mat1
Matrix4[resultmat].x.y=Matrix4[resultmat].x.y / mat1
Matrix4[resultmat].x.z=Matrix4[resultmat].x.z / mat1
Matrix4[resultmat].x.w=Matrix4[resultmat].x.w / mat1
// y
Matrix4[resultmat].y.x=Matrix4[resultmat].y.x / mat1
Matrix4[resultmat].y.y=Matrix4[resultmat].y.y / mat1
Matrix4[resultmat].y.z=Matrix4[resultmat].y.z / mat1
Matrix4[resultmat].y.w=Matrix4[resultmat].y.w / mat1
// z
Matrix4[resultmat].z.x=Matrix4[resultmat].z.x / mat1
Matrix4[resultmat].z.y=Matrix4[resultmat].z.y / mat1
Matrix4[resultmat].z.z=Matrix4[resultmat].z.z / mat1
Matrix4[resultmat].z.w=Matrix4[resultmat].z.w / mat1
// w
Matrix4[resultmat].w.x=Matrix4[resultmat].w.x / mat1
Matrix4[resultmat].w.y=Matrix4[resultmat].w.y / mat1
Matrix4[resultmat].w.z=Matrix4[resultmat].w.z / mat1
Matrix4[resultmat].w.w=Matrix4[resultmat].w.w / mat1
else
// multiply 2 matrices
// x
Matrix4[resultmat].x.x=Matrix4[val(str(mat1))].x.x / Matrix4[mat2].x.x
Matrix4[resultmat].x.y=Matrix4[val(str(mat1))].x.y / Matrix4[mat2].x.y
Matrix4[resultmat].x.z=Matrix4[val(str(mat1))].x.z / Matrix4[mat2].x.z
Matrix4[resultmat].x.w=Matrix4[val(str(mat1))].x.w / Matrix4[mat2].x.w
// y
Matrix4[resultmat].y.x=Matrix4[val(str(mat1))].y.x / Matrix4[mat2].y.x
Matrix4[resultmat].y.y=Matrix4[val(str(mat1))].y.y / Matrix4[mat2].y.y
Matrix4[resultmat].y.z=Matrix4[val(str(mat1))].y.z / Matrix4[mat2].y.z
Matrix4[resultmat].y.w=Matrix4[val(str(mat1))].y.w / Matrix4[mat2].y.w
// z
Matrix4[resultmat].z.x=Matrix4[val(str(mat1))].z.x / Matrix4[mat2].z.x
Matrix4[resultmat].z.y=Matrix4[val(str(mat1))].z.y / Matrix4[mat2].z.y
Matrix4[resultmat].z.z=Matrix4[val(str(mat1))].z.z / Matrix4[mat2].z.z
Matrix4[resultmat].z.w=Matrix4[val(str(mat1))].z.w / Matrix4[mat2].z.w
// w
Matrix4[resultmat].w.x=Matrix4[val(str(mat1))].w.x / Matrix4[mat2].w.x
Matrix4[resultmat].w.y=Matrix4[val(str(mat1))].w.y / Matrix4[mat2].w.y
Matrix4[resultmat].w.z=Matrix4[val(str(mat1))].w.z / Matrix4[mat2].w.z
Matrix4[resultmat].w.w=Matrix4[val(str(mat1))].w.w / Matrix4[mat2].w.w
endif
endfunction
// set matrix4 element (ele = 0 to 15)
function SetMatrix4Element(mat,ele,value#)
select ele
// x
case 0:
Matrix4[mat].x.x = value#
endcase
case 1:
Matrix4[mat].x.y = value#
endcase
case 2:
Matrix4[mat].x.z = value#
endcase
case 3:
Matrix4[mat].x.w = value#
endcase
// y
case 4:
Matrix4[mat].y.x = value#
endcase
case 5:
Matrix4[mat].y.y = value#
endcase
case 6:
Matrix4[mat].y.z = value#
endcase
case 7:
Matrix4[mat].y.w = value#
endcase
// z
case 8:
Matrix4[mat].z.x = value#
endcase
case 9:
Matrix4[mat].z.y = value#
endcase
case 10:
Matrix4[mat].z.z = value#
endcase
case 11:
Matrix4[mat].z.w = value#
endcase
// z
case 12:
Matrix4[mat].w.x = value#
endcase
case 13:
Matrix4[mat].w.y = value#
endcase
case 14:
Matrix4[mat].w.z = value#
endcase
case 15:
Matrix4[mat].w.w = value#
endcase
endselect
endfunction
// get matrix4 element (ele = 0 to 15)
function GetMatrix4Element(mat,ele)
select ele
// x
case 0:
result# = Matrix4[mat].x.x
endcase
case 1:
result# = Matrix4[mat].x.y
endcase
case 2:
result# = Matrix4[mat].x.z
endcase
case 3:
result# = Matrix4[mat].x.w
endcase
// y
case 4:
result# = Matrix4[mat].y.x
endcase
case 5:
result# = Matrix4[mat].y.y
endcase
case 6:
result# = Matrix4[mat].y.z
endcase
case 7:
result# = Matrix4[mat].y.w
endcase
// z
case 8:
result# = Matrix4[mat].z.x
endcase
case 9:
result# = Matrix4[mat].z.y
endcase
case 10:
result# = Matrix4[mat].z.z
endcase
case 11:
result# = Matrix4[mat].z.w
endcase
// z
case 12:
result# = Matrix4[mat].w.x
endcase
case 13:
result# = Matrix4[mat].w.y
endcase
case 14:
result# = Matrix4[mat].w.z
endcase
case 15:
result# = Matrix4[mat].w.w
endcase
endselect
endfunction result#
// set matrix4 (matrix4, 16 floats)
function SetMatrix4(mat, f0 as float, f1 as float, f2 as float, f3 as float, f4 as float, f5 as float, f6 as float, f7 as float, f8 as float, f9 as float, f10 as float, f11 as float, f12 as float, f13 as float, f14 as float, f15 as float)
// x
Matrix4[mat].x.x = f0
Matrix4[mat].x.y = f1
Matrix4[mat].x.z = f2
Matrix4[mat].x.w = f3
// y
Matrix4[mat].y.x = f4
Matrix4[mat].y.y = f5
Matrix4[mat].y.z = f6
Matrix4[mat].y.w = f7
// z
Matrix4[mat].z.x = f8
Matrix4[mat].z.y = f9
Matrix4[mat].z.z = f10
Matrix4[mat].z.w = f11
// w
Matrix4[mat].w.x = f12
Matrix4[mat].w.y = f13
Matrix4[mat].w.z = f14
Matrix4[mat].w.w = f15
endfunction
// projection matrix
function ProjectionMatrix4(mat, fov as float, aspect as float, nearDist as float, farDist)
frustumDepth as float
oneOverDepth as float
frustumDepth = farDist - nearDist
oneOverDepth = 1 / frustumDepth
// write informations into the 4x4 matrix
Matrix4[mat].x.x = 1/tan(fov*0.5) : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = farDist * oneOverDepth : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = farDist * oneOverDepth : Matrix4[mat].w.z = 1
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = (-farDist * nearDist) * oneOverDepth : Matrix4[mat].w.w = 0
endfunction
// translate Matrix 4
function TranslateMatrix4(mat, x as float, y as float, z as float)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = 1 : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = 1 : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = 1 : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = x : Matrix4[mat].y.w = y : Matrix4[mat].z.w = z : Matrix4[mat].w.w = 1
endfunction
// X Rotate Matrix 4
function RotateXMatrix4(mat, angle as float)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = 1 : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = cos(angle) : Matrix4[mat].z.y = sin(angle) : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = -sin(angle): Matrix4[mat].z.z = cos(angle) : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = 0 : Matrix4[mat].w.w = 1
endfunction
// Y Rotate Matrix 4
function RotateYMatrix4(mat, angle as float)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = cos(angle) : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = -sin(angle) : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = 1 : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = sin(angle) : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = cos(angle) : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = 0 : Matrix4[mat].w.w = 1
endfunction
// Z Rotate Matrix 4
function RotateZMatrix4(mat, angle as float)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = cos(angle) : Matrix4[mat].y.x = sin(angle) : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = -sin(angle) : Matrix4[mat].y.y = cos(angle) : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = 1 : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = 0 : Matrix4[mat].w.w = 1
endfunction
// View Matrix 4
function ViewMatrix4(xVecID, yVecID, zVecID, posVecID, matRotID, matViewID, matTempRotID)
SetIdentityMatrix4(matViewID)
setvector3(posVecID, getcamerax(1), getcameray(1), getcameraz(1) )
RotateZMatrix4( matRotID, radian(cameraAngleZ(1)) )
RotateYMatrix4( matTempRotID, radian(cameraAngleY(1)) )
MultiplyMatrix4(matRotID, matRotID, matTempRotID)
RotateXMatrix4( matTempRotID, radian(cameraAngleX(1)) )
MultiplyMatrix4(matRotID, matRotID, matTempRotID)
setvector3(xVecID, 1.0, 0.0, 0.0 )
setvector3(yVecID, 0.0, 1.0, 0.0 )
setvector3(zVecID, 0.0, 0.0, 1.0 )
TransformCoordsVector3(xVecID, matRotID)
TransformCoordsVector3(yVecID, matRotID)
TransformCoordsVector3(zVecID, matRotID)
SetMatrix4Element(matViewID, 0, GetVector3X(xVecID) )
SetMatrix4Element(matViewID, 1, GetVector3X(yVecID) )
SetMatrix4Element(matViewID, 2, GetVector3X(zVecID) )
SetMatrix4Element(matViewID, 4, GetVector3Y(xVecID) )
SetMatrix4Element(matViewID, 5, GetVector3Y(yVecID) )
SetMatrix4Element(matViewID, 6, GetVector3Y(zVecID) )
SetMatrix4Element(matViewID, 8, GetVector3Z(xVecID) )
SetMatrix4Element(matViewID, 9, GetVector3Z(yVecID) )
SetMatrix4Element(matViewID, 10,GetVector3Z(zVecID) )
SetMatrix4Element(matViewID, 12, -GetVector3Dot(posVecID, xVecID) )
SetMatrix4Element(matViewID, 13, -GetVector3Dot(posVecID, yVecID) )
SetMatrix4Element(matViewID, 14, -GetVector3Dot(posVecID, zVecID) )
endfunction
// Not finished commands:
// Identity Matrix 4
function SetIdentityMatrix4(mat)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = 1 : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = 1 : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = 1 : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = 0 : Matrix4[mat].w.w = 1
endfunction
// Inverse Matrix 4
function InverseMatrix4(mresult, msource)
endfunction
// Build Look at lh matrix
function BuildLookAtLhMatrix4()
endfunction
// various math commands
// ==================
// this function will convert an euler angle to a radian angle
function radian(eul#)
out# = eul# * 180 / Math_PI // conversion to degrees
if( out# < 0 ) then out# = out# + 360.0 // convert negative to positive angles
endfunction out#
EDIT:
0.2:
With the Build look at LHMatrix4 command
// ==================================================================================
// ====== Extended math function for AGK ======================================
// ========================================= 2016================ 0.20 ============
// ==================================================================================
// ==== Jack('the baby stuff), WickedX('the hard stuff) =============================
// ==================================================================================
/* Changelog
Version 0.1
===========
Vector 3 (already existing, but incomplete commandset)
- added normalize vector 3 command
Vector 4 (new command set)
- added vector4 create command
- added vector4 delete command
- added vector4 add command
- added vector4 substract command
- added vector4 set command
- added vector4 get x,y,z,w commands
Matrix 4 (new command set)
- added matrix 4 create command
- added matrix 4 delete command
- added matrix 4 add command
- added matrix 4 multiply command
- added matrix 4 divide command
- added matrix 4 get element command
- added matrix 4 set command
Version 0.12
============
Bug Fixes
- Fixed a major bug at deleting matrix4 and vector4 arrays (deleting caused missing links)
Matrix 4
- added matrix 4 Projection Matrix 4 command
- added matrix 4 Translate Matrix 4 command
Version 0.14
============
Bug Fixes
- Matrix multiplication is corrected (was wronger than wrong)
Vector 3
- added TransformCoordsVector3 command
Matrix 4
- added matrix 4 rotation Matrix 4 commands (x,y,z)
- added Set Matrix 4 Element command (0 - 15)
- set identity matrix4 comnmand
Version 0.2:
============
Vector 3
- added SubstractVector3 command
- Build Look At LhMatrix4 command
Work in Progress (Not finished, but have to be done to get really serious with 3d math):
- transform normals vector3 command
- Inverse Matrix4 command
- Intense debugging
Forum thread: https://forum.thegamecreators.com/thread/217064#msg2582798
========================================================================
*/
/*
// types
type Vec4_def
x as float
y as float
z as float
w as float
endtype
type Vector4_def
used as integer
x as float
y as float
z as float
w as float
endtype
type Matrix4_def
used as integer
x as Vec4_def
y as Vec4_def
z as Vec4_def
w as Vec4_def
endtype
*/
// Math Constants
// ==============
#constant Math_PI 3.14159
// extended vector3 commands Library
// =================================
// Vector3 substract substract vec2 from vec1 into resultvec
function SubstractVector3(resultvec,vec1,vec2)
Vector4[resultvec].x=Vector4[vec1].x - Vector4[vec2].x
Vector4[resultvec].y=Vector4[vec1].y - Vector4[vec2].y
Vector4[resultvec].z=Vector4[vec1].z - Vector4[vec2].z
endfunction
// normalize vector (from source to result)
function normalizeVector3(vsource)
d# = getVector3Length(vsource)
x# = GetVector3X(vsource) / d#
y# = GetVector3Y(vsource) / d#
z# = GetVector3Z(vsource) / d#
SetVector3(vsource,x#,y#,z#) // update source vector
endfunction
// Transform Coords Vector 3
function TransformCoordsVector3(VecID, MatID)
norm as float
x as float
y as float
z as float
norm = GetMatrix4Element(MatID, 3)*GetVector3X(VecID)+GetMatrix4Element(MatID, 7)*GetVector3Y(VecID)+GetMatrix4Element(MatID, 11)+GetMatrix4Element(MatID, 15)
if (norm)>0.0
x = (GetMatrix4Element(MatID, 0)*GetVector3X(VecID)+GetMatrix4Element(MatID, 4)*GetVector3Y(VecID)+GetMatrix4Element(MatID, 8)*GetVector3Z(VecID)+GetMatrix4Element(MatID, 12))/norm
y = (GetMatrix4Element(MatID, 1)*GetVector3X(VecID)+GetMatrix4Element(MatID, 5)*GetVector3Y(VecID)+GetMatrix4Element(MatID, 9)*GetVector3Z(VecID)+GetMatrix4Element(MatID, 13))/norm
z = (GetMatrix4Element(MatID, 2)*GetVector3X(VecID)+GetMatrix4Element(MatID, 6)*GetVector3Y(VecID)+GetMatrix4Element(MatID, 10)*GetVector3Z(VecID)+GetMatrix4Element(MatID, 14))/norm
else
x = 0.0
y = 0.0
z = 0.0
endif
setvector3(VecID, x, y, z)
endfunction
// this should be a good feature :
// transform normals vector3
function TransformNormalsVector3(vsource, msource)
endfunction
// vector4 commands Library
// ========================
// create vector4
function CreateVector4()
if Vector4.length=-1
dim Vector4[] as Vector4_def
endif
// find free slot
x = -1
repeat
inc x
if x>Vector4.length // if no slot exists, create a new one
Vector4.length=Vector4.length+1
exit
endif
until Vector4[x].used=0
out=x
Vector4[out].used=1 // set used to 1
endfunction out
// delete vector4 (in1)
function DeleteVector4(num)
// Vector4.remove(num)
Vector4[num].used=0
Vector4[num].x=0.0
Vector4[num].y=0.0
Vector4[num].z=0.0
Vector4[num].w=0.0
endfunction
// Vector4 add (result,in1,in2)
function AddVector4(resultvec,vec1,vec2)
Vector4[resultvec].x=Vector4[vec1].x + Vector4[vec2].x
Vector4[resultvec].y=Vector4[vec1].y + Vector4[vec2].y
Vector4[resultvec].z=Vector4[vec1].z + Vector4[vec2].z
Vector4[resultvec].w=Vector4[vec1].w + Vector4[vec2].w
endfunction
// Vector4 substract substract vec2 from vec1 into resultvec
function SubstractVector4(resultvec,vec1,vec2)
Vector4[resultvec].x=Vector4[vec1].x - Vector4[vec2].x
Vector4[resultvec].y=Vector4[vec1].y - Vector4[vec2].y
Vector4[resultvec].z=Vector4[vec1].z - Vector4[vec2].z
Vector4[resultvec].w=Vector4[vec1].w - Vector4[vec2].w
endfunction
// Vector4 set (vec,x,y,z,w)
function SetVector4(resultvec,x as float,y as float,z as float,w as float)
Vector4[resultvec].x=x
Vector4[resultvec].y=y
Vector4[resultvec].z=z
Vector4[resultvec].w=w
endfunction
// get vector4 x
function GetVector4X(vec)
out# = Vector4[vec].x
endfunction out#
// get vector4 y
function GetVector4Y(vec)
out# = Vector4[vec].y
endfunction out#
// get vector4 z
function GetVector4Z(vec)
out# = Vector4[vec].z
endfunction out#
// get vector4 w
function GetVector4W(vec)
out# = Vector4[vec].w
endfunction out#
// matrix4 commands Library
// ========================
// create matrix4 (creates a new slot in the matrix4 4x4 array)
function CreateMatrix4()
if Matrix4.length=-1 // if the matrix4 array is empty, create a new one
dim Matrix4[] as Matrix4_def
endif
// find free slot
x = -1
repeat
inc x
if x>Matrix4.length // if no slot exists, create a new one
Matrix4.length=Matrix4.length+1
exit
endif
until Matrix4[x].used=0
out=x
Matrix4[out].used=1 // set used to 1
endfunction out
// deletes matrix4 from the matrix4 array
function DeleteMatrix4(num)
// Vector4.remove(num)
Matrix4[num].used=0
Matrix4[num].x.x=0.0 : Matrix4[num].y.x=0.0 : Matrix4[num].z.x=0.0 : Matrix4[num].w.x=0.0
Matrix4[num].x.y=0.0 : Matrix4[num].y.y=0.0 : Matrix4[num].z.y=0.0 : Matrix4[num].w.y=0.0
Matrix4[num].x.z=0.0 : Matrix4[num].y.z=0.0 : Matrix4[num].z.z=0.0 : Matrix4[num].w.z=0.0
Matrix4[num].x.w=0.0 : Matrix4[num].y.w=0.0 : Matrix4[num].z.w=0.0 : Matrix4[num].w.w=0.0
endfunction
// add matrix4 1 and matrix4 2 to resultmatrix4
function AddMatrix4(resultmat,mat1,mat2)
// x
Matrix4[resultmat].x.x=Matrix4[mat1].x.x + Matrix4[mat2].x.x
Matrix4[resultmat].x.y=Matrix4[mat1].x.y + Matrix4[mat2].x.y
Matrix4[resultmat].x.z=Matrix4[mat1].x.z + Matrix4[mat2].x.z
Matrix4[resultmat].x.w=Matrix4[mat1].x.w + Matrix4[mat2].x.w
// y
Matrix4[resultmat].y.x=Matrix4[mat1].y.x + Matrix4[mat2].y.x
Matrix4[resultmat].y.y=Matrix4[mat1].y.y + Matrix4[mat2].y.y
Matrix4[resultmat].y.z=Matrix4[mat1].y.z + Matrix4[mat2].y.z
Matrix4[resultmat].y.w=Matrix4[mat1].y.w + Matrix4[mat2].y.w
// z
Matrix4[resultmat].z.x=Matrix4[mat1].z.x + Matrix4[mat2].z.x
Matrix4[resultmat].z.y=Matrix4[mat1].z.y + Matrix4[mat2].z.y
Matrix4[resultmat].z.z=Matrix4[mat1].z.z + Matrix4[mat2].z.z
Matrix4[resultmat].z.w=Matrix4[mat1].z.w + Matrix4[mat2].z.w
// w
Matrix4[resultmat].w.x=Matrix4[mat1].w.x + Matrix4[mat2].w.x
Matrix4[resultmat].w.y=Matrix4[mat1].w.y + Matrix4[mat2].w.y
Matrix4[resultmat].w.z=Matrix4[mat1].w.z + Matrix4[mat2].w.z
Matrix4[resultmat].w.w=Matrix4[mat1].w.w + Matrix4[mat2].w.w
endfunction
// substract matrix4 2 from matrix4 1 to resultmatrix4
function SubstractMatrix4(resultmat,mat1,mat2)
// x
Matrix4[resultmat].x.x=Matrix4[mat1].x.x - Matrix4[mat2].x.x
Matrix4[resultmat].x.y=Matrix4[mat1].x.y - Matrix4[mat2].x.y
Matrix4[resultmat].x.z=Matrix4[mat1].x.z - Matrix4[mat2].x.z
Matrix4[resultmat].x.w=Matrix4[mat1].x.w - Matrix4[mat2].x.w
// y
Matrix4[resultmat].y.x=Matrix4[mat1].y.x - Matrix4[mat2].y.x
Matrix4[resultmat].y.y=Matrix4[mat1].y.y - Matrix4[mat2].y.y
Matrix4[resultmat].y.z=Matrix4[mat1].y.z - Matrix4[mat2].y.z
Matrix4[resultmat].y.w=Matrix4[mat1].y.w - Matrix4[mat2].y.w
// z
Matrix4[resultmat].z.x=Matrix4[mat1].z.x - Matrix4[mat2].z.x
Matrix4[resultmat].z.y=Matrix4[mat1].z.y - Matrix4[mat2].z.y
Matrix4[resultmat].z.z=Matrix4[mat1].z.z - Matrix4[mat2].z.z
Matrix4[resultmat].z.w=Matrix4[mat1].z.w - Matrix4[mat2].z.w
// w
Matrix4[resultmat].w.x=Matrix4[mat1].w.x - Matrix4[mat2].w.x
Matrix4[resultmat].w.y=Matrix4[mat1].w.y - Matrix4[mat2].w.y
Matrix4[resultmat].w.z=Matrix4[mat1].w.z - Matrix4[mat2].w.z
Matrix4[resultmat].w.w=Matrix4[mat1].w.w - Matrix4[mat2].w.w
endfunction
// multiply matrix4 2 from matrix4 1 to resultmatrix4 or multiply resultmatrix4 with mat1 value
function MultiplyMatrix4(resultmat,mat1 as float,mat2)
if mat2=-1 // if mat2 is entered with -1, mat1 will become the multiply value
// multiply matrix by value
// x
Matrix4[resultmat].x.x=Matrix4[resultmat].x.x * mat1
Matrix4[resultmat].x.y=Matrix4[resultmat].x.y * mat1
Matrix4[resultmat].x.z=Matrix4[resultmat].x.z * mat1
Matrix4[resultmat].x.w=Matrix4[resultmat].x.w * mat1
// y
Matrix4[resultmat].y.x=Matrix4[resultmat].y.x * mat1
Matrix4[resultmat].y.y=Matrix4[resultmat].y.y * mat1
Matrix4[resultmat].y.z=Matrix4[resultmat].y.z * mat1
Matrix4[resultmat].y.w=Matrix4[resultmat].y.w * mat1
// z
Matrix4[resultmat].z.x=Matrix4[resultmat].z.x * mat1
Matrix4[resultmat].z.y=Matrix4[resultmat].z.y * mat1
Matrix4[resultmat].z.z=Matrix4[resultmat].z.z * mat1
Matrix4[resultmat].z.w=Matrix4[resultmat].z.w * mat1
// w
Matrix4[resultmat].w.x=Matrix4[resultmat].w.x * mat1
Matrix4[resultmat].w.y=Matrix4[resultmat].w.y * mat1
Matrix4[resultmat].w.z=Matrix4[resultmat].w.z * mat1
Matrix4[resultmat].w.w=Matrix4[resultmat].w.w * mat1
else
// multiply 2 matrices
// x
Matrix4[resultmat].x.x=Matrix4[val(str(mat1))].x.x * Matrix4[mat2].x.x
Matrix4[resultmat].x.y=Matrix4[val(str(mat1))].x.y * Matrix4[mat2].x.y
Matrix4[resultmat].x.z=Matrix4[val(str(mat1))].x.z * Matrix4[mat2].x.z
Matrix4[resultmat].x.w=Matrix4[val(str(mat1))].x.w * Matrix4[mat2].x.w
// y
Matrix4[resultmat].y.x=Matrix4[val(str(mat1))].y.x * Matrix4[mat2].y.x
Matrix4[resultmat].y.y=Matrix4[val(str(mat1))].y.y * Matrix4[mat2].y.y
Matrix4[resultmat].y.z=Matrix4[val(str(mat1))].y.z * Matrix4[mat2].y.z
Matrix4[resultmat].y.w=Matrix4[val(str(mat1))].y.w * Matrix4[mat2].y.w
// z
Matrix4[resultmat].z.x=Matrix4[val(str(mat1))].z.x * Matrix4[mat2].z.x
Matrix4[resultmat].z.y=Matrix4[val(str(mat1))].z.y * Matrix4[mat2].z.y
Matrix4[resultmat].z.z=Matrix4[val(str(mat1))].z.z * Matrix4[mat2].z.z
Matrix4[resultmat].z.w=Matrix4[val(str(mat1))].z.w * Matrix4[mat2].z.w
// w
Matrix4[resultmat].w.x=Matrix4[val(str(mat1))].w.x * Matrix4[mat2].w.x
Matrix4[resultmat].w.y=Matrix4[val(str(mat1))].w.y * Matrix4[mat2].w.y
Matrix4[resultmat].w.z=Matrix4[val(str(mat1))].w.z * Matrix4[mat2].w.z
Matrix4[resultmat].w.w=Matrix4[val(str(mat1))].w.w * Matrix4[mat2].w.w
endif
endfunction
// divide matrix4 2 from matrix4 1 to resultmatrix4 or divide matrix4 1 by the mat1 value
function DivideMatrix4(resultmat,mat1 as float,mat2)
if mat2=-1 // if mat2 is entered with -1, mat1 will become the multiply value
// multiply matrix by value
// x
Matrix4[resultmat].x.x=Matrix4[resultmat].x.x / mat1
Matrix4[resultmat].x.y=Matrix4[resultmat].x.y / mat1
Matrix4[resultmat].x.z=Matrix4[resultmat].x.z / mat1
Matrix4[resultmat].x.w=Matrix4[resultmat].x.w / mat1
// y
Matrix4[resultmat].y.x=Matrix4[resultmat].y.x / mat1
Matrix4[resultmat].y.y=Matrix4[resultmat].y.y / mat1
Matrix4[resultmat].y.z=Matrix4[resultmat].y.z / mat1
Matrix4[resultmat].y.w=Matrix4[resultmat].y.w / mat1
// z
Matrix4[resultmat].z.x=Matrix4[resultmat].z.x / mat1
Matrix4[resultmat].z.y=Matrix4[resultmat].z.y / mat1
Matrix4[resultmat].z.z=Matrix4[resultmat].z.z / mat1
Matrix4[resultmat].z.w=Matrix4[resultmat].z.w / mat1
// w
Matrix4[resultmat].w.x=Matrix4[resultmat].w.x / mat1
Matrix4[resultmat].w.y=Matrix4[resultmat].w.y / mat1
Matrix4[resultmat].w.z=Matrix4[resultmat].w.z / mat1
Matrix4[resultmat].w.w=Matrix4[resultmat].w.w / mat1
else
// multiply 2 matrices
// x
Matrix4[resultmat].x.x=Matrix4[val(str(mat1))].x.x / Matrix4[mat2].x.x
Matrix4[resultmat].x.y=Matrix4[val(str(mat1))].x.y / Matrix4[mat2].x.y
Matrix4[resultmat].x.z=Matrix4[val(str(mat1))].x.z / Matrix4[mat2].x.z
Matrix4[resultmat].x.w=Matrix4[val(str(mat1))].x.w / Matrix4[mat2].x.w
// y
Matrix4[resultmat].y.x=Matrix4[val(str(mat1))].y.x / Matrix4[mat2].y.x
Matrix4[resultmat].y.y=Matrix4[val(str(mat1))].y.y / Matrix4[mat2].y.y
Matrix4[resultmat].y.z=Matrix4[val(str(mat1))].y.z / Matrix4[mat2].y.z
Matrix4[resultmat].y.w=Matrix4[val(str(mat1))].y.w / Matrix4[mat2].y.w
// z
Matrix4[resultmat].z.x=Matrix4[val(str(mat1))].z.x / Matrix4[mat2].z.x
Matrix4[resultmat].z.y=Matrix4[val(str(mat1))].z.y / Matrix4[mat2].z.y
Matrix4[resultmat].z.z=Matrix4[val(str(mat1))].z.z / Matrix4[mat2].z.z
Matrix4[resultmat].z.w=Matrix4[val(str(mat1))].z.w / Matrix4[mat2].z.w
// w
Matrix4[resultmat].w.x=Matrix4[val(str(mat1))].w.x / Matrix4[mat2].w.x
Matrix4[resultmat].w.y=Matrix4[val(str(mat1))].w.y / Matrix4[mat2].w.y
Matrix4[resultmat].w.z=Matrix4[val(str(mat1))].w.z / Matrix4[mat2].w.z
Matrix4[resultmat].w.w=Matrix4[val(str(mat1))].w.w / Matrix4[mat2].w.w
endif
endfunction
// set matrix4 element (ele = 0 to 15)
function SetMatrix4Element(mat,ele,value#)
select ele
// x
case 0:
Matrix4[mat].x.x = value#
endcase
case 1:
Matrix4[mat].x.y = value#
endcase
case 2:
Matrix4[mat].x.z = value#
endcase
case 3:
Matrix4[mat].x.w = value#
endcase
// y
case 4:
Matrix4[mat].y.x = value#
endcase
case 5:
Matrix4[mat].y.y = value#
endcase
case 6:
Matrix4[mat].y.z = value#
endcase
case 7:
Matrix4[mat].y.w = value#
endcase
// z
case 8:
Matrix4[mat].z.x = value#
endcase
case 9:
Matrix4[mat].z.y = value#
endcase
case 10:
Matrix4[mat].z.z = value#
endcase
case 11:
Matrix4[mat].z.w = value#
endcase
// z
case 12:
Matrix4[mat].w.x = value#
endcase
case 13:
Matrix4[mat].w.y = value#
endcase
case 14:
Matrix4[mat].w.z = value#
endcase
case 15:
Matrix4[mat].w.w = value#
endcase
endselect
endfunction
// get matrix4 element (ele = 0 to 15)
function GetMatrix4Element(mat,ele)
select ele
// x
case 0:
result# = Matrix4[mat].x.x
endcase
case 1:
result# = Matrix4[mat].x.y
endcase
case 2:
result# = Matrix4[mat].x.z
endcase
case 3:
result# = Matrix4[mat].x.w
endcase
// y
case 4:
result# = Matrix4[mat].y.x
endcase
case 5:
result# = Matrix4[mat].y.y
endcase
case 6:
result# = Matrix4[mat].y.z
endcase
case 7:
result# = Matrix4[mat].y.w
endcase
// z
case 8:
result# = Matrix4[mat].z.x
endcase
case 9:
result# = Matrix4[mat].z.y
endcase
case 10:
result# = Matrix4[mat].z.z
endcase
case 11:
result# = Matrix4[mat].z.w
endcase
// z
case 12:
result# = Matrix4[mat].w.x
endcase
case 13:
result# = Matrix4[mat].w.y
endcase
case 14:
result# = Matrix4[mat].w.z
endcase
case 15:
result# = Matrix4[mat].w.w
endcase
endselect
endfunction result#
// set matrix4 (matrix4, 16 floats)
function SetMatrix4(mat, f0 as float, f1 as float, f2 as float, f3 as float, f4 as float, f5 as float, f6 as float, f7 as float, f8 as float, f9 as float, f10 as float, f11 as float, f12 as float, f13 as float, f14 as float, f15 as float)
// x
Matrix4[mat].x.x = f0
Matrix4[mat].x.y = f1
Matrix4[mat].x.z = f2
Matrix4[mat].x.w = f3
// y
Matrix4[mat].y.x = f4
Matrix4[mat].y.y = f5
Matrix4[mat].y.z = f6
Matrix4[mat].y.w = f7
// z
Matrix4[mat].z.x = f8
Matrix4[mat].z.y = f9
Matrix4[mat].z.z = f10
Matrix4[mat].z.w = f11
// w
Matrix4[mat].w.x = f12
Matrix4[mat].w.y = f13
Matrix4[mat].w.z = f14
Matrix4[mat].w.w = f15
endfunction
// projection matrix
function ProjectionMatrix4(mat, fov as float, aspect as float, nearDist as float, farDist)
frustumDepth as float
oneOverDepth as float
frustumDepth = farDist - nearDist
oneOverDepth = 1 / frustumDepth
// write informations into the 4x4 matrix
Matrix4[mat].x.x = 1/tan(fov*0.5) : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = farDist * oneOverDepth : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = farDist * oneOverDepth : Matrix4[mat].w.z = 1
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = (-farDist * nearDist) * oneOverDepth : Matrix4[mat].w.w = 0
endfunction
// translate Matrix 4
function TranslateMatrix4(mat, x as float, y as float, z as float)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = 1 : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = 1 : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = 1 : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = x : Matrix4[mat].y.w = y : Matrix4[mat].z.w = z : Matrix4[mat].w.w = 1
endfunction
// X Rotate Matrix 4
function RotateXMatrix4(mat, angle as float)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = 1 : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = cos(angle) : Matrix4[mat].z.y = sin(angle) : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = -sin(angle): Matrix4[mat].z.z = cos(angle) : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = 0 : Matrix4[mat].w.w = 1
endfunction
// Y Rotate Matrix 4
function RotateYMatrix4(mat, angle as float)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = cos(angle) : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = -sin(angle) : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = 1 : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = sin(angle) : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = cos(angle) : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = 0 : Matrix4[mat].w.w = 1
endfunction
// Z Rotate Matrix 4
function RotateZMatrix4(mat, angle as float)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = cos(angle) : Matrix4[mat].y.x = sin(angle) : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = -sin(angle) : Matrix4[mat].y.y = cos(angle) : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = 1 : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = 0 : Matrix4[mat].w.w = 1
endfunction
// View Matrix 4
function ViewMatrix4(xVecID, yVecID, zVecID, posVecID, matRotID, matViewID, matTempRotID)
SetIdentityMatrix4(matViewID)
setvector3(posVecID, getcamerax(1), getcameray(1), getcameraz(1) )
RotateZMatrix4( matRotID, radian(cameraAngleZ(1)) )
RotateYMatrix4( matTempRotID, radian(cameraAngleY(1)) )
MultiplyMatrix4(matRotID, matRotID, matTempRotID)
RotateXMatrix4( matTempRotID, radian(cameraAngleX(1)) )
MultiplyMatrix4(matRotID, matRotID, matTempRotID)
setvector3(xVecID, 1.0, 0.0, 0.0 )
setvector3(yVecID, 0.0, 1.0, 0.0 )
setvector3(zVecID, 0.0, 0.0, 1.0 )
TransformCoordsVector3(xVecID, matRotID)
TransformCoordsVector3(yVecID, matRotID)
TransformCoordsVector3(zVecID, matRotID)
SetMatrix4Element(matViewID, 0, GetVector3X(xVecID) )
SetMatrix4Element(matViewID, 1, GetVector3X(yVecID) )
SetMatrix4Element(matViewID, 2, GetVector3X(zVecID) )
SetMatrix4Element(matViewID, 4, GetVector3Y(xVecID) )
SetMatrix4Element(matViewID, 5, GetVector3Y(yVecID) )
SetMatrix4Element(matViewID, 6, GetVector3Y(zVecID) )
SetMatrix4Element(matViewID, 8, GetVector3Z(xVecID) )
SetMatrix4Element(matViewID, 9, GetVector3Z(yVecID) )
SetMatrix4Element(matViewID, 10,GetVector3Z(zVecID) )
SetMatrix4Element(matViewID, 12, -GetVector3Dot(posVecID, xVecID) )
SetMatrix4Element(matViewID, 13, -GetVector3Dot(posVecID, yVecID) )
SetMatrix4Element(matViewID, 14, -GetVector3Dot(posVecID, zVecID) )
endfunction
// Not finished commands:
// Inverse Matrix 4
function InverseMatrix4(mresult, msource)
endfunction
// Identity Matrix 4
function SetIdentityMatrix4(mat)
// write informations into the 4x4 matrix
Matrix4[mat].x.x = 1 : Matrix4[mat].y.x = 0 : Matrix4[mat].z.x = 0 : Matrix4[mat].w.x = 0
Matrix4[mat].x.y = 0 : Matrix4[mat].y.y = 1 : Matrix4[mat].z.y = 0 : Matrix4[mat].w.y = 0
Matrix4[mat].x.z = 0 : Matrix4[mat].y.z = 0 : Matrix4[mat].z.z = 1 : Matrix4[mat].w.z = 0
Matrix4[mat].x.w = 0 : Matrix4[mat].y.w = 0 : Matrix4[mat].z.w = 0 : Matrix4[mat].w.w = 1
endfunction
// Build Look at lh matrix
function BuildLookAtLhMatrix4()
vecEye = createvector3()
vecAt = createvector3()
vecUp = createvector3()
vecXAxis = createvector3()
vecYAxis = createvector3()
vecZAxis = createvector3()
matView = creatematrix4()
setvector3(vecEye, 0, 0, 0)
setvector3(vecAt, 0, 0, 1)
setvector3(vecUp, 0, 1, 0)
SubstractVector3(vecZAxis, vecAt, vecEye)
normalizeVector3(vecZAxis) // normalize vector3 vecZAxis, vecZAxis
GetVector3Cross( vecXAxis, vecUp, vecZAxis ) // cross product vector3 vecXAxis, vecUp, vecZAxis
normalizeVector3(vecXAxis) // normalize vector3 vecXAxis, vecXAxis
GetVector3Cross( vecYAxis, vecZAxis, vecXAxis ) // cross product vector3 vecYAxis, vecZAxis, vecXAxis
SetMatrix4Element(matView, 0, getvector3x(vecXAxis) )
SetMatrix4Element(matView, 1, getvector3x(vecYAxis) )
SetMatrix4Element(matView, 2, getvector3x(vecZAxis) )
SetMatrix4Element(matView, 3, 0.0 )
SetMatrix4Element(matView, 4, getvector3y(vecXAxis) )
SetMatrix4Element(matView, 5, getvector3y(vecYAxis) )
SetMatrix4Element(matView, 6, getvector3y(vecZAxis) )
SetMatrix4Element(matView, 7, 0.0 )
SetMatrix4Element(matView, 8, getvector3z(vecXAxis) )
SetMatrix4Element(matView, 9, getvector3z(vecYAxis) )
SetMatrix4Element(matView, 10, getvector3z(vecZAxis) )
SetMatrix4Element(matView, 11, 0.0 )
SetMatrix4Element(matView, 12, -GetVector3Dot( vecXAxis, vecEye ) )
SetMatrix4Element(matView, 13, -GetVector3Dot( vecYAxis, vecEye ) )
SetMatrix4Element(matView, 14, -GetVector3Dot( vecZAxis, vecEye ) )
SetMatrix4Element(matView, 15, 1.0 )
// is this a good idea?:
DeleteVector3(vecEye)
DeleteVector3(vecAt)
DeleteVector3(vecUp)
DeleteVector3(vecXAxis)
DeleteVector3(vecYAxis)
DeleteVector3(vecZAxis)
endfunction matView
// various math commands
// ==================
// this function will convert an euler angle to a radian angle
function radian(eul#)
out# = eul# * 180 / Math_PI // conversion to degrees
if( out# < 0 ) then out# = out# + 360.0 // convert negative to positive angles
endfunction out#