Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.h	(revision 2961)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.h	(revision 3028)
@@ -51,4 +51,5 @@
 	static CGparameter sModelMatrixParam;
 
+
 protected:
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3026)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3028)
@@ -172,5 +172,6 @@
 	currentPos /= currentPos.w;
 	
-	const float currentDepth = currentPos.z * 1e-3f;
+	const float precisionScale = 1e-3f;
+	const float currentDepth = currentPos.z * precisionScale;
 
 	const float2 ao = ssao(IN, colors, noise, samples, normal, eyeSpacePos, w, bl, br, tl, tr, normalize(viewDir));
@@ -188,5 +189,5 @@
 
 	// the current depth projected into the old frame
-	const float projDepth = projPos.z * 1e-3f;
+	const float projDepth = projPos.z * precisionScale;
 
 	// fit from unit cube into 0 .. 1
@@ -202,5 +203,5 @@
 
 	//const float oldNumSamples = oldCol.y;
-	const float oldWeight = 50;//clamp(oldCol.y, 0, temporalCoherence);
+	const float oldWeight = clamp(oldCol.y, 0, temporalCoherence);
 
 	float newWeight;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 2965)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3028)
@@ -2,4 +2,6 @@
 #include "Geometry.h"
 #include "Material.h"
+#include "ShaderProgram.h"
+#include "Texture.h"
 
 using namespace std;
@@ -11,7 +13,7 @@
 CGprofile RenderState::sCgVertexProfile; 
 
-CGprogram RenderState::sCgMrtFragmentProgram = NULL;
-CGprogram RenderState::sCgMrtFragmentTexProgram = NULL;
-CGparameter RenderState::sTexParam;
+ShaderProgram *RenderState::sCgMrtFragmentProgram = NULL;
+ShaderProgram *RenderState::sCgMrtFragmentTexProgram = NULL;
+int RenderState::sTexParam;
 
 
@@ -123,7 +125,8 @@
 
 		if (mRenderType == DEFERRED)
-			cgGLBindProgram(sCgMrtFragmentProgram);
-
-		glDisable(GL_TEXTURE_2D);
+			sCgMrtFragmentProgram->Bind();
+		else
+			glDisable(GL_TEXTURE_2D);
+
 		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 	}
@@ -136,7 +139,8 @@
 
 			if (mRenderType == DEFERRED)
-				cgGLBindProgram(sCgMrtFragmentTexProgram);
-
-			glEnable(GL_TEXTURE_2D);
+				sCgMrtFragmentTexProgram->Bind();
+			else
+				glEnable(GL_TEXTURE_2D);
+
 			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 		}
@@ -190,3 +194,24 @@
 
 
-}
+void RenderState::SetTexture(Texture *tex)
+{
+	if (tex)
+	{
+		if (GetRenderPassType() == RenderState::DEFERRED)
+		{
+			RenderState::sCgMrtFragmentProgram->SetTexture("tex", tex->GetId());
+		}
+		else
+		{
+			tex->Bind();
+		}
+	}
+	else
+	{
+		if (!GetRenderPassType() == RenderState::DEFERRED)
+			glBindTexture(GL_TEXTURE_2D, 0);		
+	}
+}
+
+
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h	(revision 3025)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h	(revision 3028)
@@ -45,13 +45,20 @@
 	void SetArray2f(const std::string &name, float *vals, int numElements);
 	void SetArray3f(const std::string &name, float *vals, int numElements);
+	/** Sets the matrix parameter.
+	*/
+	void SetMatrix(const std::string &name, const Matrix4x4 &mat);
+	/** Sets the texture parameter.
+	*/
+	void SetTexture(const std::string &name, unsigned int tex);
+	/** Enable / disable a texture parameter.
+	*/
+	void EnableTexture(const std::string &name);
 
-	void SetMatrix(const std::string &name, const Matrix4x4 &mat);
-
-	void SetTexture(const std::string &name,  unsigned int tex);
-
-	void Release();
-
+	void DisableTexture(const std::string &name);
+	/** Binds the program.
+	*/
 	void Bind();
-
+	/** Returns true if this program is valid.
+	*/
 	inline bool IsValid() { return mProgram != NULL; }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 2965)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3028)
@@ -11,4 +11,7 @@
 namespace CHCDemoEngine 
 {
+
+class ShaderProgram;
+class Texture;
 
 /** The current render state.
@@ -70,4 +73,7 @@
 	*/
 	void LockCullFaceEnabled(bool lockCull);
+	/** Set a new texture.
+	*/
+	void SetTexture(Texture *tex);
 
 
@@ -78,8 +84,9 @@
 	static CGprofile sCgVertexProfile; 
 
-	static CGprogram sCgMrtFragmentProgram;
-	static CGprogram sCgMrtFragmentTexProgram;
+	static ShaderProgram *sCgMrtFragmentProgram;
+	static ShaderProgram *sCgMrtFragmentTexProgram;
 
-	static CGparameter sTexParam;
+	static int sTexParam;
+
 
 protected:
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 2965)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3028)
@@ -59,23 +59,5 @@
 	state->SetState(mTexture != NULL, mAlphaTestEnabled, mCullFaceEnabled);
 
-	if (state->GetRenderPassType() == RenderState::DEFERRED)
-	{
-		if (mTexture)
-		{
-			cgGLSetTextureParameter(RenderState::sTexParam, mTexture->GetId());
-			cgGLEnableTextureParameter(RenderState::sTexParam);
-		}
-		else
-		{
-			cgGLDisableTextureParameter(RenderState::sTexParam);
-		}
-	}
-	else
-	{
-		if (mTexture)
-			mTexture->Bind();
-		else
-			glBindTexture(GL_TEXTURE_2D, 0);
-	}
+	state->SetTexture(mTexture);
 
 	glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3026)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3028)
@@ -281,5 +281,6 @@
 
 static CGcontext sCgContext = NULL;
-static CGprogram sCgMrtVertexProgram = NULL;
+
+ShaderProgram *sCgMrtVertexProgram = NULL;
 
 static CGparameter sEyePosParamTex;
@@ -554,53 +555,22 @@
 	cgGLSetOptimalOptions(RenderState::sCgVertexProfile);
 
+	cgGLSetManageTextureParameters(sCgContext, true);
+
 	sCgMrtVertexProgram = 
-		cgCreateProgramFromFile(sCgContext, 
-								CG_SOURCE,
-								"src/shaders/mrt.cg", 
-								RenderState::sCgVertexProfile,
-								"vtx",
-								NULL);
-
-	if (sCgMrtVertexProgram != NULL)
-	{
-		cgGLLoadProgram(sCgMrtVertexProgram);
-
-		//sInvViewProjParam = cgGetNamedParameter(sCgMrtVertexProgram, "InvViewProj");
-		Transform3::sModelMatrixParam = cgGetNamedParameter(sCgMrtVertexProgram, "ModelView");
-	}
+		new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "vtx");
+
+	if (!sCgMrtVertexProgram->IsValid())
+		cerr << "fragment program failed to load" << endl;
 
 	RenderState::sCgMrtFragmentTexProgram = 
-		cgCreateProgramFromFile(sCgContext, 
-								CG_SOURCE,
-								"src/shaders/mrt.cg", 
-								RenderState::sCgFragmentProfile,
-								"fragtex",
-								NULL);
-
-	if (RenderState::sCgMrtFragmentTexProgram != NULL)
-	{
-		cgGLLoadProgram(RenderState::sCgMrtFragmentTexProgram);
-
-		sEyePosParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "eyePos");
-		RenderState::sTexParam = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "tex");
-	}
-	else
-		cerr << "fragment tex program failed to load" << endl;
+		new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "fragtex");
+
+	if (!RenderState::sCgMrtFragmentTexProgram->IsValid())
+		cerr << "fragment program failed to load" << endl;
 
 	RenderState::sCgMrtFragmentProgram = 
-		cgCreateProgramFromFile(sCgContext, 
-								CG_SOURCE,
-								"src/shaders/mrt.cg", 
-								RenderState::sCgFragmentProfile,
-								"frag",
-								NULL);
-
-	if (RenderState::sCgMrtFragmentProgram != NULL)
-	{
-		cgGLLoadProgram(RenderState::sCgMrtFragmentProgram);
-
-		sEyePosParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "eyePos");
-	}
-	else
+		new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "frag");
+
+	if (!RenderState::sCgMrtFragmentProgram->IsValid())
 		cerr << "fragment program failed to load" << endl;
 	
@@ -938,8 +908,8 @@
 
 	cgGLEnableProfile(RenderState::sCgVertexProfile);
-	cgGLBindProgram(sCgMrtVertexProgram);
+	sCgMrtVertexProgram->Bind();
 
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(RenderState::sCgMrtFragmentProgram);
+	RenderState::sCgMrtFragmentProgram->Bind();
 
 	const Vector3 pos = camera->GetPosition();
@@ -1779,13 +1749,7 @@
 	DEL_PTR(shadowMap);
 	DEL_PTR(shadowTraverser);
-
-	if (sCgMrtVertexProgram) 
-		cgDestroyProgram(sCgMrtVertexProgram);
-
-	if (RenderState::sCgMrtFragmentProgram)
-		cgDestroyProgram(RenderState::sCgMrtFragmentProgram);
-
-	if (RenderState::sCgMrtFragmentTexProgram)
-		cgDestroyProgram(RenderState::sCgMrtFragmentTexProgram);
+	DEL_PTR(RenderState::sCgMrtFragmentProgram);
+	DEL_PTR(RenderState::sCgMrtFragmentTexProgram);
+	DEL_PTR(sCgMrtVertexProgram);
 	
 	DeferredRenderer::ReleaseCG();
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3027)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3028)
@@ -142,6 +142,4 @@
 
 	glEnd();
-
-	p->Release();
 }
 
@@ -550,6 +548,4 @@
 	glEnd();
 
-	sCgAntiAliasingProgram->Release();
-
 	PrintGLerror("antialiasing");
 }
