From cee0214ac53c2bdeceea8106836bcef46028e6b4 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 30 Nov 2023 15:04:20 +0300 Subject: [PATCH] selection normal vector debug --- src/frontend/WorldRenderer.cpp | 5 +++++ src/graphics/LineBatch.h | 5 ++++- src/logic/PlayerController.cpp | 5 +++++ src/logic/PlayerController.h | 2 ++ src/voxels/Chunks.cpp | 1 + 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/frontend/WorldRenderer.cpp b/src/frontend/WorldRenderer.cpp index 18538123..2571d384 100644 --- a/src/frontend/WorldRenderer.cpp +++ b/src/frontend/WorldRenderer.cpp @@ -189,6 +189,8 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){ Block* block = contentIds->getBlockDef(id); assert(block != nullptr); const vec3 pos = PlayerController::selectedBlockPosition; + const vec3 point = PlayerController::selectedPointPosition; + const vec3 norm = PlayerController::selectedBlockNormal; const AABB& hitbox = block->hitbox; const vec3 center = pos + hitbox.center(); const vec3 size = hitbox.size(); @@ -196,6 +198,8 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){ linesShader->uniformMatrix("u_projview", camera->getProjView()); lineBatch->lineWidth(2.0f); lineBatch->box(center, size + vec3(0.02), vec4(0.f, 0.f, 0.f, 0.5f)); + if (level->player->debug) + lineBatch->line(point, point+norm*0.5f, vec4(1.0f, 0.0f, 1.0f, 1.0f)); lineBatch->render(); } skybox->unbind(); @@ -206,6 +210,7 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera){ ctx.depthTest(true); linesShader->use(); + if (settings.debug.showChunkBorders){ linesShader->uniformMatrix("u_projview", camera->getProjView()); vec3 coord = level->player->camera->position; diff --git a/src/graphics/LineBatch.h b/src/graphics/LineBatch.h index bbecaa0b..fac9c877 100644 --- a/src/graphics/LineBatch.h +++ b/src/graphics/LineBatch.h @@ -15,13 +15,16 @@ public: LineBatch(size_t capacity=4096); ~LineBatch(); + inline void line(const glm::vec3 a, const glm::vec3 b, const glm::vec4 color) { + line(a.x, a.y, a.z, b.x, b.y, b.z, color.r, color.g, color.b, color.a); + } void line(float x1, float y1, float z1, float x2, float y2, float z2, float r, float g, float b, float a); void box(float x, float y, float z, float w, float h, float d, float r, float g, float b, float a); inline void box(glm::vec3 xyz, glm::vec3 whd, glm::vec4 rgba) { - return box(xyz.x, xyz.y, xyz.z, whd.x, whd.y, whd.z, + box(xyz.x, xyz.y, xyz.z, whd.x, whd.y, whd.z, rgba.r, rgba.g, rgba.b, rgba.a); } diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index cf7b466e..ae2ecf1a 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -107,6 +107,8 @@ void CameraControl::update(PlayerInput& input, float delta) { } vec3 PlayerController::selectedBlockPosition; +vec3 PlayerController::selectedPointPosition; +vec3 PlayerController::selectedBlockNormal; int PlayerController::selectedBlockId = -1; PlayerController::PlayerController(Level* level, const EngineSettings& settings) @@ -179,6 +181,7 @@ void PlayerController::updateControls(float delta){ player->update(level, input, delta); } +#include void PlayerController::updateInteraction(){ auto contentIds = level->content->indices; Chunks* chunks = level->chunks; @@ -206,6 +209,8 @@ void PlayerController::updateInteraction(){ player->selectedVoxel = *vox; selectedBlockId = vox->id; selectedBlockPosition = iend; + selectedPointPosition = end; + selectedBlockNormal = norm; int x = (int)iend.x; int y = (int)iend.y; int z = (int)iend.z; diff --git a/src/logic/PlayerController.h b/src/logic/PlayerController.h index 27435d72..e9e029fd 100644 --- a/src/logic/PlayerController.h +++ b/src/logic/PlayerController.h @@ -37,6 +37,8 @@ class PlayerController { void updateInteraction(); public: static glm::vec3 selectedBlockPosition; + static glm::vec3 selectedBlockNormal; + static glm::vec3 selectedPointPosition; static int selectedBlockId; PlayerController(Level* level, const EngineSettings& settings); diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index d069b0a7..d7cb67d6 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -230,6 +230,7 @@ voxel* Chunks::rayCast(vec3 start, end.y += dy / float(subs); end.z += dz / float(subs); 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;