Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 2825)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 2894)
@@ -65,7 +65,4 @@
 	*/
 	virtual ~BvhNode();
-	/** Returns unique id for this node.
-	*/
-	//inline int GetId() {return mId;}
 	/** Depth of this node in the tree.
 	*/
@@ -167,5 +164,5 @@
 
 	/////////
-	// used for view frustum culling
+	//-- used for view frustum culling
 
 	int mPlaneMask;
@@ -619,5 +616,7 @@
 
 	////////////////////////
-
+	
+	/// the bounding box of the bvh
+	AxisAlignedBox3 mBox;
 	/// the root of the hierarchy
 	BvhNode *mRoot;
@@ -627,40 +626,44 @@
 	size_t mGeometrySize;
 
-
-	////////////////
-
 	/// the current camera
 	Camera *mCamera;
+
+
+	////////////////
+	//-- tigher bounds termination criteria
+
+	/** maximal depth from which children are fetched for 
+		testing instead of the current node
+	*/
+	int mMaxDepthForTestingChildren;
+	/// threshold for computing tighter bounds
+	float mAreaRatioThreshold;
+	
+	
+	////////////////
+	//-- statistics
+
+	BvhStats mBvhStats;
+	/// the overall number of nodes
+	int mNumNodes;
+	/// the number of "virtual" (=actually used) nodes
+	int mNumVirtualNodes;
+
+
+	////////////
+	//-- rendering stuff
+
+	/// these proxy nodes are tested instead of the current node
+	BvhNodeContainer mTestNodes;
+	/// the indices used for vbo index buffering
+	unsigned int *mTestIndices;
+	/// a pointer to the end of the indices array
+	int mCurrentIndicesPtr;
+	/// the vbo id
+	unsigned int mVboId;
 	/// a vertex array used if working with indexed arrays (without vbo)
 	Vector3 *mVertices;
 	/// indices used for draw array rendering
 	unsigned int *mIndices;
-
-	/** maximal depth from which children are fetched for 
-		testing instead of the current node
-	*/
-	int mMaxDepthForTestingChildren;
-
-	float mAreaRatioThreshold;
-
-
-	BvhStats mBvhStats;
-
-	BvhNodeContainer mTestNodes;
-	
-	unsigned int *mTestIndices;
-	/// a pointer to the end of the indices array
-	int mCurrentIndicesPtr;
-
-	int mNumNodes;
-
-	/// the bounding box
-	AxisAlignedBox3 mBox;
-
-	int mNumVirtualNodes;
-
-	//////////////
-
-	unsigned int mVboId;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2893)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2894)
@@ -533,5 +533,4 @@
 		cgGLSetParameter1f(sExpFactorParam, expFactor);
 
-
 		// q: should we generate new samples or only rotate the old ones?
 		// in the first case, the sample patterns look nicer, but the kernel
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2892)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2894)
@@ -1,4 +1,4 @@
-#ifndef _ShadowMapping_H__
-#define _ShadowMapping_H__
+#ifndef _ShadowMap_H__
+#define _ShadowMap_H__
 
 #include "common.h"
@@ -20,42 +20,50 @@
 class Light;
 
-/** This class implements a shadow mapping algorithm
+/** This class implements a the computation of single shadow map
 */
-class ShadowMapping
+class ShadowMap
 {
+
 public:
-	/** constructor for a deferred shader taking the requested shadow size
+	/** Constructor taking the scene boundig box and the current camera. 
+		The shadow map has resolution size * size.
 	*/
-	ShadowMapping(const AxisAlignedBox3 &sceneBox, int size);
+	ShadowMap(int size, const AxisAlignedBox3 &sceneBox, Camera *cam);
 
-	~ShadowMapping();
-	/** Renders the shadowed scene
+	~ShadowMap();
+	/** Computes the shadow map
 	*/
-	void Render(RenderTraverser *traverser, Camera *cam);
-	/** Initialises shadow mapping and loads the required shaders:
-		This function has to be called only once.
+	void ComputeShadowMap(Light *light, RenderTraverser *traverser);
+	/** Returns computed shadow texture.
 	*/
-	static void Init(CGcontext context);
+	unsigned int GetShadowTexture() const;
+	/** Returns computed texture matrix. It must be applied on the
+		the world space positions. 
+		
+		The texture	matrix can be directly applied if the world space position
+		is available (deferred shading), otherwise it must be multiplied with 
+		the inverse model matrix.
+	*/
+	void GetTextureMatrix(Matrix4x4 &m) const;
 
-//protected:
 
-	void ComputeShadowMap(Light *light, RenderTraverser *traverser);
+protected:
 
-//protected:
-
+	/// the scene bounding box
+	AxisAlignedBox3 mSceneBox;
+	/// fbo storing the shadow texture
 	FrameBufferObject *mFbo;
-
+	/// size of the shadow map
 	int mSize;
 
-	AxisAlignedBox3 mSceneBox;
-
 	Camera *mShadowCam;
-
-	Matrix4x4 mLightViewMatrix;
-	Matrix4x4 mLightProjectionMatrix;
-
+	/// the texture matrix
+	Matrix4x4 mTextureMatrix;
+	/// the used light
 	Light *mLight;
+	/// the scene camera
+	Camera *mCamera;
 };
 
 } // namespace 
-#endif // _DeferredShader_H__
+#endif // _ShadowMapping_H__
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h	(revision 2893)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h	(revision 2894)
@@ -12,5 +12,5 @@
 
 class FrameBufferObject;
-class ShadowMapping;
+class ShadowMap;
 class Matrix4x4;
 
@@ -37,15 +37,12 @@
 	static void Init(CGcontext context);
 
+	void Render(FrameBufferObject *fbo, ShadowMap *shadowMap);
 
-	void Render(FrameBufferObject *fbo, 
- 		        const Matrix4x4 &matViewing, 
-				ShadowMapping *shadowMapping);
 
 protected:
 
 	void FirstPass(FrameBufferObject *fbo);
-	void FirstPassShadow(FrameBufferObject *fbo, 
-						 const Matrix4x4 &matViewing, 
-						 ShadowMapping *shadowMapping);
+
+	void FirstPassShadow(FrameBufferObject *fbo, ShadowMap *shadowMap);
 
 	void AntiAliasing(FrameBufferObject *fbo);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 2888)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 2894)
@@ -637,7 +637,4 @@
 void Bvh::RecomputeBounds()
 {
-	// clear old list
-	mTestNodes.clear();
-
 	// collect all nodes
 	BvhNodeContainer nodes;
@@ -662,6 +659,5 @@
 
 	// recreate indices used for indirect mode rendering
-	if (mIndices)
-		CreateIndices();
+	if (mIndices) CreateIndices();
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2893)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2894)
@@ -79,6 +79,6 @@
 
 
-ShadowMapping::ShadowMapping(const AxisAlignedBox3 &sceneBox, int size):
-mSceneBox(sceneBox), mSize(size)
+ShadowMap::ShadowMap(int size, const AxisAlignedBox3 &sceneBox, Camera *cam):
+mSceneBox(sceneBox), mSize(size), mCamera(cam)
 {
 	mFbo = new FrameBufferObject(size, size, FrameBufferObject::DEPTH_32, true);
@@ -91,34 +91,12 @@
 
 
-ShadowMapping::~ShadowMapping()
-{
-	//if (sCgShadowProgram)	cgDestroyProgram(sCgShadowProgram);
+ShadowMap::~ShadowMap()
+{
 	DEL_PTR(mFbo);
 }
 
 
-void ShadowMapping::Init(CGcontext context)
-{/*
-	sCgShadowProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/shadow.cg", 
-								RenderState::sCgFragmentProfile,
-								"main",
-								NULL);
-
-	if (sCgShadowProgram != NULL)
-	{
-		cgGLLoadProgram(sCgShadowProgram);
-		sShadowParam = cgGetNamedParameter(sCgShadowProgram, "shadowMap");
-	}
-	else
-		cerr << "shadow program failed to load" << endl;
-*/
-	PrintGLerror("init");
-}
-
-
-void ShadowMapping::ComputeShadowMap(Light *light, RenderTraverser *traverser)
+
+void ShadowMap::ComputeShadowMap(Light *light, RenderTraverser *traverser)
 {
 	mLight = light;
@@ -133,6 +111,4 @@
 
 	pos -= light->GetDirection() * Magnitude(mSceneBox.Diagonal() * 0.5f);
-	//pos.z = mSceneBox.Max().z;
-	//mShadowCam->SetPosition(pos - Magnitude(mSceneBox.Diagonal() * light->GetDirection()));
 	mShadowCam->SetPosition(pos);
 
@@ -160,4 +136,5 @@
 	glLoadIdentity();
 	
+	// setup projection
 	glOrtho(+xlen, -xlen, +ylen, -ylen, 0.0f, Magnitude(mSceneBox.Diagonal())); 
 	
@@ -168,10 +145,26 @@
 	glLoadIdentity();
 
-
+	// setup shadow camera
 	mShadowCam->SetupCameraView();
-	mShadowCam->GetModelViewMatrix(mLightViewMatrix);
-
-	mShadowCam->GetProjectionMatrix(mLightProjectionMatrix);
-	//glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mLightProjectionMatrix.x);
+
+	//////////////
+	//-- compute texture matrix
+
+	Matrix4x4 lightView, lightProj;
+
+	mShadowCam->GetModelViewMatrix(lightView);
+	mShadowCam->GetProjectionMatrix(lightProj);
+
+	static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
+								0.0f, 0.5f, 0.0f, 0.5f,
+								0.0f, 0.0f, 0.5f, 0.5f,
+								0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1]
+
+	Matrix4x4 lightProjView = lightView * lightProj;
+	mTextureMatrix = lightProjView * biasMatrix; 
+
+
+	/////////////
+	//-- render scene into shadow map
 
 	traverser->RenderScene();
@@ -192,5 +185,5 @@
 
 	delete [] data;
-*/
+	*/
 	PrintGLerror("shadow map");
 
@@ -198,201 +191,14 @@
 }
 
-/*
-void ShadowMapping::Render(RenderTraverser *traverser, Camera *cam)
-{
-	Matrix4x4 cam_proj;
-	Matrix4x4 cam_inverse_modelview;
-
-	const float bias[16] = 
-	{0.5f, 0.0f, 0.0f, 0.0f, 
-	0.0f, 0.5f, 0.0f, 0.0f,
-	0.0f, 0.0f, 0.5f, 0.0f,
-	0.5f, 0.5f, 0.5f, 1.0f	};
-
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	// update the camera, so that the user can have a free look
-	glMatrixMode(GL_MODELVIEW);
-	glLoadIdentity();
-	
-	glMatrixMode(GL_PROJECTION);
-	glLoadIdentity();
-	
-	cam->SetupCameraView();
-
-	// store the inverse of the resulting modelview matrix for the shadow computation
-	glGetFloatv(GL_MODELVIEW_MATRIX, (float *)cam_inverse_modelview.x);
-	cam_inverse_modelview.Invert();
-
-	// bind shadow map
-	glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, mFbo->GetDepthTex());
-
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
-	//glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_COMPARE_MODE, GL_NONE);
-
-	PrintGLerror("texture");
-
-	//float light_m[16];
-	
-	// compute a matrix that transforms from camera eye space to light clip space
-	// and pass it to the shader through the OpenGL texture matrices, since we
-	// don't use them now
-	glEnable(GL_TEXTURE_2D);
-	glMatrixMode(GL_TEXTURE);
-
-	glLoadMatrixf(bias);
-	glMultMatrixf((float *)mShadowMatrix.x);
-	
-	// multiply the light's (bias * crop * proj * modelview) by the inverse camera modelview
-	// so that we can transform a pixel as seen from the camera
-	glMultMatrixf((float *)cam_inverse_modelview.x);
-	
-		
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(sCgShadowProgram);
-	
-	cgGLSetTextureParameter(sShadowParam, mFbo->GetDepthTex());
-	cgGLEnableTextureParameter(sShadowParam);
-	
-	// finally, draw the scene 
-	traverser->RenderScene();
-
-	cgGLDisableTextureParameter(sShadowParam);
-
-	PrintGLerror("shadow");
-}
-*/
-
-
-//Called to draw scene
-void ShadowMapping::Render(RenderTraverser *traverser, Camera *cam)
-{
-#if 0
-	Matrix4x4 cam_inverse_modelview;
-
-	//Calculate texture matrix for projection
-	//This matrix takes us from eye space to the light's clip space
-	//It is postmultiplied by the inverse of the current view matrix when specifying texgen
-	static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
-								0.0f, 0.5f, 0.0f, 0.0f,
-								0.0f, 0.0f, 0.5f, 0.0f,
-								0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1]
-
-	/*glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	// update the camera, so that the user can have a free look
-	glMatrixMode(GL_MODELVIEW);
-	glLoadIdentity();
-	
-	glMatrixMode(GL_PROJECTION);
-	glLoadIdentity();
-	
-	cam->SetupCameraView();
-*/
-	// store the inverse of the resulting modelview matrix for the shadow computation
-	glGetFloatv(GL_MODELVIEW_MATRIX, (float *)cam_inverse_modelview.x);
-	cam_inverse_modelview.Invert();
-
-	// bind shadow map
-	glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, mFbo->GetDepthTex());
-	
-	// compute a matrix that transforms from camera eye space to light clip space
-	// and pass it to the shader through the OpenGL texture matrices, since we
-	// don't use them now
-	glEnable(GL_TEXTURE_2D);
-	glMatrixMode(GL_TEXTURE);
-
-	glLoadMatrixf((float *)biasMatrix.x);
-	glMultMatrixf((float *)mLightViewMatrix.x);
-	
-	// multiply the light's (bias * crop * proj * modelview) by the inverse camera modelview
-	// so that we can transform a pixel as seen from the camera
-	glMultMatrixf((float *)cam_inverse_modelview.x);
-
-	RgbaColor white(1, 1, 1, 0);
-	RgbaColor dark(0.2, 0.2, 0.2, 0);
-
-	glDisable(GL_LIGHTING);
-
-	/*glDisable(GL_LIGHT0);
-	glDisable(GL_LIGHT1);
-
-	//Use dim light to represent shadowed areas
-	glLightfv(GL_LIGHT1, GL_POSITION, mLight->GetDirection());
-	glLightfv(GL_LIGHT1, GL_AMBIENT, (float *)&dark.r);
-	glLightfv(GL_LIGHT1, GL_DIFFUSE, (float *)&white.r);
-	//glLightfv(GL_LIGHT1, GL_SPECULAR, black);
-	glEnable(GL_LIGHT1);
-	glEnable(GL_LIGHTING);*/
-
-	PrintGLerror("firstpass");
-
-	// finally, draw the scene 
-	traverser->RenderScene();
-
-
-	glEnable(GL_LIGHTING);
-
-
-	//3rd pass
-	//Draw with bright light
-	//glLightfv(GL_LIGHT1, GL_DIFFUSE, (float *)&white.r);
-	//glLightfv(GL_LIGHT1, GL_SPECULAR, (float *)&white.r);
-
-	Matrix4x4 textureMatrix = biasMatrix * mLightProjectionMatrix * mLightViewMatrix;
-
-	Matrix4x4 texMatT = Transpose(textureMatrix);
-
-	//Set up texture coordinate generation.
-	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
-	glTexGenfv(GL_S, GL_EYE_PLANE, texMatT.x[0]);
-	glEnable(GL_TEXTURE_GEN_S);
-
-	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
-	glTexGenfv(GL_T, GL_EYE_PLANE, texMatT.x[1]);
-	glEnable(GL_TEXTURE_GEN_T);
-
-	glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
-	glTexGenfv(GL_R, GL_EYE_PLANE, texMatT.x[2]);
-	glEnable(GL_TEXTURE_GEN_R);
-
-	glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
-	glTexGenfv(GL_Q, GL_EYE_PLANE, texMatT.x[3]);
-	glEnable(GL_TEXTURE_GEN_Q);
-
-	//Bind & enable shadow map texture
-	glBindTexture(GL_TEXTURE_2D, mFbo->GetDepthTex());
-	glEnable(GL_TEXTURE_2D);
-
-	//Enable shadow comparison
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE);
-
-	//Shadow comparison should be true (ie not in shadow) if r<=texture
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
-
-	//Shadow comparison should generate an INTENSITY result
-	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
-
-	//Set alpha test to discard false comparisons
-	glAlphaFunc(GL_GEQUAL, 0.99f);
-	glEnable(GL_ALPHA_TEST);
-
-	PrintGLerror("texture");
-
-	traverser->RenderScene();
-
-	//Disable textures and texgen
-	glDisable(GL_TEXTURE_2D);
-
-	glDisable(GL_TEXTURE_GEN_S);
-	glDisable(GL_TEXTURE_GEN_T);
-	glDisable(GL_TEXTURE_GEN_R);
-	glDisable(GL_TEXTURE_GEN_Q);
-
-	//Restore other states
-	glDisable(GL_LIGHTING);
-	glDisable(GL_ALPHA_TEST);
-#endif
+
+void ShadowMap::GetTextureMatrix(Matrix4x4 &m) const
+{
+	m = mTextureMatrix;
+}
+
+ 
+unsigned int ShadowMap::GetShadowTexture() const
+{
+	return mFbo->GetDepthTex();
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2893)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2894)
@@ -175,7 +175,7 @@
 
 static Matrix4x4 matProjectionView = IdentityMatrix();
-static Matrix4x4 matViewing = IdentityMatrix();
-
-ShadowMapping *shadowMapping = NULL;
+
+
+ShadowMap *shadowMap = NULL;
 Light *light = NULL;
 
@@ -433,5 +433,4 @@
 	DeferredShader::Init(sCgContext);
 	SsaoShader::Init(sCgContext);
-	ShadowMapping::Init(sCgContext);
 
 
@@ -446,5 +445,6 @@
 	light = new Light(lightDir, RgbaColor(1, 1, 1, 1));
 
-	shadowMapping = new ShadowMapping(bvh->GetBox(), 4096);
+	const float shadowSize = 4096;
+	shadowMap = new ShadowMap(shadowSize, bvh->GetBox(), camera);
 	
 	// frame time is restarted every frame
@@ -796,5 +796,5 @@
 	/////////////////
 
-	Matrix4x4 matProjection;
+	Matrix4x4 matViewing, matProjection;
 
 	camera->GetModelViewMatrix(matViewing);
@@ -904,5 +904,5 @@
 	case RenderState::DEFERRED:
 
-		shadowMapping->ComputeShadowMap(light, traverser);
+		shadowMap->ComputeShadowMap(light, traverser);
 
 		if (!fbo) InitFBO();
@@ -975,6 +975,6 @@
 	//-- render sky
 
-	// q: should we render sky after deferred shading? (would conveniently solves some issues)
-	// (e.g, skys without shadows)
+	// q: should we render sky after deferred shading? 
+	// this would conveniently solves some issues (e.g, skys without shadows)
 
 	RenderSky();
@@ -999,6 +999,5 @@
 			if (!deferredShader) deferredShader = new DeferredShader(texWidth, texHeight, myfar / 10.0f);
 			
-			//deferredShader->Render(fbo);
-			deferredShader->Render(fbo, matViewing, shadowMapping);
+			deferredShader->Render(fbo, shadowMap);
 		}
 	}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2893)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2894)
@@ -176,6 +176,5 @@
 
 void DeferredShader::Render(FrameBufferObject *fbo, 
-							const Matrix4x4 &matViewing, 
-							ShadowMapping *shadowMapping)
+							ShadowMap *shadowMap)
 {
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
@@ -201,5 +200,5 @@
 	glOrtho(-offs, offs, -offs, offs, 0, 1);
 
-	FirstPassShadow(fbo, matViewing, shadowMapping);
+	FirstPassShadow(fbo, shadowMap);
 	AntiAliasing(fbo);
 
@@ -328,42 +327,13 @@
 
 void DeferredShader::FirstPassShadow(FrameBufferObject *fbo, 
-									 const Matrix4x4 &matViewing, 
-									 ShadowMapping *shadowMapping)
+									 ShadowMap *shadowMap)
 {
 	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
 	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
-	GLuint shadowMap = shadowMapping->mFbo->GetDepthTex();
-/*
-	static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
-								0.0f, 0.5f, 0.0f, 0.0f,
-								0.0f, 0.0f, 0.5f, 0.0f,
-								0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1]
-*/
-	
-static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
-								0.0f, 0.5f, 0.0f, 0.5f,
-								0.0f, 0.0f, 0.5f, 0.5f,
-								0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1]
-
-	Matrix4x4 inverseView = matViewing;
-	inverseView.Invert();
-
-	Matrix4x4 lightProjView = shadowMapping->mLightViewMatrix * shadowMapping->mLightProjectionMatrix;
-
-	//cout << "matview:\n" << matViewing << endl;
-	//cout << "proj:\n" <<  shadowMapping->mLightProjectionMatrix << endl;
-	cout << "view:\n" <<  shadowMapping->mLightViewMatrix << endl;
-
-	// compute combined matrix that transforms pixels into light texture space
-	/*Matrix4x4 shadowMatrix = biasMatrix * 
-							 shadowMapping->mLightProjectionMatrix * 
-							 shadowMapping->mLightViewMatrix *
-							 inverseView;
-*/
-	//Matrix4x4 shadowMatrix = //inverseView * lightProjView * biasMatrix;*
-	Matrix4x4 shadowMatrix = lightProjView*biasMatrix; 
-							 //inverseView;
-							 cout << "combined:\n" <<  shadowMatrix << endl;
+	GLuint shadowTex = shadowMap->GetShadowTexture();
+
+	Matrix4x4 shadowMatrix;
+	shadowMap->GetTextureMatrix(shadowMatrix);
 
 	fbo->Bind();
@@ -386,5 +356,5 @@
 	cgGLEnableTextureParameter(sNormalsTexShadowParam);
 	
-	cgGLSetTextureParameter(sShadowMapParam, shadowMap);
+	cgGLSetTextureParameter(sShadowMapParam, shadowTex);
 	cgGLEnableTextureParameter(sShadowMapParam);
 
