OpenBF1942-Plus
Refractor 1, rebuilt from scratch
Loading...
Searching...
No Matches
anim.h
Go to the documentation of this file.
1// Skeletons (.ske), skins (.skn), animations (.baf), pose and skinning.
2// Spec: docs/formats/anim.md; reference: tools/anim.py
3#pragma once
4#include <cstdint>
5#include <string>
6#include <unordered_map>
7#include <vector>
8
9#include "mat.h"
10
11namespace obf {
12
13struct Bone {
14 std::string name;
15 int parent; // -1 = root; parents come first
17};
18using Skeleton = std::vector<Bone>;
19
21 uint16_t bone; // index into Skin::bones
22 float weight, offset[3];
23};
24struct SkinRecord {
25 float pos[3];
26 std::vector<SkinInfluence> influences;
27};
28struct Skin {
29 std::vector<SkinRecord> records;
30 std::vector<std::string> bones;
31};
32
33struct Animation {
34 std::vector<std::string> bones;
35 uint32_t frames = 0;
36 std::vector<std::vector<float>> channels; // per bone 7 channels (qx qy qz qw px py pz), each `frames` long
37 const float* channel(size_t bone, int c) const { return channels[bone * 7 + static_cast<size_t>(c)].data(); }
38};
39
40Skeleton ske_parse(const std::vector<uint8_t>& data);
41Skin skn_parse(const std::vector<uint8_t>& data);
42Animation baf_parse(const std::vector<uint8_t>& data);
43
44// World matrix per skeleton bone. biped_root: turn 3ds Max space (up -z, facing +y) to Y-up facing +z (soldiers).
45std::vector<Mat4> pose(const Skeleton& s, const std::vector<const Animation*>& anims, float frame, bool biped_root = false);
46// The same with one frame per animation (an upper-body layer playing at its own time).
47std::vector<Mat4> pose(const Skeleton& s, const std::vector<const Animation*>& anims, const std::vector<float>& frames, bool biped_root = false);
48
49// Mesh vertex -> skin record (by position; exact, then within 1 mm), -1 = rigid.
50std::vector<int> skin_map(const Skin& skin, const std::vector<float>& vertices, size_t floats_per_vertex);
51// Skinned positions (3 floats per vertex); rigid vertices keep their mesh position.
52void skin_apply(const Skin& skin, const std::vector<int>& map, const std::vector<int>& skin_to_skeleton,
53 const std::vector<Mat4>& world, const std::vector<float>& vertices, size_t floats_per_vertex,
54 std::vector<float>& out);
55std::vector<int> bone_index(const Skin& skin, const Skeleton& s); // skin bone -> skeleton bone, -1 if absent
56
57} // namespace obf
Definition anim.cpp:7
Skin skn_parse(const std::vector< uint8_t > &d)
Definition anim.cpp:76
std::vector< Bone > Skeleton
Definition anim.h:18
Animation baf_parse(const std::vector< uint8_t > &d)
Definition anim.cpp:103
std::vector< int > bone_index(const Skin &skin, const Skeleton &s)
Definition anim.cpp:182
std::vector< int > skin_map(const Skin &skin, const std::vector< float > &v, size_t fpv)
Definition anim.cpp:193
Skeleton ske_parse(const std::vector< uint8_t > &d)
Definition anim.cpp:56
std::vector< Mat4 > pose(const Skeleton &s, const std::vector< const Animation * > &anims, float frame, bool biped_root)
Definition anim.cpp:143
void skin_apply(const Skin &skin, const std::vector< int > &map, const std::vector< int > &to_ske, const std::vector< Mat4 > &world, const std::vector< float > &v, size_t fpv, std::vector< float > &out)
Definition anim.cpp:211
Definition anim.h:33
uint32_t frames
Definition anim.h:35
std::vector< std::vector< float > > channels
Definition anim.h:36
const float * channel(size_t bone, int c) const
Definition anim.h:37
std::vector< std::string > bones
Definition anim.h:34
Definition anim.h:13
int parent
Definition anim.h:15
Mat4 local
Definition anim.h:16
std::string name
Definition anim.h:14
Definition mat.h:8
Definition anim.h:20
float offset[3]
Definition anim.h:22
float weight
Definition anim.h:22
uint16_t bone
Definition anim.h:21
Definition anim.h:24
float pos[3]
Definition anim.h:25
std::vector< SkinInfluence > influences
Definition anim.h:26
Definition anim.h:28
std::vector< std::string > bones
Definition anim.h:30
std::vector< SkinRecord > records
Definition anim.h:29