1089{
1090 profile_segs = std::max(profile_segs, 2U);
1091 radial_segs = std::max(radial_segs, 3U);
1092
1093 std::vector<Kakshya::MeshVertex> verts;
1094 std::vector<uint32_t> indices;
1095 verts.reserve(size_t((profile_segs + 1)) * (radial_segs + 1));
1096 indices.reserve(uint32_t(profile_segs * radial_segs * 6));
1097
1098 for (uint32_t pi = 0; pi <= profile_segs; ++pi) {
1099 const float t = static_cast<float>(pi) / static_cast<float>(profile_segs);
1100 const glm::vec2 p = profile_fn(t);
1101
1102 for (uint32_t ri = 0; ri <= radial_segs; ++ri) {
1103 const float u = static_cast<float>(ri) / static_cast<float>(radial_segs);
1104 const float angle = u * sweep_radians;
1105 const float ca = std::cos(angle);
1106 const float sa = std::sin(angle);
1107
1108 const glm::vec3 pos { p.x * ca, p.y, p.x * sa };
1109
1110 constexpr float eps = 1e-4F;
1111 const glm::vec2 dp = profile_fn(glm::clamp(t + eps, 0.0F, 1.0F))
1112 - profile_fn(glm::clamp(t - eps, 0.0F, 1.0F));
1113 const glm::vec3 profile_tan { dp.x * ca, dp.y, dp.x * sa };
1114 const glm::vec3 radial { ca, 0.0F, sa };
1115 const glm::vec3 nrm = glm::normalize(glm::cross(profile_tan, radial));
1116
1117 verts.push_back({
1118 .position = pos,
1119 .uv = { u, 1.0F - t },
1120 .normal = nrm,
1121 });
1122 }
1123 }
1124
1125 const uint32_t stride = radial_segs + 1;
1126 for (uint32_t pi = 0; pi < profile_segs; ++pi) {
1127 for (uint32_t ri = 0; ri < radial_segs; ++ri) {
1128 const uint32_t
a = pi * stride + ri;
1129 const uint32_t
b =
a + 1;
1130 const uint32_t c =
a + stride;
1131 const uint32_t d = c + 1;
1132 indices.insert(indices.end(), { a, b, c, b, d, c });
1133 }
1134 }
1135
1136 auto data = Kakshya::MeshData::empty();
1137 Kakshya::MeshInsertion ins(data.vertex_variant, data.index_variant);
1138 ins.insert_flat(
1139 std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(verts.data()),
1140 verts.size() * sizeof(Kakshya::MeshVertex)),
1141 std::span<const uint32_t>(indices),
1142 Kakshya::VertexLayout::for_meshes(sizeof(Kakshya::MeshVertex)));
1143 data.layout = Kakshya::VertexLayout::for_meshes(sizeof(Kakshya::MeshVertex));
1144 data.layout.vertex_count = static_cast<uint32_t>(verts.size());
1145 return data;
1146}