add bone "offset" parameter & update player model
This commit is contained in:
@@ -140,6 +140,7 @@ void Player::updateSelectedEntity() {
|
||||
selectedEid = selection.entity;
|
||||
}
|
||||
|
||||
#include "../window/Window.hpp"
|
||||
void Player::postUpdate() {
|
||||
auto entity = level->entities->get(eid);
|
||||
if (!entity.has_value()) {
|
||||
@@ -159,14 +160,15 @@ void Player::postUpdate() {
|
||||
|
||||
skeleton.visible = currentCamera != camera;
|
||||
|
||||
auto velocityMod = glm::length(hitbox.velocity);
|
||||
|
||||
size_t bodyIndex = skeleton.config->find("body")->getIndex();
|
||||
size_t headIndex = skeleton.config->find("head")->getIndex();
|
||||
|
||||
|
||||
skeleton.pose.matrices[bodyIndex] =
|
||||
glm::rotate(glm::mat4(1.0f), glm::radians(cam.x-90), glm::vec3(0, 1, 0));
|
||||
skeleton.pose.matrices[headIndex] = glm::rotate(glm::rotate(
|
||||
glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.4f, 0.0f)),
|
||||
glm::radians(-cam.y), glm::vec3(0, 0, 1)), glm::radians(90.0f), glm::vec3(0, 1, 0));
|
||||
glm::rotate(glm::mat4(1.0f), glm::radians(cam.x), glm::vec3(0, 1, 0));
|
||||
skeleton.pose.matrices[headIndex] = glm::rotate(
|
||||
glm::mat4(1.0f), glm::radians(cam.y), glm::vec3(1, 0, 0));
|
||||
}
|
||||
|
||||
void Player::teleport(glm::vec3 position) {
|
||||
@@ -188,8 +190,9 @@ void Player::attemptToFindSpawnpoint() {
|
||||
|
||||
voxel* headvox = level->chunks->get(newpos.x, newpos.y+1, newpos.z);
|
||||
if (level->chunks->isObstacleBlock(newpos.x, newpos.y, newpos.z) ||
|
||||
headvox == nullptr || headvox->id != 0)
|
||||
headvox == nullptr || headvox->id != 0) {
|
||||
return;
|
||||
}
|
||||
spawnpoint = newpos + glm::vec3(0.5f, 0.0f, 0.5f);
|
||||
teleport(spawnpoint);
|
||||
}
|
||||
|
||||
+24
-7
@@ -4,6 +4,11 @@
|
||||
#include "../graphics/render/ModelBatch.hpp"
|
||||
#include "../graphics/core/Model.hpp"
|
||||
#include "../coders/json.hpp"
|
||||
#include "../data/dynamic_util.hpp"
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
#include <glm/gtx/norm.hpp>
|
||||
#include <glm/ext/matrix_transform.hpp>
|
||||
|
||||
using namespace rigging;
|
||||
|
||||
@@ -18,10 +23,12 @@ Bone::Bone(
|
||||
size_t index,
|
||||
std::string name,
|
||||
std::string model,
|
||||
std::vector<std::unique_ptr<Bone>> bones)
|
||||
std::vector<std::unique_ptr<Bone>> bones,
|
||||
glm::vec3 offset)
|
||||
: index(index),
|
||||
name(std::move(name)),
|
||||
bones(std::move(bones)),
|
||||
offset(offset),
|
||||
model({model, nullptr, true})
|
||||
{}
|
||||
|
||||
@@ -40,7 +47,8 @@ Skeleton::Skeleton(const SkeletonConfig* config)
|
||||
textures(),
|
||||
modelOverrides(config->getBones().size()),
|
||||
visible(true) {
|
||||
for (size_t i = 0; i < config->getBones().size(); i++) {
|
||||
const auto& bones = config->getBones();
|
||||
for (size_t i = 0; i < bones.size(); i++) {
|
||||
flags[i].visible = true;
|
||||
}
|
||||
}
|
||||
@@ -63,7 +71,13 @@ size_t SkeletonConfig::update(
|
||||
Bone* node,
|
||||
glm::mat4 matrix) const
|
||||
{
|
||||
skeleton.calculated.matrices[index] = matrix * skeleton.pose.matrices[index];
|
||||
auto boneMatrix = skeleton.pose.matrices[index];
|
||||
auto boneOffset = node->getOffset();
|
||||
glm::mat4 baseMatrix(1.0f);
|
||||
if (glm::length2(boneOffset) > 0.0f) {
|
||||
baseMatrix = glm::translate(glm::mat4(1.0f), boneOffset);
|
||||
}
|
||||
skeleton.calculated.matrices[index] = matrix * baseMatrix * boneMatrix;
|
||||
size_t count = 1;
|
||||
for (auto& subnode : node->getSubnodes()) {
|
||||
count += update(index+count, skeleton, subnode.get(), skeleton.calculated.matrices[index]);
|
||||
@@ -116,25 +130,28 @@ Bone* SkeletonConfig::find(std::string_view str) const {
|
||||
}
|
||||
|
||||
static std::tuple<size_t, std::unique_ptr<Bone>> read_node(
|
||||
dynamic::Map* root, size_t index
|
||||
const dynamic::Map_sptr& root, size_t index
|
||||
) {
|
||||
std::string name;
|
||||
std::string model;
|
||||
root->str("name", name);
|
||||
root->str("model", model);
|
||||
|
||||
glm::vec3 offset(0.0f);
|
||||
dynamic::get_vec(root, "offset", offset);
|
||||
|
||||
std::vector<std::unique_ptr<Bone>> 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);
|
||||
auto [subcount, subNode] = read_node(map, index+count);
|
||||
count += subcount;
|
||||
bones.push_back(std::move(subNode));
|
||||
}
|
||||
}
|
||||
}
|
||||
return {count, std::make_unique<Bone>(index, name, model, std::move(bones))};
|
||||
return {count, std::make_unique<Bone>(index, name, model, std::move(bones), offset)};
|
||||
}
|
||||
|
||||
std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(
|
||||
@@ -147,6 +164,6 @@ std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(
|
||||
if (rootNodeMap == nullptr) {
|
||||
throw std::runtime_error("missing 'root' element");
|
||||
}
|
||||
auto [count, rootNode] = read_node(rootNodeMap.get(), 0);
|
||||
auto [count, rootNode] = read_node(rootNodeMap, 0);
|
||||
return std::make_unique<SkeletonConfig>(std::string(name), std::move(rootNode), count);
|
||||
}
|
||||
|
||||
@@ -40,13 +40,15 @@ namespace rigging {
|
||||
size_t index;
|
||||
std::string name;
|
||||
std::vector<std::unique_ptr<Bone>> bones;
|
||||
glm::vec3 offset;
|
||||
public:
|
||||
ModelReference model;
|
||||
Bone(
|
||||
size_t index,
|
||||
std::string name,
|
||||
std::string model,
|
||||
std::vector<std::unique_ptr<Bone>> bones);
|
||||
std::vector<std::unique_ptr<Bone>> bones,
|
||||
glm::vec3 offset);
|
||||
|
||||
void setModel(const std::string& name);
|
||||
|
||||
@@ -58,6 +60,10 @@ namespace rigging {
|
||||
return index;
|
||||
}
|
||||
|
||||
glm::vec3 getOffset() const {
|
||||
return offset;
|
||||
}
|
||||
|
||||
const auto& getSubnodes() const {
|
||||
return bones;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user