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

◆ generate_grid()

MAYAFLUX_API Kakshya::MeshData MayaFlux::Kinesis::generate_grid ( const glm::vec3 &  center,
float  extent_x,
float  extent_z,
uint32_t  cols,
uint32_t  rows,
const glm::vec3 &  normal = glm::vec3(0.0F, 1.0F, 0.0F) 
)

Generate a subdivided flat grid in the XZ plane.

Vertices lie at Y = 0. U maps to X, V maps to Z. UV covers [0,1] across the full extent. Normals point along +Y throughout.

Parameters
centerWorld-space centre of the grid.
extent_xTotal width along X.
extent_zTotal depth along Z.
colsNumber of columns (cells along X). Clamped to minimum 1.
rowsNumber of rows (cells along Z). Clamped to minimum 1.
Returns
MeshData ready for TRIANGLE_LIST draw.

Definition at line 892 of file GeometryPrimitives.cpp.

899{
900 cols = std::max(cols, 1U);
901 rows = std::max(rows, 1U);
902
903 const glm::vec3 n = glm::normalize(normal);
904 glm::vec3 u;
905 if (std::abs(n.y) < 0.9F) {
906 u = glm::normalize(glm::cross(n, glm::vec3(0.0F, 1.0F, 0.0F)));
907 } else {
908 u = glm::normalize(glm::cross(n, glm::vec3(1.0F, 0.0F, 0.0F)));
909 }
910 const glm::vec3 v = glm::normalize(glm::cross(u, n));
911
912 const float half_x = extent_x * 0.5F;
913 const float half_z = extent_z * 0.5F;
914
915 std::vector<Kakshya::MeshVertex> verts;
916 std::vector<uint32_t> indices;
917 verts.reserve(uint32_t(2 * (cols + 1) * (rows + 1)));
918 indices.reserve(uint32_t(2 * cols * rows * 6));
919
920 for (uint32_t row = 0; row <= rows; ++row) {
921 const float fv = static_cast<float>(row) / static_cast<float>(rows);
922 for (uint32_t col = 0; col <= cols; ++col) {
923 const float fu = static_cast<float>(col) / static_cast<float>(cols);
924 const glm::vec3 p = center
925 + u * glm::mix(-half_x, half_x, fu)
926 + v * glm::mix(-half_z, half_z, fv);
927 verts.push_back({
928 .position = p,
929 .uv = { fu, 1.0F - fv },
930 .normal = n,
931 });
932 }
933 }
934
935 const uint32_t stride = cols + 1;
936 const auto vert_count = static_cast<uint32_t>(verts.size());
937
938 for (uint32_t row = 0; row < rows; ++row) {
939 for (uint32_t col = 0; col < cols; ++col) {
940 const uint32_t a = row * stride + col;
941 const uint32_t b = a + 1;
942 const uint32_t c = a + stride;
943 const uint32_t d = c + 1;
944 indices.insert(indices.end(), { a, b, c, b, d, c });
945 }
946 }
947
948 for (uint32_t row = 0; row <= rows; ++row) {
949 const float fv = static_cast<float>(row) / static_cast<float>(rows);
950 for (uint32_t col = 0; col <= cols; ++col) {
951 const float fu = static_cast<float>(col) / static_cast<float>(cols);
952 const glm::vec3 p = center
953 + u * glm::mix(-half_x, half_x, fu)
954 + v * glm::mix(-half_z, half_z, fv);
955 verts.push_back({
956 .position = p,
957 .uv = { fu, 1.0F - fv },
958 .normal = -n,
959 });
960 }
961 }
962
963 for (uint32_t row = 0; row < rows; ++row) {
964 for (uint32_t col = 0; col < cols; ++col) {
965 const uint32_t a = vert_count + row * stride + col;
966 const uint32_t b = a + 1;
967 const uint32_t c = a + stride;
968 const uint32_t d = c + 1;
969 indices.insert(indices.end(), { a, c, b, b, c, d });
970 }
971 }
972
973 auto data = Kakshya::MeshData::empty();
974 Kakshya::MeshInsertion ins(data.vertex_variant, data.index_variant);
975 ins.insert_flat(
976 std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(verts.data()),
977 verts.size() * sizeof(Kakshya::MeshVertex)),
978 std::span<const uint32_t>(indices),
979 Kakshya::VertexLayout::for_meshes(sizeof(Kakshya::MeshVertex)));
980 data.layout = Kakshya::VertexLayout::for_meshes(sizeof(Kakshya::MeshVertex));
981 data.layout.vertex_count = static_cast<uint32_t>(verts.size());
982 return data;
983}
uint32_t rows
std::vector< uint32_t > indices
size_t a
size_t b

References a, b, generate_grid(), indices, MayaFlux::Kakshya::MeshInsertion::insert_flat(), and rows.

Referenced by generate_grid().

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