render refactoring

This commit is contained in:
A-lex-Ra 2023-12-17 21:52:00 +06:00
parent 89d9447410
commit 88280f8002
6 changed files with 59 additions and 47 deletions

View File

@ -52,6 +52,7 @@ Engine::Engine(EngineSettings& settings, EnginePaths* paths)
auto resdir = paths->getResources(); auto resdir = paths->getResources();
contentPacks.push_back(ContentPack::read(resdir/path("content/base"))); contentPacks.push_back(ContentPack::read(resdir/path("content/base")));
contentPacks.push_back(ContentPack::read(resdir / path("content/mcclassic")));
loadContent(); loadContent();
Audio::initialize(); Audio::initialize();

View File

@ -125,7 +125,7 @@ HudRenderer::HudRenderer(Engine* engine,
TextBox* box = new TextBox(L""); TextBox* box = new TextBox(L"");
box->textSupplier([this, ax]() { box->textSupplier([this, ax]() {
Hitbox* hitbox = this->level->player->hitbox; 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) { box->textConsumer([this, ax](wstring text) {
try { try {

View File

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

View File

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

View File

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

View File

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