Skip to content

Commit fd2ee07

Browse files
authored
Merge pull request #70 from CapsCollective/feature/anim-support
Added support for skeletal mesh and animation data packing
2 parents 847a93f + 7140aaf commit fd2ee07

22 files changed

+1069
-36
lines changed

engine/resources/AnimationData.h

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// Copyright (c) 2020-present Caps Collective & contributors
3+
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
4+
//
5+
// This code is released under an unmodified zlib license.
6+
// For conditions of distribution and use, please see:
7+
// https://opensource.org/licenses/Zlib
8+
//
9+
10+
#ifndef SIEGE_ENGINE_ANIMATIONDATA_H
11+
#define SIEGE_ENGINE_ANIMATIONDATA_H
12+
13+
#include <utils/BinarySerialisation.h>
14+
#include <utils/math/mat/Mat4.h>
15+
#include <utils/math/vec/Vec3.h>
16+
17+
namespace Siege
18+
{
19+
20+
struct AnimKeyPosition
21+
{
22+
double timestamp = 0.f;
23+
Vec3 position;
24+
};
25+
26+
struct AnimKeyRotation
27+
{
28+
double timestamp = 0.f;
29+
Vec4 rotation;
30+
};
31+
32+
struct AnimKeyScale
33+
{
34+
double timestamp = 0.f;
35+
Vec3 scale;
36+
};
37+
38+
struct AnimationChannel
39+
{
40+
std::vector<AnimKeyPosition> positionKeys;
41+
std::vector<AnimKeyRotation> rotationKeys;
42+
std::vector<AnimKeyScale> scaleKeys;
43+
};
44+
45+
struct AnimationData
46+
{
47+
double length;
48+
double speed;
49+
std::map<String, AnimationChannel> channels;
50+
};
51+
52+
namespace BinarySerialisation
53+
{
54+
55+
inline void serialise(Buffer& buffer, AnimKeyPosition& value, SerialisationMode mode)
56+
{
57+
serialise(buffer, value.timestamp, mode);
58+
serialise(buffer, value.position, mode);
59+
}
60+
61+
inline void serialise(Buffer& buffer, AnimKeyRotation& value, SerialisationMode mode)
62+
{
63+
serialise(buffer, value.timestamp, mode);
64+
serialise(buffer, value.rotation, mode);
65+
}
66+
67+
inline void serialise(Buffer& buffer, AnimKeyScale& value, SerialisationMode mode)
68+
{
69+
serialise(buffer, value.timestamp, mode);
70+
serialise(buffer, value.scale, mode);
71+
}
72+
73+
inline void serialise(Buffer& buffer, AnimationChannel& value, SerialisationMode mode)
74+
{
75+
serialise(buffer, value.positionKeys, mode);
76+
serialise(buffer, value.rotationKeys, mode);
77+
serialise(buffer, value.scaleKeys, mode);
78+
}
79+
80+
inline void serialise(Buffer& buffer, AnimationData& value, SerialisationMode mode)
81+
{
82+
serialise(buffer, value.length, mode);
83+
serialise(buffer, value.speed, mode);
84+
serialise(buffer, value.channels, mode);
85+
}
86+
87+
} // namespace BinarySerialisation
88+
89+
} // namespace Siege
90+
91+
#endif // SIEGE_ENGINE_ANIMATIONDATA_H

engine/resources/PackFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
#include <filesystem>
1919
#include <map>
2020

21+
#include "AnimationData.h"
2122
#include "PackFileData.h"
2223
#include "SceneData.h"
24+
#include "SkeletalMeshData.h"
2325
#include "StaticMeshData.h"
2426
#include "Texture2DData.h"
2527

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//
2+
// Copyright (c) 2020-present Caps Collective & contributors
3+
// Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
4+
//
5+
// This code is released under an unmodified zlib license.
6+
// For conditions of distribution and use, please see:
7+
// https://opensource.org/licenses/Zlib
8+
//
9+
10+
#ifndef SIEGE_ENGINE_SKELETALMESHDATA_H
11+
#define SIEGE_ENGINE_SKELETALMESHDATA_H
12+
13+
#include <utils/BinarySerialisation.h>
14+
#include <utils/Colour.h>
15+
#include <utils/math/mat/Mat4.h>
16+
#include <utils/math/vec/Hashing.h>
17+
#include <utils/math/vec/Vec2.h>
18+
#include <utils/math/vec/Vec3.h>
19+
20+
#include "StaticMeshData.h"
21+
22+
namespace Siege
23+
{
24+
struct Bone
25+
{
26+
uint32_t id = 0;
27+
Mat4 bindMatrix;
28+
};
29+
30+
struct SkinnedVertex
31+
{
32+
Vec3 position;
33+
FColour color;
34+
Vec3 normal;
35+
Vec2 uv;
36+
Vec4 bones;
37+
Vec4 weights;
38+
};
39+
40+
inline bool operator==(const SkinnedVertex& left, const SkinnedVertex& right)
41+
{
42+
return left.position == right.position && left.color == right.color &&
43+
left.normal == right.normal && left.uv == right.uv && left.bones == right.bones &&
44+
left.weights == right.weights;
45+
}
46+
47+
inline bool operator!=(const SkinnedVertex& left, const SkinnedVertex& right)
48+
{
49+
return !(left == right);
50+
}
51+
52+
struct SkeletalMeshData
53+
{
54+
std::vector<uint32_t> indices;
55+
std::vector<SkinnedVertex> vertices;
56+
std::map<String, Bone> bones;
57+
};
58+
59+
namespace BinarySerialisation
60+
{
61+
62+
inline void serialise(Buffer& buffer, Bone& value, SerialisationMode mode)
63+
{
64+
serialise(buffer, value.id, mode);
65+
serialise(buffer, value.bindMatrix, mode);
66+
}
67+
68+
inline void serialise(Buffer& buffer, SkinnedVertex& value, SerialisationMode mode)
69+
{
70+
serialise(buffer, value.position, mode);
71+
serialise(buffer, value.color, mode);
72+
serialise(buffer, value.normal, mode);
73+
serialise(buffer, value.uv, mode);
74+
serialise(buffer, value.bones, mode);
75+
serialise(buffer, value.weights, mode);
76+
}
77+
78+
inline void serialise(Buffer& buffer, SkeletalMeshData& value, SerialisationMode mode)
79+
{
80+
serialise(buffer, value.indices, mode);
81+
serialise(buffer, value.vertices, mode);
82+
serialise(buffer, value.bones, mode);
83+
}
84+
85+
} // namespace BinarySerialisation
86+
87+
} // namespace Siege
88+
89+
namespace std
90+
{
91+
92+
template<>
93+
struct hash<Siege::SkinnedVertex>
94+
{
95+
size_t operator()(const Siege::SkinnedVertex& vertex) const
96+
{
97+
size_t seed;
98+
Siege::Hash::HashCombine(seed, vertex, vertex.bones, vertex.weights);
99+
return seed;
100+
};
101+
};
102+
103+
} // namespace std
104+
105+
#endif // SIEGE_ENGINE_SKELETALMESHDATA_H

engine/resources/StaticMeshData.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include <utils/math/vec/Vec2.h>
1717
#include <utils/math/vec/Vec3.h>
1818

19-
#include "PackFile.h"
20-
2119
namespace Siege
2220
{
2321

@@ -40,6 +38,15 @@ inline bool operator!=(const BaseVertex& left, const BaseVertex& right)
4038
return !(left == right);
4139
}
4240

41+
struct StaticMeshData
42+
{
43+
std::vector<uint32_t> indices;
44+
std::vector<BaseVertex> vertices;
45+
};
46+
47+
namespace BinarySerialisation
48+
{
49+
4350
inline void serialise(BinarySerialisation::Buffer& buffer,
4451
BaseVertex& value,
4552
BinarySerialisation::SerialisationMode mode)
@@ -50,15 +57,6 @@ inline void serialise(BinarySerialisation::Buffer& buffer,
5057
serialise(buffer, value.uv, mode);
5158
}
5259

53-
struct StaticMeshData
54-
{
55-
std::vector<uint32_t> indices;
56-
std::vector<BaseVertex> vertices;
57-
};
58-
59-
namespace BinarySerialisation
60-
{
61-
6260
inline void serialise(Buffer& buffer, StaticMeshData& value, SerialisationMode mode)
6361
{
6462
serialise(buffer, value.indices, mode);

engine/utils/BinarySerialisation.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616

1717
#include "Colour.h"
1818
#include "String.h"
19+
#include "math/mat/Mat.h"
20+
#include "math/mat/Mat4.h"
1921
#include "math/vec/Vec.h"
2022
#include "math/vec/Vec2.h"
2123
#include "math/vec/Vec3.h"
24+
#include "math/vec/Vec4.h"
2225

2326
namespace Siege::BinarySerialisation
2427
{
@@ -123,6 +126,14 @@ inline void serialise(Buffer& buffer, Vec3& value, SerialisationMode mode)
123126
serialiseNative<float>(buffer, value.z, mode);
124127
}
125128

129+
inline void serialise(Buffer& buffer, Vec4& value, SerialisationMode mode)
130+
{
131+
serialiseNative<float>(buffer, value.x, mode);
132+
serialiseNative<float>(buffer, value.y, mode);
133+
serialiseNative<float>(buffer, value.z, mode);
134+
serialiseNative<float>(buffer, value.w, mode);
135+
}
136+
126137
inline void serialise(Buffer& buffer, FColour& value, SerialisationMode mode)
127138
{
128139
serialiseNative<float>(buffer, value.r, mode);
@@ -131,6 +142,14 @@ inline void serialise(Buffer& buffer, FColour& value, SerialisationMode mode)
131142
serialiseNative<float>(buffer, value.a, mode);
132143
}
133144

145+
inline void serialise(Buffer& buffer, Mat4& value, SerialisationMode mode)
146+
{
147+
serialise(buffer, value.values[0], mode);
148+
serialise(buffer, value.values[1], mode);
149+
serialise(buffer, value.values[2], mode);
150+
serialise(buffer, value.values[3], mode);
151+
}
152+
134153
inline void serialise(Buffer& buffer, String& value, SerialisationMode mode)
135154
{
136155
switch (mode)
@@ -161,6 +180,11 @@ inline void serialise(Buffer& buffer, String& value, SerialisationMode mode)
161180
}
162181
}
163182

183+
inline void serialise(Buffer& buffer, const String& value, SerialisationMode mode)
184+
{
185+
serialise(buffer, const_cast<String&>(value), mode);
186+
}
187+
164188
template<typename T>
165189
void serialise(Buffer& buffer, std::vector<T>& value, SerialisationMode mode)
166190
{

engine/utils/Colour.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <type_traits>
1515

1616
#include "Hash.h"
17+
#include "String.h"
1718

1819
namespace Siege
1920
{
@@ -236,6 +237,26 @@ inline constexpr FColour ToIColour(const FColour& other)
236237
static_cast<float>(other.a * 255)};
237238
}
238239

240+
/**
241+
* Converts an integer colour to a string
242+
* @param colour the colour to convert to a string
243+
* @return a string representation of the colour
244+
*/
245+
inline String ToString(const IColour& colour)
246+
{
247+
return String("%d,%d,%d,%d").Formatted(colour.r, colour.g, colour.b, colour.a);
248+
}
249+
250+
/**
251+
* Converts a float colour to a string
252+
* @param colour the colour to convert to a string
253+
* @return a string representation of the colour
254+
*/
255+
inline String ToString(const FColour& colour)
256+
{
257+
return String("%.2f,%.2f,%.2f,%.2f").Formatted(colour.r, colour.g, colour.b, colour.a);
258+
}
259+
239260
} // namespace Siege
240261

241262
namespace std

engine/utils/Logging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <variant>
1616
#include <vector>
1717

18+
#include "Colour.h"
1819
#include "Macros.h"
1920
#include "String.h"
2021
#include "Token.h"
@@ -102,6 +103,8 @@ class VariantContainer
102103
DEFINE_VARIANT_TYPE(const Vec2& data, "Vector2(" + ToString(data) + ")");
103104
DEFINE_VARIANT_TYPE(const Vec3& data, "Vector3(" + ToString(data) + ")");
104105
DEFINE_VARIANT_TYPE(const Vec4& data, "Vector4(" + ToString(data) + ")");
106+
DEFINE_VARIANT_TYPE(const FColour& data, "FColour(" + ToString(data) + ")");
107+
DEFINE_VARIANT_TYPE(const IColour& data, "IColour(" + ToString(data) + ")");
105108
DEFINE_VARIANT_TYPE(const Mat2& data, "Mat2(" + ToString(data) + ")");
106109
DEFINE_VARIANT_TYPE(const Mat3& data, "Mat3(" + ToString(data) + ")");
107110
DEFINE_VARIANT_TYPE(const Mat4& data, "Mat4(" + ToString(data) + ")");

examples/game/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ else ifeq ($(platform), macos)
3030
endif
3131
engineRenderBuildDir := $(binDir)/engine/render/build
3232
exampleGameAssets := $(patsubst ./%,%, $(call rwildcard,$(exampleGameSrcDir),*.sm))
33+
exampleGameAssets += $(patsubst ./%,%, $(call rwildcard,$(exampleGameSrcDir),*.sk))
34+
exampleGameAssets += $(patsubst ./%,%, $(call rwildcard,$(exampleGameSrcDir),*.ska))
3335
exampleGameAssets += $(patsubst ./%,%, $(call rwildcard,$(exampleGameSrcDir),*.png))
3436
exampleGameAssets += $(patsubst ./%,%, $(call rwildcard,$(exampleGameSrcDir),*.jpg))
3537
exampleGameAssets += $(patsubst $(engineRenderBuildDir)/%,%, $(call rwildcard,$(engineRenderBuildDir),*.spv))

examples/render/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ else ifeq ($(platform), macos)
3030
endif
3131
engineRenderBuildDir := $(binDir)/engine/render/build
3232
exampleRenderAssets := $(patsubst ./%,%, $(call rwildcard,$(exampleRenderSrcDir),*.sm))
33+
exampleRenderAssets += $(patsubst ./%,%, $(call rwildcard,$(exampleRenderSrcDir),*.sk))
34+
exampleRenderAssets += $(patsubst ./%,%, $(call rwildcard,$(exampleRenderSrcDir),*.ska))
3335
exampleRenderAssets += $(patsubst ./%,%, $(call rwildcard,$(exampleRenderSrcDir),*.png))
3436
exampleRenderAssets += $(patsubst ./%,%, $(call rwildcard,$(exampleRenderSrcDir),*.jpg))
3537
exampleRenderAssets += $(patsubst $(engineRenderBuildDir)/%,%, $(call rwildcard,$(engineRenderBuildDir),*.spv))
5.88 KB
Binary file not shown.

0 commit comments

Comments
 (0)