OpenCOVER
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
VRSceneGraph.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
8 #ifndef VR_SCENE_GRAPH_H
9 #define VR_SCENE_GRAPH_H
10 
25 #include <util/common.h>
26 #include <osg/Node>
27 #include <osg/Matrix>
28 #include <osg/Group>
29 #include <osg/MatrixTransform>
30 #include <osg/ref_ptr>
31 #include <osg/Multisample>
32 #include <osg/Material>
33 #include <osg/ClipNode>
34 #include <osgFX/Scribe>
35 
36 #include "ui/Owner.h"
37 
38 namespace opencover {
39 namespace ui {
40 class Menu;
41 class Action;
42 class Button;
43 class SelectionList;
44 }
45 }
46 
47 namespace osg
48 {
49 class StateSet;
50 class ClipNode;
51 }
52 
53 namespace vrui
54 {
55 class coCombinedButtonInteraction;
56 }
57 
58 namespace opencover
59 {
60 class COVEREXPORT VRSceneGraph: public ui::Owner
61 {
62 public:
69  };
70  VRSceneGraph();
71  virtual ~VRSceneGraph();
72  static VRSceneGraph *instance();
73 
74  bool saveScenegraph(const std::string &filename, bool withMenu=false);
75 #ifdef PHANTOM_TRACKER
76  static void manipulateCallback(void *sceneGraph, buttonSpecCell *spec);
77 #endif
78 
79  osg::MatrixTransform *loadAxisGeode(float scale);
80  osg::Node *loadHandIcon(const char *name);
81  osg::Node *loadHandLine();
82 
83  void addMenuItem(osg::Group *itemGroup);
84  osg::Group *getMenuGroup()
85  {
86  return m_menuGroupNode.get();
87  }
88  bool menuVisible() const;
89  void toggleMenu();
90  void setMenu(bool state);
91  void setMenuMode(bool state);
92  void applyMenuModeToMenus();
93  void toggleHeadTracking(bool state);
94  void setObjects(bool state);
95 
96  // process key events
97  bool keyEvent(int type, int keySym, int mod);
98  osg::Group *getScene()
99  {
100  return m_scene.get();
101  };
102 
103  osg::Group *getObjectsScene()
104  {
105  return m_objectsScene.get();
106  }
107 
108  void config();
109  void init();
110  void update();
111 
112  osg::MatrixTransform *getTransform() const
113  {
114  return m_objectsTransform.get();
115  }
116  osg::MatrixTransform *getScaleTransform() const
117  {
118  return (m_scaleTransform);
119  }
120  osg::MatrixTransform *getHandTransform() const
121  {
122  return (m_handTransform.get());
123  }
124  osg::Vec3 getWorldPointOfInterest() const;
125  void getHandWorldPosition(float *, float *, float *);
126  void addPointerIcon(osg::Node *node);
127  void removePointerIcon(osg::Node *node);
128 
129  osg::StateSet *loadDefaultGeostate(osg::Material::ColorMode mode = osg::Material::OFF);
130  osg::StateSet *loadGlobalGeostate();
131  osg::StateSet *loadUnlightedGeostate(osg::Material::ColorMode mode = osg::Material::OFF);
132  osg::StateSet *loadTransparentGeostate(osg::Material::ColorMode mode = osg::Material::OFF);
133 
134  void setWireframe(WireframeMode mode);
135  void setPointerType(int pointerType);
137  {
138  return (m_pointerType);
139  }
140 
141  osg::BoundingSphere getBoundingSphere();
142 
143  void setNodeBounds(osg::Node *node, const osg::BoundingSphere *bs);
144 
145  bool highQuality() const;
146 
147  template <class T>
148  T *findFirstNode(const char *name, bool startsWith = false, osg::Node * rootNode = NULL)
149  {
150  if (!rootNode)
151  rootNode = m_objectsRoot;
152 
153  if (!rootNode)
154  return NULL;
155 
156  if (!name)
157  return NULL;
158 
159  if (!(startsWith ? strncmp(rootNode->getName().c_str(), name, strlen(name)) : strcmp(rootNode->getName().c_str(), name)))
160  {
161  T *node = dynamic_cast<T *>(rootNode);
162  if (node)
163  return node;
164  }
165 
166  osg::Group *group = dynamic_cast<osg::Group *>(rootNode);
167  if (group)
168  {
169  for (unsigned int i = 0; i < group->getNumChildren(); i++)
170  {
171  T *node = findFirstNode<T>(name, startsWith, group->getChild(i));
172  if (node)
173  {
174  return node;
175  }
176  }
177  return NULL;
178  }
179  else
180  {
181  return NULL;
182  }
183  }
184 
185  float scaleMode()
186  {
187  return m_scaleMode;
188  }
189  void setScaleMode(float scaleMode)
190  {
191  m_scaleMode = scaleMode;
192  }
193  float scaleFactor()
194  {
195  return m_scaleFactor;
196  }
197  void setScaleFactor(float scaleFactor, bool sync = true);
198  void scaleAllObjects(bool resetView = false);
199  bool isScalingAllObjects() const
200  {
201  return m_scalingAllObjects;
202  }
203  void boundingSphereToMatrices(const osg::BoundingSphere &boundingSphere,
204  bool resetView, osg::Matrix *currentMatrix, float *scaleFactor) const;
205  void adjustScale();
206 
207  void toggleAxis(bool state);
208  void toggleHighQuality(bool state);
209  void viewAll(bool resetView = false);
210  float floorHeight()
211  {
212  return m_floorHeight;
213  }
214  osg::ClipNode *objectsRoot()
215  {
216  return m_objectsRoot;
217  }
218 
219  osg::ref_ptr<osg::Node> getHandSphere()
220  {
221  return m_handSphere;
222  }
223 
224  void setMultisampling(osg::Multisample::Mode);
225 
226  void setScaleFromButton(float direction);
227  void setScaleFactorButton(float f);
228  void setScaleRestrictFactor(float min, float max);
229 
230  void setRestrictBox(float minX, float maxX, float minY, float maxY, float minZ, float maxZ);
231 
232  //Coloring
233  void setColor(const char *nodeName, int *color, float transparency);
234  void setColor(osg::Geode *geode, int *color, float transparency);
235  void setTransparency(const char *nodeName, float transparency);
236  void setTransparency(osg::Geode *geode, float transparency);
237  void setShader(const char *nodeName, const char *shaderName, const char *paraFloat, const char *paraVec2, const char *paraVec3, const char *paraVec4, const char *paraInt, const char *paraBool, const char *paraMat2, const char *paraMat3, const char *paraMat4);
238  void setShader(osg::Geode *geode, const char *shaderName, const char *paraFloat, const char *paraVec2, const char *paraVec3, const char *paraVec4, const char *paraInt, const char *paraBool, const char *paraMat2, const char *paraMat3, const char *paraMat4);
239  void setMaterial(const char *nodeName, int *ambient, int *diffuse, int *specular, float shininess, float transparency);
240  void setMaterial(osg::Geode *geode, int *ambient, int *diffuse, int *specular, float shininess, float transparency);
241 
242  // disables "store scenegraph"
243  void protectScenegraph();
244 
245  int m_vectorInteractor; //< don't use - for COVISE plugin only
246 
247  bool isHighQuality() const;
248 
249 private:
250  static VRSceneGraph *s_instance;
251  int readConfigFile();
252  void initAxis();
253  void initHandDeviceGeometry();
254  void initMatrices();
255  void initSceneGraph();
256  bool saveScenegraph(bool withMenu);
257 
258 #ifdef PHANTOM_TRACKER
259  int m_forceFeedbackON;
260  int m_forceFeedbackMode;
261  float m_forceScale;
262 #endif
263 
264  osg::ref_ptr<osg::Group> m_scene, m_objectsScene;
265  osg::ref_ptr<osgFX::Scribe> m_lineHider;
266  osg::ref_ptr<osg::MatrixTransform> m_handTransform;
267  osg::ref_ptr<osg::MatrixTransform> m_handAxisTransform, m_viewerAxisTransform, m_smallSceneAxisTransform;
268  osg::ref_ptr<osg::MatrixTransform> m_worldAxis, m_handAxis, m_viewerAxis, m_objectAxis, m_smallSceneAxis;
269  osg::ref_ptr<osg::Group> m_menuGroupNode;
270  osg::ref_ptr<osg::MatrixTransform> m_pointerDepthTransform;
271  float m_pointerDepth;
272  osg::ref_ptr<osg::Node> m_handPlane;
273  osg::ref_ptr<osg::Node> m_handLine;
274  osg::ref_ptr<osg::Node> m_handNormal;
275  osg::ref_ptr<osg::Node> m_handCube;
276  osg::ref_ptr<osg::Node> m_handFly;
277  osg::ref_ptr<osg::Node> m_handDrive;
278  osg::ref_ptr<osg::Node> m_handWalk;
279  osg::ref_ptr<osg::Node> m_handPyramid;
280  osg::ref_ptr<osg::Node> m_handProbe;
281  osg::ref_ptr<osg::Node> m_handAnchor;
282  osg::ref_ptr<osg::Node> m_handSphere;
283 
284  bool showSmallSceneAxis_;
285  bool transparentPointer_;
286 
287  osg::StateSet *m_rootStateSet, *m_objectsStateSet;
288  osg::ClipNode *m_objectsRoot;
289 
290  float m_floorHeight;
291  WireframeMode m_wireframe;
292  bool m_textured = true; /* =true: textures are drawn as intended */
293  bool m_shaders = true; /* =true: shaders are applied */
294  bool m_coordAxis = false; /* =true: coord Axis will be drawn */
295  bool m_showMenu = true;
296  bool m_showObjects = true;
297  bool m_firstTime = true;
298  bool m_pointerVisible = false;
299 
300  osg::Matrix m_invBaseMatrix;
301  osg::Matrix m_oldInvBaseMatrix;
302 
303  int m_pointerType;
304 
305  // attribute SCALE attached to PerformerScene objects:
306  // SCALE viewAll : scaleMode=1.0
307  // SCALE keep : scaleMode=0.0
308  // SCALE <pos number eg. 2.0 or 0.5> : scaleMode=<number>
309  // no SCALE attribute : scaleMode=-1.0
310  float m_scaleMode, m_scaleFactor;
311  float m_scaleFactorButton;
312  float m_scaleRestrictFactorMin, m_scaleRestrictFactorMax;
313  float m_transRestrictMinX, m_transRestrictMinY, m_transRestrictMinZ;
314  float m_transRestrictMaxX, m_transRestrictMaxY, m_transRestrictMaxZ;
315  bool m_scaleAllOn;
316  bool m_scalingAllObjects;
317  osg::MatrixTransform *m_scaleTransform, *m_handIconScaleTransform, *m_handAxisScaleTransform;
318  float m_handIconSize;
319  float m_handIconOffset;
320 
321  float wiiPos;
322 
323  osg::ref_ptr<osg::MatrixTransform> m_objectsTransform;
324  osg::ref_ptr<osg::Multisample> m_Multisample;
325  typedef std::set<osg::Node *> NodeSet;
326  NodeSet m_specialBoundsNodeList;
327  void dirtySpecialBounds();
328 
329  bool menusAreHidden;
330  osg::ref_ptr<osg::Program> emptyProgram_;
331 
332  bool isScenegraphProtected_;
333 
334  typedef std::map<osg::Drawable *, osg::ref_ptr<osg::Material> > StoredMaterialsMap;
335  StoredMaterialsMap storedMaterials;
336  void storeMaterial(osg::Drawable *drawable);
337  bool m_enableHighQualityOption, m_switchToHighQuality, m_highQuality;
338  vrui::coCombinedButtonInteraction *m_interactionHQ;
339 
340  ui::Menu *m_miscMenu=nullptr;
341  ui::SelectionList *m_drawStyle=nullptr;
342  ui::Button *m_trackHead=nullptr;
343  ui::SelectionList *m_showStats=nullptr;
344  ui::Button *m_showAxis=nullptr, *m_allowHighQuality=nullptr;
345  ui::Button *m_useTextures=nullptr, *m_useShaders=nullptr;
346 };
347 }
348 #endif
osg::Group * getObjectsScene()
Definition: VRSceneGraph.h:103
osg::MatrixTransform * getHandTransform() const
Definition: VRSceneGraph.h:120
float floorHeight()
Definition: VRSceneGraph.h:210
float scaleMode()
Definition: VRSceneGraph.h:185
void setScaleMode(float scaleMode)
Definition: VRSceneGraph.h:189
Definition: VRSceneGraph.h:64
Definition: VRSceneGraph.h:67
osg::Group * getMenuGroup()
Definition: VRSceneGraph.h:84
float scaleFactor()
Definition: VRSceneGraph.h:193
int m_vectorInteractor
Definition: VRSceneGraph.h:245
WireframeMode
Definition: VRSceneGraph.h:63
bool isScalingAllObjects() const
Definition: VRSceneGraph.h:199
Definition: VRSceneGraph.h:65
osg::MatrixTransform * getTransform() const
Definition: VRSceneGraph.h:112
double OSGVRUIEXPORT mod(double a, double b)
Definition: coCombinedButtonInteraction.h:20
Definition: VRSceneGraph.h:68
Definition: VRSceneGraph.h:66
osg::MatrixTransform * getScaleTransform() const
Definition: VRSceneGraph.h:116
T * findFirstNode(const char *name, bool startsWith=false, osg::Node *rootNode=NULL)
Definition: VRSceneGraph.h:148
osg::ref_ptr< osg::Node > getHandSphere()
Definition: VRSceneGraph.h:219
osg::ClipNode * objectsRoot()
Definition: VRSceneGraph.h:214
Definition: VRSceneGraph.h:60
int getPointerType()
Definition: VRSceneGraph.h:136
osg::Group * getScene()
Definition: VRSceneGraph.h:98