VR-Forces Development_Version 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, 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 
411  void setModelScaling(DtUniqueID uniqueID, bool bModelScaling);
412 
414  void setCullParameters(DtUniqueID uniqueID, short modelSet, unsigned int nodeMask, bool visible);
415 
417  typedef std::vector<DtInstanceState> InstanceStateVec;
418  typedef std::vector<InstancedLod> InstancedLodVec;
419  typedef std::vector<InstancedDof> InstancedDofVec;
420  typedef std::vector<InstanceCullData> InstanceCullDataVec;
421  typedef std::vector<InstanceMatrix> InstanceMatrixVec;
422  typedef std::vector<float> InstanceScaleVec;
423  typedef std::vector<DtElementID> InstanceElementIdVec;
424  typedef std::vector<InstancedLight> InstancedLightVec;
426 
428  inline InstanceElementIdVec& elementIds() { return myElementIds; }
429 
431  inline InstanceScaleVec& instanceScales() { return myInstanceScales; }
432 
434  inline ByteVec& instanceVisibleSet() { return myInstanceVisibleSet; }
435 
437  inline InstanceMatrixVec& drawableInstanceMatrix() { return myDrawableInstanceMatrix; }
438 
440  inline InstanceCullDataVec& instanceCullData() { return myInstanceCullData; }
441 
443  inline InstanceStateVec& instances() { return myInstances; }
444 
446  inline TextureCoordinateVec& uvOffsets() { return myUVOffsets; }
447 
450  inline InstancedLodVec& instancedLods() { return myInstancedLods; }
451 
452  inline InstancedSwitchVec& instancedSwitches() { return myInstancedSwitches; }
453 
454  inline InstancedSequenceVec& instancedSequences() { return myInstancedSequences; }
455 
456  inline InstancedDofVec& instancedDofs() { return myInstancedDofs; }
458 
459  typedef std::vector<InstanceTemplate> InstanceTemplateVec;
460 
462  inline InstanceTemplateVec& instanceTemplates() { return myInstanceTemplates; }
463 
465  inline std::vector<float>& distanceToEye() { return myDistanceToEye; }
466 
468  inline DofAnimations& dofAnimations() { return myDofAnimations; }
469 
471  inline double& lastUpdateTime() { return myLastUpdateTime; }
472 
474  inline const DtOsgCullVisitorParameters* lastCullParameters() { return myLastCullParameters.get(); }
475 
477  inline unsigned int visibleSetStride() { return myVisibleSetStride; }
478 
480  inline BoolVec& visibleSet() { return myVisibleSet; }
481 
483  inline osg::Vec3d& cameraPosition() { return myCameraPosition; }
484 
486  inline const osg::View* lastView() { return myLastView; }
487 
489  inline unsigned int drawableIndex(unsigned int instanceIx);
490 
492  void reloadShaders();
493 
494  inline GeometryVec &drawables(){ return myDrawables; }
495 
497  virtual bool isDaylight();
498 
499  protected:
502 
505 
508 
513 
516 
519 
522 
525 
536 
539 
541  std::vector<float> myDistanceToEye;
542 
545 
548 
550  unsigned int myLastNodeMask;
551 
553  unsigned int myVisibleSetStride;
554 
556  osg::Vec3d myCameraPosition;
557 
559  const osg::View* myLastView;
560 
563 
564  protected:
565 
566  friend class DtInstanceNode;
567 
569  struct Eyepoint
570  {
571  osg::Vec3d center;
572  osg::Vec3d eye;
573  osg::Vec3d up;
574  };
575 
577  struct Frustum
578  {
579  double l, r, b, t, n, f;
580  };
581 
584  {
585  unsigned int instanceIx;
586  unsigned int drawableIx;
587  osg::Vec3f pos;
588  osgUtil::LineSegmentIntersector::Intersection result;
589  };
590 
591  protected:
600  void update(double simTime, const osg::View* view, const DtOsgCullVisitorParameters* params,
601  unsigned int nodeMask, bool suppressLastUpdateTime = false);
602 
604  void registerGeometry(unsigned int templateIx, osg::Geometry* geometry, VpMap& vpMap);
605 
607  float computeDistanceToEye(const Eyepoint& eye, const osg::Vec3d& pos) const;
608 
610  void updateDistancesToEye(const Eyepoint& eye);
611 
613  void _updateDistancesToEye(const Eyepoint& eye, unsigned int instanceOffset, unsigned int numInstances);
614 
616  void allocateRenderList();
617 
619  unsigned int updateBuffers( const osg::View* view, DtGLTextureBufferObject* tbo, const ArrayRange& drawableRange, unsigned int bufferOffset, osg::Vec4* bufferStorage );
620 
624  unsigned int updateRenderList( const Eyepoint& eye, const osg::Matrixd& viewMat, unsigned int nodeMask);
625 
626  void updateLights(const osg::View* view);
627  void resizeBuffersIfNecessary(const osg::View* view, unsigned int visibleCount);
628  void updateBuffers(const osg::View* view);
629 
633  void allocateInstanceData();
634 
638  unsigned int fillRenderList(unsigned visibleSetStride, unsigned int instanceOffset, unsigned int numInstances );
639 
641  void cullRange(const Eyepoint& eye
642  , const std::vector<osg::Vec4d>& planes, bool useScale, unsigned int nodeMask
643  , unsigned int instanceOffset, unsigned int instanceCount);
644 
646  void slot_channelAdded(DtChannelManager* mgr, DtChannel* channel);
647 
649  void slot_channelRemoved(DtChannelManager* mgr, DtChannel* channel);
650 
652  void createHitList(const osg::Vec3d& end, const osg::Vec3d& start, std::vector<InstancingHit>& hitList);
653 
655  void loadInstanceShaders(osg::ref_ptr<osgEarth::VirtualProgram> vp);
656 
657  bool isOnExcludeList( DtUniqueID elementId,
658  const std::list<DtUniqueID>& ignoreList);
659 
660  protected:
661 
664  unsigned int myDynamicBufferDataSz;
665 
667  struct RenderState
668  {
670  float scale;
671  float userDefined;
672  osg::Vec2f uvOffset;
673  osg::Vec3f filterColor;
674  };
675 
677  typedef std::vector<RenderState> RenderStateVec;
678 
681  {
685 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
687  unsigned int getcount() const
688  {
689  return count.get();
690  }
691 #else
692  unsigned count;
693  unsigned getcount()
694  {
695  return count;
696  }
697 #endif
698 
700  };
701 
703  typedef std::vector<GeometryRenderList> RenderList;
704 
706 
709 
711  typedef boost::unordered_map<DtUniqueID, unsigned int> InstanceStateMap;
712 
713 
716 
719  {
721  const osg::View* view;
722 
725  };
726 
728  typedef std::vector<InstanceBuffer> InstanceBufferVec;
729 
731 
733  typedef boost::unordered_map<std::string, int> modelTypeToTemplateMap;
734 
737 
739 
742 
744  unsigned int myRequiredUvOffsets;
745 
749  unsigned int myNumThreads;
751 
755  typedef boost::unordered_map<unsigned int, unsigned int> PerTemplateCount;
756 
759 
763 
769 
773  class DT_DLL_VRVOSG AlwaysCull : public osg::Drawable::CullCallback
774  {
775  virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const
776  {
777  return true;
778  }
779  };
780 
781  osg::ref_ptr<AlwaysCull> myAlwaysCullCallback;
782 
783  osg::ref_ptr<DtInstanceNode> myInstanceRoot;
784 
788  typedef std::vector<DtInstanceProcessor*> InstanceProcessorVec;
789 
792 
795 
799  typedef std::vector<osg::View*> ViewVec;
801 
803  typedef boost::unordered_map<const osg::View*, osg::ref_ptr<DtViewSpecificGroup> > ViewToGroup;
805 
807  typedef boost::unordered_map<osg::Geometry*, osg::ref_ptr<DtPerViewUniformCallback> > GeometryToCallback;
809 
811  osg::ref_ptr<const DtOsgCullVisitorParameters> myLastCullParameters;
812 
813  void addInstanceRootToSceneGraphIfNecessary(void);
814 
815  void processNewInstanceLights(const InstanceTemplate& instanceTemplate);
816  void processNewInstanceDOFs(const InstanceTemplate& instanceTemplate);
817  void processNewInstanceSwitches(const InstanceTemplate& instanceTemplate);
818  void processNewInstanceSequences(const InstanceTemplate& instanceTemplate);
819 
820  void preCull(unsigned int maxChunkSize);
821  void frustumCull(const osg::View* view, const Eyepoint& eye, unsigned int nodeMask);
822  void postCull(unsigned int maxChunkSize);
823 
824  std::vector<osg::Vec4d> vecScratchPadPlanes;
825 
826  void _fillRenderList(bool globalScaling, unsigned int instanceOffset, unsigned int numInstances);
827 
828 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
830 #else
831  unsigned int myVisibleCount;
832 #endif
833  };
834 
840  {
841  public:
843  virtual ~DtInstanceManagerCreator();
844 
846  static DtInstanceManagerCreator& instance(DtDe& de);
847 
849  virtual DtInstanceManager* create();
850 
851  protected:
854 
855  protected:
857  };
858 }

Document ID: Generated on Mon Jul 4 01:00:18 EDT 2016 from SVN revision 166489
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)