From 616057b2b93f8d0e054974030f21994f79bfc8f6 Mon Sep 17 00:00:00 2001 From: Ara Date: Sun, 10 Dec 2023 00:38:32 +0600 Subject: [PATCH] normals finished --- src/maths/rays.cpp | 49 ++++++++++++++++++++++++++++++++----------- src/maths/rays.h | 12 ++++++++++- src/voxels/Chunks.cpp | 4 ++-- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/src/maths/rays.cpp b/src/maths/rays.cpp index 868c479fc..450fdf753 100644 --- a/src/maths/rays.cpp +++ b/src/maths/rays.cpp @@ -136,13 +136,27 @@ RayRelation Rays::rayIntersectAAFace( 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 + glm::ivec3& normal_ret, + double& currentDistApprox ){ if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision of "parallelity" return RayRelation::Parallel; @@ -158,6 +172,7 @@ RayRelation Rays::isRayIntersectsAAFace( && 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; } } @@ -166,6 +181,7 @@ RayRelation Rays::isRayIntersectsAAFace( && 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; } } @@ -178,7 +194,8 @@ RayRelation Rays::isRayIntersectsAAFace( const rayvec3& rayDir, const rayvec3& faceMin, const rayvec2& faceOppositeCorner, - glm::ivec3& normal_ret + glm::ivec3& normal_ret, + double& currentDistApprox ){ if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision of "parallelity" return RayRelation::Parallel; @@ -194,6 +211,7 @@ RayRelation Rays::isRayIntersectsAAFace( && 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; } } @@ -202,6 +220,7 @@ RayRelation Rays::isRayIntersectsAAFace( && 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; } } @@ -214,7 +233,8 @@ RayRelation Rays::isRayIntersectsAAFace( const rayvec3& rayDir, const rayvec3& faceMin, const rayvec2& faceOppositeCorner, - glm::ivec3& normal_ret + glm::ivec3& normal_ret, + double& currentDistApprox ){ if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision of "parallelity" return RayRelation::Parallel; @@ -230,6 +250,7 @@ RayRelation Rays::isRayIntersectsAAFace( && 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; } } @@ -238,6 +259,7 @@ RayRelation Rays::isRayIntersectsAAFace( && 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; } } @@ -249,6 +271,7 @@ RayRelation Rays::rayIntersectAABB( const rayvec3& rayDir, const rayvec3& boxPos, const AABB& box, + float maxDist, rayvec3& pointIn_ret, rayvec3& pointOut_ret, glm::ivec3& normal_ret){ @@ -256,16 +279,16 @@ RayRelation Rays::rayIntersectAABB( if (raysBoxCache_.find(boxPos) != raysBoxCache_.end()){ const AABBFaces& boxFaces = raysBoxCache_[boxPos]; - return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, pointIn_ret, pointOut_ret, normal_ret); + return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, pointIn_ret, pointOut_ret, normal_ret); } else { const AABBFaces& boxFaces = AABBFaces(boxPos, box); raysBoxCache_[boxPos] = boxFaces; - return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, pointIn_ret, pointOut_ret, normal_ret); + return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, pointIn_ret, pointOut_ret, normal_ret); } } else { const AABBFaces& boxFaces = AABBFaces(boxPos, box); - return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, pointIn_ret, pointOut_ret, normal_ret); + return rayIntersectAABBFaces(rayOrigin, rayDir, boxFaces, maxDist, pointIn_ret, pointOut_ret, normal_ret); } } @@ -274,48 +297,50 @@ RayRelation Rays::rayIntersectAABBFaces( const rayvec3& rayOrigin, 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 + rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second, normal_ret, faceDistApprox ); if (rel > RayRelation::None){ ++intersectedCount; } rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, normal_ret + rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, normal_ret, faceDistApprox ); if (rel > RayRelation::None){ ++intersectedCount; } rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, normal_ret + rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, normal_ret, faceDistApprox ); if (rel > RayRelation::None){ ++intersectedCount; } rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, normal_ret + rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, normal_ret, faceDistApprox ); if (rel > RayRelation::None){ ++intersectedCount; } rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, normal_ret + rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, normal_ret, faceDistApprox ); if (rel > RayRelation::None){ ++intersectedCount; } rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, normal_ret + rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, normal_ret, faceDistApprox ); if (rel > RayRelation::None){ ++intersectedCount; diff --git a/src/maths/rays.h b/src/maths/rays.h index 1b04b2573..bf6c7e766 100644 --- a/src/maths/rays.h +++ b/src/maths/rays.h @@ -56,6 +56,13 @@ static RayRelation rayIntersectAAFace( rayvec3& intersectPoint_ret ); +static double updateNormal( + double newDistApprox, + const glm::ivec3& newNormal, + double currentDistApprox, + glm::ivec3& normal_ret + ); + //optimized, not returns intersectPoint coordinates template static RayRelation isRayIntersectsAAFace( @@ -63,7 +70,8 @@ static RayRelation isRayIntersectsAAFace( const rayvec3& rayDir, const rayvec3& faceMin, const rayvec2& faceOppositeCorner, - glm::ivec3& normal_ret + glm::ivec3& normal_ret, + double& currentDistApprox ); static RayRelation rayIntersectAABB( @@ -71,6 +79,7 @@ static RayRelation rayIntersectAABB( const rayvec3& rayDir, const rayvec3& boxPos, const AABB& box, + float maxDist, rayvec3& pointIn_ret, rayvec3& pointOut_ret, glm::ivec3& normal_ret); @@ -79,6 +88,7 @@ static RayRelation rayIntersectAABBFaces( const rayvec3& rayOrigin, const rayvec3& rayDir, const AABBFaces& boxFaces, + float maxDist, rayvec3& pointIn_ret, rayvec3& pointOut_ret, glm::ivec3& normal_ret); diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 8899d617a..497bb0826 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -223,7 +223,7 @@ voxel* Chunks::rayCast(vec3 start, const Block* def = contentIds->getBlockDef(voxel->id); if (def->selectable){ - //timeutil::ScopeLogTimer lg((long long)def); + timeutil::ScopeLogTimer lg((long long)def); end.x = px + t * dx; end.y = py + t * dy; end.z = pz + t * dz; @@ -236,7 +236,7 @@ voxel* Chunks::rayCast(vec3 start, ? def->rt.hitboxes[voxel->rotation()] : def->hitbox; rayvec3 in, out; // <- now not used, but for future... - if (Rays::rayIntersectAABB(start, dir, iend, box, in, out, norm) > RayRelation::None){ + if (Rays::rayIntersectAABB(start, dir, iend, box, maxDist, in, out, norm) > RayRelation::None){ return voxel; }