Merge pull request #64 from A-lex-Ra/main

Рефакторинг (это не много, но это честная работа)
This commit is contained in:
MihailRis 2023-12-18 12:38:24 +03:00 committed by GitHub
commit 98021f533a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 60 additions and 49 deletions

View File

@ -15,7 +15,7 @@
namespace fs = std::filesystem;
ContentLoader::ContentLoader(const ContentPack* pack) : pack(pack) {
ContentLoader::ContentLoader(ContentPack* pack) : pack(pack) {
}
// TODO: add basic validation and logging

View File

@ -11,7 +11,7 @@ class ContentBuilder;
class ContentLoader {
const ContentPack* pack;
public:
ContentLoader(const ContentPack* pack);
ContentLoader(ContentPack* pack);
Block* loadBlock(std::string name, std::filesystem::path file);
void load(ContentBuilder* builder);

View File

@ -125,7 +125,7 @@ HudRenderer::HudRenderer(Engine* engine,
TextBox* box = new TextBox(L"");
box->textSupplier([this, ax]() {
Hitbox* hitbox = this->level->player->hitbox;
return std::to_wstring((int)hitbox->position[ax]);
return std::to_wstring(hitbox->position[ax]);
});
box->textConsumer([this, ax](wstring text) {
try {

View File

@ -139,12 +139,11 @@ void BlocksRenderer::face(const ivec3& coord,
index(0, 1, 2, 0, 2, 3);
}
void BlocksRenderer::face(const ivec3& coord_,
void BlocksRenderer::face(const vec3& coord,
const ivec3& axisX,
const ivec3& axisY,
const ivec3& axisZ,
const ivec3& laxisZ,
const vec3& offset,
float width,
float height,
float depth,
@ -158,8 +157,6 @@ void BlocksRenderer::face(const ivec3& coord_,
const vec3 X(axisX);
const vec3 Y(axisY);
const vec3 Z(axisZ);
vec3 coord(vec3(coord_) + offset);
if (lights) {
const vec3 sunVector = vec3(0.431934f, 0.863868f, 0.259161f);
@ -242,17 +239,21 @@ void BlocksRenderer::blockXSprite(int x, int y, int z,
/* AABB blocks render method (WIP) */
void BlocksRenderer::blockAABB(const ivec3& icoord,
const vec3& offset,
const vec3& size,
const UVRegion(&texfaces)[6],
const Block* block, ubyte rotation,
bool lights) {
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;
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);
ivec3 coord = icoord;
vec3 coord(icoord);
if (block->rotatable) {
auto& rotations = block->rotations;
auto& orient = rotations.variants[rotation];
@ -267,25 +268,25 @@ void BlocksRenderer::blockAABB(const ivec3& icoord,
vec3 fZ(Z);
// TODO: simplify this pile of magic calculations and fix 5th arg (laxisZ)
face(coord, X, Y, Z, Z+loff,
(1.0f - offset.x - size.x) * fX - (offset.z + size.z)*fZ,
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, -X, Y, -Z, Z-Z-X+loff,
(1.0f - offset.x) * fX - (offset.z + size.z) * fZ,
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, X, -Z, Y, Y-Y+loff,
(1.0f - offset.x - size.x) * fX - offset.z * fZ + size.y*fY,
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, -X, -Z, -Y, -X-Y+loff,
(1.0f - offset.x) * fX - offset.z * fZ,
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
face(coord, -Z, Y, X, X-X+loff,
(1.0f - offset.x) * fX - offset.z * fZ,
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, Z, Y, -X, -X-Y+loff,
(1.0f - offset.x - size.x) * fX - (offset.z + size.z) * fZ,
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
}
@ -427,13 +428,7 @@ void BlocksRenderer::render(const voxel* voxels) {
break;
}
case BlockModel::aabb: {
AABB inversedHitbox = def.hitbox;
inversedHitbox.a = vec3(1.0f) - inversedHitbox.a;
inversedHitbox.b = vec3(1.0f) - inversedHitbox.b;
vec3 size = inversedHitbox.size();
vec3 off = inversedHitbox.min();
blockAABB(ivec3(x,y,z), off, size, texfaces, &def, vox.rotation(), !def.rt.emissive);
blockAABB(ivec3(x,y,z), texfaces, &def, vox.rotation(), !def.rt.emissive);
break;
}
default:

View File

@ -64,12 +64,11 @@ class BlocksRenderer {
const glm::ivec3& laxisZ,
const UVRegion& region);
void face(const glm::ivec3& coord,
void face(const glm::vec3& coord,
const glm::ivec3& axisX,
const glm::ivec3& axisY,
const glm::ivec3& axisZ,
const glm::ivec3& laxisZ,
const glm::vec3& offset,
float width,
float height,
float depth,
@ -87,9 +86,7 @@ class BlocksRenderer {
void blockCube(int x, int y, int z, const UVRegion(&faces)[6], ubyte group);
void blockCubeShaded(int x, int y, int z, const UVRegion(&faces)[6], const Block* block, ubyte states);
void blockAABB(const glm::ivec3& coord,
const glm::vec3& offset,
const glm::vec3& size,
void blockAABB(const glm::ivec3& coord,
const UVRegion(&faces)[6],
const Block* block,
ubyte rotation,

View File

@ -4,6 +4,15 @@
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)
{
fix2 = glm::ivec3(0);
if (isVectorHasNegatives(axisX)) fix2 -= axisX;
if (isVectorHasNegatives(axisY)) fix2 -= axisY;
if (isVectorHasNegatives(axisZ)) fix2 -= axisZ;
}
void CoordSystem::transform(AABB& aabb) {
vec3 X(axisX);
vec3 Y(axisY);
@ -14,26 +23,26 @@ void CoordSystem::transform(AABB& aabb) {
aabb.b += fix2;
}
const BlockRotProfile BlockRotProfile::PIPE {"pipe", {//TODO consexpr or init-time fix and fix2 calculations
{ { 1, 0, 0 }, { 0, 0, 1 }, { 0, -1, 0 }, { 0, 0, -1 }, { 0, 1, 0 } }, // North
{ { 0, 0, 1 }, {-1, 0, 0 }, { 0, -1, 0 }, { 1, 0, -1 }, { 1, 1, 0 } }, // East
{ { -1, 0, 0 }, { 0, 0,-1 }, { 0, -1, 0 }, { 1, 0, 0 }, { 1, 1, 1 } }, // South
{ { 0, 0, -1 }, { 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, 0 }, { 0, 1, 1 } }, // West
{ { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }, // Up
{ { 1, 0, 0 }, { 0,-1, 0 }, { 0, 0,-1 }, { 0, 1,-1 }, { 0, 1, 1 } }, // Down
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
}};
const BlockRotProfile BlockRotProfile::PANE {"pane", {
{ { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }, { 0, 0, 0 }, { 0, 0, 0 } }, // North
{ { 0, 0,-1 }, { 0, 1, 0 }, { 1, 0, 0 }, { 1, 0, 0 }, { 0, 0, 1 } }, // East
{ {-1, 0, 0 }, { 0, 1, 0 }, { 0, 0,-1 }, { 1, 0,-1 }, { 1, 0, 1 } }, // South
{ { 0, 0, 1 }, { 0, 1, 0 }, {-1, 0, 0 }, { 0, 0,-1 }, { 1, 0, 0 } }, // West
{ { 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
}};
Block::Block(std::string name)
: name(name),
textureFaces {TEXTURE_NOTFOUND,TEXTURE_NOTFOUND,TEXTURE_NOTFOUND,
TEXTURE_NOTFOUND,TEXTURE_NOTFOUND,TEXTURE_NOTFOUND,} {
TEXTURE_NOTFOUND,TEXTURE_NOTFOUND,TEXTURE_NOTFOUND} {
rotations = BlockRotProfile::PIPE;
}

View File

@ -24,7 +24,17 @@ struct CoordSystem {
glm::ivec3 fix;
glm::ivec3 fix2;
CoordSystem() = default;
CoordSystem(glm::ivec3 axisX, glm::ivec3 axisY, glm::ivec3 axisZ, glm::ivec3 fix);
void transform(AABB& aabb);
static bool isVectorHasNegatives(glm::ivec3 vec) {
if (vec.x < 0 || vec.y < 0 || vec.z < 0) {
return true;
}
else return false;
}
};
struct BlockRotProfile {