Skip to content
This repository has been archived by the owner on Oct 1, 2023. It is now read-only.

Commit

Permalink
Particle system.
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 10, 2018
1 parent 8b2c750 commit b6c35ee
Show file tree
Hide file tree
Showing 33 changed files with 5,755 additions and 3,962 deletions.
61 changes: 23 additions & 38 deletions code/animation.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@

void getPointsFromSkeleton(Mesh* mesh, BoneNode* node, Quat q = quat(), Vec3 p = vec3(0.0f)) {
void xFormsLocalToGlobal(XForm* forms, BoneNode* node, XForm cForm = xForm()) {
XForm form = forms[node->data->index];

XForm form = mesh->basePose[node->data->index];

Vec3 oldP = p;
p = p + (q * form.translation);
q = q * form.rotation;

mesh->basePosePoints[node->data->index] = p;
cForm = xFormCombine(cForm, form);
forms[node->data->index] = cForm;

for(int i = 0; i < node->childCount; i++) {
getPointsFromSkeleton(mesh, node->children + i, q, p);
xFormsLocalToGlobal(forms, node->children + i, cForm);
}
}

Expand Down Expand Up @@ -63,7 +59,7 @@ void AnimationPlayer::update(float dt) {
frame = min(frame, (float)animation->frameCount-1);

{
int boneCount = animation->boneCount;
boneCount = animation->boneCount;

int frame1 = floor(frame);
int frame2 = ceil(frame);
Expand All @@ -84,9 +80,9 @@ void AnimationPlayer::update(float dt) {
XForm f2 = frames2[i];

XForm f3 = {};
f3.translation = lerp(t, f1.translation, f2.translation);
f3.trans = lerp(t, f1.trans, f2.trans);
// Should rotate the other way if over 180 degrees?
f3.rotation = quatLerp(f1.rotation, f2.rotation, t);
f3.rot = quatLerp(f1.rot, f2.rot, t);
f3.scale = lerp(t, f1.scale, f2.scale);

bones[i] = f3;
Expand All @@ -96,36 +92,25 @@ void AnimationPlayer::update(float dt) {

if(noLocomotion) {
// Needs some work.
Vec3 baseTranslation = animation->frames[0][0].translation;
bones[0].translation.xy = baseTranslation.xy;
Vec3 baseTranslation = animation->frames[0][0].trans;
bones[0].trans.xy = baseTranslation.xy;
}

calcMats(mesh->basePose, bones, &mesh->boneTree);
}

void AnimationPlayer::calcMats(XForm* baseBones, XForm* bones, BoneNode* node, Quat q, Vec3 p, Quat totalRot) {
XForm formPose = baseBones[node->data->index];
XForm form = bones[node->data->index];

Vec3 basePoseNodePos = mesh->basePosePoints[node->data->index];

// There is probably a better way to do this.
xFormsLocalToGlobal(bones, &mesh->boneTree);

Quat q1 = q * formPose.rotation;
Quat q2 = q * form.rotation;
Quat q3 = q2 * quatInverse(q1);
totalRot = q3 * totalRot;

p = p + (q * form.translation);
q = q * form.rotation;
// Calc mats.
{
for(int i = 0; i < boneCount; i++) {
XForm a = mesh->basePose[i];
XForm b = bones[i];

Mat4 model = translationMatrix(basePoseNodePos) *
modelMatrix(p - basePoseNodePos, vec3(1), totalRot) *
translationMatrix(-basePoseNodePos);
Quat totalRot = b.rot * quatInverse(a.rot);

mats[node->data->index] = model;
Mat4 model = translationMatrix(a.trans) *
modelMatrix(b.trans - a.trans, vec3(1), totalRot) *
translationMatrix(-a.trans);

for(int i = 0; i < node->childCount; i++) {
calcMats(baseBones, bones, node->children + i, q, p, totalRot);
mats[i] = model;
}
}
}
}
8 changes: 0 additions & 8 deletions code/animation.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@

struct XForm {
Vec3 translation;
Quat rotation;
Vec3 scale;
};

struct Bone {
char* name;
int index;
int depth;
// XForm xform;
};

struct BoneNode {
Expand Down Expand Up @@ -58,5 +51,4 @@ struct AnimationPlayer {

void setAnim(char* name);
void update(float dt);
void calcMats(XForm* baseBones, XForm* bones, BoneNode* node, Quat q = quat(), Vec3 p = vec3(0.0f), Quat totalRots = quat());
};
Loading

0 comments on commit b6c35ee

Please sign in to comment.