Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp	(revision 3005)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp	(revision 3005)
@@ -0,0 +1,349 @@
+#include <iostream>
+#include "FrameBufferObject.h"
+#include "glInterface.h"
+
+using namespace std;
+
+
+namespace CHCDemoEngine
+{
+
+
+void PrintFBOStatus(GLenum status)
+{
+	switch(status)
+	{
+	case GL_FRAMEBUFFER_COMPLETE_EXT:
+		//cout << "frame buffer object complete" << endl;
+		break;
+	case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
+		cerr << "incomplete attachment" << endl;
+		break;
+	case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
+		cerr << "missing attachment" << endl;
+		break;
+	case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
+		cerr << "incomplete dimensions" << endl;
+		break;
+	case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
+		cerr << "incomplete formats" << endl;
+		break;
+	case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
+		cerr << "incomplete draw buffer" << endl;
+		break;
+	case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
+		cerr << "incomplete read buffer" << endl;
+		break;
+	case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
+		cerr << "framebuffer unsupported" << endl;
+		break;
+	default:
+		cerr << "unknown status code " << status << endl;
+	}
+}
+
+
+ColorBufferObject::ColorBufferObject(int w, int h, 
+									 int attachment_idx,
+									 FORMAT format, 
+									 WRAP_TYPE wrapType, 
+									 FILTER_TYPE filterType, 
+									 FILTER_TYPE filterTypeMipMap 
+									 )
+{
+	Init(w, h, attachment_idx, format, wrapType, filterType, true, filterTypeMipMap);
+}
+
+
+ColorBufferObject::ColorBufferObject(int w, int h, 
+									 int attachment_idx,
+									 FORMAT format, 
+									 WRAP_TYPE wrapType, 
+									 FILTER_TYPE filterType
+									 )
+{
+	Init(w, h, attachment_idx, format, wrapType, filterType, false, FILTER_NEAREST);
+}
+
+
+ColorBufferObject::~ColorBufferObject()
+{
+	glDeleteRenderbuffersEXT(1, &mId);
+	glDeleteTextures(1, &mTexId);
+}
+
+
+void ColorBufferObject::Init(int w, int h, 
+							 int attachment_idx,
+							 FORMAT format, 
+							 WRAP_TYPE wrapType, 
+							 FILTER_TYPE filterType, 
+							 bool useMipMap, 
+							 FILTER_TYPE filterTypeMipMap
+							 )
+{
+	mWidth = w;
+	mHeight = h;
+
+	int components = GL_RGBA;
+
+	switch (format)
+	{
+	case RGB_UBYTE: 
+		mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGB8;
+		components = GL_RGB;
+		break;
+	case RGBA_UBYTE:
+		mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8; break;
+	case RGBA_FLOAT_16:
+		mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA16F_ARB; break;
+	case RGB_FLOAT_32: 
+		mGlFormat = GL_FLOAT; mInternalFormat = GL_RGB32F_ARB;
+		components = GL_RGB;
+		break;
+	case RGBA_FLOAT_32: 
+		mGlFormat = GL_FLOAT; mInternalFormat = GL_RGBA32F_ARB; break;
+	default:
+		mGlFormat = GL_UNSIGNED_BYTE; mInternalFormat = GL_RGBA8;
+		cerr << "should not come here" << endl;
+	}
+
+
+	glGenRenderbuffersEXT(1, &mId);
+	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mId);
+	
+	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, mInternalFormat, w, h);
+	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_RENDERBUFFER_EXT, mId);
+
+
+	glGenTextures(1, &mTexId);
+	glBindTexture(GL_TEXTURE_2D, mTexId);
+	glTexImage2D(GL_TEXTURE_2D, 0, mInternalFormat, w, h, 0, components, mGlFormat, NULL);
+
+	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, mrt[attachment_idx], GL_TEXTURE_2D, mTexId, 0);
+
+	GLuint minfilterParam;
+	GLuint magfilterParam;
+
+	if (filterType == FILTER_NEAREST)
+		magfilterParam = GL_NEAREST;
+	else // FILTER_LINEAR
+		magfilterParam = GL_LINEAR; 
+
+	if (!useMipMap)
+	{
+		if (filterType == FILTER_NEAREST)
+			minfilterParam = GL_NEAREST;
+		else // FILTER_LINEAR
+			minfilterParam = GL_LINEAR;
+	}
+	else
+	{
+		if (filterType == FILTER_NEAREST)
+		{
+			if (filterTypeMipMap == FILTER_NEAREST)
+				minfilterParam = GL_NEAREST_MIPMAP_NEAREST;
+			else // FILTER_LINEAR
+				minfilterParam = GL_NEAREST_MIPMAP_LINEAR;
+		}
+		else // FILTER_LINEAR
+		{
+			if (filterTypeMipMap == FILTER_NEAREST)
+				minfilterParam = GL_LINEAR_MIPMAP_NEAREST;
+			else // FILTER_LINEAR
+				minfilterParam = GL_LINEAR_MIPMAP_LINEAR;
+		}
+	}
+
+	GLuint wrapParam;
+
+	switch (wrapType)
+	{
+	case WRAP_REPEAT:
+		wrapParam = GL_REPEAT; break;
+	case WRAP_CLAMP_TO_EDGE:
+		wrapParam = GL_CLAMP_TO_EDGE; break;
+	}
+
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magfilterParam);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minfilterParam);
+
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapParam);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapParam);
+
+	if (useMipMap) 
+	{
+		glGenerateMipmapEXT(GL_TEXTURE_2D);
+	}
+
+	// print status
+	PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
+}
+
+
+
+void *ColorBufferObject::ReadTexture() const
+{
+	int bytes = 0;
+
+	switch (mInternalFormat)
+	{
+	case GL_RGB8:
+		bytes = 3; break;
+	case GL_RGBA8:
+		bytes = 4; break;
+	case GL_RGBA16F_ARB:
+		bytes = 16; break;
+	case GL_RGB32F_ARB: 
+		bytes = 24; break;
+	case GL_RGBA32F_ARB: 
+		bytes = 32; break;
+	default:
+		cerr << "should not come here" << endl;
+	}
+
+	
+	glBindTexture(GL_TEXTURE_2D, mTexId);
+	
+	/*GLint width, height;
+	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
+	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
+	cout << "w: " << width << "h: " << height << endl;
+	*/
+
+	unsigned char *data = new unsigned char[bytes * 4 * mWidth * mHeight];
+
+	glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_FLOAT, data);
+
+	glBindTexture(GL_TEXTURE_2D, 0);
+	glDisable(GL_TEXTURE_2D);
+
+	return data;
+}
+
+
+/*****************************************************************/
+/*                 FrameBufferObject implementation              */
+/*****************************************************************/
+
+
+FrameBufferObject::FrameBufferObject(int w, int h, DEPTH_FORMAT d, bool useDepthTex)
+: mWidth(w), mHeight(h), mDepthTexId(0)
+{
+	glGenFramebuffersEXT(1, &mId);
+	
+	Bind();
+	///////////
+	//-- create depth buffer
+
+	if (d != DEPTH_NONE)
+	{
+		glGenRenderbuffersEXT(1, &mDepthId);	
+		glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, mDepthId);
+
+		GLuint depthFormat;
+
+		switch (d)
+		{
+		case DEPTH_16:
+			depthFormat = GL_DEPTH_COMPONENT16; break;
+		case DEPTH_24:
+			depthFormat = GL_DEPTH_COMPONENT24; break;
+		case DEPTH_32: 
+			depthFormat = GL_DEPTH_COMPONENT32; break;
+		default:	
+			cerr << "should not come here" << endl;
+		}
+
+		glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, depthFormat, w, h);
+		
+
+		glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, mDepthId);
+
+		if (useDepthTex)
+		{
+			glGenTextures(1, &mDepthTexId);
+			glBindTexture(GL_TEXTURE_2D, mDepthTexId);
+
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+			
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+			glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+
+			glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, mWidth, mHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
+
+			glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, mDepthTexId, 0);
+		}
+	}
+
+	Release();
+}
+
+
+FrameBufferObject::~FrameBufferObject()
+{
+	glDeleteFramebuffersEXT(1, &mId);
+	glDeleteRenderbuffersEXT(1, &mDepthId);
+
+	if (mDepthTexId) glDeleteTextures(1, &mDepthTexId);
+
+	CLEAR_CONTAINER(mColorBuffers);
+}
+
+
+int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col, 
+									  ColorBufferObject::WRAP_TYPE wrapType, 
+									  ColorBufferObject::FILTER_TYPE filterType, 
+									  ColorBufferObject::FILTER_TYPE filterTypeMipMap)
+{
+	Bind();
+
+	const int idx = (int)mColorBuffers.size();
+
+	ColorBufferObject *colorBuf = 
+		new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType, filterTypeMipMap);
+
+	mColorBuffers.push_back(colorBuf);
+
+	Release();
+
+	return idx;
+}
+
+
+
+int FrameBufferObject::AddColorBuffer(ColorBufferObject::FORMAT col, 
+									  ColorBufferObject::WRAP_TYPE wrapType, 
+									  ColorBufferObject::FILTER_TYPE filterType)
+{
+	Bind();
+
+	const int idx = (int)mColorBuffers.size();
+
+	ColorBufferObject *colorBuf = 
+		new ColorBufferObject(mWidth, mHeight, idx, col, wrapType, filterType);
+
+	mColorBuffers.push_back(colorBuf);
+
+	Release();
+
+	return idx;
+}
+
+
+void FrameBufferObject::Bind() const
+{
+	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mId);
+}
+
+
+void FrameBufferObject::Release()
+{
+	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+}
+
+
+} // namespace
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 2982)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/sky_preetham.cg	(revision 3005)
@@ -31,5 +31,5 @@
 {
   float4 col: COLOR0;
-  float4 pos: COLOR1;
+  float3 pos: COLOR1;
   float4 norm: COLOR2;
 };
@@ -112,13 +112,11 @@
 
 	pix.col = IN.color2;
-	pix.pos = 1e20f;
+	pix.col.w = 2.0f;
 
 	pix.norm.xyz = IN.normal;
+	pix.norm.w = IN.mypos.w;
+
+	pix.pos = 1e20f;
 	
-	pix.norm.w = IN.mypos.w;
-	pix.pos.w = 1e20f;
-	
-	pix.col.w = 2.0f;
-
 	return pix;
 }
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2992)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3005)
@@ -48,10 +48,10 @@
 	float4 outColor;
 
-	if (position.w > 1e19f) outColor = color;
+	// hack: prevent shading the sky
+	if (position.z > 1e19f) outColor = color;
 	//if (emmisive > 1.5f) outColor = color;
 	else outColor = (ambient + diffuse) * color;
 
 	return outColor;
-	//return saturate((((ambient + diffuse)) * (1.0f - emmisive) + emmisive) * color);
 }
 
@@ -188,6 +188,5 @@
 	// calc diffuse illumination + shadow term
 	//if ((emmisive < 1.5f) // hack: prevent shadowing the sky	
-	if (
-		(position.w < 1e19f)
+	if ((position.z < 1e19f)
 		&& (angle > 1e-3f) // shadow only if diffuse color has some minimum intensity
 		)
@@ -209,5 +208,5 @@
 	// base lighting
 	//OUT.color = (emmisive > 1.5f) ? color: (ambient + diffuse) * color;
-	OUT.color = (position.w > 1e19f) ? color: (ambient + diffuse) * color;
+	OUT.color = (position.z > 1e19f) ? color: (ambient + diffuse) * color;
 
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 3004)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 3005)
@@ -37,8 +37,8 @@
 struct pixel
 {
-  float4 col: COLOR0;
-  //float4 pos: COLOR1;
-  float4 pos: COLOR1;
-  float4 norm: COLOR2;
+	float4 col: COLOR0;
+	//float4 pos: COLOR1;
+	float3 pos: COLOR1;
+	float4 norm: COLOR2;
 };
 
@@ -113,8 +113,8 @@
 	pix.col.w = length(eyePos - IN.worldPos.xyz) * maxDepth / magView;
 
+#if 1
 	// save world position in second render target
-	pix.pos = IN.worldPos * maxDepth;
-	// store the projected depth
-	pix.pos.w = projPos.z;
+	pix.pos = IN.worldPos.xyz * maxDepth;
+#endif
 
 	return pix;
@@ -150,7 +150,7 @@
 	pix.col.w = length(eyePos - IN.worldPos.xyz) * maxDepth / magView;
 
-	pix.pos = IN.worldPos * maxDepth;
-	// store the projected depth
-	pix.pos.w = projPos.z;
+#if 1
+	pix.pos = IN.worldPos.xyz * maxDepth;
+#endif
 
 	return pix;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 3003)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/tonemap.cg	(revision 3005)
@@ -22,5 +22,5 @@
 float4 DownSample(frag IN,
 				  uniform sampler2D colors,
-				  float2 downSampleOffs[4]) : COLOR
+				  uniform float2 downSampleOffs[4]): COLOR
 {    
     float4 average = .0f;
@@ -130,5 +130,5 @@
 	float logLumScaled = logLum * INV_LOGLUM_RANGE - logLumOffset;
 
-	if (oldLogLum > 1e-5f) // too hight log lum
+	if (oldLogLum > 1e-5f) // too bright
 		OUT.col.w = lerp(oldLogLum, logLumScaled, 0.1f);
 	else
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3004)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3005)
@@ -170,24 +170,30 @@
 
 
-	// the current world position
-	const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
-
 #if USE_EYE_SPACE_DEPTH
 	/// reconstruct position from the eye space depth
 	float3 viewDir = IN.view;
 	const float eyeDepth = tex2D(colors, IN.texCoord.xy).w;
-	float4 centerPosition2;
-
-	centerPosition2.xyz = eyePos - viewDir * eyeDepth;
-	centerPosition2.w = centerPosition.w;
-
-	const float2 ao = ssao(IN, colors, noiseTexture, samples, normal, centerPosition2, w, eyePos, bl, br, tl, tr);
+
+	float4 centerPosition;
+	centerPosition.xyz = eyePos - viewDir * eyeDepth;
+	centerPosition.w = centerPosition.w;
+
+	const float2 ao = ssao(IN, colors, noiseTexture, samples, normal, centerPosition, w, eyePos, bl, br, tl, tr);
 #else
 
+	// the current world position
+	const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
+
 	const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition, w, eyePos, bl, br, tl, tr);
 
 #endif
 
-	// get the current depth
+	/////////////////
+	//-- compute temporally smoothing
+
+	float4 realPos = centerPosition * maxDepth;
+	realPos.w = 1.0f;
+
+	// calulcate the current depth
 	//const float currentDepth = centerPosition.w;
 	
@@ -198,11 +204,4 @@
 	
 
-	/////////////////
-	//-- compute temporally smoothing
-
-	float4 realPos = centerPosition * maxDepth;
-	realPos.w = 1.0f;
-
-
 	///////////
 	//-- reprojection new frame into old one 
@@ -210,5 +209,5 @@
 	// calculate projected depth
 	float4 projPos = mul(oldModelViewProj, realPos);
-	projPos /= oldPos.w;
+	projPos /= projPos.w;
 
 	// the current depth projected into the old frame
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3002)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3005)
@@ -203,8 +203,4 @@
 DeferredRenderer::SAMPLING_METHOD samplingMethod = DeferredRenderer::SAMPLING_QUADRATIC;
 
-
-static Matrix4x4 matProjectionView = IdentityMatrix();
-
-
 ShadowMap *shadowMap = NULL;
 DirectionalLight *light = NULL;
@@ -287,6 +283,6 @@
 static CGparameter sBLParamTex;
 
-static Matrix4x4 oldViewProjMatrix;
-static Matrix4x4 oldMatViewing;
+static Matrix4x4 viewProjMat = IdentityMatrix();
+static Matrix4x4 oldViewProjMat = IdentityMatrix();
 
 
@@ -668,6 +664,6 @@
 	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
 	// the positions buffer
-	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
-	//fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
+	//fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
+	fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
 	// the normals buffer
 	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
@@ -905,6 +901,5 @@
 {
 	// store matrix of last frame
-	oldViewProjMatrix = matProjectionView;
-	camera->GetModelViewMatrix(oldMatViewing);
+	oldViewProjMat = viewProjMat;
 
 	glMatrixMode(GL_PROJECTION);
@@ -926,5 +921,5 @@
 
 	// store matrix for later use
-	matProjectionView = matViewing * matProjection;
+	viewProjMat = matViewing * matProjection;
 	
 	if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
@@ -1170,5 +1165,5 @@
 		// otherwise the temporal coherency is broken
 		BvhNode::SetCurrentState(1);
-		shadowMap->RenderShadowView(shadowTraverser, matProjectionView);
+		shadowMap->RenderShadowView(shadowTraverser, viewProjMat);
 		BvhNode::SetCurrentState(0);
 	}
@@ -1177,19 +1172,5 @@
 		// actually render the scene geometry using the specified algorithm
 		traverser->RenderScene();
-/*
-#if 0
-		SceneEntityContainer::const_iterator sit, sit_end = sceneEntities.end();
-
-		for (sit = sceneEntities.begin(); sit != sit_end; ++ sit)
-			renderQueue->Enqueue(*sit);
-#else
-
-		SceneEntityContainer::const_iterator sit, sit_end = sceneEntities2.end();
-
-		for (sit = sceneEntities2.begin(); sit != sit_end; ++ sit)
-			renderQueue->Enqueue(*sit);
-#endif
-*/			
-		renderQueue->Apply();
+		//renderQueue->Apply();
 	}
 
@@ -1244,5 +1225,5 @@
 
 		ShadowMap *sm = showShadowMap ? shadowMap : NULL;
-		ssaoShader->Render(fbo, oldViewProjMatrix, matProjectionView, ssaoTempCohFactor, light, useHDR, sm);
+		ssaoShader->Render(fbo, oldViewProjMat, viewProjMat, ssaoTempCohFactor, light, useHDR, sm);
 	}
 
@@ -2173,5 +2154,5 @@
 	
 	// the scene is rendered withouth any shading	
-	shadowMap->ComputeShadowMap(shadowTraverser, matProjectionView);
+	shadowMap->ComputeShadowMap(shadowTraverser, viewProjMat);
 
 	glEnable(GL_CULL_FACE);
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3004)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3005)
@@ -256,5 +256,4 @@
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
-	//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals);
 
@@ -555,4 +554,5 @@
 		cerr << "tone program failed to load" << endl;
 
+
 	sCgDownSampleProgram = 
 		cgCreateProgramFromFile(context, 
@@ -587,5 +587,5 @@
 	}
 	else
-		cerr << "intensity program failed to load" << endl;
+		cerr << "downsample program failed to load" << endl;
 
 	PrintGLerror("init");
@@ -610,5 +610,4 @@
 	
 	FrameBufferObject::Release();
-
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
 
@@ -683,5 +682,5 @@
 								   )
 {
-//#ifdef USE_3D_SSAO
+#ifdef USE_3D_SSAO
 	// bias from [-1, 1] to [0, 1]
 	static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
@@ -691,7 +690,9 @@
 
 	Matrix4x4 m = projViewMatrix * biasMatrix; 
-
 	cgGLSetMatrixParameterfc(sModelViewProjMatrixParam, (const float *)m.x);
-//#endif
+#else
+	cgGLSetMatrixParameterfc(sModelViewProjMatrixParam, (const float *)projViewMatrix.x);
+#endif
+
 
 	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
@@ -869,5 +870,4 @@
 
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
 
@@ -1316,4 +1316,22 @@
 void DeferredRenderer::DownSample(FrameBufferObject *fbo)
 {
+	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
+	GLuint colorsTex = colorBuffer->GetTexture();
+	
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+	cgGLBindProgram(sCgDownSampleProgram);
+	
+	cgGLSetTextureParameter(sColorsTexDownSampleParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexDownSampleParam);
+
+	mDownSampleFbo->Bind();
+
+	glDrawBuffers(1, mrt);
+	
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+	cgGLBindProgram(sCgDownSampleProgram);
+
 	//glPushAttrib(GL_VIEWPORT_BIT);
 	glViewport(0, 0, mWidth / 2, mHeight / 2);
@@ -1329,23 +1347,4 @@
 	glPushMatrix();
 	glLoadIdentity();
-
-
-	ColorBufferObject *colorBuffer = fbo->GetColorBuffer(colorBufferIdx);
-	GLuint colorsTex = colorBuffer->GetTexture();
-	
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(sCgDownSampleProgram);
-	
-	cgGLSetTextureParameter(sColorsTexDownSampleParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexDownSampleParam);
-
-	mDownSampleFbo->Bind();
-
-	glDrawBuffers(1, mrt);
-	
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(sCgDownSampleProgram);
 
 	const float offs = 0.5f;
@@ -1364,8 +1363,4 @@
 	FrameBufferObject::Release();
 
-	//glPopAttrib();
-	glViewport(0, 0, mWidth, mHeight);
-	PrintGLerror("downsample");
-
 	float *data = (float *)mDownSampleFbo->GetColorBuffer(0)->ReadTexture();
 	//float *data = (float *)mFbo->GetColorBuffer(colorBufferIdx)->ReadTexture();
@@ -1378,7 +1373,9 @@
 	glPopMatrix();
 
+	//glPopAttrib();
+	glViewport(0, 0, mWidth, mHeight);
+	PrintGLerror("downsample");
+
 	delete [] data;
-	
-	PrintGLerror("shadow map");
 }
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h	(revision 3001)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.h	(revision 3005)
@@ -15,5 +15,5 @@
 public:
 
-	enum FORMAT { RGBA_UBYTE, RGBA_FLOAT_16, RGB_FLOAT_32, RGBA_FLOAT_32};
+	enum FORMAT { RGB_UBYTE, RGBA_UBYTE, RGBA_FLOAT_16, RGB_FLOAT_32, RGBA_FLOAT_32};
 	enum WRAP_TYPE { WRAP_REPEAT, WRAP_CLAMP_TO_EDGE };
 	enum FILTER_TYPE { FILTER_NEAREST, FILTER_LINEAR };
