format: reformat project

Signed-off-by: Vyacheslav Ivanov <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov
2024-08-03 19:53:48 +03:00
committed by Pugemon
parent 736cd175d5
commit bbf33e8e4d
202 changed files with 7389 additions and 5609 deletions
+71 -51
View File
@@ -1,25 +1,25 @@
#include "Entities.hpp"
#include "../debug/Logger.hpp"
#include "../data/dynamic_util.hpp"
#include <glm/ext/matrix_transform.hpp>
#include <sstream>
#include "../assets/Assets.hpp"
#include "../world/Level.hpp"
#include "../maths/rays.hpp"
#include "../content/Content.hpp"
#include "../physics/Hitbox.hpp"
#include "../physics/PhysicsSolver.hpp"
#include "../graphics/render/ModelBatch.hpp"
#include "../data/dynamic_util.hpp"
#include "../debug/Logger.hpp"
#include "../engine.hpp"
#include "../graphics/core/DrawContext.hpp"
#include "../graphics/core/LineBatch.hpp"
#include "../graphics/core/Model.hpp"
#include "../graphics/core/DrawContext.hpp"
#include "../graphics/render/ModelBatch.hpp"
#include "../logic/scripting/scripting.hpp"
#include "../maths/FrustumCulling.hpp"
#include "../maths/rays.hpp"
#include "../objects/EntityDef.hpp"
#include "../objects/rigging.hpp"
#include "../logic/scripting/scripting.hpp"
#include "../engine.hpp"
#include <sstream>
#include <glm/ext/matrix_transform.hpp>
#include "../physics/Hitbox.hpp"
#include "../physics/PhysicsSolver.hpp"
#include "../world/Level.hpp"
static debug::Logger logger("entities");
@@ -39,7 +39,7 @@ void Transform::refresh() {
}
void Entity::destroy() {
if (isValid()){
if (isValid()) {
entities.despawn(id);
}
}
@@ -59,11 +59,11 @@ void Entity::setRig(const rigging::SkeletonConfig* rigConfig) {
);
}
Entities::Entities(Level* level)
: level(level), sensorsTickClock(20, 3), updateTickClock(20, 3) {
Entities::Entities(Level* level)
: level(level), sensorsTickClock(20, 3), updateTickClock(20, 3) {
}
template<void(*callback)(const Entity&, size_t, entityid_t)>
template <void (*callback)(const Entity&, size_t, entityid_t)>
static sensorcallback create_sensor_callback(Entities* entities) {
return [=](auto entityid, auto index, auto otherid) {
if (auto entity = entities->get(entityid)) {
@@ -82,7 +82,14 @@ static void initialize_body(
SensorParams params {};
params.aabb = box;
body.sensors[i] = Sensor {
true, SensorType::AABB, i, id, params, params, {}, {},
true,
SensorType::AABB,
i,
id,
params,
params,
{},
{},
create_sensor_callback<scripting::on_sensor_enter>(entities),
create_sensor_callback<scripting::on_sensor_exit>(entities)};
}
@@ -90,7 +97,14 @@ static void initialize_body(
SensorParams params {};
params.radial = glm::vec4(radius);
body.sensors[i] = Sensor {
true, SensorType::RADIUS, i, id, params, params, {}, {},
true,
SensorType::RADIUS,
i,
id,
params,
params,
{},
{},
create_sensor_callback<scripting::on_sensor_enter>(entities),
create_sensor_callback<scripting::on_sensor_exit>(entities)};
}
@@ -101,11 +115,11 @@ entityid_t Entities::spawn(
glm::vec3 position,
dynamic::Map_sptr args,
dynamic::Map_sptr saved,
entityid_t uid)
{
entityid_t uid
) {
auto skeleton = level->content->getSkeleton(def.skeletonName);
if (skeleton == nullptr) {
throw std::runtime_error("skeleton "+def.skeletonName+" not found");
throw std::runtime_error("skeleton " + def.skeletonName + " not found");
}
entityid_t id;
if (uid == 0) {
@@ -145,7 +159,7 @@ entityid_t Entities::spawn(
auto& scripting = registry.emplace<ScriptComponents>(entity);
registry.emplace<rigging::Skeleton>(entity, skeleton->instance());
for (auto& componentName : def.components) {
auto component = std::make_unique<UserComponent>(
componentName, entity_funcs_set {}, nullptr
@@ -334,8 +348,8 @@ dynamic::Value Entities::serialize(const Entity& entity) {
if (!scripts.components.empty()) {
auto& compsMap = root->putMap("comps");
for (auto& comp : scripts.components) {
auto data = scripting::get_component_value(
comp->env, SAVED_DATA_VARNAME);
auto data =
scripting::get_component_value(comp->env, SAVED_DATA_VARNAME);
compsMap.put(comp->name, data);
}
}
@@ -383,8 +397,8 @@ void Entities::updateSensors(
body.hitbox.position.x,
body.hitbox.position.y,
body.hitbox.position.z,
sensor.params.radial.w*
sensor.params.radial.w);
sensor.params.radial.w * sensor.params.radial.w
);
break;
}
sensors.push_back(&sensor);
@@ -428,18 +442,13 @@ void Entities::updatePhysics(float delta) {
float vel = glm::length(prevVel);
int substeps = static_cast<int>(delta * vel * 20);
substeps = std::min(100, std::max(2, substeps));
physics->step(
level->chunks.get(),
&hitbox,
delta,
substeps,
eid.uid
);
physics->step(level->chunks.get(), &hitbox, delta, substeps, eid.uid);
hitbox.linearDamping = hitbox.grounded * 24;
transform.setPos(hitbox.position);
if (hitbox.grounded && !grounded) {
scripting::on_entity_grounded(
*get(eid.uid), glm::length(prevVel-hitbox.velocity));
*get(eid.uid), glm::length(prevVel - hitbox.velocity)
);
}
if (!hitbox.grounded && grounded) {
scripting::on_entity_fall(*get(eid.uid));
@@ -452,21 +461,28 @@ void Entities::update(float delta) {
scripting::on_entities_update(
updateTickClock.getTickRate(),
updateTickClock.getParts(),
updateTickClock.getPart());
updateTickClock.getPart()
);
}
}
static void debug_render_skeleton(
LineBatch& batch,
const rigging::Bone* bone,
LineBatch& batch,
const rigging::Bone* bone,
const rigging::Skeleton& skeleton
) {
size_t pindex = bone->getIndex();
for (auto& sub : bone->getSubnodes()) {
size_t sindex = sub->getIndex();
batch.line(glm::vec3(skeleton.calculated.matrices[pindex] * glm::vec4(0,0,0,1)),
glm::vec3(skeleton.calculated.matrices[sindex] * glm::vec4(0,0,0,1)),
glm::vec4(0,0.5f,0,1));
batch.line(
glm::vec3(
skeleton.calculated.matrices[pindex] * glm::vec4(0, 0, 0, 1)
),
glm::vec3(
skeleton.calculated.matrices[sindex] * glm::vec4(0, 0, 0, 1)
),
glm::vec4(0, 0.5f, 0, 1)
);
debug_render_skeleton(batch, sub.get(), skeleton);
}
}
@@ -482,18 +498,18 @@ void Entities::renderDebug(
const auto& hitbox = rigidbody.hitbox;
const auto& pos = transform.pos;
const auto& size = transform.size;
if (frustum && !frustum->isBoxVisible(pos-size, pos+size)) {
if (frustum && !frustum->isBoxVisible(pos - size, pos + size)) {
continue;
}
batch.box(hitbox.position, hitbox.halfsize * 2.0f, glm::vec4(1.0f));
for (auto& sensor : rigidbody.sensors) {
if (sensor.type != SensorType::AABB)
continue;
if (sensor.type != SensorType::AABB) continue;
batch.box(
sensor.calculated.aabb.center(),
sensor.calculated.aabb.size(),
glm::vec4(1.0f, 1.0f, 0.0f, 1.0f));
sensor.calculated.aabb.center(),
sensor.calculated.aabb.size(),
glm::vec4(1.0f, 1.0f, 0.0f, 1.0f)
);
}
}
}
@@ -507,7 +523,7 @@ void Entities::renderDebug(
auto config = skeleton.config;
const auto& pos = transform.pos;
const auto& size = transform.size;
if (frustum && !frustum->isBoxVisible(pos-size, pos+size)) {
if (frustum && !frustum->isBoxVisible(pos - size, pos + size)) {
continue;
}
auto bone = config->getRoot();
@@ -517,7 +533,11 @@ void Entities::renderDebug(
}
void Entities::render(
Assets* assets, ModelBatch& batch, const Frustum* frustum, float delta, bool pause
Assets* assets,
ModelBatch& batch,
const Frustum* frustum,
float delta,
bool pause
) {
if (!pause) {
scripting::on_entities_render(delta);
@@ -530,7 +550,7 @@ void Entities::render(
}
const auto& pos = transform.pos;
const auto& size = transform.size;
if (!frustum || frustum->isBoxVisible(pos-size, pos+size)) {
if (!frustum || frustum->isBoxVisible(pos - size, pos + size)) {
const auto* rigConfig = skeleton.config;
rigConfig->render(assets, batch, skeleton, transform.combined);
}
@@ -568,7 +588,7 @@ std::vector<Entity> Entities::getAllInRadius(glm::vec3 center, float radius) {
std::vector<Entity> collected;
auto view = registry.view<Transform>();
for (auto [entity, transform] : view.each()) {
if (glm::distance2(transform.pos, center) <= radius*radius) {
if (glm::distance2(transform.pos, center) <= radius * radius) {
const auto& found = uids.find(entity);
if (found == uids.end()) {
continue;
+38 -21
View File
@@ -1,19 +1,19 @@
#ifndef OBJECTS_ENTITIES_HPP_
#define OBJECTS_ENTITIES_HPP_
#include "../typedefs.hpp"
#include "../physics/Hitbox.hpp"
#include "../data/dynamic.hpp"
#include "../util/Clock.hpp"
#include <vector>
#include <glm/glm.hpp>
#include <memory>
#include <optional>
#include <glm/glm.hpp>
#include <vector>
#include "../data/dynamic.hpp"
#include "../physics/Hitbox.hpp"
#include "../typedefs.hpp"
#include "../util/Clock.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <entt/entity/registry.hpp>
#include <glm/gtx/norm.hpp>
#include <unordered_map>
#include <entt/entity/registry.hpp>
struct entity_funcs_set {
bool init;
@@ -48,7 +48,7 @@ struct Transform {
glm::vec3 displaySize;
void refresh();
inline void setRot(glm::mat3 m) {
rot = m;
dirty = true;
@@ -80,8 +80,11 @@ struct UserComponent {
entity_funcs_set funcsset;
scriptenv env;
UserComponent(const std::string& name, entity_funcs_set funcsset, scriptenv env)
: name(name), funcsset(funcsset), env(env) {}
UserComponent(
const std::string& name, entity_funcs_set funcsset, scriptenv env
)
: name(name), funcsset(funcsset), env(env) {
}
};
struct ScriptComponents {
@@ -115,11 +118,12 @@ class Entity {
public:
Entity(
Entities& entities,
entityid_t id,
entt::registry& registry,
entityid_t id,
entt::registry& registry,
const entt::entity entity
)
: entities(entities), id(id), registry(registry), entity(entity) {}
: entities(entities), id(id), registry(registry), entity(entity) {
}
EntityId& getID() const {
return registry.get<EntityId>(entity);
@@ -186,15 +190,24 @@ public:
void updatePhysics(float delta);
void update(float delta);
void renderDebug(LineBatch& batch, const Frustum* frustum, const DrawContext& ctx);
void render(Assets* assets, ModelBatch& batch, const Frustum* frustum, float delta, bool pause);
void renderDebug(
LineBatch& batch, const Frustum* frustum, const DrawContext& ctx
);
void render(
Assets* assets,
ModelBatch& batch,
const Frustum* frustum,
float delta,
bool pause
);
entityid_t spawn(
EntityDef& def,
glm::vec3 position,
dynamic::Map_sptr args=nullptr,
dynamic::Map_sptr saved=nullptr,
entityid_t uid=0);
dynamic::Map_sptr args = nullptr,
dynamic::Map_sptr saved = nullptr,
entityid_t uid = 0
);
std::optional<Entity> get(entityid_t id) {
const auto& found = entities.find(id);
@@ -212,7 +225,11 @@ public:
/// @param ignore Ignored entity ID
/// @return An optional structure containing entity, normal and distance
std::optional<RaycastResult> rayCast(
glm::vec3 start, glm::vec3 dir, float maxDistance, entityid_t ignore=-1);
glm::vec3 start,
glm::vec3 dir,
float maxDistance,
entityid_t ignore = -1
);
void loadEntities(dynamic::Map_sptr map);
void loadEntity(const dynamic::Map_sptr& map);
@@ -237,4 +254,4 @@ public:
}
};
#endif // OBJECTS_ENTITIES_HPP_
#endif // OBJECTS_ENTITIES_HPP_
+61 -58
View File
@@ -1,20 +1,20 @@
#include "Player.hpp"
#include "../content/ContentLUT.hpp"
#include "../physics/Hitbox.hpp"
#include "../physics/PhysicsSolver.hpp"
#include "../voxels/Chunks.hpp"
#include "../world/Level.hpp"
#include "../window/Events.hpp"
#include "../window/Camera.hpp"
#include "../items/Inventory.hpp"
#include "../objects/Entities.hpp"
#include "../objects/rigging.hpp"
#include <algorithm>
#include <glm/glm.hpp>
#include <utility>
#include "../content/ContentLUT.hpp"
#include "../items/Inventory.hpp"
#include "../objects/Entities.hpp"
#include "../objects/rigging.hpp"
#include "../physics/Hitbox.hpp"
#include "../physics/PhysicsSolver.hpp"
#include "../voxels/Chunks.hpp"
#include "../window/Camera.hpp"
#include "../window/Events.hpp"
#include "../world/Level.hpp"
const float CROUCH_SPEED_MUL = 0.35f;
const float RUN_SPEED_MUL = 1.5f;
const float PLAYER_GROUND_DAMPING = 10.0f;
@@ -23,19 +23,23 @@ const float FLIGHT_SPEED_MUL = 4.0f;
const float CHEAT_SPEED_MUL = 5.0f;
const float JUMP_FORCE = 8.0f;
Player::Player(Level* level, glm::vec3 position, float speed,
std::shared_ptr<Inventory> inv, entityid_t eid) :
level(level),
speed(speed),
chosenSlot(0),
position(position),
inventory(std::move(inv)),
eid(eid),
camera(level->getCamera("base:first-person")),
spCamera(level->getCamera("base:third-person-front")),
tpCamera(level->getCamera("base:third-person-back")),
currentCamera(camera)
{
Player::Player(
Level* level,
glm::vec3 position,
float speed,
std::shared_ptr<Inventory> inv,
entityid_t eid
)
: level(level),
speed(speed),
chosenSlot(0),
position(position),
inventory(std::move(inv)),
eid(eid),
camera(level->getCamera("base:first-person")),
spCamera(level->getCamera("base:third-person-front")),
tpCamera(level->getCamera("base:third-person-back")),
currentCamera(camera) {
camera->setFov(glm::radians(90.0f));
spCamera->setFov(glm::radians(90.0f));
tpCamera->setFov(glm::radians(90.0f));
@@ -69,10 +73,10 @@ void Player::updateInput(PlayerInput& input, float delta) {
}
bool crouch = input.shift && hitbox->grounded && !input.sprint;
float speed = this->speed;
if (flight){
if (flight) {
speed *= FLIGHT_SPEED_MUL;
}
if (input.cheat){
if (input.cheat) {
speed *= CHEAT_SPEED_MUL;
}
@@ -83,20 +87,20 @@ void Player::updateInput(PlayerInput& input, float delta) {
speed *= RUN_SPEED_MUL;
}
glm::vec3 dir(0,0,0);
if (input.moveForward){
glm::vec3 dir(0, 0, 0);
if (input.moveForward) {
dir += camera->dir;
}
if (input.moveBack){
if (input.moveBack) {
dir -= camera->dir;
}
if (input.moveRight){
if (input.moveRight) {
dir += camera->right;
}
if (input.moveLeft){
if (input.moveLeft) {
dir -= camera->right;
}
if (glm::length(dir) > 0.0f){
if (glm::length(dir) > 0.0f) {
dir = glm::normalize(dir);
hitbox->velocity += dir * speed * delta * 9.0f;
}
@@ -104,12 +108,12 @@ void Player::updateInput(PlayerInput& input, float delta) {
hitbox->linearDamping = PLAYER_GROUND_DAMPING;
hitbox->verticalDamping = flight;
hitbox->gravityScale = flight ? 0.0f : 1.0f;
if (flight){
if (flight) {
hitbox->linearDamping = PLAYER_AIR_DAMPING;
if (input.jump){
if (input.jump) {
hitbox->velocity.y += speed * delta * 9;
}
if (input.shift){
if (input.shift) {
hitbox->velocity.y -= speed * delta * 9;
}
}
@@ -117,14 +121,13 @@ void Player::updateInput(PlayerInput& input, float delta) {
hitbox->linearDamping = PLAYER_AIR_DAMPING;
}
if (input.jump && hitbox->grounded){
if (input.jump && hitbox->grounded) {
hitbox->velocity.y = JUMP_FORCE;
}
if ((input.flight && !noclip) ||
(input.noclip && flight == noclip)){
if ((input.flight && !noclip) || (input.noclip && flight == noclip)) {
flight = !flight;
if (flight){
if (flight) {
hitbox->velocity.y += 1.0f;
}
}
@@ -163,10 +166,10 @@ void Player::postUpdate() {
size_t bodyIndex = skeleton.config->find("body")->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), glm::vec3(0, 1, 0));
skeleton.pose.matrices[headIndex] = glm::rotate(
glm::mat4(1.0f), glm::radians(cam.y), glm::vec3(1, 0, 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) {
@@ -177,16 +180,17 @@ void Player::teleport(glm::vec3 position) {
}
void Player::attemptToFindSpawnpoint() {
glm::vec3 newpos (
glm::vec3 newpos(
position.x + (rand() % 200 - 100),
rand() % 80 + 100,
position.z + (rand() % 200 - 100)
);
while (newpos.y > 0 && !level->chunks->isObstacleBlock(newpos.x, newpos.y-2, newpos.z)) {
while (newpos.y > 0 &&
!level->chunks->isObstacleBlock(newpos.x, newpos.y - 2, newpos.z)) {
newpos.y--;
}
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) ||
headvox == nullptr || headvox->id != 0) {
return;
@@ -269,16 +273,19 @@ std::unique_ptr<dynamic::Map> Player::serialize() const {
root->put("chosen-slot", chosenSlot);
root->put("entity", eid);
root->put("inventory", inventory->serialize());
auto found = std::find(
level->cameras.begin(), level->cameras.end(), currentCamera);
auto found =
std::find(level->cameras.begin(), level->cameras.end(), currentCamera);
if (found != level->cameras.end()) {
root->put("camera", level->content->getIndices(
ResourceType::CAMERA).getName(found - level->cameras.begin()));
root->put(
"camera",
level->content->getIndices(ResourceType::CAMERA)
.getName(found - level->cameras.begin())
);
}
return root;
}
void Player::deserialize(dynamic::Map *src) {
void Player::deserialize(dynamic::Map* src) {
auto posarr = src->list("position");
position.x = posarr->num(0);
position.y = posarr->num(1);
@@ -294,11 +301,7 @@ void Player::deserialize(dynamic::Map *src) {
if (src->has("spawnpoint")) {
auto sparr = src->list("spawnpoint");
setSpawnPoint(glm::vec3(
sparr->num(0),
sparr->num(1),
sparr->num(2)
));
setSpawnPoint(glm::vec3(sparr->num(0), sparr->num(1), sparr->num(2)));
} else {
setSpawnPoint(position);
}
@@ -307,11 +310,11 @@ void Player::deserialize(dynamic::Map *src) {
src->flag("noclip", noclip);
setChosenSlot(src->get("chosen-slot", getChosenSlot()));
src->num("entity", eid);
if (auto invmap = src->map("inventory")) {
getInventory()->deserialize(invmap.get());
}
if (src->has("camera")) {
std::string name;
src->str("camera", name);
@@ -330,7 +333,7 @@ void Player::convert(dynamic::Map* data, const ContentLUT* lut) {
Inventory::convert(inventory.get(), lut);
}
}
} else {
if (auto inventory = data->map("inventory")) {
Inventory::convert(inventory.get(), lut);
+17 -12
View File
@@ -1,15 +1,15 @@
#ifndef SRC_OBJECTS_PLAYER_HPP_
#define SRC_OBJECTS_PLAYER_HPP_
#include "../settings.hpp"
#include "../data/dynamic.hpp"
#include "../voxels/voxel.hpp"
#include "../interfaces/Serializable.hpp"
#include "../interfaces/Object.hpp"
#include <glm/glm.hpp>
#include <memory>
#include <optional>
#include <glm/glm.hpp>
#include "../data/dynamic.hpp"
#include "../interfaces/Object.hpp"
#include "../interfaces/Serializable.hpp"
#include "../settings.hpp"
#include "../voxels/voxel.hpp"
class Camera;
class Inventory;
@@ -60,8 +60,13 @@ public:
glm::vec3 cam {};
CursorSelection selection {};
Player(Level* level, glm::vec3 position, float speed,
std::shared_ptr<Inventory> inv, entityid_t eid);
Player(
Level* level,
glm::vec3 position,
float speed,
std::shared_ptr<Inventory> inv,
entityid_t eid
);
~Player();
void teleport(glm::vec3 position);
@@ -87,7 +92,7 @@ public:
void setEntity(entityid_t eid);
entityid_t getSelectedEntity() const;
std::shared_ptr<Inventory> getInventory() const;
glm::vec3 getPosition() const {
@@ -100,7 +105,7 @@ public:
glm::vec3 getSpawnPoint() const;
std::unique_ptr<dynamic::Map> serialize() const override;
void deserialize(dynamic::Map *src) override;
void deserialize(dynamic::Map* src) override;
static void convert(dynamic::Map* data, const ContentLUT* lut);
@@ -109,4 +114,4 @@ public:
}
};
#endif // SRC_OBJECTS_PLAYER_HPP_
#endif // SRC_OBJECTS_PLAYER_HPP_
+43 -32
View File
@@ -1,14 +1,14 @@
#include "rigging.hpp"
#include "../assets/Assets.hpp"
#include "../graphics/render/ModelBatch.hpp"
#include "../graphics/core/Model.hpp"
#include "../coders/json.hpp"
#include "../data/dynamic_util.hpp"
#include "../graphics/core/Model.hpp"
#include "../graphics/render/ModelBatch.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/norm.hpp>
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtx/norm.hpp>
using namespace rigging;
@@ -20,17 +20,18 @@ void ModelReference::refresh(const Assets* assets) {
}
Bone::Bone(
size_t index,
std::string name,
size_t index,
std::string name,
std::string model,
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})
{}
glm::vec3 offset
)
: index(index),
name(std::move(name)),
bones(std::move(bones)),
offset(offset),
model({model, nullptr, true}) {
}
void Bone::setModel(const std::string& name) {
if (model.name == name) {
@@ -39,7 +40,7 @@ void Bone::setModel(const std::string& name) {
model = {name, nullptr, true};
}
Skeleton::Skeleton(const SkeletonConfig* config)
Skeleton::Skeleton(const SkeletonConfig* config)
: config(config),
pose(config->getBones().size()),
calculated(config->getBones().size()),
@@ -60,27 +61,31 @@ static void get_all_nodes(std::vector<Bone*>& nodes, Bone* node) {
}
}
SkeletonConfig::SkeletonConfig(const std::string& name, std::unique_ptr<Bone> root, size_t nodesCount)
: name(name), root(std::move(root)), nodes(nodesCount) {
SkeletonConfig::SkeletonConfig(
const std::string& name, std::unique_ptr<Bone> root, size_t nodesCount
)
: name(name), root(std::move(root)), nodes(nodesCount) {
get_all_nodes(nodes, this->root.get());
}
size_t SkeletonConfig::update(
size_t index,
Skeleton& skeleton,
Bone* node,
glm::mat4 matrix) const
{
size_t index, Skeleton& skeleton, Bone* node, glm::mat4 matrix
) const {
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;
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]);
count += update(
index + count,
skeleton,
subnode.get(),
skeleton.calculated.matrices[index]
);
}
return count;
}
@@ -93,8 +98,8 @@ void SkeletonConfig::render(
Assets* assets,
ModelBatch& batch,
Skeleton& skeleton,
const glm::mat4& matrix) const
{
const glm::mat4& matrix
) const {
update(skeleton, matrix);
if (!skeleton.visible) {
@@ -113,8 +118,12 @@ void SkeletonConfig::render(
}
model = modelOverride.model ? modelOverride.model : model;
if (model) {
batch.draw(skeleton.calculated.matrices[i], skeleton.tint, model,
&skeleton.textures);
batch.draw(
skeleton.calculated.matrices[i],
skeleton.tint,
model,
&skeleton.textures
);
}
}
}
@@ -145,19 +154,19 @@ static std::tuple<size_t, std::unique_ptr<Bone>> read_node(
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, 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), offset)};
return {
count,
std::make_unique<Bone>(index, name, model, std::move(bones), offset)};
}
std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(
std::string_view src,
std::string_view file,
std::string_view name
std::string_view src, std::string_view file, std::string_view name
) {
auto root = json::parse(file, src);
auto rootNodeMap = root->map("root");
@@ -165,5 +174,7 @@ std::unique_ptr<SkeletonConfig> SkeletonConfig::parse(
throw std::runtime_error("missing 'root' element");
}
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
);
}
+24 -23
View File
@@ -1,13 +1,13 @@
#ifndef OBJECTS_RIGGING_HPP_
#define OBJECTS_RIGGING_HPP_
#include "../typedefs.hpp"
#include <vector>
#include <glm/glm.hpp>
#include <memory>
#include <string>
#include <glm/glm.hpp>
#include <unordered_map>
#include <vector>
#include "../typedefs.hpp"
class Assets;
class ModelBatch;
@@ -44,18 +44,19 @@ namespace rigging {
public:
ModelReference model;
Bone(
size_t index,
size_t index,
std::string name,
std::string model,
std::vector<std::unique_ptr<Bone>> bones,
glm::vec3 offset);
glm::vec3 offset
);
void setModel(const std::string& name);
const std::string& getName() const {
return name;
}
size_t getIndex() const {
return index;
}
@@ -70,7 +71,7 @@ namespace rigging {
};
struct BoneFlags {
bool visible: 1;
bool visible : 1;
};
struct Skeleton {
@@ -85,12 +86,12 @@ namespace rigging {
Skeleton(const SkeletonConfig* config);
};
class SkeletonConfig {
std::string name;
std::unique_ptr<Bone> root;
std::unordered_map<std::string, size_t> indices;
/// Nodes and indices are ordered from root to bones.
/// Example:
/// 0 - root
@@ -100,20 +101,22 @@ namespace rigging {
std::vector<Bone*> nodes;
size_t update(
size_t index,
Skeleton& skeleton,
Bone* node,
glm::mat4 matrix) const;
size_t index, Skeleton& skeleton, Bone* node, glm::mat4 matrix
) const;
public:
SkeletonConfig(const std::string& name, std::unique_ptr<Bone> root,
size_t nodesCount);
SkeletonConfig(
const std::string& name,
std::unique_ptr<Bone> root,
size_t nodesCount
);
void update(Skeleton& skeleton, glm::mat4 matrix) const;
void render(
Assets* assets,
ModelBatch& batch,
Skeleton& skeleton,
const glm::mat4& matrix) const;
Skeleton& skeleton,
const glm::mat4& matrix
) const;
Skeleton instance() const {
return Skeleton(this);
@@ -122,15 +125,13 @@ namespace rigging {
Bone* find(std::string_view str) const;
static std::unique_ptr<SkeletonConfig> parse(
std::string_view src,
std::string_view file,
std::string_view name
std::string_view src, std::string_view file, std::string_view name
);
const std::vector<Bone*>& getBones() const {
return nodes;
}
const std::string& getName() const {
return name;
}
@@ -141,4 +142,4 @@ namespace rigging {
};
};
#endif // OBJECTS_RIGGING_HPP_
#endif // OBJECTS_RIGGING_HPP_