Browse Source

fixed some problems with animations and improve Model3DCollections tick function

Kolja Strohm 1 year ago
parent
commit
0bebe14b4a
6 changed files with 62 additions and 7 deletions
  1. 41 1
      Animation3D.cpp
  2. 10 0
      Animation3D.h
  3. 2 1
      Model3DCollection.cpp
  4. 1 1
      Model3DCollection.h
  5. 6 3
      Welt3D.cpp
  6. 2 1
      Welt3D.h

+ 41 - 1
Animation3D.cpp

@@ -19,7 +19,7 @@ BoneAnimation::BoneAnimation(
     frames = new KeyFrame[1];
     frames[0].time = 0.0;
     frames[0].pos = originPos;
-    frames[9].rot = originRot;
+    frames[0].rot = originRot;
     current = frames[0];
 }
 
@@ -74,6 +74,19 @@ void BoneAnimation::addKeyFrame(double time, Vec3<float> pos, Vec3<float> rot)
     frames = tmp;
 }
 
+// adds a keyframe that is the same as the last keyframe with the time time
+// \returns true if the keyframe was added successfully, false if time is maller
+// than the current length of the animation
+bool BoneAnimation::doNothingUntil(double time)
+{
+    if (time > maxTime)
+    {
+        KeyFrame& last = frames[frameCount - 1];
+        addKeyFrame(time, last.pos, last.rot);
+    }
+    return 0;
+}
+
 // calculates the positions and rotation at the next time
 // \param time the passed time in seconds since the last call
 void BoneAnimation::tick(double time)
@@ -134,6 +147,12 @@ int BoneAnimation::getBoneId() const
     return boneId;
 }
 
+// returns the maximum time of the animation
+double BoneAnimation::getMaxTime() const
+{
+    return maxTime;
+}
+
 //! Constructor
 SkeletonAnimation::SkeletonAnimation()
     : ReferenceCounter(),
@@ -217,4 +236,25 @@ bool SkeletonAnimation::isFinished() const
         if (!animation->isFinished()) return 0;
     }
     return 1;
+}
+
+// returns the maximum time of the animation
+double SkeletonAnimation::getMaxTime() const
+{
+    double max = 0;
+    for (BoneAnimation* animation : subAnimations)
+    {
+        max = MAX(animation->getMaxTime(), max);
+    }
+    return max;
+}
+
+// returns the animation for a specific bone or 0 if it does not exist
+BoneAnimation* SkeletonAnimation::zAnimation(int boneId) const
+{
+    for (BoneAnimation* animation : subAnimations)
+    {
+        if (animation->getBoneId() == boneId) return animation;
+    }
+    return 0;
 }

+ 10 - 0
Animation3D.h

@@ -46,6 +46,10 @@ namespace Framework
         //! the rotation of the bone
         DLLEXPORT void addKeyFrame(
             double time, Vec3<float> pos, Vec3<float> rot);
+        // adds a keyframe that is the same as the last keyframe with the time
+        // time \returns true if the keyframe was added successfully, false if
+        // time is maller than the current length of the animation
+        DLLEXPORT bool doNothingUntil(double time);
         // calculates the positions and rotation at the next time
         // \param time the passed time in seconds since the last call
         DLLEXPORT void tick(double time);
@@ -57,6 +61,8 @@ namespace Framework
         DLLEXPORT bool isFinished() const;
         // returns the bone id this animation is for
         DLLEXPORT int getBoneId() const;
+        // returns the maximum time of the animation
+        DLLEXPORT double getMaxTime() const;
     };
 
     class SkeletonAnimation : public virtual ReferenceCounter
@@ -98,5 +104,9 @@ namespace Framework
         // returns true if the animation has reached the last keyframe and does
         // not loop
         DLLEXPORT bool isFinished() const;
+        // returns the maximum time of the animation
+        DLLEXPORT double getMaxTime() const;
+        // returns the animation for a specific bone or 0 if it does not exist
+        DLLEXPORT BoneAnimation* zAnimation(int boneId) const;
     };
 } // namespace Framework

+ 2 - 1
Model3DCollection.cpp

@@ -6,9 +6,10 @@ Model3DCollection::Model3DCollection()
     : ReferenceCounter()
 {}
 
-void Model3DCollection::tick(std::function<void(Model3D*)> f)
+bool Model3DCollection::tick(std::function<void(Model3D*)> f, double time)
 {
     forAll(f);
+    return 0;
 }
 
 void Model3DCollection::render(std::function<void(Model3D*)> f)

+ 1 - 1
Model3DCollection.h

@@ -12,7 +12,7 @@ namespace Framework
     public:
         DLLEXPORT Model3DCollection();
         virtual void forAll(std::function<void(Model3D*)> f) = 0;
-        DLLEXPORT virtual void tick(std::function<void(Model3D*)> f);
+        DLLEXPORT virtual bool tick(std::function<void(Model3D*)> f, double time);
         DLLEXPORT virtual void render(std::function<void(Model3D*)> f);
     };
 } // namespace Framework

+ 6 - 3
Welt3D.cpp

@@ -169,7 +169,8 @@ void Welt3D::doMausEreignis(MausEreignis3D& me)
 bool Welt3D::tick(double tickval)
 {
     cs.lock();
-    tick([this, &tickval](Model3D* m) { rend |= m->tick(tickval); });
+    rend |= tick(
+        [this, &tickval](Model3D* m) { rend |= m->tick(tickval); }, tickval);
     cs.unlock();
     bool tmp = rend;
     rend = 0;
@@ -208,12 +209,14 @@ void Framework::Welt3D::forAll(std::function<void(Model3D*)> f)
 }
 
 //! führt eine tick funktion auf jedem Model aus
-void Welt3D::tick(std::function<void(Model3D*)> f)
+bool Welt3D::tick(std::function<void(Model3D*)> f, double time)
 {
     for (auto m : *members)
         f(m);
+    bool res = 0;
     for (auto c : modelCollections)
-        c->tick(f);
+        res |= c->tick(f, time);
+    return res;
 }
 
 //! führt eine render funktion auf jedem Model aus

+ 2 - 1
Welt3D.h

@@ -69,7 +69,8 @@ namespace Framework
         //! führt eine funktion auf jedem Model aus
         DLLEXPORT virtual void forAll(std::function<void(Model3D*)> f) override;
         //! führt eine tick funktion auf jedem Model aus
-        DLLEXPORT virtual void tick(std::function<void(Model3D*)> f) override;
+        DLLEXPORT virtual bool tick(
+            std::function<void(Model3D*)> f, double time) override;
         //! führt eine render funktion auf jedem Model aus
         DLLEXPORT virtual void render(std::function<void(Model3D*)> f) override;
         //! Gibt die Anzahl an Punkt Lichtquellen zurück