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>
13 #include <vrvCore/DtUniqueID.h>
16 
17 #include <matrix/vlVector.h>
18 
19 #include <osgUtil/LineSegmentIntersector>
20 
21 #include <osg/ref_ptr>
22 #include <osg/LightSource>
23 
24 #include <boost/unordered_map.hpp>
25 
26 namespace osg
27 {
28  class Geometry;
29  class Image;
30  class Texture2D;
31  class Vec2f;
32  class Vec3f;
33  class Geode;
34  class View;
35 }
36 
37 namespace osgEarth
38 {
39  class VirtualProgram;
40 }
41 
42 namespace makVrv
43 {
44  class DtGLTextureBufferObject;
45  class DtInstanceProcessor;
46  class DtOsgIntersectorResult;
47  class DtChannelManager;
48  class DtChannel;
49  class DtPerViewUniformCallback;
50  class DtInstanceNode;
51  class DtLight;
52  class DtOsgCullVisitorParameters;
53  class DtViewSpecificGroup;
54 
60  {
61  public:
62  typedef std::vector<osg::ref_ptr<osg::Geometry> > GeometryVec;
63  typedef std::vector<char> BoolVec; // Using vec of chars for threadsafety
64  typedef std::vector<char> ByteVec;
65  typedef std::map<osg::ref_ptr< osgEarth::VirtualProgram >, osg::ref_ptr<osgEarth::VirtualProgram> > VpMap;
66  typedef std::vector<osg::ref_ptr<osgEarth::VirtualProgram> > VpRefList;
67  typedef std::vector<ArrayRange> DrawableRangeVec;
68  typedef std::vector<osg::Vec2f> TextureCoordinateVec;
69  typedef std::vector<float> FloatVec;
70  typedef std::vector<DtUniqueID> CullSet; // vector of element IDs that were in the frustum for a render pass
71 
73  static const unsigned int NumVec4PerInstance = 8;
74 
76  struct InstancedLod
77  {
79  double minRange;
80 
82  double maxRange;
83 
88  };
89 
94  {
97 
100  };
101 
104  {
106  enum SequenceMode { Play, Stop, Pause, Resume };
107 
110 
113 
115  float startTime;
116 
118  float endTime;
119 
122 
124  float speed;
125 
128  };
129 
132  {
134  osg::Vec3f position;
135  osg::Vec3f direction;
136  unsigned int drawableIx; // index to drawable the light is attached to
137  };
138 
141  {
144 
148  unsigned int numChildren;
149 
152 
157  };
158 
161  {
164 
167 
170 
172  osg::Vec3f initialHpr;
173 
175  osg::Vec3f initialTranslate;
176 
180  unsigned int numChildren;
181 
184  };
185 
188  {
190  osg::Vec3f trans;
191 
192  osg::Vec3f transVel;
193 
195  osg::Vec3f hpr;
196 
198  osg::Vec3f hprVel;
199 
200 
202  float lastUpdate;
203  };
204 
205  struct LightSource
206  {
207  osg::ref_ptr<osg::LightSource> lightSource;
208  unsigned int attachDrawableIx;
209  };
210 
212  typedef std::vector<LightSource> LightSourceVec;
213  typedef std::vector<ModelTypeDofData> PerDofDataVec;
214  typedef std::vector<InstancedSwitch> InstancedSwitchVec;
215  typedef std::vector<InstancedSequence> InstancedSequenceVec;
216  typedef std::vector<DofAnimation> DofAnimations;
217  typedef std::vector<osg::Node*> NodeVec;
218  typedef std::vector<InstancedSwitch*> SwitchVec;
219  typedef std::vector<InstancedSequence*> SequenceVec;
220  typedef std::vector<InstancedLod*> LodVec;
222 
225  {
228 
231 
234 
237 
240 
243 
246 
248  osg::Vec3f boundingCenter;
249 
254  unsigned int nodeMask;
255 
257  osg::ref_ptr<osg::Geode> modelGeode;
258 
261  };
262 
265 
267  boost::signal<void (const std::string&)> signal_modelTypeRegistered;
268 
270  boost::signal<void (const std::string&, DtUniqueID, DtElementID, osg::Node *)> signal_instanceCreated;
271 
273  boost::signal<void (const CullSet&)> signal_updateFinished;
275 
278  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end,
279  const osg::Vec3d& reference, DtOsgIntersectorResult& result,
280  std::list<DtUniqueID>* ignoreList=0);
281 
283  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end,
284  std::vector<DtOsgIntersectorResult>& results,
285  std::list<DtUniqueID>* ignoreList=0);
286 
289  void setSequenceMode(DtInstanceState& instance, unsigned int sequenceIndex, InstancedSequence::SequenceMode mode);
290 
292  void setSequenceSpeed(DtInstanceState& instance, unsigned int sequenceIndex, float speed);
293 
295  void setSequencePlayTime(DtInstanceState& instance, unsigned int sequenceIndex, float loopTime);
297 
300  InstanceMatrix dofState(DtInstanceState& instanceState, unsigned int partIndex);
302 
305  void setDofState(InstanceMatrix& m, DtInstanceState& state, unsigned int partIndex);
306 
308  void setDofAnimation(osg::Vec3f& hpr, osg::Vec3f hprVel, DtInstanceState& state, unsigned int partIndex);
309 
311  void setDofTranslation(osg::Vec3f& trans, osg::Vec3f& transVel, DtInstanceState& state, unsigned int partIndex);
313 
316  void setSwitchState(DtInstanceState& instance, unsigned int partIndex, unsigned int childIndex, bool state);
318 
321  void setUVOffset(DtUniqueID id, unsigned int drawableIx, osg::Vec2f uvOffset);
323 
326  void setUserDefined(DtUniqueID id, float userDefined);
328 
330  void setElementId(DtUniqueID id, DtUniqueID elementId);
331 
333  static DtInstanceManager& instance(DtDe& de);
334 
336  DtInstanceManager(DtDe& de);
337 
339  virtual ~DtInstanceManager();
340 
344  virtual void reset();
345 
347  virtual osg::Node* instanceRoot();
348 
350  bool isRegistered(const std::string& modelType);
351 
353  void registerModel(const std::string& modelType, GeometryVec& drawables, LodVec& lods,
354  SwitchVec& switches, SequenceVec& sequences, PerDofDataVec& perDofData,
355  LightSourceVec& lightSources, float boundingRadius, const DtVector& boundingCenter,
356  unsigned int nodeMask, osg::ref_ptr<osg::StateSet> rootNodeSS, bool daylightControlledLights = false);
357 
359  osg::Geode *setupModelGeode(const std::string& modelType, unsigned int nodeMask);
360 
362  void allocateImage( osg::View* view );
363 
365  void freeInstance(DtUniqueID id);
366 
371  bool newInstance(const std::string& modelType, DtUniqueID id, DtElementID elementId, osg::Node* rootNode);
372 
377  DtInstanceState* findInstanceState(DtUniqueID id);
378 
383  InstanceCullData* findInstanceCullData(DtUniqueID id);
384 
386  typedef std::vector<DtInstanceState> InstanceStateVec;
387  typedef std::vector<InstancedLod> InstancedLodVec;
388  typedef std::vector<InstancedDof> InstancedDofVec;
389  typedef std::vector<InstanceCullData> InstanceCullDataVec;
390  typedef std::vector<InstanceMatrix> InstanceMatrixVec;
391  typedef std::vector<float> InstanceScaleVec;
392  typedef std::vector<DtElementID> InstanceElementIdVec;
393  typedef std::vector<InstancedLight> InstancedLightVec;
395 
397  inline InstanceElementIdVec& elementIds() { return myElementIds; }
398 
400  inline InstanceScaleVec& instanceScales() { return myInstanceScales; }
401 
403  inline ByteVec& instanceVisibleSet() { return myInstanceVisibleSet; }
404 
406  inline InstanceMatrixVec& drawableInstanceMatrix() { return myDrawableInstanceMatrix; }
407 
409  inline InstanceCullDataVec& instanceCullData() { return myInstanceCullData; }
410 
412  inline InstanceStateVec& instances() { return myInstances; }
413 
415  inline TextureCoordinateVec& uvOffsets() { return myUVOffsets; }
416 
419  inline InstancedLodVec& instancedLods() { return myInstancedLods; }
420 
421  inline InstancedSwitchVec& instancedSwitches() { return myInstancedSwitches; }
422 
423  inline InstancedSequenceVec& instancedSequences() { return myInstancedSequences; }
424 
425  inline InstancedDofVec& instancedDofs() { return myInstancedDofs; }
427 
428  typedef std::vector<InstanceTemplate> InstanceTemplateVec;
429 
431  inline InstanceTemplateVec& instanceTemplates() { return myInstanceTemplates; }
432 
434  inline std::vector<float>& distanceToEye() { return myDistanceToEye; }
435 
437  inline DofAnimations& dofAnimations() { return myDofAnimations; }
438 
440  inline double& lastUpdateTime() { return myLastUpdateTime; }
441 
443  inline DtOsgCullVisitorParameters* lastCullParameters() { return myLastCullParameters.get(); }
444 
446  inline unsigned int visibleSetStride() { return myVisibleSetStride; }
447 
449  inline BoolVec& visibleSet() { return myVisibleSet; }
450 
452  inline osg::Vec3d& cameraPosition() { return myCameraPosition; }
453 
455  inline osg::View* lastView() { return myLastView; }
456 
458  inline unsigned int drawableIndex(unsigned int instanceIx);
459 
461  void reloadShaders();
462 
463  inline GeometryVec &drawables(){ return myDrawables; }
464 
466  virtual bool isDaylight();
467 
468  protected:
471 
474 
477 
482 
485 
488 
491 
494 
505 
508 
510  std::vector<float> myDistanceToEye;
511 
514 
517 
519  unsigned int myLastNodeMask;
520 
522  unsigned int myVisibleSetStride;
523 
525  osg::Vec3d myCameraPosition;
526 
528  osg::View* myLastView;
529 
532 
533  protected:
534 
535  friend class DtInstanceNode;
536 
538  struct Eyepoint
539  {
540  osg::Vec3d center;
541  osg::Vec3d eye;
542  osg::Vec3d up;
543  };
544 
546  struct Frustum
547  {
548  double l, r, b, t, n, f;
549  };
550 
553  {
554  unsigned int instanceIx;
555  unsigned int drawableIx;
556  osg::Vec3f pos;
557  osgUtil::LineSegmentIntersector::Intersection result;
558  };
559 
560  protected:
569  void update(double simTime, osg::View* view, DtOsgCullVisitorParameters* params,
570  unsigned int nodeMask, bool suppressLastUpdateTime = false);
571 
573  void registerGeometry(unsigned int templateIx, osg::Geometry* geometry, VpMap& vpMap);
574 
576  float computeDistanceToEye(const Eyepoint& eye, const osg::Vec3d& pos) const;
577 
579  void allocateRenderList();
580 
582  unsigned int updateBuffers( osg::View* view, DtGLTextureBufferObject* tbo, const ArrayRange& drawableRange, unsigned int bufferOffset, osg::Vec4* bufferStorage );
583 
587  unsigned int updateRenderList( const Eyepoint& eye, const Frustum& frustum, const osg::Matrixd& viewMat, unsigned int nodeMask);
588 
592  void allocateInstanceData();
593 
597  unsigned int fillRenderList(unsigned visibleSetStride, unsigned int instanceOffset, unsigned int numInstances );
598 
600  void cullRange(const Eyepoint& eye, unsigned int instanceOffset, unsigned int instanceCount,
601  std::vector<osg::Vec4d>& planes, bool useScale, unsigned int nodeMask );
602 
604  void slot_channelAdded(DtChannelManager* mgr, DtChannel* channel);
605 
607  void slot_channelRemoved(DtChannelManager* mgr, DtChannel* channel);
608 
610  void createHitList(const osg::Vec3d& end, const osg::Vec3d& start, std::vector<InstancingHit>& hitList);
611 
613  void loadInstanceShaders(osg::ref_ptr<osgEarth::VirtualProgram> vp);
614 
615  bool isOnExcludeList( DtUniqueID elementId,
616  const std::list<DtUniqueID>& ignoreList);
617 
618  protected:
619 
622  unsigned int myDynamicBufferDataSz;
623 
625  struct RenderState
626  {
628  float scale;
629  float userDefined;
630  osg::Vec2f uvOffset;
631  osg::Vec3f filterColor;
632  };
633 
635  typedef std::vector<RenderState> RenderStateVec;
636 
639  {
643  unsigned int count;
644 
647  };
648 
650  typedef std::vector<GeometryRenderList> RenderList;
651 
653 
656 
658  typedef boost::unordered_map<DtUniqueID, unsigned int> InstanceStateMap;
659 
662 
665  {
667  osg::View* view;
668 
671  };
672 
674  typedef std::vector<InstanceBuffer> InstanceBufferVec;
675 
677 
679  typedef boost::unordered_map<std::string, int> modelTypeToTemplateMap;
680 
683 
685 
688 
690  unsigned int myRequiredUvOffsets;
691 
695  unsigned int myNumThreads;
697 
701  typedef boost::unordered_map<unsigned int, unsigned int> PerTemplateCount;
702 
705 
709 
715 
719  class DT_DLL_VRVOSG AlwaysCull : public osg::Drawable::CullCallback
720  {
721  virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const
722  {
723  return true;
724  }
725  };
726 
727  osg::ref_ptr<AlwaysCull> myAlwaysCullCallback;
728 
729  osg::ref_ptr<DtInstanceNode> myInstanceRoot;
730 
734  typedef std::vector<DtInstanceProcessor*> InstanceProcessorVec;
735 
738 
741 
745  typedef std::vector<osg::View*> ViewVec;
747 
749  typedef boost::unordered_map<osg::View*, osg::ref_ptr<DtViewSpecificGroup> > ViewToGroup;
751 
753  typedef boost::unordered_map<osg::Geometry*, osg::ref_ptr<DtPerViewUniformCallback> > GeometryToCallback;
755 
757  osg::ref_ptr<DtOsgCullVisitorParameters> myLastCullParameters;
758  };
759 
765  {
766  public:
768  virtual ~DtInstanceManagerCreator();
769 
771  static DtInstanceManagerCreator& instance(DtDe& de);
772 
774  virtual DtInstanceManager* create();
775 
776  protected:
779 
780  protected:
782  };
783 }

Document ID: Generated on Tue Mar 8 22:13:38 EST 2016 from SVN revision 162938
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)