summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--API.md24
-rw-r--r--examples/resources/lib/vector2.lua112
-rw-r--r--examples/resources/lib/vector3.lua129
-rw-r--r--include/models.h2
-rw-r--r--src/audio.c6
-rw-r--r--src/lua_core.c2
-rw-r--r--src/models.c53
-rw-r--r--src/rmath.c59
8 files changed, 351 insertions, 36 deletions
diff --git a/API.md b/API.md
index a979af0..5c6aa57 100644
--- a/API.md
+++ b/API.md
@@ -2848,6 +2848,24 @@ Draw a billboard texture defined by source
---
+> success = RL_SetModelTransform( Model model, Matrix transform )
+
+Set model transform matrix
+
+- Failure return false
+- Success return true
+
+---
+
+> transform = RL_GetModelTransform( Model model )
+
+Get model transform matrix
+
+- Failure return false
+- Success return Matrix
+
+---
+
## Model - Animations
---
@@ -3210,7 +3228,7 @@ Subtract two vectors
---
-> result = RL_Vector3Subtract( Vector3 v1, Vector3 v2 )
+> result = RL_Vector3Multiply( Vector3 v1, Vector3 v2 )
Multiply vector by vector
@@ -3239,7 +3257,7 @@ Calculate one vector perpendicular vector
> result = RL_Vector3Length( Vector3 v )
-Calculate one vector perpendicular vector
+Calculate vector length
- Failure return false
- Success return float
@@ -3385,7 +3403,7 @@ Add two matrices
---
-> result = RL_MatrixAdd( Matrix left, Matrix right )
+> result = RL_MatrixSubtract( Matrix left, Matrix right )
Subtract two matrices (left - right)
diff --git a/examples/resources/lib/vector2.lua b/examples/resources/lib/vector2.lua
new file mode 100644
index 0000000..0ea10b9
--- /dev/null
+++ b/examples/resources/lib/vector2.lua
@@ -0,0 +1,112 @@
+Vector2 = {}
+Vector2.meta = {
+ __index = Vector2,
+ __tostring = function( v )
+ return "{"..tostring( v.x )..", "..tostring( v.y ).."}"
+ end,
+ __add = function( v1, v2 )
+ return Vector2:new( v1.x + v2.x, v1.y + v2.y )
+ end,
+ __sub = function( v1, v2 )
+ return Vector2:new( v1.x - v2.x, v1.y - v2.y )
+ end,
+ __mul = function( v1, v2 )
+ return Vector2:new( v1.x * v2.x, v1.y * v2.y )
+ end,
+ __div = function( v1, v2 )
+ return Vector2:new( v1.x / v2.x, v1.y / v2.y )
+ end,
+ __mod = function( v, value )
+ return Vector2:new( math.fmod( v.x, value ), math.fmod( v.y, value ) )
+ end,
+ __pow = function( v, value )
+ return Vector2:new( v.x ^ value, v.y ^ value )
+ end,
+ __unm = function( v )
+ return Vector2:new( -v.x, -v.y )
+ end,
+ __idiv = function( v, value )
+ return Vector2:new( v.x // value, v.y // value )
+ end,
+ __len = function( v )
+ local len = 0
+
+ for _, _ in pairs( v ) do
+ len = len + 1
+ end
+
+ return len
+ end,
+ __eq = function( v1, v2 )
+ return v1.x == v2.x and v1.y == v2.y
+ end,
+}
+
+function Vector2:new( x, y )
+ local o = {
+ x = x,
+ y = y,
+ }
+ setmetatable( o, Vector2.meta )
+ return o
+end
+
+function Vector2:set( vec )
+ self.x = vec[1]
+ self.y = vec[2]
+end
+
+function Vector2:arr()
+ return { self.x, self.y }
+end
+
+function Vector2:clone()
+ return Vector2:new( self.x, self.y )
+end
+
+function Vector2:abs()
+ return Vector2:new( math.abs( self.x ), math.abs( self.y ) )
+end
+
+function Vector2:length()
+ return RL_Vector2Length( self:arr() )
+end
+
+function Vector2:dot( v2 )
+ return RL_Vector2DotProduct( self:arr(), v2:arr() )
+end
+
+function Vector2:distance( v2 )
+ return RL_Vector2Distance( self:arr(), v2:arr() )
+end
+
+function Vector2:angle( v2 )
+ return RL_Vector2Angle( self:arr(), v2:arr() )
+end
+
+function Vector2:normalize()
+ local r = RL_Vector2Normalize( self:arr() )
+ return Vector2:new( r[1], r[2] )
+end
+
+function Vector2:lerp( v2, value )
+ local r = RL_Vector2Lerp( self:arr(), v2:arr(), value )
+ return Vector2:new( r[1], r[2] )
+end
+
+function Vector2:reflect( normal )
+ local r = RL_Vector2Reflect( self:arr(), normal:arr() )
+ return Vector2:new( r[1], r[2] )
+end
+
+function Vector2:rotate( angle )
+ local r = RL_Vector2Rotate( self:arr(), angle )
+ return Vector2:new( r[1], r[2] )
+end
+
+function Vector2:moveTowards( target, maxDistance )
+ local r = RL_Vector2MoveTowards( self:arr(), target:arr(), maxDistance )
+ return Vector2:new( r[1], r[2] )
+end
+
+return Vector2
diff --git a/examples/resources/lib/vector3.lua b/examples/resources/lib/vector3.lua
new file mode 100644
index 0000000..e3da9f1
--- /dev/null
+++ b/examples/resources/lib/vector3.lua
@@ -0,0 +1,129 @@
+Vector3 = {}
+Vector3.meta = {
+ __index = Vector3,
+ __tostring = function( v )
+ return "{"..tostring( v.x )..", "..tostring( v.y )..", "..tostring( v.z ).."}"
+ end,
+ __add = function( v1, v2 )
+ return Vector3:new( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z )
+ end,
+ __sub = function( v1, v2 )
+ return Vector3:new( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z )
+ end,
+ __mul = function( v1, v2 )
+ return Vector3:new( v1.x * v2.x, v1.y * v2.y, v1.z * v2.z )
+ end,
+ __div = function( v1, v2 )
+ return Vector3:new( v1.x / v2.x, v1.y / v2.y, v1.z / v2.z )
+ end,
+ __mod = function( v, value )
+ return Vector3:new( math.fmod( v.x, value ), math.fmod( v.y, value ), math.fmod( v.z, value ) )
+ end,
+ __pow = function( v, value )
+ return Vector3:new( v.x ^ value, v.y ^ value, v.z ^ value )
+ end,
+ __unm = function( v )
+ return Vector3:new( -v.x, -v.y, -v.z )
+ end,
+ __idiv = function( v, value )
+ return Vector3:new( v.x // value, v.y // value, v.z // value )
+ end,
+ __len = function( v )
+ local len = 0
+
+ for _, _ in pairs( v ) do
+ len = len + 1
+ end
+
+ return len
+ end,
+ __eq = function( v1, v2 )
+ return v1.x == v2.x and v1.y == v2.y and v1.z == v2.z
+ end,
+}
+
+function Vector3:new( x, y, z )
+ local o = {
+ x = x,
+ y = y,
+ z = z,
+ }
+ setmetatable( o, Vector3.meta )
+ return o
+end
+
+function Vector3:set( vec )
+ self.x = vec[1]
+ self.y = vec[2]
+ self.z = vec[3]
+end
+
+function Vector3:arr()
+ return { self.x, self.y, self.z }
+end
+
+function Vector3:clone()
+ return Vector3:new( self.x, self.y, self.z )
+end
+
+function Vector3:abs()
+ return Vector3:new( math.abs( self.x ), math.abs( self.y ), math.abs( self.z ) )
+end
+
+function Vector3:cross( v2 )
+ local r = RL_Vector3CrossProduct( self:arr(), v2:arr() )
+ return Vector3:new( r[1], r[2], r[3] )
+end
+
+function Vector3:perpendicular()
+ local r = RL_Vector3Perpendicular( self:arr() )
+ return Vector3:new( r[1], r[2], r[3] )
+end
+
+function Vector3:length()
+ return RL_Vector3Length( self:arr() )
+end
+
+function Vector3:lengthSqr()
+ return RL_Vector3LengthSqr( self:arr() )
+end
+
+function Vector3:dot( v2 )
+ return RL_Vector3DotProduct( self:arr(), v2:arr() )
+end
+
+function Vector3:distance( v2 )
+ return RL_Vector3Distance( self:arr(), v2:arr() )
+end
+
+function Vector3:normalize()
+ local r = RL_Vector3Normalize( self:arr() )
+ return Vector3:new( r[1], r[2], r[3] )
+end
+
+function Vector3:orthoNormalize( v2 )
+ local r1, r2 = RL_Vector3OrthoNormalize( self:arr(), v2:arr() )
+ return Vector3:new( r1[1], r1[2], r1[3] ), Vector3:new( r2[1], r2[2], r2[3] )
+end
+
+function Vector3:transform( mat )
+ local r = RL_Vector3Transform( self:arr(), mat )
+ return Vector3:new( r[1], r[2], r[3] )
+end
+
+function Vector3:rotateByQuaternion( qua )
+ local r = RL_Vector3RotateByQuaternion( self:arr(), qua )
+ return Vector3:new( r[1], r[2], r[3] )
+end
+
+function Vector3:lerp( v2, value )
+ local r = RL_Vector3Lerp( self:arr(), v2:arr(), value )
+ return Vector3:new( r[1], r[2], r[3] )
+end
+
+function Vector3:reflect( normal )
+ local r = RL_Vector3Reflect( self:arr(), normal:arr() )
+ return Vector3:new( r[1], r[2], r[3] )
+end
+
+return Vector3
diff --git a/include/models.h b/include/models.h
index 51193bb..d002807 100644
--- a/include/models.h
+++ b/include/models.h
@@ -51,6 +51,8 @@ int lmodelsSetModelMaterial( lua_State *L );
int lmodelsSetModelMeshMaterial( lua_State *L );
int lmodelsDrawBillboard( lua_State *L );
int lmodelsDrawBillboardRec( lua_State *L );
+int lmodelsSetModelTransform( lua_State *L );
+int lmodelsGetModelTransform( lua_State *L );
/* Animations. */
int lmodelsLoadModelAnimations( lua_State *L );
int lmodelsUpdateModelAnimation( lua_State *L );
diff --git a/src/audio.c b/src/audio.c
index 45ee6c9..48f3718 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -60,8 +60,10 @@ int laudioLoadSound( lua_State *L ) {
lua_pushinteger( L, i );
checkSoundRealloc( i );
}
-
- return 1;
+ else {
+ lua_pushinteger( L, -1 );
+ return 1;
+ }
}
/*
diff --git a/src/lua_core.c b/src/lua_core.c
index 5ac1aec..25cce86 100644
--- a/src/lua_core.c
+++ b/src/lua_core.c
@@ -727,6 +727,8 @@ void luaRegister() {
lua_register( L, "RL_SetModelMeshMaterial", lmodelsSetModelMeshMaterial );
lua_register( L, "RL_DrawBillboard", lmodelsDrawBillboard );
lua_register( L, "RL_DrawBillboardRec", lmodelsDrawBillboardRec );
+ lua_register( L, "RL_SetModelTransform", lmodelsSetModelTransform );
+ lua_register( L, "RL_GetModelTransform", lmodelsGetModelTransform );
/* Animations. */
lua_register( L, "RL_LoadModelAnimations", lmodelsLoadModelAnimations );
lua_register( L, "RL_UpdateModelAnimation", lmodelsUpdateModelAnimation );
diff --git a/src/models.c b/src/models.c
index f105991..483d0f8 100644
--- a/src/models.c
+++ b/src/models.c
@@ -1765,6 +1765,59 @@ int lmodelsDrawBillboardRec( lua_State *L ) {
}
/*
+> success = RL_SetModelTransform( Model model, Matrix transform )
+
+Set model transform matrix
+
+- Failure return false
+- Success return true
+*/
+int lmodelsSetModelTransform( lua_State *L ) {
+ if ( !lua_isnumber( L, -2 ) || !lua_istable( L, -1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_SetModelTransform( Model model, Matrix transform )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ Matrix transform = uluaGetMatrix( L );
+ lua_pop( L, 1 );
+ size_t modelId = lua_tointeger( L, -1 );
+
+ if ( !validModel( modelId ) ) {
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ state->models[ modelId ]->transform = transform;
+ lua_pushboolean( L, true );
+
+ return 1;
+}
+
+/*
+> transform = RL_GetModelTransform( Model model )
+
+Get model transform matrix
+
+- Failure return false
+- Success return Matrix
+*/
+int lmodelsGetModelTransform( lua_State *L ) {
+ if ( !lua_isnumber( L, -1 ) ) {
+ TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_GetModelTransform( Model model )" );
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ size_t modelId = lua_tointeger( L, -1 );
+
+ if ( !validModel( modelId ) ) {
+ lua_pushboolean( L, false );
+ return 1;
+ }
+ uluaPushMatrix( L, state->models[ modelId ]->transform );
+
+ return 1;
+}
+
+/*
## Model - Animations
*/
diff --git a/src/rmath.c b/src/rmath.c
index 4027aa8..b003b65 100644
--- a/src/rmath.c
+++ b/src/rmath.c
@@ -342,7 +342,7 @@ int lmathVector3Subtract( lua_State *L ) {
}
/*
-> result = RL_Vector3Subtract( Vector3 v1, Vector3 v2 )
+> result = RL_Vector3Multiply( Vector3 v1, Vector3 v2 )
Multiply vector by vector
@@ -411,7 +411,7 @@ int lmathVector3Perpendicular( lua_State *L ) {
/*
> result = RL_Vector3Length( Vector3 v )
-Calculate one vector perpendicular vector
+Calculate vector length
- Failure return false
- Success return float
@@ -559,11 +559,11 @@ int lmathVector3Transform( lua_State *L ) {
lua_pushboolean( L, false );
return 1;
}
- Vector3 v1 = uluaGetVector3( L );
- lua_pop( L, 1 );
Matrix mat = uluaGetMatrix( L );
+ lua_pop( L, 1 );
+ Vector3 v = uluaGetVector3( L );
- uluaPushVector3( L, Vector3Transform( v1, mat ) );
+ uluaPushVector3( L, Vector3Transform( v, mat ) );
return 1;
}
@@ -582,11 +582,11 @@ int lmathVector3RotateByQuaternion( lua_State *L ) {
lua_pushboolean( L, false );
return 1;
}
- Vector3 v1 = uluaGetVector3( L );
- lua_pop( L, 1 );
Quaternion q = uluaGetQuaternion( L );
+ lua_pop( L, 1 );
+ Vector3 v = uluaGetVector3( L );
- uluaPushVector3( L, Vector3RotateByQuaternion( v1, q ) );
+ uluaPushVector3( L, Vector3RotateByQuaternion( v, q ) );
return 1;
}
@@ -764,7 +764,7 @@ int lmathMatrixAdd( lua_State *L ) {
}
/*
-> result = RL_MatrixAdd( Matrix left, Matrix right )
+> result = RL_MatrixSubtract( Matrix left, Matrix right )
Subtract two matrices (left - right)
@@ -889,15 +889,14 @@ int lmathMatrixFrustum( lua_State *L ) {
lua_pushboolean( L, false );
return 1;
}
- // float far = lua_tonumber( L, -1 );
- // float near = lua_tonumber( L, -2 );
- // float top = lua_tonumber( L, -3 );
- // float bottom = lua_tonumber( L, -4 );
- // float right = lua_tonumber( L, -5 );
- // float left = lua_tonumber( L, -6 );
+ float far = lua_tonumber( L, -1 );
+ float near = lua_tonumber( L, -2 );
+ float top = lua_tonumber( L, -3 );
+ float bottom = lua_tonumber( L, -4 );
+ float right = lua_tonumber( L, -5 );
+ float left = lua_tonumber( L, -6 );
- // uluaPushMatrix( L, MatrixFrustum( left, right, bottom, top, near, far ) );
- uluaPushMatrix( L, MatrixFrustum( lua_tonumber( L, -6 ), lua_tonumber( L, -5 ), lua_tonumber( L, -4 ), lua_tonumber( L, -3 ), lua_tonumber( L, -2 ), lua_tonumber( L, -1 ) ) );
+ uluaPushMatrix( L, MatrixFrustum( left, right, bottom, top, near, far ) );
return 1;
}
@@ -916,13 +915,12 @@ int lmathMatrixPerspective( lua_State *L ) {
lua_pushboolean( L, false );
return 1;
}
- // float far = lua_tonumber( L, -1 );
- // float near = lua_tonumber( L, -2 );
- // float aspect = lua_tonumber( L, -3 );
- // float fovy = lua_tonumber( L, -4 );
+ float far = lua_tonumber( L, -1 );
+ float near = lua_tonumber( L, -2 );
+ float aspect = lua_tonumber( L, -3 );
+ float fovy = lua_tonumber( L, -4 );
- // uluaPushMatrix( L, MatrixPerspective( fovy, aspect, near, far ) );
- uluaPushMatrix( L, MatrixPerspective( lua_tonumber( L, -4 ), lua_tonumber( L, -3 ), lua_tonumber( L, -2 ), lua_tonumber( L, -1 ) ) );
+ uluaPushMatrix( L, MatrixPerspective( fovy, aspect, near, far ) );
return 1;
}
@@ -942,15 +940,14 @@ int lmathMatrixOrtho( lua_State *L ) {
lua_pushboolean( L, false );
return 1;
}
- // float far = lua_tonumber( L, -1 );
- // float near = lua_tonumber( L, -2 );
- // float top = lua_tonumber( L, -3 );
- // float bottom = lua_tonumber( L, -4 );
- // float right = lua_tonumber( L, -5 );
- // float left = lua_tonumber( L, -6 );
+ float far = lua_tonumber( L, -1 );
+ float near = lua_tonumber( L, -2 );
+ float top = lua_tonumber( L, -3 );
+ float bottom = lua_tonumber( L, -4 );
+ float right = lua_tonumber( L, -5 );
+ float left = lua_tonumber( L, -6 );
- // uluaPushMatrix( L, MatrixOrtho( left, right, bottom, top, near, far ) );
- uluaPushMatrix( L, MatrixOrtho( lua_tonumber( L, -6 ), lua_tonumber( L, -5 ), lua_tonumber( L, -4 ), lua_tonumber( L, -3 ), lua_tonumber( L, -2 ), lua_tonumber( L, -1 ) ) );
+ uluaPushMatrix( L, MatrixOrtho( left, right, bottom, top, near, far ) );
return 1;
}