/
rig.h
166 lines (137 loc) · 3.69 KB
/
rig.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Little Polygon SDK
// Copyright (C) 2013 Max Kaufmann
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#pragma once
#include "sprites.h"
#include "collections.h"
#include "utils.h"
#define kTimelineTranslation 1
#define kTimelineRotation 2
#define kTimelineScale 3
//------------------------------------------------------------------------------
// ASSETS
struct RigBoneAsset
{
uint32_t parentIndex;
uint32_t hash;
lpVec translation;
lpVec scale;
lpFloat radians;
lpMatrix concatenatedMatrix() const {
auto uv = unitVector(radians);
return lpMatrix(scale.x * uv, scale.y * uv.anticlockwise(), translation);
}
};
struct RigSlotAsset
{
uint32_t boneIndex;
uint32_t defaultAttachment;
Color defaultColor;
};
struct RigAttachmentAsset
{
RigSlotAsset* slot;
ImageAsset* image;
uint32_t hash;
uint32_t layerHash;
lpMatrix xform;
};
struct RigAnimationAsset
{
uint32_t hash;
lpFloat duration;
};
struct RigTimelineAsset
{
lpFloat* times;
union {
lpFloat* rotationValues;
lpVec* translationValues;
lpVec* scaleValues;
int* attachmentValues;
};
uint32_t nkeyframes;
uint32_t animHash;
union {
uint32_t boneIndex;
uint32_t slotIndex;
};
uint32_t kind;
};
struct RigAsset
{
uint32_t defaultLayer,
nbones,
nslots,
nattachments,
nanims,
ntimeslines;
RigBoneAsset* bones;
RigSlotAsset* slots;
RigAttachmentAsset* attachments;
RigAnimationAsset* anims;
RigTimelineAsset* timelines;
};
//------------------------------------------------------------------------------
// RUNTIME CONTROLLER
class Rig {
private:
const RigAsset* data;
struct Attitude {
lpFloat radians;
lpVec scale;
void applyTo(lpMatrix& mat) {
auto u = unitVector(radians);
mat.u = scale.x * u;
mat.v = scale.y * u.anticlockwise();
}
};
// (indexed by bone)
Attitude* localAttitudes;
lpMatrix* localTransforms;
lpMatrix* worldTransforms;
// (indexed by timeline)
BitArray timelineMask;
unsigned* currentKeyframes;
RigAnimationAsset* currentAnimation;
uint32_t currentLayer;
lpFloat currentTime;
bool xformDirty;
public:
Rig(const RigAsset* asset);
~Rig();
// GETTERS
bool playing() const { return currentAnimation != 0; }
lpFloat time() const { return currentTime; }
bool showingLayer(const char* name) const { return currentLayer == fnv1a(name); }
bool showingAnimation(const char* name) const { return currentAnimation->hash == fnv1a(name); }
const lpMatrix& rootTransform() const { return worldTransforms[0]; }
const lpMatrix* findTransform(const char* boneName) const;
// SETTERS
void setRootTransform(const lpMatrix& mat, bool updateChildren=true);
void setLayer(const char *layerName);
void setAnimation(const char *animName);
// METHODS
void resetPose();
void resetTime();
void refreshTransforms();
void tick(lpFloat dt);
void draw(SpritePlotter* plotter, Color c=rgba(0));
private:
void setDefaultPose();
void computeWorldTransforms();
void updateTimeline(int i);
void applyTimeline(int i);
};