diff options
| author | jussi | 2022-03-31 13:55:02 +0300 |
|---|---|---|
| committer | jussi | 2022-03-31 13:55:02 +0300 |
| commit | 30d425aa26a3aa802cb2ff55f1f7655be056f3ed (patch) | |
| tree | 5d6e0e9a0d6c14df15f3e1ac8ab143dd9a8452c4 | |
| parent | a3c28c00016fb2a2bd13b36e7269c69b0529c6a4 (diff) | |
| download | reilua-enhanced-30d425aa26a3aa802cb2ff55f1f7655be056f3ed.tar.gz reilua-enhanced-30d425aa26a3aa802cb2ff55f1f7655be056f3ed.tar.bz2 reilua-enhanced-30d425aa26a3aa802cb2ff55f1f7655be056f3ed.zip | |
Screen-space related functions for 3DCamera.
| -rw-r--r-- | API.md | 33 | ||||
| -rw-r--r-- | doc_parser.lua | 2 | ||||
| -rw-r--r-- | examples/ray/main.lua | 24 | ||||
| -rw-r--r-- | include/core.h | 4 | ||||
| -rw-r--r-- | include/lua_core.h | 1 | ||||
| -rw-r--r-- | src/core.c | 83 | ||||
| -rw-r--r-- | src/lua_core.c | 26 |
7 files changed, 164 insertions, 9 deletions
@@ -667,7 +667,7 @@ int id. Basic 3d Model type --- -> RayCollision = { hit = true, distance = 1.0, point = { 0.0, 0.0 }, normal = { 0.0, 0.0, 1.0 } } +> RayCollision = { hit = true, distance = 1.0, point = { 0.0, 0.0, 0.0 }, normal = { 0.0, 0.0, 1.0 } } Raycast hit information. NOTE: Data in named keys @@ -1721,6 +1721,37 @@ Update camera position for selected mode --- +## Core - Screen-space + +--- + +> ray = RL_GetMouseRay( Vector2 mousePosition, Camera3D camera ) + +Get a ray trace from mouse position + +- Failure return false +- Success return Ray + +--- + +> matrix = RL_GetCameraMatrix( Camera3D camera ) + +Get camera transform matrix ( view matrix ) + +- Failure return false +- Success return Matrix + +--- + +> position = RL_GetWorldToScreen( Vector3 position, Camera3D camera ) + +Get the screen space position for a 3d world space position + +- Failure return false +- Success return Vector2 + +--- + ## Shapes - Drawing --- diff --git a/doc_parser.lua b/doc_parser.lua index 8100f49..18fed00 100644 --- a/doc_parser.lua +++ b/doc_parser.lua @@ -119,7 +119,7 @@ apiFile:write( "\n> Model = ModelId\n\ int id. Basic 3d Model type\n\n---\n" ) apiFile:write( "\n> Ray = { { 0.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0 } }\n\ { position, direction }. Ray type (useful for raycast)\n\n---\n" ) -apiFile:write( "\n> RayCollision = { hit = true, distance = 1.0, point = { 0.0, 0.0 }, normal = { 0.0, 0.0, 1.0 } }\n\ +apiFile:write( "\n> RayCollision = { hit = true, distance = 1.0, point = { 0.0, 0.0, 0.0 }, normal = { 0.0, 0.0, 1.0 } }\n\ Raycast hit information. NOTE: Data in named keys\n\n---\n" ) apiFile:write( "\n> BoundingBox = { { 0.0, 0.0, 0.0 }, { 1.0, 1.0, 1.0 } }\n\ { min, max }. Bounding box type for 3d mesh\n\n---\n" ) diff --git a/examples/ray/main.lua b/examples/ray/main.lua index f0f5c2f..43374e2 100644 --- a/examples/ray/main.lua +++ b/examples/ray/main.lua @@ -12,6 +12,17 @@ local function setupWindow() RL_SetWindowPosition( { mPos[1] + mSize[1] / 2 - winSize[1] / 2, mPos[2] + mSize[2] / 2 - winSize[2] / 2 } ) end +function ray_collision() + local rayCol = RL_GetRayCollisionMesh( ray, sphereMesh, RL_MatrixIdentity() ) + + if rayCol ~= nil and rayCol.hit then + print( "hit", rayCol.hit ) + print( "distance", rayCol.distance ) + print( "point", rayCol.point[1], rayCol.point[2], rayCol.point[3] ) + print( "normal", rayCol.normal[1], rayCol.normal[2], rayCol.normal[3] ) + end +end + function init() setupWindow() @@ -23,14 +34,13 @@ function init() sphereMesh = RL_GenMeshSphere( 1.0, 8, 10 ) - -- local rayCol = RL_GetRayCollisionSphere( { { 0.5, 0, 4 }, { 0, 0, -1 } }, { 0, 0, 0 }, 1.0 ) - local rayCol = RL_GetRayCollisionMesh( ray, sphereMesh, RL_MatrixIdentity() ) + ray_collision() +end - if rayCol ~= nil and rayCol.hit then - print( "hit", rayCol.hit ) - print( "distance", rayCol.distance ) - print( "point", rayCol.point[1], rayCol.point[2], rayCol.point[3] ) - print( "normal", rayCol.normal[1], rayCol.normal[2], rayCol.normal[3] ) +function process( delta ) + if RL_IsMouseButtonPressed( 0 ) then + ray = RL_GetMouseRay( RL_GetMousePosition(), camera ) + ray_collision() end end diff --git a/include/core.h b/include/core.h index c443542..1f0691c 100644 --- a/include/core.h +++ b/include/core.h @@ -128,3 +128,7 @@ int lcoreGetGestureDragVector( lua_State *L ); int lcoreGetGestureDragAngle( lua_State *L ); int lcoreGetGesturePinchVector( lua_State *L ); int lcoreGetGesturePinchAngle( lua_State *L ); +/* Screen-space. */ +int lcoreGetMouseRay( lua_State *L ); +int lcoreGetCameraMatrix( lua_State *L ); +int lcoreGetWorldToScreen( lua_State *L ); diff --git a/include/lua_core.h b/include/lua_core.h index 694b94d..335ee49 100644 --- a/include/lua_core.h +++ b/include/lua_core.h @@ -24,6 +24,7 @@ void uluaPushVector3( lua_State *L, Vector3 vector ); void uluaPushVector4( lua_State *L, Vector4 vector ); void uluaPushRectangle( lua_State *L, Rectangle rect ); void uluaPushMatrix( lua_State *L, Matrix matrix ); +void uluaPushRay( lua_State *L, Ray ray ); void uluaPushRayCollision( lua_State *L, RayCollision rayCol ); int uluaGetTableLen( lua_State *L ); @@ -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; +} diff --git a/src/lua_core.c b/src/lua_core.c index 4cfbafd..6006474 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -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 ); |
