implement custom models preview & BlocksRenderer cleanup

This commit is contained in:
MihailRis 2024-11-05 20:39:56 +03:00
parent 8a99c118f6
commit 53d75a975c
3 changed files with 16 additions and 83 deletions

View File

@ -60,47 +60,24 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
}
batch->flush();
break;
case BlockModel::custom:
{
glm::vec3 pmul = glm::vec3(size * 0.63f);
glm::vec3 hitbox = glm::vec3();
// for (const auto& box : def.modelBoxes) {
// hitbox = glm::max(hitbox, box.size());
// }
offset.y += (1.0f - hitbox).y * 0.5f;
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
// for (size_t i = 0; i < def.modelBoxes.size(); i++) {
// const UVRegion (&boxtexfaces)[6] = {
// def.modelUVs[i * 6],
// def.modelUVs[i * 6 + 1],
// def.modelUVs[i * 6 + 2],
// def.modelUVs[i * 6 + 3],
// def.modelUVs[i * 6 + 4],
// def.modelUVs[i * 6 + 5]
// };
// batch->cube(
// def.modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul,
// def.modelBoxes[i].size() * pmul,
// boxtexfaces, glm::vec4(1.0f), !def.rt.emissive
// );
// }
// auto& points = def.modelExtraPoints;
// glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
// for (size_t i = 0; i < def.modelExtraPoints.size() / 4; i++) {
// const UVRegion& reg = def.modelUVs[def.modelBoxes.size() * 6 + i];
// batch->vertex((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
// batch->vertex((points[i * 4 + 1] - poff) * pmul, glm::vec2(reg.u2, reg.v1), glm::vec4(1.0));
// batch->vertex((points[i * 4 + 2] - poff) * pmul, glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
// batch->vertex((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
// batch->vertex((points[i * 4 + 2] - poff) * pmul, glm::vec2(reg.u2, reg.v2), glm::vec4(1.0));
// batch->vertex((points[i * 4 + 3] - poff) * pmul, glm::vec2(reg.u1, reg.v2), glm::vec4(1.0));
// }
// batch->flush();
case BlockModel::custom:{
glm::vec3 pmul = glm::vec3(size * 0.63f);
glm::vec3 hitbox = glm::vec3(1.0f);
glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
offset.y += (1.0f - hitbox).y * 0.5f;
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
const auto& model = cache->getModel(def.rt.id);
for (const auto& mesh : model.meshes) {
for (const auto& vertex : mesh.vertices) {
float d = glm::dot(glm::normalize(vertex.normal), glm::vec3(0.2, 0.8, 0.4));
d = 0.8f + d * 0.2f;
batch->vertex((vertex.coord - poff)*pmul, vertex.uv, glm::vec4(d, d, d, 1.0f));
}
batch->flush();
}
break;
}
case BlockModel::xsprite: {
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
glm::vec3 right = glm::normalize(glm::vec3(1.f, 0.f, -1.f));

View File

@ -181,40 +181,6 @@ void BlocksRenderer::face(
index(0, 1, 2, 0, 2, 3);
}
void BlocksRenderer::tetragonicFace(
const glm::vec3& coord,
const glm::vec3& p1, const glm::vec3& p2,
const glm::vec3& p3, const glm::vec3& p4,
const glm::vec3& X, const glm::vec3& Y, const glm::vec3& Z,
const UVRegion& texreg,
bool lights
) {
const auto fp1 = (p1.x - 0.5f) * X + (p1.y - 0.5f) * Y + (p1.z - 0.5f) * Z;
const auto fp2 = (p2.x - 0.5f) * X + (p2.y - 0.5f) * Y + (p2.z - 0.5f) * Z;
const auto fp3 = (p3.x - 0.5f) * X + (p3.y - 0.5f) * Y + (p3.z - 0.5f) * Z;
const auto fp4 = (p4.x - 0.5f) * X + (p4.y - 0.5f) * Y + (p4.z - 0.5f) * Z;
glm::vec4 tint(1.0f);
if (lights) {
auto dir = glm::cross(fp2 - fp1, fp3 - fp1);
auto normal = glm::normalize(dir);
float d = glm::dot(normal, SUN_VECTOR);
d = 0.8f + d * 0.2f;
tint *= d;
tint *= pickLight(coord);
// debug normal
// tint.x = normal.x * 0.5f + 0.5f;
// tint.y = normal.y * 0.5f + 0.5f;
// tint.z = normal.z * 0.5f + 0.5f;
}
vertex(coord + fp1, texreg.u1, texreg.v1, tint);
vertex(coord + fp2, texreg.u2, texreg.v1, tint);
vertex(coord + fp3, texreg.u2, texreg.v2, tint);
vertex(coord + fp4, texreg.u1, texreg.v2, tint);
index(0, 1, 3, 1, 2, 3);
}
void BlocksRenderer::blockXSprite(
int x, int y, int z,
const glm::vec3& size,

View File

@ -82,16 +82,6 @@ class BlocksRenderer {
const UVRegion& region,
bool lights
);
void tetragonicFace(
const glm::vec3& coord,
const glm::vec3& p1, const glm::vec3& p2,
const glm::vec3& p3, const glm::vec3& p4,
const glm::vec3& X,
const glm::vec3& Y,
const glm::vec3& Z,
const UVRegion& texreg,
bool lights
);
void blockCube(
const glm::ivec3& coord,
const UVRegion(&faces)[6],