refactor: remove VertexArray

This commit is contained in:
orng 2026-03-28 12:53:07 -05:00
parent eb7bb6e518
commit 5ba20b155c
7 changed files with 104 additions and 121 deletions

View file

@ -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 <array>
// 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<const Vertex, 4>{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<const Vertex, 4>{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<const Vertex, 4>{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<const Vertex, 4>{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<const Vertex, 4>{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<const Vertex, 4>{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<const Vertex, 4>{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;

View file

@ -1,4 +1,5 @@
#pragma once
#include <array>
#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<Vertex, 8> 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);
};

View file

@ -1,52 +1,44 @@
#include "../Platform/stdafx.h"
#include "Rendering/Vertex.h"
#include "Polygon.h"
#include <algorithm>
#include <span>
#include <vector>
// 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<const Vertex> vertices)
: vertexCount(vertices.size()), vertices(vertices.begin(), vertices.end()) {}
_Polygon::_Polygon(const std::span<const Vertex, 4> 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<const Vertex, 4> 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();
}

View file

@ -1,22 +1,24 @@
#pragma once
#include <span>
#include <vector>
#include "Vertex.h"
#include "Tesselator.h"
#include "../../Minecraft.World/Util/ArrayWithLength.h"
class _Polygon {
public:
VertexArray vertices;
std::vector<Vertex> 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<const Vertex> vertices);
_Polygon(std::span<const Vertex, 4> vertices, int u0, int v0, int u1,
int v1, float xTexSize, float yTexSize);
_Polygon(std::span<const Vertex, 4> vertices, float u0, float v0, float u1, float v1);
void mirror();
void render(Tesselator* t, float scale);
_Polygon* flipNormal();

View file

@ -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);
}

View file

@ -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;
};

View file

@ -114,7 +114,6 @@ typedef arrayWithLength<Tile*> TileArray;
typedef arrayWithLength<Stat*> StatArray;
typedef arrayWithLength<MobCategory*> MobCategoryArray;
typedef arrayWithLength<File*> FileArray;
typedef arrayWithLength<Vertex*> VertexArray;
typedef arrayWithLength<_Polygon*> PolygonArray;
typedef arrayWithLength<ServerLevel*> ServerLevelArray;
typedef arrayWithLength<MultiPlayerLevel*> MultiPlayerLevelArray;