From b8d7e89f0be736f64b9824d5f332bd8801474940 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 28 Dec 2023 20:03:25 +0300 Subject: [PATCH] AABB blocks render simplified --- src/graphics/BlocksRenderer.cpp | 59 +++++++++++---------------------- src/voxels/Block.cpp | 38 ++++++++++----------- src/voxels/Block.h | 6 ++-- 3 files changed, 41 insertions(+), 62 deletions(-) diff --git a/src/graphics/BlocksRenderer.cpp b/src/graphics/BlocksRenderer.cpp index 95944eeb..e554a4c7 100644 --- a/src/graphics/BlocksRenderer.cpp +++ b/src/graphics/BlocksRenderer.cpp @@ -243,22 +243,17 @@ void BlocksRenderer::blockXSprite(int x, int y, int z, // HINT: texture faces order: {east, west, bottom, top, south, north} -/* AABB blocks render method (WIP) //FIXME */ +/* AABB blocks render method (WIP) // TODO: fix lights */ void BlocksRenderer::blockAABB(const ivec3& icoord, - const UVRegion(&texfaces)[6], - const Block* block, ubyte rotation, - bool lights) { - AABB inversedHitbox = block->hitbox; - inversedHitbox.a = vec3(1.0f) - inversedHitbox.a; - inversedHitbox.b = vec3(1.0f) - inversedHitbox.b; + const UVRegion(&texfaces)[6], + const Block* block, ubyte rotation, + bool lights) { + AABB hitbox = block->hitbox; + vec3 size = hitbox.size(); - vec3 size = inversedHitbox.size(); - vec3 offset = inversedHitbox.min(); - - ivec3 X(1, 0, 0); - ivec3 Y(0, 1, 0); - ivec3 Z(0, 0, 1); - ivec3 loff(0); + vec3 X(1, 0, 0); + vec3 Y(0, 1, 0); + vec3 Z(0, 0, 1); vec3 coord(icoord); if (block->rotatable) { auto& rotations = block->rotations; @@ -266,35 +261,19 @@ void BlocksRenderer::blockAABB(const ivec3& icoord, X = orient.axisX; Y = orient.axisY; Z = orient.axisZ; - coord += orient.fix; - loff -= orient.fix; + orient.transform(hitbox); } - coord -= vec3(0.5, 0.5f, -0.5f); - vec3 fX(X); - vec3 fY(Y); - vec3 fZ(Z); - - // TODO: simplify this pile of magic calculations and fix 5th arg (laxisZ) - face(coord + (1.0f - offset.x - size.x) * fX - (offset.z + size.z) * fZ, - X, Y, Z, Z+loff, - size.x, size.y, size.z, texfaces[5], lights); // north - face(coord + (1.0f - offset.x) * fX - (offset.z + size.z) * fZ, - -X, Y, -Z, Z-Z-X+loff, - size.x, size.y, 0.0f, texfaces[4], lights); // south - face(coord + (1.0f - offset.x - size.x) * fX - offset.z * fZ + size.y * fY, - X, -Z, Y, Y-Y+loff, - size.x, size.z, 0.0f, texfaces[3], lights); // top - face(coord + (1.0f - offset.x) * fX - offset.z * fZ, - -X, -Z, -Y, -X-Y+loff, - size.x, size.z, 0.0f, texfaces[2], lights); // bottom + coord = vec3(icoord) - vec3(0.5f) + hitbox.center(); - face(coord + (1.0f - offset.x) * fX - offset.z * fZ, - -Z, Y, X, X-X+loff, - size.z, size.y, 0.0f, texfaces[1], lights); // west - face(coord + (1.0f - offset.x - size.x) * fX - (offset.z + size.z) * fZ, - Z, Y, -X, -X-Y+loff, - size.z, size.y, 0.0f, texfaces[0], lights); // east + face(coord, X*size.x, Y*size.y, Z*size.z, texfaces[5]); // north + face(coord, -X*size.x, Y*size.y, -Z*size.z, texfaces[4]); // south + + face(coord, X*size.x, -Z*size.z, Y*size.y, texfaces[3]); // top + face(coord, -X*size.x, -Z*size.z, -Y*size.y, texfaces[2]); // bottom + + face(coord, -Z*size.z, Y*size.y, X*size.x, texfaces[1]); // west + face(coord, Z*size.z, Y*size.y, -X*size.x, texfaces[0]); // east } /* Fastest solid shaded blocks render method */ diff --git a/src/voxels/Block.cpp b/src/voxels/Block.cpp index 389c839b..3b148d98 100644 --- a/src/voxels/Block.cpp +++ b/src/voxels/Block.cpp @@ -4,39 +4,39 @@ using glm::vec3; -CoordSystem::CoordSystem(glm::ivec3 axisX, glm::ivec3 axisY, glm::ivec3 axisZ, glm::ivec3 fix) - : axisX(axisX), axisY(axisY), axisZ(axisZ), fix(fix) +CoordSystem::CoordSystem(glm::ivec3 axisX, glm::ivec3 axisY, glm::ivec3 axisZ) + : axisX(axisX), axisY(axisY), axisZ(axisZ) { - fix2 = glm::ivec3(0); - if (isVectorHasNegatives(axisX)) fix2 -= axisX; - if (isVectorHasNegatives(axisY)) fix2 -= axisY; - if (isVectorHasNegatives(axisZ)) fix2 -= axisZ; + fix = glm::ivec3(0); + if (isVectorHasNegatives(axisX)) fix -= axisX; + if (isVectorHasNegatives(axisY)) fix -= axisY; + if (isVectorHasNegatives(axisZ)) fix -= axisZ; } -void CoordSystem::transform(AABB& aabb) { +void CoordSystem::transform(AABB& aabb) const { vec3 X(axisX); vec3 Y(axisY); vec3 Z(axisZ); aabb.a = X * aabb.a.x + Y * aabb.a.y + Z * aabb.a.z; aabb.b = X * aabb.b.x + Y * aabb.b.y + Z * aabb.b.z; - aabb.a += fix2; - aabb.b += fix2; + aabb.a += fix; + aabb.b += fix; } const BlockRotProfile BlockRotProfile::PIPE {"pipe", {//TODO consexpr or init-time fix calculations - { { 1, 0, 0 }, { 0, 0, 1 }, { 0, -1, 0 }, { 0, 0, -1 }}, // North - { { 0, 0, 1 }, {-1, 0, 0 }, { 0, -1, 0 }, { 1, 0, -1 }}, // East - { { -1, 0, 0 }, { 0, 0,-1 }, { 0, -1, 0 }, { 1, 0, 0 }}, // South - { { 0, 0, -1 }, { 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, 0 }}, // West - { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 0, 0, 0 } }, // Up - { { 1, 0, 0 }, { 0,-1, 0 }, { 0, 0,-1 }, { 0, 1,-1 } }, // Down + { { 1, 0, 0 }, { 0, 0, 1 }, { 0, -1, 0 }}, // North + { { 0, 0, 1 }, {-1, 0, 0 }, { 0, -1, 0 }}, // East + { { -1, 0, 0 }, { 0, 0,-1 }, { 0, -1, 0 }}, // South + { { 0, 0, -1 }, { 1, 0, 0 }, { 0, -1, 0 }}, // West + { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }}, // Up + { { 1, 0, 0 }, { 0,-1, 0 }, { 0, 0,-1 }}, // Down }}; const BlockRotProfile BlockRotProfile::PANE {"pane", { - { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 0, 0, 0 }}, // North - { { 0, 0,-1 }, { 0, 1, 0 }, { 1, 0, 0 }, { 1, 0, 0 }}, // East - { {-1, 0, 0 }, { 0, 1, 0 }, { 0, 0,-1 }, { 1, 0,-1 }}, // South - { { 0, 0, 1 }, { 0, 1, 0 }, {-1, 0, 0 }, { 0, 0,-1 }}, // West + { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }}, // North + { { 0, 0,-1 }, { 0, 1, 0 }, { 1, 0, 0 }}, // East + { {-1, 0, 0 }, { 0, 1, 0 }, { 0, 0,-1 }}, // South + { { 0, 0, 1 }, { 0, 1, 0 }, {-1, 0, 0 }}, // West }}; Block::Block(std::string name) diff --git a/src/voxels/Block.h b/src/voxels/Block.h index ea934081..7ba8ae30 100644 --- a/src/voxels/Block.h +++ b/src/voxels/Block.h @@ -28,14 +28,14 @@ struct CoordSystem { glm::ivec3 axisX; glm::ivec3 axisY; glm::ivec3 axisZ; + // Grid 3d position fix offset (for negative vectors) glm::ivec3 fix; - glm::ivec3 fix2; CoordSystem() = default; - CoordSystem(glm::ivec3 axisX, glm::ivec3 axisY, glm::ivec3 axisZ, glm::ivec3 fix); + CoordSystem(glm::ivec3 axisX, glm::ivec3 axisY, glm::ivec3 axisZ); - void transform(AABB& aabb); + void transform(AABB& aabb) const; static bool isVectorHasNegatives(glm::ivec3 vec) { if (vec.x < 0 || vec.y < 0 || vec.z < 0) {