Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2911)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2928)
@@ -153,2 +153,22 @@
 	return OUT;
 }
+
+
+/** The mrt shader for standard rendering
+*/
+pixel main_shadow_debug(fragment IN, 
+						uniform sampler2D shadowMap,
+						uniform sampler2D colorTex,
+						uniform float4x4 shadowMatrix)
+{
+	pixel OUT;
+#if 0
+	// visualize depth
+	OUT.color = tex2Dlod(colorTex, float4(IN.texCoord.xy, 0, 0));
+#else
+	float4 col;
+	col = tex2Dlod(shadowMap, float4(IN.texCoord.xy, 0, 0));
+	OUT.color = float4(col.z / col.w, 0, 0, 1);
+#endif
+	return OUT;
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2891)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2928)
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2920)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2928)
@@ -36,7 +36,10 @@
 	*/
 	void ComputeShadowMap(RenderTraverser *traverser, const Matrix4x4 &projView);
-	/** Returns computed shadow texture.
+	/** Returns computed shadow color texture.
 	*/
-	unsigned int GetShadowTexture() const;
+	unsigned int GetShadowColorTexture() const;
+	
+	unsigned int GetDepthTexture() const;
+
 	/** Returns computed texture matrix. It must be applied on the
 		the world space positions. 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2901)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 2928)
@@ -62,5 +62,8 @@
 protected:
 
-	void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor, const Matrix4x4 &oldProjViewMatrix, const Matrix4x4 &projViewMatrix);
+	void ComputeSsao(FrameBufferObject *fbo, 
+					 float tempCohFactor, 
+					 const Matrix4x4 &oldProjViewMatrix, 
+					 const Matrix4x4 &projViewMatrix);
 
 	void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor, const Matrix4x4 &oldProjViewMatrix);
@@ -78,4 +81,7 @@
 	*/
 	void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br);
+
+
+	void ShadowDebug(FrameBufferObject *fbo, ShadowMap *shadowMap);
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2925)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2928)
@@ -111,6 +111,7 @@
 	// the diffuse color buffer
 	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-
-	mShadowCam = new Camera(mSceneBox.Size().x * 0.5f, mSceneBox.Size().y * 0.5f);
+	//mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+
+	mShadowCam = new Camera(mSize, mSize);//mSceneBox.Size().x * 0.5f, mSceneBox.Size().y * 0.5f);
 	mShadowCam->SetOrtho(true);
 }
@@ -204,6 +205,7 @@
 										 )
 {
+	//return IdentityMatrix();
+
 	AxisAlignedBox3 bounds_ls = GetExtremalPoints(lightSpace, body);
-	//return IdentityMatrix();
 
 	///////////////
@@ -212,5 +214,6 @@
 
 	//const float n = 1e6f;
-	const float n = ComputeN(bounds_ls);
+	//const float n = 1e1f;
+	const float n = ComputeN(bounds_ls) * 100;
 
 	cout << "n: " << n << endl;
@@ -228,5 +231,5 @@
 
 	// the new projection center
-	Vector3	projCenter = startPt + Vector3::UNIT_Z() * n * 1000;
+	Vector3	projCenter = startPt + Vector3::UNIT_Z() * n;
 
 	cout <<"start: " << startPt << " " << projCenter << " " << Distance(lightSpace * mCamera->GetPosition(), startPt) << endl;
@@ -245,5 +248,5 @@
 	
 
-	////////
+	//////////
 	//-- now apply these values to construct the perspective lispsm matrix
 
@@ -518,5 +521,5 @@
 	//mCamera->GetModelViewMatrix(camView);
 
-	pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.1f);
+	pos -= dir * Magnitude(mSceneBox.Diagonal() * 0.5f);
 	mShadowCam->SetPosition(pos);
 
@@ -530,4 +533,6 @@
 	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);
@@ -597,4 +602,9 @@
 
 	glPopAttrib();
+
+	
+	glEnable(GL_LIGHTING);
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+
 #if 0
 	float *data = new float[mSize * mSize];
@@ -617,10 +627,14 @@
 
  
-unsigned int ShadowMap::GetShadowTexture() const
+unsigned int ShadowMap::GetDepthTexture() const
 {
 	return mFbo->GetDepthTex();
 }
 
-
+unsigned int ShadowMap::GetShadowColorTexture() const
+{
+	return mFbo->GetColorBuffer(0)->GetTexture();
+	
+}
 
 } // namespace
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2927)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2928)
@@ -914,4 +914,5 @@
 			{
 				const float shadowSize = 4096;
+				//const float shadowSize = 512;
 				shadowMap = new ShadowMap(light, shadowSize, bvh->GetBox(), camera);
 			}
@@ -925,9 +926,24 @@
 			shadowChanged = false;
 
+#if 0
+			state.SetRenderType(RenderState::DEFERRED);
+
+			fbo->Bind();
+
+			glPushAttrib(GL_VIEWPORT_BIT);
+			glViewport(0, 0, texWidth, texHeight);
+
+			cgGLEnableProfile(RenderState::sCgFragmentProfile);
+			cgGLBindProgram(RenderState::sCgMrtFragmentProgram);
+
+			cgGLEnableProfile(RenderState::sCgVertexProfile);
+			cgGLBindProgram(sCgMrtVertexProgram);
+#else
+
 			cgGLDisableProfile(RenderState::sCgFragmentProfile);
 			cgGLDisableProfile(RenderState::sCgVertexProfile);
 
 			state.SetRenderType(RenderState::DEPTH_PASS);
-
+#endif
 			// the scene is rendered withouth any shading	
 			shadowMap->ComputeShadowMap(shadowTraverser, matProjectionView);
@@ -936,6 +952,4 @@
 			BvhNode::SetCurrentState(0);
 
-			glEnable(GL_LIGHTING);
-			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 		}
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2903)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 2928)
@@ -22,4 +22,5 @@
 static CGprogram sCgAntiAliasingProgram = NULL;
 static CGprogram sCgDeferredShadowProgram = NULL;
+static CGprogram sCgShadowDebugProgram = NULL;
 
 static CGparameter sColorsTexCombineParam;
@@ -29,4 +30,8 @@
 static CGparameter sPositionsTexDeferredParam;
 static CGparameter sNormalsTexDeferredParam;
+
+static CGparameter sShadowMapDebugParam;  
+static CGparameter sShadowMatrixDebugParam;
+static CGparameter sColorsTexDebugParam;
 
 
@@ -431,4 +436,25 @@
 	else
 		cerr << "deferred program failed to load" << endl;
+
+	sCgShadowDebugProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/deferred.cg", 
+								RenderState::sCgFragmentProfile,
+								"main_shadow_debug",
+								NULL);
+
+	if (sCgShadowDebugProgram != NULL)
+	{
+		cgGLLoadProgram(sCgShadowDebugProgram);
+
+		// we need size of texture for scaling
+		sShadowMapDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "shadowMap");  
+		sShadowMatrixDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "shadowMatrix");
+		sColorsTexDebugParam = cgGetNamedParameter(sCgShadowDebugProgram, "colorTex");  
+	}
+	else
+		cerr << "shadow debug program failed to load" << endl;
+
 	PrintGLerror("init");
 }
@@ -471,26 +497,32 @@
 	glLoadIdentity();
 
-	if (shadowMap)
-		FirstPassShadow(fbo, shadowMap);
+	if (shadowMap && (mShadingMethod == GI))
+	{
+		ShadowDebug(fbo, shadowMap);
+	}
 	else
-		FirstPass(fbo);
-	
-	switch (mShadingMethod)
-	{
-	case SSAO:
-		ComputeSsao(fbo, tempCohFactor, oldProjViewMatrix, projViewMatrix);
-		CombineSsao(fbo);
-		break;
-	case GI:
-		ComputeGlobIllum(fbo, tempCohFactor, oldProjViewMatrix);
-		CombineIllum(fbo);
-		break;
-	default: // DEFAULT
-		// do nothing: standard deferred shading
-		break;
-	}
-
-	AntiAliasing(fbo);
-
+	{
+		if (shadowMap)
+			FirstPassShadow(fbo, shadowMap);
+		else
+			FirstPass(fbo);
+
+		switch (mShadingMethod)
+		{
+		case SSAO:
+			ComputeSsao(fbo, tempCohFactor, oldProjViewMatrix, projViewMatrix);
+			CombineSsao(fbo);
+			break;
+		case GI:
+			ComputeGlobIllum(fbo, tempCohFactor, oldProjViewMatrix);
+			CombineIllum(fbo);
+			break;
+		default: // DEFAULT
+			// do nothing: standard deferred shading
+			break;
+		}
+
+		AntiAliasing(fbo);
+	}
 	glEnable(GL_LIGHTING);
 	glDisable(GL_TEXTURE_2D);
@@ -735,4 +767,5 @@
 	PrintGLerror("deferred shading");
 }
+
 
 
@@ -952,5 +985,5 @@
 	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
-	GLuint shadowTex = shadowMap->GetShadowTexture();
+	GLuint shadowTex = shadowMap->GetDepthTexture();
 
 	Matrix4x4 shadowMatrix;
@@ -1008,4 +1041,47 @@
 
 
+void DeferredRenderer::ShadowDebug(FrameBufferObject *fbo, ShadowMap *shadowMap)
+{
+	GLuint colorsTex = shadowMap->GetShadowColorTexture();
+	GLuint shadowTex = shadowMap->GetDepthTexture();
+	
+
+	Matrix4x4 shadowMatrix;
+	shadowMap->GetTextureMatrix(shadowMatrix);
+
+	//glMatrixMode(GL_PROJECTION);glLoadIdentity();
+	//glViewport(0, 0, shadowMap->GetSize(), shadowMap->GetSize());
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	cgGLBindProgram(sCgShadowDebugProgram);
+
+	cgGLSetTextureParameter(sShadowMapDebugParam, shadowTex);
+	cgGLSetTextureParameter(sColorsTexDebugParam, colorsTex);
+
+	cgGLEnableTextureParameter(sShadowMapDebugParam);
+
+	cgGLSetMatrixParameterfc(sShadowMatrixDebugParam, (const float *)shadowMatrix.x);
+
+	glColor3f(1.0f, 1.0f, 1.0f);
+	
+	glBegin(GL_QUADS);
+
+	float offs2 = 0.5f;
+
+	glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
+	glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
+	glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
+	glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
+
+	glEnd();
+
+	cgGLDisableTextureParameter(sColorsTexDebugParam);
+	cgGLDisableTextureParameter(sShadowMapDebugParam);
+
+	PrintGLerror("shadows debug");
+}
+
+
 void DeferredRenderer::SetSamplingMethod(SAMPLING_METHOD s) 
 { 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2927)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2928)
@@ -12,5 +12,5 @@
 
 // our coordinate system has the positive z axis pointing up
-static Vector3 baseDir = Vector3(0, 1, 0);
+static const Vector3 baseDir = -Vector3::UNIT_Y();
 
 
@@ -49,11 +49,11 @@
 
 
-void Camera::Precompute(const Vector3 &direction) 
-{
-	Vector3 up = Vector3(0, 0, 1);
-	Vector3 right = Normalize(CrossProd(up, direction));
-	up = Normalize(CrossProd(direction, right));
-
-	mBaseOrientation = Matrix4x4(right, up, direction);
+void Camera::Precompute(const Vector3 &dir) 
+{
+	Vector3 up = Vector3::UNIT_Z();
+	Vector3 right = Normalize(CrossProd(dir, up));
+	up = Normalize(CrossProd(right, dir));
+
+	mBaseOrientation = Matrix4x4(right, up, -dir);
 	mViewOrientation = mBaseOrientation;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2911)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2928)
@@ -816,8 +816,4 @@
 			</File>
 			<File
-				RelativePath=".\src\shaders\shadow.cg"
-				>
-			</File>
-			<File
 				RelativePath=".\src\shaders\ssao.cg"
 				>
