Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2899)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2911)
@@ -4,7 +4,11 @@
 #include "RenderTraverser.h"
 #include "Light.h"
+#include "Polygon3.h"
+#include "Polyhedron.h"
+
 #include <IL/il.h>
 #include <assert.h>
 
+
 using namespace std;
 
@@ -12,4 +16,7 @@
 namespace CHCDemoEngine
 {
+
+
+static Polyhedron *polyhedron = NULL;
 
 
@@ -97,5 +104,65 @@
 
 
-void ShadowMap::ComputeShadowMap(RenderTraverser *renderer)
+void ShadowMap::DrawPolys()
+{
+	if (!polyhedron) return;
+
+	cout << "here3" << endl;
+
+	for (size_t i = 0; i < polyhedron->NumPolygons(); ++ i)
+	{
+		float r = (float)i / polyhedron->NumPolygons();
+		float g = 1;
+		float b = 1;
+
+		glColor3f(r, g, b);
+
+		glBegin(GL_LINE_LOOP);
+
+		Polygon3 *poly = polyhedron->GetPolygons()[i];
+
+		for (size_t j = 0; j < poly->mVertices.size(); ++ j)
+		{
+			Vector3 v = poly->mVertices[j];
+			glVertex3d(v.x, v.y, v.z);
+		}
+
+		glEnd();
+	}
+}
+
+
+void ShadowMap::CalcFocussedFrustum(const Matrix4x4 &camProjView, 
+									const Matrix4x4 &lightProjView, 
+									Matrix4x4 &focussed)
+{
+	///////////////////
+	//-- First step: calc clipped frustum
+
+	DEL_PTR(polyhedron);
+	polyhedron = mCamera->CalcClippedFrustum(mSceneBox);
+
+	if (polyhedron)
+	{
+		// second step: focus light frustum on clipped frustum
+		Frustum lightFrustum(lightProjView);
+
+		for (int i = 0; i < 6; ++ i)
+		{
+			// the clipping planes look outward the frustum,
+			// so distances > 0 mean that a point is outside
+			const float invLength = 1.0f / Magnitude(lightFrustum.mClipPlanes[i].mNormal);
+
+			lightFrustum.mClipPlanes[i].mD *= invLength;
+			lightFrustum.mClipPlanes[i].mNormal *= invLength;
+		}
+
+		//lightFrustum.EnclosePolyhedron(*polyhedron);
+		lightFrustum.ExtractTransformation(focussed);
+	}
+}
+
+
+void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView)
 {
 	const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f);
@@ -107,6 +174,9 @@
 	Vector3 pos = mSceneBox.Center();
 
+	Matrix4x4 camView;
+	mCamera->GetModelViewMatrix(camView);
+
 	pos -= mLight->GetDirection() * Magnitude(mSceneBox.Diagonal() * 0.5f);
-	mShadowCam->SetPosition(pos);
+	//mShadowCam->SetPosition(pos);
 
 	mFbo->Bind();
@@ -153,11 +223,44 @@
 	mShadowCam->GetProjectionMatrix(lightProj);
 
+	Matrix4x4 oldLightProjView = lightView * lightProj;
+	Matrix4x4 newLightProj;
+
+	CalcFocussedFrustum(projView, lightProj, newLightProj);
+	//CalcFocussedFrustum(projView, oldLightProjView, newLightProj);
+
+	Matrix4x4 dummyMat;
+	glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat.x);
+
+
+	cout << "old:\n" << dummyMat << endl;
+
+
+	glMatrixMode(GL_PROJECTION);
+	glLoadMatrixf((float *)newLightProj.x);
+	
+	glMatrixMode(GL_MODELVIEW);
+	//glLoadIdentity();
+
+	Matrix4x4 dummyMat3;
+	glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat3.x);
+
+	cout << "new:\n" << newLightProj << endl;
+
+	lightView = camView;
+	mLightProjView = lightView * newLightProj;
+	//mLightProjView = newLightProj;
+
+	//cout << "old: \n" << oldLightProjView << " new: \n " << mLightProjView << endl;
+	
+
 	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; 
+								0.0f, 0.0f, 0.0f, 1.0f);
+
+	mTextureMatrix = mLightProjView * biasMatrix; 
+	//mTextureMatrix = oldLightProjView * biasMatrix; 
+
+
 
 
@@ -176,6 +279,6 @@
 
 	glPopAttrib();
-	
-	/*float *data = new float[mSize * mSize];
+	/*
+	float *data = new float[mSize * mSize];
 
 	GrabDepthBuffer(data, mFbo->GetDepthTex());
@@ -183,7 +286,7 @@
 
 	delete [] data;
-	*/
+	
 	PrintGLerror("shadow map");
-
+*/
 	FrameBufferObject::Release();
 }
