VR-Forces 4.3.1 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  typedef std::vector<DtUniqueID> CullSet; // vector of element IDs that were in the frustum for a render pass
66 
68  static const unsigned int NumVec4PerInstance = 8;
69 
71  struct InstancedLod
72  {
74  double minRange;
75 
77  double maxRange;
78 
83  };
84 
89  {
92 
95  };
96 
99  {
101  enum SequenceMode { Play, Stop, Pause, Resume };
102 
105 
108 
110  float startTime;
111 
113  float endTime;
114 
117 
119  float speed;
120 
123  };
124 
127  {
129  osg::Vec3f position;
130  osg::Vec3f direction;
131  unsigned int drawableIx; // index to drawable the light is attached to
132  };
133 
136  {
139 
143  unsigned int numChildren;
144 
147 
152  };
153 
156  {
159 
162 
165 
167  osg::Vec3f initialHpr;
168 
170  osg::Vec3f initialTranslate;
171 
175  unsigned int numChildren;
176 
179  };
180 
183  {
185  osg::Vec3f trans;
186 
188  osg::Vec3f hpr;
189 
191  osg::Vec3f hprVel;
192 
194  float lastUpdate;
195  };
196 
197  struct LightSource
198  {
199  osg::ref_ptr<osg::LightSource> lightSource;
200  unsigned int attachDrawableIx;
201  };
202 
204  typedef std::vector<LightSource> LightSourceVec;
205  typedef std::vector<ModelTypeDofData> PerDofDataVec;
206  typedef std::vector<InstancedSwitch> InstancedSwitchVec;
207  typedef std::vector<InstancedSequence> InstancedSequenceVec;
208  typedef std::vector<DofAnimation> DofAnimations;
209  typedef std::vector<osg::Node*> NodeVec;
210  typedef std::vector<InstancedSwitch*> SwitchVec;
211  typedef std::vector<InstancedSequence*> SequenceVec;
212  typedef std::vector<InstancedLod*> LodVec;
214 
217  {
220 
223 
226 
229 
232 
235 
238 
240  osg::Vec3f boundingCenter;
241 
246  unsigned int nodeMask;
247 
249  osg::ref_ptr<osg::Geode> modelGeode;
250 
253  };
254 
257 
259  boost::signal<void (const std::string&)> signal_modelTypeRegistered;
260 
262  boost::signal<void (const std::string&, DtUniqueID, DtElementID, osg::Node *)> signal_instanceCreated;
263 
265  boost::signal<void (const CullSet&)> signal_updateFinished;
267 
269  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end, DtOsgIntersectorResult& result);
270 
272  bool intersect(const osg::Vec3d& start, const osg::Vec3d& end, std::vector<DtOsgIntersectorResult>& results);
273 
276  void setSequenceMode(DtInstanceState& instance, unsigned int sequenceIndex, InstancedSequence::SequenceMode mode);
277 
279  void setSequenceSpeed(DtInstanceState& instance, unsigned int sequenceIndex, float speed);
280 
282  void setSequencePlayTime(DtInstanceState& instance, unsigned int sequenceIndex, float loopTime);
284 
287  InstanceMatrix dofState(DtInstanceState& instanceState, unsigned int partIndex);
289 
292  void setDofState(InstanceMatrix& m, DtInstanceState& state, unsigned int partIndex);
293 
295  void setDofAnimation(osg::Vec3f& hpr, osg::Vec3f hprVel, DtInstanceState& state, unsigned int partIndex);
296 
298  void setDofTranslation(osg::Vec3f& trans, DtInstanceState& state, unsigned int partIndex);
300 
303  void setSwitchState(DtInstanceState& instance, unsigned int partIndex, unsigned int childIndex, bool state);
305 
308  void setUVOffset(DtUniqueID id, unsigned int drawableIx, osg::Vec2f uvOffset);
310 
313  void setUserDefined(DtUniqueID id, float userDefined);
315 
317  void setElementId(DtUniqueID id, DtUniqueID elementId);
318 
320  static DtInstanceManager& instance(DtDe& de);
321 
323  DtInstanceManager(DtDe& de);
324 
326  virtual ~DtInstanceManager();
327 
331  virtual void reset();
332 
334  virtual osg::Node* instanceRoot();
335 
337  bool isRegistered(const std::string& modelType);
338 
340  void registerModel(const std::string& modelType, GeometryVec& drawables, LodVec& lods,
341  SwitchVec& switches, SequenceVec& sequences, PerDofDataVec& perDofData,
342  LightSourceVec& lightSources, float boundingRadius, const DtVector& boundingCenter,
343  unsigned int nodeMask, osg::ref_ptr<osg::StateSet> rootNodeSS, bool daylightControlledLights = false);
344 
346  osg::Geode *setupModelGeode(const std::string& modelType, unsigned int nodeMask);
347 
349  void allocateImage( osg::View* view );
350 
352  void freeInstance(DtUniqueID id);
353 
358  bool newInstance(const std::string& modelType, DtUniqueID id, DtElementID elementId, osg::Node* rootNode);
359 
364  DtInstanceState* findInstanceState(DtUniqueID id);
365 
370  InstanceCullData* findInstanceCullData(DtUniqueID id);
371 
373  typedef std::vector<DtInstanceState> InstanceStateVec;
374  typedef std::vector<InstancedLod> InstancedLodVec;
375  typedef std::vector<InstancedDof> InstancedDofVec;
376  typedef std::vector<InstanceCullData> InstanceCullDataVec;
377  typedef std::vector<InstanceMatrix> InstanceMatrixVec;
378  typedef std::vector<float> InstanceScaleVec;
379  typedef std::vector<DtElementID> InstanceElementIdVec;
380  typedef std::vector<InstancedLight> InstancedLightVec;
382 
384  inline InstanceElementIdVec& elementIds() { return myElementIds; }
385 
387  inline InstanceScaleVec& instanceScales() { return myInstanceScales; }
388 
390  inline ByteVec& instanceVisibleSet() { return myInstanceVisibleSet; }
391 
393  inline InstanceMatrixVec& drawableInstanceMatrix() { return myDrawableInstanceMatrix; }
394 
396  inline InstanceCullDataVec& instanceCullData() { return myInstanceCullData; }
397 
399  inline InstanceStateVec& instances() { return myInstances; }
400 
402  inline TextureCoordinateVec& uvOffsets() { return myUVOffsets; }
403 
406  inline InstancedLodVec& instancedLods() { return myInstancedLods; }
407 
408  inline InstancedSwitchVec& instancedSwitches() { return myInstancedSwitches; }
409 
410  inline InstancedSequenceVec& instancedSequences() { return myInstancedSequences; }
411 
412  inline InstancedDofVec& instancedDofs() { return myInstancedDofs; }
414 
415  typedef std::vector<InstanceTemplate> InstanceTemplateVec;
416 
418  inline InstanceTemplateVec& instanceTemplates() { return myInstanceTemplates; }
419 
421  inline std::vector<float>& distanceToEye() { return myDistanceToEye; }
422 
424  inline DofAnimations& dofAnimations() { return myDofAnimations; }
425 
427  inline double& lastUpdateTime() { return myLastUpdateTime; }
428 
430  inline DtOsgCullVisitorParameters* lastCullParameters() { return myLastCullParameters.get(); }
431 
433  inline unsigned int visibleSetStride() { return myVisibleSetStride; }
434 
436  inline BoolVec& visibleSet() { return myVisibleSet; }
437 
439  inline osg::Vec3d& cameraPosition() { return myCameraPosition; }
440 
442  inline osg::View* lastView() { return myLastView; }
443 
445  inline unsigned int drawableIndex(unsigned int instanceIx);
446 
448  void reloadShaders();
449 
450  inline GeometryVec &drawables(){ return myDrawables; }
451 
453  virtual bool isDaylight();
454 
455  protected:
458 
461 
464 
469 
472 
475 
478 
481 
492 
495 
497  std::vector<float> myDistanceToEye;
498 
501 
504 
506  unsigned int myLastNodeMask;
507 
509  unsigned int myVisibleSetStride;
510 
512  osg::Vec3d myCameraPosition;
513 
515  osg::View* myLastView;
516 
519 
520  protected:
521 
522  friend class DtInstanceNode;
523 
525  struct Eyepoint
526  {
527  osg::Vec3d center;
528  osg::Vec3d eye;
529  osg::Vec3d up;
530  };
531 
533  struct Frustum
534  {
535  double l, r, b, t, n, f;
536  };
537 
540  {
541  unsigned int instanceIx;
542  unsigned int drawableIx;
543  osg::Vec3f pos;
544  osgUtil::LineSegmentIntersector::Intersection result;
545  };
546 
547  protected:
553  void update(double simTime, osg::View* view, DtOsgCullVisitorParameters* params, unsigned int nodeMask);
554 
556  void registerGeometry(unsigned int templateIx, osg::Geometry* geometry, VpMap& vpMap);
557 
559  float computeDistanceToEye(const Eyepoint& eye, const osg::Vec3d& pos) const;
560 
562  void allocateRenderList();
563 
565  unsigned int updateBuffers( osg::View* view, DtGLTextureBufferObject* tbo, const ArrayRange& drawableRange, unsigned int bufferOffset, osg::Vec4* bufferStorage );
566 
570  unsigned int updateRenderList( const Eyepoint& eye, const Frustum& frustum, const osg::Matrixd& viewMat, unsigned int nodeMask);
571 
575  void allocateInstanceData();
576 
580  unsigned int fillRenderList(unsigned visibleSetStride, unsigned int instanceOffset, unsigned int numInstances );
581 
583  void cullRange(const Eyepoint& eye, unsigned int instanceOffset, unsigned int instanceCount,
584  std::vector<osg::Vec4d>& planes, bool useScale, unsigned int nodeMask );
585 
587  void slot_channelAdded(DtChannelManager* mgr, DtChannel* channel);
588 
590  void slot_channelRemoved(DtChannelManager* mgr, DtChannel* channel);
591 
593  void createHitList(const osg::Vec3d& end, const osg::Vec3d& start, std::vector<InstancingHit>& hitList);
594 
596  void loadInstanceShaders(osg::ref_ptr<osgEarth::VirtualProgram> vp);
597 
598  protected:
599 
602  unsigned int myDynamicBufferDataSz;
603 
605  struct RenderState
606  {
608  float scale;
609  float userDefined;
610  osg::Vec2f uvOffset;
611  osg::Vec3f filterColor;
612  };
613 
615  typedef std::vector<RenderState> RenderStateVec;
616 
619  {
623  unsigned int count;
624 
627  };
628 
630  typedef std::vector<GeometryRenderList> RenderList;
631 
633 
636 
638  typedef boost::unordered_map<DtUniqueID, unsigned int> InstanceStateMap;
639 
642 
645  {
647  osg::View* view;
648 
651  };
652 
654  typedef std::vector<InstanceBuffer> InstanceBufferVec;
655 
657 
659  typedef boost::unordered_map<std::string, int> modelTypeToTemplateMap;
660 
663 
665 
668 
670  unsigned int myRequiredUvOffsets;
671 
675  unsigned int myNumThreads;
677 
681  typedef boost::unordered_map<unsigned int, unsigned int> PerTemplateCount;
682 
685 
689 
695 
699  class DT_DLL_VRVOSG AlwaysCull : public osg::Drawable::CullCallback
700  {
701  virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const
702  {
703  return true;
704  }
705  };
706 
707  osg::ref_ptr<AlwaysCull> myAlwaysCullCallback;
708 
709  osg::ref_ptr<DtInstanceNode> myInstanceRoot;
710 
714  typedef std::vector<DtInstanceProcessor*> InstanceProcessorVec;
715 
718 
721 
725  typedef std::vector<osg::View*> ViewVec;
727 
729  typedef boost::unordered_map<osg::View*, osg::ref_ptr<DtViewSpecificGroup> > ViewToGroup;
731 
733  typedef boost::unordered_map<osg::Geometry*, osg::ref_ptr<DtPerViewUniformCallback> > GeometryToCallback;
735 
737  osg::ref_ptr<DtOsgCullVisitorParameters> myLastCullParameters;
738  };
739 
745  {
746  public:
748  virtual ~DtInstanceManagerCreator();
749 
751  static DtInstanceManagerCreator& instance(DtDe& de);
752 
754  virtual DtInstanceManager* create();
755 
756  protected:
759 
760  protected:
762  };
763 }

Document ID: Generated on Wed Jul 29 00:55:00 EDT 2015 from SVN revision 155257
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)