From 60f4f33180e6b742956c439a113c2432d6932f74 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 10 Jul 2024 04:28:12 +0300 Subject: [PATCH] rename rig to skeleton --- res/content/base/entities/falling_block.json | 2 +- res/content/base/scripts/components/drop.lua | 2 +- .../base/scripts/components/falling_block.lua | 2 +- .../base/{rigs => skeletons}/block.json | 0 .../base/{rigs => skeletons}/drop-item.json | 0 .../base/{rigs => skeletons}/drop.json | 0 .../base/{rigs => skeletons}/player.json | 0 res/modules/internal/stdcomp.lua | 16 ++-- src/constants.hpp | 2 +- src/content/Content.cpp | 10 +-- src/content/Content.hpp | 8 +- src/content/ContentBuilder.cpp | 6 +- src/content/ContentBuilder.hpp | 4 +- src/content/ContentLoader.cpp | 14 ++-- src/logic/scripting/lua/api_lua.hpp | 2 +- src/logic/scripting/lua/lib__modeltree.cpp | 56 -------------- src/logic/scripting/lua/lib__skeleton.cpp | 56 ++++++++++++++ src/logic/scripting/lua/libentity.cpp | 6 +- src/logic/scripting/lua/lua_engine.cpp | 2 +- src/objects/Entities.cpp | 76 +++++++++---------- src/objects/Entities.hpp | 8 +- src/objects/EntityDef.hpp | 8 +- src/objects/rigging.cpp | 50 ++++++------ src/objects/rigging.hpp | 44 +++++------ 24 files changed, 187 insertions(+), 187 deletions(-) rename res/content/base/{rigs => skeletons}/block.json (100%) rename res/content/base/{rigs => skeletons}/drop-item.json (100%) rename res/content/base/{rigs => skeletons}/drop.json (100%) rename res/content/base/{rigs => skeletons}/player.json (100%) delete mode 100644 src/logic/scripting/lua/lib__modeltree.cpp create mode 100644 src/logic/scripting/lua/lib__skeleton.cpp diff --git a/res/content/base/entities/falling_block.json b/res/content/base/entities/falling_block.json index ecf269c5..36749323 100644 --- a/res/content/base/entities/falling_block.json +++ b/res/content/base/entities/falling_block.json @@ -2,6 +2,6 @@ "components": [ "falling_block" ], - "rig-name": "base:block", + "skeleton-name": "base:block", "hitbox": [0.8, 0.8, 0.8] } diff --git a/res/content/base/scripts/components/drop.lua b/res/content/base/scripts/components/drop.lua index 95f74cf9..6556fadc 100644 --- a/res/content/base/scripts/components/drop.lua +++ b/res/content/base/scripts/components/drop.lua @@ -1,6 +1,6 @@ local tsf = entity.transform local body = entity.rigidbody -local rig = entity.modeltree +local rig = entity.skeleton inair = true ready = false diff --git a/res/content/base/scripts/components/falling_block.lua b/res/content/base/scripts/components/falling_block.lua index 44a0774c..a4cec674 100644 --- a/res/content/base/scripts/components/falling_block.lua +++ b/res/content/base/scripts/components/falling_block.lua @@ -1,6 +1,6 @@ local tsf = entity.transform local body = entity.rigidbody -local rig = entity.modeltree +local rig = entity.skeleton ARGS = ARGS or {} local blockid = ARGS.block diff --git a/res/content/base/rigs/block.json b/res/content/base/skeletons/block.json similarity index 100% rename from res/content/base/rigs/block.json rename to res/content/base/skeletons/block.json diff --git a/res/content/base/rigs/drop-item.json b/res/content/base/skeletons/drop-item.json similarity index 100% rename from res/content/base/rigs/drop-item.json rename to res/content/base/skeletons/drop-item.json diff --git a/res/content/base/rigs/drop.json b/res/content/base/skeletons/drop.json similarity index 100% rename from res/content/base/rigs/drop.json rename to res/content/base/skeletons/drop.json diff --git a/res/content/base/rigs/player.json b/res/content/base/skeletons/player.json similarity index 100% rename from res/content/base/rigs/player.json rename to res/content/base/skeletons/player.json diff --git a/res/modules/internal/stdcomp.lua b/res/modules/internal/stdcomp.lua index ce85dc4a..17b66835 100644 --- a/res/modules/internal/stdcomp.lua +++ b/res/modules/internal/stdcomp.lua @@ -35,15 +35,15 @@ local function new_Rigidbody(eid) return setmetatable({eid=eid}, Rigidbody) end -local Modeltree = {__index={ - get_model=function(self, i) return __modeltree.get_model(self.eid, i) end, - get_matrix=function(self, i) return __modeltree.get_matrix(self.eid, i) end, - set_matrix=function(self, i, m) return __modeltree.set_matrix(self.eid, i, m) end, - set_texture=function(self, s, s2) return __modeltree.set_texture(self.eid, s, s2) end, +local Skeleton = {__index={ + get_model=function(self, i) return __skeleton.get_model(self.eid, i) end, + get_matrix=function(self, i) return __skeleton.get_matrix(self.eid, i) end, + set_matrix=function(self, i, m) return __skeleton.set_matrix(self.eid, i, m) end, + set_texture=function(self, s, s2) return __skeleton.set_texture(self.eid, s, s2) end, }} -local function new_Modeltree(eid) - return setmetatable({eid=eid}, Modeltree) +local function new_Skeleton(eid) + return setmetatable({eid=eid}, Skeleton) end -- Entity class @@ -63,7 +63,7 @@ return { local entity = setmetatable({eid=eid}, Entity) entity.transform = new_Transform(eid) entity.rigidbody = new_Rigidbody(eid) - entity.modeltree = new_Modeltree(eid) + entity.skeleton = new_Skeleton(eid) entity.components = {} entities[eid] = entity; return entity diff --git a/src/constants.hpp b/src/constants.hpp index 022fa955..bf3d51e5 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -50,6 +50,6 @@ inline const std::string FONTS_FOLDER = "fonts"; inline const std::string LAYOUTS_FOLDER = "layouts"; inline const std::string SOUNDS_FOLDER = "sounds"; inline const std::string MODELS_FOLDER = "models"; -inline const std::string RIGS_FOLDER = "rigs"; +inline const std::string SKELETONS_FOLDER = "skeletons"; #endif // CONSTANTS_HPP_ diff --git a/src/content/Content.cpp b/src/content/Content.cpp index e89f7f5a..0756129f 100644 --- a/src/content/Content.cpp +++ b/src/content/Content.cpp @@ -30,11 +30,11 @@ Content::Content( ContentUnitDefs entities, UptrsMap packs, UptrsMap blockMaterials, - UptrsMap rigs + UptrsMap skeletons ) : indices(std::move(indices)), packs(std::move(packs)), blockMaterials(std::move(blockMaterials)), - rigs(std::move(rigs)), + skeletons(std::move(skeletons)), blocks(std::move(blocks)), items(std::move(items)), entities(std::move(entities)), @@ -44,9 +44,9 @@ Content::Content( Content::~Content() { } -const rigging::RigConfig* Content::getRig(const std::string& id) const { - auto found = rigs.find(id); - if (found == rigs.end()) { +const rigging::SkeletonConfig* Content::getRig(const std::string& id) const { + auto found = skeletons.find(id); + if (found == skeletons.end()) { return nullptr; } return found->second.get(); diff --git a/src/content/Content.hpp b/src/content/Content.hpp index 3e2c7a9d..f64e063e 100644 --- a/src/content/Content.hpp +++ b/src/content/Content.hpp @@ -22,7 +22,7 @@ class Content; class ContentPackRuntime; namespace rigging { - class RigConfig; + class SkeletonConfig; } enum class contenttype { @@ -116,7 +116,7 @@ class Content { std::unique_ptr indices; UptrsMap packs; UptrsMap blockMaterials; - UptrsMap rigs; + UptrsMap skeletons; public: ContentUnitDefs blocks; ContentUnitDefs items; @@ -131,7 +131,7 @@ public: ContentUnitDefs entities, UptrsMap packs, UptrsMap blockMaterials, - UptrsMap rigs + UptrsMap skeletons ); ~Content(); @@ -139,7 +139,7 @@ public: return indices.get(); } - const rigging::RigConfig* getRig(const std::string& id) const; + const rigging::SkeletonConfig* getRig(const std::string& id) const; const BlockMaterial* findBlockMaterial(const std::string& id) const; const ContentPackRuntime* getPackRuntime(const std::string& id) const; diff --git a/src/content/ContentBuilder.cpp b/src/content/ContentBuilder.cpp index c6863e9e..a5392679 100644 --- a/src/content/ContentBuilder.cpp +++ b/src/content/ContentBuilder.cpp @@ -8,8 +8,8 @@ void ContentBuilder::add(std::unique_ptr pack) { packs[pack->getId()] = std::move(pack); } -void ContentBuilder::add(std::unique_ptr rig) { - rigs[rig->getName()] = std::move(rig); +void ContentBuilder::add(std::unique_ptr skeleton) { + skeletons[skeleton->getName()] = std::move(skeleton); } BlockMaterial& ContentBuilder::createBlockMaterial(const std::string& id) { @@ -75,7 +75,7 @@ std::unique_ptr ContentBuilder::build() { entities.build(), std::move(packs), std::move(blockMaterials), - std::move(rigs) + std::move(skeletons) ); // Now, it's time to resolve foreign keys diff --git a/src/content/ContentBuilder.hpp b/src/content/ContentBuilder.hpp index 2b7b5ed0..601b57e1 100644 --- a/src/content/ContentBuilder.hpp +++ b/src/content/ContentBuilder.hpp @@ -50,7 +50,7 @@ public: class ContentBuilder { UptrsMap blockMaterials; - UptrsMap rigs; + UptrsMap skeletons; UptrsMap packs; std::unordered_map allNames; public: @@ -61,7 +61,7 @@ public: ~ContentBuilder(); void add(std::unique_ptr pack); - void add(std::unique_ptr rig); + void add(std::unique_ptr skeleton); BlockMaterial& createBlockMaterial(const std::string& id); diff --git a/src/content/ContentLoader.cpp b/src/content/ContentLoader.cpp index 1c7c659b..dac39bc4 100644 --- a/src/content/ContentLoader.cpp +++ b/src/content/ContentLoader.cpp @@ -336,8 +336,8 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs } } root->flag("save", def.save.enabled); - root->flag("save-rig-pose", def.save.rig.pose); - root->flag("save-rig-textures", def.save.rig.textures); + root->flag("save-skeleton-pose", def.save.skeleton.pose); + root->flag("save-skeleton-textures", def.save.skeleton.textures); root->flag("save-body-velocity", def.save.body.velocity); root->flag("save-body-settings", def.save.body.settings); @@ -347,7 +347,7 @@ void ContentLoader::loadEntity(EntityDef& def, const std::string& name, const fs def.bodyType = *bodyType; } - root->str("rig-name", def.rigName); + root->str("skeleton-name", def.skeletonName); } void ContentLoader::loadEntity(EntityDef& def, const std::string& full, const std::string& name) { @@ -468,13 +468,13 @@ void ContentLoader::load() { } } - fs::path rigsDir = folder / fs::u8path("rigs"); - if (fs::is_directory(rigsDir)) { - for (const auto& entry : fs::directory_iterator(rigsDir)) { + fs::path skeletonsDir = folder / fs::u8path("skeletons"); + if (fs::is_directory(skeletonsDir)) { + for (const auto& entry : fs::directory_iterator(skeletonsDir)) { const fs::path& file = entry.path(); std::string name = pack->id+":"+file.stem().u8string(); std::string text = files::read_string(file); - builder.add(rigging::RigConfig::parse(text, file.u8string(), name)); + builder.add(rigging::SkeletonConfig::parse(text, file.u8string(), name)); } } } diff --git a/src/logic/scripting/lua/api_lua.hpp b/src/logic/scripting/lua/api_lua.hpp index 5287813d..341aa94b 100644 --- a/src/logic/scripting/lua/api_lua.hpp +++ b/src/logic/scripting/lua/api_lua.hpp @@ -29,7 +29,7 @@ extern const luaL_Reg vec4lib []; extern const luaL_Reg worldlib []; // Components -extern const luaL_Reg modeltreelib []; +extern const luaL_Reg skeletonlib []; extern const luaL_Reg rigidbodylib []; extern const luaL_Reg transformlib []; diff --git a/src/logic/scripting/lua/lib__modeltree.cpp b/src/logic/scripting/lua/lib__modeltree.cpp deleted file mode 100644 index 5fb49e79..00000000 --- a/src/logic/scripting/lua/lib__modeltree.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "libentity.hpp" - -#include "../../../objects/rigging.hpp" - -static int index_range_check(const rigging::Rig& rig, lua::Integer index) { - if (static_cast(index) >= rig.pose.matrices.size()) { - throw std::runtime_error("index out of range [0, " + - std::to_string(rig.pose.matrices.size()) + - "]"); - } - return static_cast(index); -} - -static int l_modeltree_get_model(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& rig = entity->getModeltree(); - auto* rigConfig = rig.config; - auto index = index_range_check(rig, lua::tointeger(L, 2)); - return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName()); - } - return 0; -} - -static int l_modeltree_get_matrix(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& rig = entity->getModeltree(); - auto index = index_range_check(rig, lua::tointeger(L, 2)); - return lua::pushmat4(L, rig.pose.matrices[index]); - } - return 0; -} - -static int l_modeltree_set_matrix(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& rig = entity->getModeltree(); - auto index = index_range_check(rig, lua::tointeger(L, 2)); - rig.pose.matrices[index] = lua::tomat4(L, 3); - } - return 0; -} - -static int l_modeltree_set_texture(lua::State* L) { - if (auto entity = get_entity(L, 1)) { - auto& rig = entity->getModeltree(); - rig.textures[lua::require_string(L, 2)] = lua::require_string(L, 3); - } - return 0; -} - -const luaL_Reg modeltreelib [] = { - {"get_model", lua::wrap}, - {"get_matrix", lua::wrap}, - {"set_matrix", lua::wrap}, - {"set_texture", lua::wrap}, - {NULL, NULL} -}; diff --git a/src/logic/scripting/lua/lib__skeleton.cpp b/src/logic/scripting/lua/lib__skeleton.cpp new file mode 100644 index 00000000..b2514ffd --- /dev/null +++ b/src/logic/scripting/lua/lib__skeleton.cpp @@ -0,0 +1,56 @@ +#include "libentity.hpp" + +#include "../../../objects/rigging.hpp" + +static int index_range_check(const rigging::Skeleton& skeleton, lua::Integer index) { + if (static_cast(index) >= skeleton.pose.matrices.size()) { + throw std::runtime_error("index out of range [0, " + + std::to_string(skeleton.pose.matrices.size()) + + "]"); + } + return static_cast(index); +} + +static int l_get_model(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& skeleton = entity->getSkeleton(); + auto* rigConfig = skeleton.config; + auto index = index_range_check(skeleton, lua::tointeger(L, 2)); + return lua::pushstring(L, rigConfig->getNodes()[index]->getModelName()); + } + return 0; +} + +static int l_get_matrix(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& skeleton = entity->getSkeleton(); + auto index = index_range_check(skeleton, lua::tointeger(L, 2)); + return lua::pushmat4(L, skeleton.pose.matrices[index]); + } + return 0; +} + +static int l_set_matrix(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& skeleton = entity->getSkeleton(); + auto index = index_range_check(skeleton, lua::tointeger(L, 2)); + skeleton.pose.matrices[index] = lua::tomat4(L, 3); + } + return 0; +} + +static int l_set_texture(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + auto& skeleton = entity->getSkeleton(); + skeleton.textures[lua::require_string(L, 2)] = lua::require_string(L, 3); + } + return 0; +} + +const luaL_Reg skeletonlib [] = { + {"get_model", lua::wrap}, + {"get_matrix", lua::wrap}, + {"set_matrix", lua::wrap}, + {"set_texture", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index 2cd5da71..fc7732ae 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -34,10 +34,10 @@ static int l_entity_despawn(lua::State* L) { static int l_entity_set_rig(lua::State* L) { if (auto entity = get_entity(L, 1)) { - std::string rigName = lua::require_string(L, 2); - auto rigConfig = content->getRig(rigName); + std::string skeletonName = lua::require_string(L, 2); + auto rigConfig = content->getRig(skeletonName); if (rigConfig == nullptr) { - throw std::runtime_error("rig not found '"+rigName+"'"); + throw std::runtime_error("skeleton not found '"+skeletonName+"'"); } entity->setRig(rigConfig); } diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index d99aa603..1c1ff32b 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -50,7 +50,7 @@ static void create_libs(lua::State* L) { openlib(L, "entities", entitylib); // components - openlib(L, "__modeltree", modeltreelib); + openlib(L, "__skeleton", skeletonlib); openlib(L, "__rigidbody", rigidbodylib); openlib(L, "__transform", transformlib); diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 18e266cd..1812d52b 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -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(entity); +rigging::Skeleton& Entity::getSkeleton() const { + return registry.get(entity); } -void Entity::setRig(const rigging::RigConfig* rigConfig) { - auto& rig = registry.get(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(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(entity); entities[id] = entity; uids[entity] = id; - registry.emplace(entity, rig->instance()); + registry.emplace(entity, skeleton->instance()); for (auto& componentName : def.components) { auto component = std::make_unique( 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(); - for (auto [entity, transform, rig] : view.each()) { + auto view = registry.view(); + 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); } } } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 969403da..b2611d93 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -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(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(entity).uid; diff --git a/src/objects/EntityDef.hpp b/src/objects/EntityDef.hpp index 253e4ec1..3cc015ff 100644 --- a/src/objects/EntityDef.hpp +++ b/src/objects/EntityDef.hpp @@ -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> boxSensors {}; std::vector> 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) {} diff --git a/src/objects/rigging.cpp b/src/objects/rigging.cpp index 0f6f9750..8b751a16 100644 --- a/src/objects/rigging.cpp +++ b/src/objects/rigging.cpp @@ -7,18 +7,18 @@ using namespace rigging; -RigNode::RigNode( +Bone::Bone( size_t index, std::string name, std::string model, - std::vector> subnodes) + std::vector> 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(modelName); modelUpdated = false; } } -static void get_all_nodes(std::vector& nodes, RigNode* node) { +static void get_all_nodes(std::vector& 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 root, size_t nodesCount) +SkeletonConfig::SkeletonConfig(const std::string& name, std::unique_ptr 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> read_node( +static std::tuple> read_node( dynamic::Map* root, size_t index ) { std::string name; std::string model; root->str("name", name); root->str("model", model); - std::vector> subnodes; + std::vector> 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(index, name, model, std::move(subnodes))}; + return {index + count, std::make_unique(index, name, model, std::move(bones))}; } -std::unique_ptr RigConfig::parse( +std::unique_ptr SkeletonConfig::parse( std::string_view src, std::string_view file, std::string_view name @@ -113,5 +113,5 @@ std::unique_ptr RigConfig::parse( throw std::runtime_error("missing 'root' element"); } auto [count, rootNode] = read_node(rootNodeMap.get(), 0); - return std::make_unique(std::string(name), std::move(rootNode), count); + return std::make_unique(std::string(name), std::move(rootNode), count); } diff --git a/src/objects/rigging.hpp b/src/objects/rigging.hpp index 786881bd..aa084d8f 100644 --- a/src/objects/rigging.hpp +++ b/src/objects/rigging.hpp @@ -17,8 +17,8 @@ namespace model { } namespace rigging { - struct Rig; - class RigConfig; + struct Skeleton; + class SkeletonConfig; struct Pose { std::vector matrices; @@ -28,19 +28,19 @@ namespace rigging { } }; - class RigNode { + class Bone { size_t index; std::string name; std::string modelName; - std::vector> subnodes; + std::vector> bones; model::Model* model = nullptr; bool modelUpdated = true; public: - RigNode( + Bone( size_t index, std::string name, std::string model, - std::vector> subnodes); + std::vector> 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 textures; }; - class RigConfig { + class SkeletonConfig { std::string name; - std::unique_ptr root; + std::unique_ptr root; std::unordered_map 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 nodes; + std::vector 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 root, + SkeletonConfig(const std::string& name, std::unique_ptr 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 parse( + static std::unique_ptr parse( std::string_view src, std::string_view file, std::string_view name ); - const std::vector& getNodes() const { + const std::vector& getNodes() const { return nodes; }