VR-Forces 4.3 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 myNumSplits;
262  unsigned int myAtlasWidth;
263  unsigned int myAtlasHeight;
264  unsigned int myAtlasWidthNumSplits;
266  osg::ref_ptr<osg::Uniform> myShadowAmbientFactorUniform;
271  osg::ref_ptr<osg::Camera> mySDSMCamera;
272  std::vector<osg::ref_ptr<osg::Camera> > mySDSMReduceCamera;
273  osg::ref_ptr<osg::Uniform> myFarNearSumDiffProdx2Uniform;
274  osg::ref_ptr<osg::Uniform> myMaximumViewDistanceUniform;
275  osg::ref_ptr<osg::Uniform> myMaximumDepthUniform;
276  osg::ref_ptr<osg::Uniform> myInvNumSplitsInterpUniform;
277  osg::ref_ptr<osg::Camera> mySDSMLinearizeDepthCamera;
278  osg::ref_ptr<osg::Camera> mySDSMCopyReduceDepthCamera;
279  osg::ref_ptr<osg::Camera> mySDSMPartitionCamera;
280  std::vector<osg::ref_ptr<osg::Camera> > mySDSMPartitionReduceCamera;
281  osg::ref_ptr<SdsmDrawCallback> mySDSMDrawCallback;
282  osg::ref_ptr<osg::Camera> myAtlasDepthCamera;
283  osg::ref_ptr<osg::Camera> myAtlasXBlurCamera;
284  osg::ref_ptr<osg::Camera> myAtlasYBlurCamera;
285  osg::ref_ptr<osg::Camera> myCloudShadowCamera;
286  double mySDSMTime;
287  protected:
289  virtual ~PerChannelData(){}
290  };
291 
292 
293  struct CloudShadowPreDrawCallback : public osg::Camera::DrawCallback
294  {
296  CloudShadowPreDrawCallback(DtOsgShadowTechnique& shadowTechnique, PerChannelData& perChannelData);
297 
298  // Callback Method to unbind the currently bound framebuffer object
299  virtual void operator () (osg::RenderInfo& renderInfo) const;
302  };
303 
305  virtual std::string generateVertexShader(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
306 
308  virtual std::string generateFragmentShader(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
309 
311  virtual std::string generateCloudShadowVertexShader(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
312 
314  virtual std::string generateCloudShadowFragmentShader(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
315 
317  unsigned int getFilterCount(DtShadowSettingsObject* shadowSettingObject);
318 
319  private:
320 
322  std::string generatePoissonDiscArray(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
323 
325  std::string generateHelperFunctions(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
326 
328  std::string generateFragmentComputeShadowFunction(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
329 
331  std::string generateFragmentUniformAndVariables(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
332 
334  std::string generateFragmentMain(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
335 
337  std::string generateFragmentVisualizeCascadeMain(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
338 
340  bool getCameraSettings(osg::Camera* viewCamera, PerChannelData** perChannelData, DtShadowSettingsObject** shadowSettingObject, bool* isPlanView);
341 
343  void initPerChannelData(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
344 
346  void installVirtualProgramAndSharedStateSet(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
347 
349  void packTextureAtlas(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
350 
352  void installSDSM(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
353 
355  void traverseSDSMCameras(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osgUtil::CullVisitor& cv, double cameraMagnification);
356 
358  void installCloudShadows(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
359 
361  void updateCloudShadows(osg::Camera* camera);
362 
364  void updateCloudShadows(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, const osg::Matrix& inverseViewMatrix);
365 
367  void updateCascadeImportance(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
368 
370  osg::Program* getProgramFromFile(const std::string& szVertexShader, const std::string& szFragmentShader);
371 
373  void installPreFilter(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osg::Camera* depthCamera,
374  osg::Camera* xBlurCamera, osg::Camera* yBlurCamera, osg::Texture2D* texture, osg::StateSet* stateSet, unsigned int textureUnit);
375 
377  void installPreFilter(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osg::Camera* depthCamera,
378  osg::Camera* xBlurCamera, osg::Camera* yBlurCamera, unsigned int textureLayer, osg::Texture2DArray* texture2DArray,
379  osg::Texture2DArray* preFilterTexture2DArray1, osg::Texture2DArray* preFilterTexture2DArray2);
380 
382  osg::Texture2D* createTexture2D(int width, int height, GLenum internalFormat, GLenum sourceFormat, GLenum sourceType, bool shadowComparisonMode,
383  osg::Texture2D::FilterMode minFilter = osg::Texture2D::NEAREST, osg::Texture2D::FilterMode magFilter = osg::Texture2D::NEAREST);
384 
386  osg::Texture2DArray* createTexture2DArray(int width, int height, int depth, GLenum internalFormat, GLenum sourceFormat, GLenum sourceType,
387  bool shadowComparisonMode, osg::Texture2DArray::FilterMode minFilter = osg::Texture2DArray::NEAREST, osg::Texture2DArray::FilterMode magFilter = osg::Texture2DArray::NEAREST);
388 
390  osg::Camera* createCamera(int width, int height, GLbitfield clearMask);
391 
393  osg::Geode* createFullScreenQuad(const osg::Vec2& size, const osg::Vec2& uvStart, const osg::Vec2& uvStop);
394 
396  osg::Camera* createHudCamera(int width, int height, GLbitfield clearMask);
397 
399  double computeUniformSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
400 
402  double computeLogarithmicSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
403 
405  void constructSplitLightCamera(PerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, CsmSplitData &csmSplit, const osg::Vec3& lightDirection,
406  const osg::Matrix& cameraViewMatrix, const osg::Matrix& cameraProjectionMatrix, double cameraMagnification);
407 
409  void setDirty(bool dirty) { _dirty = dirty; }
410  protected:
411 
414 
415  typedef std::map< osg::Camera*, osg::ref_ptr<PerChannelData> > PerChannelDataMap;
416 
418 
419  osg::ref_ptr<osg::LightSource> myGlobalLight;
420 
421  unsigned int myTextureUnitOffset;
422 
424 
425  static const std::string myShadowCameraPrefixString;
426  static const std::string mySDSMShadowCameraPrefixString;
427  };
428 }
429 

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)