Files
omath/source/3d_primitives/box.cpp

57 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// Created by Vlad on 4/18/2025.
//
#include "omath/3d_primitives/box.hpp"
namespace omath::primitives
{
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
const Vector3<float>& dirForward,
const Vector3<float>& dirRight,
const float ratio)
{
const auto height = top.DistTo(bottom);
const auto sideSize = height / ratio;
// corner layout (03 bottom, 47 top)
std::array<Vector3<float>, 8> p;
p[0] = bottom + (dirForward + dirRight) * sideSize; // frontrightbottom
p[1] = bottom + (dirForward - dirRight) * sideSize; // frontleftbottom
p[2] = bottom + (-dirForward + dirRight) * sideSize; // backrightbottom
p[3] = bottom + (-dirForward - dirRight) * sideSize; // backleftbottom
p[4] = top + (dirForward + dirRight) * sideSize; // frontrighttop
p[5] = top + (dirForward - dirRight) * sideSize; // frontlefttop
p[6] = top + (-dirForward + dirRight) * sideSize; // backrighttop
p[7] = top + (-dirForward - dirRight) * sideSize; // backlefttop
std::array<Triangle<Vector3<float>>, 12> poly;
// bottom face (+Y up ⇒ wind CW when viewed from above)
poly[0] = {p[0], p[2], p[3]};
poly[1] = {p[0], p[3], p[1]};
// top face
poly[2] = {p[4], p[7], p[6]};
poly[3] = {p[4], p[5], p[7]};
// front face
poly[4] = {p[0], p[5], p[1]};
poly[5] = {p[0], p[4], p[5]};
// right face
poly[6] = {p[0], p[6], p[2]};
poly[7] = {p[0], p[4], p[6]};
// back face
poly[8] = {p[2], p[7], p[3]};
poly[9] = {p[2], p[6], p[7]};
// left face
poly[10] = {p[1], p[7], p[5]};
poly[11] = {p[1], p[3], p[7]};
return poly;
}
}