Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2892)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2893)
@@ -80,5 +80,6 @@
 				  uniform sampler2D normals,		   
 				  uniform sampler2D shadowMap,
-				  uniform float4x4 shadowMatrix
+				  uniform float4x4 shadowMatrix,
+				  uniform float maxDepth
 				  )
 {
@@ -89,7 +90,4 @@
 	float4 position = tex2D(positions, IN.texCoord.xy);
 
-	float4 lightSpacePos = mul(shadowMatrix, position);
-
-	float shadowDepth = tex2D(shadowMap, IN.texCoord.xy);
 
 	// an ambient color term
@@ -99,12 +97,21 @@
 	float4 col = shade(IN, color, position, normal, amb);
 	
+	position *= maxDepth;
+	position.w = 1.0f;
+	
+	float4 lightSpacePos = mul(shadowMatrix, position);
+
+	float shadowDepth = tex2D(shadowMap, lightSpacePos.xy);
+
 	OUT.color = col;
 	
-	if (lightSpacePos.z / lightSpacePos.w < shadowDepth)
+	// hack: prevent shadowing the sky
+	if ((amb < 0.9) &&	
+		(lightSpacePos.z / lightSpacePos.w > shadowDepth))
 	{
 		OUT.color *= 0.1f;
 	}
 	
-	//OUT.color = float4(lightSpacePos.z / lightSpacePos.w);
+	//OUT.color = lightSpacePos;//float4(lightSpacePos.z / lightSpacePos.w);
 	//OUT.color = float4(shadowDepth);
 	OUT.color.w = color.w;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2892)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2893)
@@ -71,4 +71,6 @@
 		float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
 
+		//if ((texcoord.x <= 1.0f) || (texcoord.x >= 0.0f) || (texcoord.y <= 1.0f) || (texcoord.y >= 0.0f))
+		
 		// sample downsampled texture in order to speed up texture accesses
 		float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2891)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2893)
@@ -527,4 +527,5 @@
 	cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
 	
+
 	if (mUseTemporalCoherence)
 	{
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h	(revision 2892)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h	(revision 2893)
@@ -24,5 +24,5 @@
 	/** constructor for a deferred shader taking the requested output image size
 	*/
-	DeferredShader(int w, int h);
+	DeferredShader(int w, int h, float maxDepth);
 
 	~DeferredShader();
@@ -57,5 +57,5 @@
 	int mHeight;
 
-	//FrameBufferObject *mFbo;
+	float mScaleFactor;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2892)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2893)
@@ -124,6 +124,6 @@
 	mLight = light;
 
-	const float xlen = Magnitude(mSceneBox.Diagonal());// * 0.5f);
-	const float ylen = Magnitude(mSceneBox.Diagonal());// * 0.5f);
+	const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f);
+	const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f);
 	
 	mShadowCam->SetDirection(light->GetDirection());
@@ -131,5 +131,7 @@
 	// set position so that we can see the whole scene
 	Vector3 pos = mSceneBox.Center();
-	pos.z = mSceneBox.Max().z;
+
+	pos -= light->GetDirection() * Magnitude(mSceneBox.Diagonal() * 0.5f);
+	//pos.z = mSceneBox.Max().z;
 	//mShadowCam->SetPosition(pos - Magnitude(mSceneBox.Diagonal() * light->GetDirection()));
 	mShadowCam->SetPosition(pos);
@@ -148,5 +150,5 @@
 	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 
-	glPolygonOffset(1.0f, 40.0f);
+	glPolygonOffset(1.0f, 2000.0f);
 	glEnable(GL_POLYGON_OFFSET_FILL);
 
@@ -158,5 +160,5 @@
 	glLoadIdentity();
 	
-	glOrtho(+xlen, -xlen, +ylen, -ylen, 0.0f, 100);//Magnitude(mSceneBox.Diagonal())); 
+	glOrtho(+xlen, -xlen, +ylen, -ylen, 0.0f, Magnitude(mSceneBox.Diagonal())); 
 	
 
@@ -267,4 +269,5 @@
 void ShadowMapping::Render(RenderTraverser *traverser, Camera *cam)
 {
+#if 0
 	Matrix4x4 cam_inverse_modelview;
 
@@ -391,4 +394,5 @@
 	glDisable(GL_LIGHTING);
 	glDisable(GL_ALPHA_TEST);
+#endif
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2892)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2893)
@@ -443,7 +443,8 @@
 	sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
 
-	light = new Light(Vector3(0, 0, -1), RgbaColor(1, 1, 1, 1));
-
-	shadowMapping = new ShadowMapping(bvh->GetBox(), 512);
+	Vector3 lightDir = Normalize(Vector3(0, 1, -1));
+	light = new Light(lightDir, RgbaColor(1, 1, 1, 1));
+
+	shadowMapping = new ShadowMapping(bvh->GetBox(), 4096);
 	
 	// frame time is restarted every frame
@@ -962,5 +963,5 @@
 	glEnableClientState(GL_NORMAL_ARRAY);
 
-
+	
 	// reset depth pass and render visible objects
 	if (renderType == RenderState::DEPTH_PASS) 
@@ -969,15 +970,14 @@
 		RenderVisibleObjects();
 	}
-	
+
 
 	///////////////
 	//-- render sky
 
+	// q: should we render sky after deferred shading? (would conveniently solves some issues)
+	// (e.g, skys without shadows)
+
 	RenderSky();
-
-	state.Reset();
-
-	glDisableClientState(GL_VERTEX_ARRAY);
-	glDisableClientState(GL_NORMAL_ARRAY);
+	
 
 
@@ -997,5 +997,5 @@
 		else
 		{
-			if (!deferredShader) deferredShader = new DeferredShader(texWidth, texHeight);
+			if (!deferredShader) deferredShader = new DeferredShader(texWidth, texHeight, myfar / 10.0f);
 			
 			//deferredShader->Render(fbo);
@@ -1003,9 +1003,17 @@
 		}
 	}
-	
-	
+
+
+	state.SetRenderType(RenderState::FIXED);
+	state.Reset();
+
+
+	glDisableClientState(GL_VERTEX_ARRAY);
+	glDisableClientState(GL_NORMAL_ARRAY);
+
+
+
 	///////////
 
-	state.SetRenderType(RenderState::FIXED);
 
 	if (showAlgorithmTime)
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2892)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2893)
@@ -46,10 +46,10 @@
 
 static CGparameter sShadowMatrixParam;
-
-
-
-
-DeferredShader::DeferredShader(int w, int h):
-mWidth(w), mHeight(h)
+static CGparameter sMaxDepthParam;
+
+
+
+DeferredShader::DeferredShader(int w, int h, float scaleFactor):
+mWidth(w), mHeight(h), mScaleFactor(scaleFactor)
 {
 	//mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
@@ -124,5 +124,5 @@
 		sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals"); 
 		sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");  
-
+		sMaxDepthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth");
 		sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
 	}
@@ -184,4 +184,6 @@
 	glDisable(GL_TEXTURE_2D);
 	glDisable(GL_LIGHTING);
+	glDepthMask(GL_FALSE);
+	
 
 	glPushAttrib(GL_VIEWPORT_BIT);
@@ -203,5 +205,6 @@
 
 	glEnable(GL_LIGHTING);
-	glDisable(GL_TEXTURE_2D);
+	glEnable(GL_TEXTURE_2D);
+	glDepthMask(GL_TRUE);
 	
 	glMatrixMode(GL_PROJECTION);
@@ -332,9 +335,15 @@
 	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;
@@ -345,5 +354,5 @@
 	//cout << "matview:\n" << matViewing << endl;
 	//cout << "proj:\n" <<  shadowMapping->mLightProjectionMatrix << endl;
-	//cout << "view:\n" <<  shadowMapping->mLightViewMatrix << endl;
+	cout << "view:\n" <<  shadowMapping->mLightViewMatrix << endl;
 
 	// compute combined matrix that transforms pixels into light texture space
@@ -353,12 +362,7 @@
 							 inverseView;
 */
-	Matrix4x4 shadowMatrix = //inverseView * 
-							 lightProjView * 
-							 biasMatrix;
-/*
-Matrix4x4 shadowMatrix = biasMatrix * 
-							 lightProjView; 
+	//Matrix4x4 shadowMatrix = //inverseView * lightProjView * biasMatrix;*
+	Matrix4x4 shadowMatrix = lightProjView*biasMatrix; 
 							 //inverseView;
-*/
 							 cout << "combined:\n" <<  shadowMatrix << endl;
 
@@ -384,4 +388,6 @@
 	cgGLSetTextureParameter(sShadowMapParam, shadowMap);
 	cgGLEnableTextureParameter(sShadowMapParam);
+
+	cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
 
 	cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2891)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2893)
@@ -86,5 +86,13 @@
 void Camera::GetModelViewMatrix(Matrix4x4 &mat)
 {
-	glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
+	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);
 }
 
@@ -154,10 +162,7 @@
 void Camera::SetupCameraView()
 {
-	Matrix4x4 tview = mViewOrientation;
-
-	Vector3 pos = -mPosition;
-	pos = tview * pos;
-
 	Matrix4x4 viewOrientation = mViewOrientation;
+
+	Vector3 pos = mViewOrientation * -mPosition;
 
 	viewOrientation.x[3][0] = pos.x;
