update entities render to use rigs

This commit is contained in:
MihailRis 2024-07-02 15:04:41 +03:00
parent 8e08f79b54
commit 56ef207b7d
4 changed files with 63 additions and 24 deletions

View File

@ -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<Transform>();
auto model = assets->get<model::Model>("cube");
if (model == nullptr) {
return;
}
for (auto [entity, transform] : view.each()) {
auto view = registry.view<Transform, rigging::Rig>();
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);
}
}
}

View File

@ -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;

View File

@ -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<std::unique_ptr<RigNode>> 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<model::Model>(name);
}
RigConfig::RigConfig(std::unique_ptr<RigNode> root) : root(std::move(root)) {
static void get_all_nodes(std::vector<RigNode*>& nodes, RigNode* node) {
nodes[node->getIndex()] = node;
for (auto& subnode : node->getSubnodes()) {
get_all_nodes(nodes, subnode.get());
}
}
RigConfig::RigConfig(std::unique_ptr<RigNode> 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<size_t, std::unique_ptr<RigNode>> read_node(
dynamic::Map* root, size_t index
) {
@ -72,7 +100,7 @@ static std::tuple<size_t, std::unique_ptr<RigNode>> read_node(
}
}
}
return {index, std::make_unique<RigNode>(index, name, model, std::move(subnodes))};
return {index + count, std::make_unique<RigNode>(index, name, model, std::move(subnodes))};
}
std::unique_ptr<RigConfig> RigConfig::parse(
@ -84,6 +112,6 @@ std::unique_ptr<RigConfig> RigConfig::parse(
if (rootNodeMap == nullptr) {
throw std::runtime_error("missing 'root' element");
}
auto [count, rootNode] = read_node(root.get(), 0);
return std::make_unique<RigConfig>(std::move(rootNode));
auto [count, rootNode] = read_node(rootNodeMap, 0);
return std::make_unique<RigConfig>(std::move(rootNode), count);
}

View File

@ -10,6 +10,7 @@
#include <unordered_map>
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<RigNode> root);
RigConfig(std::unique_ptr<RigNode> 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 {