Changed up how resources import/export files. Added Model codecs.

This commit is contained in:
2024-02-20 22:47:52 -08:00
parent 54012df3a1
commit 93f881cf03
20 changed files with 998 additions and 882 deletions

View File

@@ -0,0 +1,40 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Array.h"
#include "KeyFrame.h"
namespace ehs
{
class AnimBone
{
private:
UInt_8 boneId;
Array<KeyFrame> keyFrames;
public:
AnimBone();
AnimBone(const UInt_8 boneId);
AnimBone(const UInt_8 boneId, const UInt_64 size);
AnimBone(const UInt_8 boneId, Array<KeyFrame> keyFrames);
AnimBone(AnimBone&& anim) noexcept;
AnimBone(const AnimBone& anim);
AnimBone& operator=(AnimBone&& anim) noexcept;
AnimBone& operator=(const AnimBone& anim);
UInt_8 GetBoneId() const;
Array<KeyFrame> GetKeyFrames() const;
Array<KeyFrame>& GetKeyFrames();
float GetPrevAndNext(KeyFrame& prev, KeyFrame& next, const float elapsed) const;
};
}

View File

@@ -0,0 +1,49 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "ehs/Array.h"
#include "AnimBone.h"
namespace ehs
{
class Animation
{
private:
UInt_64 hashId;
Str_8 id;
float duration;
Array<AnimBone> animated;
public:
Animation();
Animation(Str_8 id, const float duration);
Animation(Str_8 id, const float duration, UInt_64 size);
Animation(Str_8 id, const float duration, Array<AnimBone> animated);
Animation(Animation&& anim) noexcept;
Animation(const Animation& anim);
Animation& operator=(Animation&& anim) noexcept;
Animation& operator=(const Animation& anim);
UInt_64 GetHashId() const;
void SetId(Str_8 newId);
Str_8 GetId() const;
float GetDuration() const;
Array<AnimBone> GetAnimated() const;
Array<AnimBone>& GetAnimated();
Array<Mat4_f> Interpolate(const UInt_64 boneCount, const float elapsed) const;
};
}

73
include/ehs/io/mdl/Bone.h Normal file
View File

@@ -0,0 +1,73 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Quat.h"
#include "ehs/Mat4.h"
namespace ehs
{
class Bone
{
private:
UInt_64 hashName;
Str_8 name;
UInt_8 id;
Mat4_f animTrans;
Mat4_f localBindTrans;
Mat4_f invBindTrans;
Array<Bone> children;
public:
Bone();
Bone(Str_8 name, UInt_8 id, const Mat4_f& localBindTrans, const Mat4_f& invBindTrans);
Bone(Bone&& bone) noexcept;
Bone(const Bone& bone);
Bone& operator=(Bone&& bone) noexcept;
Bone& operator=(const Bone& bone);
UInt_64 GetHashName() const;
void SetName(Str_8 newId);
Str_8 GetName() const;
UInt_8 GetId() const;
void SetAnimTrans(const Mat4_f& newTrans);
Mat4_f GetAnimTrans() const;
void GetAnimTransRec(Array<Mat4_f>& output) const;
Mat4_f GetLocalBindTrans() const;
Mat4_f GetInvBindTrans() const;
UInt_8 GetBoneCount() const;
bool HasBone(UInt_64 hashName, UInt_8 id) const;
bool HasBone(UInt_64 hashName) const;
bool HasBone(UInt_8 id) const;
bool AddBone(Bone child);
const Bone* GetBone(UInt_64 hashName) const;
Bone* GetBone(UInt_64 hashName);
const Bone* GetBone(UInt_8 id) const;
Bone* GetBone(UInt_8 id);
const Array<Bone>& GetChildren() const;
Array<Bone>& GetChildren();
};
}

View File

@@ -0,0 +1,55 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Array.h"
#include "ehs/Vec3.h"
#include "ehs/Quat.h"
#include "ehs/Mat4.h"
#include "PropertyChange.h"
namespace ehs
{
class KeyFrame
{
private:
float num;
float timeStamp;
Vec3_f pos;
Quat_f rot;
Vec3_f scale;
Mat4_f trans;
public:
KeyFrame();
KeyFrame(const float num, const float timeStamp, const Vec3_f& pos, const Quat_f& rot, const Vec3_f& scale);
KeyFrame(const float num, const float timeStamp);
KeyFrame(const KeyFrame& kf);
KeyFrame& operator=(const KeyFrame& kf);
float GetNum() const;
float GetTimeStamp() const;
void SetPos(const Vec3_f& newPos);
Vec3_f GetPos() const;
void SetRot(const Quat_f& newRot);
Quat_f GetRot() const;
void SetScale(const Vec3_f& newScale);
Vec3_f GetScale() const;
void CalculateTransform();
Mat4_f GetTrans() const;
static Mat4_f Interpolate(const KeyFrame& prev, const KeyFrame& next, const float percentage);
};
}

93
include/ehs/io/mdl/Mdl.h Normal file
View File

@@ -0,0 +1,93 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Array.h"
#include "ehs/io/File.h"
#include "MdlCodec.h"
#include "Mesh.h"
#include "Bone.h"
#include "Animation.h"
namespace ehs
{
enum class ModelEncoding : UInt_8
{
EHM
};
class Mdl : public BaseObj
{
private:
static Array<MdlCodec> codecs;
protected:
UInt_64 hashId;
Str_8 id;
Array<Mesh> meshes;
Bone skeleton;
Array<Animation> animations;
public:
static bool HasCodec(UInt_64 hashExt);
static bool HasCodec(const Str_8& ext);
static bool AddCodec(MdlCodec codec);
static const MdlCodec* GetCodec(UInt_64 hashExt);
static const MdlCodec* GetCodec(const Str_8& ext);
Mdl();
Mdl(const Str_8& filePath);
Mdl(Str_8 id, Array<Mesh> meshes, Bone skeleton, Array<Animation> animations);
Mdl(Str_8 id, Array<Mesh> meshes, Bone skeleton);
Mdl(Str_8 id, Array<Mesh> meshes);
Mdl(Mdl&& model) noexcept;
Mdl(const Mdl& model) = default;
Mdl& operator=(Mdl&& model) noexcept;
Mdl& operator=(const Mdl& model) = default;
void Release();
UInt_64 GetHashId() const;
void SetId(Str_8 newId);
Str_8 GetId() const;
const Array<Mesh>& GetMeshes() const;
Array<Mesh>& GetMeshes();
Mesh* GetMesh(UInt_64 inHashId);
Mesh* GetMesh(const Str_8& inId);
const Bone& GetSkeleton() const;
Bone& GetSkeleton();
Animation* GetAnimation(UInt_64 inHashId);
const Array<Animation>& GetAnimations() const;
Array<Animation>& GetAnimations();
void Calculate();
bool Export(const Str_8& filePath) const;
};
bool EncodeEHM(const MdlCodec* codec, Serializer<UInt_64>& data, const Mdl* mdl);
bool DecodeEHM(const MdlCodec* codec, Serializer<UInt_64>& data, Mdl* mdl);
}

View File

@@ -0,0 +1,50 @@
#pragma once
#include "ehs/Str.h"
#include "ehs/Serializer.h"
#include "ehs/system/CPU.h"
namespace ehs
{
class Mdl;
class MdlCodec;
typedef bool (*EnocdeMdlCb)(const MdlCodec*, Serializer<UInt_64>&, const Mdl*);
typedef bool (*DecodeMdlCb)(const MdlCodec*, Serializer<UInt_64>&, Mdl*);
class MdlCodec
{
private:
Str_8 id;
UInt_64 hashExt;
Str_8 ext;
Endianness endianness;
EnocdeMdlCb encoder;
DecodeMdlCb decoder;
public:
MdlCodec();
MdlCodec(Str_8 id, Str_8 ext, Endianness end, EnocdeMdlCb encoder, DecodeMdlCb decoder);
MdlCodec(MdlCodec&& codec) noexcept;
MdlCodec(const MdlCodec& codec);
MdlCodec& operator=(MdlCodec&& codec) noexcept;
MdlCodec& operator=(const MdlCodec& codec);
Str_8 GetId() const;
UInt_64 GetHashExt() const;
Str_8 GetExt() const;
Endianness GetEndianness() const;
bool Encode(Serializer<UInt_64>& out, const Mdl* in) const;
bool Decode(Serializer<UInt_64>& in, Mdl* out) const;
};
}

80
include/ehs/io/mdl/Mesh.h Normal file
View File

@@ -0,0 +1,80 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Array.h"
#include "Vertex.h"
#include "ehs/BaseObj.h"
namespace ehs
{
class Mesh final : public BaseObj
{
protected:
UInt_64 hashId;
Str_8 id;
Array<Vertex_f> vertices;
Array<UInt_32> indices;
public:
Mesh();
Mesh(Str_8 id, Array<Vertex_f> vertices, Array<UInt_32> indices);
Mesh(Str_8 id, Array<Vertex_f> vertices);
Mesh(Mesh&& mesh) noexcept;
Mesh(const Mesh& mesh);
Mesh& operator=(Mesh&& mesh) noexcept;
Mesh& operator=(const Mesh& mesh);
void Release();
UInt_64 GetHashId() const;
void SetId(Str_8 newId);
Str_8 GetId() const;
void SetVertices(const Array<Vertex_f>& newVertices);
const Array<Vertex_f>& GetVertices() const;
Array<Vertex_f>& GetVertices();
void SetIndices(const Array<UInt_32>& newIndices);
bool HasIndices() const;
const Array<UInt_32>& GetIndices() const;
Array<UInt_32>& GetIndices();
void Calculate();
private:
static void Calculate(Vertex_f& vert1, Vertex_f& vert2, Vertex_f& vert3);
};
const Mesh portraitGui("PortraitGui",
{
{{0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}},
{{0.0f, 1.0f, 1.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f}},
{{1.0f, 0.0f, 1.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 0.0f}},
{{1.0f, 1.0f, 1.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 1.0f}}
},
{0, 1, 2, 3, 2, 1}
);
const Mesh portrait("Portrait",
{
{{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 0.0f}},
{{-0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, -1.0f}, {0.0f, 1.0f}},
{{0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 0.0f}},
{{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, -1.0f}, {1.0f, 1.0f}}
},
{0, 1, 2, 3, 2, 1}
);
}

View File

@@ -0,0 +1,32 @@
#pragma once
#include "ehs/EHS.h"
namespace ehs
{
enum class ChangeType : UInt_8
{
X_AXIS_POS,
Y_AXIS_POS,
Z_AXIS_POS,
X_AXIS_SCALE,
Y_AXIS_SCALE,
Z_AXIS_SCALE,
X_AXIS_ROT,
Y_AXIS_ROT,
Z_AXIS_ROT,
W_AXIS_ROT,
INVALID
};
class PropertyChange
{
public:
ChangeType type;
float value;
PropertyChange();
PropertyChange(const ChangeType type, const float value);
};
}

View File

@@ -0,0 +1,50 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Vec4.h"
#include "ehs/Vec3.h"
#include "ehs/Vec2.h"
#include "ehs/Color4.h"
namespace ehs
{
template<typename T = float>
class Vertex
{
public:
Vec3<T> pos;
Vec3<T> normal;
Vec2<T> uv;
Vec3<T> tan;
Vec3<T> bTan;
Vec4<UInt_8> bones;
Vec4<float> weights;
Vertex() = default;
Vertex(const Vec3<T>& pos)
: pos(pos), bones{0, 0, 0, 0}, weights{0.0f, 0.0f, 0.0f, 0.0f}
{
}
Vertex(const Vec3<T>& pos, const Vec3<T>& normal)
: pos(pos), normal(normal), bones{0, 0, 0, 0}, weights{0.0f, 0.0f, 0.0f, 0.0f}
{
}
Vertex(const Vec3<T>& pos, const Vec3<T>& normal, const Vec2<T>& uv)
: pos(pos), normal(normal), uv(uv), bones{0, 0, 0, 0}, weights{0.0f, 0.0f, 0.0f, 0.0f}
{
}
Vertex(const Vertex& vert)
: pos(vert.pos), normal(vert.normal), uv(vert.uv), bones(vert.bones), weights(vert.weights)
{
}
Vertex& operator=(const Vertex& vert) = default;
};
typedef Vertex<double> Vertex_d;
typedef Vertex<float> Vertex_f;
}