diff options
| author | jussi | 2025-06-02 20:49:51 +0300 |
|---|---|---|
| committer | jussi | 2025-06-02 20:49:51 +0300 |
| commit | 5b8af05e96b33f2d032cc31a329b89e1231d5502 (patch) | |
| tree | 9ea0f70bae6c77cfc0edeb3f4b0b11fa9fb6be94 /src/rmath.c | |
| parent | e26bb8603c5a4053f2790fc7d6ce02b3179f5289 (diff) | |
| download | reilua-enhanced-5b8af05e96b33f2d032cc31a329b89e1231d5502.tar.gz reilua-enhanced-5b8af05e96b33f2d032cc31a329b89e1231d5502.tar.bz2 reilua-enhanced-5b8af05e96b33f2d032cc31a329b89e1231d5502.zip | |
Frustum math from raylib extras.
Diffstat (limited to 'src/rmath.c')
| -rw-r--r-- | src/rmath.c | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/rmath.c b/src/rmath.c index 40e8cb8..d1e14f0 100644 --- a/src/rmath.c +++ b/src/rmath.c @@ -2,6 +2,7 @@ #include "state.h" #include "rmath.h" #include "lua_core.h" +#include "frustum.h" int imin( int a, int b ) { return a < b ? a : b; @@ -2343,3 +2344,103 @@ int lmathQuaternionEquals( lua_State* L ) { return 1; } + +/* +## Math - Frustum +*/ + +static Frustum getFrustum( lua_State* L, int index ) { + luaL_checktype( L, index, LUA_TTABLE ); + Frustum frustum = { 0 }; + + int t = index, i = 0; + lua_pushnil( L ); + + while ( lua_next( L, t ) != 0 && i < 6 ) { + frustum.Planes[i] = uluaGetVector4( L, lua_gettop( L ) ); + i++; + lua_pop( L, 1 ); + } + + return frustum; +} + +static int pushFrustum( lua_State* L, Frustum* frustum ) { + lua_createtable( L, 6, 0 ); + + for ( int i = 0; i < 6; i++ ) { + uluaPushVector4( L, frustum->Planes[i] ); + lua_rawseti( L, -2, 1 + i ); + } + + return 1; +} + +/* +> frustum = RL.ExtractFrustum( Matrix projection, Matrix modelview ) + +Extract frustum from projection and modelView matrices. + +- Success return Vector4{} +*/ +int lmathExtractFrustum( lua_State* L ) { + Matrix projection = uluaGetMatrix( L, 1 ); + Matrix modelview = uluaGetMatrix( L, 2 ); + + Frustum frustum = { 0 }; + ExtractFrustum( &frustum, projection, modelview ); + + pushFrustum( L, &frustum ); + + return 1; +} + +/* +> inFrustum = RL.PointInFrustum( Vector4{} frustum, Vector3 position ) + +Check if point inside frustum + +- Success return bool +*/ +int lmathPointInFrustum( lua_State* L ) { + Frustum frustum = getFrustum( L, 1 ); + Vector3 position = uluaGetVector3( L, 2 ); + + lua_pushboolean( L, PointInFrustumV( &frustum, position ) ); + + return 1; +} + +/* +> inFrustum = RL.SphereInFrustum( Vector4{} frustum, Vector3 position ) + +Check if sphere inside frustum + +- Success return bool +*/ +int lmathSphereInFrustum( lua_State* L ) { + Frustum frustum = getFrustum( L, 1 ); + Vector3 position = uluaGetVector3( L, 2 ); + float radius = luaL_checknumber( L, 3 ); + + lua_pushboolean( L, SphereInFrustumV( &frustum, position, radius ) ); + + return 1; +} + +/* +> inFrustum = RL.AABBInFrustum( Vector4{} frustum, Vector3 min, Vector3 max ) + +Check if AABB inside frustum + +- Success return bool +*/ +int lmathAABBInFrustum( lua_State* L ) { + Frustum frustum = getFrustum( L, 1 ); + Vector3 min = uluaGetVector3( L, 2 ); + Vector3 max = uluaGetVector3( L, 3 ); + + lua_pushboolean( L, AABBoxInFrustum( &frustum, min, max ) ); + + return 1; +} |
