NL3D::CShadowMap Class Reference

#include <shadow_map.h>


Detailed Description

This class encapsulate all Data generated by a Shadow Caster, and read for a Shadow Receiver.
Author:
Lionel Berenguier

Nevrax France

Date:
2003

Definition at line 53 of file shadow_map.h.

Public Member Functions

 CShadowMap (CShadowMapManager *smm)
 Constructor. NB: ptr is owned => shadowMap should no still live while smm dead.

float getFadeAround () const
 return compiled max of DistanceFade and TemporalOutScreenFade.

float getFinalFade () const
 same but maximize with the TemporalInScreenFade;

ITexturegetTexture () const
 You can only get the texture for filling / use in a material.

uint32 getTextureSize () const
 get the TextureSize

void initTexture (uint textSize)
 create the Texture. It reset the texture if not of same size.

void processFades ()
void resetTexture ()
 reset the Texture

 ~CShadowMap ()
void buildCasterCameraMatrix (const CVector &lightDir, const CMatrix &localPosMatrix, const CAABBox &bbShape, CMatrix &cameraMatrix)
void buildClipInfoFromMatrix ()
void buildProjectionInfos (const CMatrix &cameraMatrix, const CVector &backPoint, const CScene *scene)

Data Fields

float DistanceFade
float InScreenFadeAccum
uint64 LastGenerationFrame
CAABBox LocalBoundingBox
std::vector< CPlaneLocalClipPlanes
CMatrix LocalProjectionMatrix
float TemporalInScreenFade
float TemporalOutScreenFade

Private Attributes

float _FadeAround
float _FinalFade
CShadowMapManager_ShadowMapManager
uint32 _TextSize
NLMISC::CSmartPtr< ITexture_Texture


Constructor & Destructor Documentation

NL3D::CShadowMap::CShadowMap CShadowMapManager smm  ) 
 

Constructor. NB: ptr is owned => shadowMap should no still live while smm dead.

Definition at line 43 of file shadow_map.cpp.

References _FadeAround, _FinalFade, _TextSize, DistanceFade, InScreenFadeAccum, LastGenerationFrame, nlassert, TemporalInScreenFade, and TemporalOutScreenFade.

00044 {
00045         nlassert(smm);
00046         _ShadowMapManager= smm;
00047         _TextSize= 0;
00048         LastGenerationFrame= 0;
00049         DistanceFade= 0;
00050         TemporalOutScreenFade= 0;
00051         TemporalInScreenFade= 0;
00052         _FadeAround= 0;
00053         _FinalFade= 1;
00054         // see doc why 1.
00055         InScreenFadeAccum= 1;
00056 }

NL3D::CShadowMap::~CShadowMap  ) 
 

Definition at line 60 of file shadow_map.cpp.

References resetTexture().

00061 {
00062         resetTexture();
00063 }


Member Function Documentation

void NL3D::CShadowMap::buildCasterCameraMatrix const CVector lightDir,
const CMatrix localPosMatrix,
const CAABBox bbShape,
CMatrix cameraMatrix
 

From A BBox in Object Space, the lightDir, and the (nearly) worldMatrix, build the Camera for common Render Projection The caller has then just to do driver->setFrustum(0,1,0,1,0,1,false); driver->setupViewMatrix(cameraMatrix.inverted()); driver->setupModelMatrix(localPosMatrix); Then render his mesh.

Definition at line 135 of file shadow_map.cpp.

References NLMISC::CAABBox::getHalfSize(), NLMISC::CMatrix::getI(), NLMISC::CMatrix::getJ(), NLMISC::CMatrix::getK(), NLMISC::CAABBox::getMin(), NLMISC::CAABBox::getSize(), getTextureSize(), NLMISC::CMatrix::inverted(), NLMISC::CMatrix::normalize(), NLMISC::CAABBox::setHalfSize(), NLMISC::CMatrix::setRot(), NLMISC::CAABBox::transformAABBox(), NLMISC::CMatrix::translate(), NLMISC::CVector::x, NLMISC::CVector::y, and NLMISC::CVector::z.

Referenced by NL3D::CSkeletonModel::generateShadowMap(), and NL3D::CMeshInstance::generateShadowMap().

00136 {
00137         // compute the orthogonal LightSpace camera matrix. Remind that J look forward and K is up here.
00138         cameraMatrix.setRot(CVector::I, lightDir, CVector::K);
00139         cameraMatrix.normalize(CMatrix::YZX);
00140 
00141         CAABBox         bbLocal;
00142         bbLocal= CAABBox::transformAABBox(cameraMatrix.inverted() * localPosMatrix, bbShape);
00143 
00144         // Enlarge for 1 pixel left and right.
00145         float   scaleSize= (float)getTextureSize();
00146         scaleSize= (scaleSize+2)/scaleSize;
00147         CVector         hs= bbLocal.getHalfSize();
00148         hs.x*= scaleSize;
00149         hs.z*= scaleSize;
00150         // TestUniform
00151         /*float maxLen= max(hs.x, hs.y);
00152         maxLen= max(maxLen, hs.z);
00153         hs.x= hs.y= hs.z= maxLen;*/
00154         // EndTestUniform
00155         bbLocal.setHalfSize(hs);
00156 
00157         // setup the orthogonal camera Matrix so that it includes all the BBox
00158         cameraMatrix.translate(bbLocal.getMin());
00159         CVector vi= cameraMatrix.getI() * bbLocal.getSize().x;
00160         CVector vj= cameraMatrix.getJ() * bbLocal.getSize().y;
00161         CVector vk= cameraMatrix.getK() * bbLocal.getSize().z;
00162         cameraMatrix.setRot(vi,vj,vk);
00163 }

void NL3D::CShadowMap::buildClipInfoFromMatrix  ) 
 

The ShadowMap Caster can call this method after setting LocalProjectionMatrix. It computes auto the LocalClipPlanes and LocalBoundingBox from it. NB: don't use it if you use buildProjectionInfos().

Definition at line 98 of file shadow_map.cpp.

References NLMISC::CAABBox::extend(), NLMISC::CMatrix::getI(), NLMISC::CMatrix::getJ(), NLMISC::CMatrix::getK(), NLMISC::CMatrix::getPos(), LocalBoundingBox, LocalClipPlanes, LocalProjectionMatrix, NLMISC::CAABBox::setCenter(), and NLMISC::CAABBox::setHalfSize().

Referenced by buildProjectionInfos().

00099 {
00100         const   CMatrix &cameraMatrix= LocalProjectionMatrix;
00101         CVector vi= LocalProjectionMatrix.getI();
00102         CVector vj= LocalProjectionMatrix.getJ();
00103         CVector vk= LocalProjectionMatrix.getK();
00104         CVector vp= LocalProjectionMatrix.getPos();
00105 
00106         // **** Compute the planes.
00107         LocalClipPlanes.resize(6);
00108         // The plane 0 is the back plane.
00109         LocalClipPlanes[0].make(-vj, vp);
00110         // Left and botom plane.
00111         LocalClipPlanes[1].make(-vi, vp);
00112         LocalClipPlanes[2].make(-vk, vp);
00113         // Right and top plane.
00114         LocalClipPlanes[3].make(vi, vp + vi);
00115         LocalClipPlanes[4].make(vk, vp + vk);
00116         // Front plane.
00117         LocalClipPlanes[5].make(vj, vp + vj);
00118 
00119         // **** Compute the AA bounding box.
00120         LocalBoundingBox.setHalfSize(CVector::Null);
00121         CVector         p0= vp;
00122         CVector         p1= p0 + vi + vj + vk;
00123         LocalBoundingBox.setCenter(p0);
00124         LocalBoundingBox.extend(p0 + vi);
00125         LocalBoundingBox.extend(p0 + vj);
00126         LocalBoundingBox.extend(p0 + vk);
00127         LocalBoundingBox.extend(p1 - vi);
00128         LocalBoundingBox.extend(p1 - vj);
00129         LocalBoundingBox.extend(p1 - vk);
00130         LocalBoundingBox.extend(p1);
00131 }

void NL3D::CShadowMap::buildProjectionInfos const CMatrix cameraMatrix,
const CVector backPoint,
const CScene scene
 

From the Camera matrix computed with buildCasterCameraMatrix, compute the LocalProjectionMatrix, which modify the J axis according to backPoint and Shadow Depth. NB: automatically calls the buildClipInfoFromMatrix() method

Definition at line 167 of file shadow_map.cpp.

References buildClipInfoFromMatrix(), NLMISC::CMatrix::getI(), NLMISC::CMatrix::getJ(), NLMISC::CMatrix::getK(), NLMISC::CMatrix::getPos(), NL3D::CScene::getShadowMapMaxDepth(), LocalProjectionMatrix, NLMISC::CVector::normed(), NLMISC::CMatrix::setPos(), and NLMISC::CMatrix::setRot().

Referenced by NL3D::CSkeletonModel::generateShadowMap(), and NL3D::CMeshInstance::generateShadowMap().

00168 {
00169         // Modify the cameraMatrix to define the Aera of Shadow.
00170         CVector         projp= cameraMatrix.getPos();
00171         CVector         proji= cameraMatrix.getI();
00172         CVector         projj= cameraMatrix.getJ();
00173         CVector         projk= cameraMatrix.getK();
00174         // modify the J vector so that it gets the Wanted Len
00175         CVector vj= projj.normed();
00176         projj= vj*scene->getShadowMapMaxDepth();
00177         // Must move Pos so that the IK plane include the backPoint
00178         projp+= (backPoint*vj-projp*vj) * vj;
00179         // set the matrix
00180         LocalProjectionMatrix.setRot(proji, projj, projk);
00181         LocalProjectionMatrix.setPos(projp);
00182 
00183         // compute The clipPlanes and bbox.
00184         buildClipInfoFromMatrix();
00185 }

float NL3D::CShadowMap::getFadeAround  )  const [inline]
 

return compiled max of DistanceFade and TemporalOutScreenFade.

Definition at line 145 of file shadow_map.h.

References _FadeAround.

Referenced by processFades().

00145 {return _FadeAround;}

float NL3D::CShadowMap::getFinalFade  )  const [inline]
 

same but maximize with the TemporalInScreenFade;

Definition at line 147 of file shadow_map.h.

References _FinalFade.

Referenced by NL3D::CShadowMapManager::renderProject(), and NL3D::CShadowMapManager::selectShadowMapsToGenerate().

00147 {return _FinalFade;}

ITexture* NL3D::CShadowMap::getTexture void   )  const [inline]
 

You can only get the texture for filling / use in a material.

Definition at line 113 of file shadow_map.h.

Referenced by NL3D::CShadowMapManager::renderGenerate(), and NL3D::CShadowMapManager::renderProject().

00113 {return _Texture;}

uint32 NL3D::CShadowMap::getTextureSize  )  const [inline]
 

get the TextureSize

Definition at line 110 of file shadow_map.h.

References _TextSize, and uint32.

Referenced by buildCasterCameraMatrix(), NL3D::CSkeletonModel::updateShadowMap(), and NL3D::CMeshInstance::updateShadowMap().

00110 {return _TextSize;}

void NL3D::CShadowMap::initTexture uint  textSize  ) 
 

create the Texture. It reset the texture if not of same size.

Definition at line 66 of file shadow_map.cpp.

References _TextSize, NL3D::CShadowMapManager::allocateTexture(), LastGenerationFrame, NLMISC::raiseToNextPowerOf2(), resetTexture(), and uint.

Referenced by NL3D::CSkeletonModel::updateShadowMap(), and NL3D::CMeshInstance::updateShadowMap().

00067 {
00068         textSize= max(textSize, 2U);
00069         // if same size than setup, quit
00070         if(_TextSize==textSize)
00071                 return;
00072         resetTexture();
00073         textSize= raiseToNextPowerOf2(textSize);
00074         _TextSize= textSize;
00075 
00076         // Allocate in the Manager.
00077         _Texture= _ShadowMapManager->allocateTexture(_TextSize);
00078 
00079         // Since our texture has changed, it is no more valid. reset counter.
00080         LastGenerationFrame= 0;
00081 }

void NL3D::CShadowMap::processFades  ) 
 

Clamp Fades to 0-1. Additionaly reset the texture if DistanceFade>=1 or TemporalOutScreenFade>=1 See Implementation for Why. Additionally compile getFadeAround() and getFadeInScreen()

Definition at line 189 of file shadow_map.cpp.

References _FadeAround, _FinalFade, NLMISC::clamp(), DistanceFade, getFadeAround(), resetTexture(), TemporalInScreenFade, and TemporalOutScreenFade.

Referenced by NL3D::CClipTrav::clipSkeletonShadowMaps().

00190 {
00191         clamp(DistanceFade, 0.f, 1.f);
00192         clamp(TemporalOutScreenFade, 0.f, 1.f);
00193         clamp(TemporalInScreenFade, 0.f, 1.f);
00194 
00195         _FadeAround= max(DistanceFade, TemporalOutScreenFade);
00196         _FinalFade= max(_FadeAround, TemporalInScreenFade);
00197 
00198         /* if the fadeAround is 1, then release the texture
00199                 Don't take _FinalFade because this last may change too much cause of TemporalInScreenFade.
00200                 While FadeAround is somewhat stable (as entities and the camera don't move too much),
00201                 TemporalInScreenFade is dependent of camera rotation.
00202 
00203                 => _FinalFade allow to not draw too much shadows (CPU gain),
00204                 while FadeAround allow in addition the capacity to not use too much texture memory
00205         */
00206         if(getFadeAround()==1)
00207                 resetTexture();
00208 }

void NL3D::CShadowMap::resetTexture  ) 
 

reset the Texture

Definition at line 85 of file shadow_map.cpp.

References _TextSize, and NL3D::CShadowMapManager::releaseTexture().

Referenced by initTexture(), processFades(), and ~CShadowMap().

00086 {
00087         // release the SmartPtr
00088         if(_Texture)
00089         {
00090                 _ShadowMapManager->releaseTexture(_Texture);
00091                 _Texture= NULL;
00092         }
00093         _TextSize= 0;
00094 }


Field Documentation

float NL3D::CShadowMap::_FadeAround [private]
 

Definition at line 154 of file shadow_map.h.

Referenced by CShadowMap(), getFadeAround(), and processFades().

float NL3D::CShadowMap::_FinalFade [private]
 

Definition at line 155 of file shadow_map.h.

Referenced by CShadowMap(), getFinalFade(), and processFades().

CShadowMapManager* NL3D::CShadowMap::_ShadowMapManager [private]
 

Definition at line 153 of file shadow_map.h.

uint32 NL3D::CShadowMap::_TextSize [private]
 

Definition at line 152 of file shadow_map.h.

Referenced by CShadowMap(), getTextureSize(), initTexture(), and resetTexture().

NLMISC::CSmartPtr<ITexture> NL3D::CShadowMap::_Texture [private]
 

Definition at line 151 of file shadow_map.h.

float NL3D::CShadowMap::DistanceFade
 

They are the fade of the shadowMap, for Lod Shadow display. 0-1 values. NB: if DistanceFade==1 or TemporalOutScreenFade==1, then the ShadowMap Texture is released. Final Fade is the max of the 3.

Definition at line 86 of file shadow_map.h.

Referenced by NL3D::CClipTrav::clipSkeletonShadowMaps(), CShadowMap(), and processFades().

float NL3D::CShadowMap::InScreenFadeAccum
 

This value represent the fade time we don't know what to do with TemporalInScreenFade because the shadowMap is not visible or frustum-clipped. Once the shadowMap will be visible again, This will be add/removed (according to TemporalInScreenFade rules) to the TemporalInScreenFade. NB: by default the value is 1 so the initial state is correct when you enable cast shadowMap!

Definition at line 97 of file shadow_map.h.

Referenced by NL3D::CClipTrav::clipSkeletonShadowMaps(), and CShadowMap().

uint64 NL3D::CShadowMap::LastGenerationFrame
 

Definition at line 78 of file shadow_map.h.

Referenced by CShadowMap(), initTexture(), NL3D::CShadowMapManager::renderGenerate(), and NL3D::CShadowMapManager::selectShadowMapsToGenerate().

CAABBox NL3D::CShadowMap::LocalBoundingBox
 

Computed at shadow casting time. This is the LocalPos Bouding Box containing the shadow (AxisAligned).

See also:
generateClipInfoFromMatrix()

Definition at line 74 of file shadow_map.h.

Referenced by buildClipInfoFromMatrix(), NL3D::CShadowPolyReceiver::render(), and NL3D::CShadowMapManager::renderProject().

std::vector<CPlane> NL3D::CShadowMap::LocalClipPlanes
 

Computed at shadow casting time. They are clipping planes used to clip receivers (mirror of the OBB). Receivers may use them to clip sub received parts (as they which) Like the ProjectionLocalMatrix, this plane are in World, but the position of the Caster model.

See also:
generateClipInfoFromMatrix()

Definition at line 69 of file shadow_map.h.

Referenced by buildClipInfoFromMatrix(), and NL3D::CShadowPolyReceiver::render().

CMatrix NL3D::CShadowMap::LocalProjectionMatrix
 

Computed at shadow casting time. The matrix used to cast the Shadow. It is actualy a World ProjectionMatrix, but is only local to the Position of the Model (not rotation/scale). Hence receiver have to add the position of the caster model to this matrix to get true World. The usage of this matrix is for UV projection: XYZ= WorldProjectionMatrix * UVW. NB: Vj (ie for W) is mapped such that Vp means NearClip of the shadow and Vp+Vj means FarClip of the shadow

Definition at line 62 of file shadow_map.h.

Referenced by buildClipInfoFromMatrix(), buildProjectionInfos(), and NL3D::CShadowMapManager::renderProject().

float NL3D::CShadowMap::TemporalInScreenFade
 

Definition at line 90 of file shadow_map.h.

Referenced by NL3D::CClipTrav::clipSkeletonShadowMaps(), CShadowMap(), and processFades().

float NL3D::CShadowMap::TemporalOutScreenFade
 

Definition at line 88 of file shadow_map.h.

Referenced by NL3D::CClipTrav::clipSkeletonShadowMaps(), CShadowMap(), and processFades().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 07:43:01 2004 for NeL by doxygen 1.3.6