Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3123)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 3128)
@@ -6,11 +6,11 @@
 //-- ssao + gi parameters
 
-//#define NUM_SAMPLES 8
-#define NUM_SAMPLES 16
+#define NUM_SAMPLES 8
+//#define NUM_SAMPLES 16
 
 // for quadratic falloff
 //#define SAMPLE_INTENSITY 0.1f
-//#define SAMPLE_INTENSITY 0.07f
-#define SAMPLE_INTENSITY 0.015f
+#define SAMPLE_INTENSITY 0.07f
+//#define SAMPLE_INTENSITY 0.015f
 
 #define SAMPLE_RADIUS 8e-1f
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderManager.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderManager.cpp	(revision 3126)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderManager.cpp	(revision 3128)
@@ -86,4 +86,5 @@
 	fragmentProgMrtTex = CreateFragmentProgram("mrt", "fragtex", "defaultFragmentTexMrt");
 
+
 	// provide the current view matrix:
 	// this is an automatic parameter that is updated each frame
@@ -98,7 +99,4 @@
 	vertexProgMrt->AddParameter("oldModelMatrix", 2);
 	
-
-	// add a texture parameter
-	//fragmentProgMrtTex->AddParameter("tex", 1);
 
 
@@ -125,4 +123,11 @@
 	treeAnimProgMrt->AddParameter("oldTimer", 5);
 
+	
+	//ShaderProgram *normalMappingVertexMrt, *normalMappingFragmentMrt;
+
+	//normalMappingVertexMrt = CreateFragmentProgram("normalMapping", "vtx", "normalMappingVertexMrt");
+	//normalMappingFragmentMrt = CreateFragmentProgram("normalMapping", "frag", "normalMappingFragmentMrt");
+
+
 	cout << "cg initialization successful" << endl;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3126)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/combineSsao.cg	(revision 3128)
@@ -63,5 +63,6 @@
 						  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
 						  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
-						  float scale)
+						  float scale,
+						  int index)
 {
 	float average = .0f;
@@ -86,5 +87,4 @@
 
 		//depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
-		
 		depthFactor = max(step(5e-2f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
 		normalFactor = max(step(0.5f, dot(sampleNorm, norm)), 1e-3f);
@@ -93,5 +93,5 @@
 		//w = filterWeights[i] * depthFactor;
 
-		average += aoSample.x * w;
+		average += aoSample[index] * w;
 		total_w += w;
 	}
@@ -119,14 +119,15 @@
 	if (col.w < 1e10f)
 	{
-		//const static float scaleFactor = 10.0f;
-		const static float scaleFactor = 50.0f;
+		//const static float scaleFactor = 50.0f;
+		const static float scaleFactor = 10.0f;
+		const static float adaptFactor = 10.0f;
 
 		//ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
 		//ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights, 1.0f / (1.0f + ao.y));
 		//ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor / (scaleFactor + ao.y));
-		ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, 1.0f);
-	}
-
-	OUT.illum_col = col * ao.x;
+		ao.x = DiscontinuityFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, adaptFactor * scaleFactor * ao.z  / (adaptFactor + ao.y), 0);
+	}
+
+	OUT.illum_col.xyz = col.xyz * ao.x;
 	//OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
 	//OUT.illum_col.xyz = float3(1.0f - ao.x, 1.0f - ao.y * 1e-2f, 1);
@@ -135,2 +136,68 @@
 	return OUT;
 }
+
+float DiscontinuityFilter2(float2 texCoord,
+						  float4 ao,
+						  uniform sampler2D ssaoTex,
+						  uniform sampler2D normalsTex,
+						  uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
+						  uniform float filterWeights[NUM_SSAO_FILTERSAMPLES],
+						  float scale,
+						  int index)
+{
+	float average = .0f;
+	float total_w = .0f;
+
+	const float eyeSpaceDepth = ao.w;
+	const float3 norm = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
+
+	float4 aoSample;
+	float3 sampleNorm;
+	float w;
+	float4 offs;
+	float depthFactor;
+	float normalFactor;
+
+	for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
+	{
+		offs = float4(texCoord + filterOffs[i] * scale, 0, 0);
+		aoSample = tex2Dlod(ssaoTex, offs);
+		
+		sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
+
+		//depthFactor = clamp(1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w), 1e-3f, 1.0f);
+		depthFactor = max(step(5e-2f, 1.0f - abs(1.0f - eyeSpaceDepth / aoSample.w)), 1e-3f);
+		normalFactor = max(step(0.5f, dot(sampleNorm, norm)), 1e-3f);
+
+		w = filterWeights[i] * normalFactor * depthFactor;
+		//w = filterWeights[i] * depthFactor;
+
+		average += aoSample.y * w;
+		total_w += w;
+	}
+
+	average *= 1.0f / max(total_w, 1e-6f);
+
+	return average;
+}
+
+pixel smoothSsao(fragment IN, 
+				 uniform sampler2D ssaoTex,
+				 uniform sampler2D normalsTex,
+				 uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
+				 uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
+			  )
+{
+	pixel OUT;
+
+	float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
+
+	//const static float scaleFactor = 10.0f;
+	const static float scaleFactor = 10.0f;
+
+	ao.y = DiscontinuityFilter2(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights, scaleFactor * ao.z, 1);
+	
+	OUT.illum_col = ao;
+
+	return OUT;
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3125)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3128)
@@ -236,4 +236,5 @@
 	//	newWeight = min(temporalCoherence + 1, max(oldPixel.y - 70, 50));
 	//if (newWeight >= 2000) newWeight = 1000;
+	//newWeight -= step(2000.0f, newWeight) * 1000.0f;
 
 	return float3(oldPixel.x, newWeight, eyeSpaceDepth);
@@ -380,7 +381,7 @@
 
 	const float oldSsao = temporalVals.x;
-	const float newWeight = temporalVals.y;
-
-	//float2 noiseOffs = float2(newWeight / 139.0f, newWeight / 141.0f);
+	const float newWeight = clamp(temporalVals.y, .0f, temporalCoherence);
+
+	//float2 noiseOffs = float2(temporalVals.y / 139.0f, temporalVals.y / 141.0f);
 	float2 noiseOffs = float2(.0f);
 
@@ -399,5 +400,5 @@
 
 	OUT.illum_col.x = (ao.x + oldSsao * (newWeight - 1.0f)) / newWeight;
-	OUT.illum_col.y = newWeight;
+	OUT.illum_col.y = temporalVals.y;
 	OUT.illum_col.z = invw;
 	OUT.illum_col.w = temporalVals.z;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3127)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3128)
@@ -52,5 +52,4 @@
 	CLEAR_CONTAINER(mTrafos);
 	CLEAR_CONTAINER(mShapes);
-	//CLEAR_CONTAINER(mLODs);
 }
 
@@ -71,7 +70,6 @@
 
 	if (!hasTrafo)
-	{
-		// identity
-		trafo = new Transform3();
+	{	
+		trafo = new Transform3(); // identity
 	}
 	else
@@ -79,5 +77,4 @@
 		Matrix4x4 m;
 		str.read(reinterpret_cast<char *>(m.x), sizeof(Matrix4x4));
-		
 		trafo = new Transform3(m);
 	}
@@ -290,6 +287,7 @@
 	static ShaderProgram *sDefaultVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("defaultVertexMrt");
 
-	static ShaderProgram *sNormalMappingVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingVertexMrt");
-	static ShaderProgram *sNormalMappingFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingFragmentMrt");
+	//static ShaderProgram *sNormalMappingVertexProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingVertexMrt");
+	//static ShaderProgram *sNormalMappingFragmentProgramMrt = ShaderManager::GetSingleton()->GetShaderProgram("normalMappingFragmentMrt");
+
 
 	Technique *deferred = new Technique(*tech);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3111)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3128)
@@ -83,4 +83,5 @@
 	void CombineSsao(FrameBufferObject *fbo);
 	void CombineIllum(FrameBufferObject *fbo);
+	void SmoothSsao(FrameBufferObject *fbo);
 
 	void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3127)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3128)
@@ -471,11 +471,12 @@
 	//-- load some dynamic stuff
 
-	resourceManager->mUseNormalMapping = true;
-	//resourceManager->mUseNormalMapping = false;
-
-	LoadModel("fisch.dem", dynamicObjects);
-	
+	//resourceManager->mUseNormalMapping = true;
 	resourceManager->mUseNormalMapping = false;
-	//LoadModel("hbuddha.dem", dynamicObjects);
+
+	//LoadModel("fisch.dem", dynamicObjects);
+	LoadModel("hbuddha.dem", dynamicObjects);
+
+	resourceManager->mUseNormalMapping = false;
+
 	buddha = dynamicObjects.back();
 	
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3123)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3128)
@@ -57,8 +57,9 @@
 static ShaderProgram *sCgToneProgram = NULL;
 static ShaderProgram *sCgDownSampleProgram = NULL;
-
-ShaderContainer DeferredRenderer::sShaders;
+static ShaderProgram *sCgSmoothSsaoProgram = NULL;
+
 
 static GLuint noiseTex = 0;
+
 
 // ssao random spherical samples
@@ -322,9 +323,9 @@
 	sCgCombineIllumProgram = sm->CreateFragmentProgram("globillum", "combine", "combineGi");
 	sCgCombineSsaoProgram = sm->CreateFragmentProgram("combineSsao", "combine", "combineSsao");
-	//sCgCombineSsaoProgram = sm->CreateFragmentProgram("ssao", "combine", "combineSsao");
 	sCgAntiAliasingProgram = sm->CreateFragmentProgram("antialiasing", "main", "antiAliasing");
 	sCgToneProgram = sm->CreateFragmentProgram("tonemap", "ToneMap", "toneMap");
 	sCgDownSampleProgram = sm->CreateFragmentProgram("tonemap", "DownSample", "downSample");
 	sCgLogLumProgram = sm->CreateFragmentProgram("tonemap", "CalcAvgLogLum", "avgLogLum");
+	sCgSmoothSsaoProgram = sm->CreateFragmentProgram("combineSsao", "smoothSsao", "smoothSsao");
 
 
@@ -385,5 +386,9 @@
 
 	sCgLogLumProgram->AddParameter("colors", 0);
-	
+
+	////////////////
+
+	string smoothSsaoParams[] = {"normalsTex", "ssaoTex", "filterOffs", "filterWeights"};
+	sCgSmoothSsaoProgram->AddParameters(smoothSsaoParams, 0, 4);
 	
 	
@@ -461,4 +466,5 @@
 	case SSAO:
 		ComputeSsao(fbo, tempCohFactor);
+		SmoothSsao(fbo);
 		CombineSsao(fbo);
 		break;
@@ -536,5 +542,6 @@
 	sCgSsaoProgram->SetValue1f(i ++, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
 	
-	if (mUseTemporalCoherence || mRegenerateSamples)
+	//if (mUseTemporalCoherence || mRegenerateSamples)
+	if (mRegenerateSamples)
 	{
 		mRegenerateSamples = false;
@@ -740,5 +747,5 @@
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 	GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
-	GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
+	GLuint ssaoTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
 
 	// overwrite old color texture
@@ -1034,5 +1041,6 @@
 	glPushAttrib(GL_VIEWPORT_BIT);
 	glViewport(0, 0, mWidth, mHeight);
-	// revert to old far plane
+
+	// revert to old far and near plane
 	mCamera->SetFar(oldFar);
 	mCamera->SetNear(oldNear);
@@ -1040,3 +1048,31 @@
 
 
+void DeferredRenderer::SmoothSsao(FrameBufferObject *fbo)
+{
+	mIllumFbo->Bind();
+
+	GLuint normalsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
+
+	// overwrite old ssao texture
+	glDrawBuffers(1, mrt + 2 - mIllumFboIndex);
+
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
+
+	int i = 0;
+	sCgSmoothSsaoProgram->SetTexture(i ++, normalsTex);
+	sCgSmoothSsaoProgram->SetTexture(i ++, ssaoTex);
+
+	sCgSmoothSsaoProgram->SetArray2f(i ++, (float *)ssaoFilterOffsets, NUM_SSAO_FILTERSAMPLES);
+	sCgSmoothSsaoProgram->SetArray1f(i ++, (float *)ssaoFilterWeights, NUM_SSAO_FILTERSAMPLES);
+	
+	DrawQuad(sCgSmoothSsaoProgram);
+
+	glPopAttrib();
+
+	PrintGLerror("combine ssao");
+}
+
+
 } // namespace
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/VboFormatConverter/VboFormatConverter.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/VboFormatConverter/VboFormatConverter.cpp	(revision 3127)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/VboFormatConverter/VboFormatConverter.cpp	(revision 3128)
@@ -140,7 +140,9 @@
 	//-- texture
 
-	int texId = -1;
-	//int texId = 0;
+	//int texId = -1;
+	int texId = 0;
+	
 	str.write(reinterpret_cast<char *>(&texId), sizeof(int));
+	//str.write(reinterpret_cast<char *>(&texId2), sizeof(int));
 
 	bool alphaTestEnabled = false;
@@ -158,15 +160,16 @@
 	if (hasMaterial)
 	{
-		SimpleVec ambient, diffuse, black;
+		SimpleVec ambient, diffuse, spec, emm;
 
 		ambient.x = ambient.y = ambient.z = 0.2f;
 		diffuse.x = diffuse.y = diffuse.z = 1.0f;
-		black.x   = black.y   = black.z   = .0f;
+		spec.x   = spec.y   = spec.z   =  .0f;
+		emm = spec;
 
 		// only write rgb part of the material
 		str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
 		str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
-		str.write(reinterpret_cast<char *>(&black), sizeof(SimpleVec));
-		str.write(reinterpret_cast<char *>(&black), sizeof(SimpleVec));
+		str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
+		str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
 	}
 }
@@ -184,6 +187,6 @@
 	//-- write textures
 
-	//int textureCount = 1;
-	int textureCount = 0;
+	int textureCount = 1;
+	//int textureCount = 0;
 
 	ofile.write(reinterpret_cast<char *>(&textureCount), sizeof(int));
@@ -192,5 +195,6 @@
 	{
 		// hack
-		const string texName("wood.jpg");
+		const string texName("fischi_u1_v1.png");
+		//const string texName("wood.jpg");
 
 		int texnameSize = (int)texName.length() + 1;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter/ObjConverter.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter/ObjConverter.cpp	(revision 3102)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter/ObjConverter.cpp	(revision 3128)
@@ -364,15 +364,16 @@
 	if (hasMaterial)
 	{
-		SimpleVec ambient, diffuse, black;
+		SimpleVec ambient, diffuse, spec, emm;
 
 		ambient.x = ambient.y = ambient.z = 0.2f;
 		diffuse.x = diffuse.y = diffuse.z = 1.0f;
-		black.x   = black.y   = black.z   = .0f;
+		spec.x   = spec.y   = spec.z   =  .0f;
+		emm = spec;
 
 		// only write rgb part of the material
 		str.write(reinterpret_cast<char *>(&ambient), sizeof(SimpleVec));
 		str.write(reinterpret_cast<char *>(&diffuse), sizeof(SimpleVec));
-		str.write(reinterpret_cast<char *>(&black), sizeof(SimpleVec));
-		str.write(reinterpret_cast<char *>(&black), sizeof(SimpleVec));
+		str.write(reinterpret_cast<char *>(&spec), sizeof(SimpleVec));
+		str.write(reinterpret_cast<char *>(&emm), sizeof(SimpleVec));
 	}
 }
