[2894] | 1 | #ifndef _ShadowMap_H__
|
---|
| 2 | #define _ShadowMap_H__
|
---|
[2891] | 3 |
|
---|
| 4 | #include "common.h"
|
---|
| 5 | #include "AxisAlignedBox3.h"
|
---|
| 6 | #include "Matrix4x4.h"
|
---|
[2913] | 7 |
|
---|
[2891] | 8 |
|
---|
| 9 |
|
---|
| 10 | namespace CHCDemoEngine
|
---|
| 11 | {
|
---|
| 12 |
|
---|
| 13 | class FrameBufferObject;
|
---|
| 14 | class RenderTraverser;
|
---|
| 15 | class Vector3;
|
---|
[3062] | 16 | class PerspectiveCamera;
|
---|
[2952] | 17 | class DirectionalLight;
|
---|
[2891] | 18 |
|
---|
[3062] | 19 |
|
---|
[2963] | 20 | /** This class implements a the computation of single shadow map using
|
---|
| 21 | LispSM (light space perspective shadow mapping)
|
---|
[2891] | 22 | */
|
---|
[2894] | 23 | class ShadowMap
|
---|
[2891] | 24 | {
|
---|
[2894] | 25 |
|
---|
[2891] | 26 | public:
|
---|
[2894] | 27 | /** Constructor taking the scene boundig box and the current camera.
|
---|
[3233] | 28 | The shadow map is quadratic and has resolution mapSize squared.
|
---|
[2891] | 29 | */
|
---|
[3233] | 30 | ShadowMap(DirectionalLight *light, int mapSize, const AxisAlignedBox3 &sceneBox, PerspectiveCamera *cam);
|
---|
[2891] | 31 |
|
---|
[2894] | 32 | ~ShadowMap();
|
---|
| 33 | /** Computes the shadow map
|
---|
[2891] | 34 | */
|
---|
[2911] | 35 | void ComputeShadowMap(RenderTraverser *traverser, const Matrix4x4 &projView);
|
---|
[3233] | 36 | /** Renders the scene from shadow view using conventional shading.
|
---|
| 37 | */
|
---|
| 38 | void RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView);
|
---|
[2928] | 39 | /** Returns computed shadow color texture.
|
---|
[2891] | 40 | */
|
---|
[2928] | 41 | unsigned int GetShadowColorTexture() const;
|
---|
[2963] | 42 | /** Returns the depth texture used for shadow comparison.
|
---|
| 43 | */
|
---|
[2928] | 44 | unsigned int GetDepthTexture() const;
|
---|
[2894] | 45 | /** Returns computed texture matrix. It must be applied on the
|
---|
| 46 | the world space positions.
|
---|
| 47 |
|
---|
| 48 | The texture matrix can be directly applied if the world space position
|
---|
| 49 | is available (deferred shading), otherwise it must be multiplied with
|
---|
| 50 | the inverse model matrix.
|
---|
| 51 | */
|
---|
| 52 | void GetTextureMatrix(Matrix4x4 &m) const;
|
---|
[3233] | 53 | /** Returns shadow map size.
|
---|
[2895] | 54 | */
|
---|
| 55 | int GetSize() const { return mSize; }
|
---|
[2963] | 56 | /** Returns the camera used for light view rendering.
|
---|
| 57 | */
|
---|
[3062] | 58 | PerspectiveCamera *GetShadowCamera() const { return mShadowCam; }
|
---|
[3233] | 59 |
|
---|
[2963] | 60 | /** Draws LispSM intersection body B and the used light frustrum
|
---|
| 61 | */
|
---|
| 62 | static void VisualizeFrustra();
|
---|
[2897] | 63 |
|
---|
[2911] | 64 |
|
---|
[2894] | 65 | protected:
|
---|
[2891] | 66 |
|
---|
[2931] | 67 |
|
---|
[2913] | 68 | /** Calculates the intersection of the frustum with the box,
|
---|
| 69 | returns resultin polyhedron as array of polygons.
|
---|
| 70 | */
|
---|
| 71 | Polyhedron *CalcClippedFrustum(const AxisAlignedBox3 &box) const;
|
---|
[2915] | 72 |
|
---|
[2913] | 73 | bool CalcLightProjection(Matrix4x4 &lightProj);
|
---|
| 74 |
|
---|
[2920] | 75 | Matrix4x4 CalcLispSMTransform(const Matrix4x4 &lightProjView,
|
---|
| 76 | const AxisAlignedBox3 &bounds,
|
---|
| 77 | const VertexArray &pts);
|
---|
| 78 |
|
---|
[2913] | 79 | void IncludeLightVolume(const Polyhedron &polyhedron,
|
---|
| 80 | VertexArray &frustumPoints,
|
---|
| 81 | const Vector3 lightDir,
|
---|
| 82 | const AxisAlignedBox3 &sceneBox);
|
---|
| 83 |
|
---|
[2920] | 84 | float ComputeN(const AxisAlignedBox3 &extremalPoints) const;
|
---|
[2913] | 85 |
|
---|
[2944] | 86 | float ComputeNOpt(const Matrix4x4 &lightSpace,
|
---|
| 87 | const AxisAlignedBox3 &extremalPoints,
|
---|
| 88 | const VertexArray &body) const;
|
---|
| 89 |
|
---|
[2920] | 90 | Vector3 GetNearCameraPointE(const VertexArray &pts) const;
|
---|
[2916] | 91 |
|
---|
[2920] | 92 | Vector3 GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const;
|
---|
| 93 |
|
---|
| 94 |
|
---|
[2942] | 95 | /** Internal shador render method.
|
---|
| 96 | */
|
---|
| 97 | void _Render(RenderTraverser *renderer);
|
---|
[2920] | 98 |
|
---|
[2944] | 99 | Vector3 GetLightSpaceZ0(const Matrix4x4 &lightSpace,
|
---|
| 100 | const Vector3 &e,
|
---|
| 101 | const float maxZ,
|
---|
| 102 | const Vector3 &eyeDir) const;
|
---|
| 103 |
|
---|
| 104 |
|
---|
| 105 |
|
---|
[2913] | 106 | //////////////
|
---|
| 107 |
|
---|
[2894] | 108 | /// the scene bounding box
|
---|
| 109 | AxisAlignedBox3 mSceneBox;
|
---|
| 110 | /// fbo storing the shadow texture
|
---|
[2891] | 111 | FrameBufferObject *mFbo;
|
---|
[2894] | 112 | /// size of the shadow map
|
---|
[2891] | 113 | int mSize;
|
---|
[3034] | 114 | /// the shadow view
|
---|
[3062] | 115 | PerspectiveCamera *mShadowCam;
|
---|
[2894] | 116 | /// the texture matrix
|
---|
| 117 | Matrix4x4 mTextureMatrix;
|
---|
| 118 | /// the used light
|
---|
[2952] | 119 | DirectionalLight *mLight;
|
---|
[2894] | 120 | /// the scene camera
|
---|
[3062] | 121 | PerspectiveCamera *mCamera;
|
---|
[2911] | 122 |
|
---|
| 123 | Matrix4x4 mLightProjView;
|
---|
[2891] | 124 | };
|
---|
| 125 |
|
---|
| 126 | } // namespace
|
---|
[2894] | 127 | #endif // _ShadowMapping_H__ |
---|