Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h	(revision 2968)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.h	(revision 3034)
@@ -11,4 +11,6 @@
 class Texture;
 class RenderState;
+class GPUProgramParameters;
+class ShaderProgram;
 
 
@@ -45,4 +47,5 @@
 	Material();
 
+	~Material();
 	/** Sets ambient and diffuse color to color
 	*/
@@ -70,5 +73,4 @@
 	inline bool IsAlphaTestEnabled() const { return mAlphaTestEnabled; } 
 	inline bool IsCullFaceEnabled() const { return mCullFaceEnabled; } 
-
 	/** Renders this material.
 	*/
@@ -77,4 +79,10 @@
 	*/
 	void InitMaterial();
+
+	GPUProgramParameters *mGPUVertexParameters;
+	GPUProgramParameters *mGPUFragmentParameters;
+
+	ShaderProgram *mFragmentProgram;
+	ShaderProgram *mVertexProgram;
 
 protected:
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.cpp	(revision 3033)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.cpp	(revision 3034)
@@ -46,4 +46,5 @@
 void GPUProgramParameters::SetTexture(int idx, unsigned int tex)
 {
+	mTextures.push_back(IntParam(idx, tex));
 }
 
@@ -56,11 +57,11 @@
 void GPUProgramParameters::SetValue1f(const string &name, float val)
 {
-	SetValue1f(mProgram->GetParameter(name), val);
-}
-
-
-void ShaderProgram::SetValue2f(const string &name, float val1, float val2)
-{
-	SetValue2f(mProgram->GetParameter(name), val1, val2);
+	SetValue1f(mProgram->GetParamIdxByName(name), val);
+}
+
+
+void GPUProgramParameters::SetValue2f(const string &name, float val1, float val2)
+{
+	SetValue2f(mProgram->GetParamIdxByName(name), val1, val2);
 }
 
@@ -68,5 +69,5 @@
 void GPUProgramParameters::SetValue3f(const string &name, float val1, float val2, float val3)
 {
-	SetValue3f(mProgram->GetParameter(name), val1, val2, val3);
+	SetValue3f(mProgram->GetParamIdxByName(name), val1, val2, val3);
 }
 
@@ -74,11 +75,11 @@
 void GPUProgramParameters::SetArray1f(const string &name, float *vals, int numElements)
 {
-	SetArray1f(mProgram->GetParameter(name), numElements, (const float *)vals);
-}
-
-
-void GPUProgramParameters::GetParameter(const string &name, float *vals, int numElements)
-{
-	SetArray2f(mProgram->GetParameter(name, numElements, (const float *)vals);
+	SetArray1f(mProgram->GetParamIdxByName(name), vals, numElements);
+}
+
+
+void GPUProgramParameters::SetArray2f(const string &name, float *vals, int numElements)
+{
+	SetArray2f(mProgram->GetParamIdxByName(name), vals, numElements);
 }
 
@@ -86,5 +87,5 @@
 void GPUProgramParameters::SetArray3f(const string &name, float *vals, int numElements)
 {
-	SetArray3f(mProgram->GetParameter(name), numElements, (const float *)vals);
+	SetArray3f(mProgram->GetParamIdxByName(name), vals, numElements);
 }
 
@@ -92,5 +93,5 @@
 void GPUProgramParameters::SetMatrix(const string &name, Matrix4x4 *mat)
 {
-	SetMatrix(mProgram->GetParameter(name), mat);
+	SetMatrix(mProgram->GetParamIdxByName(name), mat);
 }
 
@@ -98,6 +99,48 @@
 void GPUProgramParameters::SetTexture(const string &name, unsigned int tex)
 {
-	SetTexture(mProgram->GetOrCreateParameter(name), tex);
-}
+	SetTexture(mProgram->GetParamIdxByName(name), tex);
+}
+
+
+void GPUProgramParameters::UpdateParameters()
+{
+	for (size_t i = 0; i < mFloats.size(); ++ i)
+	{
+		const FloatParam &p = mFloats[i];
+
+		switch (p.mNumComponents)
+		{
+		case 1:
+			mProgram->SetValue1f(p.mIndex, p.mValues[0]);
+			break;
+		case 2:
+			mProgram->SetValue2f(p.mIndex, p.mValues[0], p.mValues[1]);
+			break;
+		case 3:
+			mProgram->SetValue3f(p.mIndex, p.mValues[0], p.mValues[1], p.mValues[2]);
+			break;
+		default:
+			mProgram->SetValue1f(p.mIndex, p.mValues[0]);
+		}
+	}
+
+	for (size_t i = 0; i < mTextures.size(); ++ i)
+	{
+		const IntParam &p = mTextures[i];
+		mProgram->SetTexture(p.mIndex, p.mValue);
+	}
+
+	for (size_t i = 0; i < mMatrices.size(); ++ i)
+	{
+		const MatrixParam &p = mMatrices[i];
+		mProgram->SetMatrix(p.mIndex, *p.mValue);
+	}
+}
+
+
+
+/********************************************************/
+/*             ShaderProgram implementation             */
+/********************************************************/
 
 
@@ -116,7 +159,8 @@
 	if (mProgram) cgGLLoadProgram(mProgram);
 
-	mParameters.resize(64);
-
-	for (int i = 0; i < 64; ++ i)
+	const int maxParams = 64;
+	mParameters.resize(maxParams);
+
+	for (int i = 0; i < maxParams; ++ i)
 	{
 		mParameters[i] = NULL;
@@ -151,5 +195,7 @@
 	CGparameter p = GetOrCreateParameter(name);
 
+	mParamHash[name] = idx;
 	mParameters[idx] = p;
+
 	return p;
 }
@@ -164,5 +210,4 @@
 	{
 		p = cgGetNamedParameter(mProgram, name.c_str());
-		mParamHash[name] = i;
 	}
 	else
@@ -178,6 +223,5 @@
 CGparameter ShaderProgram::GetParameter(int idx) const
 {
-	CGparameter p = mParameters[idx];
-	return p;
+	return mParameters[idx];
 }
 
@@ -231,4 +275,55 @@
 	cgGLSetTextureParameter(p, tex);
 	cgGLEnableTextureParameter(p);
+}
+
+
+void ShaderProgram::SetValue1f(const string &name, float val)
+{
+	cgGLSetParameter1f(GetOrCreateParameter(name), val);
+}
+
+
+void ShaderProgram::SetValue2f(const string &name, float val1, float val2)
+{
+	cgGLSetParameter2f(GetOrCreateParameter(name), val1, val2);
+}
+
+
+void ShaderProgram::SetValue3f(const string &name, float val1, float val2, float val3)
+{
+	cgGLSetParameter3f(GetOrCreateParameter(name), val1, val2, val3);
+}
+
+
+void ShaderProgram::SetArray1f(const string &name, float *vals, int numElements)
+{
+	cgGLSetParameterArray1f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
+}
+
+
+void ShaderProgram::SetArray2f(const string &name, float *vals, int numElements)
+{
+	cgGLSetParameterArray2f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
+}
+
+
+void ShaderProgram::SetArray3f(const string &name, float *vals, int numElements)
+{
+	cgGLSetParameterArray3f(GetOrCreateParameter(name), 0, numElements, (const float *)vals);
+}
+
+
+void ShaderProgram::SetMatrix(const string &name, const Matrix4x4 &mat)
+{
+	cgGLSetMatrixParameterfc(GetOrCreateParameter(name), (const float *)mat.x);
+}
+
+
+void ShaderProgram::SetTexture(const string &name, unsigned int tex)
+{
+	CGparameter p = GetOrCreateParameter(name);
+
+	cgGLSetTextureParameter(p, tex);
+	cgGLEnableTextureParameter(p);
 
 	mTextureParams.push_back(p);
@@ -236,7 +331,8 @@
 
 
-int ShaderProgram::GetParameter(const std::string &name)
-{
-	return mParamHash[name];
+int ShaderProgram::GetParamIdxByName(const std::string &name) const
+{
+	CGParameterMap::const_iterator it = mParamHash.find(name);
+	return (*it).second;
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3026)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 3034)
@@ -175,7 +175,5 @@
 	const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
 
-	float4 position;
-	position.xyz = eyePos - viewDir * eyeDepth;
-	
+	const float4 worldPos = float4(eyePos - viewDir * eyeDepth, 1);
 	
 	// diffuse intensity
@@ -193,7 +191,5 @@
 		)
 	{
-		position.w = 1.0f;
-
-		float4 lightSpacePos = mul(shadowMatrix, position);
+		float4 lightSpacePos = mul(shadowMatrix, worldPos);
 		lightSpacePos /= lightSpacePos.w;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 3031)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 3034)
@@ -15,5 +15,5 @@
 
   float4 color: COLOR0;  
-  float4 worldPos: TEXCOORD1; // world position
+  float4 eyePos: TEXCOORD1; // eye position
   float3 normal: TEXCOORD2;
 };
@@ -24,9 +24,9 @@
 {
 	float4 color: COLOR0;  
-	float4 position: POSITION; // eye space
+	//float4 position: POSITION;
 	float4 texCoord: TEXCOORD0;    
 
 	float4 winPos: WPOS;
-	float4 worldPos: TEXCOORD1; // world position
+	float4 eyePos: TEXCOORD1; // eye position
 	float3 normal: TEXCOORD2;
 };
@@ -40,8 +40,8 @@
 };
 
+
 #pragma position_invariant vtx
 
-vtxout vtx(vtxin IN, 
-		   uniform float4x4 modelView)
+vtxout vtx(vtxin IN)
 {
 	vtxout OUT;
@@ -50,7 +50,7 @@
 	OUT.texCoord = IN.texCoord;
 
-	//OUT.worldPos = mul(glstate.matrix.inverse.projection, OUT.position);
-	OUT.worldPos = mul(modelView, IN.position);
 	// transform the vertex position into eye space
+	OUT.eyePos = mul(glstate.matrix.modelview[0], IN.position);
+	// transform the vertex position into post projection space
 	OUT.position = mul(glstate.matrix.mvp, IN.position);
 
@@ -60,24 +60,8 @@
 }
 
-
-// bilinear interpolation
-inline float3 Interpol(float2 w, float3 bl, float3 br, float3 tl, float3 tr)
-{
-	float3 x1 = lerp(bl, tl, w.y);
-	float3 x2 = lerp(br, tr, w.y); 
-	float3 v = lerp(x1, x2, w.x); 
-
-	return v;
-}
-
 //#pragma position_invariant fragtex
 
 pixel fragtex(fragin IN, 
-			  uniform sampler2D tex,
-			  uniform float3 eyePos,
-			  uniform float3 bl,
-			  uniform float3 br,
-			  uniform float3 tl,
-			  uniform float3 tr
+			  uniform sampler2D tex
 			  )
 {
@@ -99,5 +83,5 @@
 
 	// compute eye linear depth
-	pix.col.w = length(eyePos - IN.worldPos.xyz);
+	pix.col.w = length(IN.eyePos.xyz);
 
 	return pix;
@@ -105,10 +89,5 @@
 
 
-pixel frag(fragin IN, 
-		   uniform float3 eyePos,
-		   uniform float3 bl,
-		   uniform float3 br,
-		   uniform float3 tl,
-		   uniform float3 tr)
+pixel frag(fragin IN)
 {
 	pixel pix;
@@ -119,7 +98,8 @@
 	pix.norm = IN.normal;
 
+	pix.col.w = length(IN.eyePos.xyz);
+
 	// hack: squeeze some information about the ambient term into the target
 	//pix.col.w = glstate.material.emission.x;
-	pix.col.w = length(eyePos - IN.worldPos.xyz);
 
 	return pix;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3028)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 3034)
@@ -159,7 +159,5 @@
 	const float eyeDepth = tex2Dlod(colors, float4(IN.texCoord, 0, 0)).w;
 	const float3 eyeSpacePos = -viewDir * eyeDepth;
-	const float3 centerPosition = eyePos + eyeSpacePos;
-
-	const float4 realPos = float4(centerPosition, 1.0f);
+	const float4 worldPos = float4(eyePos + eyeSpacePos, 1.0f);
 
 
@@ -167,5 +165,5 @@
 	//-- calculcate the current projected depth for next frame
 	
-	float4 currentPos = mul(modelViewProj, realPos);
+	float4 currentPos = mul(modelViewProj, worldPos);
 	
 	const float w = SAMPLE_RADIUS / currentPos.w;
@@ -185,7 +183,7 @@
 	
 	// calculate projected depth
-	float4 projPos = mul(oldModelViewProj, realPos);
+	float4 projPos = mul(oldModelViewProj, worldPos);
 	projPos /= projPos.w;
-
+	
 	// the current depth projected into the old frame
 	const float projDepth = projPos.z * precisionScale;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 3026)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 3034)
@@ -179,11 +179,12 @@
 	const float3 eyeSpacePos = -viewDir * eyeDepth;
 
-	const float3 centerPosition = eyePos + eyeSpacePos;
-	const float4 realPos = float4(centerPosition, 1.0f);
+	const float4 worldPos = float4(eyePos + eyeSpacePos, 1.0f);
 
 	// calculcate the current projected depth for next frame
-	float4 currentPos = mul(modelViewProj, realPos);
+	float4 currentPos = mul(modelViewProj, worldPos);
+
 	const float w = SAMPLE_RADIUS / currentPos.w;
 	currentPos /= currentPos.w;
+	
 	const float currentDepth = currentPos.z * 1e-3f;
 
@@ -197,8 +198,7 @@
 	//-- compute temporally smoothing
 
-	
 	// reprojection new frame into old one 
 	// calculate projected depth
-	float4 projPos = mul(oldModelViewProj, realPos);
+	float4 projPos = mul(oldModelViewProj, worldPos);
 	projPos /= projPos.w;
 
@@ -210,6 +210,6 @@
 
 	// retrieve the sample from the last frame
-	float3 oldSsao = tex2D(oldSsaoTex, tex);
-	float3 oldIllum = tex2D(oldIllumTex, tex);
+	float3 oldSsao = tex2D(oldSsaoTex, tex).xyz;
+	float3 oldIllum = tex2D(oldIllumTex, tex).xyz;
 
 	const float oldDepth = oldSsao.z;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3031)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3034)
@@ -25,8 +25,4 @@
 
 RenderState::RenderState():
-mAlphaTestEnabled(false), 
-mCullFaceEnabled(true), 
-mTexturesEnabled(false), 
-mMode(RENDER),
 mRenderType(FIXED),
 mUseAlphaToCoverage(true),
@@ -62,5 +58,6 @@
 		glDepthMask(GL_FALSE);
 
-		SetState(false, false, false);
+		static Material defaultMat;
+		SetState(&defaultMat);
 	}
 	else // mode returns to render
@@ -88,7 +85,12 @@
 
 	
-void RenderState::SetState(Texture *tex, bool alphaTest, bool cullFace)
-{
+void RenderState::SetState(Material *mat)
+{
+	Texture *tex = mat->GetTexture();
+
 	const bool texturing = (tex != NULL);
+
+	bool alphaTest = mat->IsAlphaTestEnabled();
+	bool cullFace = mat->IsCullFaceEnabled();
 
 	if (!mLockCullFaceEnabled)
@@ -129,7 +131,5 @@
 		mTexturesEnabled = false;
 
-		if (mRenderType == DEFERRED)
-			sCgMrtFragmentProgram->Bind();
-		else
+		if (mRenderType != DEFERRED)//!mat->mFragmentProgram)
 			glDisable(GL_TEXTURE_2D);
 
@@ -143,7 +143,5 @@
 			mTexturesEnabled = true;
 
-			if (mRenderType == DEFERRED)
-				sCgMrtFragmentTexProgram->Bind();
-			else
+			if (mRenderType != DEFERRED)//!mat->mFragmentProgram)
 				glEnable(GL_TEXTURE_2D);
 
@@ -152,5 +150,35 @@
 	}
 
-	SetTexture(tex);
+	if (mRenderType == DEFERRED)
+	{
+		if (mat->mFragmentProgram && (mat->mFragmentProgram != mCurrentFragmentProgram))
+		{
+			mCurrentFragmentProgram = mat->mFragmentProgram;
+			mCurrentFragmentProgram->Bind();
+		}
+	
+		if (mat->mVertexProgram && (mat->mVertexProgram != mCurrentVertexProgram))
+		{
+			mCurrentVertexProgram = mat->mVertexProgram;
+			mCurrentVertexProgram->Bind();
+		}
+	}
+	else
+	{
+		// set fixed texture
+		if (tex)
+		{
+			if (mCurrentTexId != tex->GetId())
+			{
+				tex->Bind();
+				mCurrentTexId = tex->GetId();
+			}
+		}
+		else
+		{
+			mCurrentTexId = 0;
+			glBindTexture(GL_TEXTURE_2D, 0);
+		}
+	}
 }
 
@@ -158,20 +186,23 @@
 void RenderState::Reset()
 {
+	SetMode(RENDER);
+
 	mCurrentVboId = -1;
+	mCurrentTexId = 0;
 
 	mCullFaceEnabled = true;
+	mAlphaTestEnabled = false;
+	mTexturesEnabled = false;
+
+	mCurrentVertexProgram = NULL;
+	mCurrentFragmentProgram = NULL;
+
 	glEnable(GL_CULL_FACE);
-	
-	mAlphaTestEnabled = false;
-	
+
 	glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
 	glDisable(GL_ALPHA_TEST);
 
-	mTexturesEnabled = false;
-
 	glDisable(GL_TEXTURE_2D);
 	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-
-	SetMode(RENDER);
 }
 
@@ -201,20 +232,3 @@
 
 
-void RenderState::SetTexture(Texture *tex)
-{
-	if (GetRenderPassType() == RenderState::DEFERRED)
-	{
-		if (tex)
-			RenderState::sCgMrtFragmentTexProgram->SetTexture("tex", tex->GetId());
-	}
-	else
-	{
-		if (tex)
-			tex->Bind();
-		else
-			glBindTexture(GL_TEXTURE_2D, 0);		
-	}
-}
-
-
-}
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3021)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3034)
@@ -11,4 +11,6 @@
 #include "Shape.h"
 #include "LODInfo.h"
+#include "RenderState.h"
+#include "ShaderProgram.h"
 
 
@@ -185,9 +187,9 @@
 	str.read(reinterpret_cast<char *>(&mat->mCullFaceEnabled), sizeof(bool));
 
-	// material
-	bool hasMaterial;
-	str.read(reinterpret_cast<char *>(&hasMaterial), sizeof(bool));
-	
-	if (hasMaterial)
+	// gl material
+	bool hasGlMaterial;
+	str.read(reinterpret_cast<char *>(&hasGlMaterial), sizeof(bool));
+	
+	if (hasGlMaterial)
 	{
 		// only write rgb part of the material
@@ -198,4 +200,21 @@
 	}
 
+	mat->mVertexProgram = RenderState::sCgMrtVertexProgram;
+
+	if (mat->GetTexture())
+	{
+		mat->mFragmentProgram = RenderState::sCgMrtFragmentTexProgram;
+
+		mat->mGPUFragmentParameters = new GPUProgramParameters(mat->mFragmentProgram);
+		mat->mGPUFragmentParameters->SetTexture(0, mat->GetTexture()->GetId());
+	}	
+	else
+	{
+		mat->mFragmentProgram = RenderState::sCgMrtFragmentProgram;
+		mat->mGPUFragmentParameters = new GPUProgramParameters(mat->mFragmentProgram);
+	}
+
+	mat->mGPUVertexParameters = new GPUProgramParameters(RenderState::sCgMrtVertexProgram);
+
 	return mat;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h	(revision 3033)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h	(revision 3034)
@@ -34,4 +34,9 @@
 class GPUProgramParameters
 {
+public:
+
+	///////////
+	//-- set parameters by index
+
 	void SetValue1f(int idx, float value);
 	void SetValue2f(int idx, float val1, float val2);
@@ -49,4 +54,9 @@
 	void SetMatrix(int idx, Matrix4x4 *mat);
 
+
+	///////////
+	//-- set parameters by name (slower!)
+
+
 	void SetValue1f(const std::string &name, float value);
 	void SetValue2f(const std::string &name, float val1, float val2);
@@ -56,4 +66,5 @@
 	void SetArray2f(const std::string &name, float *vals, int numElements);
 	void SetArray3f(const std::string &name, float *vals, int numElements);
+
 	/** Sets the texture parameter.
 	*/
@@ -62,19 +73,30 @@
 	*/
 	void SetMatrix(const std::string &name, Matrix4x4 *mat);
-
-	struct ParameterFloat
+	/** Feeds the shader program with the parameter values.
+	*/
+	void UpdateParameters();
+
+
+	GPUProgramParameters(ShaderProgram *p);
+
+
+protected:
+
+	struct FloatParam
 	{
 		int mIndex;
 		float mValues[4];
-		float mNumComponents;
-	};
-
-	struct ParameterValueInt
-	{
+		int mNumComponents;
+	};
+
+	struct IntParam
+	{
+		IntParam(int idx, float val): mIndex(idx), mValue(val) {}
+
 		int mIndex;
 		float mValue;
 	};
 
-	struct ParameterValueMatrix
+	struct MatrixParam
 	{
 		int mIndex;
@@ -82,5 +104,5 @@
 	};
 
-	struct ParameterValueArray
+	struct ArrayParam
 	{
 		int mIndex;
@@ -89,19 +111,14 @@
 		int mNumEntries;
 
-		float mNumComponents;
-	};
-
-
-	GPUProgramParameters(ShaderProgram *p);
-
-
-protected:
+		int mNumComponents;
+	};
+
 
 	ShaderProgram *mProgram;
 
-	std::vector<ParameterFloat> mFloats;
-
-	std::vector<ParameterValueInt> mTextures;
-	std::vector<ParameterValueMatrix> mMatrices;
+	std::vector<FloatParam> mFloats;
+	std::vector<IntParam> mTextures;
+	std::vector<MatrixParam> mMatrices;
+	std::vector<ArrayParam> mArrays;
 };
 
@@ -134,6 +151,25 @@
 	void SetTexture(int idx, unsigned int tex);
 
+	void SetValue1f(const std::string &name, float value);
+	void SetValue2f(const std::string &name, float val1, float val2);
+	void SetValue3f(const std::string &name, float val1, float val, float val3);
+
+	void SetArray1f(const std::string &name, float *vals, int numElements);
+	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);
+
 	CGparameter AddParameter(const std::string &name, int idx);
-
+	/** Binds the program.
+	*/
+	void Bind();
+	/** Returns true if this program is valid.
+	*/
+	inline bool IsValid() const { return mProgram != NULL; }
 	/** Enable / disable a texture parameter.
 	*/
@@ -141,17 +177,11 @@
 	//void DisableTexture(const std::string &name);
 
-	/** Binds the program.
-	*/
-	void Bind();
-	/** Returns true if this program is valid.
-	*/
-	inline bool IsValid() const { return mProgram != NULL; }
-
-	inline int GetParameter(const std::string &name);
 
 
 protected:
 
-	inline CGparameter GetParameter(int idx);
+	inline int GetParamIdxByName(const std::string &name) const;
+
+	inline CGparameter GetParameter(int idx) const;
 
 	CGparameter GetOrCreateParameter(const std::string &name);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h	(revision 2848)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h	(revision 3034)
@@ -68,4 +68,5 @@
 	Material *mMaterial;
 	SceneEntity *mParent;
+
 	/// pointer to the renderqueue bucket this entity belongs to
 	RenderQueueBucket *mRenderQueueBucket;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2965)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 3034)
@@ -110,5 +110,5 @@
 	/// size of the shadow map
 	int mSize;
-
+	/// the shadow view
 	Camera *mShadowCam;
 	/// the texture matrix
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3031)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3034)
@@ -14,4 +14,5 @@
 class ShaderProgram;
 class Texture;
+class Material;
 
 /** The current render state.
@@ -47,5 +48,5 @@
 	/** Sets the current render state.
 	*/
-	void SetState(Texture *tex, bool alphaTest, bool cullFace);
+	void SetState(Material *mat);
 	/** Returns either query or render mode
 	*/
@@ -60,7 +61,4 @@
 	*/
 	inline int GetCurrentVboId() const { return mCurrentVboId; }
-	/** If alpha to coverage is instead of alpha testing
-	*/
-	void SetUseAlphaToCoverage(bool useAlphaToCoverage);
 	/** Sets the type of rendering used (foreward, depth pass, deferred)
 	*/
@@ -69,13 +67,13 @@
 	*/
 	RenderPassType GetRenderPassType() const;
+	/** If alpha to coverage is instead of alpha testing
+	*/
+	void SetUseAlphaToCoverage(bool useAlphaToCoverage);
 	/** If true, the current value of cull face enabled / disable will
 		not be changed by the render state.
 	*/
 	void LockCullFaceEnabled(bool lockCull);
-	/** Set a new texture.
-	*/
-	void SetTexture(Texture *tex);
-
-
+	
+	
 	///////////////////
 	//-- for deferred shading, we have to switch shaders on and off
@@ -93,4 +91,6 @@
 protected:
 
+	//////////////////
+
 	bool mAlphaTestEnabled;
 	bool mCullFaceEnabled;
@@ -106,4 +106,9 @@
 
 	bool mLockCullFaceEnabled;
+
+	int mCurrentTexId;
+
+	ShaderProgram *mCurrentVertexProgram;
+	ShaderProgram *mCurrentFragmentProgram;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 2961)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneEntity.cpp	(revision 3034)
@@ -84,5 +84,5 @@
 
 
-void SceneEntity::GetLODLevel(int level, 
+void SceneEntity::GetLODLevel(int level,
 							  ShapeContainer::iterator &start, 
 							  ShapeContainer::iterator &end)
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3031)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3034)
@@ -4,4 +4,7 @@
 #include "glInterface.h"
 #include "RenderState.h"
+#include "ShaderProgram.h"
+
+using namespace std;
 
 
@@ -20,15 +23,27 @@
 	mAlphaTestEnabled = false;
 	mCullFaceEnabled = true;
-
-	mAmbientColor = RgbaColor(0.2f, 0.2f, 0.2f, 1.0f);
-	mDiffuseColor = RgbaColor(1.0f, 1.0f, 1.0f, 1.0f);
-	mSpecularColor = RgbaColor(.0f, .0f, .0f, 1.0f);
-	mEmmisiveColor = RgbaColor(.0f, .0f, .0f, 1.0f);
+	
+	mGPUVertexParameters = NULL;
+	mVertexProgram = NULL;
+	
+	mGPUFragmentParameters = NULL;
+	mFragmentProgram = NULL;
 }
 
 
-Material::Material()
+Material::Material():
+mAmbientColor(RgbaColor(0.2f, 0.2f, 0.2f, 1.0f)),
+mDiffuseColor(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)),
+mSpecularColor(RgbaColor(.0f, .0f, .0f, 1.0f)),
+mEmmisiveColor(RgbaColor(.0f, .0f, .0f, 1.0f))
 {
 	InitMaterial();
+}
+
+
+Material::~Material()
+{
+	DEL_PTR(mGPUFragmentParameters);
+	DEL_PTR(mGPUVertexParameters);
 }
 
@@ -40,4 +55,5 @@
 mTexture(NULL)
 {
+	InitMaterial();
 }
 
@@ -57,5 +73,5 @@
 void Material::Render(RenderState *state)
 {
-	state->SetState(mTexture, mAlphaTestEnabled, mCullFaceEnabled);
+	state->SetState(this);
 
 	glMaterialfv(GL_FRONT, GL_AMBIENT, (float *)&mAmbientColor.r);
@@ -63,4 +79,15 @@
 	glMaterialfv(GL_FRONT, GL_EMISSION, (float *)&mEmmisiveColor.r);
 	glMaterialfv(GL_FRONT, GL_SPECULAR, (float *)&mSpecularColor.r);
+	
+
+	if (mVertexProgram)
+	{
+		mGPUVertexParameters->UpdateParameters();
+	}
+	
+	if (mFragmentProgram)
+	{
+		mGPUFragmentParameters->UpdateParameters();
+	}
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.cpp	(revision 3031)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Transform3.cpp	(revision 3034)
@@ -25,16 +25,4 @@
 {
 	if (mIsIdentity) return;
-	
-	// note: would not need to pass the model matrix, 
-	// as all required information is avaiabl in the glstate
-	// however, we require it compute the word positions,
-	// as computing the world position by use the inverse of the viewprojection
-	// matrix turned out to be too inaccurate for temporal reprojection
-
-	if (state->GetRenderPassType() == RenderState::DEFERRED)
-	{
-		//cgGLSetMatrixParameterfc(sModelMatrixParam, (const float *)mMatrix.x);
-		RenderState::sCgMrtVertexProgram->SetMatrix("modelView", mMatrix);
-	}
 
 	glPushMatrix();
@@ -46,11 +34,4 @@
 {
 	if (mIsIdentity) return;
-
-	// reset model matrix
-	if (state->GetRenderPassType() == RenderState::DEFERRED)
-	{
-		static Matrix4x4 identity = IdentityMatrix();
-		RenderState::sCgMrtVertexProgram->SetMatrix("modelView", identity);
-	}
 
 	glPopMatrix();
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3031)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3034)
@@ -452,4 +452,7 @@
 	MiddleMotion(0, 0);
 
+	// init cg shader programs
+	InitCg();
+
 	perfGraph = new PerformanceGraph(1000);
 
@@ -458,5 +461,4 @@
 	const string filename = string(model_path + "city.dem");
 	
-
 	if (loader->Load(filename, sceneEntities))
 		cout << "model " << filename << " loaded" << endl;
@@ -486,6 +488,4 @@
 	camera->SetFar(Magnitude(bvh->GetBox().Diagonal()));
 	
-	InitCg();
-
 	Vector3 cubeCenter(470.398f, 240.364f, 182.5f);
 	
@@ -510,4 +510,5 @@
 	skyDome = sceneEntities.back();
 
+	//InitCg();
 
 	const float turbitiy = 5.0f;
@@ -537,11 +538,11 @@
 void InitCg(void) 
 {
-	// Setup Cg
+	// setup cg
 	cgSetErrorCallback(cgErrorCallback);
 
-	// Create cgContext.
+	// create context.
 	sCgContext = cgCreateContext();
 
-	// get the best profile for this hardware
+	// get the optimal profile
 	RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
 	cgGLSetOptimalOptions(RenderState::sCgFragmentProfile);
@@ -550,5 +551,6 @@
 	cgGLSetOptimalOptions(RenderState::sCgVertexProfile);
 
-	cgGLSetManageTextureParameters(sCgContext, true);
+	// set textures to auto load
+	cgGLSetManageTextureParameters(sCgContext, false);
 
 	RenderState::sCgMrtVertexProgram = 
@@ -561,6 +563,9 @@
 		new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "fragtex");
 
+	
 	if (!RenderState::sCgMrtFragmentTexProgram->IsValid())
 		cerr << "fragment tex program failed to load" << endl;
+
+	RenderState::sCgMrtFragmentTexProgram->AddParameter("tex", 0);
 
 	RenderState::sCgMrtFragmentProgram = 
@@ -851,9 +856,4 @@
 	// store matrix for later use
 	viewProjMat = matViewing * matProjection;
-	
-	if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
-	{
-		RenderState::sCgMrtVertexProgram->SetMatrix("modelView", IdentityMatrix());
-	}
 }
 
@@ -912,7 +912,4 @@
 
 	const Vector3 pos = camera->GetPosition();
-
-	RenderState::sCgMrtFragmentProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z);
-	RenderState::sCgMrtFragmentTexProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z);
 
 
@@ -972,5 +969,4 @@
 
 	}
-
 	
 	// bring eye modelview matrix up-to-date
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3028)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3034)
@@ -319,4 +319,45 @@
 	InitProgram(&sCgLogLumProgram, context, "tonemap", "CalcAvgLogLum");
 
+	int i;
+
+	i = 0;
+
+	sCgSsaoProgram->AddParameter("colors", i ++);
+	sCgSsaoProgram->AddParameter("colors", i ++);
+	sCgSsaoProgram->AddParameter("oldTex", i ++);
+	sCgSsaoProgram->AddParameter("noise", i ++);
+	sCgSsaoProgram->AddParameter("eyePos", i ++);
+	sCgSsaoProgram->AddParameter("temporalCoherence", i ++);
+	sCgSsaoProgram->AddParameter("modelViewProj", i ++);
+	sCgSsaoProgram->AddParameter("oldModelViewProj", i ++);
+	sCgSsaoProgram->AddParameter("samples", i ++);
+	sCgSsaoProgram->AddParameter("bl", i ++);
+	sCgSsaoProgram->AddParameter("br", i ++);
+	sCgSsaoProgram->AddParameter("tl", i ++);
+	sCgSsaoProgram->AddParameter("tr", i ++);
+
+	i = 0;
+
+	sCgGiProgram->AddParameter("colors", i ++);
+	sCgGiProgram->AddParameter("colors", i ++);
+	sCgGiProgram->AddParameter("noise", i ++);
+	sCgGiProgram->AddParameter("oldSsaoTex", i ++);
+	sCgGiProgram->AddParameter("oldIllumTex", i ++);
+	sCgGiProgram->AddParameter("eyePos", i ++);
+	sCgGiProgram->AddParameter("temporalCoherence", i ++);
+	sCgGiProgram->AddParameter("samples", i ++);
+	sCgGiProgram->AddParameter("bl", i ++);
+	sCgGiProgram->AddParameter("br", i ++);
+	sCgGiProgram->AddParameter("tl", i ++);
+	sCgGiProgram->AddParameter("tr", i ++);
+
+	sCgAntiAliasingProgram->AddParameter("colors", 0);
+	sCgAntiAliasingProgram->AddParameter("normals", 1);
+
+	sCgDeferredProgram->AddParameter("colors", 0);
+	sCgDeferredProgram->AddParameter("normals", 1);
+	sCgDeferredProgram->AddParameter("lightDir", 2);
+
+
 	float filterWeights[NUM_PCF_TABS];
 	PoissonDiscSampleGenerator2 poisson(NUM_PCF_TABS, 1.0f);
@@ -451,16 +492,15 @@
 	glDrawBuffers(1, mrt + mIllumFboIndex);
 
+	sCgSsaoProgram->SetTexture(0, colorsTex);
+	sCgSsaoProgram->SetTexture(1, normalsTex);
+	sCgSsaoProgram->SetTexture(2, oldTex);
+	sCgSsaoProgram->SetTexture(3, noiseTex);
+
 	const Vector3 pos = mCamera->GetPosition();
-	sCgSsaoProgram->SetValue3f("eyePos", pos.x, pos.y, pos.z);
-
-	sCgSsaoProgram->SetTexture("colors", colorsTex);
-	sCgSsaoProgram->SetTexture("normals", normalsTex);
-	sCgSsaoProgram->SetTexture("oldTex", oldTex);
-	sCgSsaoProgram->SetTexture("noise", noiseTex);
-
-	sCgSsaoProgram->SetValue1f("temporalCoherence", (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
-
-	sCgSsaoProgram->SetMatrix("modelViewProj", projViewMatrix);
-	sCgSsaoProgram->SetMatrix("oldModelViewProj", oldProjViewMatrix);
+	sCgSsaoProgram->SetValue3f(4, pos.x, pos.y, pos.z);
+
+	sCgSsaoProgram->SetValue1f(5, (mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
+	sCgSsaoProgram->SetMatrix(6, projViewMatrix);
+	sCgSsaoProgram->SetMatrix(7, oldProjViewMatrix);
 
 	if (mUseTemporalCoherence || mRegenerateSamples)
@@ -472,5 +512,5 @@
 		// needs longer to converge
 		GenerateSamples(mSamplingMethod); 
-		sCgSsaoProgram->SetArray2f("samples", (float *)samples2, NUM_SAMPLES);
+		sCgSsaoProgram->SetArray2f(8, (float *)samples2, NUM_SAMPLES);
 	}
 
@@ -478,8 +518,8 @@
 	ComputeViewVectors(tl, tr, bl, br);
 
-	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);
+	sCgSsaoProgram->SetValue3f(9, bl.x, bl.y, bl.z);
+	sCgSsaoProgram->SetValue3f(10, br.x, br.y, br.z);
+	sCgSsaoProgram->SetValue3f(11, tl.x, tl.y, tl.z);
+	sCgSsaoProgram->SetValue3f(12, tr.x, tr.y, tr.z);
 
 	DrawQuad(sCgSsaoProgram);
@@ -528,6 +568,6 @@
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
 
-	sCgAntiAliasingProgram->SetTexture("colors", colorsTex);
-	sCgAntiAliasingProgram->SetTexture("normals", normalsTex);
+	sCgAntiAliasingProgram->SetTexture(0, colorsTex);
+	sCgAntiAliasingProgram->SetTexture(1, normalsTex);
 
 	sCgAntiAliasingProgram->Bind();
@@ -565,7 +605,7 @@
 	const Vector3 lightDir = -light->GetDirection();
 
-	sCgDeferredProgram->SetTexture("colors", colorsTex);
-	sCgDeferredProgram->SetTexture("normals", normalsTex);
-	sCgDeferredProgram->SetValue3f("lightDir", lightDir.x, lightDir.y, lightDir.z);
+	sCgDeferredProgram->SetTexture(0, colorsTex);
+	sCgDeferredProgram->SetTexture(1, normalsTex);
+	sCgDeferredProgram->SetValue3f(2, lightDir.x, lightDir.y, lightDir.z);
 	
 	DrawQuad(sCgDeferredProgram);
@@ -603,11 +643,15 @@
 	
 
-	sCgGiProgram->SetTexture("colors", colorsTex);
-	sCgGiProgram->SetTexture("normals", normalsTex);
-	sCgGiProgram->SetTexture("noise", noiseTex);
-	sCgGiProgram->SetTexture("oldSsaoTex", oldSsaoTex);
-	sCgGiProgram->SetTexture("oldIllumTex", oldIllumTex);
-
-	sCgGiProgram->SetValue1f("temporalCoherence", 
+	sCgGiProgram->SetTexture(0, colorsTex);
+	sCgGiProgram->SetTexture(1, normalsTex);
+	sCgGiProgram->SetTexture(2, noiseTex);
+	sCgGiProgram->SetTexture(3, oldSsaoTex);
+	sCgGiProgram->SetTexture(4, oldIllumTex);
+
+	const Vector3 pos = mCamera->GetPosition();
+	sCgGiProgram->SetValue3f(5, pos.x, pos.y, pos.z);
+
+
+	sCgGiProgram->SetValue1f(6, 
 		(mUseTemporalCoherence && !mRegenerateSamples) ? tempCohFactor : 0);
 
@@ -621,5 +665,5 @@
 		GenerateSamples(mSamplingMethod); 
 
-		sCgGiProgram->SetArray2f("samples", (float *)samples2, NUM_SAMPLES);
+		sCgGiProgram->SetArray2f(7, (float *)samples2, NUM_SAMPLES);
 	}
 
@@ -628,11 +672,8 @@
 	ComputeViewVectors(tl, tr, bl, br);
 	
-	const Vector3 pos = mCamera->GetPosition();
-	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);
+	sCgGiProgram->SetValue3f(8, bl.x, bl.y, bl.z);
+	sCgGiProgram->SetValue3f(9, br.x, br.y, br.z);
+	sCgGiProgram->SetValue3f(10, tl.x, tl.y, tl.z);
+	sCgGiProgram->SetValue3f(11, tr.x, tr.y, tr.z);
 
 	DrawQuad(sCgGiProgram);
