diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 18ca39f7..d46f3472 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -195,18 +195,13 @@ void Entities::renderDebug(LineBatch& batch, const Frustum& frustum) { } void Entities::render(Assets* assets, ModelBatch& batch, const Frustum& frustum) { - auto view = registry.view(); - auto model = assets->get("cube"); - if (model == nullptr) { - return; - } - for (auto [entity, transform] : view.each()) { + auto view = registry.view(); + for (auto [entity, transform, rig] : view.each()) { const auto& pos = transform.pos; const auto& size = transform.size; if (frustum.isBoxVisible(pos-size, pos+size)) { - batch.pushMatrix(transform.combined); - batch.draw(model); - batch.popMatrix(); + const auto* rigConfig = rig.config; + rigConfig->render(assets, batch, rig, transform.combined); } } } diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index e85c8f03..35811875 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -68,8 +68,11 @@ class Assets; class LineBatch; class ModelBatch; class Frustum; -class Rig; class Entities; +namespace riggining { + struct Rig; + class RigConfig; +} class Entity { Entities& entities; diff --git a/src/objects/rigging.cpp b/src/objects/rigging.cpp index 4d08548d..cb96fe71 100644 --- a/src/objects/rigging.cpp +++ b/src/objects/rigging.cpp @@ -1,6 +1,7 @@ #include "rigging.hpp" #include "../assets/Assets.hpp" +#include "../graphics/render/ModelBatch.hpp" #include "../graphics/core/Model.hpp" #include "../coders/json.hpp" @@ -13,7 +14,7 @@ RigNode::RigNode( std::vector> subnodes) : index(index), name(std::move(name)), - modelName(model), + modelName(std::move(model)), subnodes(std::move(subnodes)) {} @@ -22,28 +23,36 @@ void RigNode::setModel(const Assets* assets, const std::string& name) { model = assets->get(name); } -RigConfig::RigConfig(std::unique_ptr root) : root(std::move(root)) { +static void get_all_nodes(std::vector& nodes, RigNode* node) { + nodes[node->getIndex()] = node; + for (auto& subnode : node->getSubnodes()) { + get_all_nodes(nodes, subnode.get()); + } +} + +RigConfig::RigConfig(std::unique_ptr root, size_t nodesCount) + : root(std::move(root)), nodes(nodesCount) { + get_all_nodes(nodes, this->root.get()); } size_t RigConfig::update( size_t index, Rig& rig, RigNode* node, - glm::mat4 matrix) + glm::mat4 matrix) const { rig.calculated.matrices[index] = matrix * rig.pose.matrices[index]; - index++; for (auto& subnode : node->getSubnodes()) { - index = update(index, rig, subnode.get(), rig.calculated.matrices[index]); + index = update(index+1, rig, subnode.get(), rig.calculated.matrices[index]); } return index; } -void RigConfig::update(Rig& rig, glm::mat4 matrix) { +void RigConfig::update(Rig& rig, glm::mat4 matrix) const { update(0, rig, root.get(), matrix); } -void RigConfig::setup(const Assets* assets, RigNode* node) { +void RigConfig::setup(const Assets* assets, RigNode* node) const { if (node == nullptr) { setup(assets, root.get()); } else { @@ -54,6 +63,25 @@ void RigConfig::setup(const Assets* assets, RigNode* node) { } } +void RigConfig::render( + Assets*, + ModelBatch& batch, + Rig& rig, + const glm::mat4& matrix) const +{ + update(rig, matrix); + for (size_t i = 0; i < nodes.size(); i++) { + auto* node = nodes[i]; + auto model = node->getModel(); + if (model == nullptr) { + continue; + } + batch.pushMatrix(rig.calculated.matrices[i]); + batch.draw(model); + batch.popMatrix(); + } +} + static std::tuple> read_node( dynamic::Map* root, size_t index ) { @@ -72,7 +100,7 @@ static std::tuple> read_node( } } } - return {index, std::make_unique(index, name, model, std::move(subnodes))}; + return {index + count, std::make_unique(index, name, model, std::move(subnodes))}; } std::unique_ptr RigConfig::parse( @@ -84,6 +112,6 @@ std::unique_ptr RigConfig::parse( if (rootNodeMap == nullptr) { throw std::runtime_error("missing 'root' element"); } - auto [count, rootNode] = read_node(root.get(), 0); - return std::make_unique(std::move(rootNode)); + auto [count, rootNode] = read_node(rootNodeMap, 0); + return std::make_unique(std::move(rootNode), count); } diff --git a/src/objects/rigging.hpp b/src/objects/rigging.hpp index 7cef4baf..e84bb42e 100644 --- a/src/objects/rigging.hpp +++ b/src/objects/rigging.hpp @@ -10,6 +10,7 @@ #include class Assets; +class ModelBatch; namespace model { struct Model; @@ -24,6 +25,9 @@ namespace rigging { Pose(size_t size) : matrices(size) { matrices.resize(size); + for (size_t i = 0; i < size; i++) { + matrices[i] = glm::mat4(1.0f); + } } }; @@ -46,6 +50,10 @@ namespace rigging { return modelName; } + model::Model* getModel() const { + return model; + } + size_t getIndex() const { return index; } @@ -71,12 +79,17 @@ namespace rigging { size_t index, Rig& rig, RigNode* node, - glm::mat4 matrix); + glm::mat4 matrix) const; public: - RigConfig(std::unique_ptr root); + RigConfig(std::unique_ptr root, size_t nodesCount); - void update(Rig& rig, glm::mat4 matrix); - void setup(const Assets* assets, RigNode* node=nullptr); + void update(Rig& rig, glm::mat4 matrix) const; + void setup(const Assets* assets, RigNode* node=nullptr) const; + void render( + Assets* assets, + ModelBatch& batch, + Rig& rig, + const glm::mat4& matrix) const; Rig instance() { return Rig {