VR-Forces Development_Version Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DtOsgShadowTechnique.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2014 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <vrvOsg/vrvOsg.h>
13 
14 #include <osgEarth/VirtualProgram>
15 
16 #include <osgShadow/ShadowTechnique>
17 
18 #include <osg/StateSet>
19 #include <osg/Camera>
20 #include <osg/PolygonOffset>
21 #include <osg/FrameBufferObject>
22 #include <osg/Uniform>
23 #include <osg/LightSource>
24 #include <osg/RenderInfo>
25 #include <osg/Vec2>
26 #include <osg/Vec3>
27 #include <osg/Matrix>
28 #include <osg/Program>
29 #include <osg/Texture2D>
30 #include <osg/Texture2DArray>
31 #include <osg/Geode>
32 #include <osg/GL>
33 
34 #include <vector>
35 #include <map>
36 #include <string>
37 
38 namespace makVrv
39 {
40 
41  class DtChannel;
42  class DtOsgChannel;
43  class DtShadowSettingsObject;
44  class TextureApplyAttribute;
45 
48  class DT_DLL_VRVOSG DtOsgShadowTechnique : public osgShadow::ShadowTechnique
49  {
50  public:
51 
54 
56  DtOsgShadowTechnique(const DtOsgShadowTechnique& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
57 
59  virtual osg::Object* cloneType() const { return new DtOsgShadowTechnique(myDe); }
60 
62  virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new DtOsgShadowTechnique (*this, copyop); }
63 
65  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const DtOsgShadowTechnique *>(obj)!=NULL; }
66 
68  virtual const char* libraryName() const { return "makVrv"; }
69 
71  virtual const char* className() const { return "DtOsgShadowTechnique"; }
72 
74  virtual void init();
75 
77  virtual void update(osg::NodeVisitor& nv);
78 
80  virtual void cull(osgUtil::CullVisitor& cv);
81 
83  virtual void cleanSceneGraph();
84 
86  void setChannelShadowState(DtOsgChannel* osgChannel, bool enable);
87 
89  bool getChannelShadowSettings(DtOsgChannel* osgChannel, DtShadowSettingsObject** shadowSettingObject, bool* isPlanView);
90 
92  bool getChannelCloudShadowMap(DtOsgChannel* osgChannel, unsigned int* textureIndex, osg::Matrix* textureViewProjMatrix);
93 
95  bool getCameraCloudShadowMap(osg::Camera* camera, unsigned int* textureIndex, osg::Matrix* textureViewProjMatrix);
96 
98  void setGlobalLight(osg::LightSource* light) { myGlobalLight = light; }
99 
101  const osg::LightSource* getGlobalLight() const { return myGlobalLight.get(); }
102 
103  static const std::string& shadowCameraPrefixString(void);
104  static const std::string& shadowSDSMCameraPrefixString(void);
105 
106  protected:
108  virtual void reInitialize(unsigned int quality);
109 
110  virtual bool configureShadowTraversalNodeMask(bool traverseEntities, bool traverseProps, bool traverseTerrain);
111 
112  struct PreDrawFBOCallback : public osg::Camera::DrawCallback
113  {
115  PreDrawFBOCallback( osg::FrameBufferObject* fbo);
116 
118  virtual void operator () (osg::RenderInfo& renderInfo) const;
119 
121  void enableBindFbo(bool bind);
122 
123  private:
124  osg::ref_ptr<osg::FrameBufferObject> mySharedFbo;
126  };
127 
128 
129  struct PostDrawFBOCallback : public osg::Camera::DrawCallback
130  {
133 
135  virtual void operator () (osg::RenderInfo& renderInfo) const;
136 
138  void setRestoreDrawBuffer(GLenum bufferAttachment);
139 
141  void setRestoreReadBuffer(GLenum bufferAttachment);
142 
144  void enableUnbindFbo(bool unbind);
145  private:
149  };
150 
151  struct SdsmDrawCallback : public osg::Camera::DrawCallback
152  {
154  SdsmDrawCallback(bool bindFbo, GLenum colorAttachment, unsigned int numPipelines, unsigned int numPartitions, unsigned int width, unsigned int height);
155 
157  ~SdsmDrawCallback();
158 
160  virtual void operator () (osg::RenderInfo& renderInfo) const;
161 
163  float minDepth() const;
164 
166  float maxDepth() const;
167 
169  float minDepth(unsigned int splitIndex) const;
170 
172  float maxDepth(unsigned int splitIndex) const;
173 
175  osg::Vec2 partition(unsigned int splitIndex) const;
176 
178  bool isSDSMDepthAvailable() const;
179  private:
180  mutable std::vector<osg::Vec2> myPartitions;
181  mutable unsigned int myFBOBufferIndex;
182  mutable std::vector<unsigned int> myPBOBufferIndex;
183  mutable unsigned int myCurrentPBOIndex;
184  mutable unsigned int myColorTextureIndex;
185  mutable unsigned int myDepthRBOBufferIndex;
187  bool myBindFbo;
188  unsigned int myWidth;
189  unsigned int myHeight;
190  mutable float myMinDepth;
191  mutable float myMaxDepth;
192  mutable float myMinDepthArray[20];
193  mutable float myMaxDepthArray[20];
194  unsigned int myDepthSamplePos;
195  mutable bool mySDSMDepthAvailable;
196  };
197 
198 
199  struct PostDrawSDSMCallback : public osg::Camera::DrawCallback
200  {
203 
204  // Callback Method to unbind the currently bound framebuffer object
205  virtual void operator () (osg::RenderInfo& renderInfo) const;
206  };
207 
209  {
211  unsigned int myCascadeIndex;
212  };
213 
215  {
216  osg::ref_ptr<osg::Camera> myCamera;
217  osg::ref_ptr<osg::Camera> myDepthCamera;
218  osg::ref_ptr<osg::Camera> myXBlurCamera;
219  osg::ref_ptr<osg::Camera> myYBlurCamera;
220  unsigned int myTexUnit;
222  osg::ref_ptr<osg::Uniform> myAtlasTexOffsetAndScaleUniform;
223  osg::ref_ptr<osg::PolygonOffset> myPolygonOffset;
224 
225 
226  double mySplitFar;
227  osg::ref_ptr<osg::Uniform> mySplitFarUniform;
228 
229  unsigned int mySplitIndex;
231 
233 
234  unsigned int myRefreshAge;
235  bool myRefresh;
236  };
237 
238  struct PerChannelData : public osg::Referenced
239  {
242  : myAtlasWidth(0)
243  , myAtlasHeight(0)
244  , myAtlasWidthNumSplits(0)
245  , myAtlasHeightNumSplits(0)
246  , myJustInitialized(false)
247  , myNotInitialized(true)
248  , myChannel(channel)
249  , mySDSMTime(0)
250  {
251 
252  }
253  std::vector<CascadeImportanceData> myImportance;
254  std::vector<CsmSplitData> myCsmSplits;
255  osg::ref_ptr<osg::StateSet> mySharedStateSet;
256  osg::ref_ptr<osg::Uniform> myShadowTexGenUniform;
257  osg::ref_ptr<osg::Uniform> myCloudShadowTexGenUniform;
258  osg::ref_ptr<TextureApplyAttribute> myCloudShadowTexture;
260  osg::ref_ptr<osgEarth::VirtualProgram> myVirtualProgram;
261  unsigned int myAtlasWidth;
262  unsigned int myAtlasHeight;
263  unsigned int myAtlasWidthNumSplits;
265  osg::ref_ptr<osg::Uniform> myShadowAmbientFactorUniform;
270  osg::ref_ptr<osg::Camera> mySDSMCamera;
271  std::vector<osg::ref_ptr<osg::Camera> > mySDSMReduceCamera;
272  osg::ref_ptr<osg::Uniform> myFarNearSumDiffProdx2Uniform;
273  osg::ref_ptr<osg::Uniform> myMaximumViewDistanceUniform;
274  osg::ref_ptr<osg::Uniform> myMaximumDepthUniform;
275  osg::ref_ptr<osg::Uniform> myInvNumSplitsInterpUniform;
276  osg::ref_ptr<osg::Camera> mySDSMLinearizeDepthCamera;
277  osg::ref_ptr<osg::Camera> mySDSMCopyReduceDepthCamera;
278  osg::ref_ptr<osg::Camera> mySDSMPartitionCamera;
279  std::vector<osg::ref_ptr<osg::Camera> > mySDSMPartitionReduceCamera;
280  osg::ref_ptr<SdsmDrawCallback> mySDSMDrawCallback;
281  osg::ref_ptr<osg::Camera> myAtlasDepthCamera;
282  osg::ref_ptr<osg::Camera> myAtlasXBlurCamera;
283  osg::ref_ptr<osg::Camera> myAtlasYBlurCamera;
284  osg::ref_ptr<osg::Camera> myCloudShadowCamera;
285  double mySDSMTime;
286  protected:
288  virtual ~PerChannelData(){}
289  };
290 
291 
292  struct CloudShadowPreDrawCallback : public osg::Camera::DrawCallback
293  {
295  CloudShadowPreDrawCallback(DtOsgShadowTechnique& shadowTechnique, PerChannelData& perChannelData);
296 
297  // Callback Method to unbind the currently bound framebuffer object
298  virtual void operator () (osg::RenderInfo& renderInfo) const;
301  };
302 
304  virtual std::string generateVertexShader(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
305 
307  virtual std::string generateFragmentShader(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
308 
310  virtual std::string generateCloudShadowVertexShader(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
311 
313  virtual std::string generateCloudShadowFragmentShader(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
314 
316  unsigned int getFilterCount(DtShadowSettingsObject* shadowSettingObject);
317 
318  private:
319 
321  std::string generatePoissonDiscArray(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
322 
324  std::string generateHelperFunctions(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
325 
327  std::string generateFragmentComputeShadowFunction(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
328 
330  std::string generateFragmentUniformAndVariables(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
331 
333  std::string generateFragmentMain(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
334 
336  std::string generateFragmentVisualizeCascadeMain(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
337 
339  bool getCameraSettings(osg::Camera* viewCamera, PerChannelData** perChannelData, DtShadowSettingsObject** shadowSettingObject, bool* isPlanView);
340 
342  void initPerChannelData(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
343 
345  void installVirtualProgramAndSharedStateSet(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
346 
348  void packTextureAtlas(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
349 
351  void installSDSM(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
352 
354  void traverseSDSMCameras(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osgUtil::CullVisitor& cv, double cameraMagnification);
355 
357  void installCloudShadows(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
358 
360  void updateCloudShadows(osg::Camera* camera);
361 
363  void updateCloudShadows(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, const osg::Matrix& inverseViewMatrix);
364 
366  void updateCascadeImportance(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
367 
369  osg::Program* getProgramFromFile(const std::string& szVertexShader, const std::string& szFragmentShader);
370 
372  void installPreFilter(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osg::Camera* depthCamera,
373  osg::Camera* xBlurCamera, osg::Camera* yBlurCamera, osg::Texture2D* texture, osg::StateSet* stateSet, unsigned int textureUnit);
374 
376  void installPreFilter(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osg::Camera* depthCamera,
377  osg::Camera* xBlurCamera, osg::Camera* yBlurCamera, unsigned int textureLayer, osg::Texture2DArray* texture2DArray,
378  osg::Texture2DArray* preFilterTexture2DArray1, osg::Texture2DArray* preFilterTexture2DArray2);
379 
381  osg::Texture2D* createTexture2D(int width, int height, GLenum internalFormat, GLenum sourceFormat, GLenum sourceType, bool shadowComparisonMode,
382  osg::Texture2D::FilterMode minFilter = osg::Texture2D::NEAREST, osg::Texture2D::FilterMode magFilter = osg::Texture2D::NEAREST);
383 
385  osg::Texture2DArray* createTexture2DArray(int width, int height, int depth, GLenum internalFormat, GLenum sourceFormat, GLenum sourceType,
386  bool shadowComparisonMode, osg::Texture2DArray::FilterMode minFilter = osg::Texture2DArray::NEAREST, osg::Texture2DArray::FilterMode magFilter = osg::Texture2DArray::NEAREST);
387 
389  osg::Camera* createCamera(int width, int height, GLbitfield clearMask);
390 
392  osg::Geode* createFullScreenQuad(const osg::Vec2& size, const osg::Vec2& uvStart, const osg::Vec2& uvStop);
393 
395  osg::Camera* createHudCamera(int width, int height, GLbitfield clearMask);
396 
398  double computeUniformSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
399 
401  double computeLogarithmicSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
402 
404  void constructSplitLightCamera(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, CsmSplitData &csmSplit, const osg::Vec3& lightDirection,
405  const osg::Matrix& cameraViewMatrix, const osg::Matrix& cameraProjectionMatrix, double cameraMagnification);
406 
408  void setDirty(bool dirty) { _dirty = dirty; }
409  protected:
410 
413 
414  typedef std::map< const osg::Camera*, osg::ref_ptr<PerChannelData> > PerChannelDataMap;
415 
417 
418  osg::ref_ptr<osg::LightSource> myGlobalLight;
419 
420  unsigned int myTextureUnitOffset;
421 
423 
424  static const std::string myShadowCameraPrefixString;
425  static const std::string mySDSMShadowCameraPrefixString;
426  };
427 }
428 

Document ID: Generated on Mon Jul 4 01:00:18 EDT 2016 from SVN revision 166489
Copyright © 2005-2015 VT MÄK. All Rights Reserved (www.mak.com)