Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.h	(revision 2848)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.h	(revision 3101)
@@ -10,4 +10,5 @@
 
 class RenderTraverser;
+class RenderState;
 
 /** A simple class that computes the first intersection of a horizontal ray with
@@ -18,5 +19,6 @@
 public:
 
-	SceneQuery(const AxisAlignedBox3 &sceneBox, RenderTraverser *traverser);
+	SceneQuery(const AxisAlignedBox3 &sceneBox, RenderTraverser *traverser,
+		       RenderState *state);
 
 	~SceneQuery() { DEL_ARRAY_PTR(mDepth); }
@@ -38,4 +40,6 @@
 	//unsigned char *mDepth;
 	float *mDepth;
+
+	RenderState *mRenderState;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3100)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3101)
@@ -88,5 +88,5 @@
 	
 	const float sampleEyeSpaceDepth = tex2Dlod(colors, float4(texCoord, 0, 0)).w;
-	//float3 viewVec = normalize(Interpol(texcoord, bl, br, tl, tr));
+	
 	const float3 viewVec = Interpol(texCoord, bl, br, tl, tr);
 	const float3 samplePos = -viewVec * sampleEyeSpaceDepth;
@@ -158,5 +158,5 @@
 	// fit from unit cube into 0 .. 1
 	const float2 oldTexCoords = backProjPos.xy * 0.5f + 0.5f;
-	//const float2 oldTexCoords = texcoord0;
+	
 
 	// retrieve the sample from the last frame
@@ -166,20 +166,11 @@
 
 	// vector from eye pos to old sample 
-	//const float3 viewVec = normalize(Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr));
 	const float3 viewVec = Interpol(oldTexCoords, oldbl, oldbr, oldtl, oldtr);
-	//const float3 oldWorldPos = oldEyePos - oldEyeSpaceDepth * viewVec;
-	//const float3 oldWorldPos = - viewVec * eyeSpaceDepth;
-	//const float3 dummy14 = Interpol(texcoord0, bl, br, tl, tr);
-
-	// the euclidian world space distance
-	//const float depthDif = length(oldWorldPos - worldPos.xyz);
-	//const float depthDif = length(dummy14 - viewVec);
-	//const float projectedEyeSpaceDepth = length(worldPos + oldEyePos);
+	
 	const float invlen = 1.0f / length(viewVec);
 	const float projectedEyeSpaceDepth = length(dummyPt) * invlen;
 	
 	const float depthDif = abs(oldEyeSpaceDepth - projectedEyeSpaceDepth);
-	//const float depthDif = length(oldEyePos);
-
+	
 	float notValid = 0.5f;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp	(revision 3089)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp	(revision 3101)
@@ -6,4 +6,5 @@
 #include "RenderTraverser.h"
 #include "FrameBufferObject.h"
+#include "RenderState.h"
 
 #include <IL/il.h>
@@ -75,5 +76,5 @@
 	startil();
 
-	ILstring filename = ILstring("depth.tga");
+	ILstring filename = ILstring("sceneQuery.tga");
 	ilRegisterType(IL_FLOAT);
 
@@ -100,6 +101,6 @@
 
 SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox, 
-					   RenderTraverser *renderer): 
-mSceneBox(sceneBox), mDepth(NULL)
+					   RenderTraverser *renderer, RenderState *state): 
+mSceneBox(sceneBox), mDepth(NULL), mRenderState(state)
 {
 	Prepare(renderer);
@@ -112,9 +113,9 @@
 	const int py = (mSceneBox.Max(1) - pt.y) * (texHeight - 1) / mSceneBox.Size(1);
 
-	float d = mDepth[px + py * texHeight];
+	const float d = mDepth[px + py * texHeight];
 
 	static float depth = d;
 
-	if (d > 0)
+	if (d > .0f)
 	{
 		// temporal smoothing of depth values
@@ -136,6 +137,6 @@
 	cout << "Preparing scene queries" << endl;
 
-	const float xlen = mSceneBox.Size().x * 0.5f;
-	const float ylen = mSceneBox.Size().y * 0.5f;
+	const float xlen = mSceneBox.Size().x * .5f;
+	const float ylen = mSceneBox.Size().y * .5f;
 	
 	PerspectiveCamera *orthoCam = new PerspectiveCamera(xlen / ylen);
@@ -165,6 +166,10 @@
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
+	// hack: disable cull face because of alpha textured balconies
 	glDisable(GL_CULL_FACE);
-	glEnable(GL_DEPTH_TEST);
+	mRenderState->LockCullFaceEnabled(true);
+
+	//glDisable(GL_CULL_FACE);
+	//glEnable(GL_DEPTH_TEST);
 
 	glMatrixMode(GL_MODELVIEW);
@@ -177,4 +182,5 @@
 
 	glOrtho(xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z); 
+	//glOrtho(xlen, .0f, ylen, .0f, .0f, mSceneBox.Size().z); 
 
 	glMatrixMode(GL_MODELVIEW);
@@ -190,4 +196,7 @@
 	glEnableClientState(GL_NORMAL_ARRAY);
 
+	// forward rendering
+	mRenderState->SetRenderTechnique(0);
+
 	renderer->RenderScene();
 
@@ -197,6 +206,6 @@
 	renderer->SetCamera(oldCam);
 	
-
-	glEnable(GL_CULL_FACE);
+	mRenderState->LockCullFaceEnabled(false);
+	//glEnable(GL_CULL_FACE);
 
 	glMatrixMode(GL_MODELVIEW);
@@ -211,5 +220,5 @@
 
 	GrabDepthBuffer(mDepth, fbo->GetDepthTex());
-	//ExportDepthBuffer(mDepth); PrintGLerror("grab");
+	ExportDepthBuffer(mDepth); PrintGLerror("grab depth");
 
 	DEL_PTR(fbo);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 3089)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 3101)
@@ -101,7 +101,5 @@
 
 	for (; sit != sit_end; ++ sit)
-	{
 		(*sit)->Render(state);
-	}
 
 	mTransform->Unload(state);
@@ -143,7 +141,5 @@
 
 	if (lodLevel == -1)
-	{
 		lodLevel = GetCurrentLODLevel();
-	}
 
 	return mLODLevels[lodLevel].GetNumTriangles();
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3089)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3101)
@@ -86,6 +86,6 @@
 /// the visualization
 Visualization *visualization = NULL;
-/// the current render state
-RenderState state;
+/// the current render renderState
+RenderState renderState;
 /// the rendering algorithm
 int renderMode = RenderTraverser::CHCPLUSPLUS;
@@ -162,5 +162,5 @@
 int numBatches = 0;
 
-// mouse navigation state
+// mouse navigation renderState
 int xEyeBegin = 0;
 int yEyeBegin = 0;
@@ -267,5 +267,5 @@
 void SpecialKeyUp(int c, int x, int y);
 void Reshape(int w, int h);
-void Mouse(int button, int state, int x, int y);
+void Mouse(int button, int renderState, int x, int y);
 void LeftMotion(int x, int y);
 void RightMotion(int x, int y);
@@ -404,5 +404,5 @@
 	light = new DirectionalLight(lightDir, RgbaColor(1, 1, 1, 1), RgbaColor(1, 1, 1, 1));
 	// the render queue for material sorting
-	renderQueue = new RenderQueue(&state);
+	renderQueue = new RenderQueue(&renderState);
 
 	glutInitWindowSize(winWidth, winHeight);
@@ -527,5 +527,5 @@
 	
 	// the bird-eye visualization
-	visualization = new Visualization(bvh, camera, NULL, &state);
+	visualization = new Visualization(bvh, camera, NULL, &renderState);
 
 	// this function assign the render queue bucket ids of the materials in beforehand
@@ -533,5 +533,5 @@
 	PrepareRenderQueue();
 	/// forward rendering is the default
-	state.SetRenderTechnique(FORWARD);
+	renderState.SetRenderTechnique(FORWARD);
 	// frame time is restarted every frame
 	frameTimer.Start();
@@ -729,5 +729,5 @@
 	tr->SetHierarchy(bvh);
 	tr->SetRenderQueue(renderQueue);
-	tr->SetRenderState(&state);
+	tr->SetRenderState(&renderState);
 	tr->SetUseOptimization(useOptimization);
 	tr->SetUseRenderQueue(useRenderQueue);
@@ -853,5 +853,5 @@
 	// multisampling does not work with deferred shading
 	glDisable(GL_MULTISAMPLE_ARB);
-	state.SetRenderTechnique(DEFERRED);
+	renderState.SetRenderTechnique(DEFERRED);
 
 
@@ -940,5 +940,5 @@
 	
 		glEnable(GL_MULTISAMPLE_ARB);
-		state.SetRenderTechnique(FORWARD);
+		renderState.SetRenderTechnique(FORWARD);
 		//glEnable(GL_LIGHTING);
 
@@ -950,6 +950,6 @@
 
 		glDisable(GL_MULTISAMPLE_ARB);
-		state.SetUseAlphaToCoverage(false);
-		state.SetRenderTechnique(DEPTH_PASS);
+		renderState.SetUseAlphaToCoverage(false);
+		renderState.SetRenderTechnique(DEPTH_PASS);
 
 		if (!fbo) InitFBO(); fbo->Bind();
@@ -960,5 +960,5 @@
 
 		// the scene is rendered withouth any shading 
-		// (should be handled by render state)
+		// (should be handled by render renderState)
 		glShadeModel(GL_FLAT);
 		break;
@@ -967,10 +967,10 @@
 
 		glEnable(GL_MULTISAMPLE_ARB);
-		state.SetRenderTechnique(DEPTH_PASS);
+		renderState.SetRenderTechnique(DEPTH_PASS);
 
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
 		// the scene is rendered withouth any shading 
-		// (should be handled by render state)
+		// (should be handled by render renderState)
 		glShadeModel(GL_FLAT);
 		break;
@@ -1004,5 +1004,5 @@
 	if (renderLightView)
 	{
-		// change CHC++ set of state variables: 
+		// change CHC++ set of renderState variables: 
 		// must be done for each change of camera because otherwise 
 		// the temporal coherency is broken
@@ -1068,6 +1068,6 @@
 
 
-	state.SetRenderTechnique(FORWARD);
-	state.Reset();
+	renderState.SetRenderTechnique(FORWARD);
+	renderState.Reset();
 
 
@@ -1406,7 +1406,7 @@
 
 
-void Mouse(int button, int state, int x, int y) 
-{
-	if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
+void Mouse(int button, int renderState, int x, int y) 
+{
+	if ((button == GLUT_LEFT_BUTTON) && (renderState == GLUT_DOWN))
 	{
 		xEyeBegin = x;
@@ -1415,5 +1415,5 @@
 		glutMotionFunc(LeftMotion);
 	}
-	else if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))
+	else if ((button == GLUT_RIGHT_BUTTON) && (renderState == GLUT_DOWN))
 	{
 		xEyeBegin = x;
@@ -1426,5 +1426,5 @@
 			glutMotionFunc(RightMotionLight);
 	}
-	else if ((button == GLUT_MIDDLE_BUTTON) && (state == GLUT_DOWN))
+	else if ((button == GLUT_MIDDLE_BUTTON) && (renderState == GLUT_DOWN))
 	{
 		horizontalMotionBegin = x;
@@ -1808,12 +1808,8 @@
 			static char *renderMethodStr[] = 
 				{"forward", "depth pass + forward", "deferred shading", "depth pass + deferred"};
-	
 			sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d", 
 					useMultiQueries, useTightBounds, useRenderQueue);
-
 			sprintf(msg[i ++], "render technique: %s, SSAO: %d", renderMethodStr[renderMethod], useAdvancedShading);
-
 			sprintf(msg[i ++], "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf);
-
 			sprintf(msg[i ++], "assumed visible frames: %4d, max batch size: %4d", 
 				assumedVisibleFrames, maxBatchSize);
@@ -1852,6 +1848,5 @@
 			sprintf(msg[i ++], "traversed: %5d, frustum culled: %5d, query culled: %5d",
 				traversedNodes, frustumCulledNodes, queryCulledNodes);
-
-			sprintf(msg[i ++], "issued queries: %5d, state changes: %5d, render batches: %5d", 
+			sprintf(msg[i ++], "issued queries: %5d, renderState changes: %5d, render batches: %5d", 
 				issuedQueries, stateChanges, numBatches);
 
@@ -1884,5 +1879,5 @@
 {
 	if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
-		state.SetRenderTechnique(DEFERRED);
+		renderState.SetRenderTechnique(DEFERRED);
 
 	const bool useToneMapping = 
@@ -1890,7 +1885,7 @@
 		 (renderMethod == RENDER_DEFERRED)) && useHDR;
 	
-	preetham->RenderSkyDome(-light->GetDirection(), camera, &state, !useToneMapping);
-	/// once again reset the state
-	state.Reset();
+	preetham->RenderSkyDome(-light->GetDirection(), camera, &renderState, !useToneMapping);
+	/// once again reset the renderState
+	renderState.Reset();
 }
 
@@ -1914,11 +1909,11 @@
 	else
 	{
-		state.SetRenderTechnique(FORWARD);
+		renderState.SetRenderTechnique(FORWARD);
 	}
 
 	/////////////////
-	//-- reset gl state before the final visible objects pass
-
-	state.Reset();
+	//-- reset gl renderState before the final visible objects pass
+
+	renderState.Reset();
 
 	glEnableClientState(GL_NORMAL_ARRAY);
@@ -1926,5 +1921,5 @@
 	glShadeModel(GL_SMOOTH);
 	/// reset alpha to coverage flag
-	state.SetUseAlphaToCoverage(true);
+	renderState.SetUseAlphaToCoverage(true);
 	// clear color
 	glClear(GL_COLOR_BUFFER_BIT);
@@ -1947,5 +1942,5 @@
 	// switch back to standard depth func
 	glDepthFunc(GL_LESS);
-	state.Reset();
+	renderState.Reset();
 
 	PrintGLerror("visibleobjects");
@@ -1956,5 +1951,7 @@
 {
 	if (!sceneQuery)
-		sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
+	{
+		sceneQuery = new SceneQuery(bvh->GetBox(), traverser, &renderState);
+	}
 
 	Vector3 playerPos = camera->GetPosition();
@@ -1972,14 +1969,14 @@
 {
 	glDisableClientState(GL_NORMAL_ARRAY);
-	state.SetRenderTechnique(DEPTH_PASS);
+	renderState.SetRenderTechnique(DEPTH_PASS);
 	
 	// hack: disable cull face because of alpha textured balconies
 	glDisable(GL_CULL_FACE);
-	state.LockCullFaceEnabled(true);
+	renderState.LockCullFaceEnabled(true);
 
 	/// don't use alpha to coverage for the depth map (problems with fbo rendering)
-	state.SetUseAlphaToCoverage(false);
-
-	// change CHC++ set of state variables 
+	renderState.SetUseAlphaToCoverage(false);
+
+	// change CHC++ set of renderState variables 
 	// this must be done for each change of camera because
 	// otherwise the temporal coherency is broken
@@ -1993,9 +1990,9 @@
 	camera->SetFar(farDist);
 
-	state.SetUseAlphaToCoverage(true);
-	state.LockCullFaceEnabled(false);
+	renderState.SetUseAlphaToCoverage(true);
+	renderState.LockCullFaceEnabled(false);
 
 	glEnableClientState(GL_NORMAL_ARRAY);
-	// change back state
+	// change back renderState
 	BvhNode::SetCurrentState(CAMERA_PASS);
 }
@@ -2009,5 +2006,5 @@
 	for (int i = 0; i < 3; ++ i)
 	{
-		state.SetRenderTechnique(i);
+		renderState.SetRenderTechnique(i);
 
 		// fill all shapes into the render queue	once so we can establish the buckets
