Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 2943)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 2948)
@@ -28,5 +28,7 @@
 		FIXED,
 		DEPTH_PASS,
-		DEFERRED
+		DEFERRED,
+		DEPTH_PASS_DEFERRED,
+		NUM_RENDER_TYPES
 	};
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h	(revision 2946)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h	(revision 2948)
@@ -35,4 +35,6 @@
 	bool Load(const std::string &filename, SceneEntityContainer &geometry);
 
+	int GetNumEntities() const { return (int)mSceneEntities.size(); }
+
 protected:
 	
@@ -60,4 +62,5 @@
 	////////
 
+	/// the scene entities
 	SceneEntityContainer mSceneEntities;
 };
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.cpp	(revision 2840)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.cpp	(revision 2948)
@@ -24,5 +24,5 @@
 	if (!mMatrix) return;
 
-	if (state->GetRenderType() == RenderState::DEFERRED)
+	if ((state->GetRenderType() == RenderState::DEFERRED) || (state->GetRenderType() == RenderState::DEPTH_PASS_DEFERRED))
 	{
 		cgGLSetMatrixParameterfc(sModelMatrixParam, (const float *)mMatrix->x);
@@ -40,5 +40,5 @@
 	if (!mMatrix) return;
 
-	if (state->GetRenderType() == RenderState::DEFERRED)
+	if ((state->GetRenderType() == RenderState::DEFERRED) || (state->GetRenderType() == RenderState::DEPTH_PASS_DEFERRED))
 	{
 		static Matrix4x4 identity = IdentityMatrix();
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2946)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2948)
@@ -113,5 +113,4 @@
 static int texHeight = 768;
 
-
 int renderedObjects = 0;
 int renderedNodes = 0;
@@ -172,5 +171,6 @@
 bool useLODs = true;
 
-DeferredRenderer::SAMPLING_METHOD samplingMethod = DeferredRenderer::SAMPLING_POISSON;
+//DeferredRenderer::SAMPLING_METHOD samplingMethod = DeferredRenderer::SAMPLING_POISSON;
+DeferredRenderer::SAMPLING_METHOD samplingMethod = DeferredRenderer::SAMPLING_QUADRATIC;
 
 bool useAdvancedShading = false;
@@ -183,8 +183,7 @@
 bool renderLightView = false;
 
-
-
 ShadowMap *shadowMap = NULL;
 Light *light = NULL;
+DeferredRenderer *ssaoShader = NULL;
 
 
@@ -227,12 +226,11 @@
 void PlaceViewer(const Vector3 &oldPos);
 
-
-
 inline float KeyRotationAngle() { return keyRotation * elapsedTime * 1e-3f; }
 inline float KeyShift() { return keyForwardMotion * elapsedTime * 1e-3f; }
-
 void InitFBO();
-
-DeferredRenderer *ssaoShader = NULL;
+void RenderShadowMap();
+
+
+
 
 
@@ -555,4 +553,5 @@
 	// we store colors, normals, positions (for the ssao)
 	fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_32);
+	//fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_24);
 
 	// the diffuse color buffer
@@ -592,5 +591,5 @@
 void InitGLstate() 
 {
-	glClearColor(0.0f, 0.0, 0.2f, 0.0f);
+	glClearColor(0.7f, 0.0f, 0.2f, 0.0f);
 	
 	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@@ -737,5 +736,5 @@
 	tr->SetUseMultiQueries(useMultiQueries);
 	tr->SetUseTightBounds(useTightBounds);
-	tr->SetUseDepthPass(renderType == RenderState::DEPTH_PASS);
+	tr->SetUseDepthPass((renderType == RenderState::DEPTH_PASS) || (renderType == RenderState::DEPTH_PASS_DEFERRED));
 	tr->SetRenderQueue(renderQueue);
 
@@ -826,5 +825,5 @@
 	matProjectionView = matViewing * matProjection;
 	
-	if (renderType == RenderState::DEFERRED)
+	if ((renderType == RenderState::DEFERRED) || (renderType == RenderState::DEPTH_PASS_DEFERRED))
 	{
 		// set modelview matrix for shaders
@@ -859,4 +858,22 @@
 	
 	camera->SetPosition(pos);
+}
+
+
+void InitDeferredRendering()
+{
+	if (!fbo) InitFBO();
+	fbo->Bind();
+
+	// multisampling does not work with deferred shading
+	glDisable(GL_MULTISAMPLE_ARB);
+
+	state.SetRenderType(RenderState::DEFERRED);
+
+	cgGLEnableProfile(RenderState::sCgVertexProfile);
+	cgGLBindProgram(sCgMrtVertexProgram);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+	cgGLBindProgram(RenderState::sCgMrtFragmentProgram);
 }
 
@@ -925,16 +942,42 @@
 		break;
 
-	case RenderState::DEPTH_PASS:
-
-		glEnable(GL_MULTISAMPLE_ARB);
-		
+	case RenderState::DEPTH_PASS_DEFERRED:
+
+		state.Reset();
+
+		if (!fbo) InitFBO(); fbo->Bind();
+		glDrawBuffers(1, mrt);
+
 		cgGLDisableProfile(RenderState::sCgFragmentProfile);
 		cgGLDisableProfile(RenderState::sCgVertexProfile);
 
+		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+		glDisable(GL_MULTISAMPLE_ARB);
+
 		state.SetRenderType(RenderState::DEPTH_PASS);
-
+		state.SetUseAlphaToCoverage(false);
+
+
+		state.Reset();
 		// the scene is rendered withouth any shading	
 		glShadeModel(GL_FLAT);
-	
+		glDisable(GL_LIGHTING);
+	
+		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+
+		break;
+
+	case RenderState::DEPTH_PASS:
+
+		glEnable(GL_MULTISAMPLE_ARB);
+
+		cgGLDisableProfile(RenderState::sCgFragmentProfile);
+		cgGLDisableProfile(RenderState::sCgVertexProfile);
+
+		state.SetRenderType(RenderState::DEPTH_PASS);
+
+		// the scene is rendered withouth any shading	
+		glShadeModel(GL_FLAT);
 		glDisable(GL_LIGHTING);
 	
@@ -946,51 +989,15 @@
 	case RenderState::DEFERRED:
 
-		if (showShadowMap && !renderLightView)// && shadowChanged)
+		if (showShadowMap && !renderLightView)
 		{
-			shadowChanged = false;
-
-			//glEnable(GL_MULTISAMPLE_ARB);
-
-			cgGLDisableProfile(RenderState::sCgFragmentProfile);
-			cgGLDisableProfile(RenderState::sCgVertexProfile);
-
-			state.SetRenderType(RenderState::DEPTH_PASS);
-			
-			// change CHC++ set of state variables (must be done for each change of camera because
-			// otherwise the temporal coherency is broken
-			BvhNode::SetCurrentState(1);
-
-			state.SetUseAlphaToCoverage(false);
-
-			// the scene is rendered withouth any shading	
-			shadowMap->ComputeShadowMap(shadowTraverser, matProjectionView);
-
-			state.SetUseAlphaToCoverage(true);
-
-			// change back state
-			BvhNode::SetCurrentState(0);
-
+			RenderShadowMap();
 		}
 
-		if (!fbo) InitFBO();
-
-
-		// multisampling does not work with deferred shading
-		glDisable(GL_MULTISAMPLE_ARB);
-
-		state.SetRenderType(RenderState::DEFERRED);
-
-		fbo->Bind();
-	
-		glPushAttrib(GL_VIEWPORT_BIT);
+		//glPushAttrib(GL_VIEWPORT_BIT);
 		glViewport(0, 0, texWidth, texHeight);
 
-		cgGLEnableProfile(RenderState::sCgFragmentProfile);
-		cgGLBindProgram(RenderState::sCgMrtFragmentProgram);
-
-		cgGLEnableProfile(RenderState::sCgVertexProfile);
-		cgGLBindProgram(sCgMrtVertexProgram);
-
-		/// draw to 3 color buffers
+		InitDeferredRendering();
+		
+		// draw to 3 color buffers
 		glDrawBuffers(3, mrt);
 
@@ -1032,6 +1039,8 @@
 	}
 	else
+	{
 		// actually render the scene geometry using the specified algorithm
 		traverser->RenderScene();
+	}
 
 
@@ -1044,11 +1053,10 @@
 	
 	// reset depth pass and render visible objects
-	if (renderType == RenderState::DEPTH_PASS) 
-	{
-		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
-		glShadeModel(GL_SMOOTH);
-
+	if ((renderType == RenderState::DEPTH_PASS) ||
+		(renderType == RenderState::DEPTH_PASS_DEFERRED))
+	{
 		RenderVisibleObjects();
 	}
+	
 
 
@@ -1059,10 +1067,11 @@
 	// this would conveniently solves some issues (e.g, skys without shadows)
 
-	RenderSky();
-	
-
-
-	if (renderType == RenderState::DEFERRED) 
-	{
+	//RenderSky();
+	
+
+
+	if ((renderType == RenderState::DEFERRED))// || (renderType == RenderState::DEPTH_PASS_DEFERRED))
+	{
+		//glPopAttrib();
 		FrameBufferObject::Release();
 
@@ -1070,5 +1079,6 @@
 		cgGLDisableProfile(RenderState::sCgFragmentProfile);
 
-		if (!ssaoShader) ssaoShader = new DeferredRenderer(texWidth, texHeight, camera, farDist / MAX_DEPTH_CONST);
+		if (!ssaoShader) ssaoShader = 
+			new DeferredRenderer(texWidth, texHeight, camera, farDist / MAX_DEPTH_CONST);
 		
 		DeferredRenderer::SHADING_METHOD shadingMethod;
@@ -1141,5 +1151,5 @@
 		exit(0);
 		break;
-	case 32: //space
+	case 32: // space
 		renderMode = (renderMode + 1) % RenderTraverser::NUM_TRAVERSAL_TYPES;
 
@@ -1149,6 +1159,7 @@
 		if (shadowTraverser)
 		{
-			// shadow traverser has to be recomputed (this is done on demand)
+			// shadow traverser has to be recomputed
 			DEL_PTR(shadowTraverser);
+			shadowTraverser = CreateTraverser(shadowMap->GetShadowCamera());
 		}
 
@@ -1355,6 +1366,6 @@
 	case GLUT_KEY_F7:
 
-		renderType = (renderType + 1) % 3;
-		traverser->SetUseDepthPass(renderType == RenderState::DEPTH_PASS);
+		renderType = (renderType + 1) % RenderState::NUM_RENDER_TYPES;
+		traverser->SetUseDepthPass((renderType == RenderState::DEPTH_PASS) || (renderType == RenderState::DEPTH_PASS) || (renderType == RenderState::DEPTH_PASS_DEFERRED));
 		
 		break;
@@ -1590,8 +1601,7 @@
 	// hack: set far plane for viz
 	//camera->SetFar(0.35f * Magnitude(box.Diagonal()));
-	//camera->SetFar(1000);
-
-	//const float offs = box.Size().x * 0.3f;
-	const float offs = box.Size().x * 0.6f;
+
+	const float offs = box.Size().x * 0.3f;
+	//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);
@@ -1744,7 +1754,23 @@
 
 		rTime = renderTime;
-		renderedObjects = traverser->GetStats().mNumRenderedGeometry;
-		renderedNodes = traverser->GetStats().mNumRenderedNodes;
-		renderedTriangles = traverser->GetStats().mNumRenderedTriangles;
+
+		if (renderLightView && shadowTraverser)
+		{
+			renderedTriangles = shadowTraverser->GetStats().mNumRenderedTriangles;
+			renderedObjects = shadowTraverser->GetStats().mNumRenderedGeometry;
+			renderedNodes = shadowTraverser->GetStats().mNumRenderedNodes;
+		}
+		else if (showShadowMap && shadowTraverser)
+		{
+			renderedNodes = traverser->GetStats().mNumRenderedNodes + shadowTraverser->GetStats().mNumRenderedNodes;
+			renderedObjects = traverser->GetStats().mNumRenderedGeometry + shadowTraverser->GetStats().mNumRenderedGeometry;
+			renderedTriangles = traverser->GetStats().mNumRenderedTriangles + shadowTraverser->GetStats().mNumRenderedTriangles;
+		}
+		else
+		{
+			renderedTriangles = traverser->GetStats().mNumRenderedTriangles;
+			renderedObjects = traverser->GetStats().mNumRenderedGeometry;
+			renderedNodes = traverser->GetStats().mNumRenderedNodes;
+		}
 
 		traversedNodes = traverser->GetStats().mNumTraversedNodes;
@@ -1790,5 +1816,5 @@
 			int i = 0;
 
-			static char *renderTypeStr[] = {"fixed function", "fixed function + depth pass", "deferred shading"};
+			static char *renderTypeStr[] = {"forward", "depth pass + forward", "deferred shading", "depth pass + deferred"};
 	
 			sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d", 
@@ -1810,15 +1836,21 @@
 			glColor3f(1.0f, 1.0f, 0.0f);
 
-			string str;
-			string str2;
+			string objStr, totalObjStr;
+			string triStr, totalTriStr;
 
 			int len = 10;
-			CalcDecimalPoint(str, renderedTriangles, len);
-			CalcDecimalPoint(str2, bvh->GetBvhStats().mTriangles, len);
+			CalcDecimalPoint(objStr, renderedObjects, len);
+			CalcDecimalPoint(totalObjStr, (int)loader->GetNumEntities(), len);
+
+			CalcDecimalPoint(triStr, renderedTriangles, len);
+			CalcDecimalPoint(totalTriStr, bvh->GetBvhStats().mTriangles, len);
 
 			int i = 4;
 
-			sprintf(msg[i ++], "rendered: %6d of %6d nodes, %s of %s triangles", 
-				renderedNodes, bvh->GetNumVirtualNodes(), str.c_str(), str2.c_str()); 
+			sprintf(msg[i ++], "rendered: %s of %s objects, %s of %s triangles", 
+				objStr.c_str(), totalObjStr.c_str(), triStr.c_str(), totalTriStr.c_str()); 
+
+			//sprintf(msg[i ++], "rendered: %6d of %6d nodes, %s of %s triangles", 
+			//	renderedNodes, bvh->GetNumVirtualNodes(), triStr.c_str(), totalTriStr.c_str()); 
 
 			sprintf(msg[i ++], "traversed: %5d, frustum culled: %5d, query culled: %5d",
@@ -1865,14 +1897,36 @@
 }
 
+
 // render visible object from depth pass
 void RenderVisibleObjects()
 {
-	state.SetRenderType(RenderState::FIXED);
+	/*if ((renderType == RenderState::DEPTH_PASS_DEFERRED))
+	{
+		//glDisable(GL_LIGHTING);
+		state.SetUseAlphaToCoverage(false);
+		//FrameBufferObject::Release();
+		InitDeferredRendering();
+
+		glClear(GL_COLOR_BUFFER_BIT || GL_DEPTH_BUFFER_BIT);
+		// draw to 3 color buffers
+		glDrawBuffers(3, mrt);
+	}
+	else*/
+		state.SetRenderType(RenderState::FIXED);
+
 	state.Reset();
 
+	glShadeModel(GL_SMOOTH);
 	glEnable(GL_LIGHTING);
+
+	glDisable(GL_ALPHA);
+	glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
+
+	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
+
+	// draw all objects that have exactly the same depth as the current sample
+	//glDisable(GL_DEPTH_TEST);
 	glDepthFunc(GL_LEQUAL);
-
-	//cout << "visible: " << (int)traverser->GetVisibleObjects().size() << endl;
+	cout << "visible: " << (int)traverser->GetVisibleObjects().size() << endl;
 
 	SceneEntityContainer::const_iterator sit, 
@@ -1880,9 +1934,12 @@
 
 	for (sit = traverser->GetVisibleObjects().begin(); sit != sit_end; ++ sit)
+	{
 		renderQueue->Enqueue(*sit);
-		
+	}
+
 	renderQueue->Apply();
 
 	glDepthFunc(GL_LESS);
+	state.SetUseAlphaToCoverage(true);
 }
 
@@ -1900,2 +1957,29 @@
 	}
 }
+
+
+void RenderShadowMap()
+{
+	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+	cgGLDisableProfile(RenderState::sCgVertexProfile);
+
+	state.SetRenderType(RenderState::DEPTH_PASS);
+
+	// change CHC++ set of state variables (must be done for each change of camera because
+	// otherwise the temporal coherency is broken
+	BvhNode::SetCurrentState(1);
+	state.SetUseAlphaToCoverage(false);
+
+	// hack: temporarily change camera far plane
+	//camera->SetFar(0.2f * Magnitude(bvh->GetBox().Diagonal()));
+
+	// the scene is rendered withouth any shading	
+	shadowMap->ComputeShadowMap(shadowTraverser, matProjectionView);
+
+	camera->SetFar(farDist);
+
+	state.SetUseAlphaToCoverage(true);
+
+	// change back state
+	BvhNode::SetCurrentState(0);
+}
