|
7 | 7 | #include "IMeshBuffer.h" |
8 | 8 | #include "CVertexBuffer.h" |
9 | 9 | #include "CIndexBuffer.h" |
| 10 | +#include "IVertexBuffer.h" |
10 | 11 | #include "S3DVertex.h" |
| 12 | +#include "vector3d.h" |
| 13 | +#include <algorithm> |
11 | 14 | #include <cassert> |
| 15 | +#include <memory> |
| 16 | +#include <numeric> |
| 17 | +#include <optional> |
12 | 18 |
|
13 | 19 | namespace scene |
14 | 20 | { |
15 | 21 |
|
| 22 | +struct WeightBuffer |
| 23 | +{ |
| 24 | + constexpr static u16 MAX_WEIGHTS_PER_VERTEX = 4; |
| 25 | + size_t n_verts; |
| 26 | + std::unique_ptr<u16[]> joint_idxs; |
| 27 | + std::unique_ptr<f32[]> weights; |
| 28 | + |
| 29 | + std::optional<std::vector<u32>> animated_vertices; |
| 30 | + |
| 31 | + // A bit of a hack for now: Store static positions here so we can use them for skinning. |
| 32 | + // Ideally we might want a design where we do not mutate the original vertex buffer at all. |
| 33 | + std::unique_ptr<core::vector3df[]> static_positions; |
| 34 | + std::unique_ptr<core::vector3df[]> static_normals; |
| 35 | + |
| 36 | + WeightBuffer(size_t n_verts) : |
| 37 | + n_verts(n_verts), |
| 38 | + joint_idxs(std::make_unique<u16[]>(MAX_WEIGHTS_PER_VERTEX * n_verts)), |
| 39 | + weights(std::make_unique<f32[]>(MAX_WEIGHTS_PER_VERTEX * n_verts)) |
| 40 | + {} |
| 41 | + |
| 42 | + u16 *getJointIndices(u32 vertex_id) |
| 43 | + { return &joint_idxs[vertex_id * MAX_WEIGHTS_PER_VERTEX]; } |
| 44 | + const u16 *getJointIndices(u32 vertex_id) const |
| 45 | + { return &joint_idxs[vertex_id * MAX_WEIGHTS_PER_VERTEX]; } |
| 46 | + |
| 47 | + f32 *getWeights(u32 vertex_id) |
| 48 | + { return &weights[vertex_id * MAX_WEIGHTS_PER_VERTEX]; } |
| 49 | + const f32 *getWeights(u32 vertex_id) const |
| 50 | + { return &weights[vertex_id * MAX_WEIGHTS_PER_VERTEX]; } |
| 51 | + |
| 52 | + void addWeight(u32 vertex_id, u16 joint_id, f32 weight) |
| 53 | + { |
| 54 | + assert(vertex_id < n_verts); |
| 55 | + assert(weight >= 0.f); |
| 56 | + f32 *weights = getWeights(vertex_id); |
| 57 | + f32 *min_weight = std::min_element(weights, weights + MAX_WEIGHTS_PER_VERTEX); |
| 58 | + if (*min_weight > weight) |
| 59 | + return; |
| 60 | + |
| 61 | + *min_weight = weight; |
| 62 | + getJointIndices(vertex_id)[min_weight - weights] = joint_id; |
| 63 | + } |
| 64 | + |
| 65 | + void skinVertex(u32 vertex_id, core::vector3df &pos, core::vector3df &normal, |
| 66 | + const std::vector<core::matrix4> &joint_transforms) const |
| 67 | + { |
| 68 | + const u16 *joint_idxs = getJointIndices(vertex_id); |
| 69 | + const f32 *weights = getWeights(vertex_id); |
| 70 | + f32 total_weight = 0.f; |
| 71 | + core::vector3df skinned_pos; |
| 72 | + core::vector3df skinned_normal; |
| 73 | + for (u16 i = 0; i < MAX_WEIGHTS_PER_VERTEX; ++i) { |
| 74 | + u16 joint_id = joint_idxs[i]; |
| 75 | + f32 weight = weights[i]; |
| 76 | + if (weight <= 0.f) |
| 77 | + continue; |
| 78 | + |
| 79 | + const auto &transform = joint_transforms[joint_id]; |
| 80 | + core::vector3df transformed_pos = pos; |
| 81 | + transform.transformVect(transformed_pos); |
| 82 | + skinned_pos += weight * transformed_pos; |
| 83 | + skinned_normal += weight * transform.rotateAndScaleVect(normal); |
| 84 | + total_weight += weight; |
| 85 | + } |
| 86 | + if (core::equals(total_weight, 0.f)) |
| 87 | + return; |
| 88 | + |
| 89 | + pos = skinned_pos; |
| 90 | + // Need to renormalize normal after potentially scaling |
| 91 | + normal = skinned_normal.normalize(); |
| 92 | + } |
| 93 | + |
| 94 | + /// @note src and dst can be the same buffer |
| 95 | + void skin(IVertexBuffer *dst, |
| 96 | + const std::vector<core::matrix4> &joint_transforms) const |
| 97 | + { |
| 98 | + assert(animated_vertices.has_value()); |
| 99 | + for (u32 i = 0; i < animated_vertices->size(); ++i) { |
| 100 | + |
| 101 | + u32 vertex_id = (*animated_vertices)[i]; |
| 102 | + auto pos = static_positions[i]; |
| 103 | + auto normal = static_normals[i]; |
| 104 | + skinVertex(vertex_id, pos, normal, joint_transforms); |
| 105 | + dst->getPosition(vertex_id) = pos; |
| 106 | + dst->getNormal(vertex_id) = normal; |
| 107 | + } |
| 108 | + if (!animated_vertices->empty()) |
| 109 | + dst->setDirty(); |
| 110 | + } |
| 111 | + |
| 112 | + /// Normalizes weights so that they sum to 1.0 per vertex, |
| 113 | + /// stores which vertices are animated. |
| 114 | + void finalize() { |
| 115 | + assert(!animated_vertices.has_value()); |
| 116 | + animated_vertices.emplace(); |
| 117 | + for (u32 i = 0; i < n_verts; ++i) { |
| 118 | + auto *weights_i = getWeights(i); |
| 119 | + f32 total_weight = std::accumulate(weights_i, weights_i + MAX_WEIGHTS_PER_VERTEX, 0.0f); |
| 120 | + if (core::equals(total_weight, 0.0f)) { |
| 121 | + std::fill(weights_i, weights_i + MAX_WEIGHTS_PER_VERTEX, 0.0f); |
| 122 | + continue; |
| 123 | + } |
| 124 | + animated_vertices->emplace_back(i); |
| 125 | + if (core::equals(total_weight, 1.0f)) |
| 126 | + continue; |
| 127 | + for (u16 j = 0; j < MAX_WEIGHTS_PER_VERTEX; ++j) |
| 128 | + weights_i[j] /= total_weight; |
| 129 | + } |
| 130 | + animated_vertices->shrink_to_fit(); |
| 131 | + } |
| 132 | + |
| 133 | + void updateStaticPose(const IVertexBuffer *vbuf) |
| 134 | + { |
| 135 | + static_normals = std::make_unique<core::vector3df[]>(animated_vertices->size()); |
| 136 | + static_positions = std::make_unique<core::vector3df[]>(animated_vertices->size()); |
| 137 | + for (size_t idx = 0; idx < animated_vertices->size(); ++idx) { |
| 138 | + u32 vertex_id = (*animated_vertices)[idx]; |
| 139 | + static_positions[idx] = vbuf->getPosition(vertex_id); |
| 140 | + static_normals[idx] = vbuf->getNormal(vertex_id); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + void resetToStatic(IVertexBuffer *vbuf) const |
| 145 | + { |
| 146 | + assert(animated_vertices.has_value()); |
| 147 | + for (size_t idx = 0; idx < animated_vertices->size(); ++idx) { |
| 148 | + u32 vertex_id = (*animated_vertices)[idx]; |
| 149 | + vbuf->getPosition(vertex_id) = static_positions[idx]; |
| 150 | + vbuf->getNormal(vertex_id) = static_normals[idx]; |
| 151 | + } |
| 152 | + if (!animated_vertices->empty()) |
| 153 | + vbuf->setDirty(); |
| 154 | + } |
| 155 | +}; |
| 156 | + |
16 | 157 | //! A mesh buffer able to choose between S3DVertex2TCoords, S3DVertex and S3DVertexTangents at runtime |
17 | 158 | struct SSkinMeshBuffer final : public IMeshBuffer |
18 | 159 | { |
@@ -222,6 +363,8 @@ struct SSkinMeshBuffer final : public IMeshBuffer |
222 | 363 | SVertexBuffer *Vertices_Standard; |
223 | 364 | SIndexBuffer *Indices; |
224 | 365 |
|
| 366 | + std::optional<WeightBuffer> Weights; |
| 367 | + |
225 | 368 | core::matrix4 Transformation; |
226 | 369 |
|
227 | 370 | video::SMaterial Material; |
|
0 commit comments