From b011b2ca4e161fea2a742cb9b1673cd84cf0eba5 Mon Sep 17 00:00:00 2001 From: jussi Date: Mon, 5 Aug 2024 23:35:57 +0300 Subject: GetRayBoxCells fix. --- README.md | 2 + changelog | 1 + devnotes | 8 ++-- examples/ray_box_cells/main.lua | 10 ++--- examples/resources/lib/bounding_box.lua | 72 ++++++++++++++++++++++-------- examples/resources/lib/color.lua | 12 ++--- examples/resources/lib/raygui.lua | 2 +- examples/resources/lib/vector2.lua | 10 +++++ examples/resources/lib/vector3.lua | 12 ++++- examples/resources/shaders/glsl330/base.fs | 7 ++- examples/resources/shaders/glsl330/base.vs | 5 +-- include/state.h | 6 +-- src/lua_core.c | 1 + src/models.c | 27 ++++++----- src/platforms/core_desktop.c | 9 ++-- src/textures.c | 2 +- 16 files changed, 126 insertions(+), 60 deletions(-) diff --git a/README.md b/README.md index 8c054ba..d0ed567 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,8 @@ https://github.com/raysan5/raylib https://github.com/lua/lua or https://github.com/LuaJIT/LuaJIT +Note! Lua header files are from Lua 5.4.0, if you use different version be sure to replace them. + ### Linux Compile Raylib and lua by following their instructions. They will compile to libraylib.a and liblua.a by default. diff --git a/changelog b/changelog index 3de5a02..c880fbd 100644 --- a/changelog +++ b/changelog @@ -67,6 +67,7 @@ DETAILED CHANGES: - ADDED: Some raygui controls return 1 instead of 0 when pressed or scrolled. - ADDED: DrawGridEx. - ADDED: GetRayBoxCells. + - FIXED: GenImageColor color was also argument 1. ------------------------------------------------------------------------ Release: ReiLua version 0.7.0 Using Raylib 5.0 and Forked Raygui 4.0 diff --git a/devnotes b/devnotes index 5fb5536..5802cb0 100644 --- a/devnotes +++ b/devnotes @@ -19,15 +19,17 @@ Backlog { * Improve Dungeon crawler example by generating custom mesh instead of drawing 3D quads. * Platformer example physics update for true framerate independence. * Android support - - * For raylib 5.5 - * DrawBillboardPro BREAKING CHANGE. } Bugs { * glfwSet*Callback functions segfault on Windows. Should keep Lua events off for now. } +Notes { + * raylib 5.5 + * DrawBillboardPro BREAKING CHANGE. +} + Needs Testing { * rlSetUniform * rlSetShader diff --git a/examples/ray_box_cells/main.lua b/examples/ray_box_cells/main.lua index 77aa058..64e0db2 100644 --- a/examples/ray_box_cells/main.lua +++ b/examples/ray_box_cells/main.lua @@ -10,7 +10,8 @@ Cam3D = require( "camera3d" ) local monitor = 0 local camera = {} -local box = BoundinBox:new( { -8, 0, -8 }, { 16, 16, 16 } ) +-- local box = BoundinBox:new( { -8, 0, -8 }, { 16, 16, 16 } ) +local box = BoundinBox:new( { -7, 1, -8 }, { 4, 1, 7 } ) local cellSize = Vector3:new( 1, 1, 1 ) local drawCellSize = cellSize:clone() local ray = nil @@ -60,10 +61,9 @@ function RL.update( delta ) -- Raycast. - -- if not guiMouseHover then if not guiMouseHover and RL.IsMouseButtonPressed( RL.MOUSE_BUTTON_LEFT ) then ray = RL.GetMouseRay( RL.GetMousePosition(), camera.camera ) - rayCol = box:getRayCollision( ray ) + rayCol = box:getRayCollisionMAS( ray ) cells, exitPoint = RL.GetRayBoxCells( ray, box:maxToPos(), cellSize ) drawCellSize:setV( cellSize ) @@ -99,8 +99,8 @@ function RL.draw() if rayCol and rayCol.hit then RL.DrawSphere( rayCol.point, 0.1, RL.RED ) end - if exitPoint.hit then - RL.DrawSphere( exitPoint.point, 0.1, RL.RED ) + if exitPoint and exitPoint.hit then + RL.DrawSphere( exitPoint.point, 0.1, RL.YELLOW ) end if cells then for _, cell in ipairs( cells ) do diff --git a/examples/resources/lib/bounding_box.lua b/examples/resources/lib/bounding_box.lua index 3cf6787..ad3b45a 100644 --- a/examples/resources/lib/bounding_box.lua +++ b/examples/resources/lib/bounding_box.lua @@ -3,7 +3,7 @@ if table.unpack == nil then table.unpack = unpack end -local Vector3 = require( "vector3" ) +local Vector3 = Vector3 or require( "vector3" ) local BoundingBox = {} local metatable = { @@ -125,41 +125,35 @@ end function BoundingBox:getPoints() return { self.min:clone(), -- Down back left. - Vector3:new( self.max.x, self.min.y, self.min.z ), -- Down back right. - Vector3:new( self.min.x, self.min.y, self.max.z ), -- Down front left. - Vector3:new( self.max.x, self.min.y, self.max.z ), -- Down front right. - Vector3:new( self.min.x, self.max.y, self.min.z ), -- Up back left. - Vector3:new( self.max.x, self.max.y, self.min.z ), -- Up back right. - Vector3:new( self.min.x, self.max.y, self.max.z ), -- Up front left. + Vector3:new( self.max.x, self.min.y, self.min.z ), -- Down back right. + Vector3:new( self.min.x, self.min.y, self.max.z ), -- Down front left. + Vector3:new( self.max.x, self.min.y, self.max.z ), -- Down front right. + Vector3:new( self.min.x, self.max.y, self.min.z ), -- Up back left. + Vector3:new( self.max.x, self.max.y, self.min.z ), -- Up back right. + Vector3:new( self.min.x, self.max.y, self.max.z ), -- Up front left. self.max:clone(), -- Up front right. } end --- Assumes max is used as size. function BoundingBox:checkCollisionBox( b ) - return RL.CheckCollisionBoxes( self:maxToPos(), b:maxToPos() ) + return RL.CheckCollisionBoxes( self, b ) end --- Assumes max is used as size. function BoundingBox:checkCollisionSphere( center, radius ) - return RL.CheckCollisionBoxSphere( self:maxToPos(), center, radius ) + return RL.CheckCollisionBoxSphere( self, center, radius ) end --- Assumes max is used as size. function BoundingBox:checkCollisionPoint( point ) - local max = self.min + self.max - return self.min.x <= point.x and self.min.y <= point.y and self.min.z <= point.z - and point.x <= max.x - and point.y <= max.y - and point.z <= max.z + and point.x <= self.max.x + and point.y <= self.max.y + and point.z <= self.max.z end --- Assumes max is used as size. function BoundingBox:getRayCollision( ray ) - return RL.GetRayCollisionBox( ray, self:maxToPos() ) + return RL.GetRayCollisionBox( ray, self ) end -- Max to position from size. @@ -172,6 +166,36 @@ function BoundingBox:maxToSize() return BoundingBox:newV( self.min, self.max - self.min ) end +-- MAS stands for max as size. These functions handles max as size instead of position. + +function BoundingBox:checkCollisionBoxMASBox( b ) + return RL.CheckCollisionBoxes( self:tempMaxToPos(), b ) +end + +function BoundingBox:checkCollisionBoxMAS( b ) + return RL.CheckCollisionBoxes( self:tempMaxToPos(), b:tempMaxToPos() ) +end + +function BoundingBox:checkCollisionSphereMAS( center, radius ) + return RL.CheckCollisionBoxSphere( self:tempMaxToPos(), center, radius ) +end + +function BoundingBox:checkCollisionPointMAS( point ) + local max = self.min + self.max + + return self.min.x <= point.x + and self.min.y <= point.y + and self.min.z <= point.z + and point.x <= max.x + and point.y <= max.y + and point.z <= max.z +end + +function BoundingBox:getRayCollisionMAS( ray ) + return RL.GetRayCollisionBox( ray, self:tempMaxToPos() ) + -- return RL.GetRayCollisionBox( ray, self:maxToPos() ) +end + -- Temp pre generated objects to avoid "slow" table generation. local TEMP_COUNT = 100 @@ -230,6 +254,16 @@ function BoundingBox:tempB( b ) return object end +-- Max to position from size. +function BoundingBox:tempMaxToPos() + return BoundingBox:tempV( self.min, Vector3:tempT( RL.Vector3Add( self.min, self.max ) ) ) +end + +-- Max to size from position. +function BoundingBox:tempMaxToSize() + return BoundingBox:tempV( self.min, Vector3:tempT( RL.Vector3Subtract( self.max, self.min ) ) ) +end + function BoundingBox:getTempId() return curTemp end diff --git a/examples/resources/lib/color.lua b/examples/resources/lib/color.lua index 8ca3920..e16837b 100644 --- a/examples/resources/lib/color.lua +++ b/examples/resources/lib/color.lua @@ -3,8 +3,6 @@ if table.unpack == nil then table.unpack = unpack end -local Vector3 = require( "vector3" ) - local Color = {} local metatable = { __index = Color, @@ -92,7 +90,7 @@ function Color:setC( c ) end function Color:serialize() - return "Color:new("..self.r..","..self.g..","..self.b..","..self.a..")" + return "Color:new("..RL.Round( self.r )..","..RL.Round( self.g )..","..RL.Round( self.b )..","..RL.Round( self.a )..")" end function Color:arr() @@ -156,12 +154,12 @@ function Color:alphaBlend( dst, src, tint ) end function Color:lerp( color, amount ) - return Color:new( + return Color:temp( RL.Lerp( self.r, color.r, amount ), RL.Lerp( self.g, color.g, amount ), RL.Lerp( self.b, color.b, amount ), RL.Lerp( self.a, color.a, amount ) - ) + ):round() end function Color:round() @@ -173,6 +171,10 @@ function Color:round() ) end +function Color:invert() + return Color:new( 255 - self.r, 255 - self.g, 255 - self.b, self.a ) +end + -- Temp pre generated objects to avoid "slow" table generation. local TEMP_COUNT = 100 diff --git a/examples/resources/lib/raygui.lua b/examples/resources/lib/raygui.lua index 3caba0d..984b1ee 100644 --- a/examples/resources/lib/raygui.lua +++ b/examples/resources/lib/raygui.lua @@ -1729,7 +1729,7 @@ function Raygui:update() for i = #self.controls, 1, -1 do local control = self.controls[i] - if control.visible and control.update ~= nil and self:inView( control ) then + if control.visible and not control.noUpdate and control.update ~= nil and self:inView( control ) then if control:update() then self.focused = i diff --git a/examples/resources/lib/vector2.lua b/examples/resources/lib/vector2.lua index 8739ced..6da1977 100644 --- a/examples/resources/lib/vector2.lua +++ b/examples/resources/lib/vector2.lua @@ -36,6 +36,12 @@ local metatable = { __eq = function( v1, v2 ) return RL.Vector2Equals( v1, v2 ) end, + __lt = function( v1, v2 ) + return v1.x < v2.x and v1.y < v2.y + end, + __le = function( v1, v2 ) + return v1.x <= v2.x and v1.y <= v2.y + end, __concat = function( a, b ) return tostring( a )..tostring( b ) end, @@ -205,6 +211,10 @@ function Vector2:equals( v2 ) return RL.Vector2Equals( self, v2 ) end +function Vector2:sign() + return Vector2:new( RL.Sign( self.x ), RL.Sign( self.y ) ) +end + function Vector2:addEq( v2 ) self.x = self.x + v2.x self.y = self.y + v2.y diff --git a/examples/resources/lib/vector3.lua b/examples/resources/lib/vector3.lua index bfecb07..7b339f6 100644 --- a/examples/resources/lib/vector3.lua +++ b/examples/resources/lib/vector3.lua @@ -3,8 +3,6 @@ if table.unpack == nil then table.unpack = unpack end -local Vector2 = Vector2 or require( "vector2" ) - local Vector3 = {} local metatable = { __index = Vector3, @@ -38,6 +36,12 @@ local metatable = { __eq = function( v1, v2 ) return RL.Vector3Equals( v1, v2 ) end, + __lt = function( v1, v2 ) + return v1.x < v2.x and v1.y < v2.y and v1.z < v2.z + end, + __le = function( v1, v2 ) + return v1.x <= v2.x and v1.y <= v2.y and v1.z <= v2.z + end, __concat = function( a, b ) return tostring( a )..tostring( b ) end, @@ -232,6 +236,10 @@ function Vector3:equals( v2 ) return RL.Vector3Equals( self, v2 ) end +function Vector3:sign() + return Vector3:new( RL.Sign( self.x ), RL.Sign( self.y ), RL.Sign( self.z ) ) +end + function Vector3:addEq( v2 ) self.x = self.x + v2.x self.y = self.y + v2.y diff --git a/examples/resources/shaders/glsl330/base.fs b/examples/resources/shaders/glsl330/base.fs index 6b50062..57d9725 100644 --- a/examples/resources/shaders/glsl330/base.fs +++ b/examples/resources/shaders/glsl330/base.fs @@ -13,13 +13,12 @@ out vec4 finalColor; // NOTE: Add here your custom variables -void main() -{ +void main() { // Texel color fetching from texture sampler - vec4 texelColor = texture(texture0, fragTexCoord); + vec4 texelColor = texture( texture0, fragTexCoord ); // NOTE: Implement here your fragment shader code - finalColor = texelColor*colDiffuse; + finalColor = texelColor * colDiffuse; } diff --git a/examples/resources/shaders/glsl330/base.vs b/examples/resources/shaders/glsl330/base.vs index 8cc2abb..04f50db 100644 --- a/examples/resources/shaders/glsl330/base.vs +++ b/examples/resources/shaders/glsl330/base.vs @@ -15,12 +15,11 @@ out vec4 fragColor; // NOTE: Add here your custom variables -void main() -{ +void main() { // Send vertex attributes to fragment shader fragTexCoord = vertexTexCoord; fragColor = vertexColor; // Calculate final vertex position - gl_Position = mvp*vec4(vertexPosition, 1.0); + gl_Position = mvp * vec4( vertexPosition, 1.0 ); } \ No newline at end of file diff --git a/include/state.h b/include/state.h index 8168535..309a284 100644 --- a/include/state.h +++ b/include/state.h @@ -37,9 +37,9 @@ typedef struct { GLFWcursorenterfun raylibCursorEnterCallback; GLFWjoystickfun raylibJoystickCallback; /* NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ - GLFWpentabletdatafun glfwtabletDataCallback; - GLFWpentabletcursorfun glfwtabletCursorCallback; - GLFWpentabletproximityfun glfwtabletProximityCallback; + GLFWpentabletdatafun glfwTabletDataCallback; + GLFWpentabletcursorfun glfwTabletCursorCallback; + GLFWpentabletproximityfun glfwTabletProximityCallback; #elif PLATFORM_DESKTOP_SDL int SDL_eventQueueLen; SDL_Event* SDL_eventQueue; diff --git a/src/lua_core.c b/src/lua_core.c index 8071a0f..41796ef 100644 --- a/src/lua_core.c +++ b/src/lua_core.c @@ -755,6 +755,7 @@ static void defineGlobals() { assignGlobalColor( RAYWHITE, "RAYWHITE" ); // My own White (raylib logo) /* Math */ assignGlobalFloat( PI, "PI" ); // Pi + assignGlobalFloat( EPSILON, "EPSILON" ); // Epsilon assignGlobalFloat( DEG2RAD, "DEG2RAD" ); // Degrees to radians assignGlobalFloat( RAD2DEG, "RAD2DEG" ); // Radians to degrees /* Gui control state */ diff --git a/src/models.c b/src/models.c index d33e130..24474ab 100644 --- a/src/models.c +++ b/src/models.c @@ -2516,7 +2516,7 @@ int lmodelsGetRayCollisionTriangle( lua_State* L ) { /* > rayCollision = RL.GetRayCollisionQuad( Ray ray, Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4 ) -Get collision info between ray and quad +Get collision info between ray and quad. NOTE: The points are expected to be in counter-clockwise winding - Success return RayCollision */ @@ -2599,11 +2599,6 @@ int lmodelsGetRayBoxCells( lua_State* L ) { 0.0f <= ray.direction.z ? 1.0f : -1.0f }; /* We transform everything to absolute space to make this simpler. */ - Vector3 absBounds = { - 0.0f < signs.x ? boxSizeCells.x - cellPos.x : cellPos.x + 1.0f, - 0.0f < signs.y ? boxSizeCells.y - cellPos.y : cellPos.y + 1.0f, - 0.0f < signs.z ? boxSizeCells.z - cellPos.z : cellPos.z + 1.0f - }; Vector3 absDir = { fabsf( ray.direction.x ), fabsf( ray.direction.y ), @@ -2614,6 +2609,21 @@ int lmodelsGetRayBoxCells( lua_State* L ) { 0.0f < signs.y ? localRayPos.y - cellPos.y * cellSize.y : cellSize.y - ( localRayPos.y - cellPos.y * cellSize.y ), 0.0f < signs.z ? localRayPos.z - cellPos.z * cellSize.z : cellSize.z - ( localRayPos.z - cellPos.z * cellSize.z ) }; + Vector3 absBounds = { + 0.0f < signs.x ? boxSize.x - localRayPos.x : localRayPos.x, + 0.0f < signs.y ? boxSize.y - localRayPos.y : localRayPos.y, + 0.0f < signs.z ? boxSize.z - localRayPos.z : localRayPos.z + }; + absBounds = Vector3Add( absBounds, absPos ); + + Vector3 exitDis = { + ( absBounds.x - absPos.x ) / absDir.x, + ( absBounds.y - absPos.y ) / absDir.y, + ( absBounds.z - absPos.z ) / absDir.z + }; + float exitScale = fmin( fmin( exitDis.x, exitDis.y ), exitDis.z ); + Vector3 exitPoint = Vector3Add( Vector3Scale( ray.direction, exitScale ), Vector3Add( localRayPos, box.min ) ); + Vector3 absCell = { 0, 0, 0 }; int cellId = 2; /* We already added first so we will start at 2. */ @@ -2636,17 +2646,14 @@ int lmodelsGetRayBoxCells( lua_State* L ) { float rayMoveScale = fmin( fmin( cellDis.x, cellDis.y ), cellDis.z ); absPos = Vector3Add( absPos, Vector3Scale( absDir, rayMoveScale ) ); - localRayPos = Vector3Add( localRayPos, Vector3Scale( ray.direction, rayMoveScale ) ); - if ( absCell.x < absBounds.x && absCell.y < absBounds.y && absCell.z < absBounds.z ) { + if ( absPos.x < absBounds.x && absPos.y < absBounds.y && absPos.z < absBounds.z ) { uluaPushVector3( L, (Vector3){ round( cellPos.x ), round( cellPos.y ), round( cellPos.z ) } ); lua_rawseti( L, -2, cellId ); cellId++; } else { - Vector3 exitPoint = Vector3Add( localRayPos, box.min ); - uluaPushRayCollision( L, (RayCollision){ .hit = true, .distance = Vector3Distance( ray.position, exitPoint ), diff --git a/src/platforms/core_desktop.c b/src/platforms/core_desktop.c index 6d774ee..8d9f01c 100644 --- a/src/platforms/core_desktop.c +++ b/src/platforms/core_desktop.c @@ -99,6 +99,7 @@ int lcoreGetKeyScancode( lua_State* L ) { Called when the window is resized. Type GLFW_WINDOW_SIZE_EVENT */ static void windowSizeEvent( GLFWwindow* window, int width, int height ) { +// GLFWwindowsizefun windowSizeEvent( GLFWwindow* window, int width, int height ) { /* Pass through to raylib callback. */ state->raylibWindowSizeCallback( window, width, height ); @@ -617,7 +618,7 @@ static void penTabletProximityEvent( int proxState ) { static void platformRegisterEvents() { /* Window events. */ - state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), windowSizeEvent ); + state->raylibWindowSizeCallback = glfwSetWindowSizeCallback( GetWindowHandle(), (GLFWwindowsizefun)windowSizeEvent ); #if !defined( PLATFORM_WEB ) state->raylibWindowMaximizeCallback = glfwSetWindowMaximizeCallback( GetWindowHandle(), windowMaximizeEvent ); #endif @@ -634,9 +635,9 @@ static void platformRegisterEvents() { state->raylibCursorEnterCallback = glfwSetCursorEnterCallback( GetWindowHandle(), cursorEnterInputEvent ); state->raylibJoystickCallback = glfwSetJoystickCallback( joystickEvent ); /* NOTE! Experimental. Needs glfw PR https://github.com/glfw/glfw/pull/1445 */ - // state->glfwtabletDataCallback = glfwSetPenTabletDataCallback( penTabletDataEvent ); - // state->glfwtabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent ); - // state->glfwtabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent ); + // state->glfwTabletDataCallback = glfwSetPenTabletDataCallback( penTabletDataEvent ); + // state->glfwTabletCursorCallback = glfwSetPenTabletCursorCallback( penTabletCursorEvent ); + // state->glfwTabletProximityCallback = glfwSetPenTabletProximityCallback( penTabletProximityEvent ); } void luaPlatformRegister() { diff --git a/src/textures.c b/src/textures.c index 9d7221a..467485b 100644 --- a/src/textures.c +++ b/src/textures.c @@ -255,7 +255,7 @@ Generate image: plain color */ int ltexturesGenImageColor( lua_State* L ) { Vector2 size = uluaGetVector2( L, 1 ); - Color color = uluaGetColor( L, 1 ); + Color color = uluaGetColor( L, 2 ); uluaPushImage( L, GenImageColor( size.x, size.y, color ) ); -- cgit v1.2.3