Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2820)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2821)
@@ -145,4 +145,6 @@
 bool eightKeyPressed = false;
 
+bool useSsao = false;
+
 GLubyte *randomNormals = NULL;
 
@@ -216,11 +218,16 @@
 static CGcontext sCgContext = NULL;
 static CGprogram sCgMrtVertexProgram = NULL;
-static CGprogram sCgSSAOProgram = NULL;
-
+static CGprogram sCgSsaoProgram = NULL;
+static CGprogram sCgDeferredProgram = NULL;
 
 static CGparameter sColorsTexParam;
 static CGparameter sPositionsTexParam;
 static CGparameter sNormalsTexParam;
-static CGparameter sNoiseTexParam;
+//static CGparameter sNoiseTexParam;
+
+static CGparameter sColorsTexParamSsao;
+static CGparameter sPositionsTexParamSsao;
+static CGparameter sNormalsTexParamSsao;
+static CGparameter sNoiseTexParamSsao;
 
 static CGparameter sModelViewProjMatrixParam;
@@ -229,5 +236,6 @@
 static CGparameter sMaxDepthParamTex;
 
-static CGparameter sSamplesParam; 
+//static CGparameter sSamplesParam; 
+static CGparameter sSamplesParamSsao; 
 
 
@@ -279,9 +287,10 @@
 	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
 
+#if 1
 	glutCreateWindow("FriendlyCulling");
-
-	//glutGameModeString( "1024x768:32@75" ); //the settings for fullscreen mode
-	//glutEnterGameMode(); //set glut to fullscreen using the settings in the line above
-
+#else
+	glutGameModeString( "1024x768:32@75" ); //the settings for fullscreen mode
+	glutEnterGameMode(); //set glut to fullscreen using the settings in the line above
+#endif
 	glutDisplayFunc(Display);
 	glutKeyboardFunc(KeyBoard);
@@ -398,5 +407,5 @@
 								NULL);
 
-	if(sCgMrtVertexProgram != NULL)
+	if (sCgMrtVertexProgram != NULL)
 	{
 		cgGLLoadProgram(sCgMrtVertexProgram);
@@ -451,28 +460,53 @@
 	///////////////
 
-	sCgSSAOProgram = 
+	sCgSsaoProgram = 
 		cgCreateProgramFromFile(sCgContext, 
 								CG_SOURCE,
 								"src/shaders/deferred.cg", 
-								//"src/test.cg", 
 								RenderState::sCgFragmentProfile,
-								NULL,
+								"main_ssao",
 								NULL);
 
-	if(sCgSSAOProgram != NULL)
-	{
-		cgGLLoadProgram(sCgSSAOProgram);
+	if (sCgSsaoProgram != NULL)
+	{
+		cgGLLoadProgram(sCgSsaoProgram);
 
 		// we need size of texture for scaling
-		sPositionsTexParam = cgGetNamedParameter(sCgSSAOProgram, "positions");  
-		sColorsTexParam = cgGetNamedParameter(sCgSSAOProgram, "colors");  
-		sNormalsTexParam = cgGetNamedParameter(sCgSSAOProgram, "normals");  
-		sNoiseTexParam = cgGetNamedParameter(sCgSSAOProgram, "noiseTexture");
-
-		sSamplesParam = cgGetNamedParameter(sCgSSAOProgram, "samples");
-		cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
+		sPositionsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "positions");  
+		sColorsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "colors");  
+		sNormalsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "normals");  
+		sNoiseTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
+
+		sSamplesParamSsao = cgGetNamedParameter(sCgSsaoProgram, "samples");
+		cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);
 	}
 	else
 		cerr << "ssao program failed to load" << endl;
+
+	
+	sCgDeferredProgram = 
+		cgCreateProgramFromFile(sCgContext, 
+								CG_SOURCE,
+								"src/shaders/deferred.cg", 
+								RenderState::sCgFragmentProfile,
+								"main",
+								NULL);
+
+	if (sCgDeferredProgram != NULL)
+	{
+		cgGLLoadProgram(sCgDeferredProgram);
+
+		// we need size of texture for scaling
+		sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");  
+		sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");  
+		sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");  
+		//sNoiseTexParam = cgGetNamedParameter(sCgDeferredProgram, "noiseTexture");
+
+		//sSamplesParam = cgGetNamedParameter(sCgDeferredProgram, "samples");
+		//cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
+	}
+	else
+		cerr << "deferred program failed to load" << endl;
+
 
 	PrintGLerror("init");
@@ -666,4 +700,5 @@
 		"'F7',          - depth pass",
 		"'F8',          - enable/disable glFinish for more accurate timings",
+		"'F9',          - enables/disables ambient occlusion",
 		"'SPACE'        - cycles through occlusion culling algorithms",
 		"",
@@ -1159,4 +1194,7 @@
 		useGlFinish = !useGlFinish;
 		break;
+	case GLUT_KEY_F9:
+		useSsao = !useSsao;
+		break;
 	case GLUT_KEY_LEFT:
 		{
@@ -1446,6 +1484,8 @@
 	if (sCgMrtVertexProgram)
 		cgDestroyProgram(sCgMrtVertexProgram);
-	if (sCgSSAOProgram)
-		cgDestroyProgram(sCgSSAOProgram);
+	if (sCgSsaoProgram)
+		cgDestroyProgram(sCgSsaoProgram);
+	if (sCgDeferredProgram)
+		cgDestroyProgram(sCgDeferredProgram);
 	if (sCgContext)
 		cgDestroyContext(sCgContext);
@@ -1645,17 +1685,34 @@
 
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(sCgSSAOProgram);
-
-	cgGLEnableTextureParameter(sPositionsTexParam);
-	cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
-	
-	cgGLEnableTextureParameter(sColorsTexParam);
-	cgGLSetTextureParameter(sColorsTexParam, colorsTex);
-	
-	cgGLEnableTextureParameter(sNormalsTexParam);
-	cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
-
-	cgGLEnableTextureParameter(sNoiseTexParam);
-	cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
+
+	if (useSsao)
+	{
+		cgGLBindProgram(sCgSsaoProgram);
+		
+		cgGLEnableTextureParameter(sPositionsTexParamSsao);
+		cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex);
+	
+		cgGLEnableTextureParameter(sColorsTexParamSsao);
+		cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex);
+
+		cgGLEnableTextureParameter(sNormalsTexParamSsao);
+		cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex);
+
+		cgGLEnableTextureParameter(sNoiseTexParamSsao);
+		cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex);
+	}
+	else
+	{
+		cgGLBindProgram(sCgDeferredProgram);
+
+		cgGLEnableTextureParameter(sPositionsTexParam);
+		cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
+	
+		cgGLEnableTextureParameter(sColorsTexParam);
+		cgGLSetTextureParameter(sColorsTexParam, colorsTex);
+
+		cgGLEnableTextureParameter(sNormalsTexParam);
+		cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
+	}
 
 	Vector3 tl, tr, bl, br;
@@ -1677,8 +1734,19 @@
 	glEnd();
 
-	cgGLDisableTextureParameter(sColorsTexParam);
-	cgGLDisableTextureParameter(sPositionsTexParam);
-	cgGLDisableTextureParameter(sNormalsTexParam);
-	cgGLDisableTextureParameter(sNoiseTexParam);
+	if (useSsao)
+	{
+		cgGLDisableTextureParameter(sColorsTexParamSsao);
+		cgGLDisableTextureParameter(sPositionsTexParamSsao);
+		cgGLDisableTextureParameter(sNormalsTexParamSsao);
+
+		cgGLDisableTextureParameter(sNoiseTexParamSsao);
+		//cgGLDisableTextureParameter(sSamplesParamSsao);
+	}
+	else
+	{
+		cgGLDisableTextureParameter(sColorsTexParam);
+		cgGLDisableTextureParameter(sPositionsTexParam);
+		cgGLDisableTextureParameter(sNormalsTexParam);
+	}
 
 	cgGLDisableProfile(RenderState::sCgFragmentProfile);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2820)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2821)
@@ -1,5 +1,7 @@
 #define NUM_SAMPLES 8
-#define SAMPLE_INTENSITY 0.6
+#define SAMPLE_INTENSITY 0.5f
+//#define SAMPLE_INTENSITY 0.2f
 #define AREA_SIZE 3e-1f
+
 // kustls magic sample positions
 /*static const float2 samples[NUM_SAMPLES] =
@@ -57,4 +59,14 @@
 
 
+float2 rotate(float2 pt, float2 n)
+{
+  float2 ptTransformed;
+  ptTransformed.x = n.r * pt.x - n.g * pt.y;
+  ptTransformed.y = n.g * pt.x + n.r * pt.y;
+
+  return ptTransformed;
+}
+
+
 //based on kustls shader	
 float ssao(fragment IN,
@@ -88,11 +100,13 @@
     
     //sample noisetex; r stores costheta, g stores sintheta
-    float3 noise = tex2D(noiseTexture, IN.texCoord.xy).xyz * 2.0f - 1.0f;
+    float2 noise = tex2D(noiseTexture, IN.texCoord.xy).xy * 2.0f - 1.0f;
 	
     // rotation
     //float2 offsetTransformed = offset;
-    float2 offsetTransformed;
-    offsetTransformed.x = noise.r * offset.x - noise.g * offset.y;
-    offsetTransformed.y = noise.g * offset.x + noise.r * offset.y;
+    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
@@ -114,6 +128,7 @@
 
     float distance_intensity = maxdist - length_to_sample;
+    distance_intensity = max(distance_intensity, 0.0f);
+    // quadratic influence
     distance_intensity *= distance_intensity;
-    distance_intensity = max(distance_intensity, 0.0f);
     
     total_ao += cos_angle * SAMPLE_INTENSITY * distance_intensity;
@@ -155,20 +170,35 @@
 
 
+pixel main_ssao(fragment IN, 
+	       uniform sampler2D colors,
+	       uniform sampler2D positions,
+	       uniform sampler2D normals,
+	       uniform sampler2D noiseTexture,
+	       uniform float2 samples[NUM_SAMPLES])
+{
+  pixel OUT;
+
+  float4 col = shade(IN, colors, positions, normals);
+  float ao = ssao(IN, positions, normals, noiseTexture, samples);
+  
+  //OUT.color = tex2D(positions, IN.texCoord.xy);
+  //OUT.color = ao;
+  //OUT.color =  col;
+  OUT.color = ao * col;
+
+  return OUT;
+}
+
+
 pixel main(fragment IN, 
 	   uniform sampler2D colors,
            uniform sampler2D positions,
-           uniform sampler2D normals,
-	   uniform sampler2D noiseTexture,
-	   uniform float2 samples[NUM_SAMPLES])
+           uniform sampler2D normals)
 {
   pixel OUT;
 
-  float4 col = Shade(IN, positions, normals);
-  float ao = ssao(IN, positions, normals, noiseTexture, samples);
-  //float ao = 1.0f;
-  OUT.color = ao * col; 
-  //OUT.color = ao * (ambient + diffuse) * color;
-  //OUT.color = (ambient + diffuse) * color;
-      
+  float4 col = shade(IN, colors, positions, normals);
+  OUT.color = col;
+
   return OUT;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2819)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2821)
@@ -56,15 +56,8 @@
    OUT.position = mul(ModelViewProj, OUT.worldPos);
 
-   OUT.worldPos = IN.position;
-
    OUT.normal = IN.normal;
 
    return OUT;
 }
-
-/*struct Material
-{
-  float4 diffuse;
-}*/
 
 
