VR-Forces 4.6 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 
564 
569  virtual void setLightPointFadeBias( float bias );
570 
574  virtual std::string modelDefinitionNameProxy(const std::string& modelDefName);
575 
578  DtVirtualBaseClass* findCachedData(const std::string& modelType, const std::string& name);
579 
584  void setCachedData(const std::string& modelType, const std::string& name, DtVirtualBaseClass* data);
585 
587  void clearCachedData();
588 
589  void releaseGLObjects(void);
590 
591  protected:
594 
597 
600 
605 
608 
611 
614 
617 
620 
632 
635 
637  std::vector<float> myDistanceToEye;
638 
641 
644 
646  unsigned int myLastNodeMask;
647 
649  unsigned int myVisibleSetStride;
650 
652  osg::Vec3d myCameraPosition;
653 
655  const osg::View* myLastView;
656 
658  const osg::Camera* myLastCamera;
659 
662 
664  typedef boost::unordered_map<std::string, DtVirtualBaseClass*> CacheMap;
666 
667  protected:
668 
669  friend class DtInstanceNode;
670 
672  struct Eyepoint
673  {
674  osg::Vec3d center;
675  osg::Vec3d eye;
676  osg::Vec3d up;
677  };
678 
680  struct Frustum
681  {
682  double l, r, b, t, n, f;
683  };
684 
687  {
688  unsigned int instanceIx;
689  unsigned int drawableIx;
690  osg::Vec3f pos;
691  osgUtil::LineSegmentIntersector::Intersection result;
692  };
693 
694  protected:
695  friend class DtInstanceManagerTBOUpdater;
696 
705  void update(double simTime, const osg::View *view, osg::Camera *camera,
706  unsigned int nodeMask, osgUtil::CullVisitor *cv,
707  const DtOsgCullVisitorParameters* params, bool suppressLastUpdateTime);
708 
710  void registerGeometry(unsigned int templateIx, osg::Geometry* geometry, VpMap& vpMap);
711 
713  float computeDistanceToEye(const Eyepoint& eye, const osg::Vec3d& pos) const;
714 
716  void updateDistancesToEye(const Eyepoint& eye);
717 
719  void _updateDistancesToEye(const Eyepoint& eye, unsigned int instanceOffset, unsigned int numInstances);
720 
722  void allocateRenderList();
723 
725  unsigned int updateDataAndDrawables(const osg::View* view, const ArrayRange& drawableRange,
726  unsigned int bufferOffset, osg::Vec4* bufferStorage);
727 
731  unsigned int updateRenderList(const Eyepoint& eye, const osg::Matrixd& viewMat, unsigned int nodeMask);
732 
733  void updateLights(const osg::View* view);
734  void resizeBuffersIfNecessary(const osg::View* view, int visibleCount);
735  void resizeTBOIfNecessary(const osg::View* view, int visibleCount);
736  void updateBuffers(const osg::View* view);
737 
739  void updateLightPoints(const osg::Camera *camera, const osg::FrameStamp *frameStamp, osgUtil::CullVisitor *cv, const osg::Camera *sceneCamera = NULL);
740 
744  void allocateInstanceData();
745 
749  unsigned int fillRenderList(unsigned visibleSetStride, unsigned int instanceOffset, unsigned int numInstances);
750 
752  void cullRange(const Eyepoint& eye
753  , const std::vector<osg::Vec4d>& planes, bool useScale, unsigned int nodeMask
754  , unsigned int instanceOffset, unsigned int instanceCount);
755 
757  void slot_channelAdded(DtChannelManager* mgr, DtChannel* channel);
758 
760  void slot_channelRemoved(DtChannelManager* mgr, DtChannel* channel);
761 
763  void createHitList(const osg::Vec3d& end, const osg::Vec3d& start, std::vector<InstancingHit>& hitList);
764 
766  void loadInstanceShaders(osg::ref_ptr<osgEarth::VirtualProgram> vp);
767 
768  bool isOnExcludeList(DtUniqueID elementId,
769  const std::list<DtUniqueID>& ignoreList);
770 
771 
772  protected:
773 
776  unsigned int myDynamicBufferDataSz;
778 
780  struct RenderState
781  {
783  float scale;
784  float userDefined;
785  osg::Vec2f uvOffset;
786  osg::Vec3f filterColor;
788  };
789 
791  typedef std::vector<RenderState> RenderStateVec;
792 
795  {
799 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
801  unsigned int getcount() const
802  {
803  return count.get();
804  }
805 #else
806  unsigned count;
807  unsigned getcount()
808  {
809  return count;
810  }
811 #endif
812 
814  };
815 
817  typedef std::vector<GeometryRenderList> RenderList;
818 
820 
823 
825  typedef boost::unordered_map<DtUniqueID, unsigned int> InstanceStateMap;
826 
827 
830 
833  {
835  const osg::View* view;
836 
839  };
840 
842  typedef std::vector<InstanceBuffer> InstanceBufferVec;
843 
845 
847  typedef boost::unordered_map<std::string, int> modelTypeToTemplateMap;
848 
851 
853 
856 
858  unsigned int myRequiredUvOffsets;
859 
863  unsigned int myNumThreads;
865 
869  typedef boost::unordered_map<unsigned int, unsigned int> PerTemplateCount;
870 
873 
877 
883 
887  class DT_DLL_VRVOSG AlwaysCull : public osg::Drawable::CullCallback
888  {
889  virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const
890  {
891  return true;
892  }
893  };
894 
895  osg::ref_ptr<AlwaysCull> myAlwaysCullCallback;
896 
897  osg::ref_ptr<DtInstanceNode> myInstanceRoot;
898 
902  typedef std::vector<DtInstanceProcessor*> InstanceProcessorVec;
903 
906 
909 
913  typedef std::vector<osg::View*> ViewVec;
915 
917  typedef boost::unordered_map<const osg::View*, osg::ref_ptr<DtViewSpecificGroup> > ViewToGroup;
919 
921  typedef boost::unordered_map<osg::Geometry*, osg::ref_ptr<DtPerViewUniformCallback> > GeometryToCallback;
923 
925  osg::ref_ptr<const DtOsgCullVisitorParameters> myLastCullParameters;
929 
930  void addInstanceRootToSceneGraphIfNecessary(void);
931 
932  void processNewInstanceLights(const InstanceTemplate& instanceTemplate);
933  void processNewInstanceDOFs(const InstanceTemplate& instanceTemplate);
934  void processNewInstanceSwitches(const InstanceTemplate& instanceTemplate, bool isSimpleModel);
935  void processNewInstanceSequences(const InstanceTemplate& instanceTemplate);
936 
938  void processNewInstanceLightPoints(const InstanceTemplate& instanceTemplate, const std::string &lightPointGroup);
939 
940  void preCull(unsigned int maxChunkSize);
941  void frustumCull(const osg::View* view, const Eyepoint& eye, unsigned int nodeMask);
942  void postCull(unsigned int maxChunkSize);
943 
944  std::vector<osg::Vec4d> vecScratchPadPlanes;
945 
946  void _fillRenderList(bool globalScaling, unsigned int instanceOffset, unsigned int numInstances);
947 
948 #if DT_INSTANCE_MANAGER_FILLRENDERLIST_IS_PARALLEL
950 #else
951  unsigned int myVisibleCount;
952 #endif
953  class DtInstanceManagerTBOUpdater * myDrawCallUpdater;
955 
956  std::string myInstanceVS, myInstanceViewVS, myInstanceFS;
957 
958  };
959 
965  {
966  public:
968  virtual ~DtInstanceManagerCreator();
969 
971  static DtInstanceManagerCreator& instance(DtDe& de);
972 
974  virtual DtInstanceManager* create();
975 
976  protected:
979 
980  protected:
982  };
983 }

Document ID: Generated on Thu Apr 12 03:15:37 EDT 2018 from SVN revision 187986
Copyright © 2005-2018 VT MÄK. All Rights Reserved (www.mak.com)