source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h @ 3233

Revision 3233, 3.4 KB checked in by mattausch, 16 years ago (diff)
Line 
1#ifndef _ShadowMap_H__
2#define _ShadowMap_H__
3
4#include "common.h"
5#include "AxisAlignedBox3.h"
6#include "Matrix4x4.h"
7
8
9
10namespace CHCDemoEngine
11{
12
13class FrameBufferObject;
14class RenderTraverser;
15class Vector3;
16class PerspectiveCamera;
17class DirectionalLight;
18
19
20/** This class implements a the computation of single shadow map using
21        LispSM (light space perspective shadow mapping)
22*/
23class ShadowMap
24{
25
26public:
27        /** Constructor taking the scene boundig box and the current camera.
28                The shadow map is quadratic and has resolution mapSize squared.
29        */
30        ShadowMap(DirectionalLight *light, int mapSize, const AxisAlignedBox3 &sceneBox, PerspectiveCamera *cam);
31
32        ~ShadowMap();
33        /** Computes the shadow map
34        */
35        void ComputeShadowMap(RenderTraverser *traverser, const Matrix4x4 &projView);
36        /** Renders the scene from shadow view using conventional shading.
37        */
38        void RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView);
39        /** Returns computed shadow color texture.
40        */
41        unsigned int GetShadowColorTexture() const;
42        /** Returns the depth texture used for shadow comparison.
43        */
44        unsigned int GetDepthTexture() const;
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;
53        /** Returns shadow map size.
54        */
55        int GetSize() const { return mSize; }
56        /** Returns the camera used for light view rendering.
57        */
58        PerspectiveCamera *GetShadowCamera() const { return mShadowCam; }
59
60        /** Draws LispSM intersection body B and the used light frustrum
61        */
62        static void VisualizeFrustra();
63
64
65protected:
66
67
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;
72       
73        bool CalcLightProjection(Matrix4x4 &lightProj);
74
75        Matrix4x4 CalcLispSMTransform(const Matrix4x4 &lightProjView,
76                                          const AxisAlignedBox3 &bounds,
77                                                                  const VertexArray &pts);
78
79        void IncludeLightVolume(const Polyhedron &polyhedron,
80                                    VertexArray &frustumPoints,
81                                                        const Vector3 lightDir,
82                                                        const AxisAlignedBox3 &sceneBox);
83
84        float ComputeN(const AxisAlignedBox3 &extremalPoints) const;
85
86        float ComputeNOpt(const Matrix4x4 &lightSpace,
87                              const AxisAlignedBox3 &extremalPoints,
88                                          const VertexArray &body) const;
89
90        Vector3 GetNearCameraPointE(const VertexArray &pts) const;
91
92        Vector3 GetProjViewDir(const Matrix4x4 &lightSpace, const VertexArray &pts) const;
93
94
95        /** Internal shador render method.
96        */
97        void _Render(RenderTraverser *renderer);
98
99        Vector3 GetLightSpaceZ0(const Matrix4x4 &lightSpace,
100                                    const Vector3 &e,
101                                                        const float maxZ,
102                                                        const Vector3 &eyeDir) const;
103
104
105
106        //////////////
107
108        /// the scene bounding box
109        AxisAlignedBox3 mSceneBox;
110        /// fbo storing the shadow texture
111        FrameBufferObject *mFbo;
112        /// size of the shadow map
113        int mSize;
114        /// the shadow view
115        PerspectiveCamera *mShadowCam;
116        /// the texture matrix
117        Matrix4x4 mTextureMatrix;
118        /// the used light
119        DirectionalLight *mLight;
120        /// the scene camera
121        PerspectiveCamera *mCamera;
122
123        Matrix4x4 mLightProjView;
124};
125
126} // namespace
127#endif // _ShadowMapping_H__
Note: See TracBrowser for help on using the repository browser.