Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
skin.h
1#ifndef OE_SCENE_SKIN_H
2#define OE_SCENE_SKIN_H
3
4#include <vector>
5#include <string>
6#include <glm/mat4x4.hpp>
7
8#include "mesh.h"
9#include "node_ptr.h"
10#include "vertex.h"
11
13{
17 struct Bone
18 {
21
23 glm::mat4 inverse_bind_matrix = glm::mat4(1.0);
24
27
30 };
31
37 {
40
42 glm::vec3 position;
43
44 // Todo, normals, etc...
45 };
46
48 {
49 public:
53 BlendShapeManager(size_t count, size_t vertices);
54
58 std::vector<uint8_t> getPackedData();
59
63 size_t getIdFromName(const std::string& name);
64
68 void addBlendShape(const std::string& name, const std::vector<BlendShapeData>& blendshape_data/*, const size_t idSubMesh = 0*/);
69
74 void setWeight(const std::string& name, const float weight);
75
79 const std::vector<float>& getAllWeights() const { return _weights; }
80
85
86 inline uint32_t getCount() { return _count; }
87 inline uint32_t getTotalAffectedVerticesCount() { return _vertices_count; }
88
89 private:
90 uint32_t _count = 0;
91 uint32_t _vertices_count = 0;
92 std::vector<float> _blendshape_raw_data;
93
94 std::vector<std::string> _names;
95 std::vector<float> _weights;
96 };
97
101 template <IsSkinnedVertex VertexType>
102 AABB generateBoneBoundingBox(const scene::Mesh<VertexType>& mesh, const int32_t id_bone)
103 {
104 return generateBoundingBoxFromPredicate(mesh, [&id_bone] (const VertexType& v) -> bool
105 {
106 const auto& bones = v.bones;
107 const auto& weights = v.weights;
108
109 return (bones.x == id_bone && weights.x > 0.f)
110 || (bones.y == id_bone && weights.y > 0.f)
111 || (bones.z == id_bone && weights.z > 0.f)
112 || (bones.w == id_bone && weights.w > 0.f);
113 });
114 }
115
119 struct Armature
120 {
128 void addBone(NodePtr node, const AABB& bone_bbox, const glm::mat4& inverse_bind_matrix)
129 {
130 bones_matrices.push_back(glm::mat4(1.0));
131
132 bones.push_back({
133 .node = node,
134 .inverse_bind_matrix = inverse_bind_matrix,
135 .raw_bounding_box = bone_bbox,
136 .bounding_box = bone_bbox
137 });
138 }
139
145 void update();
146
147 scene::NodePtr root_node;
148
149 std::vector<Bone> bones;
150 std::vector<glm::mat4> bones_matrices;
151
152 //BlendShapeManager blendshapes
153 };
154}
155
156#endif
Axis-aligned bounding box.
Definition aabb.h:13
const std::vector< float > & getAllWeights() const
Definition skin.h:79
std::vector< uint8_t > getPackedData()
size_t getIdFromName(const std::string &name)
BlendShapeManager(size_t count, size_t vertices)
void setWeight(const std::string &name, const float weight)
void addBlendShape(const std::string &name, const std::vector< BlendShapeData > &blendshape_data)
Helpers and classes related to skinned meshes.
Definition skin.h:13
AABB generateBoneBoundingBox(const scene::Mesh< VertexType > &mesh, const int32_t id_bone)
Compute bone bounding box by taking all influenced vertices in the mesh.
Definition skin.h:102
constexpr AABB generateBoundingBoxFromPredicate(const MeshType &mesh, F &&predicate) noexcept
Generate a bounding box containing vertices matching a predicate.
Definition aabb.h:275
uint32_t index_t
Definition vertex.h:13
Definition mesh.h:61
Wrapper to a node reference to use pointers to node even if the actual node moves in memory.
Definition node_ptr.h:19
Skinned mesh skeleton.
Definition skin.h:120
void update()
Update armature.
void addBone(NodePtr node, const AABB &bone_bbox, const glm::mat4 &inverse_bind_matrix)
Add a bone to the armature.
Definition skin.h:128
index_t index
Definition skin.h:39
glm::vec3 position
Definition skin.h:42
Skinned mesh bone.
Definition skin.h:18
AABB raw_bounding_box
Definition skin.h:26
scene::NodePtr node
Definition skin.h:20
glm::mat4 inverse_bind_matrix
Definition skin.h:23
AABB bounding_box
Definition skin.h:29