Kelsidavis-WoWee/tools/editor/cli_box_emitter.cpp
Kelsi 6b5f9938a3 refactor(editor): extract addBox into shared cli_box_emitter
36 procedural mesh primitives in cli_gen_mesh.cpp each open-
coded the same ~30-line lambda for emitting a flat-shaded
axis-aligned box (per-face vertices for unique normals,
6 faces × 4 verts × 2 tris). Hoist the implementation into
cli_box_emitter.{hpp,cpp} as addFlatBox(WoweeModel&, ...)
with two overloads:

  • addFlatBox(wom, cx, cy, cz, hx, hy, hz)
    center + half-extents form, used by 34 primitives
  • addFlatBox(wom, glm::vec3 lo, glm::vec3 hi)
    lo/hi corners form, used by --gen-mesh-archway and
    --gen-mesh-fence which compute corners directly

Each lambda site collapses from ~30 lines to a 3-line thin
wrapper: cli_gen_mesh.cpp drops from 7374 to 6989 lines.
Output bytes verified identical via --info-mesh-stats
(firepit surface area 2.1100 m² unchanged, vertex/triangle
counts match across firepit/canopy/dock/archway/fence).

Future box-based primitives now opt in by including one
header instead of pasting the lambda again.
2026-05-09 11:24:35 -07:00

54 lines
1.8 KiB
C++

#include "cli_box_emitter.hpp"
#include <glm/glm.hpp>
#include <cstdint>
namespace wowee {
namespace editor {
namespace cli {
void addFlatBox(wowee::pipeline::WoweeModel& wom,
float cx, float cy, float cz,
float hx, float hy, float hz) {
struct Face { glm::vec3 n, du, dv; };
const Face faces[6] = {
{{0, 1, 0}, {1, 0, 0}, {0, 0, 1}}, // top (+Y)
{{0,-1, 0}, {1, 0, 0}, {0, 0,-1}}, // bottom (-Y)
{{1, 0, 0}, {0, 0, 1}, {0, 1, 0}}, // right (+X)
{{-1,0, 0}, {0, 0,-1}, {0, 1, 0}}, // left (-X)
{{0, 0, 1}, {-1,0, 0}, {0, 1, 0}}, // front (+Z)
{{0, 0,-1}, {1, 0, 0}, {0, 1, 0}}, // back (-Z)
};
glm::vec3 c(cx, cy, cz);
glm::vec3 ext(hx, hy, hz);
for (const Face& f : faces) {
glm::vec3 center = c + glm::vec3(f.n.x*hx, f.n.y*hy, f.n.z*hz);
glm::vec3 du(f.du.x*ext.x, f.du.y*ext.y, f.du.z*ext.z);
glm::vec3 dv(f.dv.x*ext.x, f.dv.y*ext.y, f.dv.z*ext.z);
uint32_t base = static_cast<uint32_t>(wom.vertices.size());
auto push = [&](glm::vec3 p, float u, float v) {
wowee::pipeline::WoweeModel::Vertex vtx;
vtx.position = p;
vtx.normal = f.n;
vtx.texCoord = {u, v};
wom.vertices.push_back(vtx);
};
push(center - du - dv, 0, 0);
push(center + du - dv, 1, 0);
push(center + du + dv, 1, 1);
push(center - du + dv, 0, 1);
wom.indices.insert(wom.indices.end(),
{base, base + 1, base + 2, base, base + 2, base + 3});
}
}
void addFlatBox(wowee::pipeline::WoweeModel& wom,
glm::vec3 lo, glm::vec3 hi) {
glm::vec3 c = (lo + hi) * 0.5f;
glm::vec3 h = (hi - lo) * 0.5f;
addFlatBox(wom, c.x, c.y, c.z, h.x, h.y, h.z);
}
} // namespace cli
} // namespace editor
} // namespace wowee