VR-Forces 4.5 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DtInstanceManager.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2015 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
8 
9 #pragma once
10 
11 #include <vrvOsg/vrvOsg.h>
12 #include <vrvOsg/DtInstanceState.h>
14 #include <vrvCore/DtUniqueID.h>
17 
18 #include <matrix/vlVector.h>
19 
20 #include <osgUtil/LineSegmentIntersector>
21 
22 #include <osg/ref_ptr>
23 #include <osg/LightSource>
24 
25 #include <boost/unordered_map.hpp>
26 
27 #define DT_INSTANCE_MANAGER_CULL_IS_PARALLEL 1
28 #define DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL 1
29 
30 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
32 #endif
33 
34 #define DT_INSTANCE_MANAGER_UPDATEDISTANCESTOEYE_IS_PARALLEL 1
35 
36 class DtTaitBryan;
37 
38 namespace osg
39 {
40  class Geometry;
41  class Image;
42  class Texture2D;
43  class Vec2f;
44  class Vec3f;
45  class Geode;
46  class View;
47 }
48 
49 namespace osgEarth
50 {
51  class VirtualProgram;
52 }
53 
54 namespace makVrv
55 {
56  class DtGLTextureBufferObject;
57  class DtInstanceProcessor;
58  class DtOsgIntersectorResult;
59  class DtChannelManager;
60  class DtChannel;
61  class DtPerViewUniformCallback;
62  class DtInstanceNode;
63  class DtLight;
64  class DtOsgCullVisitorParameters;
65  class DtViewSpecificGroup;
66 
72  {
73  public:
74  typedef std::vector<osg::ref_ptr<osg::Geometry> > GeometryVec;
75  typedef std::vector<char> BoolVec; // Using vec of chars for threadsafety
76  typedef std::vector<char> ByteVec;
77  typedef std::map<osg::ref_ptr< osgEarth::VirtualProgram >, osg::ref_ptr<osgEarth::VirtualProgram> > VpMap;
78  typedef std::vector<osg::ref_ptr<osgEarth::VirtualProgram> > VpRefList;
79  typedef std::vector<ArrayRange> DrawableRangeVec;
80  typedef std::vector<osg::Vec2f> TextureCoordinateVec;
81  typedef std::vector<float> FloatVec;
82 
84  static const unsigned int NumVec4PerInstance = 8;
85 
87  struct InstancedLod
88  {
90  double minRange;
91 
93  double maxRange;
94 
99  };
100 
105  {
108 
111  };
112 
115  {
117  enum SequenceMode { Play, Stop, Pause, Resume };
118 
121 
124 
126  float startTime;
127 
129  float endTime;
130 
133 
135  float speed;
136 
139  };
140 
143  {
145  osg::Vec3f position;
146  osg::Vec3f direction;
147  unsigned int drawableIx; // index to drawable the light is attached to
148  };
149 
152  {
155 
159  unsigned int numChildren;
160 
163 
168  };
169 
172  {
175 
178 
181 
183  osg::Vec3f initialHpr;
184 
186  osg::Vec3f initialTranslate;
187 
191  unsigned int numChildren;
192 
195  };
196 
199  {
201  osg::Vec3f trans;
202 
203  osg::Vec3f transVel;
204 
206  osg::Vec3f hpr;
207 
209  osg::Vec3f hprVel;
210 
211 
213  float lastUpdate;
214  };
215 
216  struct LightSource
217  {
218  osg::ref_ptr<osg::LightSource> lightSource;
219  unsigned int attachDrawableIx;
220  };
221 
223  typedef std::vector<LightSource> LightSourceVec;
224  typedef std::vector<ModelTypeDofData> PerDofDataVec;
225  typedef std::vector<InstancedSwitch> InstancedSwitchVec;
226  typedef std::vector<InstancedSequence> InstancedSequenceVec;
227  typedef std::vector<DofAnimation> DofAnimations;
228  typedef std::vector<osg::Node*> NodeVec;
229  typedef std::vector<InstancedSwitch*> SwitchVec;
230  typedef std::vector<InstancedSequence*> SequenceVec;
231  typedef std::vector<InstancedLod*> LodVec;
233 
236  {
239 
242 
245 
248 
251 
254 
257 
259  osg::Vec3f boundingCenter;
260 
265  unsigned int nodeMask;
266 
268  osg::ref_ptr<osg::Geode> modelGeode;
269 
272  };
273 
276 
278  boost::signal<void (const std::string&)> signal_modelTypeRegistered;
279 
281  boost::signal<void (const std::string&, DtUniqueID, DtElementID, osg::Node *)> signal_instanceCreated;
282 
284  boost::signal<void (const CullSet&)> signal_updateFinished;
286 
289  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end,
290  const osg::Vec3d& reference, DtOsgIntersectorResult& result,
291  std::list<DtUniqueID>* ignoreList=0);
292 
294  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end,
295  std::vector<DtOsgIntersectorResult>& results,
296  std::list<DtUniqueID>* ignoreList=0);
297 
300  void setSequenceMode(const DtInstanceState& instance, unsigned int sequenceIndex, InstancedSequence::SequenceMode mode);
301 
303  void setSequenceSpeed(const DtInstanceState& instance, unsigned int sequenceIndex, float speed);
304 
306  void setSequencePlayTime(const DtInstanceState& instance, unsigned int sequenceIndex, float loopTime);
308 
311  InstanceMatrix dofState(const DtInstanceState& instanceState, unsigned int partIndex);
313 
316  void setDofState(InstanceMatrix& m, const DtInstanceState& state, unsigned int partIndex);
317 
319  void setDofAnimation(osg::Vec3f& hpr, const osg::Vec3f& hprVel, const DtInstanceState& state, unsigned int partIndex);
320 
322  void setDofTranslation(osg::Vec3f& trans, osg::Vec3f& transVel, const DtInstanceState& state, unsigned int partIndex);
324 
327  void setSwitchState(const DtInstanceState& instance, unsigned int partIndex, unsigned int childIndex, bool state);
329 
332  void setUVOffset(DtUniqueID id, unsigned int drawableIx, osg::Vec2f uvOffset);
334 
337  void setUserDefined(DtUniqueID id, float userDefined);
339 
341  void setElementId(DtUniqueID id, DtUniqueID elementId);
342 
344  static DtInstanceManager& instance(DtDe& de);
345 
347  DtInstanceManager(DtDe& de);
348 
350  virtual ~DtInstanceManager();
351 
355  virtual void reset();
356 
358  virtual osg::Node* instanceRoot();
359 
361  bool isRegistered(const std::string& modelType);
362 
364  void registerModel(const std::string& modelType, GeometryVec& drawables, LodVec& lods,
365  SwitchVec& switches, SequenceVec& sequences, PerDofDataVec& perDofData,
366  LightSourceVec& lightSources, float boundingRadius, const DtVector& boundingCenter,
367  unsigned int nodeMask, osg::ref_ptr<osg::StateSet> rootNodeSS, bool daylightControlledLights = false);
368 
370  osg::Geode *setupModelGeode(const std::string& modelType, unsigned int nodeMask);
371 
373  void allocateImage( const osg::View* view );
374 
376  void freeInstance(DtUniqueID id);
377 
382  bool newInstance(const std::string& modelType, DtUniqueID id, DtElementID elementId, osg::Node* rootNode);
383 
388  DtInstanceState* findInstanceState(DtUniqueID id);
389 
394  InstanceCullData* findInstanceCullData(DtUniqueID id);
395 
397  void setPosition(DtUniqueID uniqueID, const DtVector& vPosition);
398  void setOrientation(DtUniqueID uniqueID, const DtTaitBryan& orientation);
399 
401  void setPosition(DtUniqueID uniqueID, const osg::Vec3d& vPosition);
402  void setOrientation(DtUniqueID uniqueID, const osg::Quat& orientation);
403 
405  void setVisible(DtUniqueID uniqueID, bool bVisible);
406 
408  void setBlendColor(DtUniqueID uniqueID, float r, float g, float b, float a);
409 
414  void setModelScaling(DtUniqueID uniqueID, bool bModelScaling, float scale);
415 
417  void setCullParameters(DtUniqueID uniqueID, short modelSet, unsigned int nodeMask, bool visible);
418 
420  typedef std::vector<DtInstanceState> InstanceStateVec;
421  typedef std::vector<InstancedLod> InstancedLodVec;
422  typedef std::vector<InstancedDof> InstancedDofVec;
423  typedef std::vector<InstanceCullData> InstanceCullDataVec;
424  typedef std::vector<InstanceMatrix> InstanceMatrixVec;
425  typedef std::vector<float> InstanceScaleVec;
426  typedef std::vector<DtElementID> InstanceElementIdVec;
427  typedef std::vector<InstancedLight> InstancedLightVec;
429 
431  inline InstanceElementIdVec& elementIds() { return myElementIds; }
432 
434  inline InstanceScaleVec& instanceScales() { return myInstanceScales; }
435 
437  inline ByteVec& instanceVisibleSet() { return myInstanceVisibleSet; }
438 
440  inline InstanceMatrixVec& drawableInstanceMatrix() { return myDrawableInstanceMatrix; }
441 
443  inline InstanceCullDataVec& instanceCullData() { return myInstanceCullData; }
444 
446  inline InstanceStateVec& instances() { return myInstances; }
447 
449  inline TextureCoordinateVec& uvOffsets() { return myUVOffsets; }
450 
453  inline InstancedLodVec& instancedLods() { return myInstancedLods; }
454 
455  inline InstancedSwitchVec& instancedSwitches() { return myInstancedSwitches; }
456 
457  inline InstancedSequenceVec& instancedSequences() { return myInstancedSequences; }
458 
459  inline InstancedDofVec& instancedDofs() { return myInstancedDofs; }
461 
462  typedef std::vector<InstanceTemplate> InstanceTemplateVec;
463 
465  inline InstanceTemplateVec& instanceTemplates() { return myInstanceTemplates; }
466 
468  inline std::vector<float>& distanceToEye() { return myDistanceToEye; }
469 
471  inline DofAnimations& dofAnimations() { return myDofAnimations; }
472 
474  inline double& lastUpdateTime() { return myLastUpdateTime; }
475 
477  inline const DtOsgCullVisitorParameters* lastCullParameters() { return myLastCullParameters.get(); }
478 
480  inline unsigned int visibleSetStride() { return myVisibleSetStride; }
481 
483  inline BoolVec& visibleSet() { return myVisibleSet; }
484 
486  inline osg::Vec3d& cameraPosition() { return myCameraPosition; }
487 
489  inline const osg::View* lastView() { return myLastView; }
490 
492  inline const osg::Camera* lastCamera() {return myLastCamera;}
493 
495  inline unsigned int drawableIndex(unsigned int instanceIx);
496 
498  void reloadShaders();
499 
500  inline GeometryVec &drawables(){ return myDrawables; }
501 
503  virtual bool isDaylight();
504 
505  protected:
508 
511 
514 
519 
522 
525 
528 
531 
542 
545 
547  std::vector<float> myDistanceToEye;
548 
551 
554 
556  unsigned int myLastNodeMask;
557 
559  unsigned int myVisibleSetStride;
560 
562  osg::Vec3d myCameraPosition;
563 
565  const osg::View* myLastView;
566 
568  const osg::Camera* myLastCamera;
569 
572 
573  protected:
574 
575  friend class DtInstanceNode;
576 
578  struct Eyepoint
579  {
580  osg::Vec3d center;
581  osg::Vec3d eye;
582  osg::Vec3d up;
583  };
584 
586  struct Frustum
587  {
588  double l, r, b, t, n, f;
589  };
590 
593  {
594  unsigned int instanceIx;
595  unsigned int drawableIx;
596  osg::Vec3f pos;
597  osgUtil::LineSegmentIntersector::Intersection result;
598  };
599 
600  protected:
609  void update(double simTime, const osg::View* view, const osg::Camera* camera, const DtOsgCullVisitorParameters* params,
610  unsigned int nodeMask, bool suppressLastUpdateTime = false);
611 
613  void registerGeometry(unsigned int templateIx, osg::Geometry* geometry, VpMap& vpMap);
614 
616  float computeDistanceToEye(const Eyepoint& eye, const osg::Vec3d& pos) const;
617 
619  void updateDistancesToEye(const Eyepoint& eye);
620 
622  void _updateDistancesToEye(const Eyepoint& eye, unsigned int instanceOffset, unsigned int numInstances);
623 
625  void allocateRenderList();
626 
628  unsigned int updateBuffers( const osg::View* view, DtGLTextureBufferObject* tbo, const ArrayRange& drawableRange, unsigned int bufferOffset, osg::Vec4* bufferStorage );
629 
633  unsigned int updateRenderList( const Eyepoint& eye, const osg::Matrixd& viewMat, unsigned int nodeMask);
634 
635  void updateLights(const osg::View* view);
636  void resizeBuffersIfNecessary(const osg::View* view, unsigned int visibleCount);
637  void updateBuffers(const osg::View* view);
638 
642  void allocateInstanceData();
643 
647  unsigned int fillRenderList(unsigned visibleSetStride, unsigned int instanceOffset, unsigned int numInstances );
648 
650  void cullRange(const Eyepoint& eye
651  , const std::vector<osg::Vec4d>& planes, bool useScale, unsigned int nodeMask
652  , unsigned int instanceOffset, unsigned int instanceCount);
653 
655  void slot_channelAdded(DtChannelManager* mgr, DtChannel* channel);
656 
658  void slot_channelRemoved(DtChannelManager* mgr, DtChannel* channel);
659 
661  void createHitList(const osg::Vec3d& end, const osg::Vec3d& start, std::vector<InstancingHit>& hitList);
662 
664  void loadInstanceShaders(osg::ref_ptr<osgEarth::VirtualProgram> vp);
665 
666  bool isOnExcludeList( DtUniqueID elementId,
667  const std::list<DtUniqueID>& ignoreList);
668 
669  protected:
670 
673  unsigned int myDynamicBufferDataSz;
674 
676  struct RenderState
677  {
679  float scale;
680  float userDefined;
681  osg::Vec2f uvOffset;
682  osg::Vec3f filterColor;
683  };
684 
686  typedef std::vector<RenderState> RenderStateVec;
687 
690  {
694 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
696  unsigned int getcount() const
697  {
698  return count.get();
699  }
700 #else
701  unsigned count;
702  unsigned getcount()
703  {
704  return count;
705  }
706 #endif
707 
709  };
710 
712  typedef std::vector<GeometryRenderList> RenderList;
713 
715 
718 
720  typedef boost::unordered_map<DtUniqueID, unsigned int> InstanceStateMap;
721 
722 
725 
728  {
730  const osg::View* view;
731 
734  };
735 
737  typedef std::vector<InstanceBuffer> InstanceBufferVec;
738 
740 
742  typedef boost::unordered_map<std::string, int> modelTypeToTemplateMap;
743 
746 
748 
751 
753  unsigned int myRequiredUvOffsets;
754 
758  unsigned int myNumThreads;
760 
764  typedef boost::unordered_map<unsigned int, unsigned int> PerTemplateCount;
765 
768 
772 
778 
782  class DT_DLL_VRVOSG AlwaysCull : public osg::Drawable::CullCallback
783  {
784  virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const
785  {
786  return true;
787  }
788  };
789 
790  osg::ref_ptr<AlwaysCull> myAlwaysCullCallback;
791 
792  osg::ref_ptr<DtInstanceNode> myInstanceRoot;
793 
797  typedef std::vector<DtInstanceProcessor*> InstanceProcessorVec;
798 
801 
804 
808  typedef std::vector<osg::View*> ViewVec;
810 
812  typedef boost::unordered_map<const osg::View*, osg::ref_ptr<DtViewSpecificGroup> > ViewToGroup;
814 
816  typedef boost::unordered_map<osg::Geometry*, osg::ref_ptr<DtPerViewUniformCallback> > GeometryToCallback;
818 
820  osg::ref_ptr<const DtOsgCullVisitorParameters> myLastCullParameters;
821 
822  void addInstanceRootToSceneGraphIfNecessary(void);
823 
824  void processNewInstanceLights(const InstanceTemplate& instanceTemplate);
825  void processNewInstanceDOFs(const InstanceTemplate& instanceTemplate);
826  void processNewInstanceSwitches(const InstanceTemplate& instanceTemplate);
827  void processNewInstanceSequences(const InstanceTemplate& instanceTemplate);
828 
829  void preCull(unsigned int maxChunkSize);
830  void frustumCull(const osg::View* view, const Eyepoint& eye, unsigned int nodeMask);
831  void postCull(unsigned int maxChunkSize);
832 
833  std::vector<osg::Vec4d> vecScratchPadPlanes;
834 
835  void _fillRenderList(bool globalScaling, unsigned int instanceOffset, unsigned int numInstances);
836 
837 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
839 #else
840  unsigned int myVisibleCount;
841 #endif
842  };
843 
849  {
850  public:
852  virtual ~DtInstanceManagerCreator();
853 
855  static DtInstanceManagerCreator& instance(DtDe& de);
856 
858  virtual DtInstanceManager* create();
859 
860  protected:
863 
864  protected:
866  };
867 }

Document ID: Generated on Thu Mar 23 18:54:12 EDT 2017 from SVN revision 174804
Copyright © 2005-2017 VT MÄK. All Rights Reserved (www.mak.com)