[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.
|
---|
[2963] | 28 | The shadow map has resolution size squared.
|
---|
[2891] | 29 | */
|
---|
[3062] | 30 | ShadowMap(DirectionalLight *light, int size, 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);
|
---|
[2928] | 36 | /** Returns computed shadow color texture.
|
---|
[2891] | 37 | */
|
---|
[2928] | 38 | unsigned int GetShadowColorTexture() const;
|
---|
[2963] | 39 | /** Returns the depth texture used for shadow comparison.
|
---|
| 40 | */
|
---|
[2928] | 41 | unsigned int GetDepthTexture() const;
|
---|
[2894] | 42 | /** Returns computed texture matrix. It must be applied on the
|
---|
| 43 | the world space positions.
|
---|
| 44 |
|
---|
| 45 | The texture matrix can be directly applied if the world space position
|
---|
| 46 | is available (deferred shading), otherwise it must be multiplied with
|
---|
| 47 | the inverse model matrix.
|
---|
| 48 | */
|
---|
| 49 | void GetTextureMatrix(Matrix4x4 &m) const;
|
---|
[2895] | 50 | /** Return shadow size.
|
---|
| 51 | */
|
---|
| 52 | int GetSize() const { return mSize; }
|
---|
[2963] | 53 | /** Returns the camera used for light view rendering.
|
---|
| 54 | */
|
---|
[3062] | 55 | PerspectiveCamera *GetShadowCamera() const { return mShadowCam; }
|
---|
[2963] | 56 | /** Renders the scene from shadow view using conventional shading.
|
---|
| 57 | */
|
---|
[2931] | 58 | void RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView);
|
---|
[2963] | 59 | /** Draws LispSM intersection body B and the used light frustrum
|
---|
| 60 | */
|
---|
| 61 | static void VisualizeFrustra();
|
---|
[2897] | 62 |
|
---|
[2911] | 63 |
|
---|
[2894] | 64 | protected:
|
---|
[2891] | 65 |
|
---|
[2931] | 66 |
|
---|
[2913] | 67 | /** Calculates the intersection of the frustum with the box,
|
---|
| 68 | returns resultin polyhedron as array of polygons.
|
---|
| 69 | */
|
---|
| 70 | Polyhedron *CalcClippedFrustum(const AxisAlignedBox3 &box) const;
|
---|
[2915] | 71 |
|
---|
[2913] | 72 | bool CalcLightProjection(Matrix4x4 &lightProj);
|
---|
| 73 |
|
---|
[2920] | 74 | Matrix4x4 CalcLispSMTransform(const Matrix4x4 &lightProjView,
|
---|
| 75 | const AxisAlignedBox3 &bounds,
|
---|
| 76 | const VertexArray &pts);
|
---|
| 77 |
|
---|
[2913] | 78 | void IncludeLightVolume(const Polyhedron &polyhedron,
|
---|
| 79 | VertexArray &frustumPoints,
|
---|
| 80 | const Vector3 lightDir,
|
---|
| 81 | const AxisAlignedBox3 &sceneBox);
|
---|
| 82 |
|
---|
[2920] | 83 | float ComputeN(const AxisAlignedBox3 &extremalPoints) const;
|
---|
[2913] | 84 |
|
---|
[2944] | 85 | float ComputeNOpt(const Matrix4x4 &lightSpace,
|
---|
| 86 | const AxisAlignedBox3 &extremalPoints,
|
---|
| 87 | const VertexArray &body) const;
|
---|
| 88 |
|
---|
[2920] | 89 | Vector3 GetNearCameraPointE(const VertexArray &pts) const;
|
---|
[2916] | 90 |
|
---|
[2920] | 91 | Vector3 GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const;
|
---|
| 92 |
|
---|
| 93 |
|
---|
[2942] | 94 | /** Internal shador render method.
|
---|
| 95 | */
|
---|
| 96 | void _Render(RenderTraverser *renderer);
|
---|
[2920] | 97 |
|
---|
[2944] | 98 | Vector3 GetLightSpaceZ0(const Matrix4x4 &lightSpace,
|
---|
| 99 | const Vector3 &e,
|
---|
| 100 | const float maxZ,
|
---|
| 101 | const Vector3 &eyeDir) const;
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 |
|
---|
[2913] | 105 | //////////////
|
---|
| 106 |
|
---|
[2894] | 107 | /// the scene bounding box
|
---|
| 108 | AxisAlignedBox3 mSceneBox;
|
---|
| 109 | /// fbo storing the shadow texture
|
---|
[2891] | 110 | FrameBufferObject *mFbo;
|
---|
[2894] | 111 | /// size of the shadow map
|
---|
[2891] | 112 | int mSize;
|
---|
[3034] | 113 | /// the shadow view
|
---|
[3062] | 114 | PerspectiveCamera *mShadowCam;
|
---|
[2894] | 115 | /// the texture matrix
|
---|
| 116 | Matrix4x4 mTextureMatrix;
|
---|
| 117 | /// the used light
|
---|
[2952] | 118 | DirectionalLight *mLight;
|
---|
[2894] | 119 | /// the scene camera
|
---|
[3062] | 120 | PerspectiveCamera *mCamera;
|
---|
[2911] | 121 |
|
---|
| 122 | Matrix4x4 mLightProjView;
|
---|
[2891] | 123 | };
|
---|
| 124 |
|
---|
| 125 | } // namespace
|
---|
[2894] | 126 | #endif // _ShadowMapping_H__ |
---|