mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-12 06:21:50 +00:00
feat: named bones and skeleton building
This commit is contained in:
parent
495e5fa994
commit
c34cb57fb8
6 changed files with 147 additions and 15 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include "xml.hpp"
|
#include "xml.hpp"
|
||||||
#include "util/stringutil.hpp"
|
#include "util/stringutil.hpp"
|
||||||
#include "graphics/commons/Model.hpp"
|
#include "objects/rigging.hpp"
|
||||||
#include "io/io.hpp"
|
#include "io/io.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
using namespace vcm;
|
using namespace vcm;
|
||||||
using namespace xml;
|
using namespace xml;
|
||||||
using namespace model;
|
using namespace model;
|
||||||
|
using namespace rigging;
|
||||||
|
|
||||||
static const std::unordered_map<std::string, int> side_indices {
|
static const std::unordered_map<std::string, int> side_indices {
|
||||||
{"north", 0},
|
{"north", 0},
|
||||||
|
|
@ -42,6 +43,10 @@ public:
|
||||||
calculateMatrix();
|
calculateMatrix();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const glm::mat4& getTransform() const {
|
||||||
|
return combined;
|
||||||
|
}
|
||||||
|
|
||||||
void addBox(
|
void addBox(
|
||||||
const std::string& texture,
|
const std::string& texture,
|
||||||
bool shading,
|
bool shading,
|
||||||
|
|
@ -106,7 +111,13 @@ private:
|
||||||
glm::mat4 combined {1.0f};
|
glm::mat4 combined {1.0f};
|
||||||
};
|
};
|
||||||
|
|
||||||
static void perform_element(const xmlelement& root, ModelBuilder& builder);
|
struct Context {
|
||||||
|
VcmModel& vcmModel;
|
||||||
|
Bone& bone;
|
||||||
|
size_t& boneIndex;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void perform_element(const xmlelement& root, ModelBuilder& builder, Context& ctx);
|
||||||
|
|
||||||
static void perform_rect(const xmlelement& root, ModelBuilder& builder) {
|
static void perform_rect(const xmlelement& root, ModelBuilder& builder) {
|
||||||
auto from = root.attr("from").asVec3();
|
auto from = root.attr("from").asVec3();
|
||||||
|
|
@ -292,7 +303,9 @@ static void perform_box(const xmlelement& root, ModelBuilder& builder) {
|
||||||
builder.pop();
|
builder.pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void perform_bone(const xmlelement& root, ModelBuilder& builder) {
|
static void perform_bone(const xmlelement& root, ModelBuilder& builder, Context& ctx) {
|
||||||
|
std::string name = root.attr("name", "").getText();
|
||||||
|
|
||||||
glm::mat4 tsf(1.0f);
|
glm::mat4 tsf(1.0f);
|
||||||
if (root.has("move")) {
|
if (root.has("move")) {
|
||||||
tsf = glm::translate(tsf, root.attr("move").asVec3());
|
tsf = glm::translate(tsf, root.attr("move").asVec3());
|
||||||
|
|
@ -313,14 +326,29 @@ static void perform_bone(const xmlelement& root, ModelBuilder& builder) {
|
||||||
tsf = glm::scale(tsf, root.attr("scale").asVec3());
|
tsf = glm::scale(tsf, root.attr("scale").asVec3());
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.push(std::move(tsf));
|
if (name.empty()) {
|
||||||
for (const auto& elem : root.getElements()) {
|
builder.push(std::move(tsf));
|
||||||
perform_element(*elem, builder);
|
for (const auto& elem : root.getElements()) {
|
||||||
|
perform_element(*elem, builder, ctx);
|
||||||
|
}
|
||||||
|
builder.pop();
|
||||||
|
} else {
|
||||||
|
glm::vec3 origin = builder.getTransform() * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
|
auto& bone = ctx.bone.addBone(
|
||||||
|
Bone(ctx.boneIndex++, name, name, {}, std::move(origin))
|
||||||
|
);
|
||||||
|
Context boneContext {ctx.vcmModel, bone, ctx.boneIndex};
|
||||||
|
Model boneModel;
|
||||||
|
ModelBuilder boneModelBuilder(boneModel);
|
||||||
|
boneModelBuilder.push(std::move(tsf));
|
||||||
|
for (const auto& elem : root.getElements()) {
|
||||||
|
perform_element(*elem, boneModelBuilder, boneContext);
|
||||||
|
}
|
||||||
|
ctx.vcmModel.parts[std::move(name)] = std::move(boneModel);
|
||||||
}
|
}
|
||||||
builder.pop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void perform_element(const xmlelement& root, ModelBuilder& builder) {
|
static void perform_element(const xmlelement& root, ModelBuilder& builder, Context& ctx) {
|
||||||
auto tag = root.getTag();
|
auto tag = root.getTag();
|
||||||
|
|
||||||
if (tag == "rect") {
|
if (tag == "rect") {
|
||||||
|
|
@ -330,22 +358,31 @@ static void perform_element(const xmlelement& root, ModelBuilder& builder) {
|
||||||
} else if (tag == "tri") {
|
} else if (tag == "tri") {
|
||||||
perform_triangle(root, builder);
|
perform_triangle(root, builder);
|
||||||
} else if (tag == "bone") {
|
} else if (tag == "bone") {
|
||||||
perform_bone(root, builder);
|
perform_bone(root, builder, ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::unique_ptr<model::Model> load_model(const xmlelement& root) {
|
static VcmModel load_model(const xmlelement& root) {
|
||||||
|
VcmModel vcmModel {};
|
||||||
Model model;
|
Model model;
|
||||||
ModelBuilder builder(model);
|
ModelBuilder builder(model);
|
||||||
|
|
||||||
|
size_t boneIndex = 0;
|
||||||
|
Bone rootBone(boneIndex++, "root", "root", {}, glm::vec3(0.0f));
|
||||||
|
Context ctx { vcmModel, rootBone, boneIndex };
|
||||||
|
|
||||||
for (const auto& elem : root.getElements()) {
|
for (const auto& elem : root.getElements()) {
|
||||||
perform_element(*elem, builder);
|
perform_element(*elem, builder, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::make_unique<model::Model>(std::move(model));
|
vcmModel.parts["root"] = std::move(model);
|
||||||
|
vcmModel.skeleton = std::make_unique<SkeletonConfig>(
|
||||||
|
"", std::make_unique<Bone>(std::move(rootBone)), boneIndex
|
||||||
|
);
|
||||||
|
return vcmModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<model::Model> vcm::parse(
|
VcmModel vcm::parseFull(
|
||||||
std::string_view file, std::string_view src, bool usexml
|
std::string_view file, std::string_view src, bool usexml
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
|
|
@ -362,3 +399,41 @@ std::unique_ptr<model::Model> vcm::parse(
|
||||||
throw std::runtime_error(err.errorLog());
|
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));
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,24 @@
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace model {
|
#include "graphics/commons/Model.hpp"
|
||||||
struct Model;
|
|
||||||
|
namespace rigging {
|
||||||
|
class SkeletonConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace vcm {
|
namespace vcm {
|
||||||
|
struct VcmModel {
|
||||||
|
std::unordered_map<std::string, model::Model> parts;
|
||||||
|
std::unique_ptr<rigging::SkeletonConfig> skeleton;
|
||||||
|
};
|
||||||
|
|
||||||
|
VcmModel parseFull(
|
||||||
|
std::string_view file, std::string_view src, bool usexml
|
||||||
|
);
|
||||||
|
|
||||||
std::unique_ptr<model::Model> parse(
|
std::unique_ptr<model::Model> parse(
|
||||||
std::string_view file, std::string_view src, bool usexml
|
std::string_view file, std::string_view src, bool usexml
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -162,6 +162,12 @@ void Mesh::transform(const glm::mat4& matrix) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Mesh::translate(const glm::vec3& offset) {
|
||||||
|
for (auto& vertex : vertices) {
|
||||||
|
vertex.coord += offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Model::clean() {
|
void Model::clean() {
|
||||||
meshes.erase(
|
meshes.erase(
|
||||||
std::remove_if(meshes.begin(), meshes.end(),
|
std::remove_if(meshes.begin(), meshes.end(),
|
||||||
|
|
@ -178,6 +184,12 @@ void Model::transform(const glm::mat4& matrix) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Model::translate(const glm::vec3& offset) {
|
||||||
|
for (auto& mesh : meshes) {
|
||||||
|
mesh.translate(offset);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Model::merge(const Model& source) {
|
void Model::merge(const Model& source) {
|
||||||
for (const auto& srcMesh : source.meshes) {
|
for (const auto& srcMesh : source.meshes) {
|
||||||
auto& dstMesh = addMesh(srcMesh.texture, srcMesh.shading);
|
auto& dstMesh = addMesh(srcMesh.texture, srcMesh.shading);
|
||||||
|
|
@ -188,3 +200,18 @@ void Model::merge(const Model& source) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Model::merge(Model&& source) {
|
||||||
|
for (auto& srcMesh : source.meshes) {
|
||||||
|
auto& dstMesh = addMesh(srcMesh.texture, srcMesh.shading);
|
||||||
|
if (dstMesh.vertices.empty()) {
|
||||||
|
dstMesh = std::move(srcMesh);
|
||||||
|
} else {
|
||||||
|
dstMesh.vertices.insert(
|
||||||
|
dstMesh.vertices.end(),
|
||||||
|
srcMesh.vertices.begin(),
|
||||||
|
srcMesh.vertices.end()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,9 @@ namespace model {
|
||||||
|
|
||||||
/// @brief Apply transformation matrix to vertices coordinates
|
/// @brief Apply transformation matrix to vertices coordinates
|
||||||
void transform(const glm::mat4& matrix);
|
void transform(const glm::mat4& matrix);
|
||||||
|
|
||||||
|
/// @brief Move vertices coordinates by offset
|
||||||
|
void translate(const glm::vec3& offset);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Model {
|
struct Model {
|
||||||
|
|
@ -92,14 +95,22 @@ namespace model {
|
||||||
meshes.push_back({texture, {}, shading});
|
meshes.push_back({texture, {}, shading});
|
||||||
return meshes[meshes.size() - 1];
|
return meshes[meshes.size() - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief Remove all empty meshes
|
/// @brief Remove all empty meshes
|
||||||
void clean();
|
void clean();
|
||||||
|
|
||||||
/// @brief Apply transformation matrix to vertices coordinates
|
/// @brief Apply transformation matrix to vertices coordinates
|
||||||
void transform(const glm::mat4& matrix);
|
void transform(const glm::mat4& matrix);
|
||||||
|
|
||||||
|
/// @brief Move vertices coordinates by offset
|
||||||
|
void translate(const glm::vec3& offset);
|
||||||
|
|
||||||
/// @brief Copy or merge meshes from source model to this.
|
/// @brief Copy or merge meshes from source model to this.
|
||||||
/// Does not remove duplicates
|
/// Does not remove duplicates
|
||||||
void merge(const Model& source);
|
void merge(const Model& source);
|
||||||
|
|
||||||
|
/// @brief Move or merge meshes from source model to this.
|
||||||
|
/// Does not remove duplicates
|
||||||
|
void merge(Model&& source);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,11 @@ Bone::Bone(
|
||||||
model({model, nullptr, true}) {
|
model({model, nullptr, true}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bone& Bone::addBone(Bone&& bone) {
|
||||||
|
bones.push_back(std::make_unique<Bone>(std::move(bone)));
|
||||||
|
return *bones[bones.size() - 1];
|
||||||
|
}
|
||||||
|
|
||||||
void Bone::setModel(const std::string& name) {
|
void Bone::setModel(const std::string& name) {
|
||||||
if (model.name == name) {
|
if (model.name == name) {
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,8 @@ namespace rigging {
|
||||||
const glm::vec3& offset
|
const glm::vec3& offset
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Bone& addBone(Bone&& bone);
|
||||||
|
|
||||||
void setModel(const std::string& name);
|
void setModel(const std::string& name);
|
||||||
|
|
||||||
const std::string& getName() const {
|
const std::string& getName() const {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue