Screen-space related functions for 3DCamera.

This commit is contained in:
jussi
2022-03-31 13:55:02 +03:00
parent a3c28c0001
commit 30d425aa26
7 changed files with 164 additions and 9 deletions

View File

@@ -2337,3 +2337,86 @@ int lcoreUpdateCamera3D( lua_State *L ) {
return 1;
}
/*
## Core - Screen-space
*/
/*
> ray = RL_GetMouseRay( Vector2 mousePosition, Camera3D camera )
Get a ray trace from mouse position
- Failure return false
- Success return Ray
*/
int lcoreGetMouseRay( lua_State *L ) {
if ( !lua_istable( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_GetMouseRay( Vector2 mousePosition, Camera3D camera )" );
lua_pushboolean( L, false );
return 1;
}
size_t cameraId = lua_tointeger( L, -1 );
lua_pop( L, 1 );
Vector2 mousePosition = uluaGetVector2( L );
if ( !validCamera3D( cameraId ) ) {
lua_pushboolean( L, false );
return 1;
}
uluaPushRay( L, GetMouseRay( mousePosition, *state->camera3Ds[ cameraId ] ) );
return 1;
}
/*
> matrix = RL_GetCameraMatrix( Camera3D camera )
Get camera transform matrix ( view matrix )
- Failure return false
- Success return Matrix
*/
int lcoreGetCameraMatrix( lua_State *L ) {
if ( !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_GetCameraMatrix( Camera3D camera )" );
lua_pushboolean( L, false );
return 1;
}
size_t cameraId = lua_tointeger( L, -1 );
if ( !validCamera3D( cameraId ) ) {
lua_pushboolean( L, false );
return 1;
}
uluaPushMatrix( L, GetCameraMatrix( *state->camera3Ds[ cameraId ] ) );
return 1;
}
/*
> position = RL_GetWorldToScreen( Vector3 position, Camera3D camera )
Get the screen space position for a 3d world space position
- Failure return false
- Success return Vector2
*/
int lcoreGetWorldToScreen( lua_State *L ) {
if ( !lua_istable( L, -2 ) || !lua_isnumber( L, -1 ) ) {
TraceLog( LOG_WARNING, "%s", "Bad call of function. RL_GetWorldToScreen( Vector3 position, Camera3D camera )" );
lua_pushboolean( L, false );
return 1;
}
size_t cameraId = lua_tointeger( L, -1 );
lua_pop( L, 1 );
Vector3 position = uluaGetVector3( L );
if ( !validCamera3D( cameraId ) ) {
lua_pushboolean( L, false );
return 1;
}
uluaPushVector2( L, GetWorldToScreen( position, *state->camera3Ds[ cameraId ] ) );
return 1;
}

View File

@@ -597,6 +597,10 @@ void luaRegister() {
lua_register( L, "RL_GetGestureDragAngle", lcoreGetGestureDragAngle );
lua_register( L, "RL_GetGesturePinchVector", lcoreGetGesturePinchVector );
lua_register( L, "RL_GetGesturePinchAngle", lcoreGetGesturePinchAngle );
/* Screen-space. */
lua_register( L, "RL_GetMouseRay", lcoreGetMouseRay );
lua_register( L, "RL_GetCameraMatrix", lcoreGetCameraMatrix );
lua_register( L, "RL_GetWorldToScreen", lcoreGetWorldToScreen );
/* Shapes. */
/* Drawing. */
@@ -1304,6 +1308,28 @@ void uluaPushMatrix( lua_State *L, Matrix matrix ) {
lua_rawseti( L, -2, 4 );
}
void uluaPushRay( lua_State *L, Ray ray ) {
lua_createtable( L, 2, 0 );
lua_createtable( L, 3, 0 );
lua_pushnumber( L, ray.position.x );
lua_rawseti( L, -2, 1 );
lua_pushnumber( L, ray.position.y );
lua_rawseti( L, -2, 2 );
lua_pushnumber( L, ray.position.z );
lua_rawseti( L, -2, 3 );
lua_rawseti( L, -2, 1 );
lua_createtable( L, 3, 0 );
lua_pushnumber( L, ray.direction.x );
lua_rawseti( L, -2, 1 );
lua_pushnumber( L, ray.direction.y );
lua_rawseti( L, -2, 2 );
lua_pushnumber( L, ray.direction.z );
lua_rawseti( L, -2, 3 );
lua_rawseti( L, -2, 2 );
}
void uluaPushRayCollision( lua_State *L, RayCollision rayCol ) {
lua_createtable( L, 4, 0 );
lua_pushboolean( L, rayCol.hit );