From eba062f75000febf28eb732210ddf42a6b066270 Mon Sep 17 00:00:00 2001 From: Nelonn Date: Sun, 10 Dec 2023 16:22:25 +0300 Subject: [PATCH 1/3] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BB=20=D1=84=D0=B0=D0=B9=D0=BB=20=D1=81=D0=B1=D0=BE?= =?UTF-8?q?=D1=80=D0=BA=D0=B8=20=D0=B8=20=D0=BF=D0=BE=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BB=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BE=D0=BA=D0=BD=D0=B0=20=D0=B4=D0=BB=D1=8F=20macOS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ CMakeLists.txt | 13 +++++++++---- README.md | 8 +++++++- src/window/Window.cpp | 7 ++++++- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 55057c5a..aa5d689a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ Debug/src/**/*.o Debug/voxel_engine /build +/cmake-build-** /screenshots /world @@ -11,6 +12,7 @@ Debug/voxel_engine /settings.toml /controls.json +/.idea .vscode /.vs *.sln diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bef1499..c8429711 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,12 @@ cmake_minimum_required(VERSION 3.15) project(VoxelEngine) +set(CMAKE_CXX_STANDARD 17) + file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.hpp) file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp) add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES}) -target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17) if(MSVC) target_compile_options(${PROJECT_NAME} PRIVATE /W4) @@ -13,7 +14,7 @@ if(MSVC) else() target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -lstdc++fs # additional warnings - -Wformat-nonliteral -Wcast-align + -Wformat-nonliteral -Wcast-align -Wpointer-arith -Wundef -Wwrite-strings -Wno-unused-parameter) endif() @@ -28,14 +29,18 @@ if (WIN32) else() find_package(PNG REQUIRED) set(PNGLIB PNG::PNG) -endif(WIN32) +endif() + +if (APPLE) + find_package(glfw3 3.3 REQUIRED) +endif () set(LIBS "") if(UNIX) find_package(Threads REQUIRED) set(LIBS ${LIBS} Threads::Threads) -endif(UNIX) +endif() if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie") diff --git a/README.md b/README.md index 1f5d51f3..066b8eea 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ git clone --recursive https://github.com/MihailRis/VoxelEngine-Cpp.git cd VoxelEngine-Cpp mkdir build cd build -cmake ../ +cmake .. cmake --build . ``` @@ -39,3 +39,9 @@ If you use X11 If you use Wayland `$ sudo pacman -S glfw-wayland glew glm libpng openal` + +#### macOS: + +`$ brew install glfw3 glew glm libpng` + +Download, compile and install OpenAL diff --git a/src/window/Window.cpp b/src/window/Window.cpp index 6a4f72c6..d6b0eee3 100644 --- a/src/window/Window.cpp +++ b/src/window/Window.cpp @@ -132,7 +132,12 @@ int Window::initialize(DisplaySettings& settings){ glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE); +#ifdef __APPLE__ + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); +#else + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE); +#endif glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); glfwWindowHint(GLFW_SAMPLES, settings.samples); From 1e39b5d05dab0f7718509a4a6970519c2acbc037 Mon Sep 17 00:00:00 2001 From: A-lex-Ra Date: Sun, 10 Dec 2023 20:06:18 +0600 Subject: [PATCH 2/3] rays fixed --- .gitignore | 1 + src/maths/rays.cpp | 351 ++++++++++++++++++------------------------ src/maths/rays.h | 70 +++++---- src/voxels/Chunks.cpp | 5 +- 4 files changed, 192 insertions(+), 235 deletions(-) diff --git a/.gitignore b/.gitignore index 55057c5a..cee55439 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ Debug/voxel_engine /build /screenshots +/out /world /worlds/**/* diff --git a/src/maths/rays.cpp b/src/maths/rays.cpp index 450fdf75..3f9c5cc8 100644 --- a/src/maths/rays.cpp +++ b/src/maths/rays.cpp @@ -34,33 +34,28 @@ RayRelation Rays::rayIntersectAAFace( const rayvec3& rayDir, const rayvec3& faceMin, const rayvec2& faceOppositeCorner, //y and z global coords of opposite corner - rayvec3& intersectPoint_ret + glm::ivec3& normal_ret, + scalar_t& distance_ret //sinonym of rayCoef ){ if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision return RayRelation::Parallel; } - glm::float64 rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x); - intersectPoint_ret = {faceMin.x, - rayCoef*rayDir.y + rayOrigin.y, + scalar_t rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x);// equivalent to distance if raydir normalized + if (rayCoef < 0) return RayRelation::None; + rayvec3 intersectPoint = {faceMin.x, + rayCoef*rayDir.y + rayOrigin.y, rayCoef*rayDir.z + rayOrigin.z}; - - if (rayDir.x > 0){ - if (intersectPoint_ret.y >= faceMin.y - && intersectPoint_ret.y <= faceOppositeCorner[0] - && intersectPoint_ret.z >= faceMin.z - && intersectPoint_ret.z <= faceOppositeCorner[1]){ - return RayRelation::Intersect; - } - } - else{ - if (intersectPoint_ret.y <= faceMin.y - && intersectPoint_ret.y >= faceOppositeCorner[0] - && intersectPoint_ret.z <= faceMin.z - && intersectPoint_ret.z >= faceOppositeCorner[1]){ - return RayRelation::Intersect; - } - } + + if (intersectPoint.y >= faceMin.y + && intersectPoint.y <= faceOppositeCorner[0] + && intersectPoint.z >= faceMin.z + && intersectPoint.z <= faceOppositeCorner[1]){ + distance_ret = rayCoef; // believe that raydir normalized + if (rayDir.x > 0) normal_ret = -X_AXIS; + else normal_ret = X_AXIS; + return RayRelation::Intersect; + } return RayRelation::None; } @@ -70,33 +65,28 @@ RayRelation Rays::rayIntersectAAFace( const rayvec3& rayDir, const rayvec3& faceMin, const rayvec2& faceOppositeCorner, //x and z global coords of opposite corner - rayvec3& intersectPoint_ret + glm::ivec3& normal_ret, + scalar_t& distance_ret ){ if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision return RayRelation::Parallel; } - glm::float64 rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y); - intersectPoint_ret = {rayCoef*rayDir.x + rayOrigin.x, + scalar_t rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y);// equivalent to distance if raydir normalized + if (rayCoef < 0) return RayRelation::None; + rayvec3 intersectPoint = { rayCoef *rayDir.x + rayOrigin.x, faceMin.y, rayCoef*rayDir.z + rayOrigin.z}; - - if (rayDir.y > 0){ - if (intersectPoint_ret.x >= faceMin.x //Face-hit check - && intersectPoint_ret.x <= faceOppositeCorner[0] - && intersectPoint_ret.z >= faceMin.z - && intersectPoint_ret.z <= faceOppositeCorner[1] ){ - return RayRelation::Intersect; - } - } - else{ - if (intersectPoint_ret.x <= faceMin.x //Face-hit check for negative dir. - && intersectPoint_ret.x >= faceOppositeCorner[0] - && intersectPoint_ret.z <= faceMin.z - && intersectPoint_ret.z >= faceOppositeCorner[1]){ - return RayRelation::Intersect; - } - } + + if (intersectPoint.x >= faceMin.x //Face-hit check + && intersectPoint.x <= faceOppositeCorner[0] + && intersectPoint.z >= faceMin.z + && intersectPoint.z <= faceOppositeCorner[1] ){ + distance_ret = rayCoef; // believe that raydir normalized + if (rayDir.y > 0) normal_ret = -Y_AXIS; + else normal_ret = Y_AXIS; + return RayRelation::Intersect; + } return RayRelation::None; } @@ -106,162 +96,105 @@ RayRelation Rays::rayIntersectAAFace( const rayvec3& rayDir, const rayvec3& faceMin, const rayvec2& faceOppositeCorner, //x and y global coords of opposite corner - rayvec3& intersectPoint_ret + glm::ivec3& normal_ret, + scalar_t& distance_ret ){ if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision return RayRelation::Parallel; } - glm::float64 rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z); - intersectPoint_ret = {rayCoef*rayDir.x + rayOrigin.x, + scalar_t rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z); // equivalent to distance if raydir normalized + if (rayCoef < 0) return RayRelation::None; + rayvec3 intersectPoint = { rayCoef *rayDir.x + rayOrigin.x, rayCoef*rayDir.y + rayOrigin.y, faceMin.z}; - - if (rayDir.z > 0){ - if (intersectPoint_ret.x >= faceMin.x //Face-hit check - && intersectPoint_ret.x <= faceOppositeCorner[0] - && intersectPoint_ret.y >= faceMin.y - && intersectPoint_ret.y <= faceOppositeCorner[1] ){ - return RayRelation::Intersect; - } - } - else{ - if (intersectPoint_ret.x <= faceMin.x //Face-hit check - && intersectPoint_ret.x >= faceOppositeCorner[0] - && intersectPoint_ret.y <= faceMin.y - && intersectPoint_ret.y >= faceOppositeCorner[1] ){ - return RayRelation::Intersect; - } + + if (intersectPoint.x >= faceMin.x //Face-hit check + && intersectPoint.x <= faceOppositeCorner[0] + && intersectPoint.y >= faceMin.y + && intersectPoint.y <= faceOppositeCorner[1] ){ + distance_ret = rayCoef; // believe that raydir normalized + if (rayDir.z > 0) normal_ret = -Z_AXIS; + else normal_ret = Z_AXIS; + return RayRelation::Intersect; } return RayRelation::None; } -double Rays::updateNormal( - double newDistApprox, - const glm::ivec3& newNormal, - double currentDistApprox, - glm::ivec3& normal_ret - ){ - if (newDistApprox < currentDistApprox){ - currentDistApprox = newDistApprox; - normal_ret = newNormal; - } - return currentDistApprox; - } - template <> RayRelation Rays::isRayIntersectsAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, - const rayvec3& faceMin, - const rayvec2& faceOppositeCorner, - glm::ivec3& normal_ret, - double& currentDistApprox + const rayvec3& rayOrigin, + const rayvec3& rayDir, + const rayvec3& faceMin, + const rayvec2& faceOppositeCorner ){ - if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision of "parallelity" + if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8) { //precision of "parallelity" return RayRelation::Parallel; } - glm::float64 rayCoef = (faceMin.x - rayOrigin.x); - rayvec3 intersectPointMult = {faceMin.x * rayDir.x, - rayCoef*rayDir.y + rayOrigin.y * rayDir.x, - rayCoef*rayDir.z + rayOrigin.z * rayDir.x}; - - if (rayDir.x > 0){ - if (intersectPointMult.y >= faceMin.y * rayDir.x //Face-hit check - && intersectPointMult.y <= faceOppositeCorner[0] * rayDir.x - && intersectPointMult.z >= faceMin.z * rayDir.x - && intersectPointMult.z <= faceOppositeCorner[1] * rayDir.x){ - currentDistApprox = updateNormal(fabs(rayCoef * rayDir.y * rayDir.z), -X_AXIS, currentDistApprox, normal_ret); - return RayRelation::Intersect; - } - } - else{ - if (intersectPointMult.y <= faceMin.y * rayDir.x //Face-hit check for negative dir. - && intersectPointMult.y >= faceOppositeCorner[0] * rayDir.x - && intersectPointMult.z <= faceMin.z * rayDir.x - && intersectPointMult.z >= faceOppositeCorner[1] * rayDir.x){ - currentDistApprox = updateNormal(fabs(rayCoef * rayDir.y * rayDir.z), X_AXIS, currentDistApprox, normal_ret); - return RayRelation::Intersect; - } + scalar_t rayCoef = (faceMin.x - rayOrigin.x) / (rayDir.x); + if (rayCoef < 0) return RayRelation::None; + rayvec3 intersectPoint = { faceMin.x, + rayCoef * rayDir.y + rayOrigin.y, + rayCoef * rayDir.z + rayOrigin.z }; + + if (intersectPoint.y >= faceMin.y + && intersectPoint.y <= faceOppositeCorner[0] + && intersectPoint.z >= faceMin.z + && intersectPoint.z <= faceOppositeCorner[1]) { + return RayRelation::Intersect; } return RayRelation::None; } template <> RayRelation Rays::isRayIntersectsAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, - const rayvec3& faceMin, - const rayvec2& faceOppositeCorner, - glm::ivec3& normal_ret, - double& currentDistApprox - ){ - if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision of "parallelity" + const rayvec3& rayOrigin, + const rayvec3& rayDir, + const rayvec3& faceMin, + const rayvec2& faceOppositeCorner + ) { + if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8) { //precision of "parallelity" return RayRelation::Parallel; } - glm::float64 rayCoef = (faceMin.y - rayOrigin.y); - rayvec3 intersectPointMult = {rayCoef*rayDir.x + rayOrigin.x * rayDir.y, - faceMin.y * rayDir.y, - rayCoef*rayDir.z + rayOrigin.z * rayDir.y}; - - if (rayDir.y > 0){ - if (intersectPointMult.x >= faceMin.x * rayDir.y //Face-hit check - && intersectPointMult.x <= faceOppositeCorner[0] * rayDir.y - && intersectPointMult.z >= faceMin.z * rayDir.y - && intersectPointMult.z <= faceOppositeCorner[1] * rayDir.y){ - currentDistApprox = updateNormal(fabs(rayCoef * rayDir.x * rayDir.z), -Y_AXIS, currentDistApprox, normal_ret); - return RayRelation::Intersect; - } - } - else{ - if (intersectPointMult.x <= faceMin.x * rayDir.y //Face-hit check for negative dir. - && intersectPointMult.x >= faceOppositeCorner[0] * rayDir.y - && intersectPointMult.z <= faceMin.z * rayDir.y - && intersectPointMult.z >= faceOppositeCorner[1] * rayDir.y){ - currentDistApprox = updateNormal(fabs(rayCoef * rayDir.x * rayDir.z), Y_AXIS, currentDistApprox, normal_ret); - return RayRelation::Intersect; - } + scalar_t rayCoef = (faceMin.y - rayOrigin.y) / (rayDir.y); + if (rayCoef < 0) return RayRelation::None; + rayvec3 intersectPoint = { rayCoef * rayDir.x + rayOrigin.x, + faceMin.y, + rayCoef * rayDir.z + rayOrigin.z }; + + if (intersectPoint.x >= faceMin.x //Face-hit check + && intersectPoint.x <= faceOppositeCorner[0] + && intersectPoint.z >= faceMin.z + && intersectPoint.z <= faceOppositeCorner[1]) { + return RayRelation::Intersect; } return RayRelation::None; } template <> RayRelation Rays::isRayIntersectsAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, - const rayvec3& faceMin, - const rayvec2& faceOppositeCorner, - glm::ivec3& normal_ret, - double& currentDistApprox - ){ - if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision of "parallelity" + const rayvec3& rayOrigin, + const rayvec3& rayDir, + const rayvec3& faceMin, + const rayvec2& faceOppositeCorner + ) { + if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8) { //precision of "parallelity" return RayRelation::Parallel; } - glm::float64 rayCoef = (faceMin.z - rayOrigin.z); - rayvec3 intersectPointMult = {rayCoef*rayDir.x + rayOrigin.x * rayDir.z, - rayCoef*rayDir.y + rayOrigin.y * rayDir.z, - faceMin.z * rayDir.z}; - - if (rayDir.z > 0){ - if (intersectPointMult.x >= faceMin.x * rayDir.z //Face-hit check - && intersectPointMult.x <= faceOppositeCorner[0] * rayDir.z - && intersectPointMult.y >= faceMin.y * rayDir.z - && intersectPointMult.y <= faceOppositeCorner[1] * rayDir.z){ - currentDistApprox = updateNormal(fabs(rayCoef * rayDir.x * rayDir.y), -Z_AXIS, currentDistApprox, normal_ret); - return RayRelation::Intersect; - } - } - else{ - if (intersectPointMult.x <= faceMin.x * rayDir.z //Face-hit check - && intersectPointMult.x >= faceOppositeCorner[0] * rayDir.z - && intersectPointMult.y <= faceMin.y * rayDir.z - && intersectPointMult.y >= faceOppositeCorner[1] * rayDir.z){ - currentDistApprox = updateNormal(fabs(rayCoef * rayDir.x * rayDir.y), Z_AXIS, currentDistApprox, normal_ret); - return RayRelation::Intersect; - } + scalar_t rayCoef = (faceMin.z - rayOrigin.z) / (rayDir.z); + if (rayCoef < 0) return RayRelation::None; + rayvec3 intersectPoint = { rayCoef * rayDir.x + rayOrigin.x, + rayCoef * rayDir.y + rayOrigin.y, + faceMin.z }; + + if (intersectPoint.x >= faceMin.x //Face-hit check + && intersectPoint.x <= faceOppositeCorner[0] + && intersectPoint.y >= faceMin.y + && intersectPoint.y <= faceOppositeCorner[1]) { + return RayRelation::Intersect; } return RayRelation::None; } @@ -272,23 +205,22 @@ RayRelation Rays::rayIntersectAABB( const rayvec3& boxPos, const AABB& box, float maxDist, - rayvec3& pointIn_ret, - rayvec3& pointOut_ret, - glm::ivec3& normal_ret){ + glm::ivec3& normal_ret, + scalar_t& distance_ret){ if constexpr (IS_RAYS_BOX_CACHE_ON){ if (raysBoxCache_.find(boxPos) != raysBoxCache_.end()){ const AABBFaces& boxFaces = raysBoxCache_[boxPos]; - return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, pointIn_ret, pointOut_ret, normal_ret); + return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret); } else { const AABBFaces& boxFaces = AABBFaces(boxPos, box); raysBoxCache_[boxPos] = boxFaces; - return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, pointIn_ret, pointOut_ret, normal_ret); + return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret); } } else { const AABBFaces& boxFaces = AABBFaces(boxPos, box); - return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, pointIn_ret, pointOut_ret, normal_ret); + return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, normal_ret, distance_ret); } } @@ -298,54 +230,63 @@ RayRelation Rays::rayIntersectAABBFaces( const rayvec3& rayDir, const AABBFaces& boxFaces, float maxDist, - rayvec3& pointIn_ret, - rayvec3& pointOut_ret, - glm::ivec3& normal_ret){//TODO: points returning - RayRelation rel; - double faceDistApprox = maxDist; - unsigned char intersectedCount = 0; //this code is very uncomfortable, DONT LEARN IT! - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second, normal_ret, faceDistApprox - ); - if (rel > RayRelation::None){ - ++intersectedCount; + glm::ivec3& normal_ret, + scalar_t& distance_ret){//TODO: points returning + + scalar_t faceDist; + distance_ret = maxDist; + glm::ivec3 bufNormal; + //unsigned char intersectedCount = 0; //this code is very uncomfortable, DONT LEARN IT! + bool isIntersect = false; + + if (rayIntersectAAFace( + rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second, bufNormal, faceDist + ) > RayRelation::None && faceDist < distance_ret){ + isIntersect = true; + normal_ret = bufNormal; + distance_ret = faceDist; } - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, normal_ret, faceDistApprox - ); - if (rel > RayRelation::None){ - ++intersectedCount; + if (rayIntersectAAFace( + rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, bufNormal, faceDist + ) > RayRelation::None && faceDist < distance_ret) { + isIntersect = true; + normal_ret = bufNormal; + distance_ret = faceDist; } - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, normal_ret, faceDistApprox - ); - if (rel > RayRelation::None){ - ++intersectedCount; + if (rayIntersectAAFace( + rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, bufNormal, faceDist + ) > RayRelation::None && faceDist < distance_ret) { + isIntersect = true; + normal_ret = bufNormal; + distance_ret = faceDist; } - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, normal_ret, faceDistApprox - ); - if (rel > RayRelation::None){ - ++intersectedCount; + if (rayIntersectAAFace( + rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, bufNormal, faceDist + ) > RayRelation::None && faceDist < distance_ret) { + isIntersect = true; + normal_ret = bufNormal; + distance_ret = faceDist; } - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, normal_ret, faceDistApprox - ); - if (rel > RayRelation::None){ - ++intersectedCount; + if (rayIntersectAAFace( + rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, bufNormal, faceDist + ) > RayRelation::None && faceDist < distance_ret) { + isIntersect = true; + normal_ret = bufNormal; + distance_ret = faceDist; } - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, normal_ret, faceDistApprox - ); - if (rel > RayRelation::None){ - ++intersectedCount; + if (rayIntersectAAFace( + rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, bufNormal, faceDist + ) > RayRelation::None && faceDist < distance_ret) { + isIntersect = true; + normal_ret = bufNormal; + distance_ret = faceDist; } - if (intersectedCount > 0) return RayRelation::Intersect; + if (isIntersect) return RayRelation::Intersect; return RayRelation::None; } \ No newline at end of file diff --git a/src/maths/rays.h b/src/maths/rays.h index 769259f2..7956dec8 100644 --- a/src/maths/rays.h +++ b/src/maths/rays.h @@ -10,6 +10,7 @@ typedef glm::highp_dvec3 rayvec3; typedef glm::highp_dvec2 rayvec2; +typedef double scalar_t; enum class RayRelation{ Embed=2, Intersect=1, Parallel=0, None=0 @@ -37,43 +38,58 @@ public: template<> struct std::hash{ std::size_t operator()(const rayvec3& r) const noexcept{ - return std::hash{}(r.x) ^ (std::hash{}(r.y) << 1) ^ (std::hash{}(r.z) << 2); + return std::hash{}(r.x) ^ (std::hash{}(r.y) << 1) ^ (std::hash{}(r.z) << 2); } }; class Rays{ protected: - static const bool IS_RAYS_BOX_CACHE_ON = false; + static const bool IS_RAYS_BOX_CACHE_ON = true; static std::unordered_map raysBoxCache_; //[boxPos]: faces array public: -template -static RayRelation rayIntersectAAFace( - const rayvec3& rayOrigin, - const rayvec3& rayDir, - const rayvec3& faceMin, - const rayvec2& faceOppositeCorner, - rayvec3& intersectPoint_ret - ); - -static double updateNormal( - double newDistApprox, - const glm::ivec3& newNormal, - double currentDistApprox, - glm::ivec3& normal_ret - ); - -//optimized, not returns intersectPoint coordinates +//optimized, NOT returns intersectPoint coordinates and normal vector template static RayRelation isRayIntersectsAAFace( const rayvec3& rayOrigin, + const rayvec3& rayDir, + const rayvec3& faceMin, + const rayvec2& faceOppositeCorner + ); + +//returns only normal +template +static RayRelation rayIntersectAAFace( + const rayvec3& rayOrigin, + const rayvec3& rayDir, + const rayvec3& faceMin, + const rayvec2& faceOppositeCorner, + glm::ivec3& normal_ret +); + +//returns normal and distance +template +static RayRelation rayIntersectAAFace( + const rayvec3& rayOrigin, const rayvec3& rayDir, const rayvec3& faceMin, const rayvec2& faceOppositeCorner, glm::ivec3& normal_ret, - double& currentDistApprox - ); + scalar_t& distance_ret + ); + +// returns normal, distance and intersection point +template +static RayRelation rayIntersectAAFace( + const rayvec3& rayOrigin, + const rayvec3& rayDir, + const rayvec3& faceMin, + const rayvec2& faceOppositeCorner, + glm::ivec3& normal_ret, + scalar_t& distance_ret, + rayvec3& intersectPoint_ret +); static RayRelation rayIntersectAABB( const rayvec3& rayOrigin, @@ -81,18 +97,16 @@ static RayRelation rayIntersectAABB( const rayvec3& boxPos, const AABB& box, float maxDist, - rayvec3& pointIn_ret, - rayvec3& pointOut_ret, - glm::ivec3& normal_ret); + glm::ivec3& normal_ret, + scalar_t& distance_ret); -static RayRelation rayIntersectAABBFaces( +static RayRelation rayIntersectAABBFaces( // calculates only normal and distance const rayvec3& rayOrigin, const rayvec3& rayDir, const AABBFaces& boxFaces, float maxDist, - rayvec3& pointIn_ret, - rayvec3& pointOut_ret, - glm::ivec3& normal_ret); + glm::ivec3& normal_ret, + scalar_t& distance_ret); }; #endif // SRC_VOXNATHS_H_ diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index ad4a6f09..13b4631f 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -234,8 +234,9 @@ voxel* Chunks::rayCast(vec3 start, const AABB& box = def->rotatable ? def->rt.hitboxes[voxel->rotation()] : def->hitbox; - rayvec3 in, out; // <- now not used, but for future... - if (Rays::rayIntersectAABB(start, dir, iend, box, maxDist, in, out, norm) > RayRelation::None){ + scalar_t distance; + if (Rays::rayIntersectAABB(start, dir, iend, box, maxDist, norm, distance) > RayRelation::None){ + end = start + (dir * vec3(distance)); return voxel; } From 380d1d0959a0f12cd4716356223c066bea2fd6c7 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 10 Dec 2023 17:40:08 +0300 Subject: [PATCH 3/3] world does not appear in list first time after creation fix --- src/frontend/menu.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index 3d90fee7..8ccf3fb5 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -213,6 +213,7 @@ Panel* create_new_world_panel(Engine* engine, PagesControl* menu) { std::cout << "world seed: " << seed << std::endl; auto folder = paths->getWorldsFolder()/u8path(nameutf8); + std::filesystem::create_directories(folder); Level* level = World::create(nameutf8, folder, seed,