From 64f6f1738616f747a5956e4c4f023fb34e329f74 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 5 Dec 2023 10:25:43 +0300 Subject: [PATCH] AABB blocks rotation support (WIP part 3) --- src/content/Content.cpp | 24 ++++++++++-------------- src/graphics/BlocksRenderer.cpp | 6 ------ src/voxels/Block.cpp | 1 + src/voxels/Block.h | 5 +++-- src/voxels/Chunks.cpp | 11 +++++++---- 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/src/content/Content.cpp b/src/content/Content.cpp index 64d986d0..5322a98d 100644 --- a/src/content/Content.cpp +++ b/src/content/Content.cpp @@ -27,20 +27,14 @@ Content* ContentBuilder::build() { // Generating runtime info def->rt.id = blockDefsIndices.size(); def->rt.emissive = *((uint32_t*)def->emission); - - // building hitbox grid 3d for raycasts - const AABB& hitbox = def->hitbox; - for (uint gy = 0; gy < BLOCK_AABB_GRID; gy++) { - for (uint gz = 0; gz < BLOCK_AABB_GRID; gz++) { - for (uint gx = 0; gx < BLOCK_AABB_GRID; gx++) { - float x = gx / float(BLOCK_AABB_GRID); - float y = gy / float(BLOCK_AABB_GRID); - float z = gz / float(BLOCK_AABB_GRID); - bool flag = hitbox.inside({x, y, z}); - if (!flag) - def->rt.solid = false; - def->rt.hitboxGrid[gy][gz][gx] = flag; - } + def->rt.solid = def->model == BlockModel::block; + + if (def->rotatable) { + const AABB& hitbox = def->hitbox; + for (uint i = 0; i < BlockRotProfile::MAX_COUNT; i++) { + AABB aabb = hitbox; + def->rotations.variants[i].transform(aabb); + def->rt.hitboxes[i] = aabb; } } @@ -48,6 +42,8 @@ Content* ContentBuilder::build() { if (groups->find(def->drawGroup) == groups->end()) { groups->insert(def->drawGroup); } + + } ContentIndices* indices = new ContentIndices(blockDefsIndices); return new Content(indices, groups, blockDefs); diff --git a/src/graphics/BlocksRenderer.cpp b/src/graphics/BlocksRenderer.cpp index f9a9f573..fcd72ad8 100644 --- a/src/graphics/BlocksRenderer.cpp +++ b/src/graphics/BlocksRenderer.cpp @@ -372,7 +372,6 @@ vec4 BlocksRenderer::pickSoftLight(float x, float y, float z, return pickSoftLight({int(round(x)), int(round(y)), int(round(z))}, right, up); } -#include void BlocksRenderer::render(const voxel* voxels) { int begin = chunk->bottom * (CHUNK_W * CHUNK_D); int end = chunk->top * (CHUNK_W * CHUNK_D); @@ -410,14 +409,9 @@ void BlocksRenderer::render(const voxel* voxels) { AABB hitbox = def.hitbox; hitbox.a = vec3(1.0f)-hitbox.a; hitbox.b = vec3(1.0f)-hitbox.b; - std::cout << hitbox.a.x << " " << hitbox.a.y << " " << hitbox.a.z << " --- "; - std::cout << hitbox.b.x << " " << hitbox.b.y << " " << hitbox.b.z << std::endl; vec3 size = hitbox.size(); - - std::cout << "s " << size.x << " " << size.y << " " << size.z << std::endl; vec3 off = hitbox.min(); - std::cout << "m " << off.x << " " << off.y << " " << off.z << std::endl; blockCubeShaded(ivec3(x,y,z), off, size, texfaces, &def, vox.states); break; } diff --git a/src/voxels/Block.cpp b/src/voxels/Block.cpp index 085646ed..d794cfbc 100644 --- a/src/voxels/Block.cpp +++ b/src/voxels/Block.cpp @@ -30,4 +30,5 @@ Block::Block(std::string name) Block::Block(std::string name, std::string texture) : name(name), textureFaces{texture,texture,texture,texture,texture,texture} { + rotations = BlockRotProfile::PIPE; } diff --git a/src/voxels/Block.h b/src/voxels/Block.h index fa6002db..8aa90ed1 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -28,7 +28,8 @@ struct CoordSystem { }; struct BlockRotProfile { - CoordSystem variants[16]; + static const int MAX_COUNT = 16; + CoordSystem variants[MAX_COUNT]; /* Wood logs, pillars, pipes 3 orientations supported @@ -65,7 +66,7 @@ public: blockid_t id; bool solid = true; bool emissive = false; - bool hitboxGrid[BLOCK_AABB_GRID][BLOCK_AABB_GRID][BLOCK_AABB_GRID]; + AABB hitboxes[BlockRotProfile::MAX_COUNT]; } rt; Block(std::string name); diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index c98781c6..698e6385 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -73,11 +73,14 @@ const AABB* Chunks::isObstacle(float x, float y, float z){ return nullptr; const Block* def = contentIds->getBlockDef(v->id); if (def->obstacle) { + const AABB& hitbox = def->rotatable + ? def->rt.hitboxes[v->states & BLOCK_ROT_MASK] + : def->hitbox; if (def->rt.solid) { - return &def->hitbox; + return &hitbox; } else { - if (def->hitbox.inside({x - ix, y - iy, z - iz})) - return &def->hitbox; + if (hitbox.inside({x - ix, y - iy, z - iz})) + return &hitbox; return nullptr; } } @@ -222,7 +225,7 @@ voxel* Chunks::rayCast(vec3 start, // TODO: replace this dumb solution with something better if (def && !def->rt.solid) { const int gridSize = BLOCK_AABB_GRID * 2; - const AABB& box = def->hitbox; + const AABB& box = def->rotatable ? def->rt.hitboxes[voxel->states & BLOCK_ROT_MASK] : def->hitbox; const int subs = gridSize; iend = vec3(ix, iy, iz); end -= iend;