From 5ba20b155c614bca92a264dbd94c07331c061532 Mon Sep 17 00:00:00 2001 From: orng Date: Sat, 28 Mar 2026 12:53:07 -0500 Subject: [PATCH 1/5] refactor: remove `VertexArray` --- Minecraft.Client/Rendering/Cube.cpp | 89 ++++++++++++-------------- Minecraft.Client/Rendering/Cube.h | 8 +-- Minecraft.Client/Rendering/Polygon.cpp | 69 +++++++++----------- Minecraft.Client/Rendering/Polygon.h | 14 ++-- Minecraft.Client/Rendering/Vertex.cpp | 21 +----- Minecraft.Client/Rendering/Vertex.h | 23 +++++-- Minecraft.World/Util/ArrayWithLength.h | 1 - 7 files changed, 104 insertions(+), 121 deletions(-) diff --git a/Minecraft.Client/Rendering/Cube.cpp b/Minecraft.Client/Rendering/Cube.cpp index 259eb9cac..07222aa43 100644 --- a/Minecraft.Client/Rendering/Cube.cpp +++ b/Minecraft.Client/Rendering/Cube.cpp @@ -1,18 +1,9 @@ #include "../Platform/stdafx.h" #include "Models/Model.h" #include "Models/ModelPart.h" +#include "Rendering/Vertex.h" #include "Cube.h" - -// 4J - added - helper function to set up vertex arrays -VertexArray Cube::VertexArray4(Vertex* v0, Vertex* v1, Vertex* v2, Vertex* v3) { - VertexArray ret = VertexArray(4); - ret[0] = v0; - ret[1] = v1; - ret[2] = v2; - ret[3] = v3; - - return ret; -} +#include // void Cube::addBox(float x0, float y0, float z0, int w, int h, int d, float g) Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, @@ -25,7 +16,11 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, z0(z0), x1(x0 + w), y1(y0 + h), - z1(z0 + d) { + z1(z0 + d), + vertices({Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, + Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, + Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, + Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}}) { // this->x0 = x0; // this->y0 = y0; // this->z0 = z0; @@ -33,7 +28,6 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, // this->y1 = y0 + h; // this->z1 = z0 + d; - vertices = VertexArray(8); polygons = PolygonArray(6); float x1 = x0 + w; @@ -53,15 +47,15 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, x0 = tmp; } - Vertex* u0 = new Vertex(x0, y0, z0, 0, 0); - Vertex* u1 = new Vertex(x1, y0, z0, 0, 8); - Vertex* u2 = new Vertex(x1, y1, z0, 8, 8); - Vertex* u3 = new Vertex(x0, y1, z0, 8, 0); + const Vertex u0 = Vertex(x0, y0, z0, 0, 0); + const Vertex u1 = Vertex(x1, y0, z0, 0, 8); + const Vertex u2 = Vertex(x1, y1, z0, 8, 8); + const Vertex u3 = Vertex(x0, y1, z0, 8, 0); - Vertex* l0 = new Vertex(x0, y0, z1, 0, 0); - Vertex* l1 = new Vertex(x1, y0, z1, 0, 8); - Vertex* l2 = new Vertex(x1, y1, z1, 8, 8); - Vertex* l3 = new Vertex(x0, y1, z1, 8, 0); + const Vertex l0 = Vertex(x0, y0, z1, 0, 0); + const Vertex l1 = Vertex(x1, y0, z1, 0, 8); + const Vertex l2 = Vertex(x1, y1, z1, 8, 8); + const Vertex l3 = Vertex(x0, y1, z1, 8, 0); vertices[0] = u0; vertices[1] = u1; @@ -75,42 +69,43 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, // 4J - added ability to mask individual faces int faceCount = 0; if (faceMask & 1) - polygons[faceCount++] = - new _Polygon(VertexArray4(l1, u1, u2, l2), xTexOffs + d + w, - yTexOffs + d, xTexOffs + d + w + d, yTexOffs + d + h, - modelPart->xTexSize, modelPart->yTexSize); // Right + polygons[faceCount++] = new _Polygon( + std::array{l1, u1, u2, l2}, xTexOffs + d + w, + yTexOffs + d, xTexOffs + d + w + d, yTexOffs + d + h, + modelPart->xTexSize, modelPart->yTexSize); // Right if (faceMask & 2) - polygons[faceCount++] = - new _Polygon(VertexArray4(u0, l0, l3, u3), xTexOffs + 0, - yTexOffs + d, xTexOffs + d, yTexOffs + d + h, - modelPart->xTexSize, modelPart->yTexSize); // Left + polygons[faceCount++] = new _Polygon( + std::array{u0, l0, l3, u3}, xTexOffs + 0, + yTexOffs + d, xTexOffs + d, yTexOffs + d + h, modelPart->xTexSize, + modelPart->yTexSize); // Left if (faceMask & 4) - polygons[faceCount++] = - new _Polygon(VertexArray4(l1, l0, u0, u1), xTexOffs + d, - yTexOffs + 0, xTexOffs + d + w, yTexOffs + d, - modelPart->xTexSize, modelPart->yTexSize); // Up + polygons[faceCount++] = new _Polygon( + std::array{l1, l0, u0, u1}, xTexOffs + d, + yTexOffs + 0, xTexOffs + d + w, yTexOffs + d, modelPart->xTexSize, + modelPart->yTexSize); // Up if (bFlipPoly3UVs) { if (faceMask & 8) - polygons[faceCount++] = - new _Polygon(VertexArray4(u2, u3, l3, l2), xTexOffs + d + w, - yTexOffs + 0, xTexOffs + d + w + w, yTexOffs + d, - modelPart->xTexSize, modelPart->yTexSize); // Down + polygons[faceCount++] = new _Polygon( + std::array{u2, u3, l3, l2}, xTexOffs + d + w, + yTexOffs + 0, xTexOffs + d + w + w, yTexOffs + d, + modelPart->xTexSize, modelPart->yTexSize); // Down } else { if (faceMask & 8) - polygons[faceCount++] = - new _Polygon(VertexArray4(u2, u3, l3, l2), xTexOffs + d + w, - yTexOffs + d, xTexOffs + d + w + w, yTexOffs + 0, - modelPart->xTexSize, modelPart->yTexSize); // Down + polygons[faceCount++] = new _Polygon( + std::array{u2, u3, l3, l2}, xTexOffs + d + w, + yTexOffs + d, xTexOffs + d + w + w, yTexOffs + 0, + modelPart->xTexSize, modelPart->yTexSize); // Down } if (faceMask & 16) - polygons[faceCount++] = - new _Polygon(VertexArray4(u1, u0, u3, u2), xTexOffs + d, - yTexOffs + d, xTexOffs + d + w, yTexOffs + d + h, - modelPart->xTexSize, modelPart->yTexSize); // Front + polygons[faceCount++] = new _Polygon( + std::array{u1, u0, u3, u2}, xTexOffs + d, + yTexOffs + d, xTexOffs + d + w, yTexOffs + d + h, + modelPart->xTexSize, modelPart->yTexSize); // Front if (faceMask & 32) polygons[faceCount++] = new _Polygon( - VertexArray4(l0, l1, l2, l3), xTexOffs + d + w + d, yTexOffs + d, - xTexOffs + d + w + d + w, yTexOffs + d + h, modelPart->xTexSize, + std::array{l0, l1, l2, l3}, xTexOffs + d + w + d, + yTexOffs + d, xTexOffs + d + w + d + w, yTexOffs + d + h, + modelPart->xTexSize, modelPart->yTexSize); // Back polygons.length = faceCount; diff --git a/Minecraft.Client/Rendering/Cube.h b/Minecraft.Client/Rendering/Cube.h index 549672e45..1debdc556 100644 --- a/Minecraft.Client/Rendering/Cube.h +++ b/Minecraft.Client/Rendering/Cube.h @@ -1,4 +1,5 @@ #pragma once +#include #include "../../Minecraft.World/Util/ArrayWithLength.h" #include "Vertex.h" #include "Polygon.h" @@ -7,7 +8,7 @@ class Model; class Cube { private: - VertexArray vertices; + std::array vertices; PolygonArray polygons; public: @@ -19,11 +20,6 @@ public: float z0, int w, int h, int d, float g, int faceMask = 63, bool bFlipPoly3UVs = false); // 4J - added faceMask -private: - VertexArray VertexArray4(Vertex* v0, Vertex* v1, Vertex* v2, - Vertex* v3); // 4J added - -public: void render(Tesselator* t, float scale); Cube* setId(const std::wstring& id); }; diff --git a/Minecraft.Client/Rendering/Polygon.cpp b/Minecraft.Client/Rendering/Polygon.cpp index 44d65648b..bcfa26b37 100644 --- a/Minecraft.Client/Rendering/Polygon.cpp +++ b/Minecraft.Client/Rendering/Polygon.cpp @@ -1,52 +1,44 @@ -#include "../Platform/stdafx.h" +#include "Rendering/Vertex.h" #include "Polygon.h" +#include +#include +#include -// 4J added for common init code -void _Polygon::_init(VertexArray vertices) { - vertexCount = 0; - _flipNormal = false; - - this->vertices = vertices; - vertexCount = vertices.length; -} - -_Polygon::_Polygon(VertexArray vertices) { _init(vertices); } - -_Polygon::_Polygon(VertexArray vertices, int u0, int v0, int u1, int v1, - float xTexSize, float yTexSize) { - _init(vertices); +_Polygon::_Polygon(const std::span vertices) + : vertexCount(vertices.size()), vertices(vertices.begin(), vertices.end()) {} +_Polygon::_Polygon(const std::span vertices, int u0, int v0, + int u1, int v1, float xTexSize, float yTexSize) + : vertexCount(vertices.size()) { // 4J - added - don't assume that u1 > u0, v1 > v0 float us = (u1 > u0) ? (0.1f / xTexSize) : (-0.1f / xTexSize); float vs = (v1 > v0) ? (0.1f / yTexSize) : (-0.1f / yTexSize); - vertices[0] = vertices[0]->remap(u1 / xTexSize - us, v0 / yTexSize + vs); - vertices[1] = vertices[1]->remap(u0 / xTexSize + us, v0 / yTexSize + vs); - vertices[2] = vertices[2]->remap(u0 / xTexSize + us, v1 / yTexSize - vs); - vertices[3] = vertices[3]->remap(u1 / xTexSize - us, v1 / yTexSize - vs); + this->vertices = { + vertices[0].remap(u1 / xTexSize - us, v0 / yTexSize + vs), + vertices[1].remap(u0 / xTexSize + us, v0 / yTexSize + vs), + vertices[2].remap(u0 / xTexSize + us, v1 / yTexSize - vs), + vertices[3].remap(u1 / xTexSize - us, v1 / yTexSize - vs), + }; } -_Polygon::_Polygon(VertexArray vertices, float u0, float v0, float u1, - float v1) { - _init(vertices); - - vertices[0] = vertices[0]->remap(u1, v0); - vertices[1] = vertices[1]->remap(u0, v0); - vertices[2] = vertices[2]->remap(u0, v1); - vertices[3] = vertices[3]->remap(u1, v1); -} +_Polygon::_Polygon(const std::span vertices, float u0, + float v0, float u1, float v1) + : vertexCount(vertices.size()), + vertices({ + vertices[0].remap(u1, v0), + vertices[1].remap(u0, v0), + vertices[2].remap(u0, v1), + vertices[3].remap(u1, v1), + }) {} void _Polygon::mirror() { - VertexArray newVertices = VertexArray(vertices.length); - for (unsigned int i = 0; i < vertices.length; i++) - newVertices[i] = vertices[vertices.length - i - 1]; - delete[] vertices.data; - vertices = newVertices; + std::reverse(vertices.begin(), vertices.end()); } void _Polygon::render(Tesselator* t, float scale) { - Vec3 v0 = vertices[1]->pos.vectorTo(vertices[0]->pos); - Vec3 v1 = vertices[1]->pos.vectorTo(vertices[2]->pos); + Vec3 v0 = vertices[1].pos.vectorTo(vertices[0].pos); + Vec3 v1 = vertices[1].pos.vectorTo(vertices[2].pos); Vec3 n = v1.cross(v0).normalize(); t->begin(); @@ -57,10 +49,11 @@ void _Polygon::render(Tesselator* t, float scale) { } for (int i = 0; i < 4; i++) { - Vertex* v = vertices[i]; - t->vertexUV((float)(v->pos.x * scale), (float)(v->pos.y * scale), - (float)(v->pos.z * scale), (float)(v->u), (float)(v->v)); + Vertex v = vertices[i]; + t->vertexUV((float)(v.pos.x * scale), (float)(v.pos.y * scale), + (float)(v.pos.z * scale), (float)(v.u), (float)(v.v)); } + t->end(); } diff --git a/Minecraft.Client/Rendering/Polygon.h b/Minecraft.Client/Rendering/Polygon.h index 38c6e9094..77c4a90a5 100644 --- a/Minecraft.Client/Rendering/Polygon.h +++ b/Minecraft.Client/Rendering/Polygon.h @@ -1,22 +1,24 @@ #pragma once + +#include +#include #include "Vertex.h" #include "Tesselator.h" #include "../../Minecraft.World/Util/ArrayWithLength.h" class _Polygon { public: - VertexArray vertices; + std::vector vertices; int vertexCount; private: bool _flipNormal; public: - void _init(VertexArray vertices); // 4J added for common init code - _Polygon(VertexArray vertices); - _Polygon(VertexArray vertices, int u0, int v0, int u1, int v1, - float xTexSize, float yTexSize); - _Polygon(VertexArray vertices, float u0, float v0, float u1, float v1); + _Polygon(std::span vertices); + _Polygon(std::span vertices, int u0, int v0, int u1, + int v1, float xTexSize, float yTexSize); + _Polygon(std::span vertices, float u0, float v0, float u1, float v1); void mirror(); void render(Tesselator* t, float scale); _Polygon* flipNormal(); diff --git a/Minecraft.Client/Rendering/Vertex.cpp b/Minecraft.Client/Rendering/Vertex.cpp index 7222ed172..b323c0f79 100644 --- a/Minecraft.Client/Rendering/Vertex.cpp +++ b/Minecraft.Client/Rendering/Vertex.cpp @@ -1,22 +1,5 @@ -#include "../Platform/stdafx.h" #include "Vertex.h" -Vertex::Vertex(float x, float y, float z, float u, float v) { - this->pos = Vec3(x, y, z); - this->u = u; - this->v = v; -} - -Vertex* Vertex::remap(float u, float v) { return new Vertex(this, u, v); } - -Vertex::Vertex(Vertex* vertex, float u, float v) { - this->pos = vertex->pos; - this->u = u; - this->v = v; -} - -Vertex::Vertex(Vec3* pos, float u, float v) { - this->pos = *pos; - this->u = u; - this->v = v; +Vertex Vertex::remap(const float u, const float v) const { + return Vertex(pos.x, pos.y, pos.z, u, v); } diff --git a/Minecraft.Client/Rendering/Vertex.h b/Minecraft.Client/Rendering/Vertex.h index 7c4492a8d..7a175011f 100644 --- a/Minecraft.Client/Rendering/Vertex.h +++ b/Minecraft.Client/Rendering/Vertex.h @@ -7,8 +7,23 @@ public: float u, v; public: - Vertex(float x, float y, float z, float u, float v); - Vertex* remap(float u, float v); - Vertex(Vertex* vertex, float u, float v); - Vertex(Vec3* pos, float u, float v); + constexpr Vertex(float x, float y, float z, float u, float v) + : pos({x, y, z}) + , u(u) + , v(v) + {} + + constexpr Vertex(Vertex* vertex, float u, float v) + : pos(vertex->pos) + , u(u) + , v(v) + {} + + constexpr Vertex(Vec3* pos, float u, float v) + : pos(*pos) + , u(u) + , v(v) + {} + + Vertex remap(float u, float v) const; }; diff --git a/Minecraft.World/Util/ArrayWithLength.h b/Minecraft.World/Util/ArrayWithLength.h index d433329f1..8f3668342 100644 --- a/Minecraft.World/Util/ArrayWithLength.h +++ b/Minecraft.World/Util/ArrayWithLength.h @@ -114,7 +114,6 @@ typedef arrayWithLength TileArray; typedef arrayWithLength StatArray; typedef arrayWithLength MobCategoryArray; typedef arrayWithLength FileArray; -typedef arrayWithLength VertexArray; typedef arrayWithLength<_Polygon*> PolygonArray; typedef arrayWithLength ServerLevelArray; typedef arrayWithLength MultiPlayerLevelArray; From f614e661d2001a5ca789fa59a7fd5976e94a9f2e Mon Sep 17 00:00:00 2001 From: orng Date: Sat, 28 Mar 2026 13:34:20 -0500 Subject: [PATCH 2/5] refactor: remove `PolygonArray` --- Minecraft.Client/Rendering/Cube.cpp | 35 ++++++++++---------------- Minecraft.Client/Rendering/Cube.h | 2 +- Minecraft.Client/Rendering/Polygon.h | 1 + Minecraft.World/Util/ArrayWithLength.h | 1 - 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Minecraft.Client/Rendering/Cube.cpp b/Minecraft.Client/Rendering/Cube.cpp index 07222aa43..67222bf94 100644 --- a/Minecraft.Client/Rendering/Cube.cpp +++ b/Minecraft.Client/Rendering/Cube.cpp @@ -20,16 +20,8 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, vertices({Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, - Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}}) { - // this->x0 = x0; - // this->y0 = y0; - // this->z0 = z0; - // this->x1 = x0 + w; - // this->y1 = y0 + h; - // this->z1 = z0 + d; - - polygons = PolygonArray(6); - + Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}}), + polygons({}) { float x1 = x0 + w; float y1 = y0 + h; float z1 = z0 + d; @@ -69,55 +61,54 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, // 4J - added ability to mask individual faces int faceCount = 0; if (faceMask & 1) - polygons[faceCount++] = new _Polygon( + polygons[faceCount++] = _Polygon( std::array{l1, u1, u2, l2}, xTexOffs + d + w, yTexOffs + d, xTexOffs + d + w + d, yTexOffs + d + h, modelPart->xTexSize, modelPart->yTexSize); // Right if (faceMask & 2) - polygons[faceCount++] = new _Polygon( + polygons[faceCount++] = _Polygon( std::array{u0, l0, l3, u3}, xTexOffs + 0, yTexOffs + d, xTexOffs + d, yTexOffs + d + h, modelPart->xTexSize, modelPart->yTexSize); // Left if (faceMask & 4) - polygons[faceCount++] = new _Polygon( + polygons[faceCount++] = _Polygon( std::array{l1, l0, u0, u1}, xTexOffs + d, yTexOffs + 0, xTexOffs + d + w, yTexOffs + d, modelPart->xTexSize, modelPart->yTexSize); // Up if (bFlipPoly3UVs) { if (faceMask & 8) - polygons[faceCount++] = new _Polygon( + polygons[faceCount++] = _Polygon( std::array{u2, u3, l3, l2}, xTexOffs + d + w, yTexOffs + 0, xTexOffs + d + w + w, yTexOffs + d, modelPart->xTexSize, modelPart->yTexSize); // Down } else { if (faceMask & 8) - polygons[faceCount++] = new _Polygon( + polygons[faceCount++] = _Polygon( std::array{u2, u3, l3, l2}, xTexOffs + d + w, yTexOffs + d, xTexOffs + d + w + w, yTexOffs + 0, modelPart->xTexSize, modelPart->yTexSize); // Down } if (faceMask & 16) - polygons[faceCount++] = new _Polygon( + polygons[faceCount++] = _Polygon( std::array{u1, u0, u3, u2}, xTexOffs + d, yTexOffs + d, xTexOffs + d + w, yTexOffs + d + h, modelPart->xTexSize, modelPart->yTexSize); // Front if (faceMask & 32) - polygons[faceCount++] = new _Polygon( + polygons[faceCount++] = _Polygon( std::array{l0, l1, l2, l3}, xTexOffs + d + w + d, yTexOffs + d, xTexOffs + d + w + d + w, yTexOffs + d + h, modelPart->xTexSize, modelPart->yTexSize); // Back - polygons.length = faceCount; if (modelPart->bMirror) { - for (unsigned int i = 0; i < polygons.length; i++) - polygons[i]->mirror(); + for (unsigned int i = 0; i < polygons.size(); i++) + polygons[i].mirror(); } } void Cube::render(Tesselator* t, float scale) { - for (int i = 0; i < polygons.length; i++) { - polygons[i]->render(t, scale); + for (int i = 0; i < polygons.size(); i++) { + polygons[i].render(t, scale); } } diff --git a/Minecraft.Client/Rendering/Cube.h b/Minecraft.Client/Rendering/Cube.h index 1debdc556..1a3974275 100644 --- a/Minecraft.Client/Rendering/Cube.h +++ b/Minecraft.Client/Rendering/Cube.h @@ -9,7 +9,7 @@ class Model; class Cube { private: std::array vertices; - PolygonArray polygons; + std::array<_Polygon, 6> polygons; public: const float x0, y0, z0, x1, y1, z1; diff --git a/Minecraft.Client/Rendering/Polygon.h b/Minecraft.Client/Rendering/Polygon.h index 77c4a90a5..a9ecf5638 100644 --- a/Minecraft.Client/Rendering/Polygon.h +++ b/Minecraft.Client/Rendering/Polygon.h @@ -15,6 +15,7 @@ private: bool _flipNormal; public: + _Polygon() = default; _Polygon(std::span vertices); _Polygon(std::span vertices, int u0, int v0, int u1, int v1, float xTexSize, float yTexSize); diff --git a/Minecraft.World/Util/ArrayWithLength.h b/Minecraft.World/Util/ArrayWithLength.h index 8f3668342..c476a372f 100644 --- a/Minecraft.World/Util/ArrayWithLength.h +++ b/Minecraft.World/Util/ArrayWithLength.h @@ -114,7 +114,6 @@ typedef arrayWithLength TileArray; typedef arrayWithLength StatArray; typedef arrayWithLength MobCategoryArray; typedef arrayWithLength FileArray; -typedef arrayWithLength<_Polygon*> PolygonArray; typedef arrayWithLength ServerLevelArray; typedef arrayWithLength MultiPlayerLevelArray; typedef arrayWithLength LevelArray; From 659b9c32cbde78228e93bd3772c8120861091990 Mon Sep 17 00:00:00 2001 From: orng Date: Sat, 28 Mar 2026 13:50:35 -0500 Subject: [PATCH 3/5] fix: OOB when iterating over all cube faces --- Minecraft.Client/Rendering/Cube.cpp | 4 ++-- Minecraft.Client/Rendering/Cube.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Minecraft.Client/Rendering/Cube.cpp b/Minecraft.Client/Rendering/Cube.cpp index 67222bf94..2291d0923 100644 --- a/Minecraft.Client/Rendering/Cube.cpp +++ b/Minecraft.Client/Rendering/Cube.cpp @@ -59,7 +59,7 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, vertices[7] = l3; // 4J - added ability to mask individual faces - int faceCount = 0; + faceCount = 0; if (faceMask & 1) polygons[faceCount++] = _Polygon( std::array{l1, u1, u2, l2}, xTexOffs + d + w, @@ -107,7 +107,7 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, } void Cube::render(Tesselator* t, float scale) { - for (int i = 0; i < polygons.size(); i++) { + for (int i = 0; i < faceCount; i++) { polygons[i].render(t, scale); } } diff --git a/Minecraft.Client/Rendering/Cube.h b/Minecraft.Client/Rendering/Cube.h index 1a3974275..9075d0950 100644 --- a/Minecraft.Client/Rendering/Cube.h +++ b/Minecraft.Client/Rendering/Cube.h @@ -1,5 +1,6 @@ #pragma once #include +#include #include "../../Minecraft.World/Util/ArrayWithLength.h" #include "Vertex.h" #include "Polygon.h" @@ -10,6 +11,7 @@ class Cube { private: std::array vertices; std::array<_Polygon, 6> polygons; + uint8_t faceCount; public: const float x0, y0, z0, x1, y1, z1; From b630ec8800dbed70a9bec99a830ddd1d2c927f23 Mon Sep 17 00:00:00 2001 From: orng Date: Sun, 29 Mar 2026 18:34:00 -0500 Subject: [PATCH 4/5] fix: Vec3 construct wasn't constexpr --- Minecraft.World/Util/Vec3.cpp | 9 --------- Minecraft.World/Util/Vec3.h | 5 +++-- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/Minecraft.World/Util/Vec3.cpp b/Minecraft.World/Util/Vec3.cpp index 00718fabe..8119ae772 100644 --- a/Minecraft.World/Util/Vec3.cpp +++ b/Minecraft.World/Util/Vec3.cpp @@ -5,15 +5,6 @@ #include "AABB.h" -Vec3::Vec3(double x, double y, double z) { - if (x == -0.0) x = 0.0; - if (y == -0.0) y = 0.0; - if (z == -0.0) z = 0.0; - this->x = x; - this->y = y; - this->z = z; -} - Vec3 Vec3::vectorTo(const Vec3& p) const { return {p.x - x, p.y - y, p.z - z}; } Vec3 Vec3::normalize() const { diff --git a/Minecraft.World/Util/Vec3.h b/Minecraft.World/Util/Vec3.h index bd470f663..a45cbebec 100644 --- a/Minecraft.World/Util/Vec3.h +++ b/Minecraft.World/Util/Vec3.h @@ -9,8 +9,9 @@ class Vec3 { public: double x, y, z; - Vec3() {} - Vec3(double x, double y, double z); + constexpr Vec3() = default; + constexpr Vec3(const double x, const double y, const double z) + : x(x), y(y), z(z) {} Vec3 vectorTo(const Vec3& p) const; Vec3 normalize() const; From 7062ef756474362793f7d267c9ef0944d726e411 Mon Sep 17 00:00:00 2001 From: orng Date: Sun, 29 Mar 2026 18:40:55 -0500 Subject: [PATCH 5/5] chore: fmt --- Minecraft.Client/Rendering/Cube.cpp | 13 ++++++------- Minecraft.Client/Rendering/Polygon.cpp | 7 +++---- Minecraft.Client/Rendering/Polygon.h | 3 ++- Minecraft.Client/Rendering/Vertex.h | 16 +++------------- 4 files changed, 14 insertions(+), 25 deletions(-) diff --git a/Minecraft.Client/Rendering/Cube.cpp b/Minecraft.Client/Rendering/Cube.cpp index 2291d0923..b091fa158 100644 --- a/Minecraft.Client/Rendering/Cube.cpp +++ b/Minecraft.Client/Rendering/Cube.cpp @@ -21,7 +21,7 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}, Vertex{0, 0, 0, 0, 0}}), - polygons({}) { + polygons({}) { float x1 = x0 + w; float y1 = y0 + h; float z1 = z0 + d; @@ -89,10 +89,10 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, modelPart->xTexSize, modelPart->yTexSize); // Down } if (faceMask & 16) - polygons[faceCount++] = _Polygon( - std::array{u1, u0, u3, u2}, xTexOffs + d, - yTexOffs + d, xTexOffs + d + w, yTexOffs + d + h, - modelPart->xTexSize, modelPart->yTexSize); // Front + polygons[faceCount++] = + _Polygon(std::array{u1, u0, u3, u2}, xTexOffs + d, + yTexOffs + d, xTexOffs + d + w, yTexOffs + d + h, + modelPart->xTexSize, modelPart->yTexSize); // Front if (faceMask & 32) polygons[faceCount++] = _Polygon( std::array{l0, l1, l2, l3}, xTexOffs + d + w + d, @@ -101,8 +101,7 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0, modelPart->yTexSize); // Back if (modelPart->bMirror) { - for (unsigned int i = 0; i < polygons.size(); i++) - polygons[i].mirror(); + for (unsigned int i = 0; i < polygons.size(); i++) polygons[i].mirror(); } } diff --git a/Minecraft.Client/Rendering/Polygon.cpp b/Minecraft.Client/Rendering/Polygon.cpp index bcfa26b37..d0de18504 100644 --- a/Minecraft.Client/Rendering/Polygon.cpp +++ b/Minecraft.Client/Rendering/Polygon.cpp @@ -5,7 +5,8 @@ #include _Polygon::_Polygon(const std::span vertices) - : vertexCount(vertices.size()), vertices(vertices.begin(), vertices.end()) {} + : vertexCount(vertices.size()), + vertices(vertices.begin(), vertices.end()) {} _Polygon::_Polygon(const std::span vertices, int u0, int v0, int u1, int v1, float xTexSize, float yTexSize) @@ -32,9 +33,7 @@ _Polygon::_Polygon(const std::span vertices, float u0, vertices[3].remap(u1, v1), }) {} -void _Polygon::mirror() { - std::reverse(vertices.begin(), vertices.end()); -} +void _Polygon::mirror() { std::reverse(vertices.begin(), vertices.end()); } void _Polygon::render(Tesselator* t, float scale) { Vec3 v0 = vertices[1].pos.vectorTo(vertices[0].pos); diff --git a/Minecraft.Client/Rendering/Polygon.h b/Minecraft.Client/Rendering/Polygon.h index a9ecf5638..c56306533 100644 --- a/Minecraft.Client/Rendering/Polygon.h +++ b/Minecraft.Client/Rendering/Polygon.h @@ -19,7 +19,8 @@ public: _Polygon(std::span vertices); _Polygon(std::span vertices, int u0, int v0, int u1, int v1, float xTexSize, float yTexSize); - _Polygon(std::span vertices, float u0, float v0, float u1, float v1); + _Polygon(std::span vertices, float u0, float v0, float u1, + float v1); void mirror(); void render(Tesselator* t, float scale); _Polygon* flipNormal(); diff --git a/Minecraft.Client/Rendering/Vertex.h b/Minecraft.Client/Rendering/Vertex.h index 7a175011f..b0437b9ba 100644 --- a/Minecraft.Client/Rendering/Vertex.h +++ b/Minecraft.Client/Rendering/Vertex.h @@ -8,22 +8,12 @@ public: public: constexpr Vertex(float x, float y, float z, float u, float v) - : pos({x, y, z}) - , u(u) - , v(v) - {} + : pos({x, y, z}), u(u), v(v) {} constexpr Vertex(Vertex* vertex, float u, float v) - : pos(vertex->pos) - , u(u) - , v(v) - {} + : pos(vertex->pos), u(u), v(v) {} - constexpr Vertex(Vec3* pos, float u, float v) - : pos(*pos) - , u(u) - , v(v) - {} + constexpr Vertex(Vec3* pos, float u, float v) : pos(*pos), u(u), v(v) {} Vertex remap(float u, float v) const; };