Skip to content

Commit 4302f10

Browse files
committed
fix bin animation, fix interpolate, make animation formats more generalized
1 parent 969caf3 commit 4302f10

File tree

14 files changed

+289
-165
lines changed

14 files changed

+289
-165
lines changed

include/io/BinIO.hpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@
99

1010
namespace BIN {
1111

12-
struct AnimInfo {
13-
bool mLoop { 0 };
14-
bool mPlaying { false };
15-
bool mLoaded { false };
16-
float mCurrentFrame { 0.0f }; // float so that we can change speed more easily
17-
float mPlaybackSpeed { 0.5f };
18-
uint32_t mFrameCount { 0 };
19-
};
20-
2112
#pragma pack(push, 1)
2213
struct Header {
2314
uint8_t Version { 0x02 };
@@ -192,12 +183,13 @@ namespace BIN {
192183
void InitShaders();
193184
void DestroyShaders();
194185

186+
class Animation;
187+
195188
class Model
196189
{
197190
public:
198191
Header mHeader;
199192

200-
AnimInfo mAnim;
201193

202194
std::map<uint16_t, TextureHeader> mTextureHeaders;
203195

@@ -206,22 +198,18 @@ namespace BIN {
206198

207199
std::map<uint16_t, Material> mMaterials;
208200
std::map<uint16_t, SceneGraphNode> mGraphNodes;
209-
std::map<uint16_t, GraphNodeTrack> mAnimationTracks;
210201

211202
std::vector<glm::vec3> mPositions;
212203
std::vector<glm::vec3> mNormals;
213204
std::vector<glm::vec2> mTexCoords;
214205
std::vector<glm::vec4> mColors;
215206

216207
void ReadSceneGraphNode(bStream::CStream* stream, uint32_t index);
217-
void DrawScenegraphNode(uint32_t idx, glm::mat4 transform);
208+
void DrawScenegraphNode(uint32_t idx, glm::mat4 transform, Animation* animation);
218209

219210
glm::vec3 bbMax {0, 0, 0}, bbMin {0, 0, 0};
220211

221-
void LoadAnimation(bStream::CStream* stream);
222-
void ClearAnimation();
223-
224-
void Draw(glm::mat4* transform, int32_t id, bool selected);
212+
void Draw(glm::mat4* transform, int32_t id, bool selected, Animation* animation = nullptr);
225213

226214
void Load(bStream::CStream* stream);
227215
void Write(bStream::CStream* stream);
@@ -233,4 +221,29 @@ namespace BIN {
233221
~Model();
234222
};
235223

224+
class Animation
225+
{
226+
bool mLoop { 0 };
227+
bool mPlaying { false };
228+
bool mLoaded { false };
229+
float mTime { 0.0f }; // float so that we can change speed more easily
230+
uint32_t mFrameCount { 0 };
231+
232+
std::map<uint16_t, GraphNodeTrack> mAnimationTracks;
233+
234+
public:
235+
bool Playing() { return mPlaying; }
236+
void ResetTracks();
237+
void Play();
238+
void Stop();
239+
240+
glm::mat4 GetNodeFrame(uint16_t node);
241+
void Step(float dt) { mTime += dt * 10; if(mTime >= mFrameCount && mLoop) { mTime = 0.0f; ResetTracks(); } else if(mTime >= mFrameCount) { mPlaying = false; } }
242+
243+
void Load(Model* model, bStream::CStream* stream);
244+
245+
Animation(){}
246+
~Animation(){}
247+
};
248+
236249
}

include/io/KeyframeIO.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ struct LKeyframeCommon
1515
{
1616
float frame;
1717
float value;
18-
float inslope { 1.0f };
19-
float outslope { 1.0f };
18+
float inslope { 0.0f };
19+
float outslope { 0.0f };
2020
};
2121

2222
class LTrackCommon

include/io/MdlIO.hpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ namespace MDL {
145145
};
146146

147147
struct Bone {
148+
int32_t ParentIndex { -1 };
148149
Bone* Parent { nullptr };
149150
glm::vec3 Translation;
150151
glm::quat Rotation;
@@ -154,6 +155,14 @@ namespace MDL {
154155
return glm::scale(glm::mat4(1.0f), Scale) * glm::toMat4(Rotation) * glm::translate(glm::mat4(1.0f), Translation);
155156
}
156157

158+
glm::mat4 ParentTransform(){
159+
if(Parent != nullptr){
160+
return Transform() * Parent->ParentTransform();
161+
} else {
162+
return Transform();
163+
}
164+
}
165+
157166
};
158167

159168
struct Weight {
@@ -208,15 +217,14 @@ namespace MDL {
208217
private:
209218
float mTime { 0.0f };
210219
float mSpeed { 0.1f };
211-
212-
uint32_t mPreviousKeyframe { 0 };
213-
uint32_t mNextKeyframe { 1 };
220+
float mEnd { 0.0f };
214221

215222
std::vector<JointTrack> mJointAnimations;
216223
public:
217-
void Step(){ mSpeed += mTime; }
224+
void ResetTracks();
218225
glm::mat4 GetJoint(uint32_t id);
219226
void Load(bStream::CStream* stream);
227+
void Step(float dt){ mTime += dt; if(mTime > mEnd){ mTime = 0.0f; ResetTracks(); } }
220228
Animation(){}
221229
~Animation(){}
222230
};
@@ -245,8 +253,7 @@ namespace MDL {
245253
std::vector<Weight> mWeights;
246254
std::vector<Bone> mSkeleton;
247255

248-
void ConvertBonesToLocalSpace();
249-
void ConvertBonesToWorldSpace();
256+
void SetupInverseBindPose();
250257
void BuildScenegraphSkeleton(uint32_t index, uint32_t parentIndex);
251258
void InitSkeletonRenderer(uint32_t index, uint32_t parentIndex);
252259

include/io/Util.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@ struct Readable {
7878
};
7979

8080
float InterpolateHermite(float factor, float timeA, float valueA, float outTangent, float timeB, float valueB, float inTangent);
81-
float MixTrack(LTrackCommon& track, uint32_t frameCount, float time, uint32_t& previousKey, uint32_t& nextKey);
81+
float MixTrack(LTrackCommon& track, float time, uint32_t& previousKey, uint32_t& nextKey);

include/scene/ModelViewer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace PreviewWidget {
2525

2626
void InitPreview();
2727
void CleanupPreview();
28-
void RenderPreview();
28+
void RenderPreview(float dt);
2929

3030
void PlayAnimation();
3131
void PauseAnimation();

src/DOM/BGRenderDOMNode.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void LBGRenderDOMNode::RenderBG(float dt)
3434
glm::vec3 skew;
3535
glm::vec4 persp;
3636
glm::decompose(*mTransform.get(), mScale, rotQuat, mPosition, skew, persp);
37+
rotQuat = glm::conjugate(rotQuat);
3738

3839
glm::vec3 rot = glm::eulerAngles(rotQuat);
3940
mRotation.x = (glm::degrees(rot.x) == 180.0f ? 0.0f : glm::degrees(rot.x));

src/UIUtil.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ void LUIUtility::RenderTransformUI(glm::mat4* transform, glm::vec3& translation,
234234
glm::vec4 persp;
235235

236236
glm::decompose(*transform, oldScale, oldRot, oldPos, skew, persp);
237+
oldRot = glm::conjugate(oldRot);
237238

238239
if (ImGui::TreeNode("Transform"))
239240
{

0 commit comments

Comments
 (0)