diff --git a/src/tsr/systems.hpp b/src/tsr/entity.hpp similarity index 65% rename from src/tsr/systems.hpp rename to src/tsr/entity.hpp index 2b9a3b2..c516998 100644 --- a/src/tsr/systems.hpp +++ b/src/tsr/entity.hpp @@ -1,3 +1,3 @@ #pragma once #include -#include "world.hpp" \ No newline at end of file +#include "game.hpp" \ No newline at end of file diff --git a/src/tsr/entity_model.cpp b/src/tsr/entity_model.cpp new file mode 100644 index 0000000..7cb554a --- /dev/null +++ b/src/tsr/entity_model.cpp @@ -0,0 +1,143 @@ +#include "entity_model.hpp" + +using namespace TSR; + +void ModelSystem::AddModel(entt::entity ent, const std::string& name) { + auto& reg = Game::Registry(); + auto& comp = reg.emplace_or_replace(ent); + comp.model = AssetMap::Get(name); + + if (comp.model->IsSkeletal()) { + auto num_bones = comp.model->GetSkeleton().Bones().size(); + comp.bone_transforms_mat.resize(num_bones); + comp.bone_transforms[0].resize(num_bones); + comp.bone_transforms[1].resize(num_bones); + } + + if (comp.model->HasAnimations()) + reg.emplace_or_replace(ent); + else + reg.erase(ent); + + for (int i = 0; i < TSR_NUM_COLORS; ++i) + comp.ctx.colors[i] = glm::vec4(1.0f); + + comp.spawned_at = Game::FrameNum(); + + auto& bone_transforms = comp.bone_transforms[Game::FrameNum() % 2]; + for (auto& t : bone_transforms) { + t.pos = glm::vec3(0.0f); + t.rot = glm::quat(glm::mat4(1.0f)); + t.scl = glm::vec3(1.0f); + } +} + +void TSR::ModelSystem::RemoveModel(entt::entity ent) { + auto& reg = Game::Registry(); + reg.erase(ent); + reg.erase(ent); +} + +void ModelSystem::Anim(entt::entity ent, const std::string& anim_name) { + auto& comp = Game::Registry().get(ent); + auto id = comp.model->GetAnimId(anim_name); + + auto& anim = comp.animations[id]; + anim.t = 0.0f; + +} + +void ModelSystem::StopAnim(entt::entity ent, const std::string& anim_name) { + auto& comp = Game::Registry().get(ent); + auto id = comp.model->GetAnimId(anim_name); + + comp.animations.erase(id); +} + +void ModelSystem::Animate() { + auto frame_idx = Game::FrameNum() % 2; + + float frame_time = 1.0f / (float)Game::TPS(); + + auto v = Game::Registry().view(); + for (auto [ent, model] : v.each()) { + auto& anims = model.animations; + auto& bone_transforms = model.bone_transforms[frame_idx]; + + for (auto it = anims.begin(); it != anims.end(); ) { + bool remove = false; + const auto& anim = model.model->GetAnim(it->first); + auto& anim_data = it->second; + + anim_data.t += anim.tps * frame_time; + + if (anim_data.t > anim.duration) { + if (anim.is_cyclic) { + anim_data.t = glm::mod(anim_data.t, anim.duration); + } + else { + anim_data.t = anim.duration; + remove = true; + } + } + + if (anim.skel) { + auto& skel_anim = *anim.skel; + + for (int i = 0; i < anim.skel->Channels().size(); ++i) { + + int fr1 = glm::floor(anim_data.t); + int fr2 = glm::ceil(anim_data.t); + float t = glm::fract(anim_data.t); + + const auto& frame1 = anim.skel->Channels()[i].frames[fr1]; + const auto& frame2 = anim.skel->Channels()[i].frames[fr2]; + + auto& trans = bone_transforms[anim.bone_ids[i]]; + trans.pos = glm::mix(frame1.pos, frame2.pos, t); + trans.rot = glm::slerp(frame1.rot, frame2.rot, t); + trans.scl = glm::mix(frame1.scl, frame2.scl, t); + } + } + + if (remove) + it = anims.erase(it); + else + ++it; + } + + + } + +} + +void ModelSystem::Render(TSR::Renderer& renderer) { + + //auto v = World::Registry().view(); + //for (auto [ent, mesh, render, trans] : v.each()) { + // render.ctx.m_model_matrix = glm::toMat4(trans.rot) * glm::translate(glm::mat4(1.0f), trans.pos); + // + // for (auto& drawable : mesh.parts) + // renderer.Add(TSR::DrawRef(&drawable, &render.ctx)); + + + //} + + auto frame_t = Game::FrameT(); + auto frame_idx1 = Game::FrameNum() % 2; + auto frame_idx0 = 1 - frame_idx1; + + auto v = Game::Registry().view(); + for (auto [ent, model, trans] : v.each()) { + + } + + + + + //entt::view v(World::Get().Reg()); + + + //World::Get().Reg() + +} \ No newline at end of file diff --git a/src/tsr/entity_model.hpp b/src/tsr/entity_model.hpp new file mode 100644 index 0000000..7eb4f7f --- /dev/null +++ b/src/tsr/entity_model.hpp @@ -0,0 +1,39 @@ +#pragma once +#include "entity.hpp" +#include "model.hpp" +#include +#include +#include "../tsr/renderer.hpp" + +namespace TSR { + + struct ModelAnimation { + float t; + }; + + struct ModelComponent { + AssetPtr model; + TSR::DrawCtx ctx; + std::vector bone_transforms_mat; + std::vector bone_transforms[2]; + std::map animations; + uint64_t spawned_at; + }; + + struct AnimationComponent {}; + + class ModelSystem { + public: + static void AddModel(entt::entity ent, const std::string& name); + static void RemoveModel(entt::entity ent); + static void Anim(entt::entity ent, const std::string& anim_name); + static void StopAnim(entt::entity ent, const std::string& anim_name); + + static void Animate(); + static void Render(Renderer& renderer); + + + + }; + +} diff --git a/src/tsr/game.cpp b/src/tsr/game.cpp new file mode 100644 index 0000000..478835e --- /dev/null +++ b/src/tsr/game.cpp @@ -0,0 +1,8 @@ +#include "game.hpp" + +using namespace TSR; + +entt::registry Game::s_registry; +uint64_t Game::s_sv_frame = 0; + float Game::s_frame_t = 0.0f; + int Game::s_tps = 50; \ No newline at end of file diff --git a/src/tsr/game.hpp b/src/tsr/game.hpp new file mode 100644 index 0000000..c094448 --- /dev/null +++ b/src/tsr/game.hpp @@ -0,0 +1,34 @@ +#pragma once +#include +#include +#include +#include +#include +#include + +namespace TSR { + + struct TransformComponent { + glm::vec3 pos[2]; + glm::quat rot[2]; + }; + + class Game { + static entt::registry s_registry; + static uint64_t s_sv_frame; + static float s_frame_t; + static int s_tps; + + public: + static void SvFrame(); + static void ClFrame(float frame_t); + + static entt::registry& Registry() { return s_registry; } + static uint64_t FrameNum() { return s_sv_frame; } + static float FrameT() { return s_frame_t; } + static int TPS() { return s_tps; } + }; + +} + + diff --git a/src/tsr/ia.hpp b/src/tsr/ia.hpp index 8e55192..2bf0414 100644 --- a/src/tsr/ia.hpp +++ b/src/tsr/ia.hpp @@ -22,7 +22,7 @@ namespace TSR { glm::mat4 offset; }; - struct SkAnimFrame { + struct BoneTransform { glm::vec3 pos; glm::quat rot; glm::vec3 scl; @@ -30,7 +30,7 @@ namespace TSR { struct SkAnimChannel { std::string name; - std::vector frames; + std::vector frames; }; //struct SkAnim { diff --git a/src/tsr/model.cpp b/src/tsr/model.cpp index 98e62de..cb0e8b8 100644 --- a/src/tsr/model.cpp +++ b/src/tsr/model.cpp @@ -116,8 +116,19 @@ Model::Model(const std::string& name) { anim.skel = AssetMap::Get(anim_json.at("skel").get()); for (const auto& chan : anim.skel->Channels()) anim.bone_ids.push_back(m_skeleton->GetBoneIdByName(chan.name)); + + anim.duration = anim.skel->Duration(); + anim.tps = anim.skel->TPS(); + } + else { + anim.duration = anim_json.at("duration").get(); + anim.tps = anim_json.at("tps").get(); } + anim.is_cyclic = false; + if (anim_json.contains("cyclic")) + anim.is_cyclic = anim_json.at("cyclic").get(); + // events } } diff --git a/src/tsr/model.hpp b/src/tsr/model.hpp index 31be706..d4eae24 100644 --- a/src/tsr/model.hpp +++ b/src/tsr/model.hpp @@ -29,17 +29,23 @@ namespace TSR { struct Animation { AssetPtr skel; std::vector bone_ids; + float duration; + float tps; + bool is_cyclic; /* events */ }; + typedef int AnimationId; + typedef int PartId; + class Model : public Asset { AssetPtr m_skeleton; std::vector m_parts; - std::map m_part_names; + std::map m_part_names; std::vector m_animations; - std::map m_anim_names; + std::map m_anim_names; public: @@ -60,6 +66,8 @@ namespace TSR { const Skeleton& GetSkeleton() const { return *m_skeleton; } int GetAnimId(const std::string& name) { return m_anim_names.at(name); } const Animation& GetAnim(int id) { return m_animations[id]; } + bool IsSkeletal() const { return m_skeleton.get() != nullptr; } + bool HasAnimations() const { return m_animations.size() > 0; } }; } \ No newline at end of file diff --git a/src/tsr/renderer.hpp b/src/tsr/renderer.hpp index eecdaa3..cbb7147 100644 --- a/src/tsr/renderer.hpp +++ b/src/tsr/renderer.hpp @@ -9,6 +9,7 @@ #include "assets.hpp" #include "ia.hpp" + namespace TSR { enum RenderType { @@ -93,7 +94,7 @@ namespace TSR { struct BasicDrawCtx { glm::mat4 model_matrix; - glm::vec4 colors[4]; + glm::vec4 colors[TSR_NUM_COLORS]; }; class InstancedSSBO { diff --git a/src/tsr/rendersystem.cpp b/src/tsr/rendersystem.cpp deleted file mode 100644 index 2ea9449..0000000 --- a/src/tsr/rendersystem.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "rendersystem.hpp" - -using namespace TSR; - -void RenderSystem::Render(TSR::Renderer& renderer) { - - //auto v = World::Registry().view(); - //for (auto [ent, mesh, render, trans] : v.each()) { - // render.ctx.m_model_matrix = glm::toMat4(trans.rot) * glm::translate(glm::mat4(1.0f), trans.pos); - // - // for (auto& drawable : mesh.parts) - // renderer.Add(TSR::DrawRef(&drawable, &render.ctx)); - - - //} - - - - - - //entt::view v(World::Get().Reg()); - - - //World::Get().Reg() - -} diff --git a/src/tsr/rendersystem.hpp b/src/tsr/rendersystem.hpp deleted file mode 100644 index 346fbd8..0000000 --- a/src/tsr/rendersystem.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "systems.hpp" -#include -#include "../tsr/renderer.hpp" - -namespace TSR { - - struct ModelComponent { - TSR::DrawCtx ctx; - - std::vector bone_transforms; - - - }; - - class RenderSystem { - public: - - static void Render(TSR::Renderer& renderer); - - - - }; - -} diff --git a/src/tsr/tsr.hpp b/src/tsr/tsr.hpp index aa87688..f6b7580 100644 --- a/src/tsr/tsr.hpp +++ b/src/tsr/tsr.hpp @@ -22,6 +22,8 @@ #define TSR_NO_BONE 255 #define TSR_MAX_BONES TSR_NO_BONE +#define TSR_NUM_COLORS 4 + #define U8(_S) (const char*)u8##_S namespace TSR { diff --git a/src/tsr/world.cpp b/src/tsr/world.cpp deleted file mode 100644 index c100111..0000000 --- a/src/tsr/world.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "world.hpp" - -using namespace TSR; - -//std::unique_ptr World::s_world; -entt::registry World::s_registry; \ No newline at end of file diff --git a/src/tsr/world.hpp b/src/tsr/world.hpp deleted file mode 100644 index 3f9f8d6..0000000 --- a/src/tsr/world.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once -#include -#include -#include -#include -#include -#include - -namespace TSR { - - struct TransformComponent { - glm::vec3 pos; - glm::quat rot; - }; - - class World { - static entt::registry s_registry; - - //entt::registry m_reg; - - - - public: - - static entt::registry& Registry() { - return s_registry; - } - - }; - -} - - diff --git a/tsrecs.vcxproj b/tsrecs.vcxproj index 4f643c5..90d39ba 100644 --- a/tsrecs.vcxproj +++ b/tsrecs.vcxproj @@ -131,8 +131,8 @@ - - + + @@ -143,9 +143,9 @@ - - - + + + diff --git a/tsrecs.vcxproj.filters b/tsrecs.vcxproj.filters index 3d11d62..8df5ece 100644 --- a/tsrecs.vcxproj.filters +++ b/tsrecs.vcxproj.filters @@ -33,10 +33,10 @@ Source Files - + Source Files - + Source Files @@ -65,13 +65,13 @@ Header Files - + Header Files - + Header Files - + Header Files