66 return absl::FailedPreconditionError(
"ROM is not loaded");
77 for (
int i = 0; i < 2; ++i) {
78 size_t base = i * kPolyEntrySize;
79 if (base + kPolyEntrySize > region.size()) {
84 shape.
name = ShapeNameForIndex(i);
88 static_cast<uint16_t
>(region[base + 2] | (region[base + 3] << 8));
90 static_cast<uint16_t
>(region[base + 4] | (region[base + 5] << 8));
93 const uint32_t vertex_pc = ToPc(shape.
vertex_ptr);
94 const size_t vertex_bytes =
static_cast<size_t>(shape.
vertex_count) * 3;
99 for (
size_t idx = 0; idx + 2 < vertex_blob.size(); idx += 3) {
101 v.
x =
static_cast<int8_t
>(vertex_blob[idx]);
102 v.
y =
static_cast<int8_t
>(vertex_blob[idx + 1]);
103 v.
z =
static_cast<int8_t
>(vertex_blob[idx + 2]);
108 uint32_t face_pc = ToPc(shape.
face_ptr);
115 for (
int j = 0; j < count_byte; ++j) {
121 face.
shade = shade_byte;
122 shape.
faces.push_back(std::move(face));
125 shapes_.push_back(std::move(shape));
131 return absl::OkStatus();
146 std::vector<uint8_t> vertex_blob;
147 vertex_blob.reserve(shape.
vertices.size() * 3);
148 for (
const auto& v : shape.
vertices) {
149 vertex_blob.push_back(
static_cast<uint8_t
>(
static_cast<int8_t
>(v.x)));
150 vertex_blob.push_back(
static_cast<uint8_t
>(
static_cast<int8_t
>(v.y)));
151 vertex_blob.push_back(
static_cast<uint8_t
>(
static_cast<int8_t
>(v.z)));
158 std::vector<uint8_t> face_blob;
159 for (
const auto& face : shape.
faces) {
160 face_blob.push_back(
static_cast<uint8_t
>(face.vertex_indices.size()));
161 for (
auto idx : face.vertex_indices) {
162 face_blob.push_back(idx);
164 face_blob.push_back(face.shade);
173 ImGui::TextUnformatted(
"Load a ROM to edit 3D objects.");
180 ImGui::TextColored(ImVec4(1.0f, 0.3f, 0.3f, 1.0f),
181 "Failed to load shapes: %s", status.message().data());
188 ImGui::Text(
"ALTTP polyhedral data @ $09:%04X (PC $%05X), %u bytes",
189 static_cast<uint16_t
>(kPolyTableSnes & 0xFFFF),
TablePc(),
191 ImGui::TextUnformatted(
192 "Shapes: 0 = Crystal, 1 = Triforce (IDs used by POLYSHAPE)");
198 for (
size_t i = 0; i <
shapes_.size(); ++i) {
200 if (ImGui::Selectable(
shapes_[i].name.c_str(), selected)) {
212 ImGui::TextColored(ImVec4(1.0f, 0.3f, 0.3f, 1.0f),
"Reload failed: %s",
213 status.message().data());
217 ImGui::BeginDisabled(!
dirty_);
221 ImGui::TextColored(ImVec4(1.0f, 0.3f, 0.3f, 1.0f),
"Save failed: %s",
222 status.message().data());
225 ImGui::EndDisabled();
228 ImGui::TextUnformatted(
"No polyhedral shapes found.");
320 ImGui::TextUnformatted(
"No vertices");
324 for (
size_t i = 0; i < shape.
vertices.size(); ++i) {
325 ImGui::PushID(
static_cast<int>(i));
327 std::string label = absl::StrFormat(
"Vertex %zu", i);
328 if (ImGui::Selectable(label.c_str(), is_selected)) {
333 ImGui::SetNextItemWidth(
337 if (ImGui::InputInt3(
"##coords", coords)) {
338 shape.
vertices[i].x = Clamp(coords[0], -127, 127);
339 shape.
vertices[i].y = Clamp(coords[1], -127, 127);
340 shape.
vertices[i].z = Clamp(coords[2], -127, 127);
348 if (shape.
faces.empty()) {
349 ImGui::TextUnformatted(
"No faces");
353 ImGui::TextUnformatted(
"Faces (vertex indices + shade)");
354 for (
size_t i = 0; i < shape.
faces.size(); ++i) {
355 ImGui::PushID(
static_cast<int>(i));
356 ImGui::Text(
"Face %zu", i);
358 int shade = shape.
faces[i].shade;
360 if (ImGui::InputInt(
"Shade##face", &shade, 0, 0)) {
361 shape.
faces[i].shade =
static_cast<uint8_t
>(Clamp(shade, 0, 0xFF));
366 ImGui::TextUnformatted(
"Vertices:");
367 const int max_idx = shape.
vertices.empty()
369 :
static_cast<int>(shape.
vertices.size() - 1);
370 for (
size_t v = 0; v < shape.
faces[i].vertex_indices.size(); ++v) {
372 int idx = shape.
faces[i].vertex_indices[v];
373 ImGui::SetNextItemWidth(
375 if (ImGui::InputInt(absl::StrFormat(
"##v%zu", v).c_str(), &idx, 0, 0)) {
376 idx = Clamp(idx, 0, max_idx);
377 shape.
faces[i].vertex_indices[v] =
static_cast<uint8_t
>(idx);
391 ImVec2 plot_size = ImVec2(-1, 220);
392 ImPlotFlags flags = ImPlotFlags_NoLegend | ImPlotFlags_Equal;
393 if (ImPlot::BeginPlot(label, plot_size, flags)) {
396 ImPlot::SetupAxes(x_label, y_label, ImPlotAxisFlags_AutoFit,
397 ImPlotAxisFlags_AutoFit);
398 ImPlot::SetupAxisLimits(ImAxis_X1, -80, 80, ImGuiCond_Once);
399 ImPlot::SetupAxisLimits(ImAxis_Y1, -80, 80, ImGuiCond_Once);
401 for (
size_t i = 0; i < shape.
vertices.size(); ++i) {
418 ImVec4 color = is_selected ? kSelectedVertexColor : kVertexColor;
420 int point_id =
static_cast<int>(i * 10 +
static_cast<size_t>(plane));
421 if (ImPlot::DragPoint(point_id, &x, &y, color, 6.0f)) {
423 int rounded_x = Clamp(
static_cast<int>(std::lround(x)), -127, 127);
424 int rounded_y = Clamp(
static_cast<int>(std::lround(y)), -127, 127);
456 static float rot_x = 0.35f;
457 static float rot_y = -0.4f;
458 static float rot_z = 0.0f;
459 static float zoom = 1.0f;
461 ImGui::TextUnformatted(
"Preview (orthographic)");
463 ImGui::SliderFloat(
"Rot X", &rot_x, -3.14f, 3.14f,
"%.2f");
466 ImGui::SliderFloat(
"Rot Y", &rot_y, -3.14f, 3.14f,
"%.2f");
469 ImGui::SliderFloat(
"Rot Z", &rot_z, -3.14f, 3.14f,
"%.2f");
472 ImGui::SliderFloat(
"Zoom", &zoom, 0.5f, 3.0f,
"%.2f");
480 std::vector<RotV> rotated(shape.
vertices.size());
482 const double cx = std::cos(rot_x);
483 const double sx = std::sin(rot_x);
484 const double cy = std::cos(rot_y);
485 const double sy = std::sin(rot_y);
486 const double cz = std::cos(rot_z);
487 const double sz = std::sin(rot_z);
489 for (
size_t i = 0; i < shape.
vertices.size(); ++i) {
496 double y1 = y * cx - z * sx;
497 double z1 = y * sx + z * cx;
499 double x2 = x * cy + z1 * sy;
500 double z2 = -x * sy + z1 * cy;
502 double x3 = x2 * cz - y1 * sz;
503 double y3 = x2 * sz + y1 * cz;
505 rotated[i] = {x3 * zoom, y3 * zoom, z2 * zoom};
512 std::vector<FaceDepth> order;
513 order.reserve(shape.
faces.size());
514 for (
size_t i = 0; i < shape.
faces.size(); ++i) {
516 for (
auto idx : shape.
faces[i].vertex_indices) {
517 if (idx < rotated.size()) {
518 accum += rotated[idx].z;
522 shape.
faces[i].vertex_indices.empty()
524 : accum /
static_cast<double>(shape.
faces[i].vertex_indices.size());
525 order.push_back({avg, i});
528 std::sort(order.begin(), order.end(),
529 [](
const FaceDepth& a,
const FaceDepth& b) {
530 return a.depth < b.depth;
533 ImVec2 preview_size(-1, 260);
534 ImPlotFlags flags = ImPlotFlags_NoLegend | ImPlotFlags_Equal;
535 if (ImPlot::BeginPlot(
"PreviewXY", preview_size, flags)) {
536 ImPlot::SetupAxes(
nullptr,
nullptr, ImPlotAxisFlags_NoDecorations,
537 ImPlotAxisFlags_NoDecorations);
538 ImPlot::SetupAxisLimits(ImAxis_X1, -120, 120, ImGuiCond_Always);
539 ImPlot::SetupAxisLimits(ImAxis_Y1, -120, 120, ImGuiCond_Always);
541 ImDrawList* dl = ImPlot::GetPlotDrawList();
542 ImVec4 base_color = ImVec4(0.8f, 0.9f, 1.0f, 0.55f);
544 for (
const auto& fd : order) {
545 const auto& face = shape.
faces[fd.idx];
546 if (face.vertex_indices.size() < 3) {
550 std::vector<ImVec2> pts;
551 pts.reserve(face.vertex_indices.size());
553 for (
auto idx : face.vertex_indices) {
554 if (idx >= rotated.size()) {
557 ImVec2 p = ImPlot::PlotToPixels(rotated[idx].x, rotated[idx].y);
561 if (pts.size() < 3) {
565 ImU32 fill_col = ImGui::GetColorU32(base_color);
566 ImU32 line_col = ImGui::GetColorU32(ImVec4(0.2f, 0.4f, 0.6f, 1.0f));
567 dl->AddConvexPolyFilled(pts.data(),
static_cast<int>(pts.size()),
569 dl->AddPolyline(pts.data(),
static_cast<int>(pts.size()), line_col,
570 ImDrawFlags_Closed, 2.0f);
574 for (
size_t i = 0; i < rotated.size(); ++i) {
575 ImVec2 p = ImPlot::PlotToPixels(rotated[i].x, rotated[i].y);
576 ImU32 col = ImGui::GetColorU32(kVertexColor);
577 dl->AddCircleFilled(p, 4.0f, col);