add modeltree:get_matrix, :set_matrix

This commit is contained in:
MihailRis
2024-07-02 22:13:16 +03:00
parent 8a10ed24da
commit 42ea88501d
7 changed files with 53 additions and 18 deletions
+30 -1
View File
@@ -111,16 +111,43 @@ static int l_get_size(lua::State* L) {
return 0;
}
static int index_range_check(const rigging::Rig& rig, lua::Integer index) {
if (static_cast<size_t>(index) >= rig.pose.matrices.size()) {
throw std::runtime_error("index out of range [0, " +
std::to_string(rig.pose.matrices.size()) +
"]");
}
return static_cast<int>(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 = lua::tointeger(L, 2);
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;
}
const luaL_Reg entitylib [] = {
{"exists", lua::wrap<l_exists>},
{"spawn", lua::wrap<l_spawn>},
@@ -130,6 +157,8 @@ const luaL_Reg entitylib [] = {
const luaL_Reg modeltreelib [] = {
{"get_model", lua::wrap<l_modeltree_get_model>},
{"get_matrix", lua::wrap<l_modeltree_get_matrix>},
{"set_matrix", lua::wrap<l_modeltree_set_matrix>},
{NULL, NULL}
};
+1 -1
View File
@@ -78,9 +78,9 @@ entityid_t Entities::spawn(
auto& scripting = registry.emplace<Scripting>(
entity, entity_funcs_set {}, nullptr);
entities[id] = entity;
registry.emplace<rigging::Rig>(entity, rig->instance());
scripting.env = scripting::on_entity_spawn(
def, id, scripting.funcsset, std::move(args));
registry.emplace<rigging::Rig>(entity, rig->instance());
return id;
}
+3 -2
View File
@@ -42,10 +42,11 @@ size_t RigConfig::update(
glm::mat4 matrix) const
{
rig.calculated.matrices[index] = matrix * rig.pose.matrices[index];
size_t count = 1;
for (auto& subnode : node->getSubnodes()) {
index = update(index+1, rig, subnode.get(), rig.calculated.matrices[index]);
count += update(index+count, rig, subnode.get(), rig.calculated.matrices[index]);
}
return index;
return count;
}
void RigConfig::update(Rig& rig, glm::mat4 matrix) const {
+5 -8
View File
@@ -1,5 +1,5 @@
#ifndef OBJECTS_SKELETON_HPP_
#define OBJECTS_SKELETON_HPP_
#ifndef OBJECTS_RIGGING_HPP_
#define OBJECTS_RIGGING_HPP_
#include "../typedefs.hpp"
@@ -23,11 +23,8 @@ namespace rigging {
struct Pose {
std::vector<glm::mat4> matrices;
Pose(size_t size) : matrices(size) {
matrices.resize(size);
for (size_t i = 0; i < size; i++) {
matrices[i] = glm::mat4(1.0f);
}
Pose(size_t size) {
matrices.resize(size, glm::mat4(1.0f));
}
};
@@ -115,4 +112,4 @@ namespace rigging {
};
};
#endif // OBJECTS_SKELETON_HPP_
#endif // OBJECTS_RIGGING_HPP_