mirror of
https://github.com/4jcraft/4jcraft.git
synced 2026-04-24 05:53:38 +00:00
63 lines
2 KiB
C++
63 lines
2 KiB
C++
#include "Rendering/Vertex.h"
|
|
#include "Polygon.h"
|
|
#include <algorithm>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
_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);
|
|
|
|
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(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() { 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 n = v1.cross(v0).normalize();
|
|
|
|
t->begin();
|
|
if (_flipNormal) {
|
|
t->normal(-(float)n.x, -(float)n.y, -(float)n.z);
|
|
} else {
|
|
t->normal((float)n.x, (float)n.y, (float)n.z);
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
t->end();
|
|
}
|
|
|
|
_Polygon* _Polygon::flipNormal() {
|
|
_flipNormal = true;
|
|
return this;
|
|
}
|