Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Texture.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Texture.cpp	(revision 2817)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Texture.cpp	(revision 2825)
@@ -61,21 +61,19 @@
 {
 	glEnable(GL_TEXTURE_2D);
+
 	glGenTextures(1, &mTexId);
 	glBindTexture(GL_TEXTURE_2D, mTexId);
+	
 	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-	//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
-	//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
-	
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
-
-	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
+	glTexImage2D(GL_RGBA, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
 	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, mWidth, mHeight, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
-	//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, mImage);
+
+	glBindTexture(GL_TEXTURE_2D, 0);
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Environment.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Environment.h	(revision 2825)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Environment.h	(revision 2825)
@@ -0,0 +1,47 @@
+#ifndef __Environment_H__
+#define __Environment_H__
+
+
+#include <string>
+#include <iostream>
+#include <fstream>
+
+#include "common.h"
+#include "Vector3.h"
+
+
+namespace CHCDemoEngine
+{
+
+/** Loads startup environment for the demo
+*/
+class Environment
+{
+public:
+
+	/** Reads the environment
+	*/
+	bool Read(const std::string &filename);
+
+	//////////
+	//-- members
+
+	int mAssumedVisibleFrames;
+	int mMaxBatchSize;
+
+	int mTrianglesPerVirtualLeaf;
+
+	/// these values get scaled with the frame rate
+	float mKeyForwardMotion;
+	float mKeyRotation;
+
+	int mWinWidth;
+	int mWinHeight;
+
+	Vector3 mPosition;
+	Vector3 mPitch;
+	Vector3 mYaw;
+};
+
+}
+#endif
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp	(revision 2802)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp	(revision 2825)
@@ -78,5 +78,5 @@
 
 		if (mShowBounds)
-			mBvh->RenderBoundsForViz(node, mUseTightBounds);		
+			mBvh->RenderBoundsForViz(node, mRenderState, mUseTightBounds);		
 	}
 	else 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 2802)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 2825)
@@ -13,4 +13,5 @@
 #include "Geometry.h"
 #include "RenderState.h"
+#include "Material.h"
 #include "gzstream.h"
 
@@ -1030,10 +1031,16 @@
 
 
-void Bvh::RenderBoundsForViz(BvhNode *node, bool useTightBounds)
+void Bvh::RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds)
 {
 	glDisable(GL_TEXTURE_2D);
 	glDisable(GL_LIGHTING);
 	glColor3f(1, 1, 1);
-	
+
+	// hack: for deferred shading we have to define a material
+	static Material boxMat;
+	boxMat.SetEmmisive(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f));
+
+	boxMat.Render(state);
+
 	if (!useTightBounds)
 	{
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 2819)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 2825)
@@ -22,4 +22,11 @@
 		QUERY,
 		RENDER
+	};
+
+	enum RenderType
+	{
+		FIXED,
+		DEPTH_PASS,
+		DEFERRED
 	};
 
@@ -51,6 +58,9 @@
 	inline int GetCurrentVboId() { return mCurrentVboId; }
 
-	void SetDepthPass(bool depthPass) { mIsDepthPass = depthPass; }
-	bool IsDepthPass() { return mIsDepthPass; }
+	void SetRenderType(RenderType type) { mRenderType = type; }
+	RenderType GetRenderType() { return mRenderType; }
+
+
+	///////////////////
 
 	static CGprofile sCgFragmentProfile;
@@ -70,5 +80,5 @@
 	int mCurrentVboId;
 
-	bool mIsDepthPass;
+	RenderType mRenderType;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 2823)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 2825)
@@ -30,15 +30,24 @@
 	if (mMaterial) mMaterial->Render(state);
 
-	static Matrix4x4 identity = IdentityMatrix();
-
 	if (mTransform)
 	{
-		cgGLSetMatrixParameterfc(sModelMatrixParam, (const float *)mTransform->x);
-		//glPushMatrix();
-		//glMultMatrixf((float *)mTransform->x);
-		mGeometry->Render(state);
+		if (state->GetRenderType() == RenderState::DEFERRED)
+		{
+			cgGLSetMatrixParameterfc(sModelMatrixParam, (const float *)mTransform->x);
 
-		cgGLSetMatrixParameterfc(sModelMatrixParam, (const float *)identity.x);
-		//glPopMatrix();
+			mGeometry->Render(state);
+
+			static Matrix4x4 identity = IdentityMatrix();
+			cgGLSetMatrixParameterfc(sModelMatrixParam, (const float *)identity.x);
+		}
+		else
+		{
+			glPushMatrix();
+			glMultMatrixf((float *)mTransform->x);
+			
+			mGeometry->Render(state);
+
+			glPopMatrix();
+		}
 	}
 	else
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 2822)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 2825)
@@ -60,5 +60,4 @@
 
 
-//void Material::Render(RenderState *state)
 void Material::Render(RenderState *state)
 {
@@ -68,30 +67,30 @@
 	{
 		mTexture->Bind();
-
-		cgGLSetParameter4f(sAmbientTexParam, 
-						   mEmmisiveColor.r, 
-						   mEmmisiveColor.g, 
-						   mEmmisiveColor.b, 
-						   mEmmisiveColor.a);
-
-		cgGLSetParameter4f(sDiffuseTexParam, mDiffuseColor.r, mDiffuseColor.g, mDiffuseColor.b, mDiffuseColor.a);
 	}
 	else
 	{
-		cgGLSetParameter4f(sAmbientParam, 
-						   mEmmisiveColor.r, 
-						   mEmmisiveColor.g, 
-						   mEmmisiveColor.b, 
-						   mEmmisiveColor.a);
-
-		cgGLSetParameter4f(sDiffuseParam, mDiffuseColor.r, mDiffuseColor.g, mDiffuseColor.b, mDiffuseColor.a);
-		
 		glBindTexture(GL_TEXTURE_2D, 0);
 	}
 
-	glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
-	glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&mDiffuseColor.r);
-	glMaterialfv(GL_FRONT, GL_EMISSION, (float *)&mEmmisiveColor.r);
-	glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r);
+	if (state->GetRenderType() == RenderState::DEFERRED)
+	{
+		if (mTexture)
+		{
+			cgGLSetParameter4f(sAmbientTexParam, mEmmisiveColor.r, mEmmisiveColor.g, mEmmisiveColor.b, mEmmisiveColor.a);
+			cgGLSetParameter4f(sDiffuseTexParam, mDiffuseColor.r, mDiffuseColor.g, mDiffuseColor.b, mDiffuseColor.a);
+		}
+		else
+		{
+			cgGLSetParameter4f(sAmbientParam, mEmmisiveColor.r, mEmmisiveColor.g, mEmmisiveColor.b, mEmmisiveColor.a);
+			cgGLSetParameter4f(sDiffuseParam, mDiffuseColor.r, mDiffuseColor.g, mDiffuseColor.b, mDiffuseColor.a);
+		}
+	}
+	else if (state->GetRenderType() == RenderState::FIXED)
+	{
+		glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
+		glMaterialfv(GL_FRONT, GL_DIFFUSE, (float *)&mDiffuseColor.r);
+		glMaterialfv(GL_FRONT, GL_EMISSION, (float *)&mEmmisiveColor.r);
+		glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r);
+	}
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2823)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2825)
@@ -133,6 +133,4 @@
 bool useMultiQueries = true;
 bool flyMode = true;
-bool depthPass = false;
-bool useGlFinish = false;
 
 SceneEntityContainer skyGeometry;
@@ -150,4 +148,6 @@
 
 PerfTimer frameTimer, algTimer;
+
+int renderType = RenderState::FIXED;
 
 
@@ -340,8 +340,4 @@
 	}
 
-	// create noise texture for ssao
-	CreateNoiseTex2D();
-
-
 	const string bvh_filename = string(model_path + "city.bvh");
 	BvhLoader bvhLoader;
@@ -360,6 +356,10 @@
 	InitCg();
 
+	// create noise texture for ssao
+	CreateNoiseTex2D();
+
 	bvh->SetCamera(camera);
 
+	// init render traverser
 	ResetTraverser();
 
@@ -369,5 +369,5 @@
 	visualization = new Visualization(bvh, camera, NULL, &state);
 
-	//sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
+	sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
 
 
@@ -479,4 +479,5 @@
 		sNormalsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "normals");  
 		sNoiseTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
+		cgGLDisableTextureParameter(sNoiseTexParamSsao);
 
 		sSamplesParamSsao = cgGetNamedParameter(sCgSsaoProgram, "samples");
@@ -792,5 +793,5 @@
 	traverser->SetUseMultiQueries(useMultiQueries);
 	traverser->SetUseTightBounds(useTightBounds);
-	traverser->SetUseDepthPass(depthPass);
+	traverser->SetUseDepthPass(renderType == RenderState::DEPTH_PASS);
 	traverser->SetRenderQueue(renderQueue);
 }
@@ -801,7 +802,6 @@
 	glEnable(GL_LIGHTING);
 	glEnable(GL_LIGHT0);
-	//glEnable(GL_LIGHT1);
-	glDisable(GL_LIGHT1);
-
+	glEnable(GL_LIGHT1);
+	
 	//GLfloat ambient[] = {0.5, 0.5, 0.5, 1.0};
 	GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
@@ -809,6 +809,6 @@
 	GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
 	    
-	GLfloat lmodel_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
-	//GLfloat lmodel_ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
+	//GLfloat lmodel_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
+	GLfloat lmodel_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
 
 	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
@@ -816,27 +816,28 @@
 	glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
 
-	GLfloat position[] = {1.0, 1.0, 1.0, 0.0};
-	glLightfv(GL_LIGHT0, GL_POSITION, position);
-
 
 	////////////
 	//-- second light
 
-	GLfloat ambient1[] = {0.5, 0.5, 0.5, 1.0};
-	//GLfloat diffuse1[] = {1.0, 1.0, 1.0, 1.0};
-	GLfloat diffuse1[] = {0.5, 0.5, 0.5, 1.0};
-	GLfloat specular1[] = {0.5, 0.5, 0.5, 1.0};
+	GLfloat ambient1[] = {0.2, 0.2, 0.2, 1.0};
+	GLfloat diffuse1[] = {1.0, 1.0, 1.0, 1.0};
+	//GLfloat diffuse1[] = {0.5, 0.5, 0.5, 1.0};
+	GLfloat specular1[] = {0.0, 0.0, 0.0, 1.0};
 
 	glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
 	glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
 	glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
-	
-	GLfloat position1[] = {0.0, 1.0, 0.0, 1.0};
+
+	
+	//////////////////////////////
+
+	GLfloat position[] = {0.8f, -1.0f, 0.7f, 0.0f};
+	glLightfv(GL_LIGHT0, GL_POSITION, position);
+
+	GLfloat position1[] = {-0.5f, 0.5f, 0.4f, 0.0f};
 	glLightfv(GL_LIGHT1, GL_POSITION, position1);
 
 	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
 	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
-	//glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
-	//glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SEPARATE_SPECULAR_COLOR_EXT);
 	glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SINGLE_COLOR_EXT);
 }
@@ -854,9 +855,21 @@
 	camera->SetupCameraView();
 
-	GLfloat position[] = {0.8f, 1.0f, 1.5f, 0.0f};
+	GLfloat position[] = {0.8f, -1.0f, 0.7f, 0.0f};
 	glLightfv(GL_LIGHT0, GL_POSITION, position);
 
-	GLfloat position1[] = {bvh->GetBox().Center().x, bvh->GetBox().Center().y, bvh->GetBox().Max().z, 1.0f};
+	GLfloat position1[] = {-0.5f, 0.5f, 0.4f, 0.0f};
 	glLightfv(GL_LIGHT1, GL_POSITION, position1);
+
+	
+	if (renderType == RenderState::DEFERRED)
+	{
+		// set modelview matrix for shaders
+		cgGLSetStateMatrixParameter(sModelViewProjMatrixParam, 
+									CG_GL_MODELVIEW_PROJECTION_MATRIX, 
+									CG_GL_MATRIX_IDENTITY);
+
+		static Matrix4x4 identity = IdentityMatrix();
+		cgGLSetMatrixParameterfc(SceneEntity::sModelMatrixParam, (const float *)identity.x);
+	}	
 }
 
@@ -909,49 +922,67 @@
 	if (!flyMode) PlaceViewer(oldPos);
 
-	if (useGlFinish) glFinish();
-
 	algTimer.Start();
 
-	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
-	
-	glPushAttrib(GL_VIEWPORT_BIT);
-	glViewport(0, 0, texWidth, texHeight);
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(RenderState::sCgMrtFragmentProgram);
-
-	cgGLEnableProfile(RenderState::sCgVertexProfile);
-	cgGLBindProgram(sCgMrtVertexProgram);
-
-	glDrawBuffers(3, mrt);
-	
+	
+	// render without shading
+	switch (renderType)
+	{
+	case RenderState::FIXED:
+	
+		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+
+		state.SetRenderType(RenderState::FIXED);
+		glEnable(GL_LIGHTING);
+
+		cgGLDisableProfile(RenderState::sCgFragmentProfile);
+		cgGLDisableProfile(RenderState::sCgVertexProfile);
+
+		glDrawBuffers(1, mrt);
+
+		break;
+
+	case RenderState::DEPTH_PASS:
+		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+
+		state.SetRenderType(RenderState::DEPTH_PASS);
+		glDisable(GL_LIGHTING);
+
+		cgGLDisableProfile(RenderState::sCgFragmentProfile);
+		cgGLDisableProfile(RenderState::sCgVertexProfile);
+	
+		glDrawBuffers(1, mrt);
+
+		break;
+	
+	case RenderState::DEFERRED:
+		state.SetRenderType(RenderState::DEFERRED);
+
+		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
+	
+		glPushAttrib(GL_VIEWPORT_BIT);
+		glViewport(0, 0, texWidth, texHeight);
+
+		cgGLEnableProfile(RenderState::sCgFragmentProfile);
+		cgGLBindProgram(RenderState::sCgMrtFragmentProgram);
+
+		cgGLEnableProfile(RenderState::sCgVertexProfile);
+		cgGLBindProgram(sCgMrtVertexProgram);
+
+		glDrawBuffers(3, mrt);
+
+		break;
+	}
+
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
 	glDepthFunc(GL_LESS);
+
 	glDisable(GL_TEXTURE_2D);
 	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-
-	// render without shading
-	if (depthPass) 
-	{
-		state.SetDepthPass(true);
-		glDisable(GL_LIGHTING);
-	}
-	else
-	{
-		state.SetDepthPass(false);
-		glEnable(GL_LIGHTING);
-	}
-	
+		
 
 	// bring eye modelview matrix up-to-date
 	SetupEyeView();
 
-	// set modelview matrix for shaders
-	cgGLSetStateMatrixParameter(sModelViewProjMatrixParam, CG_GL_MODELVIEW_PROJECTION_MATRIX, CG_GL_MATRIX_IDENTITY);
-	
-	static Matrix4x4 identity = IdentityMatrix();
-	cgGLSetMatrixParameterfc(SceneEntity::sModelMatrixParam, (const float *)identity.x);
-	
 
 	// actually render the scene geometry using one of the specified algorithms
@@ -959,5 +990,5 @@
 
 
-	//////
+	/////////
 	//-- do the rest of the rendering
 
@@ -967,5 +998,5 @@
 
 	// reset depth pass and render visible objects
-	if (depthPass) 
+	if (renderType == RenderState::DEPTH_PASS) 
 	{
 		RenderVisibleObjects();
@@ -983,19 +1014,25 @@
 	glDisableClientState(GL_NORMAL_ARRAY);
 	
-	
-	cgGLDisableProfile(RenderState::sCgVertexProfile);
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
-
-	glPopAttrib();
-
-	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
-
-	DisplayRenderTexture();
-
-	if (useGlFinish) glFinish();
+
+	if (renderType == RenderState::DEFERRED) 
+	{
+		glPopAttrib();
+		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+
+		cgGLDisableProfile(RenderState::sCgVertexProfile);
+		cgGLDisableProfile(RenderState::sCgFragmentProfile);
+
+		glDrawBuffers(1, mrt);
+
+		DisplayRenderTexture();
+	}
+
 
 	algTime = algTimer.Elapsedms();
 
+
 	///////////
+
+	state.SetRenderType(RenderState::FIXED);
 
 	if (visMode) DisplayVisualization();
@@ -1188,11 +1225,10 @@
 		break;
 	case GLUT_KEY_F7:
-		depthPass = !depthPass;
-		traverser->SetUseDepthPass(depthPass);
+
+		renderType = (renderType + 1) % 3;
+		traverser->SetUseDepthPass(renderType == RenderState::DEPTH_PASS);
+		
 		break;
 	case GLUT_KEY_F8:
-		useGlFinish = !useGlFinish;
-		break;
-	case GLUT_KEY_F9:
 		useSsao = !useSsao;
 		break;
@@ -1582,7 +1618,9 @@
 	sprintf(msg[i ++], "assumed visible frames: %4d, max batch size: %4d", 
 		    assumedVisibleFrames, maxBatchSize);
-
-	sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d, depth pass: %d, glFinish: %d", 
-		    useMultiQueries, useTightBounds, useRenderQueue, depthPass, useGlFinish);
+	
+	static char *renderTypeStr[] = {"fixed function", "fixed function + depth pass", "deferred shading"};
+
+	sprintf(msg[i ++], "multiqueries: %d, tight bounds: %d, render queue: %d, render technique: %s", 
+		    useMultiQueries, useTightBounds, useRenderQueue, renderTypeStr[renderType]);
 
 	sprintf(msg[i ++], "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf);
@@ -1630,5 +1668,5 @@
 void RenderVisibleObjects()
 {
-	state.SetDepthPass(false);
+	state.SetRenderType(RenderState::FIXED);
 	state.Reset();
 
@@ -1811,7 +1849,7 @@
 void CreateNoiseTex2D()
 {
-	randomNormals = new GLubyte[texWidth * texHeight * 4];
-
-	for (int i = 0; i < texWidth * texHeight * 4; i += 4)
+	randomNormals = new GLubyte[texWidth * texHeight * 3];
+
+	for (int i = 0; i < texWidth * texHeight * 3; i += 3)
 	{
 		// create random samples over sphere
@@ -1822,5 +1860,4 @@
 		randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);
 		randomNormals[i + 2] = 0;
-		randomNormals[i + 3] = 255;
 	}
 
@@ -1828,7 +1865,5 @@
 	glGenTextures(1, &noiseTex);
 	glBindTexture(GL_TEXTURE_2D, noiseTex);
-	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
-
-	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, texWidth, texHeight, GL_RGBA, GL_UNSIGNED_BYTE, randomNormals);
+		
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@@ -1836,4 +1871,7 @@
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
+	//glTexImage2D(GL_RGBA, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, randomNormals);
+	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, texWidth, texHeight, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
+
 	glBindTexture(GL_TEXTURE_2D, 0);
 	glDisable(GL_TEXTURE_2D);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Environment.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Environment.cpp	(revision 2825)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Environment.cpp	(revision 2825)
@@ -0,0 +1,15 @@
+#include "Environment.h"
+
+
+using namespace std;
+
+
+namespace CHCDemoEngine
+{
+
+bool Environment::Read(const string &filename)
+{
+	return true;
+}
+
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2823)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2825)
@@ -1,5 +1,5 @@
 #define NUM_SAMPLES 12
-//#define SAMPLE_INTENSITY 0.4f
-#define SAMPLE_INTENSITY 0.7f
+#define SAMPLE_INTENSITY 1.2f
+//#define SAMPLE_INTENSITY 0.7f
 #define AREA_SIZE 5e-1f
 
@@ -102,8 +102,5 @@
     float2 offsetTransformed = rotate(offset, noise);
     //float2 offsetTransformed = reflect(offset, noise);
-
-    //offsetTransformed.x = noise.r * offset.x - noise.g * offset.y;
-    //offsetTransformed.y = noise.g * offset.x + noise.r * offset.y;
-		
+	       
     // weight with projected coordinate to reach similar kernel size for near and far
     float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
@@ -118,7 +115,8 @@
     float cos_angle = dot(direction_to_sample, currentNormal);
     cos_angle = max(cos_angle, 0.0f);
-    
+    cos_angle *= cos_angle;
+
     // distance between current position and sample position controls AO intensity.
-    //const float maxdist = 2e-1f;
+    //const float maxdist = 4e-1f;
     const float maxdist = 5e-1f;
     
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 2819)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 2825)
@@ -14,9 +14,11 @@
 
 
+/////////////////
+
 RenderState::RenderState():
 mAlphaTestEnabled(false), 
 mTexturesEnabled(false), 
 mMode(RENDER),
-mIsDepthPass(false)
+mRenderType(FIXED)
 {
 	Reset(); 
@@ -35,11 +37,14 @@
 	if (mode == QUERY)
 	{
-		if (!mIsDepthPass)
+		if (mRenderType != DEPTH_PASS)
 		{
 			glDisable(GL_LIGHTING);
 			glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 
-			cgGLDisableProfile(sCgFragmentProfile);
-			cgGLDisableProfile(sCgVertexProfile);
+			if (mRenderType == DEFERRED)
+			{
+				cgGLDisableProfile(sCgFragmentProfile);
+				cgGLDisableProfile(sCgVertexProfile);
+			}
 		}
 
@@ -53,11 +58,14 @@
 		//-- restore render state
 
-		if (!mIsDepthPass)
+		if (mRenderType != DEPTH_PASS)
 		{
 			glEnable(GL_LIGHTING);
 			glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
 
-			cgGLEnableProfile(sCgFragmentProfile);
-			cgGLEnableProfile(sCgVertexProfile);
+			if (mRenderType == DEFERRED)
+			{
+				cgGLEnableProfile(sCgFragmentProfile);
+				cgGLEnableProfile(sCgVertexProfile);
+			}
 		}
 
@@ -82,11 +90,11 @@
 	}
 
-	// allow only alpha textures in depth pass
 	if (mTexturesEnabled && 
-		(!texturing || mIsDepthPass && !mAlphaTestEnabled))
+		(!texturing || (mRenderType == DEPTH_PASS) && !mAlphaTestEnabled))
 	{
 		mTexturesEnabled = false;
 
-		cgGLBindProgram(sCgMrtFragmentProgram);
+		if (mRenderType == DEFERRED)
+			cgGLBindProgram(sCgMrtFragmentProgram);
 
 		glDisable(GL_TEXTURE_2D);
@@ -95,9 +103,11 @@
 	else if (!mTexturesEnabled && texturing)
 	{
-		if (!mIsDepthPass || mAlphaTestEnabled)
+		// support only alpha textures in depth pass
+		if ((mRenderType != DEPTH_PASS) || mAlphaTestEnabled)
 		{
 			mTexturesEnabled = true;
 
-			cgGLBindProgram(sCgMrtFragmentTexProgram);
+			if (mRenderType == DEFERRED)
+				cgGLBindProgram(sCgMrtFragmentTexProgram);
 
 			glEnable(GL_TEXTURE_2D);
@@ -111,4 +121,5 @@
 {
 	mCurrentVboId = -1;
+
 	SetState(false, false);
 	SetState(RENDER);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 2824)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 2825)
@@ -48,5 +48,4 @@
 	{
 		trafo = NULL;
-		sceneGeom = new SceneEntity(geom, mat, trafo);
 	}
 	else
@@ -55,23 +54,8 @@
 		str.read(reinterpret_cast<char *>(trafo->x), sizeof(Matrix4x4));
 		mTrafos.push_back(trafo);
-#if 0 // use static geometry 
-		Vector3 *vertices = new Vector3[geom->mNumVertices];
-
-		for (int i = 0; i < geom->mNumVertices; ++ i)
-		{
-			Vector3 v = geom->mVertices[i];
-
-			v = (*trafo) * v;
-			vertices[i] = v;
-		}
-
-		Geometry *tgeom = new Geometry(vertices, geom->mNormals, geom->mTexCoords, geom->mNumVertices, false);
-
-		sceneGeom = new SceneEntity(tgeom, mat, NULL);
-#else
-		// use instancing
-		sceneGeom = new SceneEntity(geom, mat, trafo);
-#endif
-	}
+	}
+
+	// use instancing
+	sceneGeom = new SceneEntity(geom, mat, trafo);
 
 	return sceneGeom;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 2800)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h	(revision 2825)
@@ -556,5 +556,5 @@
 	/** Render wireframe bvh for visualization purpose.
 	*/
-	void RenderBoundsForViz(BvhNode *node, bool useTightBounds);
+	void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds);
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2809)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 2825)
@@ -216,4 +216,12 @@
 			</File>
 			<File
+				RelativePath=".\src\Environment.cpp"
+				>
+			</File>
+			<File
+				RelativePath=".\src\Environment.h"
+				>
+			</File>
+			<File
 				RelativePath=".\src\glInterface.h"
 				>
