vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FadeFilter.h
Go to the documentation of this file.
1 #ifndef FADE_FILTER_H
2 #define FADE_FILTER_H
3 
5 
6 #ifdef COMPILE_PHANTOM_DEVICE
7 
8 namespace vreckoDP_PHANToM {
9 
10 /* PHANToM filter to fade-in or fade-out the forces and keep them at zero level in the meantime.
11  Can be used alone to fade in the force on the start of the program,
12  or as a base class from which other filter is inherited. */
13 class FadeForceFilter : public PHANToMFilter {
14 public:
15  FadeForceFilter(const char* abName = "FadeFilter");
16 
17  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *parameters);
18  virtual void postInitialize(void);
19 
20  virtual void apply(PHANToMSTATE &pState, osg::Vec3 &force, osg::Vec3 &torque);
21 
22 protected:
23 // Parameters loaded from the Parameters element:
24  double defaultFadeTimeInSecs;
25  bool bFadeInFromStart;
26 
27  enum FadeState {
28  Normal, // nothing is done
29  FadingOut, // fading out
30  Lowered, // forces are at zero level
31  FadingIn // fading in
32  };
33 
34  FadeState fadeState;
35  double fadeTimeInSecs;
36  osg::Timer_t fadeTimeInTicks;
37  osg::Timer_t fadeStart; // [in Timer tick units]
38 
39 
40  void startFade(bool bFadeIn, double newFadeTimeInSecs = -1);
41  // If [bFadeIn] is "false", we are fading out.
42  // This method will also take in account any previous fade that might be currently in progress.
43  // If [fadeTimeInSecs] is negative, the [defaultFadeTimeInSecs] will be used.
44 
45  inline float currentFadeFactor(osg::Timer_t currentTick) { // returns 0.0 - 1.0
46  checkFadingForEnd(currentTick);
47 
48  if (fadeState == Normal)
49  return 1.0f;
50  if (fadeState == Lowered)
51  return 0.0f;
52 
53  if (fadeState == FadingIn)
54  return (float)(currentTick - fadeStart) / (float)fadeTimeInTicks;
55 
56  // => this must be a fade-out
57  return 1.0f - ((float)(currentTick - fadeStart) / (float)fadeTimeInTicks);
58  }
59 
60  inline void checkFadingForEnd(osg::Timer_t currentTick) {
61  if ((fadeState == Normal) || (fadeState == Lowered))
62  return; // all OK
63 
64  if (currentTick > fadeStart + fadeTimeInTicks) {
65  // fading passed the end -> stop fading
66  if (fadeState == FadingIn)
67  fadeState = Normal;
68  else
69  fadeState = Lowered;
70  }
71  }
72 };
73 
74 }
75 
76 #endif
77 
78 
79 #endif