update entities render to use rigs
This commit is contained in:
@@ -195,18 +195,13 @@ void Entities::renderDebug(LineBatch& batch, const Frustum& frustum) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Entities::render(Assets* assets, ModelBatch& batch, const Frustum& frustum) {
|
void Entities::render(Assets* assets, ModelBatch& batch, const Frustum& frustum) {
|
||||||
auto view = registry.view<Transform>();
|
auto view = registry.view<Transform, rigging::Rig>();
|
||||||
auto model = assets->get<model::Model>("cube");
|
for (auto [entity, transform, rig] : view.each()) {
|
||||||
if (model == nullptr) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (auto [entity, transform] : view.each()) {
|
|
||||||
const auto& pos = transform.pos;
|
const auto& pos = transform.pos;
|
||||||
const auto& size = transform.size;
|
const auto& size = transform.size;
|
||||||
if (frustum.isBoxVisible(pos-size, pos+size)) {
|
if (frustum.isBoxVisible(pos-size, pos+size)) {
|
||||||
batch.pushMatrix(transform.combined);
|
const auto* rigConfig = rig.config;
|
||||||
batch.draw(model);
|
rigConfig->render(assets, batch, rig, transform.combined);
|
||||||
batch.popMatrix();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,8 +68,11 @@ class Assets;
|
|||||||
class LineBatch;
|
class LineBatch;
|
||||||
class ModelBatch;
|
class ModelBatch;
|
||||||
class Frustum;
|
class Frustum;
|
||||||
class Rig;
|
|
||||||
class Entities;
|
class Entities;
|
||||||
|
namespace riggining {
|
||||||
|
struct Rig;
|
||||||
|
class RigConfig;
|
||||||
|
}
|
||||||
|
|
||||||
class Entity {
|
class Entity {
|
||||||
Entities& entities;
|
Entities& entities;
|
||||||
|
|||||||
+38
-10
@@ -1,6 +1,7 @@
|
|||||||
#include "rigging.hpp"
|
#include "rigging.hpp"
|
||||||
|
|
||||||
#include "../assets/Assets.hpp"
|
#include "../assets/Assets.hpp"
|
||||||
|
#include "../graphics/render/ModelBatch.hpp"
|
||||||
#include "../graphics/core/Model.hpp"
|
#include "../graphics/core/Model.hpp"
|
||||||
#include "../coders/json.hpp"
|
#include "../coders/json.hpp"
|
||||||
|
|
||||||
@@ -13,7 +14,7 @@ RigNode::RigNode(
|
|||||||
std::vector<std::unique_ptr<RigNode>> subnodes)
|
std::vector<std::unique_ptr<RigNode>> subnodes)
|
||||||
: index(index),
|
: index(index),
|
||||||
name(std::move(name)),
|
name(std::move(name)),
|
||||||
modelName(model),
|
modelName(std::move(model)),
|
||||||
subnodes(std::move(subnodes))
|
subnodes(std::move(subnodes))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
@@ -22,28 +23,36 @@ void RigNode::setModel(const Assets* assets, const std::string& name) {
|
|||||||
model = assets->get<model::Model>(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 RigConfig::update(
|
||||||
size_t index,
|
size_t index,
|
||||||
Rig& rig,
|
Rig& rig,
|
||||||
RigNode* node,
|
RigNode* node,
|
||||||
glm::mat4 matrix)
|
glm::mat4 matrix) const
|
||||||
{
|
{
|
||||||
rig.calculated.matrices[index] = matrix * rig.pose.matrices[index];
|
rig.calculated.matrices[index] = matrix * rig.pose.matrices[index];
|
||||||
index++;
|
|
||||||
for (auto& subnode : node->getSubnodes()) {
|
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;
|
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);
|
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) {
|
if (node == nullptr) {
|
||||||
setup(assets, root.get());
|
setup(assets, root.get());
|
||||||
} else {
|
} 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(
|
static std::tuple<size_t, std::unique_ptr<RigNode>> read_node(
|
||||||
dynamic::Map* root, size_t index
|
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(
|
std::unique_ptr<RigConfig> RigConfig::parse(
|
||||||
@@ -84,6 +112,6 @@ std::unique_ptr<RigConfig> RigConfig::parse(
|
|||||||
if (rootNodeMap == nullptr) {
|
if (rootNodeMap == nullptr) {
|
||||||
throw std::runtime_error("missing 'root' element");
|
throw std::runtime_error("missing 'root' element");
|
||||||
}
|
}
|
||||||
auto [count, rootNode] = read_node(root.get(), 0);
|
auto [count, rootNode] = read_node(rootNodeMap, 0);
|
||||||
return std::make_unique<RigConfig>(std::move(rootNode));
|
return std::make_unique<RigConfig>(std::move(rootNode), count);
|
||||||
}
|
}
|
||||||
|
|||||||
+17
-4
@@ -10,6 +10,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
class Assets;
|
class Assets;
|
||||||
|
class ModelBatch;
|
||||||
|
|
||||||
namespace model {
|
namespace model {
|
||||||
struct Model;
|
struct Model;
|
||||||
@@ -24,6 +25,9 @@ namespace rigging {
|
|||||||
|
|
||||||
Pose(size_t size) : matrices(size) {
|
Pose(size_t size) : matrices(size) {
|
||||||
matrices.resize(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;
|
return modelName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
model::Model* getModel() const {
|
||||||
|
return model;
|
||||||
|
}
|
||||||
|
|
||||||
size_t getIndex() const {
|
size_t getIndex() const {
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
@@ -71,12 +79,17 @@ namespace rigging {
|
|||||||
size_t index,
|
size_t index,
|
||||||
Rig& rig,
|
Rig& rig,
|
||||||
RigNode* node,
|
RigNode* node,
|
||||||
glm::mat4 matrix);
|
glm::mat4 matrix) const;
|
||||||
public:
|
public:
|
||||||
RigConfig(std::unique_ptr<RigNode> root);
|
RigConfig(std::unique_ptr<RigNode> root, size_t nodesCount);
|
||||||
|
|
||||||
void update(Rig& rig, glm::mat4 matrix);
|
void update(Rig& rig, glm::mat4 matrix) const;
|
||||||
void setup(const Assets* assets, RigNode* node=nullptr);
|
void setup(const Assets* assets, RigNode* node=nullptr) const;
|
||||||
|
void render(
|
||||||
|
Assets* assets,
|
||||||
|
ModelBatch& batch,
|
||||||
|
Rig& rig,
|
||||||
|
const glm::mat4& matrix) const;
|
||||||
|
|
||||||
Rig instance() {
|
Rig instance() {
|
||||||
return Rig {
|
return Rig {
|
||||||
|
|||||||
Reference in New Issue
Block a user