My Project
ShadowMap.h
1 #pragma once
2 
3 namespace ParaEngine
4 {
5  class CEffectFile;
6 
8  class CShadowMap
9  {
10  public:
11  CShadowMap(void);
12  ~CShadowMap(void);
13 
14  enum SHADOW_TECHNIQUE
15  {
16  SHADOWTYPE_PSM=0,
17  SHADOWTYPE_LSPSM=1,
18  SHADOWTYPE_TSM=2,
19  SHADOWTYPE_ORTHO=3,
20  };
21  public:
22  HRESULT InvalidateDeviceObjects(); // called just before device is Reset
23  HRESULT RestoreDeviceObjects(); // called when device is restored
27  HRESULT BeginShadowPass();
29  HRESULT EndShadowPass();
30 
32  bool SaveShadowMapToFile(string filePath);
33 
34  const Matrix4* GetTexViewProjMatrix();
35  const Matrix4* GetViewProjMatrix();
36 
45  HRESULT SetShadowTexture(CEffectFile& pEffect, int nTextureIndex, int nUseBlur=0);
47  void UnsetShadowTexture(int nTextureIndex);
49  bool SupportsHWShadowMaps(){return m_bSupportsHWShadowMaps;}
50 
53 
54 
63  SHADOW_TECHNIQUE GetShadowTechnique(){return (SHADOW_TECHNIQUE)m_iShadowType;};
79  void SetShadowTechnique(SHADOW_TECHNIQUE technique){m_iShadowType = technique;};
80 
82  bool UsingBlurredShadowMap(){return m_bBlurSMColorTexture;}
83 
86  void SetShadowMapTexelSizeLevel(int nLevel);
88  void SetShadowMapTexelSize(int nWidth, int nHeight);
89 
92  static void SetDefaultShadowMapTexelSizeLevel(int nLevel){g_nShadowMapTexelSizeLevel = nLevel;};
93 
95  bool PrepareAllSurfaces();
96 
98  void AddShadowCasterPoint(const CShapeAABB& aabb);
99  private:
100  static int g_nShadowMapTexelSizeLevel;
101 #ifdef USE_DIRECTX_RENDERER
102  LPDIRECT3DSURFACE9 m_pBackBuffer, m_pZBuffer;
103  LPDIRECT3DSURFACE9 m_pSMColorSurface, m_pSMColorSurfaceBlurredHorizontal, m_pSMColorSurfaceBlurredVertical, m_pSMZSurface;
104 #else
105  GLuint mOldFrameBufferObject;
106  GLuint mSMFrameBufferObject,mSMDepthStencilBufferObject;
107 #endif
108  asset_ptr<TextureEntity> m_pSMColorTexture, m_pSMColorTextureBlurredHorizontal, m_pSMColorTextureBlurredVertical, m_pSMZTexture;
109  int m_shadowTexWidth, m_shadowTexHeight;
110  // light direction
111  Vector3 m_lightDir;
112 
114  std::vector<CShapeAABB> m_ShadowCasterPoints;
116  std::vector<CShapeAABB> m_ShadowReceiverPoints;
117 
118  //bit depth of shadow map
119  int m_bitDepth;
120 
121  // Transforms
122  Matrix4 m_LightViewProj;
123 
124  // this value can only be set when this instance is being created
125  bool m_bSupportsHWShadowMaps;
126  // this value can only be set when this instance is being created
127  bool m_bBlurSMColorTexture;
128 
129  int m_iShadowType;
130  bool m_bUnitCubeClip;
131  bool m_bSlideBack;
132  bool m_bDisplayShadowMap;
133  bool m_bShadowTestInverted;
134  float m_fAspect, m_zNear, m_zFar, m_ppNear, m_ppFar, m_fSlideBack, m_fLSPSM_Nopt, m_fCosGamma;
135 
136  //various bias values
137  float m_fBiasSlope;
138  int m_iDepthBias;
139  float m_fMinInfinityZ;
140  float m_fLSPSM_NoptWeight;
141  float m_fTSM_Delta;
142 
143  Matrix4 m_World;
144  Matrix4 m_View;
145  Matrix4 m_Projection;
146  Matrix4 m_textureMatrix;
147 
148  bool ComputeVirtualCameraParameters();
149  bool BuildTSMProjectionMatrix();
150  bool BuildPSMProjectionMatrix();
151  bool BuildLSPSMProjectionMatrix();
152  bool BuildOrthoShadowProjectionMatrix();
153 #ifdef USE_DIRECTX_RENDERER
154  HRESULT CheckResourceFormatSupport(D3DFORMAT fmt, D3DRESOURCETYPE resType, DWORD dwUsage);
155 #endif
156  };
157 
158 }
void SetShadowMapTexelSize(int nWidth, int nHeight)
Set the size of the shadow map.
static void SetDefaultShadowMapTexelSizeLevel(int nLevel)
set the size level of the shadow map when it is created.
Definition: ShadowMap.h:92
HRESULT EndShadowPass()
end the shadow map
void SetShadowTechnique(SHADOW_TECHNIQUE technique)
I will support two shadow mapping method.
Definition: ShadowMap.h:79
different physics engine has different winding order.
Definition: EventBinding.h:32
HRESULT SetShadowTexture(CEffectFile &pEffect, int nTextureIndex, int nUseBlur=0)
apply the current shadow map to the selected effect file at the specified texture index ...
bool SaveShadowMapToFile(string filePath)
save shadow map to file
HRESULT BeginShadowPass()
The function is responsible for building the perspective shadow map projection matrix and set the ren...
void AddShadowCasterPoint(const CShapeAABB &aabb)
add a shadow caster point.
Standard 3-dimensional vector.
Definition: ParaVector3.h:16
bool PrepareAllSurfaces()
prepare all device objects.
AABB-related code.
Definition: ShapeAABB.h:11
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
bool UsingBlurredShadowMap()
whether to use blurred shadow map for the global terrain.
Definition: ShadowMap.h:82
Definition: effect_file.h:323
int GetShadowMapTexelSize()
get the size of the shadow map.
void SetShadowMapTexelSizeLevel(int nLevel)
change the size level of the shadow map.
some functions about shadow map
Definition: ShadowMap.h:8
SHADOW_TECHNIQUE GetShadowTechnique()
I will support two shadow mapping method.
Definition: ShadowMap.h:63
bool SupportsHWShadowMaps()
return true if device support hardware shadow map.
Definition: ShadowMap.h:49
void UnsetShadowTexture(int nTextureIndex)
unset projective texture