1
0
Fork 0
Univerxel/src/client/contouring/boxing.hpp

59 lines
2.7 KiB
C++

#pragma once
#include <vector>
#include <glm/gtc/matrix_transform.hpp>
#include <meshoptimizer.h>
#include "../data/geometry/Faces.hpp"
#include "../render/buffer/VertexData.hpp"
using namespace geometry;
namespace contouring::box {
static const auto BOX_FACES = 6;
static const std::vector<glm::vec3> g_quad_vertices = {
glm::vec3(0.5f, 0.5f, 0.5f), glm::vec3(0.5f, -0.5f, 0.5f), glm::vec3(0.5f, -0.5f, -0.5f),
glm::vec3(0.5f, -0.5f, -0.5f), glm::vec3(0.5f, 0.5f, -0.5f), glm::vec3(0.5f, 0.5f, 0.5f)};
static const glm::vec3 g_cube_normals[BOX_FACES] = {
glm::vec3(1, 0, 0), glm::vec3(-1, 0, 0), glm::vec3(0, 1, 0), glm::vec3(0, -1, 0), glm::vec3(0, 0, 1), glm::vec3(0, 0, -1)};
static const glm::mat4 g_cube_rotate[BOX_FACES] = {
glm::mat4(1),
glm::rotate<float>(glm::mat4(1), glm::pi<float>(), glm::vec3(0, 1, 0)),
glm::rotate<float>(glm::mat4(1), glm::half_pi<float>(), glm::vec3(0, 0, 1)),
glm::rotate<float>(glm::mat4(1), glm::half_pi<float>(), glm::vec3(0, 0, -1)),
glm::rotate<float>(glm::mat4(1), glm::half_pi<float>(), glm::vec3(0, -1, 0)),
glm::rotate<float>(glm::mat4(1), glm::half_pi<float>(), glm::vec3(0, 1, 0)),
};
static void addQuad(std::vector<buffer::PackedVertexData> & out, glm::vec3 position, ushort texture, Face face, glm::vec3 size) {
for (auto vertex : g_quad_vertices) {
const auto p = glm::vec3(g_cube_rotate[static_cast<int>(face)] * glm::vec4(vertex, 1)) * size + position;
out.emplace_back(meshopt_quantizeHalf(p.x), meshopt_quantizeHalf(p.y),
meshopt_quantizeHalf(p.z), texture,
meshopt_quantizeHalf(g_cube_normals[static_cast<int>(face)].x), meshopt_quantizeHalf(g_cube_normals[static_cast<int>(face)].y),
meshopt_quantizeHalf(g_cube_normals[static_cast<int>(face)].z));
}
}
static void addCube(std::vector<buffer::PackedVertexData>& out, glm::vec3 position, uint texture, Faces faces = Faces::All, glm::vec3 size = glm::vec3(1)) {
if (faces && Faces::Right)
addQuad(out, position, texture, Face::Right, size);
if(faces && Faces::Left)
addQuad(out, position, texture, Face::Left, size);
if (faces && Faces::Up)
addQuad(out, position, texture, Face::Up, size);
if (faces && Faces::Down)
addQuad(out, position, texture, Face::Down, size);
if (faces && Faces::Forward)
addQuad(out, position, texture, Face::Forward, size);
if (faces && Faces::Backward)
addQuad(out, position, texture, Face::Backward, size);
}
}