add bone "offset" parameter & update player model

This commit is contained in:
MihailRis 2024-07-21 18:27:11 +03:00
parent 3590bd14cd
commit 0333ce6990
8 changed files with 151 additions and 81 deletions

View File

@ -0,0 +1,47 @@
# Blender v2.79 (sub 0) OBJ File: 'player.blend'
# www.blender.org
mtllib player-body.mtl
o Cube.001
v -0.125000 -0.900000 0.070903
v -0.125000 -0.900000 -0.070903
v 0.125000 -0.900000 -0.070903
v 0.125000 -0.900000 0.070903
v -0.125000 0.491919 0.070903
v 0.125000 0.491919 0.070903
v 0.125000 0.491919 -0.070903
v -0.125000 0.491919 -0.070903
vt 0.783122 0.009685
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vt 0.783122 0.009685
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vt 0.112175 0.434439
vt 0.311556 0.434439
vt 0.311556 0.633819
vt 0.112175 0.633819
vt 0.783122 0.009685
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 0.0000
vn -1.0000 0.0000 0.0000
vn -0.0000 -0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
usemtl entities/player
s 1
f 1/1/1 2/2/1 3/3/1 4/4/1
f 5/5/2 6/6/2 7/7/2 8/8/2
f 1/1/3 5/9/3 8/10/3 2/11/3
f 2/12/4 8/13/4 7/14/4 3/15/4
f 3/16/5 7/17/5 6/18/5 4/4/5
f 5/5/6 1/19/6 4/20/6 6/21/6

View File

@ -0,0 +1,42 @@
# Blender v2.79 (sub 0) OBJ File: 'player.blend'
# www.blender.org
mtllib player-hand.mtl
o Cube.000_Cube.002
v 0.062480 -0.613786 -0.062480
v 0.062480 -0.613786 0.062480
v -0.062480 -0.613786 0.062480
v -0.062480 -0.613786 -0.062480
v 0.062480 0.070352 -0.062480
v -0.062480 0.070352 -0.062480
v -0.062480 0.070352 0.062480
v 0.062480 0.070352 0.062480
vt 0.783122 0.009685
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vt 0.783122 0.009685
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vt 0.436482 0.280393
vt 0.433740 0.937829
vt 0.436146 0.914519
vt 0.438665 0.292591
vt 0.492515 0.918221
vt 0.493194 0.293103
vt 0.493371 0.941872
vt 0.494058 0.280870
vn 0.0000 -1.0000 0.0000
vn 0.0000 1.0000 -0.0000
vn 1.0000 -0.0000 0.0000
vn -0.0000 -0.0000 1.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 -1.0000
usemtl entities/player
s 1
f 1/1/1 2/2/1 3/3/1 4/4/1
f 5/5/2 6/6/2 7/7/2 8/8/2
f 1/9/3 5/10/3 8/11/3 2/12/3
f 2/12/4 8/11/4 7/13/4 3/14/4
f 3/14/5 7/13/5 6/15/5 4/16/5
f 5/10/6 1/9/6 4/16/6 6/15/6

View File

@ -2,14 +2,14 @@
# www.blender.org # www.blender.org
mtllib player-head.mtl mtllib player-head.mtl
o Cube.002_Cube.003 o Cube.002_Cube.003
v -0.238204 0.476553 -0.238204 v -0.206512 0.031837 0.206512
v -0.238204 0.000145 -0.238204 v -0.206512 0.444861 0.206512
v -0.238204 0.476553 0.238204 v -0.206512 0.444861 -0.206512
v -0.238204 0.000145 0.238204 v -0.206512 0.031837 -0.206512
v 0.238204 0.000145 0.238204 v 0.206512 0.444861 -0.206512
v 0.238204 0.476553 0.238204 v 0.206512 0.031837 -0.206512
v 0.238204 0.000145 -0.238204 v 0.206512 0.444861 0.206512
v 0.238204 0.476553 -0.238204 v 0.206512 0.031837 0.206512
vt 0.783122 0.009685 vt 0.783122 0.009685
vt 0.982503 0.009685 vt 0.982503 0.009685
vt 0.982503 0.209065 vt 0.982503 0.209065
@ -32,17 +32,17 @@ vt 0.982503 0.209065
vt 0.783122 0.009685 vt 0.783122 0.009685
vt 0.982503 0.009685 vt 0.982503 0.009685
vt 0.783122 0.209065 vt 0.783122 0.209065
vn -1.0000 0.0000 0.0000 vn -1.0000 -0.0000 -0.0000
vn 0.0000 0.0000 -1.0000 vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000 vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000 vn -0.0000 -0.0000 1.0000
vn 0.0000 -1.0000 0.0000 vn 0.0000 -1.0000 -0.0000
vn 0.0000 1.0000 0.0000 vn -0.0000 1.0000 0.0000
usemtl entities/player usemtl entities/player
s off s 1
f 4/1/1 3/2/1 1/3/1 2/4/1 f 1/1/1 2/2/1 3/3/1 4/4/1
f 2/5/2 1/6/2 8/7/2 7/8/2 f 4/5/2 3/6/2 5/7/2 6/8/2
f 7/9/3 8/10/3 6/11/3 5/12/3 f 6/9/3 5/10/3 7/11/3 8/12/3
f 5/13/4 6/14/4 3/15/4 4/16/4 f 8/13/4 7/14/4 2/15/4 1/16/4
f 2/17/5 7/18/5 5/19/5 4/16/5 f 4/17/5 6/18/5 8/19/5 1/16/5
f 8/20/6 1/21/6 3/15/6 6/22/6 f 5/20/6 3/21/6 2/15/6 7/22/6

View File

@ -1,46 +0,0 @@
# Blender v2.79 (sub 0) OBJ File: 'player.blend'
# www.blender.org
mtllib player.mtl
o Cube
v 0.125000 -0.900000 -0.125000
v 0.125000 -0.900000 0.125000
v -0.125000 -0.900000 0.125000
v -0.125000 -0.900000 -0.125000
v 0.125000 0.491919 -0.125000
v 0.125000 0.491919 0.125000
v -0.125000 0.491919 0.125000
v -0.125000 0.491919 -0.125000
vt 0.783122 0.009685
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vt 0.783122 0.009685
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vt 0.783122 0.009685
vt 0.982503 0.009685
vt 0.783122 0.209065
vt 0.783122 0.009685
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.982503 0.009685
vt 0.982503 0.209065
vt 0.783122 0.209065
vn 0.0000 -1.0000 -0.0000
vn 0.0000 1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn -0.0000 -0.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
usemtl entities/player
s off
f 1/1/1 2/2/1 3/3/1 4/4/1
f 5/5/2 8/6/2 7/7/2 6/8/2
f 1/1/3 5/9/3 6/10/3 2/11/3
f 2/12/4 6/13/4 7/7/4 3/14/4
f 3/15/5 7/16/5 8/17/5 4/4/5
f 5/5/6 1/18/6 4/19/6 8/20/6

View File

@ -3,11 +3,12 @@
"nodes": [ "nodes": [
{ {
"name": "body", "name": "body",
"model": "player", "model": "player-body",
"nodes": [ "nodes": [
{ {
"name": "head", "name": "head",
"model": "player-head" "model": "player-head",
"offset": [0, 0.4, 0]
} }
] ]
} }

View File

@ -140,6 +140,7 @@ void Player::updateSelectedEntity() {
selectedEid = selection.entity; selectedEid = selection.entity;
} }
#include "../window/Window.hpp"
void Player::postUpdate() { void Player::postUpdate() {
auto entity = level->entities->get(eid); auto entity = level->entities->get(eid);
if (!entity.has_value()) { if (!entity.has_value()) {
@ -159,14 +160,15 @@ void Player::postUpdate() {
skeleton.visible = currentCamera != camera; skeleton.visible = currentCamera != camera;
auto velocityMod = glm::length(hitbox.velocity);
size_t bodyIndex = skeleton.config->find("body")->getIndex(); size_t bodyIndex = skeleton.config->find("body")->getIndex();
size_t headIndex = skeleton.config->find("head")->getIndex(); size_t headIndex = skeleton.config->find("head")->getIndex();
skeleton.pose.matrices[bodyIndex] = skeleton.pose.matrices[bodyIndex] =
glm::rotate(glm::mat4(1.0f), glm::radians(cam.x-90), 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::rotate( skeleton.pose.matrices[headIndex] = glm::rotate(
glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.4f, 0.0f)), glm::mat4(1.0f), glm::radians(cam.y), glm::vec3(1, 0, 0));
glm::radians(-cam.y), glm::vec3(0, 0, 1)), glm::radians(90.0f), glm::vec3(0, 1, 0));
} }
void Player::teleport(glm::vec3 position) { 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); voxel* headvox = level->chunks->get(newpos.x, newpos.y+1, newpos.z);
if (level->chunks->isObstacleBlock(newpos.x, newpos.y, newpos.z) || if (level->chunks->isObstacleBlock(newpos.x, newpos.y, newpos.z) ||
headvox == nullptr || headvox->id != 0) headvox == nullptr || headvox->id != 0) {
return; return;
}
spawnpoint = newpos + glm::vec3(0.5f, 0.0f, 0.5f); spawnpoint = newpos + glm::vec3(0.5f, 0.0f, 0.5f);
teleport(spawnpoint); teleport(spawnpoint);
} }

View File

@ -4,6 +4,11 @@
#include "../graphics/render/ModelBatch.hpp" #include "../graphics/render/ModelBatch.hpp"
#include "../graphics/core/Model.hpp" #include "../graphics/core/Model.hpp"
#include "../coders/json.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; using namespace rigging;
@ -18,10 +23,12 @@ Bone::Bone(
size_t index, size_t index,
std::string name, std::string name,
std::string model, std::string model,
std::vector<std::unique_ptr<Bone>> bones) std::vector<std::unique_ptr<Bone>> bones,
glm::vec3 offset)
: index(index), : index(index),
name(std::move(name)), name(std::move(name)),
bones(std::move(bones)), bones(std::move(bones)),
offset(offset),
model({model, nullptr, true}) model({model, nullptr, true})
{} {}
@ -40,7 +47,8 @@ Skeleton::Skeleton(const SkeletonConfig* config)
textures(), textures(),
modelOverrides(config->getBones().size()), modelOverrides(config->getBones().size()),
visible(true) { 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; flags[i].visible = true;
} }
} }
@ -63,7 +71,13 @@ size_t SkeletonConfig::update(
Bone* node, Bone* node,
glm::mat4 matrix) const 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; size_t count = 1;
for (auto& subnode : node->getSubnodes()) { for (auto& subnode : node->getSubnodes()) {
count += update(index+count, skeleton, subnode.get(), skeleton.calculated.matrices[index]); 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( 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 name;
std::string model; std::string model;
root->str("name", name); root->str("name", name);
root->str("model", model); root->str("model", model);
glm::vec3 offset(0.0f);
dynamic::get_vec(root, "offset", offset);
std::vector<std::unique_ptr<Bone>> bones; std::vector<std::unique_ptr<Bone>> bones;
size_t count = 1; size_t count = 1;
if (auto nodesList = root->list("nodes")) { if (auto nodesList = root->list("nodes")) {
for (size_t i = 0; i < nodesList->size(); i++) { for (size_t i = 0; i < nodesList->size(); i++) {
if (const auto& map = nodesList->map(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; count += subcount;
bones.push_back(std::move(subNode)); 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( std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(
@ -147,6 +164,6 @@ std::unique_ptr<SkeletonConfig> SkeletonConfig::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(rootNodeMap.get(), 0); auto [count, rootNode] = read_node(rootNodeMap, 0);
return std::make_unique<SkeletonConfig>(std::string(name), std::move(rootNode), count); return std::make_unique<SkeletonConfig>(std::string(name), std::move(rootNode), count);
} }

View File

@ -40,13 +40,15 @@ namespace rigging {
size_t index; size_t index;
std::string name; std::string name;
std::vector<std::unique_ptr<Bone>> bones; std::vector<std::unique_ptr<Bone>> bones;
glm::vec3 offset;
public: public:
ModelReference model; ModelReference model;
Bone( Bone(
size_t index, size_t index,
std::string name, std::string name,
std::string model, 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); void setModel(const std::string& name);
@ -58,6 +60,10 @@ namespace rigging {
return index; return index;
} }
glm::vec3 getOffset() const {
return offset;
}
const auto& getSubnodes() const { const auto& getSubnodes() const {
return bones; return bones;
} }