VR-Forces 5.0.1 Developer's Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtOsgShadowTechnique.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2020 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <vrvOsg/vrvOsg.h>
13 
16 
17 #include <osgEarth/VirtualProgram>
18 
19 #include <osgShadow/ShadowTechnique>
20 
21 #include <osg/StateSet>
22 #include <osg/Camera>
23 #include <osg/PolygonOffset>
24 #include <osg/FrameBufferObject>
25 #include <osg/Uniform>
26 #include <osg/LightSource>
27 #include <osg/RenderInfo>
28 #include <osg/Vec2>
29 #include <osg/Vec3>
30 #include <osg/Matrix>
31 #include <osg/MatrixTransform>
32 #include <osg/Program>
33 #include <osg/Texture2D>
34 #include <osg/Texture2DArray>
35 #include <osg/Geode>
36 #include <osg/Geometry>
37 #include <osg/GL>
38 
39 #include <vector>
40 #include <map>
41 #include <string>
42 
43 #include <tbb/mutex.h>
44 
45 namespace makVrv
46 {
47 
48  class DtShadowTweakables;
49  class DtChannel;
50  class DtOsgChannel;
51  class DtShadowSettingsObject;
52  class TextureApplyAttribute;
53  class DtOsgShadowTechnique;
54  struct DtCurrentPassData;
55  struct DtPerChannelData;
56 
59  class DT_DLL_VRVOSG DtOsgShadowTechnique : public osgShadow::ShadowTechnique
60  {
61  public:
62 
65 
67  DtOsgShadowTechnique(const DtOsgShadowTechnique& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
68 
70  virtual osg::Object* cloneType() const { return new DtOsgShadowTechnique(myDe); }
71 
73  virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new DtOsgShadowTechnique (*this, copyop); }
74 
76  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const DtOsgShadowTechnique *>(obj)!=NULL; }
77 
79  virtual const char* libraryName() const { return "makVrv"; }
80 
82  virtual const char* className() const { return "DtOsgShadowTechnique"; }
83 
85  virtual void init();
86 
88  virtual void postConstructionInit();
89 
91  virtual void update(osg::NodeVisitor& nv);
92 
94  virtual void cull(osgUtil::CullVisitor& cv);
95 
97  virtual void cleanSceneGraph();
98 
100  void setChannelShadowState(DtOsgChannel* osgChannel, bool enable);
101 
103  bool getChannelShadowSettings(DtOsgChannel* osgChannel, DtShadowSettingsObject** shadowSettingObject, bool* isPlanView);
104 
106  bool getCameraCloudShadowMap(osg::Camera* camera, unsigned int* textureIndex, osg::Matrix* textureViewProjMatrix,
107  unsigned int* textureIndex2, osg::Matrix* textureViewProjMatrix2);
108 
110  void setGlobalLight(osg::LightSource* light) { myGlobalLight = light; }
111 
113  const osg::LightSource* getGlobalLight() const { return myGlobalLight.get(); }
114 
115  static const std::string& shadowCameraPrefixString(void);
116  static const std::string& shadowSDSMCameraPrefixString(void);
117 
119  const osg::StateSet * mapCameraToStateSet(const osg::Camera* camera) const;
120 
122 
124  virtual void resizeGLObjectBuffers(unsigned int maxSize) override;
125 
127  virtual void releaseGLObjects(osg::State* = 0) const override;
128  protected:
130  virtual void reInitialize(unsigned int quality);
131 
134  virtual void cleanupOldPerChannelData();
135 
136  virtual bool configureShadowTraversalNodeMask(bool traverseEntities, bool traverseProps, bool traverseTerrain);
137 
138  virtual void slot_refresh_maps();
139  public:
140  // per window info
141  static const int MAX_NUM_CASCADES = 6;
142 
143  protected:
144 
146  virtual std::string generateVertexShader(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
147 
149  virtual std::string generateFragmentShader(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
150 
152  virtual std::string generateCloudShadowVertexShader(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
153 
154 
156  unsigned int getFilterCount(DtShadowSettingsObject* shadowSettingObject);
157 
160  bool getReceiveShadows(const DtShadowSettingsObject* shadowSettings) const;
161 
162  private:
163 
165  std::string generateFragmentComputeShadowFunction(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
166 
168  std::string generateFragmentUniformAndVariables(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
169 
171  std::string generateFragmentMain(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
172 
174  bool getCameraSettings(osg::Camera* viewCamera, DtPerChannelData** perChannelData, DtShadowSettingsObject** shadowSettingObject, bool* isPlanView);
175 
177  void initPerChannelData(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
178 
179 
181  void installReceiverVPandSharedStateSet(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
182 
183  void triggerExternalModuleShaderRefresh();
184 
185 
187  void installSDSM(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
188 
190  void traverseSDSMCameras( DtCurrentPassData & d, osgUtil::CullVisitor& cv);
191 
193  void installCloudShadows(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
194 
196  void updateCloudShadows(osg::RenderInfo& renderInfo, osg::Camera* shadowCamera);
197 
199  void updateCascadeImportance(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
200 
202  osg::Program* getProgramFromFile(const std::string& szVertexShader, const std::string& szFragmentShader);
203 
205  void installPreFilter(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osg::Camera* depthCamera,
206  osg::Camera* xBlurCamera, osg::Camera* yBlurCamera, osg::Texture2DArray* texture, osg::StateSet* stateSet, unsigned int textureUnit);
207 
209  void installPreFilter(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osg::Camera* depthCamera,
210  osg::Camera* xBlurCamera, osg::Camera* yBlurCamera, unsigned int textureLayer, osg::Texture2DArray* texture2DArray,
211  osg::Texture2DArray* preFilterTexture2DArray1, osg::Texture2DArray* preFilterTexture2DArray2);
212 
213  void setupReverseZBuffer(osg::Camera* depthCamera, osg::Camera* xBlurCamera, osg::Camera* yBlurCamera);
214  void setupBlurCamera(osg::Camera* camera, osg::ref_ptr<osg::Program> &blurProgram, const osg::Vec2 & direction);
215 
217  osg::Texture2D* createTexture2D(int width, int height, GLenum internalFormat, GLenum sourceFormat, GLenum sourceType, bool shadowComparisonMode,
218  osg::Texture2D::FilterMode minFilter = osg::Texture2D::NEAREST, osg::Texture2D::FilterMode magFilter = osg::Texture2D::NEAREST);
219 
221  osg::Texture2DArray* createTexture2DArray(int width, int height, int depth, GLenum internalFormat, GLenum sourceFormat, GLenum sourceType,
222  bool shadowComparisonMode, osg::Texture2DArray::FilterMode minFilter = osg::Texture2DArray::NEAREST, osg::Texture2DArray::FilterMode magFilter = osg::Texture2DArray::NEAREST);
223 
225  osg::Camera* createCamera(int width, int height, GLbitfield clearMask);
226 
228  osg::Geode* createFullScreenQuad(const osg::Vec2& size, const osg::Vec2& uvStart, const osg::Vec2& uvStop);
229 
231  osg::Camera* createHudCamera(int width, int height, GLbitfield clearMask, const char * name = NULL);
232 
234  double computeUniformSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
235 
237  double computeLogarithmicSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
238 
240  {
241  osg::Matrix myViewMatrix;
242  osg::Matrix myProjectionMatrix;
244  };
246  CameraDescription constructSplitLightCamera(const DtCurrentPassData & d, int split_index, bool farCascade = false);
247 
248  void updateCameras(const DtCurrentPassData & d);
249 
250  void getSplitCameraDistances(const DtCurrentPassData & d, int split_index, double &splitCamNear, double &splitCamFar);
251 
253  void setDirty(bool dirty) { _dirty = dirty; }
254 
255  // not really used yet.
256  void frustumBoundingBoxLightViewSpace(const DtCurrentPassData & d, int split_index/*for_debug visualization*/, const osg::Matrix & lightViewMatrix,
257  double nearPlane, double farPlane, osg::Vec3d& minFrustum, osg::Vec3d& maxFrustum);
258 
259  friend struct DtCurrentPassData;
261 
262  protected:
263 
265  virtual ~DtOsgShadowTechnique();
266 
267  typedef std::map< const osg::Camera*, osg::ref_ptr<DtPerChannelData> > PerChannelDataMap;
268  typedef std::vector< osg::ref_ptr<DtPerChannelData> > PerChannelDataVector;
269 
272 
276 
278 
279  static const std::string myShadowCameraPrefixString;
280  static const std::string mySDSMShadowCameraPrefixString;
281 
284 
287 
290 
293 
294  // Avoid camera deletion (and resulting GL calls) in cull thread.
295  std::vector<osg::ref_ptr<osg::Camera> > myDeferredDeleteCamera;
296 
297  // Protect myDeferredDeleteCamera
299 
300  bool myMulticastIsSupported; // true if multicast extension is supported
301  bool myShadingRateIsSupported; // true if shading rate extension is supported
302  };
303 }
304 
DT_DLL_EXAMPLESCENEROOTPLUGIN void init(DtDe &anIG)
static const std::string myShadowCameraPrefixString
Definition: DtOsgShadowTechnique.h:279
void setDirty(bool dirty)
Sets dirty for shadow technique&#39;s to true or false.
Definition: DtOsgShadowTechnique.h:253
void setGlobalLight(osg::LightSource *light)
Sets global light source.
Definition: DtOsgShadowTechnique.h:110
Definition: DtShadowSettingsObject.h:24
osg::ref_ptr< osg::Program > myVsmBlurTextureProgram
Definition: DtOsgShadowTechnique.h:282
Definition: DtOsgShadowTechnique.h:239
osg::ref_ptr< osg::Program > myVsmPassthroughArrayProgram
Definition: DtOsgShadowTechnique.h:285
osg::ref_ptr< osg::Program > myVsmPassthroughProgram
Definition: DtOsgShadowTechnique.h:286
Contains makVrv::DtSdsmDrawCallback class.
static const std::string mySDSMShadowCameraPrefixString
Definition: DtOsgShadowTechnique.h:280
osg::ref_ptr< osg::Program > myEsmBlurTexArrayProgram
Definition: DtOsgShadowTechnique.h:289
#define DT_DLL_VRVOSG
Definition: vrvOsg.h:23
PerChannelDataMap myPerChannelDataMap
Definition: DtOsgShadowTechnique.h:270
voidpf void uLong size
Definition: ioapi.h:39
osg::ref_ptr< osg::Program > myVsmBlurTexArrayProgram
Definition: DtOsgShadowTechnique.h:283
static const int MAX_NUM_CASCADES
Definition: DtPerChannelData.h:47
Contains the DtOsgTextureApplyAttribute class.
DtDe & myDe
Definition: DtOsgShadowTechnique.h:277
osg::ref_ptr< osg::Program > myEVsmBlurTextureProgram
Definition: DtOsgShadowTechnique.h:291
osg::Matrix myProjectionMatrix
Definition: DtOsgShadowTechnique.h:242
This class provides the OSG-specific implementation of a DtChannel.
Definition: DtOsgChannel.h:52
osg::ref_ptr< osg::Uniform > myReceiveShadowsUniform
Definition: DtOsgShadowTechnique.h:275
virtual const char * libraryName() const
Returns the namespace string.
Definition: DtOsgShadowTechnique.h:79
bool myMulticastIsSupported
Definition: DtOsgShadowTechnique.h:300
std::vector< osg::ref_ptr< DtPerChannelData > > PerChannelDataVector
Definition: DtOsgShadowTechnique.h:268
virtual osg::Object * cloneType() const
Creates a new object of Type DtOsgShadowTechnique.
Definition: DtOsgShadowTechnique.h:70
class DtShadowTweakables * myShadowUI
Definition: DtOsgShadowTechnique.h:121
Definition: DtCurrentPassData.h:29
Definition: DtPerChannelData.h:49
osg::ref_ptr< osg::Program > myEsmBlurTextureProgram
Definition: DtOsgShadowTechnique.h:288
osg::Matrix myViewMatrix
Definition: DtOsgShadowTechnique.h:241
Definition: DtShadowTweakables.h:21
std::vector< osg::ref_ptr< osg::Camera > > myDeferredDeleteCamera
Definition: DtOsgShadowTechnique.h:295
PerChannelDataVector myPerChannelDataDeleteList
Definition: DtOsgShadowTechnique.h:271
The DtOsgShadowTechnique class implements a robust shadow mapping system.
Definition: DtOsgShadowTechnique.h:59
osg::ref_ptr< osg::Program > myEVsmBlurTexArrayProgram
Definition: DtOsgShadowTechnique.h:292
Definition: DtCloudShadowPreDrawCallback.h:25
unsigned int GLenum
Definition: DtGlTextureBufferObject.h:18
const osg::LightSource * getGlobalLight() const
Gets global light source.
Definition: DtOsgShadowTechnique.h:113
virtual bool isSameKindAs(const osg::Object *obj) const
Returns true if object is of the DtOsgShadowTechnique class. Otherwise, returns false.
Definition: DtOsgShadowTechnique.h:76
std::map< const osg::Camera *, osg::ref_ptr< DtPerChannelData > > PerChannelDataMap
Definition: DtOsgShadowTechnique.h:267
Contains DLL export macros.
The DtDe is the core component of any VR-Vantage application. It owns a DtRenderer, DtDeCommunicator, as well as a DtDisplay and other things.
Definition: DtDe.h:70
osg::Matrix myTextureViewProjectionMatrix
Definition: DtOsgShadowTechnique.h:243
bool myShadingRateIsSupported
Definition: DtOsgShadowTechnique.h:301
osg::ref_ptr< osg::LightSource > myGlobalLight
Definition: DtOsgShadowTechnique.h:273
osg::ref_ptr< osg::Uniform > myLightDirectionUniform
Definition: DtOsgShadowTechnique.h:274
tbb::mutex myDeferredCameraDeletionMutex
Definition: DtOsgShadowTechnique.h:298
virtual osg::Object * clone(const osg::CopyOp &copyop) const
Creates a new copy of DtOsgShadowTechnique object.
Definition: DtOsgShadowTechnique.h:73
virtual const char * className() const
Returns the class name string.
Definition: DtOsgShadowTechnique.h:82

Document ID: Generated on Mon Jun 20 00:38:30 EDT 2022 from SVN revision 244029
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)