From 5ba20b155c614bca92a264dbd94c07331c061532 Mon Sep 17 00:00:00 2001 From: orng Date: Sat, 28 Mar 2026 12:53:07 -0500 Subject: [PATCH] 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;