Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
SceneOpenSceneGraph.hh
1 #ifndef __SceneOpenSceneGraph_hh__
2 #define __SceneOpenSceneGraph_hh__
3 
4 #include <bias_config.h>
5 
6 #include <GLviewer/Scenes/SceneBase.hh>
7 #include <Gui/biasgl.h>
8 #ifdef WIN32
9 #undef GL_VERSION_2_1
10 #endif
11 #include <osg/Timer>
12 #include <osg/ShapeDrawable>
13 #include <osgViewer/Viewer>
14 #include <osgDB/ReadFile>
15 #include <Base/Math/Vector3.hh>
16 #include <GLviewer/Scenes/NodeInfo.hh>
17 #include <string>
18 
19 #include <osgAnimation/BasicAnimationManager>
20 #include <osgAnimation/Animation>
21 
22 
23 namespace BIAS
24 {
25 
26  class MyBasicAnimationManager : public osgAnimation::BasicAnimationManager{
27 
28  public:
29  MyBasicAnimationManager() : BasicAnimationManager(){
30  timer = 0.0;
31  counter = 0;
32  }
34 
35  void operator()(osg::Node* node, osg::NodeVisitor* nv){
36  if (nv && nv->getVisitorType() == osg::NodeVisitor::UPDATE_VISITOR)
37  {
38  if (needToLink())
39  {
40  /** manager need to link, it means that an animation has been added
41  so we need to relink all item animated with all animations.
42  We apply the linker visitor on the manager node to affect
43  all its children.
44  But it should not be done here, it should be done in the
45  update of AnimationManager
46  */
47  link(node);
48  }
49  //const osg::FrameStamp* fs = nv->getFrameStamp();
50  update(timer);
51 
52  //std::cout << "Update animation: " << counter++ << ", time: " << timer << std::endl;
53  }
54  traverse(node,nv);
55  }
56 
57  void NextFrame(){
58  timer += 0.04;
59  }
60 
61  void playAnimation(osgAnimation::Animation* pAnimation, int priority = 0, float weight = 1.0)
62  {
63  _lastUpdate = 0.0;
64  timer = 0.0;
65  osgAnimation::BasicAnimationManager::playAnimation(pAnimation,priority,weight);
66  }
67 
68  protected:
69  double timer;
70  int counter;
71 
72 
73  };
74 
75  /** \class SceneOpenSceneGraph
76  \brief Implements narrow access to OpenSceneGraph.
77  \author woelk 09/2007
78  \ingroup g_glviewer*/
79  class BIASGLviewer_EXPORT SceneOpenSceneGraph : public SceneBase
80  {
81  public:
82  SceneOpenSceneGraph(bool redirect = true);
83 
84  virtual ~SceneOpenSceneGraph();
85 
86  /** Initializes OpenSG tree for scene appendance.
87  \return -1 if gl context was not available.
88  \return 0 if initialization was successful or was skipped.
89  \attention only successful if gl context is available.
90  \attention Initialization is skipped if already done.
91  */
92  int Init();
93 
94  /** \brief deletes internal pointers
95  \author woelk 04/2006 */
96  void Reset();
97 
98  /** \brief Append an OSG subtree to the constructed root tree. */
99  void AppendSubTree(osg::ref_ptr<osg::Node>& childNode);
100 
101  /** \brief Append an OSG subtree to the constructed root tree
102  with switch node. WARNING: @param switchNode is updated from the
103  input node to the now prepended switch node. So that the user has
104  a reference to the complete scene branch*/
105  int AppendSubTreeSwitch(osg::ref_ptr<osg::Node>& switchNode,
106  eNodeType NodeType = eNT_Default);
107 
108  /**
109  * \brief Loads contents from file into scene graph.
110  * \param optimize: unused at the moment, only for compatibility with OpenSG
111  * */
112  int AppendSubTreeFromFile(const std::string& fileName,
113  osg::ref_ptr<osg::Node>& childNode,
114  bool optimize = false);
115 
116  int AppendSubTreeFromFile(const std::string& fileName)
117  { osg::ref_ptr<osg::Node> dummy;
118  return AppendSubTreeFromFile(fileName, dummy); }
119 
120  /** \brief Loads contents from 2D image file into scene graph. */
121  int AppendSubTreeFromImageFile(const std::string& fileName,
122  const double& width_mm,
123  osg::ref_ptr<osg::Node>& imageNode);
124 
125  /** \brief Saves contents beginning at node into a file. */
126  bool SaveSubTree(osg::Node *node, const std::string &fileName);
127 
128  /** \brief removes the subtree and the parents except the globalroot
129  of a given node */
130  void RemoveSubTreeAndParents(osg::Node* Node);
131 
132  void RemoveSubTree(osg::ref_ptr<osg::Node>& childNode);
133  void RemoveSubTree(osg::Node* childNode);
134 
135  /** \brief Removes the camera as a beacon for the lights and sets
136  * a fixed position for Point Light or fixed direction
137  * for Directional Light */
138  void SetFixedPointLight(bool on,BIAS::Vector3<double> posPointLight);
139  void SetFixedDirectionalLight(bool on,BIAS::Vector3<double> dirDirectionalLight);
140 
141  /** \brief Toggles the headlight. Initial headlight state is on. */
142  void HeadlightSwitch(bool on=true);
143  /** \brief Toggle the light visible switch */
145  { lightRepresentationVisible_ = !lightRepresentationVisible_;
146  UpdateLightRepresentation_(); };
147 
148  /** \brief Returns center of bounding box around the whole scene.
149  * \attention only works after Init() was called. */
150  BIAS::Vector3<double> GetBoundingBoxCenter();
151 
152  /** Returns bounding box around the whole scene.
153  * \attention only works after Init() was called.
154  * \param min[out]: minimum bounding box value
155  * \param max[out]: maximum bounding box value */
156  virtual void GetBoundingBox(BIAS::Vector3<double> & min,
157  BIAS::Vector3<double> & max);
158 
159  void MakeTransparent(const double transparency);
160 
161  inline void MakeOpaque() { MakeTransparent(0.0); };
162 
163  bool RightMouseDoubleClicked(int x, int y);
164 
165  //enable automatical animation
166  void SetAnimate(const bool animate);
167 
168  /**
169  * \brief setanimationtime() calls draw.
170  * this may not be the desired behaviour in certain cases.
171  * therefore a "CallDraw" flag has been added. */
172  void SetAnimationTime(const double current_time,bool CallDraw=true);
173 
174  void MakeRootShadowNode(osg::ref_ptr<osg::LightSource> lightSource);
175 
176 
177  /** \brief returns ref_ptr to root of tree
178  \author grauel */
179  osg::ref_ptr<osg::Node> GetSceneRoot();
180  TreeDescr GetTreeDescr();
181 
182  /**
183  @brief sets node visbility in guis like treectrlopensgwx
184  @ATTENTION this changes only the nodeinfomap and has
185  no effect on the rendering of the node.
186  if you want to toggle the rendering of nodes use
187  open scene graph methods e.g. node switches.
188  */
189  void SetNodeVisible(osg::Node* node, const bool visible);
190  void SetNodeOccluder(osg::Node* node, const bool state);
191  void SetNodeMarkerID(osg::Node* node, const unsigned long long &id);
193  { return NodeInfoMap_;}
194 
195  bool Save(std::string filename);
196  bool Load(std::string filename);
197  void Clear();
198  bool IsEditableNode(osg::Node* node);
199 
200  /** @brief adds a rectangular mirror surface into the scene
201  * @param ul,..,lr upper left and lower right corners of the mirror
202  * @param mirrortexture filename for texture (color) of the mirror glass,
203  * if the file cannot be opened, white texture is used.
204  *
205  * This function adds a mirror into the scene, specified by its four
206  * corners. However, in the texture of the mirror, you can reduce the
207  * mirror area by setting alpha values to zero. The color values of the
208  * mirror are mixed with the reflection.
209  * Mirror works only when stencil buffer is available.
210  */
211  void AddMirror(const BIAS::Vector3<double>& ul,
212  const BIAS::Vector3<double>& ur,
213  const BIAS::Vector3<double>& ll,
214  const BIAS::Vector3<double>& lr,
215  const std::string &mirrortexture, bool cutUpperHalf=false);
216  void AddParticles();
217  osg::Node* createMirroredScene(osg::Node* model,
218  const BIAS::Vector3<double>& ul,
219  const BIAS::Vector3<double>& ur,
220  const BIAS::Vector3<double>& ll,
221  const BIAS::Vector3<double>& lr,
222  const std::string &mirrortexture, bool cutUpperHalf);
223 
224  void SetClearMask(int clearMask) {
225  clearMask_ = clearMask;
226  sceneView_->getCamera()->setClearMask(clearMask_);
227  }
228 
230  sceneView_->updateTraversal();
231  sceneView_->renderingTraversals();
232  if (sceneView_.valid()) sceneView_->frame();
233  }
234 
236  if(hasOpenSceneGraphAnimation_){
237  osgAnimation::AnimationList animlst = animationManager_.getAnimationList();
238  std::cout << animlst[0]->getStartTime() << std::endl;
239  animationManager_.playAnimation(animlst[0]);
240  std::cout << "after: " << animlst[0]->getStartTime() << std::endl;
241  }
242  }
243 
245  if(hasOpenSceneGraphAnimation_){
246  osgAnimation::AnimationList animlst = animationManager_.getAnimationList();
247  animationManager_.stopAnimation(animlst[0]);
248  }
249  }
250 
252  osgAnimation::AnimationList animlst = animationManager_.getAnimationList();
253  return animationManager_.isPlaying(animlst[0]);
254  }
255 
257  animationManager_.NextFrame();
258  }
259 
260  protected:
264  osg::observer_ptr<osgViewer::GraphicsWindow> embedWindow_;
270  osg::Timer timer_;
271  osg::Timer_t startTick_;
273  double timeOffset_; ///< Time offset because of time "pauses"
274  double lastPauseTime_; ///< Time when the animation was paused for the last time
276 
279  bool animate_;
280 
282  double animTime_;
283 
284  virtual void Draw();
285 
286  void UpdateLightRepresentation_();
287  void DisableZWriteForTransparentNodes_();
288  void CreateStandardNodes_();
289 
290  std::string NodePtr2IDString_(osg::Node* node);
291  NodeInfoMap NodeInfoMap_; //stores additional infos
292  //per node such as visbility
293  //in the gui e.g. in the
294  //treectrlopensgwx
295  //void MergeTransforms_(osg::Node *node);
296 
298 
299  };
300 
301  struct AnimationManagerFinder : public osg::NodeVisitor
302  {
304  AnimationManagerFinder() : osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
305  void apply(osg::Node& node) {
306  if (_am.valid())
307  return;
308  if (node.getUpdateCallback()) {
309  osgAnimation::AnimationManagerBase* b = dynamic_cast<osgAnimation::AnimationManagerBase*>(node.getUpdateCallback());
310  if (b) {
311  _am = new osgAnimation::BasicAnimationManager(*b);
312  return;
313  }
314  }
315  traverse(node);
316  }
317  };
318 
319 
320 
321 
322 
323 }//end of namespace
324 
325 #endif // __SceneOpenSceneGraph_hh__
void operator()(osg::Node *node, osg::NodeVisitor *nv)
osg::ref_ptr< osg::Group > rootTree_
double lastPauseTime_
Time when the animation was paused for the last time.
container for scene root and ptr to nodeinfomap
Definition: NodeInfo.hh:224
const NodeInfoMap & GetNodeInfoMapRef()
osg::ref_ptr< osgViewer::Viewer > sceneView_
double timeOffset_
Time offset because of time &quot;pauses&quot;.
MyBasicAnimationManager animationManager_
Implements narrow access to OpenSceneGraph.
osg::ref_ptr< osgAnimation::BasicAnimationManager > _am
osg::ref_ptr< osg::Light > light_
Base class for all scenes.
Definition: SceneBase.hh:68
osg::ref_ptr< osg::MatrixTransform > lightTransform_
osg::observer_ptr< osgViewer::GraphicsWindow > embedWindow_
void playAnimation(osgAnimation::Animation *pAnimation, int priority=0, float weight=1.0)
osg::ref_ptr< osg::Switch > lightRepresentationSwitch_
void SetClearMask(int clearMask)
osg::ref_ptr< osg::FrameStamp > frameStamp_
int AppendSubTreeFromFile(const std::string &fileName)
osg::ref_ptr< osg::Geode > lightRepresentationGeode_
eNodeType
Definition: NodeInfo.hh:16
void LightRepresentationVisibleSwitch()
Toggle the light visible switch.