VR-Forces 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) 2025 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
9 
10 #pragma once
11 
12 #include <vrvOsg/vrvOsg.h>
13 
15 
16 #include <vrvUtil/signalslib.h>
17 
18 #include <osgEarth/VirtualProgram>
19 
20 #include <osgShadow/ShadowTechnique>
21 
22 #include <osg/StateSet>
23 #include <osg/Camera>
24 #include <osg/PolygonOffset>
25 #include <osg/FrameBufferObject>
26 #include <osg/Uniform>
27 #include <osg/LightSource>
28 #include <osg/RenderInfo>
29 #include <osg/Vec2>
30 #include <osg/Vec3>
31 #include <osg/Matrix>
32 #include <osg/MatrixTransform>
33 #include <osg/Program>
34 #include <osg/Texture2D>
35 #include <osg/Texture2DArray>
36 #include <osg/Geode>
37 #include <osg/Geometry>
38 #include <osg/GL>
39 
40 #include <vector>
41 #include <map>
42 #include <sstream>
43 #include <string>
44 
45 #include <tbb/mutex.h>
46 
47 namespace makVrv
48 {
49 
50  class DtShadowTweakables;
51  class DtChannel;
52  class DtOsgChannel;
53  class DtShadowSettingsObject;
54  class TextureApplyAttribute;
55  class DtOsgShadowTechnique;
56  struct DtCurrentPassData;
57  struct DtPerChannelData;
58  class DtSkyObjectManager;
59 
62  class DT_DLL_VRVOSG DtOsgShadowTechnique : public osgShadow::ShadowTechnique
63  {
64  public:
65 
68 
70  DtOsgShadowTechnique(const DtOsgShadowTechnique& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
71 
73  virtual osg::Object* cloneType() const { return new DtOsgShadowTechnique(myDe); }
74 
76  virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new DtOsgShadowTechnique (*this, copyop); }
77 
79  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const DtOsgShadowTechnique *>(obj)!=NULL; }
80 
82  virtual const char* libraryName() const { return "makVrv"; }
83 
85  virtual const char* className() const { return "DtOsgShadowTechnique"; }
86 
88  virtual void init();
89 
91  virtual void postConstructionInit();
92 
94  virtual void update(osg::NodeVisitor& nv);
95 
97  virtual void cull(osgUtil::CullVisitor& cv);
98 
100  virtual void cleanSceneGraph();
101 
103  void setChannelShadowState(DtOsgChannel* osgChannel, bool enable);
104 
106  bool getChannelShadowSettings(DtOsgChannel* osgChannel, DtShadowSettingsObject** shadowSettingObject, bool* isPlanView);
107 
109  void setGlobalLight(osg::LightSource* light) { myGlobalLight = light; }
110 
112  const osg::LightSource* getGlobalLight() const { return myGlobalLight.get(); }
113 
114  static const std::string& shadowCameraPrefixString(void);
115 
117  const osg::StateSet * mapCameraToStateSet(const osg::Camera* camera) const;
118 
120 
122  virtual void resizeGLObjectBuffers(unsigned int maxSize) override;
123 
125  virtual void releaseGLObjects(osg::State* = 0) const override;
126  protected:
128  virtual void reInitialize(unsigned int quality);
129 
132  virtual void cleanupOldPerChannelData();
133 
134  virtual bool configureShadowTraversalNodeMask(bool traverseEntities, bool traverseProps, bool traverseTerrain);
135 
136  virtual void slot_refresh_maps();
137  public:
138  // per window info
139  static const int MAX_NUM_CASCADES = 6;
140 
141  protected:
142 
144  virtual std::string generateVertexShader(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
145 
147  virtual std::string generateFragmentShader(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
148 
150  unsigned int getFilterCount(DtShadowSettingsObject* shadowSettingObject);
151 
154  bool getReceiveShadows(const DtShadowSettingsObject* shadowSettings) const;
155 
165  virtual void computeFrustumPoints(const osg::Matrix& projectionMat, bool reverseZ, const osg::Matrix& inverseViewMat,
166  double nearPlane, double farPlane, osg::Vec3d* outFrustumPoints);
167 
169  virtual void chooseComputeShadowFunction(std::stringstream& sstr,
170  DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettings);
171 
172  private:
173 
175  std::string generateFragmentComputeShadowFunction(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
176 
178  std::string generateFragmentComputeShadowFunctionEsm(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
179 
181  std::string generateFragmentComputeShadowFunctionVsm(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
182 
184  std::string generateFragmentUniformAndVariables(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
185 
187  std::string generateFragmentMain(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
188 
190  bool getCameraSettings(osg::Camera* viewCamera, DtPerChannelData** perChannelData, DtShadowSettingsObject** shadowSettingObject, bool* isPlanView);
191 
193  void initPerChannelData(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
194 
195 
197  void installReceiverVPandSharedStateSet(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
198 
199  void triggerExternalModuleShaderRefresh();
200 
202  void updateCascadeImportance(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
203 
205  osg::Program* getProgramFromFile(const std::string& szVertexShader, const std::string& szFragmentShader);
206 
208  void installPreFilter(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osg::Camera* depthCamera,
209  osg::Camera* xBlurCamera, osg::Camera* yBlurCamera, osg::Texture2DArray* texture, osg::StateSet* stateSet, unsigned int textureUnit);
210 
212  void installPreFilter(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject, osg::Camera* depthCamera,
213  osg::Camera* xBlurCamera, osg::Camera* yBlurCamera, unsigned int textureLayer, osg::Texture2DArray* texture2DArray,
214  osg::Texture2DArray* preFilterTexture2DArray1, osg::Texture2DArray* preFilterTexture2DArray2);
215 
216  void setupReverseZBuffer(osg::Camera* depthCamera, osg::Camera* xBlurCamera, osg::Camera* yBlurCamera);
217  void setupBlurCamera(osg::Camera* camera, osg::ref_ptr<osg::Program> &blurProgram, const osg::Vec2 & direction);
218 
220  osg::Texture2D* createTexture2D(int width, int height, GLenum internalFormat, GLenum sourceFormat, GLenum sourceType, bool shadowComparisonMode,
221  osg::Texture2D::FilterMode minFilter = osg::Texture2D::NEAREST, osg::Texture2D::FilterMode magFilter = osg::Texture2D::NEAREST);
222 
224  osg::Texture2DArray* createTexture2DArray(int width, int height, int depth, GLenum internalFormat, GLenum sourceFormat, GLenum sourceType,
225  bool shadowComparisonMode, osg::Texture2DArray::FilterMode minFilter = osg::Texture2DArray::NEAREST, osg::Texture2DArray::FilterMode magFilter = osg::Texture2DArray::NEAREST);
226 
228  osg::Camera* createCamera(int width, int height, GLbitfield clearMask);
229 
231  osg::Geode* createFullScreenQuad(const osg::Vec2& size, const osg::Vec2& uvStart, const osg::Vec2& uvStop);
232 
234  osg::Camera* createHudCamera(int width, int height, GLbitfield clearMask, const char * name = NULL);
235 
237  double computeUniformSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
238 
240  double computeLogarithmicSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
241 
243  {
244  osg::Matrix myViewMatrix;
245  osg::Matrix myProjectionMatrix;
247  };
249  CameraDescription constructSplitLightCamera(const DtCurrentPassData & d, int split_index, bool farCascade = false);
250 
251  void updateCameras(const DtCurrentPassData & d);
252 
253  void getSplitCameraDistances(const DtCurrentPassData & d, int split_index, double &splitCamNear, double &splitCamFar);
254 
256  void setDirty(bool dirty) { _dirty = dirty; }
257 
258  // not really used yet.
259  void frustumBoundingBoxLightViewSpace(const DtCurrentPassData & d, int split_index/*for_debug visualization*/, const osg::Matrix & lightViewMatrix,
260  double nearPlane, double farPlane, osg::Vec3d& minFrustum, osg::Vec3d& maxFrustum);
261 
262  friend struct DtCurrentPassData;
263 
264  protected:
265 
267  virtual ~DtOsgShadowTechnique();
268 
269  typedef std::map< const osg::Camera*, osg::ref_ptr<DtPerChannelData> > PerChannelDataMap;
270  typedef std::vector< osg::ref_ptr<DtPerChannelData> > PerChannelDataVector;
271 
274 
278 
280 
281  static const std::string myShadowCameraPrefixString;
282 
285 
288 
291 
294 
295  // Avoid camera deletion (and resulting GL calls) in cull thread.
296  std::vector<osg::ref_ptr<osg::Camera> > myDeferredDeleteCamera;
297 
298  // Protect myDeferredDeleteCamera
300 
301  bool myMulticastIsSupported; // true if multicast extension is supported
302  bool myShadingRateIsSupported; // true if shading rate extension is supported
303 
305 
306  boost::signalslib::connection myTerrainPatchAddedConnection;
307  boost::signalslib::connection myTerrainPatchDeletedConnection;
308 
309  };
310 }
311 
DT_DLL_EXAMPLESCENEROOTPLUGIN void init(DtDe &anIG)
static const std::string myShadowCameraPrefixString
Definition: DtOsgShadowTechnique.h:281
void setDirty(bool dirty)
Sets dirty for shadow technique&#39;s to true or false.
Definition: DtOsgShadowTechnique.h:256
void setGlobalLight(osg::LightSource *light)
Sets global light source.
Definition: DtOsgShadowTechnique.h:109
Definition: DtShadowSettingsObject.h:24
osg::ref_ptr< osg::Program > myVsmBlurTextureProgram
Definition: DtOsgShadowTechnique.h:283
Definition: DtOsgShadowTechnique.h:242
osg::ref_ptr< osg::Program > myVsmPassthroughArrayProgram
Definition: DtOsgShadowTechnique.h:286
osg::ref_ptr< osg::Program > myVsmPassthroughProgram
Definition: DtOsgShadowTechnique.h:287
osg::ref_ptr< osg::Program > myEsmBlurTexArrayProgram
Definition: DtOsgShadowTechnique.h:290
#define DT_DLL_VRVOSG
Definition: vrvOsg.h:23
PerChannelDataMap myPerChannelDataMap
Definition: DtOsgShadowTechnique.h:272
boost::signalslib::connection myTerrainPatchAddedConnection
Definition: DtOsgShadowTechnique.h:306
voidpf void uLong size
Definition: ioapi.h:39
osg::ref_ptr< osg::Program > myVsmBlurTexArrayProgram
Definition: DtOsgShadowTechnique.h:284
static const int MAX_NUM_CASCADES
Definition: DtPerChannelData.h:47
Definition: DtSkyObjectManager.h:30
Contains the DtOsgTextureApplyAttribute class.
DtDe & myDe
Definition: DtOsgShadowTechnique.h:279
osg::ref_ptr< osg::Program > myEVsmBlurTextureProgram
Definition: DtOsgShadowTechnique.h:292
osg::Matrix myProjectionMatrix
Definition: DtOsgShadowTechnique.h:245
This class provides the OSG-specific implementation of a DtChannel.
Definition: DtOsgChannel.h:58
osg::ref_ptr< osg::Uniform > myReceiveShadowsUniform
Definition: DtOsgShadowTechnique.h:277
virtual const char * libraryName() const
Returns the namespace string.
Definition: DtOsgShadowTechnique.h:82
bool myMulticastIsSupported
Definition: DtOsgShadowTechnique.h:301
std::vector< osg::ref_ptr< DtPerChannelData > > PerChannelDataVector
Definition: DtOsgShadowTechnique.h:270
virtual osg::Object * cloneType() const
Creates a new object of Type DtOsgShadowTechnique.
Definition: DtOsgShadowTechnique.h:73
class DtShadowTweakables * myShadowUI
Definition: DtOsgShadowTechnique.h:119
Definition: DtCurrentPassData.h:29
Definition: DtPerChannelData.h:49
osg::ref_ptr< osg::Program > myEsmBlurTextureProgram
Definition: DtOsgShadowTechnique.h:289
osg::Matrix myViewMatrix
Definition: DtOsgShadowTechnique.h:244
Definition: DtShadowTweakables.h:21
std::vector< osg::ref_ptr< osg::Camera > > myDeferredDeleteCamera
Definition: DtOsgShadowTechnique.h:296
PerChannelDataVector myPerChannelDataDeleteList
Definition: DtOsgShadowTechnique.h:273
The DtOsgShadowTechnique class implements a robust shadow mapping system.
Definition: DtOsgShadowTechnique.h:62
DtSkyObjectManager & mySkyObjectManager
Definition: DtOsgShadowTechnique.h:304
osg::ref_ptr< osg::Program > myEVsmBlurTexArrayProgram
Definition: DtOsgShadowTechnique.h:293
unsigned int GLenum
Definition: DtGlTextureBufferObject.h:19
const osg::LightSource * getGlobalLight() const
Gets global light source.
Definition: DtOsgShadowTechnique.h:112
virtual bool isSameKindAs(const osg::Object *obj) const
Returns true if object is of the DtOsgShadowTechnique class. Otherwise, returns false.
Definition: DtOsgShadowTechnique.h:79
std::map< const osg::Camera *, osg::ref_ptr< DtPerChannelData > > PerChannelDataMap
Definition: DtOsgShadowTechnique.h:269
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:76
osg::Matrix myTextureViewProjectionMatrix
Definition: DtOsgShadowTechnique.h:246
bool myShadingRateIsSupported
Definition: DtOsgShadowTechnique.h:302
osg::ref_ptr< osg::LightSource > myGlobalLight
Definition: DtOsgShadowTechnique.h:275
osg::ref_ptr< osg::Uniform > myLightDirectionUniform
Definition: DtOsgShadowTechnique.h:276
tbb::mutex myDeferredCameraDeletionMutex
Definition: DtOsgShadowTechnique.h:299
boost::signalslib::connection myTerrainPatchDeletedConnection
Definition: DtOsgShadowTechnique.h:307
virtual osg::Object * clone(const osg::CopyOp &copyop) const
Creates a new copy of DtOsgShadowTechnique object.
Definition: DtOsgShadowTechnique.h:76
virtual const char * className() const
Returns the class name string.
Definition: DtOsgShadowTechnique.h:85

Document ID: Generated on Thu Oct 23 22:29:17 EDT 2025 from SVN revision 280951
Copyright © 2005-2024 MAK Technologies. All Rights Reserved (www.mak.com)