mirror of
https://github.com/4jcraft/4jcraft.git
synced 2026-08-20 09:47:11 +00:00
refactor: remove VertexArray
This commit is contained in:
parent
eb7bb6e518
commit
5ba20b155c
|
|
@ -1,18 +1,9 @@
|
||||||
#include "../Platform/stdafx.h"
|
#include "../Platform/stdafx.h"
|
||||||
#include "Models/Model.h"
|
#include "Models/Model.h"
|
||||||
#include "Models/ModelPart.h"
|
#include "Models/ModelPart.h"
|
||||||
|
#include "Rendering/Vertex.h"
|
||||||
#include "Cube.h"
|
#include "Cube.h"
|
||||||
|
#include <array>
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// void Cube::addBox(float x0, float y0, float z0, int w, int h, int d, float g)
|
// 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,
|
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),
|
z0(z0),
|
||||||
x1(x0 + w),
|
x1(x0 + w),
|
||||||
y1(y0 + h),
|
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->x0 = x0;
|
||||||
// this->y0 = y0;
|
// this->y0 = y0;
|
||||||
// this->z0 = z0;
|
// this->z0 = z0;
|
||||||
|
|
@ -33,7 +28,6 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0,
|
||||||
// this->y1 = y0 + h;
|
// this->y1 = y0 + h;
|
||||||
// this->z1 = z0 + d;
|
// this->z1 = z0 + d;
|
||||||
|
|
||||||
vertices = VertexArray(8);
|
|
||||||
polygons = PolygonArray(6);
|
polygons = PolygonArray(6);
|
||||||
|
|
||||||
float x1 = x0 + w;
|
float x1 = x0 + w;
|
||||||
|
|
@ -53,15 +47,15 @@ Cube::Cube(ModelPart* modelPart, int xTexOffs, int yTexOffs, float x0, float y0,
|
||||||
x0 = tmp;
|
x0 = tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vertex* u0 = new Vertex(x0, y0, z0, 0, 0);
|
const Vertex u0 = Vertex(x0, y0, z0, 0, 0);
|
||||||
Vertex* u1 = new Vertex(x1, y0, z0, 0, 8);
|
const Vertex u1 = Vertex(x1, y0, z0, 0, 8);
|
||||||
Vertex* u2 = new Vertex(x1, y1, z0, 8, 8);
|
const Vertex u2 = Vertex(x1, y1, z0, 8, 8);
|
||||||
Vertex* u3 = new Vertex(x0, y1, z0, 8, 0);
|
const Vertex u3 = Vertex(x0, y1, z0, 8, 0);
|
||||||
|
|
||||||
Vertex* l0 = new Vertex(x0, y0, z1, 0, 0);
|
const Vertex l0 = Vertex(x0, y0, z1, 0, 0);
|
||||||
Vertex* l1 = new Vertex(x1, y0, z1, 0, 8);
|
const Vertex l1 = Vertex(x1, y0, z1, 0, 8);
|
||||||
Vertex* l2 = new Vertex(x1, y1, z1, 8, 8);
|
const Vertex l2 = Vertex(x1, y1, z1, 8, 8);
|
||||||
Vertex* l3 = new Vertex(x0, y1, z1, 8, 0);
|
const Vertex l3 = Vertex(x0, y1, z1, 8, 0);
|
||||||
|
|
||||||
vertices[0] = u0;
|
vertices[0] = u0;
|
||||||
vertices[1] = u1;
|
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
|
// 4J - added ability to mask individual faces
|
||||||
int faceCount = 0;
|
int faceCount = 0;
|
||||||
if (faceMask & 1)
|
if (faceMask & 1)
|
||||||
polygons[faceCount++] =
|
polygons[faceCount++] = new _Polygon(
|
||||||
new _Polygon(VertexArray4(l1, u1, u2, l2), xTexOffs + d + w,
|
std::array<const Vertex, 4>{l1, u1, u2, l2}, xTexOffs + d + w,
|
||||||
yTexOffs + d, xTexOffs + d + w + d, yTexOffs + d + h,
|
yTexOffs + d, xTexOffs + d + w + d, yTexOffs + d + h,
|
||||||
modelPart->xTexSize, modelPart->yTexSize); // Right
|
modelPart->xTexSize, modelPart->yTexSize); // Right
|
||||||
if (faceMask & 2)
|
if (faceMask & 2)
|
||||||
polygons[faceCount++] =
|
polygons[faceCount++] = new _Polygon(
|
||||||
new _Polygon(VertexArray4(u0, l0, l3, u3), xTexOffs + 0,
|
std::array<const Vertex, 4>{u0, l0, l3, u3}, xTexOffs + 0,
|
||||||
yTexOffs + d, xTexOffs + d, yTexOffs + d + h,
|
yTexOffs + d, xTexOffs + d, yTexOffs + d + h, modelPart->xTexSize,
|
||||||
modelPart->xTexSize, modelPart->yTexSize); // Left
|
modelPart->yTexSize); // Left
|
||||||
if (faceMask & 4)
|
if (faceMask & 4)
|
||||||
polygons[faceCount++] =
|
polygons[faceCount++] = new _Polygon(
|
||||||
new _Polygon(VertexArray4(l1, l0, u0, u1), xTexOffs + d,
|
std::array<const Vertex, 4>{l1, l0, u0, u1}, xTexOffs + d,
|
||||||
yTexOffs + 0, xTexOffs + d + w, yTexOffs + d,
|
yTexOffs + 0, xTexOffs + d + w, yTexOffs + d, modelPart->xTexSize,
|
||||||
modelPart->xTexSize, modelPart->yTexSize); // Up
|
modelPart->yTexSize); // Up
|
||||||
if (bFlipPoly3UVs) {
|
if (bFlipPoly3UVs) {
|
||||||
if (faceMask & 8)
|
if (faceMask & 8)
|
||||||
polygons[faceCount++] =
|
polygons[faceCount++] = new _Polygon(
|
||||||
new _Polygon(VertexArray4(u2, u3, l3, l2), xTexOffs + d + w,
|
std::array<const Vertex, 4>{u2, u3, l3, l2}, xTexOffs + d + w,
|
||||||
yTexOffs + 0, xTexOffs + d + w + w, yTexOffs + d,
|
yTexOffs + 0, xTexOffs + d + w + w, yTexOffs + d,
|
||||||
modelPart->xTexSize, modelPart->yTexSize); // Down
|
modelPart->xTexSize, modelPart->yTexSize); // Down
|
||||||
} else {
|
} else {
|
||||||
if (faceMask & 8)
|
if (faceMask & 8)
|
||||||
polygons[faceCount++] =
|
polygons[faceCount++] = new _Polygon(
|
||||||
new _Polygon(VertexArray4(u2, u3, l3, l2), xTexOffs + d + w,
|
std::array<const Vertex, 4>{u2, u3, l3, l2}, xTexOffs + d + w,
|
||||||
yTexOffs + d, xTexOffs + d + w + w, yTexOffs + 0,
|
yTexOffs + d, xTexOffs + d + w + w, yTexOffs + 0,
|
||||||
modelPart->xTexSize, modelPart->yTexSize); // Down
|
modelPart->xTexSize, modelPart->yTexSize); // Down
|
||||||
}
|
}
|
||||||
if (faceMask & 16)
|
if (faceMask & 16)
|
||||||
polygons[faceCount++] =
|
polygons[faceCount++] = new _Polygon(
|
||||||
new _Polygon(VertexArray4(u1, u0, u3, u2), xTexOffs + d,
|
std::array<const Vertex, 4>{u1, u0, u3, u2}, xTexOffs + d,
|
||||||
yTexOffs + d, xTexOffs + d + w, yTexOffs + d + h,
|
yTexOffs + d, xTexOffs + d + w, yTexOffs + d + h,
|
||||||
modelPart->xTexSize, modelPart->yTexSize); // Front
|
modelPart->xTexSize, modelPart->yTexSize); // Front
|
||||||
if (faceMask & 32)
|
if (faceMask & 32)
|
||||||
polygons[faceCount++] = new _Polygon(
|
polygons[faceCount++] = new _Polygon(
|
||||||
VertexArray4(l0, l1, l2, l3), xTexOffs + d + w + d, yTexOffs + d,
|
std::array<const Vertex, 4>{l0, l1, l2, l3}, xTexOffs + d + w + d,
|
||||||
xTexOffs + d + w + d + w, yTexOffs + d + h, modelPart->xTexSize,
|
yTexOffs + d, xTexOffs + d + w + d + w, yTexOffs + d + h,
|
||||||
|
modelPart->xTexSize,
|
||||||
modelPart->yTexSize); // Back
|
modelPart->yTexSize); // Back
|
||||||
polygons.length = faceCount;
|
polygons.length = faceCount;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include <array>
|
||||||
#include "../../Minecraft.World/Util/ArrayWithLength.h"
|
#include "../../Minecraft.World/Util/ArrayWithLength.h"
|
||||||
#include "Vertex.h"
|
#include "Vertex.h"
|
||||||
#include "Polygon.h"
|
#include "Polygon.h"
|
||||||
|
|
@ -7,7 +8,7 @@ class Model;
|
||||||
|
|
||||||
class Cube {
|
class Cube {
|
||||||
private:
|
private:
|
||||||
VertexArray vertices;
|
std::array<Vertex, 8> vertices;
|
||||||
PolygonArray polygons;
|
PolygonArray polygons;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
@ -19,11 +20,6 @@ public:
|
||||||
float z0, int w, int h, int d, float g, int faceMask = 63,
|
float z0, int w, int h, int d, float g, int faceMask = 63,
|
||||||
bool bFlipPoly3UVs = false); // 4J - added faceMask
|
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);
|
void render(Tesselator* t, float scale);
|
||||||
Cube* setId(const std::wstring& id);
|
Cube* setId(const std::wstring& id);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,52 +1,44 @@
|
||||||
#include "../Platform/stdafx.h"
|
#include "Rendering/Vertex.h"
|
||||||
#include "Polygon.h"
|
#include "Polygon.h"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <span>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
// 4J added for common init code
|
_Polygon::_Polygon(const std::span<const Vertex> vertices)
|
||||||
void _Polygon::_init(VertexArray vertices) {
|
: vertexCount(vertices.size()), vertices(vertices.begin(), vertices.end()) {}
|
||||||
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, 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
|
// 4J - added - don't assume that u1 > u0, v1 > v0
|
||||||
float us = (u1 > u0) ? (0.1f / xTexSize) : (-0.1f / xTexSize);
|
float us = (u1 > u0) ? (0.1f / xTexSize) : (-0.1f / xTexSize);
|
||||||
float vs = (v1 > v0) ? (0.1f / yTexSize) : (-0.1f / yTexSize);
|
float vs = (v1 > v0) ? (0.1f / yTexSize) : (-0.1f / yTexSize);
|
||||||
|
|
||||||
vertices[0] = vertices[0]->remap(u1 / xTexSize - us, v0 / yTexSize + vs);
|
this->vertices = {
|
||||||
vertices[1] = vertices[1]->remap(u0 / xTexSize + us, v0 / yTexSize + vs);
|
vertices[0].remap(u1 / xTexSize - us, v0 / yTexSize + vs),
|
||||||
vertices[2] = vertices[2]->remap(u0 / xTexSize + us, v1 / yTexSize - vs);
|
vertices[1].remap(u0 / xTexSize + us, v0 / yTexSize + vs),
|
||||||
vertices[3] = vertices[3]->remap(u1 / xTexSize - us, v1 / 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,
|
_Polygon::_Polygon(const std::span<const Vertex, 4> vertices, float u0,
|
||||||
float v1) {
|
float v0, float u1, float v1)
|
||||||
_init(vertices);
|
: vertexCount(vertices.size()),
|
||||||
|
vertices({
|
||||||
vertices[0] = vertices[0]->remap(u1, v0);
|
vertices[0].remap(u1, v0),
|
||||||
vertices[1] = vertices[1]->remap(u0, v0);
|
vertices[1].remap(u0, v0),
|
||||||
vertices[2] = vertices[2]->remap(u0, v1);
|
vertices[2].remap(u0, v1),
|
||||||
vertices[3] = vertices[3]->remap(u1, v1);
|
vertices[3].remap(u1, v1),
|
||||||
}
|
}) {}
|
||||||
|
|
||||||
void _Polygon::mirror() {
|
void _Polygon::mirror() {
|
||||||
VertexArray newVertices = VertexArray(vertices.length);
|
std::reverse(vertices.begin(), vertices.end());
|
||||||
for (unsigned int i = 0; i < vertices.length; i++)
|
|
||||||
newVertices[i] = vertices[vertices.length - i - 1];
|
|
||||||
delete[] vertices.data;
|
|
||||||
vertices = newVertices;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _Polygon::render(Tesselator* t, float scale) {
|
void _Polygon::render(Tesselator* t, float scale) {
|
||||||
Vec3 v0 = vertices[1]->pos.vectorTo(vertices[0]->pos);
|
Vec3 v0 = vertices[1].pos.vectorTo(vertices[0].pos);
|
||||||
Vec3 v1 = vertices[1]->pos.vectorTo(vertices[2]->pos);
|
Vec3 v1 = vertices[1].pos.vectorTo(vertices[2].pos);
|
||||||
Vec3 n = v1.cross(v0).normalize();
|
Vec3 n = v1.cross(v0).normalize();
|
||||||
|
|
||||||
t->begin();
|
t->begin();
|
||||||
|
|
@ -57,10 +49,11 @@ void _Polygon::render(Tesselator* t, float scale) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
for (int i = 0; i < 4; i++) {
|
||||||
Vertex* v = vertices[i];
|
Vertex v = vertices[i];
|
||||||
t->vertexUV((float)(v->pos.x * scale), (float)(v->pos.y * scale),
|
t->vertexUV((float)(v.pos.x * scale), (float)(v.pos.y * scale),
|
||||||
(float)(v->pos.z * scale), (float)(v->u), (float)(v->v));
|
(float)(v.pos.z * scale), (float)(v.u), (float)(v.v));
|
||||||
}
|
}
|
||||||
|
|
||||||
t->end();
|
t->end();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,24 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <span>
|
||||||
|
#include <vector>
|
||||||
#include "Vertex.h"
|
#include "Vertex.h"
|
||||||
#include "Tesselator.h"
|
#include "Tesselator.h"
|
||||||
#include "../../Minecraft.World/Util/ArrayWithLength.h"
|
#include "../../Minecraft.World/Util/ArrayWithLength.h"
|
||||||
|
|
||||||
class _Polygon {
|
class _Polygon {
|
||||||
public:
|
public:
|
||||||
VertexArray vertices;
|
std::vector<Vertex> vertices;
|
||||||
int vertexCount;
|
int vertexCount;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool _flipNormal;
|
bool _flipNormal;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void _init(VertexArray vertices); // 4J added for common init code
|
_Polygon(std::span<const Vertex> vertices);
|
||||||
_Polygon(VertexArray vertices);
|
_Polygon(std::span<const Vertex, 4> vertices, int u0, int v0, int u1,
|
||||||
_Polygon(VertexArray vertices, int u0, int v0, int u1, int v1,
|
int v1, float xTexSize, float yTexSize);
|
||||||
float xTexSize, float yTexSize);
|
_Polygon(std::span<const Vertex, 4> vertices, float u0, float v0, float u1, float v1);
|
||||||
_Polygon(VertexArray vertices, float u0, float v0, float u1, float v1);
|
|
||||||
void mirror();
|
void mirror();
|
||||||
void render(Tesselator* t, float scale);
|
void render(Tesselator* t, float scale);
|
||||||
_Polygon* flipNormal();
|
_Polygon* flipNormal();
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,5 @@
|
||||||
#include "../Platform/stdafx.h"
|
|
||||||
#include "Vertex.h"
|
#include "Vertex.h"
|
||||||
|
|
||||||
Vertex::Vertex(float x, float y, float z, float u, float v) {
|
Vertex Vertex::remap(const float u, const float v) const {
|
||||||
this->pos = Vec3(x, y, z);
|
return Vertex(pos.x, pos.y, pos.z, u, v);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,23 @@ public:
|
||||||
float u, v;
|
float u, v;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Vertex(float x, float y, float z, float u, float v);
|
constexpr Vertex(float x, float y, float z, float u, float v)
|
||||||
Vertex* remap(float u, float v);
|
: pos({x, y, z})
|
||||||
Vertex(Vertex* vertex, float u, float v);
|
, u(u)
|
||||||
Vertex(Vec3* pos, float u, float v);
|
, 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;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,6 @@ typedef arrayWithLength<Tile*> TileArray;
|
||||||
typedef arrayWithLength<Stat*> StatArray;
|
typedef arrayWithLength<Stat*> StatArray;
|
||||||
typedef arrayWithLength<MobCategory*> MobCategoryArray;
|
typedef arrayWithLength<MobCategory*> MobCategoryArray;
|
||||||
typedef arrayWithLength<File*> FileArray;
|
typedef arrayWithLength<File*> FileArray;
|
||||||
typedef arrayWithLength<Vertex*> VertexArray;
|
|
||||||
typedef arrayWithLength<_Polygon*> PolygonArray;
|
typedef arrayWithLength<_Polygon*> PolygonArray;
|
||||||
typedef arrayWithLength<ServerLevel*> ServerLevelArray;
|
typedef arrayWithLength<ServerLevel*> ServerLevelArray;
|
||||||
typedef arrayWithLength<MultiPlayerLevel*> MultiPlayerLevelArray;
|
typedef arrayWithLength<MultiPlayerLevel*> MultiPlayerLevelArray;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue