VR-Forces 4.7 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtInstanceManager.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2018 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 #include <osgViewer/Renderer>
22 
23 #include <osg/ref_ptr>
24 #include <osg/LightSource>
25 
26 #include <boost/unordered_map.hpp>
27 
28 #define DT_INSTANCE_MANAGER_CULL_IS_PARALLEL 1
29 #define DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL 1
30 
31 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
33 #endif
34 
35 #define DT_INSTANCE_MANAGER_UPDATEDISTANCESTOEYE_IS_PARALLEL 1
36 
37 class DtTaitBryan;
38 
39 namespace osg
40 {
41  class Geometry;
42  class Image;
43  class Texture2D;
44  class Vec2f;
45  class Vec3f;
46  class Geode;
47  class View;
48 }
49 
50 namespace osgEarth
51 {
52  class VirtualProgram;
53 }
54 
55 namespace makVrv
56 {
57  class DtGlTextureBufferObject;
58  class DtInstanceProcessor;
59  class DtInstanceLpProcessor;
60  class DtOsgIntersectorResult;
61  class DtChannelManager;
62  class DtChannel;
63  class DtPerViewUniformCallback;
64  class DtInstanceNode;
65  class DtLight;
66  class DtOsgCullVisitorParameters;
67  class DtViewSpecificGroup;
68 
74  {
75  public:
76  typedef std::vector<osg::ref_ptr<osg::Geometry> > GeometryVec;
77  typedef std::vector<char> BoolVec; // Using vec of chars for threadsafety
78  typedef std::vector<char> ByteVec;
79  typedef std::map<osg::ref_ptr< osgEarth::VirtualProgram >, osg::ref_ptr<osgEarth::VirtualProgram> > VpMap;
80  typedef std::vector<osg::ref_ptr<osgEarth::VirtualProgram> > VpRefList;
81  typedef std::vector<ArrayRange> DrawableRangeVec;
82  typedef std::vector<osg::Vec2f> TextureCoordinateVec;
83  typedef std::vector<float> FloatVec;
84 
86  static const unsigned int NumVec4PerInstance = 8;
87 
89  struct InstancedLod
90  {
92  double minRange;
93 
95  double maxRange;
96 
101  };
102 
107  {
110 
113 
116  };
117 
120  {
122  enum SequenceMode { Play, Stop, Pause, Resume };
123 
126 
129 
131  float startTime;
132 
134  float endTime;
135 
138 
140  float speed;
141 
144  };
145 
148  {
150  osg::Vec3f position;
151  osg::Vec3f direction;
152  unsigned int drawableIx; // index to drawable the light is attached to
153  };
154 
157  {
160 
164  unsigned int numChildren;
165 
168 
173  };
174 
177  {
180 
183 
186 
188  osg::Vec3f initialHpr;
189 
191  osg::Vec3f initialTranslate;
192 
196  unsigned int numChildren;
197 
200  };
201 
204  {
206  osg::Vec3f trans;
207 
208  osg::Vec3f transVel;
209 
211  osg::Vec3f hpr;
212 
214  osg::Vec3f hprVel;
215 
216 
218  float lastUpdate;
219  };
220 
221  struct LightSource
222  {
223  osg::ref_ptr<osg::LightSource> lightSource;
224  unsigned int attachDrawableIx;
225  float range;
227  };
228 
230  struct LightPoint
231  {
232  osg::ref_ptr<osg::Node> lightPoint;
233  unsigned int attachDrawableIx;
234  int groupId;
235  };
236 
238  typedef std::vector<LightSource> LightSourceVec;
239  typedef std::vector<ModelTypeDofData> PerDofDataVec;
240  typedef std::vector<InstancedSwitch> InstancedSwitchVec;
241  typedef std::vector<InstancedSequence> InstancedSequenceVec;
242  typedef std::vector<DofAnimation> DofAnimations;
243  typedef std::vector<osg::Node*> NodeVec;
244  typedef std::vector<InstancedSwitch*> SwitchVec;
245  typedef std::vector<InstancedSequence*> SequenceVec;
246  typedef std::vector<InstancedLod*> LodVec;
247  typedef std::vector<LightPoint> LightPointVec;
249 
252  {
255 
258 
261 
264 
267 
270 
273 
276 
278  osg::Vec3f boundingCenter;
279 
284  unsigned int nodeMask;
285 
287  osg::ref_ptr<osg::Geode> modelGeode;
288 
291  };
292 
295 
297  boost::signal<void(const std::string&)> signal_modelTypeRegistered;
298 
300  boost::signal<void(const std::string&, DtUniqueID, DtElementID, osg::Node *)> signal_instanceCreated;
301 
303  boost::signal<void(const CullSet&)> signal_updateFinished;
305 
308  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end,
309  const osg::Vec3d& reference, DtOsgIntersectorResult& result,
310  std::list<DtUniqueID>* ignoreList = 0);
311 
313  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end,
314  std::vector<DtOsgIntersectorResult>& results,
315  std::list<DtUniqueID>* ignoreList = 0);
316 
319  void setSequenceMode(const DtInstanceState& instance, unsigned int sequenceIndex, InstancedSequence::SequenceMode mode);
320 
322  void setSequenceSpeed(const DtInstanceState& instance, unsigned int sequenceIndex, float speed);
323 
325  void setSequencePlayTime(const DtInstanceState& instance, unsigned int sequenceIndex, float loopTime);
327 
330  InstanceMatrix dofState(const DtInstanceState& instanceState, unsigned int partIndex);
332 
335  void setDofState(InstanceMatrix& m, const DtInstanceState& state, unsigned int partIndex);
336 
338  void setDofAnimation(osg::Vec3f& hpr, const osg::Vec3f& hprVel, const DtInstanceState& state, unsigned int partIndex);
339 
341  void setDofTranslation(osg::Vec3f& trans, osg::Vec3f& transVel, const DtInstanceState& state, unsigned int partIndex);
343 
346  void setSwitchState(const DtInstanceState& instance, unsigned int partIndex, unsigned int childIndex, bool state);
348 
351  void setUVOffset(DtUniqueID id, unsigned int drawableIx, osg::Vec2f uvOffset);
353 
356  void setUserDefined(DtUniqueID id, float userDefined);
358 
361  void setLightpointRange(DtUniqueID id, float range);
363 
365  void setElementId(DtUniqueID id, DtUniqueID elementId);
366 
368  osg::StateSet* getModelTypeRootStateSet(const std::string &modelType);
369 
371  static DtInstanceManager& instance(DtDe& de);
372 
374  DtInstanceManager(DtDe& de);
375 
377  virtual ~DtInstanceManager();
378 
382  virtual void reset();
383 
385  virtual osg::Node* instanceRoot();
386 
388  bool isRegistered(const std::string& modelType);
389 
391  void registerModel(const std::string& modelType, GeometryVec& drawables, LodVec& lods,
392  SwitchVec& switches, SequenceVec& sequences, PerDofDataVec& perDofData,
393  LightSourceVec& lightSources, LightPointVec &lightPointNodes, float boundingRadius, const DtVector& boundingCenter,
394  unsigned int nodeMask, osg::ref_ptr<osg::StateSet> rootNodeSS, bool daylightControlledLights = false);
395 
397  osg::Geode *setupModelGeode(const std::string& modelType, unsigned int nodeMask);
398 
400  void allocateImage(const osg::View* view);
401 
403  void freeInstance(DtUniqueID id);
404 
409  bool newInstance(const std::string& modelType, DtUniqueID id, DtElementID elementId, osg::Node* rootNode, const std::string &lightPointGroup, bool isSimpleModel);
410 
415  DtInstanceState* findInstanceState(DtUniqueID id);
416 
421  InstanceCullData* findInstanceCullData(DtUniqueID id);
422 
424  void setPosition(DtUniqueID uniqueID, const DtVector& vPosition);
425  void setOrientation(DtUniqueID uniqueID, const DtTaitBryan& orientation);
426 
428  void setPosition(DtUniqueID uniqueID, const osg::Vec3d& vPosition);
429  void setOrientation(DtUniqueID uniqueID, const osg::Quat& orientation);
430 
432  void setVisible(DtUniqueID uniqueID, bool bVisible);
433 
435  void setBlendColor(DtUniqueID uniqueID, float r, float g, float b, float a);
436 
441  void setModelScaling(DtUniqueID uniqueID, bool bModelScaling, float scale);
442 
444  void setCullParameters(DtUniqueID uniqueID, short modelSet, unsigned int nodeMask, bool visible);
445 
447  typedef std::vector<DtInstanceState> InstanceStateVec;
448  typedef std::vector<InstancedLod> InstancedLodVec;
449  typedef std::vector<InstancedDof> InstancedDofVec;
450  typedef std::vector<InstanceCullData> InstanceCullDataVec;
451  typedef std::vector<InstanceMatrix> InstanceMatrixVec;
452  typedef std::vector<float> InstanceScaleVec;
453  typedef std::vector<DtElementID> InstanceElementIdVec;
454  typedef std::vector<InstancedLight> InstancedLightVec;
456 
458  inline InstanceElementIdVec& elementIds() { return myElementIds; }
459 
461  inline InstanceScaleVec& instanceScales() { return myInstanceScales; }
462 
464  inline ByteVec& instanceVisibleSet() { return myInstanceVisibleSet; }
465 
467  inline InstanceMatrixVec& drawableInstanceMatrix() { return myDrawableInstanceMatrix; }
468 
470  inline InstanceCullDataVec& instanceCullData() { return myInstanceCullData; }
471 
473  inline InstanceStateVec& instances() { return myInstances; }
474 
476  inline TextureCoordinateVec& uvOffsets() { return myUVOffsets; }
477 
480  inline InstancedLodVec& instancedLods() { return myInstancedLods; }
481 
482  inline InstancedSwitchVec& instancedSwitches() { return myInstancedSwitches; }
483 
484  inline InstancedSequenceVec& instancedSequences() { return myInstancedSequences; }
485 
486  inline InstancedDofVec& instancedDofs() { return myInstancedDofs; }
488 
489  typedef std::vector<InstanceTemplate> InstanceTemplateVec;
490 
492  inline InstanceTemplateVec& instanceTemplates() { return myInstanceTemplates; }
493 
495  inline std::vector<float>& distanceToEye() { return myDistanceToEye; }
496 
498  inline DofAnimations& dofAnimations() { return myDofAnimations; }
499 
501  inline double& lastUpdateTime() { return myLastUpdateTime; }
502 
504  inline const DtOsgCullVisitorParameters* lastCullParameters() { return myLastCullParameters.get(); }
505 
507  inline unsigned int visibleSetStride() { return myVisibleSetStride; }
508 
510  inline BoolVec& visibleSet() { return myVisibleSet; }
511 
513  inline osg::Vec3d& cameraPosition() { return myCameraPosition; }
514 
516  inline const osg::View* lastView() { return myLastView; }
517 
519  inline const osg::Camera* lastCamera() { return myLastCamera; }
520 
522  inline unsigned int drawableIndex(unsigned int instanceIx);
523 
525  void reloadShaders();
526 
527  inline GeometryVec &drawables(){ return myDrawables; }
528 
529 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
530  inline unsigned int visibleCount() const { return myVisibleCount.get(); }
531 
532 #else
533  inline unsigned int visibleCount() const { return myVisibleCount; }
534 #endif
535 
536  inline unsigned int dynamicBufferDataSize() const { return myDynamicBufferDataSz; }
537 
539  virtual bool isDaylight();
540 
542  virtual void traverse(osg::NodeVisitor& nv);
543 
548  virtual void updateForIntersection(double simTime, const osg::View *view, osg::Camera *camera, unsigned int nodeMask, DtOsgCullVisitorParameters *params);
549 
552  virtual void updateForDrawing(osgUtil::CullVisitor *cv, const DtOsgCullVisitorParameters* params);
553 
554  virtual float lightpointRange(unsigned int instanceIx);
555 
559  virtual void setDefaultLightPointMaxDistance( float distance );
560 
562  virtual float defaultLightPointMaxDistance() const;
563 
568  virtual void setLightPointFadeBias( float bias );
569 
571  virtual bool cullDistanceEnabled();
572 
575  virtual float cullDistance();
576 
580  virtual std::string modelDefinitionNameProxy(const std::string& modelDefName);
581 
584  DtVirtualBaseClass* findCachedData(const std::string& modelType, const std::string& name);
585 
590  void setCachedData(const std::string& modelType, const std::string& name, DtVirtualBaseClass* data);
591 
593  void clearCachedData();
594 
595  void releaseGLObjects(void);
596 
597  protected:
600 
603 
606 
611 
614 
617 
620 
623 
626 
638 
641 
643  std::vector<float> myDistanceToEye;
644 
647 
650 
652  unsigned int myLastNodeMask;
653 
655  unsigned int myVisibleSetStride;
656 
658  osg::Vec3d myCameraPosition;
659 
661  const osg::View* myLastView;
662 
664  const osg::Camera* myLastCamera;
665 
668 
670  typedef boost::unordered_map<std::string, DtVirtualBaseClass*> CacheMap;
672 
673  protected:
674 
675  friend class DtInstanceNode;
676 
678  struct Eyepoint
679  {
680  osg::Vec3d center;
681  osg::Vec3d eye;
682  osg::Vec3d up;
683  };
684 
686  struct Frustum
687  {
688  double l, r, b, t, n, f;
689  };
690 
693  {
694  unsigned int instanceIx;
695  unsigned int drawableIx;
696  osg::Vec3f pos;
697  osgUtil::LineSegmentIntersector::Intersection result;
698  };
699 
700  protected:
701  friend class DtInstanceManagerTBOUpdater;
702 
711  void update(double simTime, const osg::View *view, osg::Camera *camera,
712  unsigned int nodeMask, osgUtil::CullVisitor *cv,
713  const DtOsgCullVisitorParameters* params, bool suppressLastUpdateTime);
714 
716  void registerGeometry(unsigned int templateIx, osg::Geometry* geometry, VpMap& vpMap);
717 
719  float computeDistanceToEye(const Eyepoint& eye, const osg::Vec3d& pos) const;
720 
722  void updateDistancesToEye(const Eyepoint& eye);
723 
725  void _updateDistancesToEye(const Eyepoint& eye, unsigned int instanceOffset, unsigned int numInstances);
726 
728  void allocateRenderList();
729 
731  unsigned int updateDataAndDrawables(const osg::View* view, const ArrayRange& drawableRange,
732  unsigned int bufferOffset, osg::Vec4* bufferStorage);
733 
737  unsigned int updateRenderList(const Eyepoint& eye, const osg::Matrixd& viewMat, unsigned int nodeMask);
738 
739  void updateLights(const osg::View* view);
740  void resizeBuffersIfNecessary(const osg::View* view, int visibleCount);
741  void resizeTBOIfNecessary(const osg::View* view, int visibleCount);
742  void updateBuffers(const osg::View* view);
743 
745  void updateLightPoints(const osg::Camera *camera, const osg::FrameStamp *frameStamp, osgUtil::CullVisitor *cv, const osg::Camera *sceneCamera = NULL);
746 
750  void allocateInstanceData();
751 
755  unsigned int fillRenderList(unsigned visibleSetStride, unsigned int instanceOffset, unsigned int numInstances);
756 
758  void cullRange(const Eyepoint& eye
759  , const std::vector<osg::Vec4d>& planes, bool useScale, unsigned int nodeMask
760  , unsigned int instanceOffset, unsigned int instanceCount);
761 
763  void slot_channelAdded(DtChannelManager* mgr, DtChannel* channel);
764 
766  void slot_channelRemoved(DtChannelManager* mgr, DtChannel* channel);
767 
769  void createHitList(const osg::Vec3d& end, const osg::Vec3d& start, std::vector<InstancingHit>& hitList);
770 
772  void loadInstanceShaders(osg::ref_ptr<osgEarth::VirtualProgram> vp);
773 
774  bool isOnExcludeList(DtUniqueID elementId,
775  const std::list<DtUniqueID>& ignoreList);
776 
777  protected:
778 
781  unsigned int myDynamicBufferDataSz;
783 
785  struct RenderState
786  {
788  float scale;
789  float userDefined;
790  osg::Vec2f uvOffset;
791  osg::Vec3f filterColor;
793  };
794 
796  typedef std::vector<RenderState> RenderStateVec;
797 
800  {
804 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
806  unsigned int getcount() const
807  {
808  return count.get();
809  }
810 #else
811  unsigned count;
812  unsigned getcount()
813  {
814  return count;
815  }
816 #endif
817 
819  };
820 
822  typedef std::vector<GeometryRenderList> RenderList;
823 
825 
828 
830  typedef boost::unordered_map<DtUniqueID, unsigned int> InstanceStateMap;
831 
832 
835 
838  {
840  const osg::View* view;
841 
844  };
845 
847  typedef std::vector<InstanceBuffer> InstanceBufferVec;
848 
850 
852  typedef boost::unordered_map<std::string, int> modelTypeToTemplateMap;
853 
856 
858 
861 
863  unsigned int myRequiredUvOffsets;
864 
868  unsigned int myNumThreads;
870 
874  typedef boost::unordered_map<unsigned int, unsigned int> PerTemplateCount;
875 
878 
882 
888 
892  class DT_DLL_VRVOSG AlwaysCull : public osg::Drawable::CullCallback
893  {
894  virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const
895  {
896  return true;
897  }
898  };
899 
900  osg::ref_ptr<AlwaysCull> myAlwaysCullCallback;
901 
902  osg::ref_ptr<DtInstanceNode> myInstanceRoot;
903 
907  typedef std::vector<DtInstanceProcessor*> InstanceProcessorVec;
908 
911 
914 
918  typedef std::vector<osg::View*> ViewVec;
920 
922  typedef boost::unordered_map<const osg::View*, osg::ref_ptr<DtViewSpecificGroup> > ViewToGroup;
924 
926  typedef boost::unordered_map<osg::Geometry*, osg::ref_ptr<DtPerViewUniformCallback> > GeometryToCallback;
928 
930  osg::ref_ptr<const DtOsgCullVisitorParameters> myLastCullParameters;
934 
935  void addInstanceRootToSceneGraphIfNecessary(void);
936 
937  void processNewInstanceLights(const InstanceTemplate& instanceTemplate);
938  void processNewInstanceDOFs(const InstanceTemplate& instanceTemplate);
939  void processNewInstanceSwitches(const InstanceTemplate& instanceTemplate, bool isSimpleModel);
940  void processNewInstanceSequences(const InstanceTemplate& instanceTemplate);
941 
943  void processNewInstanceLightPoints(const InstanceTemplate& instanceTemplate, const std::string &lightPointGroup);
944 
945  void preCull(unsigned int maxChunkSize);
946  void frustumCull(const osg::View* view, const Eyepoint& eye, unsigned int nodeMask);
947  void postCull(unsigned int maxChunkSize);
948 
949  std::vector<osg::Vec4d> vecScratchPadPlanes;
950 
951  void _fillRenderList(bool globalScaling, unsigned int instanceOffset, unsigned int numInstances);
952 
953 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
955 #else
956  unsigned int myVisibleCount;
957 #endif
958  class DtInstanceManagerTBOUpdater * myDrawCallUpdater;
960 
961  std::string myInstanceVS, myInstanceViewVS, myInstanceFS;
962 
963  };
964 
970  {
971  public:
973  virtual ~DtInstanceManagerCreator();
974 
976  static DtInstanceManagerCreator& instance(DtDe& de);
977 
979  virtual DtInstanceManager* create();
980 
981  protected:
984 
985  protected:
987  };
988 }

Document ID: Generated on Fri Apr 26 21:53:14 EDT 2019 from SVN revision 197883
Copyright © 2005-2019 VT MAK. All Rights Reserved (www.mak.com)