update vcm::parse semantics

This commit is contained in:
MihailRis 2026-01-15 22:57:04 +03:00
parent c34cb57fb8
commit 6760d16052
4 changed files with 55 additions and 51 deletions

View file

@ -381,8 +381,8 @@ assetload::postfunc assetload::model(
auto text = io::read_string(path);
try {
auto model = vcm::parse(path.string(), text, path.extension() == ".xml")
.release();
auto vcmModel = vcm::parse(path.string(), text, path.extension() == ".xml");
auto model = std::make_unique<model::Model>(std::move(vcmModel.squash())).release();
return [=](Assets* assets) {
request_textures(loader, *model);
assets->store(std::unique_ptr<model::Model>(model), name);

View file

@ -2,7 +2,6 @@
#include "xml.hpp"
#include "util/stringutil.hpp"
#include "objects/rigging.hpp"
#include "io/io.hpp"
#include <vector>
@ -16,6 +15,45 @@ using namespace xml;
using namespace model;
using namespace rigging;
static int calc_offsets(
const Bone& bone, std::vector<glm::vec3>& dst, int index, int depth, int parent
) {
if (depth == 0) {
dst[0] = bone.getOffset();
} else {
dst[index] = dst[parent] + bone.getOffset();
}
const auto& subBones = bone.getSubnodes();
int subIndex = index + 1;
for (int i = 0; i < subBones.size(); i++) {
subIndex += calc_offsets(*subBones[i], dst, subIndex, depth + 1, index);
}
return subIndex - index;
}
model::Model& VcmModel::squash() {
std::vector<glm::vec3> fullOffsets(skeleton->getBones().size());
calc_offsets(*skeleton->getRoot(), fullOffsets, 0, 0, 0);
Model squashed;
for (auto& [name, model] : parts) {
if (auto bone = skeleton->find(name)) {
model.translate(fullOffsets[bone->getIndex()]);
} else {
throw std::runtime_error("invalid state: bones/parts mismatch");
}
squashed.merge(std::move(model));
}
parts = { {"", std::move(squashed)} };
skeleton.reset();
return parts.at("");
}
model::Model VcmModel::squashed() const {
return std::move(VcmModel {parts, std::nullopt}).squash();
}
static const std::unordered_map<std::string, int> side_indices {
{"north", 0},
{"south", 1},
@ -376,13 +414,13 @@ static VcmModel load_model(const xmlelement& root) {
}
vcmModel.parts["root"] = std::move(model);
vcmModel.skeleton = std::make_unique<SkeletonConfig>(
vcmModel.skeleton = SkeletonConfig(
"", std::make_unique<Bone>(std::move(rootBone)), boneIndex
);
return vcmModel;
}
VcmModel vcm::parseFull(
VcmModel vcm::parse(
std::string_view file, std::string_view src, bool usexml
) {
try {
@ -399,41 +437,3 @@ VcmModel vcm::parseFull(
throw std::runtime_error(err.errorLog());
}
}
static int calc_offsets(
const Bone& bone, std::vector<glm::vec3>& dst, int index, int depth, int parent
) {
if (depth == 0) {
dst[0] = bone.getOffset();
} else {
dst[index] = dst[parent] + bone.getOffset();
}
const auto& subBones = bone.getSubnodes();
int subIndex = index + 1;
for (int i = 0; i < subBones.size(); i++) {
subIndex += calc_offsets(*subBones[i], dst, subIndex, depth + 1, index);
}
return subIndex - index;
}
std::unique_ptr<model::Model> vcm::parse(
std::string_view file, std::string_view src, bool usexml
) {
auto vcmModel = parseFull(file, src, usexml);
std::vector<glm::vec3> fullOffsets(vcmModel.skeleton->getBones().size());
calc_offsets(*vcmModel.skeleton->getRoot(), fullOffsets, 0, 0, 0);
Model fullModel;
for (auto& [name, model] : vcmModel.parts) {
if (auto bone = vcmModel.skeleton->find(name)) {
model.translate(fullOffsets[bone->getIndex()]);
} else {
throw std::runtime_error("invalid state: bones/parts mismatch");
}
fullModel.merge(std::move(model));
}
return std::make_unique<Model>(std::move(fullModel));
}

View file

@ -2,9 +2,11 @@
#include <memory>
#include <string>
#include <optional>
#include <unordered_map>
#include "graphics/commons/Model.hpp"
#include "objects/rigging.hpp"
namespace rigging {
class SkeletonConfig;
@ -13,14 +15,13 @@ namespace rigging {
namespace vcm {
struct VcmModel {
std::unordered_map<std::string, model::Model> parts;
std::unique_ptr<rigging::SkeletonConfig> skeleton;
std::optional<rigging::SkeletonConfig> skeleton;
model::Model& squash();
model::Model squashed() const;
};
VcmModel parseFull(
std::string_view file, std::string_view src, bool usexml
);
std::unique_ptr<model::Model> parse(
VcmModel parse(
std::string_view file, std::string_view src, bool usexml
);
}

View file

@ -57,11 +57,14 @@ static int l_parse_model(lua::State* L) {
auto name = lua::require_string(L, 3);
if (format == "xml" || format == "vcm") {
auto vcmModel = vcm::parse(name, string, format == "xml");
engine->getAssets()->store(
vcm::parse(name, string, format == "xml"), name
std::make_unique<model::Model>(std::move(vcmModel.squash())), name
);
} else {
throw std::runtime_error("unknown format " + util::quote(std::string(format)));
throw std::runtime_error(
"unknown format " + util::quote(std::string(format))
);
}
return 0;
}