summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjussi2023-11-13 23:42:58 +0200
committerjussi2023-11-13 23:42:58 +0200
commit7b10306ed5c87517800a6058ad84e27c866f81c6 (patch)
tree68b58fed65ed81362a80b34974bfaa8c4345e5c8
parente06d98e0ed820c64df99c451e2c24a481d7349e8 (diff)
downloadreilua-enhanced-7b10306ed5c87517800a6058ad84e27c866f81c6.tar.gz
reilua-enhanced-7b10306ed5c87517800a6058ad84e27c866f81c6.tar.bz2
reilua-enhanced-7b10306ed5c87517800a6058ad84e27c866f81c6.zip
Matrix library.
-rw-r--r--API.md4
-rw-r--r--ReiLua_API.lua8
-rw-r--r--changelog1
-rw-r--r--devnotes3
-rw-r--r--examples/resources/lib/matrix.lua151
-rw-r--r--src/lua_core.c13
-rw-r--r--src/rmath.c4
7 files changed, 176 insertions, 8 deletions
diff --git a/API.md b/API.md
index 56444ad..47dece1 100644
--- a/API.md
+++ b/API.md
@@ -7710,7 +7710,7 @@ Get z-rotation matrix (angle in radians)
---
-> result = RL.MatrixRotateXYZ( Vector3 angle )
+> result = RL.MatrixRotateXYZ( Vector3 angles )
Get xyz-rotation matrix (angles in radians)
@@ -7718,7 +7718,7 @@ Get xyz-rotation matrix (angles in radians)
---
-> result = RL.MatrixRotateZYX( Vector3 angle )
+> result = RL.MatrixRotateZYX( Vector3 angles )
Get zyx-rotation matrix (angles in radians)
diff --git a/ReiLua_API.lua b/ReiLua_API.lua
index 839ff74..67ace3f 100644
--- a/ReiLua_API.lua
+++ b/ReiLua_API.lua
@@ -4958,15 +4958,15 @@ function RL.MatrixRotateZ( angle ) end
---Get xyz-rotation matrix (angles in radians)
---- Success return Matrix
----@param angle table
+---@param angles table
---@return any result
-function RL.MatrixRotateXYZ( angle ) end
+function RL.MatrixRotateXYZ( angles ) end
---Get zyx-rotation matrix (angles in radians)
---- Success return Matrix
----@param angle table
+---@param angles table
---@return any result
-function RL.MatrixRotateZYX( angle ) end
+function RL.MatrixRotateZYX( angles ) end
---Get scaling matrix
---- Success return Matrix
diff --git a/changelog b/changelog
index ae18b6e..b0a6bfd 100644
--- a/changelog
+++ b/changelog
@@ -17,6 +17,7 @@ KEY CHANGES:
- CHANGED: DrawText is now DrawTextEx like in Raylib.
- ADDED: Global variable descriptions for API.
- CHANGED: Organized functions by putting them in the same order as in Raylib.
+ - ADDED: Matrix library.
DETAILED CHANGES:
- CHANGED: GenImageColor now takes Vector2 as size.
diff --git a/devnotes b/devnotes
index 3f63a1f..361c46f 100644
--- a/devnotes
+++ b/devnotes
@@ -15,6 +15,9 @@ Backlog {
* More Textures management functions.
* Shader buffer storage object management (ssbo).
+ * Matrix class.
+ * Quaternion class.
+
* Examples
* Improve Dungeon crawler example by generating custom mesh instead of drawing 3D quads.
* Platformer example physics process for true framerate independence.
diff --git a/examples/resources/lib/matrix.lua b/examples/resources/lib/matrix.lua
new file mode 100644
index 0000000..4b8cee3
--- /dev/null
+++ b/examples/resources/lib/matrix.lua
@@ -0,0 +1,151 @@
+-- For luaJit compatibility.
+if table.unpack == nil then
+ table.unpack = unpack
+end
+
+local function deepCopy( orig )
+ local copy
+
+ if type( orig ) == "table" then
+ copy = {}
+
+ for origKey, origValue in next, orig, nil do
+ -- If object has clone method, use that.
+ copy[ deepCopy( origKey ) ] = deepCopy( origValue )
+ end
+
+ -- setmetatable( copy, utillib.deepCopy( getmetatable( orig ) ) )
+ else -- number, string, boolean, etc.
+ copy = orig
+ end
+
+ return copy
+end
+
+Matrix = {}
+Matrix.meta = {
+ __index = Matrix,
+ __tostring = function( m )
+ return "{\n"
+ .." { "..m.m[1][1]..", "..m.m[1][2]..", "..m.m[1][3]..", "..m.m[1][4].." },\n"
+ .." { "..m.m[2][1]..", "..m.m[2][2]..", "..m.m[2][3]..", "..m.m[2][4].." },\n"
+ .." { "..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].." },\n"
+ .." { "..m.m[3][1]..", "..m.m[3][2]..", "..m.m[3][3]..", "..m.m[3][4].." },\n"
+ .."}"
+ end,
+ __add = function( m1, m2 )
+ return Matrix:new( RL.MatrixAdd( m1, m2 ) )
+ end,
+ __sub = function( m1, m2 )
+ return Matrix:new( RL.MatrixSubtract( m1, m2 ) )
+ end,
+ __mul = function( m1, m2 )
+ return Matrix:new( RL.MatrixMultiply( m1, m2 ) )
+ end
+}
+
+function Matrix:new( m )
+ local object = setmetatable( {}, Matrix.meta )
+
+ if type( m ) == "table" then
+ object.m = deepCopy( m )
+ else
+ object.m = RL.MatrixIdentity()
+ end
+
+ return object
+end
+
+function Matrix:set( m )
+ if type( m ) == "table" then
+ self.m = deepCopy( m )
+ else
+ self.m = RL.MatrixIdentity()
+ end
+end
+
+-- function Matrix:arr()
+-- return self.m
+-- end
+
+-- function Matrix:unpack()
+-- return self.m
+-- end
+
+function Matrix:clone()
+ return Matrix:new( self )
+end
+
+function Matrix:determinant()
+ return RL.MatrixDeterminant( self )
+end
+
+function Matrix:trace()
+ return RL.MatrixTranspose( self )
+end
+
+function Matrix:transpose()
+ return Matrix:new( RL.MatrixTranspose( self ) )
+end
+
+function Matrix:multiply( m2 )
+ return Matrix:new( RL.MatrixMultiply( self, m2 ) )
+end
+
+function Matrix:invert()
+ return Matrix:new( RL.MatrixInvert( self ) )
+end
+
+function Matrix:identity()
+ return Matrix:new( RL.MatrixIdentity() )
+end
+
+function Matrix:translate( translate )
+ return Matrix:new( RL.MatrixTranslate( translate ) )
+end
+
+function Matrix:rotate( axis, angle )
+ return Matrix:new( RL.MatrixRotate( axis, angle ) )
+end
+
+function Matrix:rotateX( angle )
+ return Matrix:new( RL.MatrixRotateX( angle ) )
+end
+
+function Matrix:rotateY( angle )
+ return Matrix:new( RL.MatrixRotateY( angle ) )
+end
+
+function Matrix:rotateZ( angle )
+ return Matrix:new( RL.MatrixRotateZ( angle ) )
+end
+
+function Matrix:rotateXYZ( angles )
+ return Matrix:new( RL.MatrixRotateXYZ( angles ) )
+end
+
+function Matrix:rotateZYX( angles )
+ return Matrix:new( RL.MatrixRotateZYX( angles ) )
+end
+
+function Matrix:scale( scale )
+ return Matrix:new( RL.MatrixScale( scale ) )
+end
+
+function Matrix:frustrum( left, right, bottom, top, near, far )
+ return Matrix:new( RL.MatrixFrustum( left, right, bottom, top, near, far ) )
+end
+
+function Matrix:perspective( fovy, aspect, near, far )
+ return Matrix:new( RL.MatrixPerspective( fovy, aspect, near, far ) )
+end
+
+function Matrix:ortho( left, right, bottom, top, near, far )
+ return Matrix:new( RL.MatrixOrtho( left, right, bottom, top, near, far ) )
+end
+
+function Matrix:lookAt( eye, target, up )
+ return Matrix:new( RL.MatrixLookAt( eye, target, up ) )
+end
+
+return Matrix
diff --git a/src/lua_core.c b/src/lua_core.c
index 843a38a..0eca9be 100644
--- a/src/lua_core.c
+++ b/src/lua_core.c
@@ -2693,6 +2693,19 @@ Matrix uluaGetMatrix( lua_State *L, int index ) {
if ( lua_isnumber( L, -1 ) ) {
m[i][j] = lua_tonumber( L, -1 );
}
+ /* Look for one depth further if Matrix given as class. */
+ else if ( lua_istable( L, -1 ) ) {
+ int t3 = lua_gettop( L ), k = 0;
+ lua_pushnil( L );
+
+ while ( lua_next( L, t3 ) != 0 ) {
+ if ( lua_isnumber( L, -1 ) ) {
+ m[j][k] = lua_tonumber( L, -1 );
+ }
+ k++;
+ lua_pop( L, 1 );
+ }
+ }
j++;
lua_pop( L, 1 );
}
diff --git a/src/rmath.c b/src/rmath.c
index 529a347..8dd9905 100644
--- a/src/rmath.c
+++ b/src/rmath.c
@@ -1308,7 +1308,7 @@ int lmathMatrixRotateZ( lua_State *L ) {
}
/*
-> result = RL.MatrixRotateXYZ( Vector3 angle )
+> result = RL.MatrixRotateXYZ( Vector3 angles )
Get xyz-rotation matrix (angles in radians)
@@ -1323,7 +1323,7 @@ int lmathMatrixRotateXYZ( lua_State *L ) {
}
/*
-> result = RL.MatrixRotateZYX( Vector3 angle )
+> result = RL.MatrixRotateZYX( Vector3 angles )
Get zyx-rotation matrix (angles in radians)