Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h	(revision 2919)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.h	(revision 2932)
@@ -160,4 +160,5 @@
 Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far);
 Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up);
+Matrix4x4 MyLookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up);
 
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h	(revision 2931)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.h	(revision 2932)
@@ -40,4 +40,5 @@
 class Camera
 {
+	friend class ShadowMap;
 public:
 	
@@ -46,5 +47,6 @@
 	Camera(int width, int height, float fieldOfView = 90.f);
 
-
+	/** Sets the current camera position.
+	*/
 	void SetPosition(const Vector3 &pos);
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2931)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2932)
@@ -109,6 +109,4 @@
 
 	Matrix4x4 mLightProjView;
-
-	Matrix4x4 mLightMatrix;
 };
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2931)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2932)
@@ -23,4 +23,5 @@
 static Polyhedron *polyhedron = NULL;
 static Polyhedron *lightPoly = NULL;
+static Matrix4x4 dummy = IdentityMatrix();
 
 
@@ -183,4 +184,14 @@
 	DrawPoly(lightPoly, Vector3(1, 0, 1));
 	DrawPoly(polyhedron, Vector3(0, 1, 0));
+
+	Vector3 pt = Vector3::ZERO();
+
+	pt = dummy * pt;
+
+	glPointSize(10.0f);
+
+	glBegin(GL_POINTS);
+	glVertex3f(pt.x, pt.y, pt.z);
+	glEnd();
 }
 
@@ -212,6 +223,6 @@
 	//-- first find the free parameter values n, and P (the projection center), and the projection depth
 
-	//const float n = 1e2f;
-	const float n = 1e6f;
+	const float n = 1e2f;
+	//const float n = 1e6f;
 	//const float n = ComputeN(bounds_ls) * 100;
 
@@ -357,5 +368,5 @@
 	cout << "dummy: " << Normalize(dummyVec2 - dummyVec) << endl;
 	//project the view direction into the shadow map plane
-	projDir.y = 0.0;
+	projDir.y = .0f;
 
 	return Normalize(projDir);
@@ -401,5 +412,5 @@
 
 	//switch to the lightspace used in the article
-	lightProj = lightProj * transform2LispSM;
+	//lightProj = lightProj * transform2LispSM;
 
 	const Vector3 projViewDir = GetProjViewDir(lightView * lightProj, frustumPoints);
@@ -412,8 +423,8 @@
 	//calculate a frame matrix that uses the projViewDir[lightspace] as up vector
 	//look(from position, into the direction of the projected direction, with unchanged up-vector)
-	const Matrix4x4 frame = LookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
+	const Matrix4x4 frame = MyLookAt(Vector3::ZERO(), projViewDir, Vector3::UNIT_Y());
 
 	cout << "frame\n " << frame << endl;
-	lightProj = lightProj * frame;
+	//lightProj = lightProj * frame;
 
 	cout << "here9\n" << lightProj << endl;
@@ -421,8 +432,4 @@
 	const Matrix4x4 matLispSM = 
 		CalcLispSMTransform(lightView * lightProj, extremalPoints, frustumPoints);
-
-	Vector3 mydummy = projViewDir;//Vector3::UNIT_Z();
-	//cout << "transformed unit z vector: " << Normalize(lightView * lightProj * mydummy) << endl;
-	cout << "transformed unit z vector: " << Normalize(frame * mydummy) << endl;
 
 	lightProj = lightProj * matLispSM;
@@ -436,5 +443,5 @@
 	transformToGL.x[3][3] =  1.0f;
 
-	lightProj = lightProj * transformToGL;
+	//lightProj = lightProj * transformToGL;
 	//cout << "here4 \n" << lightProj << endl;
 
@@ -485,131 +492,4 @@
 	return Normalize(up);
 }
-
-
-void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView)
-{
-	const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f);
-	const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f);
-	
-	//const Vector3 dir = mLight->GetDirection();
-	const Vector3 dir(0, 0, -1);
-
-	Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
-	mLightMatrix = LookAt(mCamera->GetPosition(), dir, upVec);
-
-	mShadowCam->SetDirection(dir);
-	
-	//cout << "lightdir: " << mShadowCam->GetDirection() << endl;
-
-	// set position so that we can see the whole scene
-	Vector3 pos = mSceneBox.Center();
-
-	//Matrix4x4 camView;
-	//mCamera->GetModelViewMatrix(camView);
-
-	pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
-	mShadowCam->SetPosition(pos);
-
-	mFbo->Bind();
-	
-	glDrawBuffers(1, mrt);
-
-	glPushAttrib(GL_VIEWPORT_BIT);
-	glViewport(0, 0, mSize, mSize);
-
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	//glEnable(GL_LIGHTING);
-	//glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-	glDisable(GL_LIGHTING);
-	glDisable(GL_TEXTURE_2D);
-	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
-
-	glPolygonOffset(1.0f, 2000.0f);
-	glEnable(GL_POLYGON_OFFSET_FILL);
-
-	glShadeModel(GL_FLAT);
-	glEnable(GL_DEPTH_TEST);
-
-	// setup projection
-	/*glMatrixMode(GL_PROJECTION);
-	glLoadIdentity();
-	glOrtho(+xlen, -xlen, +ylen, -ylen, 0.0f, Magnitude(mSceneBox.Diagonal())); 
-
-	Matrix4x4 dummyMat;
-	glGetFloatv(GL_PROJECTION_MATRIX, (float *)dummyMat.x);
-	cout << "old:\n" << dummyMat << endl;
-	*/
-
-	Matrix4x4 lightView, lightProj;
-
-	//mShadowCam->GetModelViewMatrix(lightView);
-	lightView = mLightMatrix;
-
-	CalcLightProjection(lightProj);
-
-	glMatrixMode(GL_PROJECTION);
-	glPushMatrix();
-	glLoadMatrixf((float *)lightProj.x);
-
-	mLightProjView = lightView * lightProj;
-
-	DEL_PTR(lightPoly);
-	lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
-
-	//cout << "new:\n" << lightProj << endl;
-
-	glMatrixMode(GL_MODELVIEW);
-	glPushMatrix();
-	glLoadIdentity();
-
-	//mShadowCam->SetupCameraView();
-	glLoadMatrixf((float *)mLightMatrix.x);
-
-	//////////////
-	//-- compute texture matrix
-
-	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);
-
-	mTextureMatrix = mLightProjView * biasMatrix; 
-
-
-
-
-	/////////////
-	//-- render scene into shadow map
-
-	renderer->RenderScene();
-
-	
-	glDisable(GL_POLYGON_OFFSET_FILL);
-	glMatrixMode(GL_MODELVIEW);
-	glPopMatrix();
-
-	glMatrixMode(GL_PROJECTION);
-	glPopMatrix();
-
-	glPopAttrib();
-
-	
-	glEnable(GL_LIGHTING);
-	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-
-#if 0
-	float *data = new float[mSize * mSize];
-
-	GrabDepthBuffer(data, mFbo->GetDepthTex());
-	ExportDepthBuffer(data, mSize);
-
-	delete [] data;
-	
-	PrintGLerror("shadow map");
-#endif
-	FrameBufferObject::Release();
-}
-
 
 
@@ -667,34 +547,51 @@
 
 
-void ShadowMap::RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView)
-{
+void ShadowMap::ComputeShadowMap(RenderTraverser *renderer, const Matrix4x4 &projView)
+{
+	const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f);
+	const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f);
+	
 	//const Vector3 dir = mLight->GetDirection();
 	const Vector3 dir(0, 0, -1);
 
-	mShadowCam->SetDirection(dir);
+	// set position so that we can see the whole scene
+	//Vector3 pos = mSceneBox.Center();
+	//pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
+	//mShadowCam->SetPosition(pos);
+	mShadowCam->SetPosition(mCamera->GetPosition());
 
 	Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
-	mLightMatrix = LookAt(mCamera->GetPosition(), dir, upVec);
-
-	// set position so that we can see the whole scene
-	Vector3 pos = mSceneBox.Center();
-
-	pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
-	mShadowCam->SetPosition(pos);
+	Matrix4x4 lightView = 
+		MyLookAt(mShadowCam->GetPosition(), dir, upVec);
+
+	mShadowCam->mViewOrientation = lightView;
+
+	//cout << "here45:\n" << lightView << endl;
+
+	//mShadowCam->SetDirection(dir);
+	//mShadowCam->GetModelViewMatrix(lightView);
+	//cout << "here46:\n" << lightView << endl;
+
+	mFbo->Bind();
+	
+	glDrawBuffers(1, mrt);
+
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, mSize, mSize);
 
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
-	glEnable(GL_LIGHTING);
-	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+	glDisable(GL_LIGHTING);
+	glDisable(GL_TEXTURE_2D);
+	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 
 	glPolygonOffset(1.0f, 2000.0f);
 	glEnable(GL_POLYGON_OFFSET_FILL);
-glDisable(GL_CULL_FACE);
+
+	glShadeModel(GL_FLAT);
 	glEnable(GL_DEPTH_TEST);
 
-	Matrix4x4 lightView, lightProj;
-	
-	//mShadowCam->GetModelViewMatrix(lightView);
-	lightView = mLightMatrix;
+	Matrix4x4 lightProj;
+
 	CalcLightProjection(lightProj);
 
@@ -705,8 +602,8 @@
 	mLightProjView = lightView * lightProj;
 
+	cout << "here3" << endl;
 	DEL_PTR(lightPoly);
 	lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
-
-	//cout << "new:\n" << lightProj << endl;
+	cout << "here4\n" << lightView << endl;
 
 	glMatrixMode(GL_MODELVIEW);
@@ -714,7 +611,102 @@
 	glLoadIdentity();
 
-	//mShadowCam->SetupCameraView();
-	glLoadMatrixf((float *)mLightMatrix.x);
-
+	mShadowCam->SetupCameraView();
+
+
+	//////////////
+	//-- compute texture matrix
+
+	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);
+
+	mTextureMatrix = mLightProjView * biasMatrix; 
+
+
+
+
+	/////////////
+	//-- render scene into shadow map
+
+	renderer->RenderScene();
+
+	
+	glDisable(GL_POLYGON_OFFSET_FILL);
+	glMatrixMode(GL_MODELVIEW);
+	glPopMatrix();
+
+	glMatrixMode(GL_PROJECTION);
+	glPopMatrix();
+
+	glPopAttrib();
+
+	
+	glEnable(GL_LIGHTING);
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+
+#if 0
+	float *data = new float[mSize * mSize];
+
+	GrabDepthBuffer(data, mFbo->GetDepthTex());
+	ExportDepthBuffer(data, mSize);
+
+	delete [] data;
+	
+	PrintGLerror("shadow map");
+#endif
+	FrameBufferObject::Release();
+}
+
+
+void ShadowMap::RenderShadowView(RenderTraverser *renderer, const Matrix4x4 &projView)
+{
+	//const Vector3 dir = mLight->GetDirection();
+	const Vector3 dir(0, 0, -1);
+
+	mShadowCam->SetDirection(dir);
+
+	// set position so that we can see the whole scene
+	Vector3 pos = mSceneBox.Center();
+	pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
+
+	mShadowCam->SetPosition(mCamera->GetPosition());
+
+	Vector3 upVec = CalcUpVec(mCamera->GetDirection(), dir);
+	Matrix4x4 lightView = MyLookAt(mShadowCam->GetPosition(), dir, upVec);
+
+	mShadowCam->mViewOrientation = lightView;
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	glEnable(GL_LIGHTING);
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+
+	glPolygonOffset(1.0f, 2000.0f);
+	glEnable(GL_POLYGON_OFFSET_FILL);
+	
+	glEnable(GL_DEPTH_TEST);
+
+	Matrix4x4 lightProj;
+	
+	CalcLightProjection(lightProj);
+
+	glMatrixMode(GL_PROJECTION);
+	glPushMatrix();
+	glLoadMatrixf((float *)lightProj.x);
+
+	mLightProjView = lightView * lightProj;
+
+	dummy = mLightProjView;
+
+	DEL_PTR(lightPoly);
+	lightPoly = CreatePolyhedron(mLightProjView, mSceneBox);
+
+	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
+	glLoadIdentity();
+
+	mShadowCam->SetupCameraView();
+	
 
 	/////////////
@@ -736,4 +728,5 @@
 	
 	DrawPoly(hpoly, Vector3(1, 1, 1));
+
 	DEL_PTR(hpoly);
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2931)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2932)
@@ -1569,5 +1569,5 @@
 
 	//const float offs = box.Size().x * 0.3f;
-	const float offs = box.Size().x * 0.4f;
+	const float offs = box.Size().x * 0.6f;
 	
 	Vector3 vizpos = Vector3(box.Min().x, box.Min().y  - box.Size().y * 0.35f, box.Min().z + box.Size().z * 50);
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2924)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Matrix4x4.cpp	(revision 2932)
@@ -731,3 +731,28 @@
 }
 
-}
+
+Matrix4x4 MyLookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up) 
+{
+	const Vector3 nDir = Normalize(dir);
+	Vector3 nUp = Normalize(up);
+
+	Vector3 nRight = Normalize(CrossProd(nDir, nUp));
+	nUp = Normalize(CrossProd(nRight, nDir));
+
+	Matrix4x4 m(nRight, nUp, -nDir);
+
+	/*m.x[3][0] = -DotProd(nRight, position);
+	m.x[3][1] = -DotProd(nUp, position);
+	m.x[3][2] = -DotProd(nDir, position);*/
+
+	Vector3 position = m * -pos;
+	
+	// note: left handet system => we go into positive z
+	m.x[3][0] = position.x;
+	m.x[3][1] = position.y;
+	m.x[3][2] = position.z;
+
+	return m;
+}
+
+}
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2930)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2932)
@@ -293,6 +293,5 @@
 								CG_SOURCE,
 #ifdef USE_3D_SSAO
-								"src/shaders/ssao3d.cg", 
-
+								"src/shaders/ssao3d.cg",
 #else
 								"src/shaders/ssao.cg", 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2931)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2932)
@@ -11,5 +11,6 @@
 using namespace std;
 
-// our coordinate system has the positive z axis pointing up
+// our coordinate system is left-handed
+// we look into positive y and have the positive z axis pointing up
 static const Vector3 baseDir = -Vector3::UNIT_Y();
 
@@ -88,11 +89,8 @@
 	mat = mViewOrientation;
 
-	Vector3 pos = mViewOrientation * -mPosition;
-	
-	mat.x[3][0] = pos.x;
-	mat.x[3][1] = pos.y;
-	mat.x[3][2] = pos.z;
-
-	//glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
+	// note: left handed system => we go into positive z
+	mat.x[3][0] = -DotProd(GetRightVector(), mPosition);
+	mat.x[3][1] = -DotProd(GetUpVector(), mPosition);
+	mat.x[3][2] = DotProd(GetDirection(), mPosition);
 }
 
@@ -127,13 +125,8 @@
 void Camera::SetupCameraView()
 {
-	Matrix4x4 viewOrientation = mViewOrientation;
-
-	Vector3 pos = mViewOrientation * -mPosition;
-
-	viewOrientation.x[3][0] = pos.x;
-	viewOrientation.x[3][1] = pos.y;
-	viewOrientation.x[3][2] = pos.z;
-
-	glLoadMatrixf((float *)viewOrientation.x);
+	Matrix4x4 m;
+	GetModelViewMatrix(m);
+
+	glLoadMatrixf((float *)m.x);
 }
 
