Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2871)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2873)
@@ -15,9 +15,11 @@
 
 // number of ssao samples
-#define NUM_SAMPLES 16
+#define NUM_SAMPLES 10
 
 
 static CGprogram sCgSsaoProgram = NULL;
-static CGprogram sCgDeferredProgram2 = NULL;
+static CGprogram sCgGiProgram = NULL;
+
+static CGprogram sCgDeferredProgram = NULL;
 static CGprogram sCgAntiAliasingProgram = NULL;
 static CGprogram sCgCombineProgram = NULL;
@@ -30,8 +32,11 @@
 static CGparameter sNormalsTexDeferredParam;
 
+
+///////////////////////////////////////7
+
+
 static CGparameter sColorsTexParam;
 static CGparameter sPositionsTexParam;
 static CGparameter sNormalsTexParam;
-
 
 static CGparameter sOldModelViewProjMatrixParam;
@@ -43,4 +48,25 @@
 static CGparameter sExpFactorParam;
 
+
+///////////////////////////////////////
+
+
+static CGparameter sColorsTexGiParam;
+static CGparameter sPositionsTexGiParam;
+static CGparameter sNormalsTexGiParam;
+
+
+static CGparameter sOldModelViewProjMatrixGiParam;
+static CGparameter sMaxDepthGiParam;
+static CGparameter sSamplesGiParam; 
+static CGparameter sOldSsaoTexGiParam;
+static CGparameter sOldIllumTexGiParam;
+static CGparameter sNoiseTexGiParam;
+static CGparameter sNoiseMultiplierGiParam;
+static CGparameter sExpFactorGiParam;
+
+
+////////////
+
 static CGparameter sColorsTexAntiAliasingParam;
 static CGparameter sNormalsTexAntiAliasingParam;
@@ -88,4 +114,5 @@
 	mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
 	mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+	mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
 	
 	mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
@@ -93,4 +120,5 @@
 	mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
 	mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+	mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
 	
 	mFbo3 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
@@ -98,7 +126,4 @@
 	mFbo3->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
 		
-	mFbo4 = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
-	// the diffuse color buffer
-	mFbo4->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
 
 	// create noise texture for ssao
@@ -121,44 +146,24 @@
 void SsaoShader::Init(CGcontext context)
 {	
-	sCgDeferredProgram2 = 
+	sCgDeferredProgram = 
 		cgCreateProgramFromFile(context, 
 								CG_SOURCE,
 								"src/shaders/deferred.cg", 
 								RenderState::sCgFragmentProfile,
-								"main2",
+								"main",
 								NULL);
 
-	if (sCgDeferredProgram2 != NULL)
-	{
-		cgGLLoadProgram(sCgDeferredProgram2);
+	if (sCgDeferredProgram != NULL)
+	{
+		cgGLLoadProgram(sCgDeferredProgram);
 
 		// we need size of texture for scaling
-		sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "positions");  
-		sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "colors");  
-		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram2, "normals"); 
+		sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");  
+		sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");  
+		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 
 	}
 	else
 		cerr << "deferred program failed to load" << endl;
 
-
-	/*sCgCombineProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/ssao.cg", 
-								RenderState::sCgFragmentProfile,
-								"combined",
-								NULL);
-
-	if (sCgCombineProgram != NULL)
-	{
-		cgGLLoadProgram(sCgCombineProgram);
-
-		// we need size of texture for scaling
-		sColorsTexCombineParam = cgGetNamedParameter(sCgCombineProgram, "colors");  
-		sSsaoTexCombineParam = cgGetNamedParameter(sCgCombineProgram, "ssaoTex"); 
-	}
-	else
-		cerr << "combined program failed to load" << endl;
-*/
 
 	///////////////
@@ -189,4 +194,5 @@
 		sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
 		sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");  
+		
 
 		// generate samples for ssao kernel
@@ -196,4 +202,39 @@
 	else
 		cerr << "ssao program failed to load" << endl;
+
+	sCgGiProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/globillum.cg", 
+								RenderState::sCgFragmentProfile,
+								"main",
+								NULL);
+
+	if (sCgGiProgram != NULL)
+	{
+		cgGLLoadProgram(sCgGiProgram);
+
+		// we need size of texture for scaling
+		sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions");  
+		sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors");  
+		sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals");  
+		sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
+		sNoiseMultiplierGiParam = cgGetNamedParameter(sCgGiProgram, "noiseMultiplier");
+		
+		sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
+		sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth");
+		sExpFactorGiParam = cgGetNamedParameter(sCgGiProgram, "expFactor");
+
+		sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
+		
+		sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex");  
+		sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex");  
+
+		// generate samples for ssao kernel
+		GenerateSamples(); 
+		cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
+	}
+	else
+		cerr << "globillum program failed to load" << endl;
 
 	sCgAntiAliasingProgram = 
@@ -258,5 +299,8 @@
 
 	FirstPass(fbo);
+	
 	ComputeSsao(fbo, expFactor);
+	//ComputeGlobIllum(fbo, expFactor);
+
 	//Combine(fbo);
 	AntiAliasing(fbo);
@@ -323,5 +367,8 @@
 
 
-	//GenerateSamples(); 
+	// q: should we generate new samples or only rotate the old ones?
+	// in the first case, the sample patterns look nicer, but the kernel
+	// needs longer to converge
+	GenerateSamples(); 
 	cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
 
@@ -499,5 +546,5 @@
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
 
-	cgGLBindProgram(sCgDeferredProgram2);
+	cgGLBindProgram(sCgDeferredProgram);
 
 	cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
@@ -535,47 +582,92 @@
 
 
-void SsaoShader::Combine(FrameBufferObject *fbo)
+void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo, float expFactor)
 {
 	GLuint colorsTex = mFbo3->GetColorBuffer(0)->GetTexture();
-	GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
-
-	mFbo4->Bind();
-	//mNewFbo->Bind();
+	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+
+	if (1)
+	{
+		// generate mip map levels for position texture
+		glBindTexture(GL_TEXTURE_2D, positionsTex);
+		glGenerateMipmapEXT(GL_TEXTURE_2D);
+
+		// generate mip map levels for position texture
+		glBindTexture(GL_TEXTURE_2D, colorsTex);
+		glGenerateMipmapEXT(GL_TEXTURE_2D);
+	}
+
+
+	// read the second buffer, write to the first buffer
+	mNewFbo->Bind();
+	glDrawBuffers(3, mymrt);
+
+	GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
+	GLuint oldIllumTex = mOldFbo->GetColorBuffer(2)->GetTexture();
 
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-	
-	glDrawBuffers(1, mymrt);
 
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
-	cgGLBindProgram(sCgCombineProgram);
-
-	cgGLSetTextureParameter(sColorsTexCombineParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexCombineParam);
-
-	cgGLSetTextureParameter(sSsaoTexCombineParam, ssaoTex);
-	cgGLEnableTextureParameter(sSsaoTexCombineParam);
+	cgGLBindProgram(sCgGiProgram);
+
+	cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
+	cgGLEnableTextureParameter(sPositionsTexGiParam);
+
+	cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexGiParam);
+
+	cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
+	cgGLEnableTextureParameter(sNormalsTexGiParam);
+
+	cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
+	cgGLEnableTextureParameter(sNoiseTexGiParam);
+
+	cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
+	cgGLEnableTextureParameter(sOldSsaoTexGiParam);
+
+	cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
+	cgGLEnableTextureParameter(sOldIllumTexGiParam);
+
+	cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
+	
+	cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
+	cgGLSetParameter1f(sExpFactorGiParam, expFactor);
+
+
+	// q: should we generate new samples or only rotate the old ones?
+	// in the first case, the sample patterns look nicer, but the kernel
+	// needs longer to converge
+	GenerateSamples(); 
+	cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
+
+	Vector3 tl, tr, bl, br;
+	ComputeViewVectors(tl, tr, bl, br);
 
 	glColor3f(1.0f, 1.0f, 1.0f);
 
-	const float offs = 0.5f;
-
 	glBegin(GL_QUADS);
 
-	glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
-	glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
-	glTexCoord2f(1, 1); glVertex3f( offs,  offs, -0.5f);
-	glTexCoord2f(0, 1); glVertex3f(-offs,  offs, -0.5f);
+	// note: slightly larger texture hides ambient occlusion error on border but costs resolution
+	//const float new_offs = 0.55f;
+	const float new_offs = 0.5f;
+	
+	glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f);
+	glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f);
+	glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f);
+	glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f);
 
 	glEnd();
 
-	cgGLDisableTextureParameter(sColorsTexCombineParam);
-	cgGLDisableTextureParameter(sSsaoTexCombineParam);
-
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+	cgGLDisableTextureParameter(sColorsTexGiParam);
+	cgGLDisableTextureParameter(sPositionsTexGiParam);
+	cgGLDisableTextureParameter(sNormalsTexGiParam);
+	cgGLDisableTextureParameter(sNoiseTexGiParam);
+	cgGLDisableTextureParameter(sOldSsaoTexGiParam);
+	cgGLDisableTextureParameter(sOldIllumTexGiParam);
 
 	FrameBufferObject::Release();
 
-	PrintGLerror("deferred shading");
+	PrintGLerror("globillum first pass");
 }
 
