Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2833)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2834)
@@ -39,6 +39,15 @@
 
 /////////////
-//-- textures
+//-- fbos
+
 GLuint fbo;
+GLuint fbo1;
+GLuint fbo2;
+
+bool isFirstTexture = true;
+
+/////////////
+//-- renderbuffers
+
 
 GLuint depthBuffer;
@@ -46,8 +55,18 @@
 GLuint positionsBuffer;
 GLuint colorsBuffer;
+GLuint colorsBuffer1;
+GLuint colorsBuffer2;
 GLuint normalsBuffer;
+
+
+/////////////
+//-- textures
+
 
 GLuint positionsTex;
 GLuint colorsTex;
+GLuint colorsTex1;
+GLuint colorsTex2;
+
 GLuint normalsTex;
 
@@ -169,8 +188,11 @@
 
 // ssao number of samples
-#define NUM_SAMPLES 8
+//#define NUM_SAMPLES 8
+#define NUM_SAMPLES 16
 
 // ssao random spherical samples
 static float samples[NUM_SAMPLES * 2];
+
+static Matrix4x4 matProjectionView = IdentityMatrix();
 
 
@@ -249,11 +271,14 @@
 
 static CGparameter sModelViewProjMatrixParam;
+static CGparameter sOldModelViewProjMatrixParam;
 static CGparameter sMaxDepthParam;
 static CGparameter sMaxDepthParamTex;
 
 static CGparameter sSamplesParamSsao; 
-//static CGparameter sNoiseMultiplierParam;; 
-
-
+
+static CGparameter sOldTexParam;
+static CGparameter sOldTexParamSsao;
+
+static CGparameter sNoiseMultiplierParam;
 
 static void cgErrorCallback()
@@ -439,6 +464,4 @@
 void InitCg(void) 
 {
-	GenerateSamples();
-
 	// Setup Cg
 	cgSetErrorCallback(cgErrorCallback);
@@ -488,4 +511,5 @@
 
 		cgGLSetParameter1f(sMaxDepthParamTex, 10.0f / myfar);
+		Debug << "maxdepth: " << 10.0f / myfar << endl;
 	}
 	else
@@ -535,9 +559,13 @@
 		sNormalsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "normals");  
 		sNoiseTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
-		//sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
+		sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
+		sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
 		
 		cgGLDisableTextureParameter(sNoiseTexParamSsao);
 
 		sSamplesParamSsao = cgGetNamedParameter(sCgSsaoProgram, "samples");
+		sOldTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "oldTex");  
+
+		GenerateSamples();
 		cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);
 	}
@@ -561,5 +589,7 @@
 		sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions");  
 		sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors");  
-		sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");  
+		sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals"); 
+
+		sOldTexParam = cgGetNamedParameter(sCgDeferredProgram, "oldTex");  
 	}
 	else
@@ -569,5 +599,5 @@
 	PrintGLerror("init");
 
-	cout << "cg initialised" << endl;
+	cout << "cg initialization successful" << endl;
 }
 
@@ -619,5 +649,5 @@
 	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer);
 	
-	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, texWidth, texHeight);
+	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA16F_ARB, texWidth, texHeight);
 	int samples = 8;
 	//glEnable(GL_MULTISAMPLE_ARB);
@@ -628,5 +658,5 @@
 	glBindTexture(GL_TEXTURE_2D, colorsTex);
 
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);
 	glGenerateMipmapEXT(GL_TEXTURE_2D);
 	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex, 0);
@@ -651,5 +681,5 @@
 	glGenTextures(1, &positionsTex);
 	glBindTexture(GL_TEXTURE_2D, positionsTex);
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB,  texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);
 	//glGenerateMipmapEXT(GL_TEXTURE_2D);
 	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, positionsTex, 0);
@@ -675,6 +705,5 @@
 	glGenTextures(1, &normalsTex);
 	glBindTexture(GL_TEXTURE_2D, normalsTex);
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
-	//glGenerateMipmapEXT(GL_TEXTURE_2D);
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
 	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, normalsTex, 0);
 
@@ -699,4 +728,70 @@
 	PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
 	
+	
+
+	//////////////////////////////////
+
+
+
+	glGenFramebuffersEXT(1, &fbo1);
+	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1);
+
+	
+	////////////
+	//-- colors buffer
+
+	glGenRenderbuffersEXT(1, &colorsBuffer1);
+	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer1);
+	
+	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA16F_ARB, texWidth, texHeight);
+	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer1);
+	
+	glGenTextures(1, &colorsTex1);
+	glBindTexture(GL_TEXTURE_2D, colorsTex1);
+
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);
+	glGenerateMipmapEXT(GL_TEXTURE_2D);
+	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex1, 0);
+
+	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_WRAP_S, GL_CLAMP_TO_EDGE);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+	PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
+
+
+
+	/////////////////////////
+
+	glGenFramebuffersEXT(1, &fbo2);
+	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2);
+
+	
+	////////////
+	//-- colors buffer
+
+	glGenRenderbuffersEXT(1, &colorsBuffer2);
+	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer2);
+	
+	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA16F_ARB, texWidth, texHeight);
+	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer2);
+	
+	glGenTextures(1, &colorsTex2);
+	glBindTexture(GL_TEXTURE_2D, colorsTex2);
+
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, texWidth, texHeight, 0, GL_RGBA, GL_FLOAT, NULL);
+	glGenerateMipmapEXT(GL_TEXTURE_2D);
+	glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex2, 0);
+
+	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_WRAP_S, GL_CLAMP_TO_EDGE);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
+
+	PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
+
 	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
 
@@ -917,4 +1012,11 @@
 void SetupEyeView()
 {
+	Matrix4x4 matViewing, matProjection;
+
+	if (renderType == RenderState::DEFERRED)
+	{
+		cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)matProjectionView.x);
+	}
+
 	glMatrixMode(GL_PROJECTION);
 	glLoadIdentity();
@@ -932,4 +1034,9 @@
 	glLightfv(GL_LIGHT1, GL_POSITION, position1);
 
+	
+	camera->GetModelViewMatrix(matViewing);
+	camera->GetProjectionMatrix(matProjection);
+
+	matProjectionView = matViewing * matProjection;
 	
 	if (renderType == RenderState::DEFERRED)
@@ -942,5 +1049,5 @@
 		static Matrix4x4 identity = IdentityMatrix();
 		cgGLSetMatrixParameterfc(SceneEntity::sModelMatrixParam, (const float *)identity.x);
-	}	
+	}		
 }
 
@@ -1124,24 +1231,4 @@
 		perfGraph->AddData(algTime);
 
-		/*float col[3];
-		switch (renderMode)
-		{
-		case RenderTraverser::CHCPLUSPLUS:
-			col[0] = 1.0f; col[1] = 0.0f; col[2] = 0.0f;
-			break;
-		case RenderTraverser::CHC:
-			col[0] = 0.0f; col[1] = 1.0f; col[2] = 1.0f;
-			break;
-		case RenderTraverser::STOP_AND_WAIT:
-			col[0] = 1.0f; col[1] = 1.0f; col[2] = 0.0f;
-			break;
-		case RenderTraverser::CULL_FRUSTUM:
-			col[0] = 1.0f; col[1] = 0.0f; col[2] = 1.0f;
-			break;
-		default:
-			col[0] = 1.0f; col[1] = 0.0f; col[2] = 0.0f;
-		}
-		perfGraph->SetGraphColor(col);
-		*/
 		perfGraph->Draw();
 	}
@@ -1152,4 +1239,6 @@
 
 	DisplayStats();
+
+	isFirstTexture = !isFirstTexture;
 
 	glutSwapBuffers();
@@ -1530,4 +1619,5 @@
 	glPushMatrix();
 	glLoadIdentity();
+
 	gluOrtho2D(0, winWidth, 0, winHeight);
 
@@ -1540,5 +1630,7 @@
 void End2D() 
 {
+	glMatrixMode(GL_PROJECTION);
 	glPopMatrix();
+
 	glMatrixMode(GL_MODELVIEW);
 	glPopMatrix();
@@ -1570,6 +1662,5 @@
 
 	const float offs = box.Size().x * 0.3f;
-	//const float yoffs = box.Size().y * 0.5f;
-
+	
 	Vector3 pos = camera->GetPosition();
 
@@ -1578,7 +1669,10 @@
 	visCamera->SetPosition(vizpos);
 	
+	glPushAttrib(GL_VIEWPORT_BIT);
 	glViewport(winWidth - winWidth / 3, winHeight - winHeight / 3, winWidth / 3, winHeight / 3);
 
 	glMatrixMode(GL_PROJECTION);
+	glPushMatrix();
+
 	glLoadIdentity();
 	
@@ -1586,4 +1680,5 @@
 
 	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
 
 	visCamera->SetupCameraView();
@@ -1609,6 +1704,12 @@
 	visualization->Render();
 	
-	// reset vp
-	glViewport(0, 0, winWidth, winHeight);
+	
+	// reset previous settings
+	glPopAttrib();
+
+	glMatrixMode(GL_PROJECTION);
+	glPopMatrix();
+	glMatrixMode(GL_MODELVIEW);
+	glPopMatrix();
 }
 
@@ -1880,6 +1981,71 @@
 
 
+void DisplayRenderTexture3()
+{
+	glEnable(GL_TEXTURE_2D);
+
+	if (isFirstTexture)
+		glBindTexture(GL_TEXTURE_2D, colorsTex1);
+	else
+		glBindTexture(GL_TEXTURE_2D, colorsTex2);
+
+	glDisable(GL_LIGHTING);
+	
+	glMatrixMode(GL_PROJECTION);
+	glPushMatrix();
+	glLoadIdentity();
+
+	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
+	glLoadIdentity();
+
+	const float offs = 0.5f;
+	glOrtho(-offs, offs, -offs, offs, 0, 1);
+	
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
+	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);
+
+	glEnd();
+
+	glEnable(GL_LIGHTING);
+	glDisable(GL_TEXTURE_2D);
+	
+	glMatrixMode(GL_PROJECTION);
+	glPopMatrix();
+
+	glMatrixMode(GL_MODELVIEW);
+	glPopMatrix();
+
+	PrintGLerror("displaytexture3");
+}
+
+
 void DisplayRenderTexture()
 {
+	GLuint oldTex;
+
+	if (isFirstTexture)
+	{
+		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo1);
+		oldTex = colorsTex2;
+	}
+	else 
+	{
+		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo2);
+		oldTex = colorsTex1;
+	}
+
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, texWidth, texHeight);
+
+	glDrawBuffers(1, mrt);
+
 	glDisable(GL_ALPHA_TEST);
 	glDisable(GL_TEXTURE_2D);
@@ -1906,17 +2072,22 @@
 		cgGLBindProgram(sCgSsaoProgram);
 		
+		cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex);
 		cgGLEnableTextureParameter(sPositionsTexParamSsao);
-		cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex);
-	
+
+		cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex);
 		cgGLEnableTextureParameter(sColorsTexParamSsao);
-		cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex);
-
+
+		cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex);
 		cgGLEnableTextureParameter(sNormalsTexParamSsao);
-		cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex);
-
+
+		cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex);
 		cgGLEnableTextureParameter(sNoiseTexParamSsao);
-		cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex);
-
-		//cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 7.0f));
+
+		cgGLSetTextureParameter(sOldTexParamSsao, oldTex);
+		cgGLEnableTextureParameter(sOldTexParamSsao);
+
+		cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
+
+		//GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);
 	}
 	else
@@ -1924,12 +2095,15 @@
 		cgGLBindProgram(sCgDeferredProgram);
 
+		cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
 		cgGLEnableTextureParameter(sPositionsTexParam);
-		cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
-	
+
+		cgGLSetTextureParameter(sColorsTexParam, colorsTex);
 		cgGLEnableTextureParameter(sColorsTexParam);
-		cgGLSetTextureParameter(sColorsTexParam, colorsTex);
-
+
+		cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
 		cgGLEnableTextureParameter(sNormalsTexParam);
-		cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
+
+		cgGLSetTextureParameter(sOldTexParam, oldTex);
+		cgGLEnableTextureParameter(sOldTexParam);
 	}
 
@@ -1952,10 +2126,20 @@
 	glEnd();
 
-	cgGLDisableTextureParameter(sColorsTexParamSsao);
-	cgGLDisableTextureParameter(sPositionsTexParamSsao);
-	cgGLDisableTextureParameter(sNormalsTexParamSsao);
 	
 	if (useSsao)
+	{
+		cgGLDisableTextureParameter(sColorsTexParamSsao);
+		cgGLDisableTextureParameter(sPositionsTexParamSsao);
+		cgGLDisableTextureParameter(sNormalsTexParamSsao);
 		cgGLDisableTextureParameter(sNoiseTexParamSsao);
+		cgGLDisableTextureParameter(sOldTexParamSsao);
+	}
+	else
+	{
+		cgGLDisableTextureParameter(sColorsTexParam);
+		cgGLDisableTextureParameter(sPositionsTexParam);
+		cgGLDisableTextureParameter(sNormalsTexParam);
+		cgGLDisableTextureParameter(sOldTexParam);
+	}
 
 	cgGLDisableProfile(RenderState::sCgFragmentProfile);
@@ -1970,6 +2154,11 @@
 	glPopMatrix();
 
-	
+	glPopAttrib();
+
 	PrintGLerror("displaytexture");
+
+	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+
+	DisplayRenderTexture3();
 }
 
@@ -2014,42 +2203,11 @@
 #else
 
-/*void GenerateSamples()
-{
-	float scale = 1.0f / (float)NUM_SAMPLES;
-
-	////////
-	//-- generate uniformly distributed spherical samples
-
-	for (int i = 0; i < NUM_SAMPLES; ++ i)
-	{
-		// create stratified samples over sphere
-		const float rx = ((float)i + RandomValue(0, 1)) * scale;
-		const float ry = ((float)i + RandomValue(0, 1)) * scale;
-
-		const float theta = 2.0f * acos(sqrt(1.0f - rx));
-		const float phi = 2.0f * M_PI * ry;
-
-		Vector3 unit = Vector3(sin(theta) * cos(phi), sin(theta) * sin(phi), cos(theta));
-
-		//////
-		//-- we sample in texture space => store 2D component
-
-		// q: project to plane or just drop z?
-		Vector3 proj = unit / (1.0f + fabs(unit.z));
-		//Vector3 proj = unit;
-
-		samples[i + 0] = proj.x;
-		samples[i + 1] = proj.y;
-		//cout << samples[i] << " " << samples[i + 1] << endl;
-	}
-}*/
 
 void GenerateSamples()
 {
 	// generates poisson distribution on disc
-	//float factor = 5.0f;
-	float minDist = 2.0f;//factor / sqrt((float)NUM_SAMPLES);
-
-	cout << "minDist= " << minDist << endl;
+	float minDist = 2.0f / sqrt((float)NUM_SAMPLES);
+
+	//cout << "minDist before= " << minDist << endl;
 
 	for (int i = 0; i < NUM_SAMPLES * 2; i += 2)
@@ -2096,8 +2254,8 @@
 			}
 		}
-		cout << "sample: " << samples[i] << " " << i /2 << " " << samples[i + 1] << " r: " << sqrt(samples[i] * samples[i]  + samples[i + 1] * samples[i + 1]) << " tries: " << totalTries << endl;
-	}
-
-	cout << "minDist= " << minDist << endl;
+		//cout << "sample: " << samples[i] << " " << i /2 << " " << samples[i + 1] << " r: " << sqrt(samples[i] * samples[i]  + samples[i + 1] * samples[i + 1]) << " tries: " << totalTries << endl;
+	}
+
+	//cout << "minDist after= " << minDist << endl;
 }
 
@@ -2118,9 +2276,15 @@
 	float t2 = w_far * 0.5f;
 
+#if 0
 	bl = Normalize(-Vector3(-t1, -t2, 1.0f));
 	br = Normalize(-Vector3( t1, -t2, 1.0f));
 	tl = Normalize(-Vector3(-t1,  t2, 1.0f));
 	tr = Normalize(-Vector3( t1,  t2, 1.0f));
-
+#else
+	bl = -Normalize(camera->GetDirection());
+	br = -Normalize(camera->GetDirection());
+	tl = -Normalize(camera->GetDirection());
+	tr = -Normalize(camera->GetDirection());
+#endif
 	// normalize to 0 .. 1
 	bl = bl * 0.5f + 0.5f;
