Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2860)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2861)
@@ -33,4 +33,6 @@
 #include "SampleGenerator.h"
 #include "FrameBufferObject.h"
+#include "SsaoShader.h"
+#include "DeferredShader.h"
 
 
@@ -45,35 +47,7 @@
 //-- fbos
 
-GLuint fbo;
-GLuint fbo1;
-GLuint fbo2;
+FrameBufferObject *fbo;
 
 bool isFirstTexture = true;
-
-/////////////
-//-- renderbuffers
-
-
-GLuint depthBuffer;
-
-GLuint positionsBuffer;
-GLuint colorsBuffer;
-GLuint colorsBuffer1;
-GLuint colorsBuffer2;
-GLuint normalsBuffer;
-
-
-/////////////
-//-- textures
-
-
-GLuint positionsTex;
-GLuint colorsTex;
-GLuint colorsTex1;
-GLuint colorsTex2;
-
-GLuint normalsTex;
-
-GLuint noiseTex;
 
 GLuint fontTex;
@@ -177,4 +151,5 @@
 
 bool useSsao = false;
+static float ssaoExpFactor = 0.1f;
 
 bool showAlgorithmTime = false;
@@ -190,17 +165,6 @@
 bool useFullScreen = false;
 
-// exp factor for ssao
-float expFactor = 0.05f;
-
-// ssao number of samples
-//#define NUM_SAMPLES 8
-#define NUM_SAMPLES 16
-
-// ssao random spherical samples
-static float samples[NUM_SAMPLES * 2];
-
 static Matrix4x4 matProjectionView = IdentityMatrix();
 
-FrameBufferObject *myfbo = NULL;
 
 
@@ -241,5 +205,4 @@
 
 void PlaceViewer(const Vector3 &oldPos);
-void DisplayRenderTexture();
 
 
@@ -249,10 +212,6 @@
 void InitFBO();
 
-void CreateNoiseTex2D();
-
-void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br);
-
-void GenerateSamples();
-
+SsaoShader *ssaoShader = NULL;
+DeferredShader *deferredShader = NULL;
 
 GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};
@@ -264,26 +223,10 @@
 static CGcontext sCgContext = NULL;
 static CGprogram sCgMrtVertexProgram = NULL;
-static CGprogram sCgSsaoProgram = NULL;
-static CGprogram sCgDeferredProgram = NULL;
-
-static CGparameter sColorsTexParam;
-static CGparameter sPositionsTexParam;
-static CGparameter sNormalsTexParam;
-
-static CGparameter sColorsTexParamSsao;
-static CGparameter sPositionsTexParamSsao;
-static CGparameter sNormalsTexParamSsao;
-static CGparameter sNoiseTexParamSsao;
 
 static CGparameter sModelViewProjMatrixParam;
-static CGparameter sOldModelViewProjMatrixParam;
 static CGparameter sMaxDepthParam;
-static CGparameter sMaxDepthParamSsao;
 static CGparameter sMaxDepthParamTex;
 
-static CGparameter sSamplesParamSsao; 
-static CGparameter sOldTexParamSsao;
-static CGparameter sNoiseMultiplierParam;
-static CGparameter sExpFactorParamSsao;
+static Matrix4x4 oldViewProjMatrix;
 
 
@@ -296,4 +239,5 @@
 		printf("%s\n\n", cgGetErrorString(lastError));
 		printf("%s\n", cgGetLastListing(sCgContext));
+		
 		printf("Cg error, exiting...\n");
 
@@ -343,5 +287,5 @@
 
 		env.GetBoolParam(string("useFullScreen"), useFullScreen);
-		env.GetFloatParam(string("expFactor"), expFactor);
+		env.GetFloatParam(string("expFactor"), ssaoExpFactor);
 		env.GetVectorParam(string("camPosition"), camPos);
 		env.GetVectorParam(string("camDirection"), camDir);
@@ -360,5 +304,5 @@
 		cout << "useFullScreen: " << useFullScreen << endl;
 		cout << "camPosition: " << camPos << endl;
-		cout << "expFactor: " << expFactor << endl;
+		cout << "expFactor: " << ssaoExpFactor << endl;
 		//cout << "model path: " << model_path << endl;
 	}
@@ -466,6 +410,9 @@
 	InitCg();
 
-	// create noise texture for ssao
-	CreateNoiseTex2D();
+	SsaoShader::Init(sCgContext);
+	DeferredShader::Init(sCgContext);
+
+	ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f);
+	deferredShader = new DeferredShader(texWidth, texHeight);
 
 	// initialize the render traverser
@@ -499,5 +446,4 @@
 	// get the best profile for this hardware
 	RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
-	//assert(sCgFragmentProfile != CG_PROFILE_UNKNOWN);
 	cgGLSetOptimalOptions(RenderState::sCgFragmentProfile);
 
@@ -564,101 +510,7 @@
 		cerr << "fragment program failed to load" << endl;
 
-	PrintGLerror("test");
-
-
-	///////////////
-
-	sCgSsaoProgram = 
-		cgCreateProgramFromFile(sCgContext, 
-								CG_SOURCE,
-								"src/shaders/deferred.cg", 
-								RenderState::sCgFragmentProfile,
-								"main_ssao",
-								NULL);
-
-	if (sCgSsaoProgram != NULL)
-	{
-		cgGLLoadProgram(sCgSsaoProgram);
-
-		// we need size of texture for scaling
-		sPositionsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "positions");  
-		sColorsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "colors");  
-		sNormalsTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "normals");  
-		sNoiseTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
-		sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
-		sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
-		sMaxDepthParamSsao = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
-		sExpFactorParamSsao = cgGetNamedParameter(sCgSsaoProgram, "expFactor");
-
-		cgGLSetParameter1f(sMaxDepthParamSsao, myfar / 10.0f);
-		cgGLSetParameter1f(sExpFactorParamSsao, expFactor);
-
-		sSamplesParamSsao = cgGetNamedParameter(sCgSsaoProgram, "samples");
-		sOldTexParamSsao = cgGetNamedParameter(sCgSsaoProgram, "oldTex");  
-
-		GenerateSamples(); 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"); 
-	}
-	else
-		cerr << "deferred program failed to load" << endl;
-
-
 	PrintGLerror("init");
 
 	cout << "cg initialization successful" << endl;
-}
-
-
-void PrintFBOStatus(GLenum status)
-{
-	switch(status)
-	{
-	case GL_FRAMEBUFFER_COMPLETE_EXT:
-		cout << "frame buffer object created successfully" << 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;
-	}
 }
 
@@ -668,189 +520,12 @@
 	// this fbo basicly stores the scene information we get from standard rendering of a frame
 	// we store colors, normals, positions (for the ssao)
-	fbo = new FrameBufferObject(texWidth, texHeight, true, FrameBufferObject::DEPTH_24);
-
+	fbo = new FrameBufferObject(texWidth, texHeight, FrameBufferObject::DEPTH_24);
 
 	// the diffuse color buffer
 	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false, false);
-
 	// the positions buffer
 	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false);
-
 	// the normals buffer
 	fbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false, false);
-
-
-
-	////////////////////////
-
-	glGenFramebuffersEXT(1, &fbo);
-	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
-
-	
-	////////////
-	//-- colors buffer
-
-	glGenRenderbuffersEXT(1, &colorsBuffer);
-	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, colorsBuffer);
-	
-#if 1
-	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);
-#else
-	int samples = 8;
-	glEnable(GL_MULTISAMPLE_ARB);
-	glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples, GL_RGBA8, texWidth, texHeight);
-#endif
-
-	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, colorsBuffer);
-	
-	glGenTextures(1, &colorsTex);
-	glBindTexture(GL_TEXTURE_2D, colorsTex);
-
-	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_ATTACHMENT0_EXT, GL_TEXTURE_2D, colorsTex, 0);
-
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_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);
-
-
-	//////////
-	//-- positions buffer
-
-	PrintFBOStatus(glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT));
-	glGenRenderbuffersEXT(1, &positionsBuffer);
-	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, positionsBuffer);
-	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA32F_ARB, texWidth, texHeight);
-
-	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_RENDERBUFFER_EXT, positionsBuffer);
-
-	glGenTextures(1, &positionsTex);
-	glBindTexture(GL_TEXTURE_2D, positionsTex);
-	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);
-
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-
-	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));
-	
-	
-	////////
-	//-- normals buffer
-
-	glGenRenderbuffersEXT(1, &normalsBuffer);
-	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, normalsBuffer);
-	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, texWidth, texHeight);
-	
-	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_RENDERBUFFER_EXT, normalsBuffer);
-
-	glGenTextures(1, &normalsTex);
-	glBindTexture(GL_TEXTURE_2D, normalsTex);
-	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);
-
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-
-	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));
-
-
-	///////////
-	//-- create depth buffer
-
-	glGenRenderbuffersEXT(1, &depthBuffer);	
-	glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthBuffer);
-	glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT24, texWidth, texHeight);
-	glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthBuffer);
-
-	
-	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_RGBA32F_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_RGBA32F_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);
-
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-
-	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_RGBA32F_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_RGBA32F_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_MAG_FILTER, GL_NEAREST);
-	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_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);
 
 	PrintGLerror("fbo");
@@ -1073,8 +748,6 @@
 	Matrix4x4 matViewing, matProjection;
 
-	if (renderType == RenderState::DEFERRED)
-	{
-		cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)matProjectionView.x);
-	}
+	// store matrix of last frame
+	oldViewProjMatrix = matProjectionView;
 
 	glMatrixMode(GL_PROJECTION);
@@ -1170,5 +843,5 @@
 		//glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
 
-		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+		fbo->Release();
 
 		state.SetRenderType(RenderState::FIXED);
@@ -1189,5 +862,5 @@
 		//glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
 
-		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+		fbo->Release();
 
 		cgGLDisableProfile(RenderState::sCgFragmentProfile);
@@ -1215,5 +888,5 @@
 		state.SetRenderType(RenderState::DEFERRED);
 
-		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
+		fbo->Bind();
 	
 		glPushAttrib(GL_VIEWPORT_BIT);
@@ -1226,4 +899,5 @@
 		cgGLBindProgram(sCgMrtVertexProgram);
 
+		/// draw to 3 color buffers
 		glDrawBuffers(3, mrt);
 
@@ -1240,4 +914,5 @@
 		
 
+	// reset lod levels for current frame
 	LODLevel::InitFrame(camera->GetPosition());
 
@@ -1279,6 +954,5 @@
 	if (renderType == RenderState::DEFERRED) 
 	{
-		//glPopAttrib();
-		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+		fbo->Release();
 
 		cgGLDisableProfile(RenderState::sCgVertexProfile);
@@ -1287,7 +961,9 @@
 		glDrawBuffers(1, mrt);
 
-		DisplayRenderTexture();
-	}
-
+		if (useSsao)
+			ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor);
+		else
+			deferredShader->Render(fbo);
+	}
 	
 	///////////
@@ -1379,9 +1055,9 @@
 		break;
 	case '7':
-		expFactor *= 0.5f;
+		ssaoExpFactor *= 0.5f;
 		break;
 	case '8':
-		expFactor *= 2.0f;
-		if (expFactor > 1.0f) expFactor = 1.0f;
+		ssaoExpFactor *= 2.0f;
+		if (ssaoExpFactor > 1.0f) ssaoExpFactor = 1.0f;
 		break;
 	case 'o':
@@ -1819,22 +1495,7 @@
 	if (RenderState::sCgMrtFragmentTexProgram)
 		cgDestroyProgram(RenderState::sCgMrtFragmentTexProgram);
-	if (sCgSsaoProgram)
-		cgDestroyProgram(sCgSsaoProgram);
-	if (sCgDeferredProgram)
-		cgDestroyProgram(sCgDeferredProgram);
+	
 	if (sCgContext)
 		cgDestroyContext(sCgContext);
-
-	glDeleteFramebuffersEXT(1, &fbo);
-	glDeleteRenderbuffersEXT(1, &depthBuffer);
-	glDeleteRenderbuffersEXT(1, &colorsBuffer);
-	glDeleteRenderbuffersEXT(1, &normalsBuffer);
-	glDeleteRenderbuffersEXT(1, &positionsBuffer);
-
-	glDeleteTextures(1, &colorsTex);
-	glDeleteTextures(1, &normalsTex);
-	glDeleteTextures(1, &positionsTex);
-	glDeleteTextures(1, &noiseTex);
-	glDeleteTextures(1, &fontTex);
 }
 
@@ -2065,358 +1726,2 @@
 	}
 }
-
-
-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);
-	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);
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
-	if (useSsao)
-	{
-		cgGLBindProgram(sCgSsaoProgram);
-		
-		cgGLSetTextureParameter(sPositionsTexParamSsao, positionsTex);
-		cgGLEnableTextureParameter(sPositionsTexParamSsao);
-
-		cgGLSetTextureParameter(sColorsTexParamSsao, colorsTex);
-		cgGLEnableTextureParameter(sColorsTexParamSsao);
-
-		cgGLSetTextureParameter(sNormalsTexParamSsao, normalsTex);
-		cgGLEnableTextureParameter(sNormalsTexParamSsao);
-
-		cgGLSetTextureParameter(sNoiseTexParamSsao, noiseTex);
-		cgGLEnableTextureParameter(sNoiseTexParamSsao);
-
-		cgGLSetTextureParameter(sOldTexParamSsao, oldTex);
-		cgGLEnableTextureParameter(sOldTexParamSsao);
-
-		cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
-		cgGLSetParameter1f(sExpFactorParamSsao, expFactor);
-
-		GenerateSamples(); cgGLSetParameterArray2f(sSamplesParamSsao, 0, NUM_SAMPLES, (const float *)samples);
-	}
-	else
-	{
-		cgGLBindProgram(sCgDeferredProgram);
-
-		cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
-		cgGLEnableTextureParameter(sPositionsTexParam);
-
-		cgGLSetTextureParameter(sColorsTexParam, colorsTex);
-		cgGLEnableTextureParameter(sColorsTexParam);
-
-		cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
-		cgGLEnableTextureParameter(sNormalsTexParam);
-	}
-
-	Vector3 tl, tr, bl, br;
-	ComputeViewVectors(tl, tr, bl, br);
-
-	glColor3f(1.0f, 1.0f, 1.0f);
-
-	glBegin(GL_QUADS);
-
-	// note: slightly larger texture hides ambient occlusion error on border but costs resolution
-	//float offs2 = 0.55f;
-	float offs2 = 0.5f;
-
-	glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
-	glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
-	glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
-	glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
-
-	glEnd();
-
-	
-	if (useSsao)
-	{
-		cgGLDisableTextureParameter(sColorsTexParamSsao);
-		cgGLDisableTextureParameter(sPositionsTexParamSsao);
-		cgGLDisableTextureParameter(sNormalsTexParamSsao);
-		cgGLDisableTextureParameter(sNoiseTexParamSsao);
-		cgGLDisableTextureParameter(sOldTexParamSsao);
-	}
-	else
-	{
-		cgGLDisableTextureParameter(sColorsTexParam);
-		cgGLDisableTextureParameter(sPositionsTexParam);
-		cgGLDisableTextureParameter(sNormalsTexParam);
-	}
-
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
-	
-	glEnable(GL_LIGHTING);
-	glDisable(GL_TEXTURE_2D);
-	
-	glMatrixMode(GL_PROJECTION);
-	glPopMatrix();
-
-	glMatrixMode(GL_MODELVIEW);
-	glPopMatrix();
-
-	glPopAttrib();
-
-	PrintGLerror("displaytexture");
-
-	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
-
-	DisplayRenderTexture3();
-}
-
-#if 0
-// kustls magic sample positions
-void GenerateSamples()
-{
-	static const float mysamples[] =
-	{ 
-		-0.326212f, -0.405805f,
-		-0.840144f, -0.07358f,
-		-0.695914f, 0.457137f,
-		-0.203345f, 0.620716,
-		0.96234f, -0.194983f,
-		0.473434f, -0.480026f,
-		0.519456, 0.767022f,
-		0.185461f, -0.893124f,
-		0.507431f, 0.064425f,
-		0.89642f, 0.412458f,
-		-0.32194f, -0.932615f,
-		-0.791559f, -0.597705f,
-		0.326212f, 0.405805f,
-		0.840144f, 0.07358f,
-		0.695914f, -0.457137f,
-		0.203345f, -0.620716,
-		-0.96234f, 0.194983f,
-		-0.473434f, 0.480026f,
-		-0.519456, -0.767022f,
-		-0.185461f, 0.893124f,
-		-0.507431f, -0.064425f,
-		-0.89642f, -0.412458f,
-		0.32194f, 0.932615f,
-		0.791559f, 0.597705f
-	};
-
-	for (int i = 0; i < NUM_SAMPLES; ++ i)
-	{
-		samples[i] = mysamples[i];
-	}
-}
-
-#else
-
-
-void GenerateSamples()
-{
-	/*static HaltonSequence halton;
-
-	float r[2];
-
-	// generates poisson distribution on disc
-	float minDist = 2.0f / sqrt((float)NUM_SAMPLES);
-
-	//cout << "minDist before= " << minDist << endl;
-
-	for (int i = 0; i < NUM_SAMPLES * 2; i += 2)
-	{
-		int tries = 0, totalTries = 0;
-
-		// repeat until valid sample was found
-		while (1)
-		{
-			++ tries;
-			++ totalTries;
-
-			halton.GetNext(2, r);
-
-			//const float rx = RandomValue(-1, 1);
-			//const float ry = RandomValue(-1, 1);
-
-			const float rx = r[0] * 2.0f - 1.0f;
-			const float ry = r[1] * 2.0f - 1.0f;
-
-			// check if in disk, else exit early
-			if (rx * rx + ry * ry > 1)
-				continue;
-
-			bool sampleValid = true;
-
-			// check poisson property
-			for (int j = 0; ((j < i) && sampleValid); j += 2)
-			{
-				const float dist = 
-					sqrt((samples[j] - rx) * (samples[j] - rx) +
-					     (samples[j + 1] - ry) * (samples[j + 1] - ry));
-			
-				if (dist < minDist)
-					sampleValid = false;
-			}
-
-			if (sampleValid)
-			{
-				samples[i] = rx;
-				samples[i + 1]= ry;
-				break;
-			}
-
-			if (tries > 2000)
-			{
-				minDist *= 0.9f;
-				tries = 0;
-			}
-		}
-	}
-*/
-
-	static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);
-	poisson.Generate((Sample2 *)samples);
-}
-
-
-#endif
-
-
-void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
-{
-	float myfov = fov * M_PI / 180.0f;
-
-	const float w_far = 2.0f * tan(myfov / 2.0f);
-	const float aspect = texWidth / texHeight;
-
-	const float h_far = w_far / aspect;
-
-	float t1 = h_far * 0.5f;
-	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;
-	br = br * 0.5f + 0.5f;
-	tl = tl * 0.5f + 0.5f;
-	tr = tr * 0.5f + 0.5f;
-}
-
-
-void CreateNoiseTex2D()
-{
-	randomNormals = new GLubyte[texWidth * texHeight * 3];
-
-	for (int i = 0; i < texWidth * texHeight * 3; i += 3)
-	{
-		// create random samples over sphere
-		const float rx = RandomValue(0, 1);
-		const float theta = 2.0f * acos(sqrt(1.0f - rx));
-
-		randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);
-		randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);
-		randomNormals[i + 2] = 0;
-	}
-
-	glEnable(GL_TEXTURE_2D);
-	glGenTextures(1, &noiseTex);
-	glBindTexture(GL_TEXTURE_2D, noiseTex);
-		
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-
-	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
-	//gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, texWidth, texHeight, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
-
-	glBindTexture(GL_TEXTURE_2D, 0);
-	glDisable(GL_TEXTURE_2D);
-
-	cout << "created noise texture" << endl;
-	PrintGLerror("noisetexture");
-}
