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) 2026 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 class DtCVMarkerSeries;
48 
49 namespace makVrv
50 {
51  class DtChannel;
52  class DtOsgChannel;
53  class DtOsgRenderer;
54  class DtOsgShaderFilesCache;
55  class DtShadowSettingsObject;
56  class DtOsgShadowTechnique;
57  class DtShadowTweakables;
58  class DtSkyObjectManager;
59  class TextureApplyAttribute;
60 
61  struct DtCascadeData;
62  class DtPerChannelData;
63  struct DtCurrentPassData;
64 
87  class DT_DLL_VRVOSG DtOsgShadowTechnique : public osgShadow::ShadowTechnique
88  {
89  public:
90 
93 
95  DtOsgShadowTechnique(const DtOsgShadowTechnique& es, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
96 
98  virtual osg::Object* cloneType() const { return new DtOsgShadowTechnique(myDe); }
99 
101  virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new DtOsgShadowTechnique (*this, copyop); }
102 
104  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const DtOsgShadowTechnique *>(obj)!=nullptr; }
105 
107  virtual const char* libraryName() const { return "makVrv"; }
108 
110  virtual const char* className() const { return "DtOsgShadowTechnique"; }
111 
113  virtual void init();
114 
116  virtual void postConstructionInit();
117 
119  virtual void update(osg::NodeVisitor& nv);
120 
122  virtual void cull(osgUtil::CullVisitor& cv);
123 
125  virtual void cleanSceneGraph();
126 
128  void setChannelShadowState(DtOsgChannel* osgChannel, bool enable);
129 
131  bool getChannelShadowSettings(DtOsgChannel* osgChannel, DtShadowSettingsObject** shadowSettingObject, bool* isPlanView);
132 
134  void setGlobalLight(osg::LightSource* light);
135 
137  const osg::LightSource* getGlobalLight() const;
138 
139  static const std::string& shadowCameraPrefixString();
140 
142  const osg::StateSet * mapCameraToStateSet(const osg::Camera* camera) const;
143 
145  virtual void resizeGLObjectBuffers(unsigned int maxSize) override;
146 
148  virtual void releaseGLObjects(osg::State* = 0) const override;
149 
150  protected:
151 
153  virtual ~DtOsgShadowTechnique();
154 
156  virtual void reInitialize(unsigned int quality);
157 
160  virtual void cleanupOldPerChannelData();
161 
162  virtual bool configureShadowTraversalNodeMask(bool traverseEntities, bool traverseProps, bool traverseTerrain);
163 
164  virtual void slot_refresh_maps();
165 
167  unsigned int getFilterCount(DtShadowSettingsObject* shadowSettingObject);
168 
171  bool getReceiveShadows(const DtShadowSettingsObject* shadowSettings) const;
172 
182  virtual void computeFrustumPoints(const osg::Matrix& projectionMat, bool reverseZ, const osg::Matrix& inverseViewMat,
183  double nearPlane, double farPlane, osg::Vec3d* outFrustumPoints);
184 
191  virtual const osg::Vec3 computeLightDirection(osg::Camera* viewCamera) const;
192 
203  virtual void updateSplitDistancesAndUniforms(DtPerChannelData* perChannelData,
204  DtShadowSettingsObject* shadowSettings, const DtCurrentPassData& currentPassData);
205 
220  virtual void updateShadowUniformsAndFilters(DtPerChannelData* perChannelData,
221  DtShadowSettingsObject* shadowSettings, osg::Camera* viewCamera, DtCurrentPassData& currentPassData,
222  bool& bRefreshFarCascade, unsigned int& unbindingIndex);
223 
241  virtual void updateAndRenderCascade(unsigned int splitIndex, DtCurrentPassData& currentPassData,
242  DtPerChannelData* perChannelData, const DtShadowSettingsObject* shadowSettings,
243  const osg::Camera* viewCamera, osgUtil::CullVisitor& cv, unsigned int& unbindingIndex,
244  bool bRefreshFarCascade, DtCVMarkerSeries& series, int& cascades_rendered);
245 
247  virtual void updateShadowOverlayGui(const DtPerChannelData* perChannelData,
248  const DtCurrentPassData& currentPassData, int cascadesRendered);
249 
251  virtual void updateOsgEarthUniforms(const osg::Camera* viewCamera, osg::Camera* cascadeCamera);
252 
253  private:
254 
256  int farShadowMapCascadeSize() const;
257 
259  bool getCameraSettings(osg::Camera* viewCamera, DtPerChannelData** perChannelData,
260  DtShadowSettingsObject** shadowSettingObject, bool* isPlanView);
261 
263  void initPerChannelData(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
264 
268  void setupMulticastMask(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettings);
269 
275  bool initShadowSplitCamera(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettings,
276  unsigned int numberOfSplits, const osg::ref_ptr<osg::Texture2DArray>& depthTexture2DArray,
277  const osg::ref_ptr<osg::Texture2DArray>& depthTextureFarCascade, const osg::ref_ptr<osg::Texture2DArray>& textureColor2DArray,
278  const osg::ref_ptr<osg::Texture2DArray>& textureColor2DFarCascade, DtCascadeData& cascade);
279 
284  osg::StateSet* setupShadowCamera(DtCascadeData& cascade, DtPerChannelData* perChannelData,
285  DtShadowSettingsObject* shadowSettings, bool farCascade, unsigned int splitIndex, int farCascadeSize,
286  const osg::ref_ptr<osg::Texture2DArray>& depthTexture2DArray,
287  const osg::ref_ptr<osg::Texture2DArray>& depthTextureFarCascade,
288  const osg::ref_ptr<osg::Texture2DArray>& textureColor2DArray,
289  const osg::ref_ptr<osg::Texture2DArray>& textureColor2DFarCascade);
290 
295  void setupShadowSplitStateSet(osg::StateSet* cameraStateSet, DtCascadeData& cascade,
296  DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettings, unsigned int splitIndex);
297 
299  void initShadowTextures(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettings,
300  osg::ref_ptr<osg::Texture2DArray>& depthTexture2DArray, osg::ref_ptr<osg::Texture2DArray>& depthTextureFarCascade,
301  osg::ref_ptr<osg::Texture2DArray>& textureColor2DArray, osg::ref_ptr<osg::Texture2DArray>& textureColor2DFarCascade,
302  osg::ref_ptr<osg::Texture2D>& depthTexture2DAtlas, osg::ref_ptr<osg::Texture2D>& debugColor2DAtlas);
303 
305  void initDebugVisualizers(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettings,
306  DtOsgRenderer* osgRenderer, const osg::ref_ptr<osg::Texture2DArray>& textureColor2DArray,
307  const osg::ref_ptr<osg::Texture2DArray>& textureColor2DFarCascade, const osg::ref_ptr<osg::Texture2DArray>& depthTextureFarCascade,
308  const osg::ref_ptr<osg::Texture2D>& depthTexture2DAtlas, const osg::ref_ptr<osg::Texture2D>& debugColor2DAtlas);
309 
311  void installReceiverVPandSharedStateSet(DtPerChannelData* perChannelData, DtShadowSettingsObject* shadowSettingObject);
312 
314  void createPreProcessorDefines(std::string& defines, osg::StateSet& stateSet, DtShadowSettingsObject* shadowSettings);
315 
316  void triggerExternalModuleShaderRefresh();
317 
319  osg::Program* getProgramFromFile(const std::string& szVertexShader, const std::string& szFragmentShader);
320 
322  osg::Texture2DArray* createTexture2DArray(int width, int height, int depth, GLenum internalFormat, GLenum sourceFormat,
323  GLenum sourceType, bool shadowComparisonMode, osg::Texture2DArray::FilterMode minFilter = osg::Texture2DArray::NEAREST,
324  osg::Texture2DArray::FilterMode magFilter = osg::Texture2DArray::NEAREST);
325 
327  osg::Camera* createCamera(int width, int height, GLbitfield clearMask);
328 
330  osg::Geode* createFullScreenQuad(const osg::Vec2& size, const osg::Vec2& uvStart, const osg::Vec2& uvStop);
331 
333  osg::Camera* createHudCamera(int width, int height, GLbitfield clearMask, const char * name = nullptr);
334 
336  double computeUniformSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
337 
339  double computeLogarithmicSplit(double nearDistance, double farDistance, unsigned int splitIndex, unsigned int numSplits);
340 
352  osg::Matrix buildShadowBiasMatrix(bool useReverseZ, const osg::Vec2f& scaleFactor) const;
353 
355  {
356  osg::Matrix myViewMatrix;
357  osg::Matrix myProjectionMatrix;
359  };
361  CameraDescription constructSplitLightCamera(const DtCurrentPassData& d, int split_index, bool farCascade = false);
362 
363  void updateCameras(const DtCurrentPassData& d);
364 
365  void getSplitCameraDistances(const DtCurrentPassData& d, int split_index, double& splitCamNear, double& splitCamFar);
366 
368  void setDirty(bool dirty) { _dirty = dirty; }
369 
370  // not really used yet.
371  void frustumBoundingBoxLightViewSpace(const DtCurrentPassData& d, int split_index/*for_debug visualization*/,
372  const osg::Matrix& lightViewMatrix, double nearPlane, double farPlane, osg::Vec3d& minFrustum, osg::Vec3d& maxFrustum);
373 
383  osg::Matrix computeExactLightViewMatrix(const osg::Vec3d& targetPos, const DtCurrentPassData& d,
384  double maxCasterDistance, osg::Vec3d* outLightPos = nullptr) const;
385 
396  void computeFrustumCornerRays(const osg::Matrix& projectionMat, bool reverseZ,
397  const osg::Matrix& inverseViewMat, osg::Vec3d outRays[4]) const;
398 
414  void computeCascadeCenterAndRadius(const DtCurrentPassData& d, const osg::Vec3d rays[4], double nearPlane,
415  double farPlane, osg::Vec3d& outCenter, double& outRadius) const;
416 
433  void quantizeCascadeCenter(const osg::Vec3d& inCenter, double radius, int shadowMapSize,
434  const osg::Vec3d& lightCamUp, const osg::Vec3d& lightCamSide, const osg::Vec3d& lightDirection,
435  osg::Vec3d& outCenter) const;
436 
439  {
440  osg::Vec3d myFrustumMidpoint;
441  osg::Vec3d myFrustumMin;
442  osg::Vec3d myFrustumMax;
443  int myShadowMapSize = 0;
444 
446  {
447  float mySegmentSizeX = 0.f;
448  float mySegmentSizeY = 0.f;
449  osg::Vec2f myScaleFactor;
452  osg::Vec2f myPixelFrustumSize;
454  };
455  std::vector<CascadeEntry> myCascades;
456  };
457 
458  // Visualizes multicast debug information in the shadow overlay GUI.
459  void drawMulticastDetailsGui(const MulticastDebugInfo& info);
460 
461  friend struct DtCurrentPassData;
462 
463  protected:
464 
465  typedef std::map< const osg::Camera*, osg::ref_ptr<DtPerChannelData> > PerChannelDataMap;
466  typedef std::vector< osg::ref_ptr<DtPerChannelData> > PerChannelDataVector;
467 
470 
474 
476 
478 
479  static const std::string myShadowCameraPrefixString;
480 
481  // Avoid camera deletion (and resulting GL calls) in cull thread.
482  std::vector<osg::ref_ptr<osg::Camera> > myDeferredDeleteCamera;
483 
484  // Protect myDeferredDeleteCamera
486 
487  bool myMulticastIsSupported; // true if multicast extension is supported
488  bool myShadingRateIsSupported; // true if shading rate extension is supported
489 
491 
492  boost::signalslib::connection myTerrainPatchAddedConnection;
493  boost::signalslib::connection myTerrainPatchDeletedConnection;
494 
496 
497  // Maximum viewport dimensions queried from the GPU driver. Used to cap shadow
498  // viewport sizes to the hardware limit without hardcoding a specific value.
499  osg::Vec2f myMaxViewportDims;
500 
501  // Anisotropy helps on NVIDIA hardware to reduce aliasing even when no mips are used (as with shadow maps).
502  // You can see the subtle improvement when moving near the ground looking out over the terrain.
504  };
505 }
506 
DT_DLL_EXAMPLESCENEROOTPLUGIN void init(DtDe &anIG)
static const std::string myShadowCameraPrefixString
Definition: DtOsgShadowTechnique.h:479
void setDirty(bool dirty)
Sets dirty for shadow technique&#39;s to true or false.
Definition: DtOsgShadowTechnique.h:368
osg::Vec2f myPixelFrustumSize
Definition: DtOsgShadowTechnique.h:452
The DtOsgRenderer is the component responsible for rendering 3D images onto the screen via OpenSceneG...
Definition: DtOsgRenderer.h:92
Definition: DtPerChannelData.h:102
Definition: DtShadowSettingsObject.h:24
osg::Vec3d myFrustumMax
Definition: DtOsgShadowTechnique.h:442
Definition: DtOsgShadowTechnique.h:354
Holds all state for a single shadow cascade split.
Definition: DtPerChannelData.h:42
#define DT_DLL_VRVOSG
Definition: vrvOsg.h:23
PerChannelDataMap myPerChannelDataMap
Definition: DtOsgShadowTechnique.h:468
boost::signalslib::connection myTerrainPatchAddedConnection
Definition: DtOsgShadowTechnique.h:492
Creates a series/span line in the CV tool.
Definition: DtConcurrencyViewerMacros.h:27
voidpf void uLong size
Definition: ioapi.h:39
Definition: DtSkyObjectManager.h:30
Contains the DtOsgTextureApplyAttribute class.
osg::Vec2f myMaxViewportDims
Definition: DtOsgShadowTechnique.h:499
DtDe & myDe
Definition: DtOsgShadowTechnique.h:477
osg::Matrix myProjectionMatrix
Definition: DtOsgShadowTechnique.h:357
This class provides the OSG-specific implementation of a DtChannel.
Definition: DtOsgChannel.h:58
class for holding loader shader files, so that they are not repeatedly loaded from disk ...
Definition: DtOsgShaderFilesCache.h:27
osg::ref_ptr< osg::Uniform > myReceiveShadowsUniform
Definition: DtOsgShadowTechnique.h:473
virtual const char * libraryName() const
Returns the namespace string.
Definition: DtOsgShadowTechnique.h:107
bool myMulticastIsSupported
Definition: DtOsgShadowTechnique.h:487
Definition: DtOsgShadowTechnique.h:445
std::vector< osg::ref_ptr< DtPerChannelData > > PerChannelDataVector
Definition: DtOsgShadowTechnique.h:466
virtual osg::Object * cloneType() const
Creates a new object of Type DtOsgShadowTechnique.
Definition: DtOsgShadowTechnique.h:98
Definition: DtCurrentPassData.h:29
osg::Matrix myViewMatrix
Definition: DtOsgShadowTechnique.h:356
DtShadowTweakables * myShadowUI
Definition: DtOsgShadowTechnique.h:475
Definition: DtShadowTweakables.h:21
std::vector< osg::ref_ptr< osg::Camera > > myDeferredDeleteCamera
Definition: DtOsgShadowTechnique.h:482
std::vector< CascadeEntry > myCascades
Definition: DtOsgShadowTechnique.h:455
float myAnisotropy
Definition: DtOsgShadowTechnique.h:503
PerChannelDataVector myPerChannelDataDeleteList
Definition: DtOsgShadowTechnique.h:469
Implements cascaded shadow mapping (CSM) for the VRV rendering pipeline.
Definition: DtOsgShadowTechnique.h:87
DtSkyObjectManager & mySkyObjectManager
Definition: DtOsgShadowTechnique.h:490
unsigned int GLenum
Definition: DtGlTextureBufferObject.h:19
osg::Vec2f myScaleFactor
Definition: DtOsgShadowTechnique.h:449
virtual bool isSameKindAs(const osg::Object *obj) const
Returns true if object is of the DtOsgShadowTechnique class. Otherwise, returns false.
Definition: DtOsgShadowTechnique.h:104
osg::Vec2f myFrustumSegmentSizeRatio
Definition: DtOsgShadowTechnique.h:451
osg::Vec2f myPixelOffsetTopLeft
Definition: DtOsgShadowTechnique.h:453
std::map< const osg::Camera *, osg::ref_ptr< DtPerChannelData > > PerChannelDataMap
Definition: DtOsgShadowTechnique.h:465
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
Stores debug information for multicast rendering, which can be visualized in the shadow overlay GUI...
Definition: DtOsgShadowTechnique.h:438
osg::Matrix myTextureViewProjectionMatrix
Definition: DtOsgShadowTechnique.h:358
osg::Vec3d myFrustumMin
Definition: DtOsgShadowTechnique.h:441
osg::Vec2f myOffsetSegmentSizeRatio
Definition: DtOsgShadowTechnique.h:450
DtOsgShaderFilesCache & myShaderFilesCache
Definition: DtOsgShadowTechnique.h:495
bool myShadingRateIsSupported
Definition: DtOsgShadowTechnique.h:488
osg::ref_ptr< osg::LightSource > myGlobalLight
Definition: DtOsgShadowTechnique.h:471
osg::ref_ptr< osg::Uniform > myLightDirectionUniform
Definition: DtOsgShadowTechnique.h:472
tbb::mutex myDeferredCameraDeletionMutex
Definition: DtOsgShadowTechnique.h:485
boost::signalslib::connection myTerrainPatchDeletedConnection
Definition: DtOsgShadowTechnique.h:493
virtual osg::Object * clone(const osg::CopyOp &copyop) const
Creates a new copy of DtOsgShadowTechnique object.
Definition: DtOsgShadowTechnique.h:101
osg::Vec3d myFrustumMidpoint
Definition: DtOsgShadowTechnique.h:440
virtual const char * className() const
Returns the class name string.
Definition: DtOsgShadowTechnique.h:110

Document ID: Generated on Thu Oct 15 21:27:02 EDT 2026 from SVN revision 296421
Copyright © 2005-2026 MAK Technologies. All Rights Reserved (www.mak.com)