diff --git a/src/frontend/gui/panels.h b/src/frontend/gui/panels.h index 1911abfc..9480719f 100644 --- a/src/frontend/gui/panels.h +++ b/src/frontend/gui/panels.h @@ -93,8 +93,8 @@ namespace gui { void back(); void clearHistory(); void reset(); -//comment or erase pragma if you hate notes.. -#pragma message("gui::PagesControl::current() returns Page by-value! (can initiate unexpected behaviour)") + +//FIXME "gui::PagesControl::current() returns Page by-value! (can initiate unexpected behaviour)") Page current(); }; } diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index acd72244..f731f9e2 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -110,7 +110,7 @@ void CameraControl::update(PlayerInput& input, float delta) { vec3 PlayerController::selectedBlockPosition; vec3 PlayerController::selectedPointPosition; -vec3 PlayerController::selectedBlockNormal; +ivec3 PlayerController::selectedBlockNormal; int PlayerController::selectedBlockId = -1; int PlayerController::selectedBlockStates = 0; @@ -192,7 +192,8 @@ void PlayerController::updateInteraction(){ Lighting* lighting = level->lighting; Camera* camera = player->camera; vec3 end; - vec3 norm; + ivec3 iend; + ivec3 norm; bool xkey = Events::pressed(keycode::X); bool lclick = Events::jactive(BIND_PLAYER_ATTACK) || @@ -203,7 +204,7 @@ void PlayerController::updateInteraction(){ if (xkey) { maxDistance *= 20.0f; } - vec3 iend; + voxel* vox = chunks->rayCast(camera->position, camera->front, maxDistance, @@ -215,9 +216,9 @@ void PlayerController::updateInteraction(){ selectedBlockPosition = iend; selectedPointPosition = end; selectedBlockNormal = norm; - int x = (int)iend.x; - int y = (int)iend.y; - int z = (int)iend.z; + int x = iend.x; + int y = iend.y; + int z = iend.z; uint8_t states = 0; Block* def = contentIds->getBlockDef(player->choosenBlock); @@ -250,9 +251,9 @@ void PlayerController::updateInteraction(){ } if (rclick){ if (block->model != BlockModel::xsprite){ - x = (int)(iend.x)+(int)(norm.x); - y = (int)(iend.y)+(int)(norm.y); - z = (int)(iend.z)+(int)(norm.z); + x = (iend.x)+(norm.x); + y = (iend.y)+(norm.y); + z = (iend.z)+(norm.z); } vox = chunks->get(x, y, z); if (vox && (block = contentIds->getBlockDef(vox->id))->replaceable) { diff --git a/src/logic/PlayerController.h b/src/logic/PlayerController.h index a2f7ccf9..158b0b87 100644 --- a/src/logic/PlayerController.h +++ b/src/logic/PlayerController.h @@ -37,7 +37,7 @@ class PlayerController { void updateInteraction(); public: static glm::vec3 selectedBlockPosition; - static glm::vec3 selectedBlockNormal; + static glm::ivec3 selectedBlockNormal; static glm::vec3 selectedPointPosition; static int selectedBlockId; static int selectedBlockStates; diff --git a/src/maths/rays.cpp b/src/maths/rays.cpp index 4a0a899f..868c479f 100644 --- a/src/maths/rays.cpp +++ b/src/maths/rays.cpp @@ -4,22 +4,18 @@ #include "glm/glm.hpp" -constexpr std::array AABBFACES_KINDS_ORDER = { - AAFaceKind::Xperp, AAFaceKind::Xperp, - AAFaceKind::Yperp, AAFaceKind::Yperp, - AAFaceKind::Zperp, AAFaceKind::Zperp}; std::unordered_map Rays::raysBoxCache_ = {}; const rayvec3 X_AXIS = rayvec3(1,0,0), Y_AXIS = rayvec3(0,1,0), Z_AXIS = rayvec3(0,0,1); -//make edges from AABB +//make faces from AABB AABBFaces::AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox){ - rayvec3 pbMin = parentBox.min(), + rayvec3 pbMin = parentBox.min(), // every face is min-point and opposite corner point pbMax = parentBox.max(), pbRealPos = parentBoxPos + pbMin; rayvec2 yzMax = rayvec2(parentBoxPos.y + pbMax.y, parentBoxPos.z + pbMax.z ), xzMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.z + pbMax.z ), xyMax = rayvec2(parentBoxPos.x + pbMax.x, parentBoxPos.y + pbMax.y ); - faces[0] = {pbRealPos, yzMax}; + faces[0] = {pbRealPos, yzMax}; //in order of AABBFaces::KINDS_ORDER! faces[1] = {parentBoxPos + rayvec3(pbMax.x, pbMin.y, pbMin.z), yzMax}; @@ -41,9 +37,6 @@ RayRelation Rays::rayIntersectAAFace( rayvec3& intersectPoint_ret ){ if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision - if (rayOrigin.x == faceMin.x) { - return RayRelation::Embed; // there can be check of hit, but not necessarily - } return RayRelation::Parallel; } @@ -80,9 +73,6 @@ RayRelation Rays::rayIntersectAAFace( rayvec3& intersectPoint_ret ){ if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision - if (rayOrigin.y == faceMin.y) { - return RayRelation::Embed; // there can be check of hit, but not necessarily - } return RayRelation::Parallel; } @@ -119,9 +109,6 @@ RayRelation Rays::rayIntersectAAFace( rayvec3& intersectPoint_ret ){ if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision - if (rayOrigin.z == faceMin.z) { - return RayRelation::Embed; // there can be check of hit, but not necessarily - } return RayRelation::Parallel; } @@ -154,12 +141,10 @@ RayRelation Rays::isRayIntersectsAAFace( const rayvec3& rayOrigin, const rayvec3& rayDir, const rayvec3& faceMin, - const rayvec2& faceOppositeCorner + const rayvec2& faceOppositeCorner, + glm::ivec3& normal_ret ){ if (fabs(glm::dot(rayDir, X_AXIS)) < 1.0E-8){ //precision of "parallelity" - if (rayOrigin.x == faceMin.x) { - return RayRelation::Embed; // there can be check of hit, but not necessarily - } return RayRelation::Parallel; } @@ -192,12 +177,10 @@ RayRelation Rays::isRayIntersectsAAFace( const rayvec3& rayOrigin, const rayvec3& rayDir, const rayvec3& faceMin, - const rayvec2& faceOppositeCorner + const rayvec2& faceOppositeCorner, + glm::ivec3& normal_ret ){ - if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision - if (rayOrigin.y == faceMin.y) { - return RayRelation::Embed; // there can be check of hit, but not necessarily - } + if (fabs(glm::dot(rayDir, Y_AXIS)) < 1.0E-8){ //precision of "parallelity" return RayRelation::Parallel; } @@ -230,12 +213,10 @@ RayRelation Rays::isRayIntersectsAAFace( const rayvec3& rayOrigin, const rayvec3& rayDir, const rayvec3& faceMin, - const rayvec2& faceOppositeCorner + const rayvec2& faceOppositeCorner, + glm::ivec3& normal_ret ){ - if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision - if (rayOrigin.z == faceMin.z) { - return RayRelation::Embed; // there can be check of hit, but not necessarily - } + if (fabs(glm::dot(rayDir, Z_AXIS)) < 1.0E-8){ //precision of "parallelity" return RayRelation::Parallel; } @@ -295,39 +276,51 @@ RayRelation Rays::rayIntersectAABBFaces( const AABBFaces& boxFaces, rayvec3& pointIn_ret, rayvec3& pointOut_ret, - glm::ivec3& normal_ret){//TODO: refs update + glm::ivec3& normal_ret){//TODO: points returning RayRelation rel; - unsigned char intersectedCount = 0; - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[0].first, boxFaces.faces[0].second - ); - intersectedCount+= (bool)rel; + 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 + ); + if (rel > RayRelation::None){ + ++intersectedCount; + } - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second - ); - intersectedCount+= (bool)rel; + rel = isRayIntersectsAAFace( + rayOrigin, rayDir, boxFaces.faces[1].first, boxFaces.faces[1].second, normal_ret + ); + if (rel > RayRelation::None){ + ++intersectedCount; + } - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second - ); - intersectedCount+= (bool)rel; + rel = isRayIntersectsAAFace( + rayOrigin, rayDir, boxFaces.faces[2].first, boxFaces.faces[2].second, normal_ret + ); + if (rel > RayRelation::None){ + ++intersectedCount; + } - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second - ); - intersectedCount+= (bool)rel; + rel = isRayIntersectsAAFace( + rayOrigin, rayDir, boxFaces.faces[3].first, boxFaces.faces[3].second, normal_ret + ); + if (rel > RayRelation::None){ + ++intersectedCount; + } - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second - ); - intersectedCount+= (bool)rel; - - rel = isRayIntersectsAAFace( - rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second - ); - intersectedCount+= (bool)rel; + rel = isRayIntersectsAAFace( + rayOrigin, rayDir, boxFaces.faces[4].first, boxFaces.faces[4].second, normal_ret + ); + if (rel > RayRelation::None){ + ++intersectedCount; + } + rel = isRayIntersectsAAFace( + rayOrigin, rayDir, boxFaces.faces[5].first, boxFaces.faces[5].second, normal_ret + ); + if (rel > RayRelation::None){ + ++intersectedCount; + } + if (intersectedCount > 0) 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 94293bb6..1b04b257 100644 --- a/src/maths/rays.h +++ b/src/maths/rays.h @@ -23,6 +23,11 @@ class AABBFaces{ public: std::array, AABBFACES_COUNT> faces; // every face is min-point and opposite corner point + static constexpr std::array KINDS_ORDER = { + AAFaceKind::Xperp, AAFaceKind::Xperp, + AAFaceKind::Yperp, AAFaceKind::Yperp, + AAFaceKind::Zperp, AAFaceKind::Zperp}; + AABBFaces(){}; AABBFaces(const rayvec3& parentBoxPos, const AABB& parentBox); @@ -57,7 +62,8 @@ static RayRelation isRayIntersectsAAFace( const rayvec3& rayOrigin, const rayvec3& rayDir, const rayvec3& faceMin, - const rayvec2& faceOppositeCorner + const rayvec2& faceOppositeCorner, + glm::ivec3& normal_ret ); static RayRelation rayIntersectAABB( diff --git a/src/util/timeutil.cpp b/src/util/timeutil.cpp index 04be381f..e299bec1 100644 --- a/src/util/timeutil.cpp +++ b/src/util/timeutil.cpp @@ -16,7 +16,7 @@ int64_t timeutil::Timer::stop() { timeutil::ScopeLogTimer::ScopeLogTimer(long long id) : scopeid_(id) {} timeutil::ScopeLogTimer::~ScopeLogTimer() { - std::cout << "Scope "<< scopeid_ <<" finished in "<< ScopeLogTimer::stop() << " micros."< using glm::vec3; +using glm::ivec3; using std::shared_ptr; Chunks::Chunks(int w, int d, @@ -177,13 +178,12 @@ void Chunks::set(int x, int y, int z, int id, uint8_t states){ chunk->setModified(true); } #include "../util/timeutil.h" -#include voxel* Chunks::rayCast(vec3 start, vec3 dir, float maxDist, vec3& end, - vec3& norm, - vec3& iend) { + ivec3& norm, + ivec3& iend) { float px = start.x; float py = start.y; float pz = start.z; @@ -197,9 +197,9 @@ voxel* Chunks::rayCast(vec3 start, int iy = floor(py); int iz = floor(pz); - float stepx = (dx > 0.0f) ? 1.0f : -1.0f; - float stepy = (dy > 0.0f) ? 1.0f : -1.0f; - float stepz = (dz > 0.0f) ? 1.0f : -1.0f; + int stepx = (dx > 0.0f) ? 1 : -1; + int stepy = (dy > 0.0f) ? 1 : -1; + int stepz = (dz > 0.0f) ? 1 : -1; constexpr float infinity = std::numeric_limits::infinity(); @@ -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; @@ -231,61 +231,12 @@ voxel* Chunks::rayCast(vec3 start, iend.y = iy; iend.z = iz; - if (def && !def->rt.solid) { - const int gridSize = BLOCK_AABB_GRID * 2; + if (!def->rt.solid) { const AABB& box = def->rotatable ? def->rt.hitboxes[voxel->rotation()] : def->hitbox; - /*const int subs = gridSize; - iend = vec3(ix, iy, iz); - end -= iend; // in-block coordinates - int six = end.x * gridSize; - int siy = end.y * gridSize; - int siz = end.z * gridSize; - float stxMax = (txDelta < infinity) ? txDelta * xdist : infinity; - float styMax = (tyDelta < infinity) ? tyDelta * ydist : infinity; - float stzMax = (tzDelta < infinity) ? tzDelta * zdist : infinity; - for (int i = 0; i < subs*3; i++) { - end.x = six / float(gridSize); - end.y = siy / float(gridSize); - end.z = siz / float(gridSize); - if (box.inside(end)) { - end += iend; - norm.x = norm.y = norm.z = 0.0f; - if (steppedIndex == 0) norm.x = -stepx; - if (steppedIndex == 1) norm.y = -stepy; - if (steppedIndex == 2) norm.z = -stepz; - return voxel; - } - if (stxMax < styMax) { - if (stxMax < stzMax) { - six += stepx; - stxMax += txDelta; - steppedIndex = 0; - } else { - siz += stepz; - stzMax += tzDelta; - steppedIndex = 2; - } - } else { - if (styMax < stzMax) { - siy += stepy; - styMax += tyDelta; - steppedIndex = 1; - } else { - siz += stepz; - stzMax += tzDelta; - steppedIndex = 2; - } - } - }*/ - rayvec3 in, out; - glm::ivec3 normal; - if ((bool)Rays::rayIntersectAABB(start, dir, iend, box, in, out, normal)){ - norm.x = norm.y = norm.z = 0.0f; - if (steppedIndex == 0) norm.x = -stepx; - if (steppedIndex == 1) norm.y = -stepy; - if (steppedIndex == 2) norm.z = -stepz; + rayvec3 in, out; // <- now not used, but for future... + if (Rays::rayIntersectAABB(start, dir, iend, box, in, out, norm) > RayRelation::None){ return voxel; } @@ -294,7 +245,7 @@ voxel* Chunks::rayCast(vec3 start, iend.y = iy; iend.z = iz; - norm.x = norm.y = norm.z = 0.0f; + norm.x = norm.y = norm.z = 0; if (steppedIndex == 0) norm.x = -stepx; if (steppedIndex == 1) norm.y = -stepy; if (steppedIndex == 2) norm.z = -stepz; @@ -334,7 +285,7 @@ voxel* Chunks::rayCast(vec3 start, end.x = px + t * dx; end.y = py + t * dy; end.z = pz + t * dz; - norm.x = norm.y = norm.z = 0.0f; + norm.x = norm.y = norm.z = 0; return nullptr; } diff --git a/src/voxels/Chunks.h b/src/voxels/Chunks.h index 687499f2..78ed84bb 100644 --- a/src/voxels/Chunks.h +++ b/src/voxels/Chunks.h @@ -48,8 +48,8 @@ public: glm::vec3 dir, float maxLength, glm::vec3& end, - glm::vec3& norm, - glm::vec3& iend); + glm::ivec3& norm, + glm::ivec3& iend); const AABB* isObstacle(float x, float y, float z);