Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3025)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3026)
@@ -61,5 +61,4 @@
 pixel main(fragment IN, 
 		   uniform sampler2D colors,
-		   uniform sampler2D positions,
 		   uniform sampler2D normals,
 		   uniform float3 lightDir
@@ -151,5 +150,5 @@
 				  uniform float4x4 shadowMatrix,
 				  uniform float sampleWidth,
-				  uniform sampler2D noiseTexture,
+				  uniform sampler2D noise,
 				  uniform float2 samples[NUM_PCF_TABS],
 				  uniform float weights[NUM_PCF_TABS],
@@ -199,6 +198,6 @@
 		lightSpacePos /= lightSpacePos.w;
 
-		float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noiseTexture);
-		//float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noiseTexture);
+		float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, weights, noise);
+		//float shadowTerm = CalcShadowTerm(IN, shadowMap, sampleWidth, lightSpacePos.xy, lightSpacePos.z, samples, noise);
 
 		diffuse *= shadowTerm;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2976)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 3026)
@@ -12,9 +12,9 @@
 };
 
-// the barrier for detecting a discontinuity
-uniform float4 e_barrier = float4(5e-5, 5e-5, 0, 0); // x = normal, y = depth
-// the weights for normal / depth discontinuity
-uniform float4 e_weights = float4(0.5f, 0.5f, 1.0f, 1.0f); // x = normal, y = depth
-//uniform float4 e_weights = float4(1.0f, 1.0f, 1.0f, 1.0f); // x = normal, y = depth
+// the barrier for detecting a discontinuity (x = normal, y = depth)
+uniform float4 e_barrier = float4(5e-5, 5e-5, 0, 0); 
+// the weights for normal / depth discontinuity (x = normal, y = depth)
+uniform float4 e_weights = float4(0.5f, 0.5f, 1.0f, 1.0f); 
+//uniform float4 e_weights = float4(1.0f, 1.0f, 1.0f, 1.0f);
 uniform float4 e_kernel = float4(0.5f, 1.0f, 1.0f, 1.0f); 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3019)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3026)
@@ -137,5 +137,5 @@
 		   uniform sampler2D colors,
 		   uniform sampler2D normals,
-		   uniform sampler2D noiseTexture,
+		   uniform sampler2D noise,
 		   uniform float2 samples[NUM_SAMPLES],
 		   uniform sampler2D oldTex,
@@ -161,5 +161,5 @@
 	const float3 centerPosition = eyePos + eyeSpacePos;
 
-	float4 realPos = float4(centerPosition, 1.0f);
+	const float4 realPos = float4(centerPosition, 1.0f);
 
 
@@ -174,5 +174,5 @@
 	const float currentDepth = currentPos.z * 1e-3f;
 
-	const float2 ao = ssao(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir));
+	const float2 ao = ssao(IN, colors, noise, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir));
 
 
@@ -202,5 +202,5 @@
 
 	//const float oldNumSamples = oldCol.y;
-	const float oldWeight = clamp(oldCol.y, 0, temporalCoherence);
+	const float oldWeight = 50;//clamp(oldCol.y, 0, temporalCoherence);
 
 	float newWeight;
@@ -209,5 +209,5 @@
 	//const float newNumSamples = ao.y;
 
-	if (//(temporalCoherence > 0) &&
+	if ((temporalCoherence > 0) &&
 		(tex.x >= 0.0f) && (tex.x < 1.0f) && 
 		(tex.y >= 0.0f) && (tex.y < 1.0f) && 
@@ -228,5 +228,4 @@
 	}
 
-	//OUT.illum_col.y=depthDif;
 	OUT.illum_col.y = newWeight;
 	OUT.illum_col.z = currentDepth;
@@ -269,6 +268,6 @@
 	float3 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
 
-	if (ao.y < 2000.0f)
-		ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
+	//if (ao.y < 2000.0f) 
+	//	ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
 
 	OUT.illum_col = col * ao.x;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 3019)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 3026)
@@ -61,5 +61,5 @@
 				   uniform float3 currentNormal,
 				   uniform float3 centerPosition,
-				   float w,
+				   float scaleFactor,
 				   uniform float3 bl,
 				   uniform float3 br,
@@ -96,5 +96,5 @@
 #endif
 		// weight with projected coordinate to reach similar kernel size for near and far
-		float2 texcoord = IN.texCoord.xy + offsetTransformed * w;
+		float2 texcoord = IN.texCoord.xy + offsetTransformed * scaleFactor;
 
 		//if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f)) ++ numSamples;
@@ -151,5 +151,5 @@
 		   uniform sampler2D colors,
 		   uniform sampler2D normals,
-		   uniform sampler2D noiseTexture,
+		   uniform sampler2D noise,
 		   uniform float2 samples[NUM_SAMPLES],
 		   uniform sampler2D oldSsaoTex,
@@ -191,5 +191,5 @@
 	//-- compute color bleeding + ao
 
-	GiStruct gi = globIllum(IN, colors, noiseTexture, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(IN.view));
+	GiStruct gi = globIllum(IN, colors, noise, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(IN.view));
 	
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3021)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3026)
@@ -107,4 +107,11 @@
 					FrameBufferObject *downSampleFbo, int downSampleBufferIdx);
 
+	void DrawQuad(ShaderProgram *p);
+
+	static bool InitProgram(ShaderProgram **program, 
+		                    CGcontext context, 
+							const std::string &filename,
+							const std::string &functionName);
+
 
 	////////////
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3023)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3026)
@@ -322,4 +322,5 @@
 int main(int argc, char* argv[])
 {
+/*
 #ifdef _CRT_SET
 
@@ -334,5 +335,5 @@
 	_CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR); 
 #endif
-
+*/
 	cout << "=== reading environment file ===" << endl << endl;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3025)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3026)
@@ -44,133 +44,26 @@
 {
 
-static CGprogram sCgSsaoProgram = NULL;
-static CGprogram sCgGiProgram = NULL;
-
-static CGprogram sCgDeferredProgram = NULL;
-static CGprogram sCgAntiAliasingProgram = NULL;
-static CGprogram sCgDeferredShadowProgram = NULL;
-
-static CGparameter sColorsTexCombineParam;
-static CGparameter sSsaoTexCombineParam;
-
-static CGparameter sColorsTexDeferredParam;
-static CGparameter sPositionsTexDeferredParam;
-static CGparameter sNormalsTexDeferredParam;
-
-static CGprogram sCgCombinedSsaoProgram = NULL;
-static CGprogram sCgCombinedIllumProgram = NULL;
-
-static CGparameter sColorsTexLogLumParam;
-
-static CGparameter sDownSampleOffsetParam;
-static CGparameter sFilterOffsetsParam;
-static CGparameter sFilterWeightsParam;
-static CGparameter sPCFFilterWeightsParam;
-
-static CGprogram sCgDownSampleProgram;
-static CGprogram sCgToneProgram;
-static CGprogram sCgLogLumProgram;
-
+static ShaderProgram *sCgSsaoProgram = NULL;
+static ShaderProgram *sCgGiProgram = NULL;
+
+static ShaderProgram *sCgDeferredProgram = NULL;
+static ShaderProgram *sCgAntiAliasingProgram = NULL;
+static ShaderProgram *sCgDeferredShadowProgram = NULL;
+
+static ShaderProgram *sCgCombineSsaoProgram = NULL;
+static ShaderProgram *sCgCombineIllumProgram = NULL;
+static ShaderProgram *sCgLogLumProgram = NULL;
+static ShaderProgram *sCgToneProgram = NULL;
+static ShaderProgram *sCgDownSampleProgram = NULL;
 
 ShaderContainer DeferredRenderer::sShaders;
 
-
-///////////////////////////////////////
-
-
-static CGparameter sColorsTexParam;
-static CGparameter sNormalsTexParam;
-
-static CGparameter sOldModelViewProjMatrixParam;
-static CGparameter sModelViewProjMatrixParam;
-static CGparameter sEyePosParam;
-static CGparameter sEyePosShadowParam;
-static CGparameter sSamplesParam; 
-static CGparameter sOldTexParam;
-static CGparameter sNoiseTexParam;
-static CGparameter sTemporalCoherenceParam;
-
-
-///////////////////////////////////////
-
-
-static CGparameter sColorsTexGiParam;
-static CGparameter sNormalsTexGiParam;
-
-
-static CGparameter sOldModelViewProjMatrixGiParam;
-static CGparameter sModelViewProjMatrixGiParam;
-static CGparameter sSamplesGiParam; 
-static CGparameter sOldSsaoTexGiParam;
-static CGparameter sOldIllumTexGiParam;
-static CGparameter sNoiseTexGiParam;
-static CGparameter sTemporalCoherenceGiParam;
-
-
-static CGparameter sColorsTexCombinedIllumParam;
-static CGparameter sSsaoTexCombinedIllumParam;
-static CGparameter sIllumTexCombinedIllumParam;
-
-static CGparameter sColorsTexCombinedSsaoParam;
-static CGparameter sSsaoTexCombinedSsaoParam;
-
-static CGparameter sTLParam;
-static CGparameter sTRParam;
-static CGparameter sBRParam;
-static CGparameter sBLParam;
-
-
-static CGparameter sTLGiParam;
-static CGparameter sTRGiParam;
-static CGparameter sBRGiParam;
-static CGparameter sBLGiParam;
-
-static CGparameter sEyePosGiParam;
-
-
-
-////////////
-
-static CGparameter sColorsTexAntiAliasingParam;
-static CGparameter sNormalsTexAntiAliasingParam;
-
-
-static CGparameter sShadowMapParam;
-static CGparameter sPositionsTexShadowParam;  
-static CGparameter sColorsTexShadowParam;  
-static CGparameter sNormalsTexShadowParam;
-
-static CGparameter sShadowMatrixParam;
-static CGparameter sSampleWidthParam;
-
-static CGparameter sNoiseTexShadowParam;
-static CGparameter sSamplesShadowParam; 
-
-static CGparameter sLightDirParam;
-static CGparameter sLightDirShadowParam;
-static CGparameter sImageKeyParam;
-static CGparameter sMiddleGreyParam;
-static CGparameter sWhiteLumParam;
-
-
-static CGparameter sColorsTexInitialParam;
-static CGparameter sColorsTexToneParam;
-
-static CGparameter sColorsTexDownSampleParam;
-
-//#define USE_3D_SSAO
-
-
 static GLuint noiseTex = 0;
 
 // ssao random spherical samples
-#ifdef USE_3D_SSAO
-static Sample2 samples3[NUM_SAMPLES];
-#else
 static Sample2 samples2[NUM_SAMPLES];
-#endif
-
 // number of pcf tabs
 Sample2 pcfSamples[NUM_PCF_TABS];
+
 
 int DeferredRenderer::colorBufferIdx = 0;
@@ -230,14 +123,32 @@
 }
 
+
+void DeferredRenderer::DrawQuad(ShaderProgram *p)
+{
+	Vector3 tl, tr, bl, br;
+	ComputeViewVectors(tl, tr, bl, br);
+
+	p->Bind();
+
+	// note: slightly larger texture could hide ambient occlusion error on border but costs resolution
+	const float offs = 0.5f;
+	
+	glBegin(GL_QUADS);
+
+	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-offs, -offs, -0.5f);
+	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( offs, -offs, -0.5f);
+	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( offs,  offs, -0.5f);
+	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f);
+
+	glEnd();
+
+	p->Release();
+}
+
+
 /** Generate poisson disc distributed sample points on the unit disc
 */
 static void GenerateSamples(int sampling)
 {
-#ifdef USE_3D_SSAO
-
-	SphericalSampleGenerator sph(NUM_SAMPLES, 1.0f);
-	sph.Generate((float *)samples3);
-
-#else
 	switch (sampling)
 	{
@@ -255,9 +166,9 @@
 		break;
 	default: // SAMPLING_DEFAULT
-
-		RandomSampleGenerator2 g(NUM_SAMPLES, 1.0f);
-		g.Generate((float *)samples2);
-	}
-#endif
+		{
+			RandomSampleGenerator2 g(NUM_SAMPLES, 1.0f);
+			g.Generate((float *)samples2);
+		}
+	}
 }
 
@@ -273,17 +184,4 @@
 	for (int i = 0; i < w * h * 3; i += 3)
 	{
-		
-#ifdef USE_3D_SSAO
-		//halton.GetNext(2, r);
-		r[0] = RandomValue(0, 1);
-		r[1] = RandomValue(0, 1);
-
-		const float theta = 2.0f * acos(sqrt(1.0f - r[0]));
-		const float phi = 2.0f * M_PI * r[1];
-
-		randomNormals[i + 0] = sin(theta) * cos(phi);
-		randomNormals[i + 1] = sin(theta) * sin(phi);
-		randomNormals[i + 2] = cos(theta);
-#else
 		// create random samples on a circle
 		r[0] = RandomValue(0, 1);
@@ -295,5 +193,4 @@
 		randomNormals[i + 1] = sin(theta);
 		randomNormals[i + 2] = 0;
-#endif
 	}
 
@@ -387,298 +284,52 @@
 
 
+bool DeferredRenderer::InitProgram(ShaderProgram **program, 
+								   CGcontext context, 
+								   const std::string &filename,
+								   const std::string &functionName)
+{
+	const string fullName = "src/shaders/" + filename + ".cg";
+
+	cout << "fullname: " << fullName << endl;
+
+	*program = new ShaderProgram(context, fullName, RenderState::sCgFragmentProfile, functionName);
+
+	sShaders.push_back(*program);
+
+	if (!(*program)->IsValid())
+	{
+		cerr << "Program" << functionName << " in " << fullName << " failed to load" << endl;
+		return false;
+	}
+
+	return true;
+}
+
+
 void DeferredRenderer::InitCG(CGcontext context)
 {	
-	ShaderProgram *pr;
-
-	sCgDeferredProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/deferred.cg", 
-								RenderState::sCgFragmentProfile,
-								"main",
-								NULL);
-
-	if (sCgDeferredProgram != NULL)
-	{
-		cgGLLoadProgram(sCgDeferredProgram);
-
-		// we need size of texture for scaling
-		sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");  
-		sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");  
-		sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
-		
-		sLightDirParam = cgGetNamedParameter(sCgDeferredProgram, "lightDir");
-
-		pr = new ShaderProgram(sCgDeferredProgram);
-		sShaders.push_back(pr);
-	}
-	else
-		cerr << "deferred program failed to load" << endl;
-
-
-	///////////////
-
-	sCgSsaoProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-#ifdef USE_3D_SSAO
-								"src/shaders/ssao3d.cg",
-#else
-								"src/shaders/ssao.cg", 
-#endif
-								RenderState::sCgFragmentProfile,
-								"main",
-								NULL);
-
-	if (sCgSsaoProgram != NULL)
-	{
-		cgGLLoadProgram(sCgSsaoProgram);
-
-		sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors");  
-		sEyePosParam = cgGetNamedParameter(sCgSsaoProgram, "eyePos");
-		sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals");  
-		sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
-		
-		sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
-		sModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "modelViewProj");
-		sTemporalCoherenceParam = cgGetNamedParameter(sCgSsaoProgram, "temporalCoherence");
-
-		sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");  
-		sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
-
-		sTLParam = cgGetNamedParameter(sCgSsaoProgram, "tl"); 
-		sTRParam = cgGetNamedParameter(sCgSsaoProgram, "tr"); 
-		sBRParam = cgGetNamedParameter(sCgSsaoProgram, "br"); 
-		sBLParam = cgGetNamedParameter(sCgSsaoProgram, "bl"); 
-
-		pr = new ShaderProgram(sCgSsaoProgram);
-		sShaders.push_back(pr);
-	}
-	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
-		sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors");  
-		sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals");  
-		
-		sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
-		sModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "modelViewProj");
-		sTemporalCoherenceGiParam = cgGetNamedParameter(sCgGiProgram, "temporalCoherence");
-
-		sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
-		sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
-		
-		sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex");  
-		sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex"); 
-
-		sTLGiParam = cgGetNamedParameter(sCgGiProgram, "tl"); 
-		sTRGiParam = cgGetNamedParameter(sCgGiProgram, "tr"); 
-		sBRGiParam = cgGetNamedParameter(sCgGiProgram, "br"); 
-		sBLGiParam = cgGetNamedParameter(sCgGiProgram, "bl"); 
-
-		sEyePosGiParam = cgGetNamedParameter(sCgGiProgram, "eyePos");
-
-		pr = new ShaderProgram(sCgGiProgram);
-		sShaders.push_back(pr);
-	}
-	else
-		cerr << "globillum program failed to load" << endl;
-
-	sCgCombinedIllumProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/globillum.cg", 
-								RenderState::sCgFragmentProfile,
-								"combine",
-								NULL);
-
-	if (sCgCombinedIllumProgram != NULL)
-	{
-		cgGLLoadProgram(sCgCombinedIllumProgram);
-
-		sColorsTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "colors");  
-		sSsaoTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "ssaoTex");
-		sIllumTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "illumTex");
-
-		pr = new ShaderProgram(sCgCombinedIllumProgram);
-		sShaders.push_back(pr);
-	}
-	else
-		cerr << "combined illum program failed to load" << endl;
-
-
-	sCgCombinedSsaoProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/ssao.cg", 
-								RenderState::sCgFragmentProfile,
-								"combine",
-								NULL);
-
-	if (sCgCombinedSsaoProgram != NULL)
-	{
-		cgGLLoadProgram(sCgCombinedSsaoProgram);
-
-		sColorsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "colors");  
-		sSsaoTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "ssaoTex");
-
-		sFilterOffsetsParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "filterOffs");
-		sFilterWeightsParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "filterWeights");
-
-		pr = new ShaderProgram(sCgCombinedSsaoProgram);
-		sShaders.push_back(pr);
-	}
-	else
-		cerr << "combied illum program failed to load" << endl;
-
-	
-	sCgAntiAliasingProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/antialiasing.cg", 
-								RenderState::sCgFragmentProfile,
-								"main",
-								NULL);
-
-	if (sCgAntiAliasingProgram != NULL)
-	{
-		cgGLLoadProgram(sCgAntiAliasingProgram);
-
-		sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");  
-		sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
-
-		pr = new ShaderProgram(sCgAntiAliasingProgram);
-		sShaders.push_back(pr);
-	}
-	else
-		cerr << "antialiasing program failed to load" << endl;
-
-	sCgDeferredShadowProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/deferred.cg", 
-								RenderState::sCgFragmentProfile,
-								"main_shadow",
-								NULL);
-
-	if (sCgDeferredShadowProgram != NULL)
-	{
-		cgGLLoadProgram(sCgDeferredShadowProgram);
-
-		// we need size of texture for scaling
-		sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions");  
-		sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors");  
-		sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");
-
-		sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");  
-		sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth");
-		sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
-
-		sNoiseTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "noiseTexture");
-		sSamplesShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "samples");
-		sLightDirShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "lightDir");
-
-		sEyePosShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "eyePos");
-
-		float filterWeights[NUM_PCF_TABS];
-
-		sPCFFilterWeightsParam = cgGetNamedParameter(sCgDeferredShadowProgram, "weights");
-
-		PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f);
-		poisson.Generate((float *)pcfSamples);
-
-		
-		for (int i = 0; i < NUM_PCF_TABS; ++ i)
-		{
-			filterWeights[i] = GaussianDistribution(pcfSamples[i].x, pcfSamples[i].y, 1.0f);
-		}
-
-		cgGLSetParameterArray2f(sSamplesShadowParam, 0, NUM_PCF_TABS, (const float *)pcfSamples);
-		cgGLSetParameterArray1f(sPCFFilterWeightsParam, 0, NUM_PCF_TABS, (const float *)filterWeights);
-
-		pr = new ShaderProgram(sCgDeferredShadowProgram);
-		sShaders.push_back(pr);
-	}
-	else
-		cerr << "deferred program failed to load" << endl;
-
-	sCgLogLumProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/tonemap.cg", 
-								RenderState::sCgFragmentProfile,
-								"CalcAvgLogLum",
-								NULL);
-
-	if (sCgLogLumProgram != NULL)
-	{
-		cgGLLoadProgram(sCgLogLumProgram);
-		sColorsTexLogLumParam = cgGetNamedParameter(sCgLogLumProgram, "colors"); 
-
-		pr = new ShaderProgram(sCgLogLumProgram);
-		sShaders.push_back(pr);
-	}
-	else
-		cerr << "avg loglum program failed to load" << endl;
-
-
-	sCgToneProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/tonemap.cg", 
-								RenderState::sCgFragmentProfile,
-								"ToneMap",
-								NULL);
-
-	if (sCgToneProgram != NULL)
-	{
-		cgGLLoadProgram(sCgToneProgram);
-
-		sImageKeyParam = cgGetNamedParameter(sCgToneProgram, "imageKey");
-		sMiddleGreyParam = cgGetNamedParameter(sCgToneProgram, "middleGrey");
-		sWhiteLumParam = cgGetNamedParameter(sCgToneProgram, "whiteLum");
-
-		sColorsTexToneParam = cgGetNamedParameter(sCgToneProgram, "colors");  
-
-		pr = new ShaderProgram(sCgToneProgram);
-		sShaders.push_back(pr);
-	}
-	else
-		cerr << "tone program failed to load" << endl;
-
-
-	sCgDownSampleProgram = 
-		cgCreateProgramFromFile(context, 
-								CG_SOURCE,
-								"src/shaders/tonemap.cg", 
-								RenderState::sCgFragmentProfile,
-								"DownSample",
-								NULL);
-
-	if (sCgDownSampleProgram != NULL)
-	{
-		cgGLLoadProgram(sCgDownSampleProgram);
-
-		// we need size of texture for scaling
-		sColorsTexDownSampleParam = cgGetNamedParameter(sCgDownSampleProgram, "colors"); 
-		sDownSampleOffsetParam = cgGetNamedParameter(sCgDownSampleProgram, "downSampleOffs");
-
-		pr = new ShaderProgram(sCgDownSampleProgram);
-		sShaders.push_back(pr);
-	}
-	else
-		cerr << "downsample program failed to load" << endl;
+	InitProgram(&sCgDeferredProgram, context, "deferred", "main");
+	InitProgram(&sCgDeferredShadowProgram, context, "deferred", "main_shadow");
+	InitProgram(&sCgSsaoProgram, context, "ssao", "main");
+	InitProgram(&sCgGiProgram, context, "globillum", "main");
+	InitProgram(&sCgCombineIllumProgram, context, "globillum", "combine");
+	InitProgram(&sCgCombineSsaoProgram, context, "ssao", "combine");
+	InitProgram(&sCgAntiAliasingProgram, context, "antialiasing", "main");
+	InitProgram(&sCgToneProgram, context, "tonemap", "ToneMap");
+	InitProgram(&sCgDownSampleProgram, context, "tonemap", "DownSample");
+	InitProgram(&sCgToneProgram, context, "tonemap", "ToneMap");
+	InitProgram(&sCgLogLumProgram, context, "tonemap", "CalcAvgLogLum");
+
+	float filterWeights[NUM_PCF_TABS];
+	PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f);
+	poisson.Generate((float *)pcfSamples);
+
+	for (int i = 0; i < NUM_PCF_TABS; ++ i)
+	{
+		filterWeights[i] = GaussianDistribution(pcfSamples[i].x, pcfSamples[i].y, 1.0f);
+	}
+
+	sCgDeferredShadowProgram->SetArray2f("samples", (float *)pcfSamples, NUM_PCF_TABS);
+	sCgDeferredShadowProgram->SetArray1f("weights", (float *)filterWeights, NUM_PCF_TABS);
 
 	PrintGLerror("init");
@@ -785,18 +436,7 @@
 								   )
 {
-#ifdef USE_3D_SSAO
-	// bias from [-1, 1] to [0, 1]
-	static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
-								0.0f, 0.5f, 0.0f, 0.5f,
-								0.0f, 0.0f, 0.5f, 0.5f,
-								0.0f, 0.0f, 0.0f, 1.0f); 
-
-	Matrix4x4 m = projViewMatrix * biasMatrix; 
-	cgGLSetMatrixParameterfc(sModelViewProjMatrixParam, (const float *)m.x);
-#else
-	cgGLSetMatrixParameterfc(sModelViewProjMatrixParam, (const float *)projViewMatrix.x);
-#endif
-
-	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
+	sCgSsaoProgram->SetMatrix("modelViewProj", projViewMatrix);
+	sCgSsaoProgram->SetMatrix("oldModelViewProj", oldProjViewMatrix);
+
 #if 0
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
@@ -816,23 +456,14 @@
 	glDrawBuffers(1, mrt + mIllumFboIndex);
 
-	
-	cgGLBindProgram(sCgSsaoProgram);
-
-	cgGLSetTextureParameter(sColorsTexParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexParam);
-
-	cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
-	cgGLEnableTextureParameter(sNormalsTexParam);
-
-	cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
-	cgGLEnableTextureParameter(sNoiseTexParam);
-
-	cgGLSetTextureParameter(sOldTexParam, oldTex);
-	cgGLEnableTextureParameter(sOldTexParam);
-	
 	Vector3 pos = mCamera->GetPosition();
-	cgGLSetParameter3f(sEyePosParam, pos.x, pos.y, pos.z);
-	
-	cgGLSetParameter1f(sTemporalCoherenceParam, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
+
+	sCgSsaoProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z);
+	sCgSsaoProgram->SetTexture("colors", colorsTex);
+	sCgSsaoProgram->SetTexture("normals", normalsTex);
+	sCgSsaoProgram->SetTexture("oldTex", colorsTex);
+	sCgSsaoProgram->SetTexture("noise", noiseTex);
+
+	sCgSsaoProgram->SetValue1f("temporalCoherence", (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
+	
 
 	if (mUseTemporalCoherence || mRegenerateSamples)
@@ -844,10 +475,5 @@
 		// needs longer to converge
 		GenerateSamples(mSamplingMethod); 
-
-#ifdef USE_3D_SSAO
-		cgGLSetParameterArray3f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples3);
-#else
-		cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples2);
-#endif
+		sCgSsaoProgram->SetArray2f("samples", (float *)samples2, NUM_SAMPLES);
 	}
 
@@ -855,28 +481,10 @@
 	ComputeViewVectors(tl, tr, bl, br);
 
-	cgGLSetParameter3f(sBLParam, bl.x, bl.y, bl.z);
-	cgGLSetParameter3f(sBRParam, br.x, br.y, br.z);
-	cgGLSetParameter3f(sTLParam, tl.x, tl.y, tl.z);
-	cgGLSetParameter3f(sTRParam, tr.x, tr.y, tr.z);
-
-
-	glBegin(GL_QUADS);
-
-	// note: slightly larger texture could hide ambient occlusion error on border but costs resolution
-	const float offs = 0.5f;
-	
-	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-offs, -offs, -0.5f);
-	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( offs, -offs, -0.5f);
-	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( offs,  offs, -0.5f);
-	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f);
-
-	//cout<<bl << " " << br << endl;
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexParam);
-	cgGLDisableTextureParameter(sNormalsTexParam);
-	cgGLDisableTextureParameter(sNoiseTexParam);
-	cgGLDisableTextureParameter(sOldTexParam);
+	sCgSsaoProgram->SetValue3f("bl", bl.x, bl.y, bl.z);
+	sCgSsaoProgram->SetValue3f("br", br.x, br.y, br.z);
+	sCgSsaoProgram->SetValue3f("tl", tl.x, tl.y, tl.z);
+	sCgSsaoProgram->SetValue3f("tr", tr.x, tr.y, tr.z);
+
+	DrawQuad(sCgSsaoProgram);
 
 	glPopAttrib();
@@ -923,13 +531,9 @@
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
 
-	cgGLBindProgram(sCgAntiAliasingProgram);
-	
-	cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
-
-	cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
-	cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
-
-	// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	sCgAntiAliasingProgram->SetTexture("colors", colorsTex);
+	sCgAntiAliasingProgram->SetTexture("normals", normalsTex);
+
+	sCgAntiAliasingProgram->Bind();
+
 	glColor3f(1.0f, 1.0f, 1.0f);
 
@@ -947,6 +551,5 @@
 	glEnd();
 
-	cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
-	cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
+	sCgAntiAliasingProgram->Release();
 
 	PrintGLerror("antialiasing");
@@ -964,41 +567,15 @@
 	colorBufferIdx = 3 - colorBufferIdx;
 	glDrawBuffers(1, mrt + colorBufferIdx);
-	
-	cgGLBindProgram(sCgDeferredProgram);
-
-	cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexDeferredParam);
-
-	cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
-	cgGLEnableTextureParameter(sPositionsTexDeferredParam);
-
-	cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex);
-	cgGLEnableTextureParameter(sNormalsTexDeferredParam);
-	
-	Vector3 lightDir = -light->GetDirection();
-	cgGLSetParameter3f(sLightDirParam, lightDir.x, lightDir.y, lightDir.z);
-
-	Vector3 tl, tr, bl, br;
-	ComputeViewVectors(tl, tr, bl, br);
-	
-	// note: slightly larger texture could hide ambient occlusion error on border but costs resolution
-	const float offs = 0.5f;
-	
-	glBegin(GL_QUADS);
-
-	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-offs, -offs, -0.5f);
-	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( offs, -offs, -0.5f);
-	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( offs,  offs, -0.5f);
-	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexDeferredParam);
-	cgGLDisableTextureParameter(sPositionsTexDeferredParam);
-	cgGLDisableTextureParameter(sNormalsTexDeferredParam);
+
+	const Vector3 lightDir = -light->GetDirection();
+
+	sCgDeferredProgram->SetTexture("colors", colorsTex);
+	sCgDeferredProgram->SetTexture("normals", normalsTex);
+	sCgDeferredProgram->SetValue3f("lightDir", lightDir.x, lightDir.y, lightDir.z);
+	
+	DrawQuad(sCgDeferredProgram);
 
 	PrintGLerror("deferred shading");
 }
-
 
 
@@ -1008,6 +585,6 @@
 										const Matrix4x4 &oldProjViewMatrix)
 {
-	cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
-	cgGLSetMatrixParameterfc(sModelViewProjMatrixGiParam, (const float *)projViewMatrix.x);
+	sCgGiProgram->SetMatrix("oldModelViewProj", oldProjViewMatrix);
+	sCgGiProgram->SetMatrix("modelViewProj", projViewMatrix);
 
 #if 0
@@ -1030,25 +607,13 @@
 	GLuint oldIllumTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex + 1)->GetTexture();
 	
-	cgGLBindProgram(sCgGiProgram);
-
-
-	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(sTemporalCoherenceGiParam, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
-
+
+	sCgGiProgram->SetTexture("colors", colorsTex);
+	sCgGiProgram->SetTexture("normals", normalsTex);
+	sCgGiProgram->SetTexture("noise", noiseTex);
+	sCgGiProgram->SetTexture("oldSsaoTex", oldSsaoTex);
+	sCgGiProgram->SetTexture("oldIllumTex", oldIllumTex);
+
+	sCgGiProgram->SetValue1f("temporalCoherence", 
+		(mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
 
 	if (mUseTemporalCoherence || mRegenerateSamples)
@@ -1061,5 +626,5 @@
 		GenerateSamples(mSamplingMethod); 
 
-		cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples2);
+		sCgGiProgram->SetArray2f("samples", (float *)samples2, NUM_SAMPLES);
 	}
 
@@ -1069,29 +634,12 @@
 	
 	const Vector3 pos = mCamera->GetPosition();
-	cgGLSetParameter3f(sEyePosGiParam, pos.x, pos.y, pos.z);
-
-	cgGLSetParameter3f(sBLGiParam, bl.x, bl.y, bl.z);
-	cgGLSetParameter3f(sBRGiParam, br.x, br.y, br.z);
-	cgGLSetParameter3f(sTLGiParam, tl.x, tl.y, tl.z);
-	cgGLSetParameter3f(sTRGiParam, tr.x, tr.y, tr.z);
-
-
-	// note: slightly larger texture could hide ambient occlusion error on border but costs resolution
-	const float offs = 0.5f;
-	
-	glBegin(GL_QUADS);
-
-	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-offs, -offs, -0.5f);
-	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( offs, -offs, -0.5f);
-	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( offs,  offs, -0.5f);
-	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexGiParam);
-	cgGLDisableTextureParameter(sNormalsTexGiParam);
-	cgGLDisableTextureParameter(sNoiseTexGiParam);
-	cgGLDisableTextureParameter(sOldSsaoTexGiParam);
-	cgGLDisableTextureParameter(sOldIllumTexGiParam);
+	sCgGiProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z);
+
+	sCgGiProgram->SetValue3f("bl", bl.x, bl.y, bl.z);
+	sCgGiProgram->SetValue3f("br", br.x, br.y, br.z);
+	sCgGiProgram->SetValue3f("tl", tl.x, tl.y, tl.z);
+	sCgGiProgram->SetValue3f("tr", tr.x, tr.y, tr.z);
+
+	DrawQuad(sCgGiProgram);
 
 	glPopAttrib();
@@ -1116,32 +664,9 @@
 	glDrawBuffers(1, mrt + colorBufferIdx);
 
-	cgGLBindProgram(sCgCombinedIllumProgram);
-
-
-	cgGLSetTextureParameter(sColorsTexCombinedIllumParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexCombinedIllumParam);
-
-	cgGLSetTextureParameter(sSsaoTexCombinedIllumParam, ssaoTex);
-	cgGLEnableTextureParameter(sSsaoTexCombinedIllumParam);
-
-	cgGLSetTextureParameter(sIllumTexCombinedIllumParam, illumTex);
-	cgGLEnableTextureParameter(sIllumTexCombinedIllumParam);
-	
-	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);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexCombinedIllumParam);
-	cgGLDisableTextureParameter(sSsaoTexCombinedIllumParam);
-	cgGLDisableTextureParameter(sIllumTexCombinedIllumParam);
+	sCgCombineIllumProgram->SetTexture("colors", colorsTex);
+	sCgCombineIllumProgram->SetTexture("ssaoTex", ssaoTex);
+	sCgCombineIllumProgram->SetTexture("illumTex", illumTex);
+	
+	DrawQuad(sCgCombineIllumProgram);
 
 	PrintGLerror("combine");
@@ -1162,6 +687,4 @@
 	glDrawBuffers(1, mrt + colorBufferIdx);
 
-	cgGLBindProgram(sCgCombinedSsaoProgram);
-
 	float filterOffsets[NUM_DOWNSAMPLES * 2];
 	float filterWeights[NUM_DOWNSAMPLES];
@@ -1185,29 +708,11 @@
 
 	//ComputeSampleOffsets(filterOffsets, fbo->GetWidth(), fbo->GetHeight());
-	cgGLSetParameterArray2f(sFilterOffsetsParam, 0, NUM_DOWNSAMPLES, (const float *)filterOffsets);
-	cgGLSetParameterArray1f(sFilterWeightsParam, 0, NUM_DOWNSAMPLES, (const float *)filterWeights);
-
-	cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexCombinedSsaoParam);
-
-	cgGLSetTextureParameter(sSsaoTexCombinedSsaoParam, ssaoTex);
-	cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
-
-	
-	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);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexCombinedSsaoParam);
-	cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
+	sCgCombineSsaoProgram->SetArray2f("filterOffs", (float *)filterOffsets, NUM_DOWNSAMPLES);
+	sCgCombineSsaoProgram->SetArray1f("filterWeights", (float *)filterWeights, NUM_DOWNSAMPLES);
+
+	sCgCombineSsaoProgram->SetTexture("colors", colorsTex);
+	sCgCombineSsaoProgram->SetTexture("ssaoTex", ssaoTex);
+	
+	DrawQuad(sCgCombineSsaoProgram);
 	
 	PrintGLerror("combine ssao");
@@ -1233,53 +738,19 @@
 	glDrawBuffers(1, mrt + colorBufferIdx);
 
-	
-	cgGLBindProgram(sCgDeferredShadowProgram);
-
-	cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexShadowParam);
-
-	cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
-	cgGLEnableTextureParameter(sPositionsTexShadowParam);
-
-	cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
-	cgGLEnableTextureParameter(sNormalsTexShadowParam);
-	
-	cgGLSetTextureParameter(sShadowMapParam, shadowTex);
-	cgGLEnableTextureParameter(sShadowMapParam);
-
-	cgGLSetParameter1f(sSampleWidthParam, 2.0f / shadowMap->GetSize());
-
-	 
-	cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
-
-	cgGLSetTextureParameter(sNoiseTexShadowParam, noiseTex);
-	cgGLEnableTextureParameter(sNoiseTexShadowParam);
-
-	Vector3 lightDir = -light->GetDirection();
-	cgGLSetParameter3f(sLightDirShadowParam, lightDir.x, lightDir.y, lightDir.z);
-
-	Vector3 tl, tr, bl, br;
-	ComputeViewVectors(tl, tr, bl, br);
-	
+
+	sCgDeferredShadowProgram->SetTexture("colors", colorsTex);
+	sCgDeferredShadowProgram->SetTexture("normals", normalsTex);
+	sCgDeferredShadowProgram->SetTexture("shadowMap", shadowTex);
+	sCgDeferredShadowProgram->SetTexture("noise", noiseTex);
+	sCgDeferredShadowProgram->SetMatrix("shadowMatrix", shadowMatrix);
+	sCgDeferredShadowProgram->SetValue1f("sampleWidth", 2.0f / shadowMap->GetSize());
+
+	const Vector3 lightDir = -light->GetDirection();
+	sCgDeferredShadowProgram->SetValue3f("lightDir", lightDir.x, lightDir.y, lightDir.z);
+
 	const Vector3 pos = mCamera->GetPosition();
-	cgGLSetParameter3f(sEyePosShadowParam, pos.x, pos.y, pos.z);
-
-	// note: slightly larger texture could hide ambient occlusion error on border but costs resolution
-	const float offs = 0.5f;
-	
-	glBegin(GL_QUADS);
-
-	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-offs, -offs, -0.5f);
-	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( offs, -offs, -0.5f);
-	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( offs,  offs, -0.5f);
-	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-offs,  offs, -0.5f);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexShadowParam);
-	cgGLDisableTextureParameter(sPositionsTexShadowParam);
-	cgGLDisableTextureParameter(sNormalsTexShadowParam);
-	cgGLDisableTextureParameter(sShadowMapParam);
-	cgGLDisableTextureParameter(sNoiseTexShadowParam);
+	sCgDeferredShadowProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z);
+
+	DrawQuad(sCgDeferredShadowProgram);
 
 	PrintGLerror("deferred shading + shadows");
@@ -1337,22 +808,7 @@
 	glDrawBuffers(1, mrt + colorBufferIdx);
 	
-	cgGLBindProgram(sCgLogLumProgram);
-	
-	cgGLSetTextureParameter(sColorsTexLogLumParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexLogLumParam);
-	
-	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);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexLogLumParam);
-
+	sCgLogLumProgram->SetTexture("colors", colorsTex);
+	DrawQuad(sCgLogLumProgram);
+	
 	PrintGLerror("ToneMapParams");
 
@@ -1404,37 +860,17 @@
 	glPushAttrib(GL_VIEWPORT_BIT);
 	glViewport(0, 0, downSampleFbo->GetWidth(), downSampleFbo->GetHeight());
-
-	cgGLBindProgram(sCgDownSampleProgram);
-	
-	cgGLSetTextureParameter(sColorsTexDownSampleParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexDownSampleParam);
+	
+	sCgDownSampleProgram->SetTexture("colors", colorsTex);
 
 	float downSampleOffsets[NUM_DOWNSAMPLES * 2];
 	ComputeSampleOffsets(downSampleOffsets, fbo->GetWidth(), fbo->GetHeight());
 
-	cgGLSetParameterArray2f(sDownSampleOffsetParam, 0, NUM_DOWNSAMPLES, (const float *)downSampleOffsets);
+	sCgDownSampleProgram->SetArray2f("downSampleOffs", (float *)downSampleOffsets, NUM_DOWNSAMPLES);
 
 	mDownSampleFbo->Bind();
 	glDrawBuffers(1, mrt + downSampleBufferIdx);
 
-	cgGLBindProgram(sCgDownSampleProgram);
-	
-	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);
-
-	glEnd();
-
-	cgGLDisableTextureParameter(sColorsTexDownSampleParam);
-
-	/*float *data = (float *)mDownSampleFbo->GetColorBuffer(0)->ReadTexture();
-	ExportData(data, mWidth / 2, mHeight / 2);
-	delete [] data;*/
-
+	DrawQuad(sCgDownSampleProgram);
+	
 	glPopAttrib();
 	
@@ -1456,29 +892,10 @@
 	glDrawBuffers(1, mrt + colorBufferIdx);
 
-	cgGLBindProgram(sCgToneProgram);
-
-	cgGLSetTextureParameter(sColorsTexToneParam, colorsTex);
-	cgGLEnableTextureParameter(sColorsTexToneParam);
-
-	cgGLSetParameter1f(sImageKeyParam, imageKey);
-	cgGLSetParameter1f(sWhiteLumParam, whiteLum);
-	cgGLSetParameter1f(sMiddleGreyParam, middleGrey);
-
-	glColor3f(1.0f, 1.0f, 1.0f);
-
-
-	glBegin(GL_QUADS);
-
-	const float offs = 0.5f;
-
-
-	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();
-
-	cgGLDisableTextureParameter(sColorsTexToneParam);
+	sCgToneProgram->SetTexture("colors", colorsTex);
+	sCgToneProgram->SetValue1f("imageKey", imageKey);
+	sCgToneProgram->SetValue1f("whiteLum", whiteLum);
+	sCgToneProgram->SetValue1f("middleGrey", middleGrey);
+
+	DrawQuad(sCgToneProgram);
 
 	PrintGLerror("ToneMap");
@@ -1486,5 +903,3 @@
 
 
-
-
 } // namespace
