rename rig to skeleton

This commit is contained in:
MihailRis
2024-07-10 04:28:12 +03:00
parent 91230ecbeb
commit 60f4f33180
24 changed files with 187 additions and 187 deletions
+38 -38
View File
@@ -22,7 +22,7 @@ static debug::Logger logger("entities");
static inline std::string COMP_TRANSFORM = "transform";
static inline std::string COMP_RIGIDBODY = "rigidbody";
static inline std::string COMP_MODELTREE = "modeltree";
static inline std::string COMP_SKELETON = "skeleton";
void Transform::refresh() {
combined = glm::mat4(1.0f);
@@ -38,15 +38,15 @@ void Entity::destroy() {
}
}
rigging::Rig& Entity::getModeltree() const {
return registry.get<rigging::Rig>(entity);
rigging::Skeleton& Entity::getSkeleton() const {
return registry.get<rigging::Skeleton>(entity);
}
void Entity::setRig(const rigging::RigConfig* rigConfig) {
auto& rig = registry.get<rigging::Rig>(entity);
rig.config = rigConfig;
rig.pose.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
rig.calculated.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
void Entity::setRig(const rigging::SkeletonConfig* rigConfig) {
auto& skeleton = registry.get<rigging::Skeleton>(entity);
skeleton.config = rigConfig;
skeleton.pose.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
skeleton.calculated.matrices.resize(rigConfig->getNodes().size(), glm::mat4(1.0f));
}
Entities::Entities(Level* level) : level(level) {
@@ -70,9 +70,9 @@ entityid_t Entities::spawn(
dynamic::Map_sptr saved,
entityid_t uid)
{
auto rig = level->content->getRig(def.rigName);
if (rig == nullptr) {
throw std::runtime_error("rig "+def.rigName+" not found");
auto skeleton = level->content->getRig(def.skeletonName);
if (skeleton == nullptr) {
throw std::runtime_error("skeleton "+def.skeletonName+" not found");
}
auto entity = registry.create();
entityid_t id;
@@ -107,7 +107,7 @@ entityid_t Entities::spawn(
auto& scripting = registry.emplace<ScriptComponents>(entity);
entities[id] = entity;
uids[entity] = id;
registry.emplace<rigging::Rig>(entity, rig->instance());
registry.emplace<rigging::Skeleton>(entity, skeleton->instance());
for (auto& componentName : def.components) {
auto component = std::make_unique<UserComponent>(
componentName, entity_funcs_set {}, nullptr);
@@ -149,7 +149,7 @@ void Entities::loadEntity(const dynamic::Map_sptr& map) {
void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) {
auto& transform = entity.getTransform();
auto& body = entity.getRigidbody();
auto& rig = entity.getModeltree();
auto& skeleton = entity.getSkeleton();
if (auto bodymap = map->map(COMP_RIGIDBODY)) {
dynamic::get_vec(bodymap, "vel", body.hitbox.velocity);
@@ -166,20 +166,20 @@ void Entities::loadEntity(const dynamic::Map_sptr& map, Entity entity) {
dynamic::get_vec(tsfmap, "size", transform.size);
dynamic::get_mat(tsfmap, "rot", transform.rot);
}
std::string rigName = rig.config->getName();
map->str("rig", rigName);
if (rigName != rig.config->getName()) {
rig.config = level->content->getRig(rigName);
std::string skeletonName = skeleton.config->getName();
map->str("skeleton", skeletonName);
if (skeletonName != skeleton.config->getName()) {
skeleton.config = level->content->getRig(skeletonName);
}
if (auto rigmap = map->map(COMP_MODELTREE)) {
if (auto texturesmap = rigmap->map("textures")) {
if (auto skeletonmap = map->map(COMP_SKELETON)) {
if (auto texturesmap = skeletonmap->map("textures")) {
for (auto& [slot, _] : texturesmap->values) {
texturesmap->str(slot, rig.textures[slot]);
texturesmap->str(slot, skeleton.textures[slot]);
}
}
if (auto posearr = rigmap->list("pose")) {
for (size_t i = 0; i < std::min(rig.pose.matrices.size(), posearr->size()); i++) {
dynamic::get_mat(posearr, i, rig.pose.matrices[i]);
if (auto posearr = skeletonmap->list("pose")) {
for (size_t i = 0; i < std::min(skeleton.pose.matrices.size(), posearr->size()); i++) {
dynamic::get_mat(posearr, i, skeleton.pose.matrices[i]);
}
}
}
@@ -237,21 +237,21 @@ dynamic::Value Entities::serialize(const Entity& entity) {
}
}
}
auto& rig = entity.getModeltree();
if (rig.config->getName() != def.rigName) {
root->put("rig", rig.config->getName());
auto& skeleton = entity.getSkeleton();
if (skeleton.config->getName() != def.skeletonName) {
root->put("skeleton", skeleton.config->getName());
}
if (def.save.rig.pose || def.save.rig.textures) {
auto& rigmap = root->putMap(COMP_MODELTREE);
if (def.save.rig.textures) {
auto& map = rigmap.putMap("textures");
for (auto& [slot, texture] : rig.textures) {
if (def.save.skeleton.pose || def.save.skeleton.textures) {
auto& skeletonmap = root->putMap(COMP_SKELETON);
if (def.save.skeleton.textures) {
auto& map = skeletonmap.putMap("textures");
for (auto& [slot, texture] : skeleton.textures) {
map.put(slot, texture);
}
}
if (def.save.rig.pose) {
auto& list = rigmap.putList("pose");
for (auto& mat : rig.pose.matrices) {
if (def.save.skeleton.pose) {
auto& list = skeletonmap.putList("pose");
for (auto& mat : skeleton.pose.matrices) {
list.put(dynamic::to_value(mat));
}
}
@@ -390,16 +390,16 @@ void Entities::render(Assets* assets, ModelBatch& batch, const Frustum& frustum,
scripting::on_entities_render();
}
auto view = registry.view<Transform, rigging::Rig>();
for (auto [entity, transform, rig] : view.each()) {
auto view = registry.view<Transform, rigging::Skeleton>();
for (auto [entity, transform, skeleton] : view.each()) {
if (transform.dirty) {
transform.refresh();
}
const auto& pos = transform.pos;
const auto& size = transform.size;
if (frustum.isBoxVisible(pos-size, pos+size)) {
const auto* rigConfig = rig.config;
rigConfig->render(assets, batch, rig, transform.combined);
const auto* rigConfig = skeleton.config;
rigConfig->render(assets, batch, skeleton, transform.combined);
}
}
}
+4 -4
View File
@@ -94,8 +94,8 @@ class Frustum;
class Entities;
namespace rigging {
struct Rig;
class RigConfig;
struct Skeleton;
class SkeletonConfig;
}
class Entity {
@@ -136,9 +136,9 @@ public:
return registry.get<ScriptComponents>(entity);
}
rigging::Rig& getModeltree() const;
rigging::Skeleton& getSkeleton() const;
void setRig(const rigging::RigConfig* rigConfig);
void setRig(const rigging::SkeletonConfig* rigConfig);
entityid_t getUID() const {
return registry.get<EntityId>(entity).uid;
+4 -4
View File
@@ -10,7 +10,7 @@
#include "../physics/Hitbox.hpp"
namespace rigging {
class RigConfig;
class SkeletonConfig;
}
struct EntityDef {
@@ -23,14 +23,14 @@ struct EntityDef {
glm::vec3 hitbox {0.25f};
std::vector<std::pair<size_t, AABB>> boxSensors {};
std::vector<std::pair<size_t, float>> radialSensors {};
std::string rigName = name;
std::string skeletonName = name;
struct {
bool enabled = true;
struct {
bool textures = false;
bool pose = false;
} rig;
} skeleton;
struct {
bool velocity = true;
bool settings = false;
@@ -39,7 +39,7 @@ struct EntityDef {
struct {
entityid_t id;
rigging::RigConfig* rig;
rigging::SkeletonConfig* skeleton;
} rt {};
EntityDef(const std::string& name) : name(name) {}
+25 -25
View File
@@ -7,18 +7,18 @@
using namespace rigging;
RigNode::RigNode(
Bone::Bone(
size_t index,
std::string name,
std::string model,
std::vector<std::unique_ptr<RigNode>> subnodes)
std::vector<std::unique_ptr<Bone>> bones)
: index(index),
name(std::move(name)),
modelName(std::move(model)),
subnodes(std::move(subnodes))
bones(std::move(bones))
{}
void RigNode::setModel(const std::string& name) {
void Bone::setModel(const std::string& name) {
if (modelName == name) {
return;
}
@@ -26,83 +26,83 @@ void RigNode::setModel(const std::string& name) {
modelUpdated = true;
}
void RigNode::refreshModel(const Assets* assets) {
void Bone::refreshModel(const Assets* assets) {
if (modelUpdated) {
model = assets->get<model::Model>(modelName);
modelUpdated = false;
}
}
static void get_all_nodes(std::vector<RigNode*>& nodes, RigNode* node) {
static void get_all_nodes(std::vector<Bone*>& nodes, Bone* node) {
nodes[node->getIndex()] = node;
for (auto& subnode : node->getSubnodes()) {
get_all_nodes(nodes, subnode.get());
}
}
RigConfig::RigConfig(const std::string& name, std::unique_ptr<RigNode> root, size_t nodesCount)
SkeletonConfig::SkeletonConfig(const std::string& name, std::unique_ptr<Bone> root, size_t nodesCount)
: name(name), root(std::move(root)), nodes(nodesCount) {
get_all_nodes(nodes, this->root.get());
}
size_t RigConfig::update(
size_t SkeletonConfig::update(
size_t index,
Rig& rig,
RigNode* node,
Skeleton& skeleton,
Bone* node,
glm::mat4 matrix) const
{
rig.calculated.matrices[index] = matrix * rig.pose.matrices[index];
skeleton.calculated.matrices[index] = matrix * skeleton.pose.matrices[index];
size_t count = 1;
for (auto& subnode : node->getSubnodes()) {
count += update(index+count, rig, subnode.get(), rig.calculated.matrices[index]);
count += update(index+count, skeleton, subnode.get(), skeleton.calculated.matrices[index]);
}
return count;
}
void RigConfig::update(Rig& rig, glm::mat4 matrix) const {
update(0, rig, root.get(), matrix);
void SkeletonConfig::update(Skeleton& skeleton, glm::mat4 matrix) const {
update(0, skeleton, root.get(), matrix);
}
void RigConfig::render(
void SkeletonConfig::render(
Assets* assets,
ModelBatch& batch,
Rig& rig,
Skeleton& skeleton,
const glm::mat4& matrix) const
{
update(rig, matrix);
update(skeleton, matrix);
for (size_t i = 0; i < nodes.size(); i++) {
auto* node = nodes[i];
node->refreshModel(assets);
if (auto model = node->getModel()) {
batch.pushMatrix(rig.calculated.matrices[i]);
batch.draw(model, &rig.textures);
batch.pushMatrix(skeleton.calculated.matrices[i]);
batch.draw(model, &skeleton.textures);
batch.popMatrix();
}
}
}
static std::tuple<size_t, std::unique_ptr<RigNode>> read_node(
static std::tuple<size_t, std::unique_ptr<Bone>> read_node(
dynamic::Map* root, size_t index
) {
std::string name;
std::string model;
root->str("name", name);
root->str("model", model);
std::vector<std::unique_ptr<RigNode>> subnodes;
std::vector<std::unique_ptr<Bone>> bones;
size_t count = 1;
if (auto nodesList = root->list("nodes")) {
for (size_t i = 0; i < nodesList->size(); i++) {
if (const auto& map = nodesList->map(i)) {
auto [subcount, subNode] = read_node(map.get(), index+count);
subcount += count;
subnodes.push_back(std::move(subNode));
bones.push_back(std::move(subNode));
}
}
}
return {index + count, std::make_unique<RigNode>(index, name, model, std::move(subnodes))};
return {index + count, std::make_unique<Bone>(index, name, model, std::move(bones))};
}
std::unique_ptr<RigConfig> RigConfig::parse(
std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(
std::string_view src,
std::string_view file,
std::string_view name
@@ -113,5 +113,5 @@ std::unique_ptr<RigConfig> RigConfig::parse(
throw std::runtime_error("missing 'root' element");
}
auto [count, rootNode] = read_node(rootNodeMap.get(), 0);
return std::make_unique<RigConfig>(std::string(name), std::move(rootNode), count);
return std::make_unique<SkeletonConfig>(std::string(name), std::move(rootNode), count);
}
+22 -22
View File
@@ -17,8 +17,8 @@ namespace model {
}
namespace rigging {
struct Rig;
class RigConfig;
struct Skeleton;
class SkeletonConfig;
struct Pose {
std::vector<glm::mat4> matrices;
@@ -28,19 +28,19 @@ namespace rigging {
}
};
class RigNode {
class Bone {
size_t index;
std::string name;
std::string modelName;
std::vector<std::unique_ptr<RigNode>> subnodes;
std::vector<std::unique_ptr<Bone>> bones;
model::Model* model = nullptr;
bool modelUpdated = true;
public:
RigNode(
Bone(
size_t index,
std::string name,
std::string model,
std::vector<std::unique_ptr<RigNode>> subnodes);
std::vector<std::unique_ptr<Bone>> bones);
void setModel(const std::string& name);
@@ -59,59 +59,59 @@ namespace rigging {
}
const auto& getSubnodes() const {
return subnodes;
return bones;
}
};
struct Rig {
const RigConfig* config;
struct Skeleton {
const SkeletonConfig* config;
Pose pose;
Pose calculated;
std::unordered_map<std::string, std::string> textures;
};
class RigConfig {
class SkeletonConfig {
std::string name;
std::unique_ptr<RigNode> root;
std::unique_ptr<Bone> root;
std::unordered_map<std::string, size_t> indices;
/// Nodes and indices are ordered from root to subnodes.
/// Nodes and indices are ordered from root to bones.
/// Example:
/// 0 - root
/// 1 --- sub1
/// 2 ----- subsub1
/// 3 --- sub2
std::vector<RigNode*> nodes;
std::vector<Bone*> nodes;
size_t update(
size_t index,
Rig& rig,
RigNode* node,
Skeleton& skeleton,
Bone* node,
glm::mat4 matrix) const;
public:
RigConfig(const std::string& name, std::unique_ptr<RigNode> root,
SkeletonConfig(const std::string& name, std::unique_ptr<Bone> root,
size_t nodesCount);
void update(Rig& rig, glm::mat4 matrix) const;
void update(Skeleton& skeleton, glm::mat4 matrix) const;
void render(
Assets* assets,
ModelBatch& batch,
Rig& rig,
Skeleton& skeleton,
const glm::mat4& matrix) const;
Rig instance() const {
return Rig {
Skeleton instance() const {
return Skeleton {
this, Pose(nodes.size()), Pose(nodes.size()), {}
};
}
static std::unique_ptr<RigConfig> parse(
static std::unique_ptr<SkeletonConfig> parse(
std::string_view src,
std::string_view file,
std::string_view name
);
const std::vector<RigNode*>& getNodes() const {
const std::vector<Bone*>& getNodes() const {
return nodes;
}