mno
This commit is contained in:
parent
bb6eab5fdb
commit
3f33390ad7
@ -1,3 +1,3 @@
|
||||
#pragma once
|
||||
#include <entt/entt.hpp>
|
||||
#include "world.hpp"
|
||||
#include "game.hpp"
|
||||
143
src/tsr/entity_model.cpp
Normal file
143
src/tsr/entity_model.cpp
Normal file
@ -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<ModelComponent>(ent);
|
||||
comp.model = AssetMap::Get<Model>(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<AnimationComponent>(ent);
|
||||
else
|
||||
reg.erase<AnimationComponent>(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<ModelComponent>(ent);
|
||||
reg.erase<AnimationComponent>(ent);
|
||||
}
|
||||
|
||||
void ModelSystem::Anim(entt::entity ent, const std::string& anim_name) {
|
||||
auto& comp = Game::Registry().get<ModelComponent>(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<ModelComponent>(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<AnimationComponent, ModelComponent>();
|
||||
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<ModelComponent, TransformComponent>();
|
||||
//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<ModelComponent, TransformComponent>();
|
||||
for (auto [ent, model, trans] : v.each()) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//entt::view<MeshComponent> v(World::Get().Reg());
|
||||
|
||||
|
||||
//World::Get().Reg()
|
||||
|
||||
}
|
||||
39
src/tsr/entity_model.hpp
Normal file
39
src/tsr/entity_model.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include "entity.hpp"
|
||||
#include "model.hpp"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include "../tsr/renderer.hpp"
|
||||
|
||||
namespace TSR {
|
||||
|
||||
struct ModelAnimation {
|
||||
float t;
|
||||
};
|
||||
|
||||
struct ModelComponent {
|
||||
AssetPtr<Model> model;
|
||||
TSR::DrawCtx ctx;
|
||||
std::vector<glm::mat4> bone_transforms_mat;
|
||||
std::vector<BoneTransform> bone_transforms[2];
|
||||
std::map<AnimationId, ModelAnimation> 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);
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
8
src/tsr/game.cpp
Normal file
8
src/tsr/game.cpp
Normal file
@ -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;
|
||||
34
src/tsr/game.hpp
Normal file
34
src/tsr/game.hpp
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
#include <memory>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
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; }
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -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<SkAnimFrame> frames;
|
||||
std::vector<BoneTransform> frames;
|
||||
};
|
||||
|
||||
//struct SkAnim {
|
||||
|
||||
@ -116,8 +116,19 @@ Model::Model(const std::string& name) {
|
||||
anim.skel = AssetMap::Get<SkAnim>(anim_json.at("skel").get<std::string>());
|
||||
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<float>();
|
||||
anim.tps = anim_json.at("tps").get<float>();
|
||||
}
|
||||
|
||||
anim.is_cyclic = false;
|
||||
if (anim_json.contains("cyclic"))
|
||||
anim.is_cyclic = anim_json.at("cyclic").get<bool>();
|
||||
|
||||
// events
|
||||
}
|
||||
}
|
||||
|
||||
@ -29,17 +29,23 @@ namespace TSR {
|
||||
struct Animation {
|
||||
AssetPtr<SkAnim> skel;
|
||||
std::vector<int> bone_ids;
|
||||
float duration;
|
||||
float tps;
|
||||
bool is_cyclic;
|
||||
/* events */
|
||||
};
|
||||
|
||||
typedef int AnimationId;
|
||||
typedef int PartId;
|
||||
|
||||
class Model : public Asset {
|
||||
AssetPtr<Skeleton> m_skeleton;
|
||||
|
||||
std::vector<Drawable> m_parts;
|
||||
std::map<std::string, int> m_part_names;
|
||||
std::map<std::string, PartId> m_part_names;
|
||||
|
||||
std::vector<Animation> m_animations;
|
||||
std::map<std::string, int> m_anim_names;
|
||||
std::map<std::string, AnimationId> 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; }
|
||||
};
|
||||
|
||||
}
|
||||
@ -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 {
|
||||
|
||||
@ -1,26 +0,0 @@
|
||||
#include "rendersystem.hpp"
|
||||
|
||||
using namespace TSR;
|
||||
|
||||
void RenderSystem::Render(TSR::Renderer& renderer) {
|
||||
|
||||
//auto v = World::Registry().view<ModelComponent, TransformComponent>();
|
||||
//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<MeshComponent> v(World::Get().Reg());
|
||||
|
||||
|
||||
//World::Get().Reg()
|
||||
|
||||
}
|
||||
@ -1,25 +0,0 @@
|
||||
#pragma once
|
||||
#include "systems.hpp"
|
||||
#include <vector>
|
||||
#include "../tsr/renderer.hpp"
|
||||
|
||||
namespace TSR {
|
||||
|
||||
struct ModelComponent {
|
||||
TSR::DrawCtx ctx;
|
||||
|
||||
std::vector<glm::mat4> bone_transforms;
|
||||
|
||||
|
||||
};
|
||||
|
||||
class RenderSystem {
|
||||
public:
|
||||
|
||||
static void Render(TSR::Renderer& renderer);
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
@ -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 {
|
||||
|
||||
@ -1,6 +0,0 @@
|
||||
#include "world.hpp"
|
||||
|
||||
using namespace TSR;
|
||||
|
||||
//std::unique_ptr<World> World::s_world;
|
||||
entt::registry World::s_registry;
|
||||
@ -1,33 +0,0 @@
|
||||
#pragma once
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#include <glm/gtx/quaternion.hpp>
|
||||
#include <memory>
|
||||
#include <entt/entt.hpp>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -131,8 +131,8 @@
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\tsr\camera.cpp" />
|
||||
<ClCompile Include="src\tsr\model.cpp" />
|
||||
<ClCompile Include="src\tsr\rendersystem.cpp" />
|
||||
<ClCompile Include="src\tsr\world.cpp" />
|
||||
<ClCompile Include="src\tsr\entity_model.cpp" />
|
||||
<ClCompile Include="src\tsr\game.cpp" />
|
||||
<ClCompile Include="src\tsr\assets.cpp" />
|
||||
<ClCompile Include="src\tsr\filesystem.cpp" />
|
||||
<ClCompile Include="src\tsr\ia.cpp" />
|
||||
@ -143,9 +143,9 @@
|
||||
<ItemGroup>
|
||||
<ClInclude Include="src\tsr\camera.hpp" />
|
||||
<ClInclude Include="src\tsr\model.hpp" />
|
||||
<ClInclude Include="src\tsr\rendersystem.hpp" />
|
||||
<ClInclude Include="src\tsr\systems.hpp" />
|
||||
<ClInclude Include="src\tsr\world.hpp" />
|
||||
<ClInclude Include="src\tsr\entity_model.hpp" />
|
||||
<ClInclude Include="src\tsr\entity.hpp" />
|
||||
<ClInclude Include="src\tsr\game.hpp" />
|
||||
<ClInclude Include="src\tsr\assets.hpp" />
|
||||
<ClInclude Include="src\tsr\filesystem.hpp" />
|
||||
<ClInclude Include="src\tsr\ia.hpp" />
|
||||
|
||||
@ -33,10 +33,10 @@
|
||||
<ClCompile Include="src\tsr\ia.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\tsr\world.cpp">
|
||||
<ClCompile Include="src\tsr\game.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\tsr\rendersystem.cpp">
|
||||
<ClCompile Include="src\tsr\entity_model.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\tsr\model.cpp">
|
||||
@ -65,13 +65,13 @@
|
||||
<ClInclude Include="src\tsr\tsr.hpp">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\tsr\world.hpp">
|
||||
<ClInclude Include="src\tsr\game.hpp">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\tsr\rendersystem.hpp">
|
||||
<ClInclude Include="src\tsr\entity_model.hpp">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\tsr\systems.hpp">
|
||||
<ClInclude Include="src\tsr\entity.hpp">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\tsr\model.hpp">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user