VR-Forces 4.8 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtOsgFindNodesVisitor.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2019 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <vrvOsg/vrvOsg.h>
13 
16 
18 
21 
22 #include <osg/Switch>
23 #include <osg/LOD>
24 #include <osg/PositionAttitudeTransform>
25 #include <osg/NodeVisitor>
26 #include <osg/MatrixTransform>
27 #include <osgSim/MultiSwitch>
28 #include <osgSim/DOFTransform>
29 
30 #include <vector>
31 #include <deque>
32 #include <osg/PolygonOffset>
33 
36 
37 namespace makVrv
38 {
39 
42  {
44  : hasDofParent(false)
45  , cullFaceEnabled(true)
46  {
47  }
48 
50  osg::Matrixd xform;
52  osg::NodePath nodePath;
54  bool hasDofParent; //for now just check to see if there is a dof node.
57  };
58 
60  template<typename T>
61  struct DtOsgFindNodesVisitor : public osg::NodeVisitor
62  {
63  DtOsgFindNodesVisitor(osg::NodeVisitor::TraversalMode traversalMode) : osg::NodeVisitor(traversalMode) { }
64 
65  void apply(osg::Node& node)
66  {
67  T* result = dynamic_cast<T*>(&node);
68  if (result)
69  results.push_back(result);
70  traverse(node);
71  }
72 
73  std::vector<T*> results;
74  };
75 
77  template<typename T>
78  struct DtOsgFindNodesVisitorRefPtr : public osg::NodeVisitor
79  {
80  DtOsgFindNodesVisitorRefPtr(osg::NodeVisitor::TraversalMode traversalMode) : osg::NodeVisitor(traversalMode) { }
81 
82  void apply(osg::Node& node)
83  {
84  T* result = dynamic_cast<T*>(&node);
85  if (result)
86  results.push_back(result);
87  traverse(node);
88  }
89 
90  std::vector< osg::ref_ptr<T> > results;
91  };
92 
95  template<typename T>
96  struct DtOsgFindNodesVisitorRefPtrFlattenGraph : public osg::NodeVisitor
97  {
98  DtOsgFindNodesVisitorRefPtrFlattenGraph(osg::NodeVisitor::TraversalMode traversalMode, osg::Node::NodeMask gatherNodeMask = 0, const std::set< osg::Node* > &nodeIgnoreList = std::set<osg::Node*>())
99  : osg::NodeVisitor(traversalMode)
101  , myGatherNodeMask(gatherNodeMask)
102  {
103  matrixStack.push_back(osg::Matrix::identity());
104  }
105 
106  virtual bool isCullFaceEnabled(osg::StateSet* ss) const
107  {
108  bool cullFaceEnabled = (myCullFaceEnabledStack.empty() == false) ? myCullFaceEnabledStack.top() : true;
109  if (ss == 0)
110  {
111  return cullFaceEnabled;
112  }
113 
114  osg::StateAttribute::GLModeValue cullFaceModeValue = ss->getMode(GL_CULL_FACE);
115  if ((cullFaceModeValue & osg::StateAttribute::INHERIT) == osg::StateAttribute::INHERIT)
116  {
117  return cullFaceEnabled;
118  }
119  else
120  {
121  return (cullFaceModeValue & osg::StateAttribute::ON) == osg::StateAttribute::ON;
122  }
123  }
124 
125  virtual DtIndirectPolygonOffset gatherPolygonOffset(osg::StateSet* ss) const
126  {
127  DtIndirectPolygonOffset dtPolygonOffset = (myPolygonOffsetStack.empty() == false) ? myPolygonOffsetStack.top() : DtIndirectPolygonOffset(0, 0);
128 
129  if (ss == 0)
130  {
131  return dtPolygonOffset;
132  }
133 
134  osg::PolygonOffset* polygonOffset = dynamic_cast<osg::PolygonOffset*>(ss->getAttribute(osg::StateAttribute::POLYGONOFFSET));
135  if (polygonOffset)
136  {
137  dtPolygonOffset = DtIndirectPolygonOffset(polygonOffset->getFactor()*polygonOffset->getFactorMultiplier(), polygonOffset->getUnits()*polygonOffset->getUnitsMultiplier());
138  }
139  return dtPolygonOffset;
140  }
141 
142  virtual void apply(osg::Node& node)
143  {
144  if (nodeIgnoreList.find(&node) != nodeIgnoreList.end())
145  {
146  return;
147  }
148 
149  bool cullFaceEnabled = isCullFaceEnabled(node.getStateSet());
150  myCullFaceEnabledStack.push(cullFaceEnabled);
151  DtIndirectPolygonOffset polygonOffset = gatherPolygonOffset(node.getStateSet());
152  myPolygonOffsetStack.push(polygonOffset);
153 
154 
155  T* result = dynamic_cast<T*>(&node);
156  if (result)
157  {
158  bool processThisNode = true;
159 
160  //the myGatherNodesMask is really an inverse mask. If any bits are the
161  // same between the mask and the node mask then it is a non indirected object
162  // so it should not be included in the results
163  // We also need to make sure all the children are not indirected either
164  if ((node.getNodeMask() & myGatherNodeMask) != 0)
165  {
166  processThisNode = false;
167  for(unsigned int i = 0; i < result->getNumChildren(); i++)
168  {
169  osg::Node* child = result->getChild(i);
170  if (child)
171  {
172  if ((child->getNodeMask() & myGatherNodeMask) == 0)
173  {
174  processThisNode = true;
175  }
176  }
177  }
178  }
179 
180  if (processThisNode)
181  {
182  DtNodePlusTraversalInfo nodeAndXForm;
183 
184  osg::Matrixd mat = matrixStack.back();
185 
186  osg::MatrixTransform* nodeAsMatrixXForm = dynamic_cast<osg::MatrixTransform*>(&node);
187  if (nodeAsMatrixXForm)
188  {
189  mat = nodeAsMatrixXForm->getMatrix() * mat;
190  }
191 
192  nodeAndXForm.xform = mat;
193  nodeAndXForm.node = result;
194  nodeAndXForm.lodStack = lodStack;
195  nodeAndXForm.nodePath = getNodePath();
196  nodeAndXForm.switchStack = switchStack;
197  //for now dof nodes are handled outside of indirect so it is enough to know that it has a sequence as a parent.
198  nodeAndXForm.hasDofParent = !dofStack.empty();
199 
200  nodeAndXForm.cullFaceEnabled = cullFaceEnabled;
201  nodeAndXForm.polygonOffset = polygonOffset;
202 
203  results.push_back(nodeAndXForm);
204  }
205  }
206 
207  osgSim::DOFTransform* dof = dynamic_cast<osgSim::DOFTransform*>(&node);
208  if (dof)
209  {
210  dofStack.push_back(dof);
211  }
212 
213  osg::MatrixTransform* mat = dynamic_cast<osg::MatrixTransform*>(&node);
214  if (mat)
215  {
216  matrixStack.push_back(mat->getMatrix() * matrixStack.back());
217  }
218 
219  osg::PositionAttitudeTransform* pat = dynamic_cast<osg::PositionAttitudeTransform*>(&node);
220  if (pat)
221  {
222  osg::Matrixd patMat;
223  patMat.setTrans(pat->getPosition());
224  patMat.setRotate(pat->getAttitude());
225  patMat.makeScale(pat->getScale());
226  matrixStack.push_back(patMat * matrixStack.back());
227  }
228 
229 
230  osg::Switch* osgSwitch = dynamic_cast<osg::Switch*>(&node);
231  if (osgSwitch)
232  {
233  for (unsigned int i = 0; i < osgSwitch->getNumChildren(); ++i)
234  {
235  osg::Node* child = osgSwitch->getChild(i);
236  if (child)
237  {
239  0,
240  (int)i,
241  (unsigned int)switchStack.size(),
242  osg::NodePath(),
243  0
244  };
245  switchStack.push_back(DtIndirectSwitchManager::NodeSwitchInfoPair(osgSwitch, switchInfo));
246  child->accept(*this);
247  switchStack.pop_back();
248  }
249  }
251  myPolygonOffsetStack.pop();
252  return;
253  }
254 
255  osgSim::MultiSwitch* multiSwitch = dynamic_cast<osgSim::MultiSwitch*>(&node);
256  if (multiSwitch)
257  {
258  for (unsigned int i = 0; i < multiSwitch->getNumChildren(); ++i)
259  {
260  osg::Node* child = multiSwitch->getChild(i);
261  if (child)
262  {
264  0,
265  (int)i,
266  (unsigned int)switchStack.size(),
267  osg::NodePath(),
268  0
269  };
270  switchStack.push_back(DtIndirectSwitchManager::NodeSwitchInfoPair(multiSwitch, switchInfo));
271  child->accept(*this);
272  switchStack.pop_back();
273  }
274  }
276  myPolygonOffsetStack.pop();
277  return;
278  }
279 
280  DtDynamicTerrainSwitchNode* dynamicSwitch = dynamic_cast<DtDynamicTerrainSwitchNode*>(&node);
281  if (dynamicSwitch)
282  {
283  for (unsigned int i = 0; i < dynamicSwitch->getNumChildren(); ++i)
284  {
285  osg::Node* child = dynamicSwitch->getChild(i);
286  if (child)
287  {
289  0,
290  (int)i,
291  (unsigned int)switchStack.size(),
292  this->getNodePath(),
293  0
294  };
295  switchStack.push_back(DtIndirectSwitchManager::NodeSwitchInfoPair(dynamicSwitch, switchInfo));
296  child->accept(*this);
297  switchStack.pop_back();
298  }
299  }
301  myPolygonOffsetStack.pop();
302  return;
303  }
304 
305  osg::Sequence* dynamicSequence = dynamic_cast<osg::Sequence*>(&node);
306  if (dynamicSequence)
307  {
308  for (unsigned int i = 0; i < dynamicSequence->getNumChildren(); ++i)
309  {
310  osg::Node* child = dynamicSequence->getChild(i);
311  if (child)
312  {
314  0,
315  (int)i,
316  (unsigned int)switchStack.size(),
317  this->getNodePath(),
318  0
319  };
320  switchStack.push_back(DtIndirectSwitchManager::NodeSwitchInfoPair(dynamicSequence, switchInfo));
321  child->accept(*this);
322  switchStack.pop_back();
323  }
324  }
326  myPolygonOffsetStack.pop();
327  return;
328  }
329 
330  osg::LOD* lod = dynamic_cast<osg::LOD*>(&node);
331  if (lod)
332  {
333  for (unsigned int i = 0; i < lod->getNumChildren(); i++)
334  {
336  info.myCenter = lod->getCenter() * matrixStack.back();
337  const osg::LOD::MinMaxPair& range = lod->getRangeList()[i];
338  info.myStartRange = range.first;
339  info.myEndRange = range.second;
340 
341  lodStack.push_back(info);
342  lod->getChild(i)->accept(*this);
343  lodStack.pop_back();
344  }
345  }
346  else
347  {
348  traverse(node);
349  }
350 
351  if (mat)
352  {
353  matrixStack.pop_back();
354  }
355 
356  if (dof)
357  {
358  dofStack.pop_back();
359  }
360 
362  myPolygonOffsetStack.pop();
363  }
364 
365  std::vector< DtNodePlusTraversalInfo > results;
366  std::set< osg::Node* > nodeIgnoreList;
367  std::deque<osg::Matrixd> matrixStack;
370  std::deque<osgSim::DOFTransform*> dofStack;
371  osg::Node::NodeMask myGatherNodeMask;
372 
373  std::stack<bool> myCullFaceEnabledStack;
374  std::stack<DtIndirectPolygonOffset> myPolygonOffsetStack;
375 
376  };
377 }
structure for holding the information about a particular instance of a particular switch...
Definition: DtIndirectSwitchManager.h:61
DtIndirectSwitchManager::DequeNodeSwitchInfoPairs switchStack
Definition: DtOsgFindNodesVisitor.h:53
float myEndRange
Definition: DtIndirectLodManager.h:89
DtIndirectSwitchManager::DequeNodeSwitchInfoPairs switchStack
Definition: DtOsgFindNodesVisitor.h:369
std::vector< DtNodePlusTraversalInfo > results
Definition: DtOsgFindNodesVisitor.h:365
structure for holding the information about a particular instance of a particular lod...
Definition: DtIndirectLodManager.h:86
DtDynamicTerrainSwitchNode replaces an osgSim::multiswitch in a terrain patch (or page) that is tagge...
Definition: DtDynamicTerrainSwitchNode.h:74
bool hasDofParent
Definition: DtOsgFindNodesVisitor.h:54
virtual bool isCullFaceEnabled(osg::StateSet *ss) const
Definition: DtOsgFindNodesVisitor.h:106
std::set< osg::Node * > nodeIgnoreList
Definition: DtOsgFindNodesVisitor.h:366
std::deque< NodeSwitchInfoPair > DequeNodeSwitchInfoPairs
Definition: DtIndirectSwitchManager.h:72
visitor for gathering all nodes of the same type in a scene graph. Returns vector of raw pointers ...
Definition: DtOsgFindNodesVisitor.h:61
typedef int(ZCALLBACK *close_file_func) OF((voidpf opaque
Contains makVrv::DtDynamicTerrainSequenceNode class.
DtGeometrySpecification class declaration.
structure for holding the traversal info which is gathered by the DtOsgFindNodesVisitorRefPtrFlattenG...
Definition: DtOsgFindNodesVisitor.h:41
DtOsgFindNodesVisitorRefPtr(osg::NodeVisitor::TraversalMode traversalMode)
Definition: DtOsgFindNodesVisitor.h:80
DtIndirectLodManager::DequeIndirectLodInfos lodStack
Definition: DtOsgFindNodesVisitor.h:51
DtNodePlusTraversalInfo()
Definition: DtOsgFindNodesVisitor.h:43
std::deque< osg::Matrixd > matrixStack
Definition: DtOsgFindNodesVisitor.h:367
osg::Matrixd xform
Definition: DtOsgFindNodesVisitor.h:50
osg::Vec3 myCenter
Definition: DtIndirectLodManager.h:91
float myStartRange
Definition: DtIndirectLodManager.h:88
visitor for gathering all nodes of the same type in a scene graph. Returns vector of ref pointers ...
Definition: DtOsgFindNodesVisitor.h:78
virtual void apply(osg::Node &node)
Definition: DtOsgFindNodesVisitor.h:142
std::stack< DtIndirectPolygonOffset > myPolygonOffsetStack
Definition: DtOsgFindNodesVisitor.h:374
Contains makVrv::DtIndirectSwitchManager class.
DtIndirectPolygonOffset polygonOffset
Definition: DtOsgFindNodesVisitor.h:56
std::pair< NodeObserverPtr, IndirectSwitchInfo > NodeSwitchInfoPair
Definition: DtIndirectSwitchManager.h:71
void apply(osg::Node &node)
Definition: DtOsgFindNodesVisitor.h:82
DtOsgFindNodesVisitor(osg::NodeVisitor::TraversalMode traversalMode)
Definition: DtOsgFindNodesVisitor.h:63
osg::NodePath nodePath
Definition: DtOsgFindNodesVisitor.h:52
bool cullFaceEnabled
Definition: DtOsgFindNodesVisitor.h:55
Contains makVrv::DtIndirectLodManager class.
osg::Node::NodeMask myGatherNodeMask
Definition: DtOsgFindNodesVisitor.h:371
DtIndirectLodManager::DequeIndirectLodInfos lodStack
Definition: DtOsgFindNodesVisitor.h:368
non virtual container class for defining a polygon offset state
Definition: DtIndirectPolygonOffset.h:18
Contains DLL export macros.
Contains makVrv::DtDynamicTerrainSwitchNode class.
osg::ref_ptr< osg::Node > node
Definition: DtOsgFindNodesVisitor.h:49
std::stack< bool > myCullFaceEnabledStack
Definition: DtOsgFindNodesVisitor.h:373
std::vector< T * > results
Definition: DtOsgFindNodesVisitor.h:73
visitor for gathering all nodes of the same type in a scene graph.
Definition: DtOsgFindNodesVisitor.h:96
myGatherNodeMask(gatherNodeMask)
Definition: DtOsgFindNodesVisitor.h:101
std::deque< IndirectLodInfo > DequeIndirectLodInfos
Definition: DtIndirectLodManager.h:95
assert macros
std::vector< osg::ref_ptr< T > > results
Definition: DtOsgFindNodesVisitor.h:90
Contains the DtIndirectPolygonOffset class declaration.
virtual DtIndirectPolygonOffset gatherPolygonOffset(osg::StateSet *ss) const
Definition: DtOsgFindNodesVisitor.h:125
std::deque< osgSim::DOFTransform * > dofStack
Definition: DtOsgFindNodesVisitor.h:370
void apply(osg::Node &node)
Definition: DtOsgFindNodesVisitor.h:65

Document ID: Generated on Thu Aug 27 10:56:05 EDT 2020 from SVN revision 217100
Copyright © 2005-2020 MAK Technologies. All Rights Reserved (www.mak.com)