VR-Forces 4.3 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) 2014 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
8 
9 #pragma once
10 
11 #include <vrvOsg/vrvOsg.h>
12 
13 #include <vrvCore/DtDe.h>
14 #include <vrvCore/DtDisplay.h>
15 #include <vrvCore/DtUniqueID.h>
16 
18 #include <vrvOsg/DtInstanceState.h>
20 
22 
23 #include <osgViewer/CompositeViewer>
24 
25 #include <osgEarth/VirtualProgram>
26 
27 #include <boost/unordered_map.hpp>
28 #include <boost/signals.hpp>
29 
30 class DtVector;
31 
32 namespace osg
33 {
34  class Geometry;
35  class Image;
36  class Texture2D;
37 }
38 
39 namespace makVrv
40 {
41  class DtGLTextureBufferObject;
42  class DtInstanceProcessor;
43  class DtOsgIntersectorResult;
44  class DtChannelManager;
45  class DtChannel;
46  class DtPerViewUniformCallback;
47  class DtInstanceNode;
48  class DtLight;
49 
55  {
56  public:
57  typedef std::vector<osg::ref_ptr<osg::Geometry> > GeometryVec;
58  typedef std::vector<char> BoolVec; // Using vec of chars for threadsafety
59  typedef std::vector<char> ByteVec;
60  typedef std::map<osg::ref_ptr< osgEarth::VirtualProgram >, osg::ref_ptr<osgEarth::VirtualProgram> > VpMap;
61  typedef std::vector<osg::ref_ptr<osgEarth::VirtualProgram> > VpRefList;
62  typedef std::vector<ArrayRange> DrawableRangeVec;
63  typedef std::vector<osg::Vec2f> TextureCoordinateVec;
64  typedef std::vector<float> FloatVec;
65 
67  static const unsigned int NumVec4PerInstance = 8;
68 
70  struct InstancedLod
71  {
73  double minRange;
74 
76  double maxRange;
77 
82  };
83 
88  {
91 
94  };
95 
98  {
100  enum SequenceMode { Play, Stop, Pause, Resume };
101 
104 
107 
109  float startTime;
110 
112  float endTime;
113 
116 
118  float speed;
119 
122  };
123 
126  {
128  osg::Vec3f position;
129  osg::Vec3f direction;
130  unsigned int drawableIx; // index to drawable the light is attached to
131  };
132 
135  {
138 
142  unsigned int numChildren;
143 
146 
151  };
152 
155  {
158 
161 
164 
166  osg::Vec3f initialHpr;
167 
169  osg::Vec3f initialTranslate;
170 
174  unsigned int numChildren;
175 
178  };
179 
182  {
184  osg::Vec3f trans;
185 
187  osg::Vec3f hpr;
188 
190  osg::Vec3f hprVel;
191 
193  float lastUpdate;
194  };
195 
196  struct LightSource
197  {
198  osg::ref_ptr<osg::LightSource> lightSource;
199  unsigned int attachDrawableIx;
200  };
201 
203  typedef std::vector<LightSource> LightSourceVec;
204  typedef std::vector<ModelTypeDofData> PerDofDataVec;
205  typedef std::vector<InstancedSwitch> InstancedSwitchVec;
206  typedef std::vector<InstancedSequence> InstancedSequenceVec;
207  typedef std::vector<DofAnimation> DofAnimations;
208  typedef std::vector<osg::Node*> NodeVec;
209  typedef std::vector<InstancedSwitch*> SwitchVec;
210  typedef std::vector<InstancedSequence*> SequenceVec;
211  typedef std::vector<InstancedLod*> LodVec;
213 
216  {
219 
222 
225 
228 
231 
234 
237 
239  osg::Vec3f boundingCenter;
240 
245  unsigned int nodeMask;
246  };
247 
250 
252  boost::signal<void (const std::string&)> signal_modelTypeRegistered;
253 
255 
257  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end, DtOsgIntersectorResult& result);
258 
260  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end, std::vector<DtOsgIntersectorResult>& results);
261 
264  void setSequenceMode(DtInstanceState& instance, unsigned int sequenceIndex, InstancedSequence::SequenceMode mode);
265 
267  void setSequenceSpeed(DtInstanceState& instance, unsigned int sequenceIndex, float speed);
268 
270  void setSequencePlayTime(DtInstanceState& instance, unsigned int sequenceIndex, float loopTime);
272 
275  InstanceMatrix dofState(DtInstanceState& instanceState, unsigned int partIndex);
277 
280  void setDofState(InstanceMatrix& m, DtInstanceState& state, unsigned int partIndex);
281 
283  void setDofAnimation(osg::Vec3f& hpr, osg::Vec3f hprVel, DtInstanceState& state, unsigned int partIndex);
284 
286  void setDofTranslation(osg::Vec3f& trans, DtInstanceState& state, unsigned int partIndex);
288 
291  void setSwitchState(DtInstanceState& instance, unsigned int partIndex, unsigned int childIndex, bool state);
293 
296  void setUVOffset(DtUniqueID id, unsigned int drawableIx, osg::Vec2f uvOffset);
298 
301  void setUserDefined(DtUniqueID id, float userDefined);
303 
305  static DtInstanceManager& instance(DtDe& de);
306 
308  DtInstanceManager(DtDe& de);
309 
311  virtual ~DtInstanceManager();
312 
316  virtual void reset();
317 
319  virtual osg::Node* instanceRoot();
320 
322  bool isRegistered(const std::string& modelType);
323 
325  void registerModel(const std::string& modelType, GeometryVec& drawables, LodVec& lods,
326  SwitchVec& switches, SequenceVec& sequences, PerDofDataVec& perDofData,
327  LightSourceVec& lightSources, float boundingRadius, const DtVector& boundingCenter,
328  unsigned int nodeMask, osg::ref_ptr<osg::StateSet> rootNodeSS);
329 
331  void setupModelGeode(const std::string& modelType, unsigned int nodeMask);
332 
334  void allocateImage( osg::View* view );
335 
337  void freeInstance(DtUniqueID id);
338 
343  bool newInstance(const std::string& modelType, DtUniqueID id, DtElementID elementId);
344 
349  DtInstanceState* findInstanceState(DtUniqueID id);
350 
355  InstanceCullData* findInstanceCullData(DtUniqueID id);
356 
358  typedef std::vector<DtInstanceState> InstanceStateVec;
359  typedef std::vector<InstancedLod> InstancedLodVec;
360  typedef std::vector<InstancedDof> InstancedDofVec;
361  typedef std::vector<InstanceCullData> InstanceCullDataVec;
362  typedef std::vector<InstanceMatrix> InstanceMatrixVec;
363  typedef std::vector<float> InstanceScaleVec;
364  typedef std::vector<DtElementID> InstanceElementIdVec;
365  typedef std::vector<InstancedLight> InstancedLightVec;
367 
369  inline InstanceElementIdVec& elementIds() { return myElementIds; }
370 
372  inline InstanceScaleVec& instanceScales() { return myInstanceScales; }
373 
375  inline ByteVec& instanceVisibleSet() { return myInstanceVisibleSet; }
376 
378  inline InstanceMatrixVec& drawableInstanceMatrix() { return myDrawableInstanceMatrix; }
379 
381  inline InstanceCullDataVec& instanceCullData() { return myInstanceCullData; }
382 
384  inline InstanceStateVec& instances() { return myInstances; }
385 
387  inline TextureCoordinateVec& uvOffsets() { return myUVOffsets; }
388 
391  inline InstancedLodVec& instancedLods() { return myInstancedLods; }
392 
393  inline InstancedSwitchVec& instancedSwitches() { return myInstancedSwitches; }
394 
395  inline InstancedSequenceVec& instancedSequences() { return myInstancedSequences; }
396 
397  inline InstancedDofVec& instancedDofs() { return myInstancedDofs; }
399 
400  typedef std::vector<InstanceTemplate> InstanceTemplateVec;
401 
403  inline InstanceTemplateVec& instanceTemplates() { return myInstanceTemplates; }
404 
406  inline std::vector<float>& distanceToEye() { return myDistanceToEye; }
407 
409  inline DofAnimations& dofAnimations() { return myDofAnimations; }
410 
412  inline double& lastUpdateTime() { return myLastUpdateTime; }
413 
415  inline DtOsgCullVisitorParameters* lastCullParameters() { return myLastCullParameters.get(); }
416 
418  inline unsigned int visibleSetStride() { return myVisibleSetStride; }
419 
421  inline BoolVec& visibleSet() { return myVisibleSet; }
422 
424  inline osg::Vec3d& cameraPosition() { return myCameraPosition; }
425 
427  inline osg::View* lastView() { return myLastView; }
428 
430  inline unsigned int drawableIndex(unsigned int instanceIx);
431 
433  void reloadShaders();
434 
435  inline GeometryVec &drawables(){ return myDrawables;};
436 
437  protected:
439  BoolVec myVisibleSet;
440 
443 
446 
451 
454 
457 
460 
463 
474 
477 
479  std::vector<float> myDistanceToEye;
480 
483 
486 
488  unsigned int myLastNodeMask;
489 
491  unsigned int myVisibleSetStride;
492 
494  osg::Vec3d myCameraPosition;
495 
497  osg::View* myLastView;
498 
499  protected:
500 
501  friend class DtInstanceNode;
502 
504  struct Eyepoint
505  {
506  osg::Vec3d center;
507  osg::Vec3d eye;
508  osg::Vec3d up;
509  };
510 
512  struct Frustum
513  {
514  double l, r, b, t, n, f;
515  };
516 
519  {
520  unsigned int instanceIx;
521  unsigned int drawableIx;
522  osg::Vec3f pos;
523  osgUtil::LineSegmentIntersector::Intersection result;
524  };
525 
526  protected:
532  void update(double simTime, osg::View* view, DtOsgCullVisitorParameters* params, unsigned int nodeMask);
533 
535  void registerGeometry(const std::string& modelType, osg::Geometry* geometry, VpMap& vpMap);
536 
538  float computeDistanceToEye(const Eyepoint& eye, const osg::Vec3d& pos) const;
539 
541  void allocateRenderList();
542 
544  unsigned int updateBuffers( osg::View* view, DtGLTextureBufferObject* tbo, const ArrayRange& drawableRange, unsigned int bufferOffset, osg::Vec4* bufferStorage );
545 
549  unsigned int updateRenderList( const Eyepoint& eye, const Frustum& frustum, const osg::Matrixd& viewMat, unsigned int nodeMask);
550 
554  void allocateInstanceData();
555 
559  unsigned int fillRenderList(unsigned visibleSetStride, unsigned int instanceOffset, unsigned int numInstances );
560 
562  void cullRange(const Eyepoint& eye, unsigned int instanceOffset, unsigned int instanceCount,
563  std::vector<osg::Vec4d>& planes, bool useScale, unsigned int nodeMask );
564 
566  void slot_channelAdded(DtChannelManager* mgr, DtChannel* channel);
567 
569  void slot_channelRemoved(DtChannelManager* mgr, DtChannel* channel);
570 
572  void createHitList(const osg::Vec3d& end, const osg::Vec3d& start, std::vector<InstancingHit>& hitList);
573 
575  void loadInstanceShaders(osg::ref_ptr<osgEarth::VirtualProgram> vp);
576 
577 
578  protected:
579 
581  unsigned int myDynamicBufferDataSz;
582 
584  struct RenderState
585  {
587  float scale;
588  float userDefined;
589  osg::Vec2f uvOffset;
590  osg::Vec3f filterColor;
591  };
592 
594  typedef std::vector<RenderState> RenderStateVec;
595 
598  {
602  unsigned int count;
603 
606  };
607 
609  typedef std::vector<GeometryRenderList> RenderList;
610 
612 
615 
617  typedef boost::unordered_map<DtUniqueID, unsigned int> InstanceStateMap;
618 
621 
624  {
626  osg::View* view;
627 
630  };
631 
633  typedef std::vector<InstanceBuffer> InstanceBufferVec;
634 
636 
638  typedef boost::unordered_map<std::string, int> modelTypeToTemplateMap;
639 
642 
644 
647 
649  unsigned int myRequiredUvOffsets;
650 
654  unsigned int myNumThreads;
656 
660  typedef boost::unordered_map<unsigned int, unsigned int> PerTemplateCount;
661 
664 
668 
674 
676  typedef boost::unordered_map<std::string, osg::ref_ptr<osg::Geode> > modelTypeToGroup;
677 
679 
683  class DT_DLL_VRVOSG AlwaysCull : public osg::Drawable::CullCallback
684  {
685  virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const
686  {
687  return true;
688  }
689  };
690 
691  osg::ref_ptr<AlwaysCull> myAlwaysCullCallback;
692 
693  osg::ref_ptr<DtInstanceNode> myInstanceRoot;
694 
698  typedef std::vector<DtInstanceProcessor*> InstanceProcessorVec;
699 
702 
705 
709  typedef std::vector<osg::View*> ViewVec;
711 
713  typedef boost::unordered_map<osg::View*, osg::ref_ptr<DtViewSpecificGroup> > ViewToGroup;
715 
717  typedef boost::unordered_map<osg::Geometry*, osg::ref_ptr<DtPerViewUniformCallback> > GeometryToCallback;
719 
721  osg::ref_ptr<DtOsgCullVisitorParameters> myLastCullParameters;
722  };
723 
729  {
730  public:
732  virtual ~DtInstanceManagerCreator();
733 
735  static DtInstanceManagerCreator& instance(DtDe& de);
736 
738  virtual DtInstanceManager* create();
739 
740  protected:
743 
744  protected:
746  };
747 }

Document ID: Generated on Wed Mar 11 21:20:57 EDT 2015 from SVN revision 150940
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)