MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ generate_box()

MAYAFLUX_API Kakshya::MeshData MayaFlux::Kinesis::generate_box ( const glm::vec3 &  center,
const glm::vec3 &  half_extents,
uint32_t  subdivisions = 1 
)

Generate a solid box as an indexed TRIANGLE_LIST mesh.

Six faces, four vertices each, two triangles per face. Per-face normals point outward along the face axis. UV origin is bottom-left matching Vulkan image layout and the generate_quad convention.

Parameters
centerCentre of the box in world space.
half_extentsHalf-size along each axis. Non-uniform values yield a cuboid.
subdivisionsReserved for future per-face tessellation. Currently ignored.
Returns
MeshData ready for MeshBuffer::set_mesh_data() or MeshWriterNode::set_mesh().

Definition at line 749 of file GeometryPrimitives.cpp.

753{
754 const uint32_t n = std::max(subdivisions, 1U);
755
756 std::vector<Kakshya::MeshVertex> verts;
757 std::vector<uint32_t> indices;
758 verts.reserve(uint32_t(6 * (n + 1) * (n + 1)));
759 indices.reserve(uint32_t(6 * n * n * 6));
760
761 struct FaceDef {
762 glm::vec3 origin;
763 glm::vec3 u_axis;
764 glm::vec3 v_axis;
765 glm::vec3 normal;
766 };
767
768 const std::array<FaceDef, 6> faces = { {
769 { .origin = { -1, -1, 1 }, .u_axis = { 2, 0, 0 }, .v_axis = { 0, 2, 0 }, .normal = { 0, 0, 1 } },
770 { .origin = { 1, -1, -1 }, .u_axis = { -2, 0, 0 }, .v_axis = { 0, 2, 0 }, .normal = { 0, 0, -1 } },
771 { .origin = { 1, -1, 1 }, .u_axis = { 0, 0, -2 }, .v_axis = { 0, 2, 0 }, .normal = { 1, 0, 0 } },
772 { .origin = { -1, -1, -1 }, .u_axis = { 0, 0, 2 }, .v_axis = { 0, 2, 0 }, .normal = { -1, 0, 0 } },
773 { .origin = { -1, 1, 1 }, .u_axis = { 2, 0, 0 }, .v_axis = { 0, 0, -2 }, .normal = { 0, 1, 0 } },
774 { .origin = { -1, -1, -1 }, .u_axis = { 2, 0, 0 }, .v_axis = { 0, 0, 2 }, .normal = { 0, -1, 0 } },
775 } };
776
777 for (const auto& f : faces) {
778 const auto base = static_cast<uint32_t>(verts.size());
779
780 for (uint32_t row = 0; row <= n; ++row) {
781 const float fv = static_cast<float>(row) / static_cast<float>(n);
782 for (uint32_t col = 0; col <= n; ++col) {
783 const float fu = static_cast<float>(col) / static_cast<float>(n);
784 const glm::vec3 p = f.origin + f.u_axis * fu + f.v_axis * fv;
785 verts.push_back({
786 .position = center + p * half_extents,
787 .uv = { fu, 1.0F - fv },
788 .normal = f.normal,
789 });
790 }
791 }
792
793 const uint32_t stride = n + 1;
794 for (uint32_t row = 0; row < n; ++row) {
795 for (uint32_t col = 0; col < n; ++col) {
796 const uint32_t a = base + row * stride + col;
797 const uint32_t b = a + 1;
798 const uint32_t c = a + stride;
799 const uint32_t d = c + 1;
800 indices.insert(indices.end(), { a, b, d, a, d, c });
801 }
802 }
803 }
804
805 auto data = Kakshya::MeshData::empty();
806 Kakshya::MeshInsertion ins(data.vertex_variant, data.index_variant);
807 ins.insert_flat(
808 std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(verts.data()),
809 verts.size() * sizeof(Kakshya::MeshVertex)),
810 std::span<const uint32_t>(indices),
811 Kakshya::VertexLayout::for_meshes(sizeof(Kakshya::MeshVertex)));
812 data.layout = Kakshya::VertexLayout::for_meshes(sizeof(Kakshya::MeshVertex));
813 return data;
814}
size_t a
size_t b

References a, b, generate_box(), and MayaFlux::Kakshya::MeshInsertion::insert_flat().

Referenced by generate_box().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: