Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln	(revision 3037)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln	(revision 3038)
@@ -7,9 +7,4 @@
 EndProject
 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MainApp", "MainApp.vcproj", "{A564071F-D054-4556-883B-999D7F9CC58C}"
-	ProjectSection(ProjectDependencies) = postProject
-		{03661866-4093-4B02-B26A-028EA91AF023} = {03661866-4093-4B02-B26A-028EA91AF023}
-	EndProjectSection
-EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "IrradianceMapping", "IrradianceMapping.vcproj", "{91680C49-A358-48AE-A02C-66CB884B7D7F}"
 	ProjectSection(ProjectDependencies) = postProject
 		{03661866-4093-4B02-B26A-028EA91AF023} = {03661866-4093-4B02-B26A-028EA91AF023}
@@ -34,8 +29,4 @@
 		{A564071F-D054-4556-883B-999D7F9CC58C}.Release|Win32.ActiveCfg = Release|Win32
 		{A564071F-D054-4556-883B-999D7F9CC58C}.Release|Win32.Build.0 = Release|Win32
-		{91680C49-A358-48AE-A02C-66CB884B7D7F}.Debug|Win32.ActiveCfg = Debug|Win32
-		{91680C49-A358-48AE-A02C-66CB884B7D7F}.Debug|Win32.Build.0 = Debug|Win32
-		{91680C49-A358-48AE-A02C-66CB884B7D7F}.Release|Win32.ActiveCfg = Release|Win32
-		{91680C49-A358-48AE-A02C-66CB884B7D7F}.Release|Win32.Build.0 = Release|Win32
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp	(revision 3031)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp	(revision 3038)
@@ -248,6 +248,7 @@
 	// render pending objects before changing to query mode
 	if (mUseRenderQueue && (mRenderState->GetMode() == RenderState::RENDER))
+	{
 		ApplyRenderQueue();
-	
+	}
 
 	query.BeginQuery();
@@ -255,5 +256,7 @@
 	// change to query mode and render box
 	if (mRenderState->SetMode(RenderState::QUERY))
+	{
 		++ mStats.mNumStateChanges;
+	}
 
 	mBvh->RenderBounds(query.GetNodes(), mRenderState, mUseTightBounds);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/treeanimation.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/treeanimation.cg	(revision 3038)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/treeanimation.cg	(revision 3038)
@@ -0,0 +1,122 @@
+//--------------------------------------------------------------------------------------
+// Input and Output structs
+//--------------------------------------------------------------------------------------
+
+
+
+struct VertexInput
+{
+	float4 position: POSITION;
+	float3 normal: NORMAL;
+	float3 color: COLOR;
+	float2 texcoord: TEXCOORD0;
+};
+
+
+// vtx output
+struct vtxout
+{
+	float4 position: POSITION; // eye space
+	float4 texCoord: TEXCOORD0;    
+
+	float4 color: COLOR0;  
+	float4 worldPos: TEXCOORD1; // world position
+	float3 normal: TEXCOORD2;
+	float4 mypos: TEXCOORD3;
+	float4 color2: TEXCOORD4;
+};
+
+
+struct pixel
+{
+	float4 col: COLOR0;
+	float4 norm: COLOR1;
+	float3 pos: COLOR2;
+};
+
+
+// fragment input
+struct fragin
+{
+	float4 color: COLOR0;  
+	float4 position: POSITION; // eye space
+	float4 texCoord: TEXCOORD0;    
+
+	float4 projPos: WPOS;
+	float4 worldPos: TEXCOORD1; // world position
+	float3 normal: TEXCOORD2;
+	float4 mypos: TEXCOORD3;
+	float4 color2: TEXCOORD4;
+};
+//--------------------------------------------------------------------------------------
+// Vertex Shaders
+//--------------------------------------------------------------------------------------
+
+
+vtxout default_vs(VertexInput IN,
+				  uniform float3 lightDir,
+				  uniform float2 thetaSun,
+				  uniform float3 zenithColor,
+				  uniform float3 aColor,
+				  uniform float3 bColor,
+				  uniform float3 cColor,
+				  uniform float3 dColor,
+				  uniform float3 eColor,
+				  uniform float4x4 ModelView,
+				  uniform float multiplier)
+{
+	vtxout OUT;
+
+	OUT.position = mul(glstate.matrix.mvp, IN.position);
+
+
+	const float dotLN = dot(lightDir, IN.normal);   
+	const float cos2gamma = dotLN * dotLN; 
+
+	const float gamma = acos(dotLN);    
+	const float theta = dot(float3(0.0, 0.0, 1.0), IN.normal);
+
+    float3 num = (1.0 + aColor * exp(bColor / theta)) * (1.0 + cColor * exp(dColor * gamma) + eColor * cos2gamma);    
+	float3 den = (1.0 + aColor * exp(bColor)) * (1.0 + cColor * exp(dColor * thetaSun.x) + eColor * thetaSun.y);   
+    float3 xyY = (num / den) * zenithColor;    
+    
+    float3 XYZ;                                                                                             
+                                                                                                            
+    XYZ.x = (xyY.x / xyY.y) * xyY.z;                                                                        
+	XYZ.y = xyY.z;                                                                                          
+	XYZ.z = ((1.0f - xyY.x - xyY.y) / xyY.y) * xyY.z;   
+	
+    const static float3x3 conv_Mat = 
+		float3x3(3.240479, -1.537150, -0.498535,
+		         -0.969256, 1.875992,  0.041556,
+			     0.055648, -0.204043,  1.057311);
+
+	float3 hcol = mul(conv_Mat, XYZ);
+	OUT.color = float4(hcol, 1.0);
+
+	OUT.color.rgb *= multiplier;
+
+	OUT.color2 = OUT.color;
+	OUT.worldPos = mul(ModelView, IN.position);
+
+	OUT.normal = IN.normal;
+	OUT.mypos = OUT.position;
+
+	return OUT;
+}
+
+
+pixel frag_skydome(fragin IN)
+{
+	pixel pix;
+
+	pix.col = IN.color2;
+	pix.col.w = 1e20f;
+
+	pix.norm.xyz = IN.normal;
+	pix.norm.w = IN.mypos.w;
+
+	//pix.pos = 1e20f;
+	
+	return pix;
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3036)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.cpp	(revision 3038)
@@ -4,20 +4,12 @@
 #include "ShaderProgram.h"
 #include "Texture.h"
+#include "ResourceManager.h"
+
 
 using namespace std;
 
+
 namespace CHCDemoEngine 
 {
-
-CGprofile RenderState::sCgFragmentProfile;
-CGprofile RenderState::sCgVertexProfile; 
-
-ShaderProgram *RenderState::sCgMrtFragmentProgram = NULL;
-ShaderProgram *RenderState::sCgMrtFragmentTexProgram = NULL;
-ShaderProgram *RenderState::sCgMrtVertexProgram = NULL;
-
-
-int RenderState::sTexParam;
-
 
 
@@ -29,8 +21,53 @@
 mLockCullFaceEnabled(false)
 {
-	Reset(); 
-}
-
-	
+	SetMode(RENDER);
+
+	mCurrentVboId = -1;
+	mCurrentTexId = 0;
+
+	mCullFaceEnabled = true;
+	mAlphaTestEnabled = false;
+	mTexturesEnabled = false;
+
+	mCurrentVertexProgram = NULL;
+	mCurrentFragmentProgram = NULL;
+
+	glEnable(GL_CULL_FACE);
+
+	glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
+	glDisable(GL_ALPHA_TEST);
+
+	glDisable(GL_TEXTURE_2D);
+	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+}
+
+
+void RenderState::Reset()
+{
+	SetMode(RENDER);
+
+	mCurrentVboId = -1;
+	mCurrentTexId = 0;
+
+	mCullFaceEnabled = true;
+	mAlphaTestEnabled = false;
+	mTexturesEnabled = false;
+
+	mCurrentVertexProgram = NULL;
+	mCurrentFragmentProgram = NULL;
+
+	glEnable(GL_CULL_FACE);
+
+	glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
+	glDisable(GL_ALPHA_TEST);
+
+	glDisable(GL_TEXTURE_2D);
+	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+
+	//ResourceManager::GetSingleton()->DisableFragmentProfile();
+	//ResourceManager::GetSingleton()->DisableVertexProfile();
+}
+
+
 bool RenderState::SetMode(Mode mode)
 {
@@ -51,6 +88,6 @@
 			if (mRenderType == DEFERRED)
 			{
-				cgGLDisableProfile(sCgFragmentProfile);
-				cgGLDisableProfile(sCgVertexProfile);
+				ResourceManager::GetSingleton()->DisableFragmentProfile();
+				ResourceManager::GetSingleton()->DisableFragmentProfile();
 			}
 		}
@@ -73,6 +110,6 @@
 			if (mRenderType == DEFERRED)
 			{
-				cgGLEnableProfile(sCgFragmentProfile);
-				cgGLEnableProfile(sCgVertexProfile);
+				ResourceManager::GetSingleton()->EnableFragmentProfile();
+				ResourceManager::GetSingleton()->EnableVertexProfile();
 			}
 		}
@@ -152,14 +189,25 @@
 	if (mRenderType == DEFERRED)
 	{
-		if (mat->GetFragmentProgram() && (mat->GetFragmentProgram() != mCurrentFragmentProgram))
-		{
-			mCurrentFragmentProgram = mat->GetFragmentProgram();
-			mCurrentFragmentProgram->Bind();
+		if (mat->GetFragmentProgram())
+		{
+			if (mat->GetFragmentProgram() != mCurrentFragmentProgram)
+			{			
+				mCurrentFragmentProgram = mat->GetFragmentProgram();
+				mCurrentFragmentProgram->Bind();
+			}
+			
+			mat->GetFragmentProgramParameters()->UpdateParameters();
+
 		}
 	
-		if (mat->GetVertexProgram() && (mat->GetVertexProgram() != mCurrentVertexProgram))
-		{
-			mCurrentVertexProgram = mat->GetVertexProgram();
-			mCurrentVertexProgram->Bind();
+		if (mat->GetVertexProgram())
+		{
+			if  (mat->GetVertexProgram() != mCurrentVertexProgram)
+			{
+				mCurrentVertexProgram = mat->GetVertexProgram();
+				mCurrentVertexProgram->Bind();
+			}
+
+			mat->GetVertexProgramParameters()->UpdateParameters();
 		}
 	}
@@ -181,28 +229,4 @@
 		}
 	}
-}
-
-
-void RenderState::Reset()
-{
-	SetMode(RENDER);
-
-	mCurrentVboId = -1;
-	mCurrentTexId = 0;
-
-	mCullFaceEnabled = true;
-	mAlphaTestEnabled = false;
-	mTexturesEnabled = false;
-
-	mCurrentVertexProgram = NULL;
-	mCurrentFragmentProgram = NULL;
-
-	glEnable(GL_CULL_FACE);
-
-	glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);
-	glDisable(GL_ALPHA_TEST);
-
-	glDisable(GL_TEXTURE_2D);
-	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3037)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3038)
@@ -13,4 +13,6 @@
 #include "RenderState.h"
 #include "ShaderProgram.h"
+#include <Cg/cg.h>
+#include <Cg/cgGL.h>
 
 
@@ -32,6 +34,39 @@
 {
 
+
+/////////
+//-- cg stuff
+
+static CGcontext sCgContext = NULL;
+
+static CGprofile sCgFragmentProfile;
+static CGprofile sCgVertexProfile; 
+
+
 // only instance of the resource manager
 ResourceManager *ResourceManager::sResourceManager = NULL;
+
+
+static void cgErrorCallback()
+{
+	CGerror lastError = cgGetError();
+
+	if(lastError)
+	{
+		printf("%s\n\n", cgGetErrorString(lastError));
+		printf("%s\n", cgGetLastListing(sCgContext));
+		
+		printf("Cg error, exiting...\n");
+
+		exit(0);
+	}
+}
+
+
+ResourceManager::ResourceManager()
+{ 
+	InitCg(); 
+}
+
 
 ResourceManager::~ResourceManager()
@@ -47,4 +82,7 @@
 	CLEAR_CONTAINER(mGPUParameters);
 	CLEAR_CONTAINER(mShaders);
+	
+	if (sCgContext)
+		cgDestroyContext(sCgContext);
 }
 
@@ -188,5 +226,4 @@
 	
 	str.read(reinterpret_cast<char *>(&mat->mAlphaTestEnabled), sizeof(bool));
-
 	str.read(reinterpret_cast<char *>(&mat->mCullFaceEnabled), sizeof(bool));
 
@@ -204,9 +241,7 @@
 	}
 
-	mat->mVertexProgram = RenderState::sCgMrtVertexProgram;
-
 	if (mat->GetTexture())
 	{
-		mat->mFragmentProgram = RenderState::sCgMrtFragmentTexProgram;
+		mat->mFragmentProgram = mMrtDefaultFragmentTexProgram;
 
 		mat->mGPUFragmentParameters = CreateGPUProgramParameters(mat->mFragmentProgram);
@@ -215,9 +250,10 @@
 	else
 	{
-		mat->mFragmentProgram = RenderState::sCgMrtFragmentProgram;
+		mat->mFragmentProgram = mMrtDefaultFragmentProgram;
 		mat->mGPUFragmentParameters = CreateGPUProgramParameters(mat->mFragmentProgram);
 	}
 
-	mat->mGPUVertexParameters = CreateGPUProgramParameters(RenderState::sCgMrtVertexProgram);
+	mat->mVertexProgram = mMrtDefaultVertexProgram;
+	mat->mGPUVertexParameters = CreateGPUProgramParameters(mat->mVertexProgram);
 
 	return mat;
@@ -328,10 +364,93 @@
 
 
-ShaderProgram *CreateShaderProgram(const std::string &filename, const std::string &funcName, bool isFragment)
-{
-	ShaderProgram *p;
+ShaderProgram *ResourceManager::CreateFragmentProgram(const std::string &filename, const std::string &funcName)
+{
+	const string fullName = "src/shaders/" + filename + ".cg";
+
+	ShaderProgram *p = new ShaderProgram(sCgContext, fullName, sCgFragmentProfile, funcName);
+
+	mShaders.push_back(p);
+
+	if (!p->IsValid())
+	{
+		DEL_PTR(p);
+		cerr << "Program " << funcName << " in " << fullName << " failed to load" << endl;
+	}
+	
+	cout << "Program " << funcName << " in " << fullName << " loaded" << endl;
+
 	return p;
 }
 
-}
-
+
+ShaderProgram *ResourceManager::CreateVertexProgram(const std::string &filename, const std::string &funcName)
+{
+	const string fullName = "src/shaders/" + filename + ".cg";
+	
+	ShaderProgram *p = new ShaderProgram(sCgContext, fullName, sCgVertexProfile, funcName);
+	mShaders.push_back(p);
+
+	if (!p->IsValid())
+	{
+		DEL_PTR(p);
+		cerr << "Program " << funcName << " in " << fullName << " failed to load" << endl;
+	}
+	
+	cout << "Program " << funcName << " in " << fullName << " loaded" << endl;
+
+	return p;
+}
+
+
+void ResourceManager::InitCg() 
+{
+	// setup cg
+	cgSetErrorCallback(cgErrorCallback);
+	// create context.
+	sCgContext = cgCreateContext();
+	// get the optimal fragment profile
+	sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
+	cgGLSetOptimalOptions(sCgFragmentProfile);
+	// get the optimal vertex profile
+	sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
+	cgGLSetOptimalOptions(sCgVertexProfile);
+
+	// set textures to auto load
+	cgGLSetManageTextureParameters(sCgContext, false);
+
+	mMrtDefaultVertexProgram = CreateVertexProgram("mrt", "vtx");
+	mMrtDefaultFragmentProgram = CreateFragmentProgram("mrt", "fragtex");//CreateFragmentProgram("mrt", "frag");
+	mMrtDefaultFragmentTexProgram = CreateFragmentProgram("mrt", "fragtex");
+	mMrtDefaultFragmentTexProgram->AddParameter("tex", 0);
+
+	cout << "cg initialization successful" << endl;
+}
+
+
+void ResourceManager::EnableFragmentProfile()
+{
+	cgGLEnableProfile(sCgFragmentProfile);
+}
+
+
+void ResourceManager::EnableVertexProfile()
+{
+	cgGLEnableProfile(sCgVertexProfile);
+}
+
+
+void ResourceManager::DisableFragmentProfile()
+{
+	cgGLDisableProfile(sCgFragmentProfile);
+}
+
+
+void ResourceManager::DisableVertexProfile()
+{
+	cgGLDisableProfile(sCgVertexProfile);
+	
+}
+
+
+}
+
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 3036)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 3038)
@@ -9,4 +9,6 @@
 #include "Shape.h"
 #include "Material.h"
+#include "ResourceManager.h"
+
 
 
@@ -22,13 +24,10 @@
 
 
+
+
 using namespace CHCDemoEngine;
 using namespace std;
 
 
-static ShaderProgram *sCgSkyProgram = NULL;
-static ShaderProgram *sCgMrtFragmentSkyDomeProgram = NULL;
-
-ShaderContainer SkyPreetham::sShaders;
-
 
 inline float CBQ(float x)
@@ -44,45 +43,26 @@
 
 
-void SkyPreetham::InitCG(CGcontext context)
+void SkyPreetham::InitCG()
 {	
-	sCgSkyProgram = new ShaderProgram(context, 
-		                              "src/shaders/sky_preetham.cg",
-									  RenderState::sCgVertexProfile,	
-									  "default_vs");
-
-	if (sCgSkyProgram->IsValid())
+	mSkyVtxProgram = 
+		ResourceManager::GetSingleton()->CreateVertexProgram("sky_preetham", "default_vs");
+		
+	if (mSkyVtxProgram->IsValid())
 	{
-		sCgSkyProgram->AddParameter("lightDir", 0);
-		sCgSkyProgram->AddParameter("thetaSun", 1);
-		sCgSkyProgram->AddParameter("zenithColor", 2);
-		sCgSkyProgram->AddParameter("aColor", 3);
-		sCgSkyProgram->AddParameter("bColor", 4);
-		sCgSkyProgram->AddParameter("cColor", 5);
-		sCgSkyProgram->AddParameter("dColor", 6);
-		sCgSkyProgram->AddParameter("eColor", 7);
-		sCgSkyProgram->AddParameter("multiplier", 8);
+		mSkyVtxProgram->AddParameter("lightDir", 0);
+		mSkyVtxProgram->AddParameter("thetaSun", 1);
+		mSkyVtxProgram->AddParameter("zenithColor", 2);
+		mSkyVtxProgram->AddParameter("aColor", 3);
+		mSkyVtxProgram->AddParameter("bColor", 4);
+		mSkyVtxProgram->AddParameter("cColor", 5);
+		mSkyVtxProgram->AddParameter("dColor", 6);
+		mSkyVtxProgram->AddParameter("eColor", 7);
+		mSkyVtxProgram->AddParameter("multiplier", 8);
 	}
 	else
 		cerr << "sky program failed to load" << endl;
 
-		sShaders.push_back(sCgSkyProgram);
-
-	sCgMrtFragmentSkyDomeProgram = 
-		new ShaderProgram(context, 
-		                  "src/shaders/sky_preetham.cg", 
-						  RenderState::sCgFragmentProfile,
-						  "frag_skydome");
-
-	// simple shader used for just pushing out the fragments to the mrts
-	if (!sCgMrtFragmentSkyDomeProgram->IsValid())
-		cerr << "fragment skyprogram failed to load" << endl;
-	
-	sShaders.push_back(sCgMrtFragmentSkyDomeProgram);
-}
-
-
-void SkyPreetham::ReleaseCG()
-{
-	CLEAR_CONTAINER(sShaders);
+	mSkyFragProgram = 
+		ResourceManager::GetSingleton()->CreateFragmentProgram("sky_preetham", "frag_skydome");
 }
 
@@ -96,8 +76,10 @@
 	Shape *shape = mSkyDome->GetShape(0);
 
+	InitCG();
+
 	Material *mat = shape->GetMaterial();
 
-	mat->SetFragmentProgram(sCgMrtFragmentSkyDomeProgram);
-	mat->SetVertexProgram(sCgSkyProgram);
+	mat->SetFragmentProgram(mSkyFragProgram);
+	mat->SetVertexProgram(mSkyVtxProgram);
 }
 
@@ -148,13 +130,11 @@
 	{	
 		// use tone mapping
-		sCgSkyProgram->SetValue1f(8, 1.0f);
+		vtxParams->SetValue1f(8, 1.0f);
 	}
 	else
 	{
 		// no tone mapping => scale
-		sCgSkyProgram->SetValue1f(8, 8e-5f);
+		vtxParams->SetValue1f(8, 8e-5f);
 	}
-
-	cgGLEnableProfile(RenderState::sCgVertexProfile);
 
 	// Render sky dome.
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h	(revision 3036)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h	(revision 3038)
@@ -26,10 +26,7 @@
 typedef std::vector<CGparameter> CGParameterArray;
 
-struct ParameterPair1f
-{
-	unsigned int mParameterIdx;
-	float mValue;
-};
-
+
+/** This class represents the parameter values for a given GPU program.
+*/
 class GPUProgramParameters
 {
@@ -85,5 +82,6 @@
 	*/
 	void UpdateParameters();
-	
+
+
 
 protected:
@@ -151,5 +149,6 @@
 
 
-
+/** This class represents a wrapper for a GPU shader program (vertex or fragment).
+*/
 class ShaderProgram
 {
@@ -167,4 +166,12 @@
 	~ShaderProgram() { if (mProgram != NULL) cgDestroyProgram(mProgram); }
 
+	/** Assigns a parameter to the specified index.
+	*/
+	CGparameter AddParameter(const std::string &name, int idx);
+
+
+	//////////////
+	//-- set shader parameters per index 
+
 	void SetValue1f(int idx, float value);
 	void SetValue2f(int idx, float val1, float val2);
@@ -178,13 +185,17 @@
 	void SetTexture(int idx, unsigned int tex);
 
+
+	////////////
+	//-- set shader parameters per parameter name (slow!)
+
+	/// Float parameters
 	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);
-
+	/// Array parameters
 	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.
-	*/
+	/// Sets a matrix parameter
 	void SetMatrix(const std::string &name, const Matrix4x4 &mat);
 	/** Sets the texture parameter.
@@ -192,5 +203,4 @@
 	void SetTexture(const std::string &name, unsigned int tex);
 
-	CGparameter AddParameter(const std::string &name, int idx);
 	/** Binds the program.
 	*/
@@ -226,5 +236,4 @@
 
 
-typedef  std::vector<ShaderProgram *> ShaderContainer;
 
 } // namespace 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3027)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3038)
@@ -22,5 +22,4 @@
 
 
-typedef  std::vector<FrameBufferObject *> FBOContainer;
 
 /** This class implements a deferred shading algorithm that takes
@@ -64,14 +63,9 @@
 
 	void SetShadingMethod(SHADING_METHOD s);
-	
-	/** Initialises the deferred shader and loads the required shaders:
-		This function has to be called only once.
-	*/
-	static void InitCG(CGcontext context);
-	/** Releases all shader resources.
-	*/
-	static void ReleaseCG();
 
+	// hack: store the color buffer idx for the first flipflip-mrt here
+	// TODO matt: make this less hacky
 	static int colorBufferIdx;
+
 
 protected:
@@ -92,5 +86,9 @@
 	void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
 
-	void ComputeToneParameters(FrameBufferObject *fbo, DirectionalLight *light, float &imageKey, float &whiteLum, float &middleGrey);
+	void ComputeToneParameters(FrameBufferObject *fbo, 
+							   DirectionalLight *light,
+							   float &imageKey, 
+							   float &whiteLum,
+							   float &middleGrey);
 
 	void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey);
@@ -113,8 +111,8 @@
 	void DrawQuad(ShaderProgram *p);
 
-	static bool InitProgram(ShaderProgram **program, 
-		                    CGcontext context, 
-							const std::string &filename,
-							const std::string &functionName);
+	/** Initialises the deferred shader and loads the required shaders:
+		This function has to be called only once.
+	*/
+	void InitCg();
 
 
@@ -145,4 +143,5 @@
 };
 
+
 } // namespace 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3034)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderState.h	(revision 3038)
@@ -4,7 +4,4 @@
 
 #include "common.h"
-#include "glInterface.h"
-#include <Cg/cg.h>
-#include <Cg/cgGL.h>
 
 
@@ -75,17 +72,4 @@
 	void LockCullFaceEnabled(bool lockCull);
 	
-	
-	///////////////////
-	//-- for deferred shading, we have to switch shaders on and off
-
-	static CGprofile sCgFragmentProfile;
-	static CGprofile sCgVertexProfile; 
-
-	static ShaderProgram *sCgMrtFragmentProgram;
-	static ShaderProgram *sCgMrtFragmentTexProgram;
-	static ShaderProgram *sCgMrtVertexProgram;
-
-	static int sTexParam;
-
 
 protected:
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h	(revision 3037)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h	(revision 3038)
@@ -63,5 +63,13 @@
 	/** Creates a new shader program.
 	*/
-	ShaderProgram *CreateShaderProgram(const std::string &filename, const std::string &funcName, bool isFragment);
+	ShaderProgram *CreateFragmentProgram(const std::string &filename, const std::string &funcName);
+	ShaderProgram *CreateVertexProgram(const std::string &filename, const std::string &funcName);
+
+	void EnableFragmentProfile();
+	void EnableVertexProfile();
+
+	void DisableFragmentProfile();
+	void DisableVertexProfile();
+
 
 protected:
@@ -71,7 +79,10 @@
 		singleton pattern.
 	*/
-	ResourceManager() {}
+	ResourceManager();
 
 	~ResourceManager();
+
+
+	void InitCg();
 
 	void LoadTextures(igzstream &str);
@@ -88,5 +99,8 @@
 	//std::map<int, Shape *> mShapesTable;
 
-	// these are kept to be able to delete these resources afterwards
+
+	/////////////
+	//-- these are kept to be able to delete these resources afterwards
+
 	TextureContainer mTextures;
 	MaterialContainer mMaterials;
@@ -94,7 +108,6 @@
 	TransformContainer mTrafos;
 	GPUParamContainer mGPUParameters;
-
+	ShaderContainer mShaders;
 	ShapeContainer mShapes;
-
 	LODContainer mLODs;
 
@@ -102,5 +115,15 @@
 
 	/// the scene entities
+
 	SceneEntityContainer mSceneEntities;
+
+
+	////////////
+	//-- default shader programs
+
+	ShaderProgram *mMrtDefaultVertexProgram;
+	ShaderProgram *mMrtDefaultFragmentProgram;
+	ShaderProgram *mMrtDefaultFragmentTexProgram;
+
 
 private:
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3036)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Material.cpp	(revision 3038)
@@ -79,15 +79,4 @@
 	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/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 3021)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 3038)
@@ -6,4 +6,5 @@
 #include "Polygon3.h"
 #include "Polyhedron.h"
+#include "ResourceManager.h"
 
 #include <IL/il.h>
@@ -16,8 +17,4 @@
 namespace CHCDemoEngine
 {
-
-static CGprogram sCgShadowProgram;
-static CGparameter sShadowParam;
-
 
 static Polyhedron *polyhedron = NULL;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 3021)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 3038)
@@ -15,4 +15,5 @@
 	class RenderState;
 	class Camera;
+	class ShaderProgram;
 }
 
@@ -40,6 +41,5 @@
 						 bool scaleToRange) const;
 
-	static void InitCG(CGcontext context);
-	static void ReleaseCG();
+	void InitCG();
 
 
@@ -50,16 +50,20 @@
 
 
-	/// Members.
-/*	FEffect*		mEffect;
+	//////////
+
+	/*	FEffect*		mEffect;
 
 	FEffect*		mSunEffect;
 	FTexture2D*		mSunTexture;
 	FVertexBuffer*	mSunQuad;
-*/
+	*/
+
 	CHCDemoEngine::SceneEntity *mSkyDome;
 
+	/// the turbidity of the sky
 	float mTurbidity;
 
-	static CHCDemoEngine::ShaderContainer sShaders;
+	CHCDemoEngine::ShaderProgram *mSkyVtxProgram;
+	CHCDemoEngine::ShaderProgram *mSkyFragProgram;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3037)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3038)
@@ -18,7 +18,4 @@
 #include <time.h>
 #include "glInterface.h"
-
-#include <Cg/cg.h>
-#include <Cg/cgGL.h>
 
 
@@ -66,12 +63,10 @@
 #define MAX_DEPTH_CONST 10.0f
 
-// fbo
+
+GLuint fontTex;
+/// fbo
 FrameBufferObject *fbo = NULL;
-
-GLuint fontTex;
-
 /// the renderable scene geometry
 SceneEntityContainer sceneEntities;
-
 // traverses and renders the hierarchy
 RenderTraverser *traverser = NULL;
@@ -90,6 +85,7 @@
 /// the rendering algorithm
 int renderMode = RenderTraverser::CHCPLUSPLUS;
-// eye near plane distance
+/// eye near plane distance
 float nearDist = 0.2f;
+/// eye far plane distance
 float farDist = 1e6f;
 /// the field of view
@@ -99,4 +95,5 @@
 
 int assumedVisibleFrames = 10;
+
 int maxBatchSize = 50;
 
@@ -233,5 +230,4 @@
 void InitGLstate();
 void InitRenderTexture();
-void InitCg();
 void CleanUp();
 void SetupEyeView();
@@ -275,33 +271,7 @@
 void RenderShadowMap(float newfar);
 
-
-
-/////////
-//-- cg stuff
-
-static CGcontext sCgContext = NULL;
-
-static CGparameter sEyePosParamTex;
-static CGparameter sEyePosParam;
-
 static Matrix4x4 viewProjMat = IdentityMatrix();
 static Matrix4x4 oldViewProjMat = IdentityMatrix();
 
-
-
-static void cgErrorCallback()
-{
-	CGerror lastError = cgGetError();
-
-	if(lastError)
-	{
-		printf("%s\n\n", cgGetErrorString(lastError));
-		printf("%s\n", cgGetLastListing(sCgContext));
-		
-		printf("Cg error, exiting...\n");
-
-		exit(0);
-	}
-}
 
 
@@ -452,7 +422,4 @@
 	MiddleMotion(0, 0);
 
-	// init cg shader programs
-	InitCg();
-
 	perfGraph = new PerformanceGraph(1000);
 
@@ -532,53 +499,4 @@
 	
 	return 0;
-}
-
-
-void InitCg(void) 
-{
-	// setup cg
-	cgSetErrorCallback(cgErrorCallback);
-
-	// create context.
-	sCgContext = cgCreateContext();
-
-	// get the optimal profile
-	RenderState::sCgFragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);
-	cgGLSetOptimalOptions(RenderState::sCgFragmentProfile);
-
-	RenderState::sCgVertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);
-	cgGLSetOptimalOptions(RenderState::sCgVertexProfile);
-
-	// set textures to auto load
-	cgGLSetManageTextureParameters(sCgContext, false);
-
-	RenderState::sCgMrtVertexProgram = 
-		new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgVertexProfile, "vtx");
-
-	if (!RenderState::sCgMrtVertexProgram->IsValid())
-		cerr << "fragment program failed to load" << endl;
-
-	RenderState::sCgMrtFragmentTexProgram = 
-		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 = 
-		new ShaderProgram(sCgContext, "src/shaders/mrt.cg", RenderState::sCgFragmentProfile, "frag");
-
-	if (!RenderState::sCgMrtFragmentProgram->IsValid())
-		cerr << "fragment program failed to load" << endl;
-	
-
-	PrintGLerror("init");
-
-	DeferredRenderer::InitCG(sCgContext);
-	SkyPreetham::InitCG(sCgContext);
-
-	cout << "cg initialization successful" << endl;
 }
 
@@ -904,9 +822,6 @@
 	state.SetRenderPassType(RenderState::DEFERRED);
 
-	cgGLEnableProfile(RenderState::sCgVertexProfile);
-	RenderState::sCgMrtVertexProgram->Bind();
-
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	RenderState::sCgMrtFragmentProgram->Bind();
+	loader->EnableVertexProfile();
+	loader->EnableFragmentProfile();
 
 	const Vector3 pos = camera->GetPosition();
@@ -991,6 +906,6 @@
 		glEnable(GL_LIGHTING);
 
-		cgGLDisableProfile(RenderState::sCgFragmentProfile);
-		cgGLDisableProfile(RenderState::sCgVertexProfile);
+		loader->DisableFragmentProfile();
+		loader->DisableVertexProfile();
 
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -1011,6 +926,6 @@
 		glDrawBuffers(1, mrt);
 
-		cgGLDisableProfile(RenderState::sCgFragmentProfile);
-		cgGLDisableProfile(RenderState::sCgVertexProfile);
+		loader->DisableFragmentProfile();
+		loader->DisableVertexProfile();
 
 		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -1027,7 +942,7 @@
 
 		glEnable(GL_MULTISAMPLE_ARB);
-
-		cgGLDisableProfile(RenderState::sCgFragmentProfile);
-		cgGLDisableProfile(RenderState::sCgVertexProfile);
+		
+		loader->DisableFragmentProfile();
+		loader->DisableVertexProfile();
 
 		state.SetRenderPassType(RenderState::DEPTH_PASS);
@@ -1112,6 +1027,6 @@
 		FrameBufferObject::Release();
 
-		cgGLDisableProfile(RenderState::sCgVertexProfile);
-		cgGLDisableProfile(RenderState::sCgFragmentProfile);
+		loader->DisableVertexProfile();
+		loader->DisableFragmentProfile();
 
 		if (!ssaoShader) ssaoShader = 
@@ -1741,16 +1656,7 @@
 	DEL_PTR(shadowMap);
 	DEL_PTR(shadowTraverser);
-	DEL_PTR(RenderState::sCgMrtFragmentProgram);
-	DEL_PTR(RenderState::sCgMrtFragmentTexProgram);
-	DEL_PTR(RenderState::sCgMrtVertexProgram);
 	
 	ResourceManager::DelSingleton();
 	loader = NULL;
-
-	DeferredRenderer::ReleaseCG();
-	SkyPreetham::ReleaseCG();
-
-	if (sCgContext)
-		cgDestroyContext(sCgContext);
 }
 
@@ -1963,5 +1869,6 @@
 void RenderSky()
 {
-	cgGLEnableProfile(RenderState::sCgVertexProfile);
+	loader->EnableVertexProfile();
+	loader->EnableFragmentProfile();
 
 	if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
@@ -1974,6 +1881,6 @@
 	preetham->RenderSkyDome(-light->GetDirection(), camera, &state, !useToneMapping);
 
-	cgGLDisableProfile(RenderState::sCgVertexProfile);
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+	loader->DisableVertexProfile();
+	loader->DisableFragmentProfile();
 }
 
@@ -2050,11 +1957,15 @@
 void RenderShadowMap(float newfar)
 {
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
-	cgGLDisableProfile(RenderState::sCgVertexProfile);
+	loader->DisableVertexProfile();
+	loader->DisableFragmentProfile();
 
 	glDisableClientState(GL_NORMAL_ARRAY);
 
 	state.SetRenderPassType(RenderState::DEPTH_PASS);
+	
+	// hack: disable cull face because of alpha textured balconies
+	glDisable(GL_CULL_FACE);
 	state.LockCullFaceEnabled(true);
+
 	state.SetUseAlphaToCoverage(false);
 
@@ -2066,6 +1977,5 @@
 	// hack: temporarily change camera far plane
 	camera->SetFar(newfar);
-	glDisable(GL_CULL_FACE);
-	
+
 	// the scene is rendered withouth any shading	
 	shadowMap->ComputeShadowMap(shadowTraverser, viewProjMat);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/CHCPlusPlusTraverser.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/CHCPlusPlusTraverser.cpp	(revision 2878)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/CHCPlusPlusTraverser.cpp	(revision 3038)
@@ -294,7 +294,7 @@
 		while (!mIQueue.empty())
 			mQueryQueue.push(GetNextMultiQuery(mIQueue));
-		}
-		else
-		{
+	}
+	else
+	{
 		BvhNodeContainer::const_iterator it, it_end = mIQueue.end();
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3036)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3038)
@@ -9,4 +9,6 @@
 #include "ShadowMapping.h"
 #include "Light.h"
+#include "ResourceManager.h"
+
 
 #include <IL/il.h>
@@ -260,4 +262,6 @@
 
 	mFBOs.push_back(mDownSampleFbo);
+
+	InitCg();
 }
 
@@ -276,46 +280,20 @@
 
 
-void DeferredRenderer::ReleaseCG()
-{
-	CLEAR_CONTAINER(sShaders);
-}
-
-
-bool DeferredRenderer::InitProgram(ShaderProgram **program, 
-								   CGcontext context, 
-								   const std::string &filename,
-								   const std::string &functionName)
-{
-	const string fullName = "src/shaders/" + filename + ".cg";
-
-	*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;
-	}
-	
-	cout << "Program " << functionName << " in " << fullName << " loaded" << endl;
-
-	return true;
-}
-
-
-void DeferredRenderer::InitCG(CGcontext context)
+
+void DeferredRenderer::InitCg()
 {	
-	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");
+	ResourceManager *rm = ResourceManager::GetSingleton();
+
+	sCgDeferredProgram = rm->CreateFragmentProgram("deferred", "main");
+	sCgDeferredShadowProgram = rm->CreateFragmentProgram("deferred", "main_shadow");
+	sCgSsaoProgram = rm->CreateFragmentProgram("ssao", "main");
+	sCgGiProgram = rm->CreateFragmentProgram("globillum", "main");
+	sCgCombineIllumProgram = rm->CreateFragmentProgram("globillum", "combine");
+	sCgCombineSsaoProgram = rm->CreateFragmentProgram("ssao", "combine");
+	sCgAntiAliasingProgram = rm->CreateFragmentProgram("antialiasing", "main");
+	sCgToneProgram = rm->CreateFragmentProgram("tonemap", "ToneMap");
+	sCgDownSampleProgram = rm->CreateFragmentProgram("tonemap", "DownSample");
+	sCgToneProgram = rm->CreateFragmentProgram("tonemap", "ToneMap");
+	sCgLogLumProgram = rm->CreateFragmentProgram("tonemap", "CalcAvgLogLum");
 
 	int i;
@@ -425,5 +403,5 @@
 	
 
-	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+	ResourceManager::GetSingleton()->EnableFragmentProfile();
 
 	glDisable(GL_ALPHA_TEST);
@@ -497,5 +475,5 @@
 
 	FrameBufferObject::Release();
-	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+	ResourceManager::GetSingleton()->DisableFragmentProfile();
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h	(revision 3036)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h	(revision 3038)
@@ -35,4 +35,6 @@
 class Texture;
 class Material;
+class ShaderProgram;
+class FrameBufferObject;
 
 struct LODLevel;
@@ -486,4 +488,8 @@
 typedef std::queue<BvhNode *> BvhNodeQueue;
 
+
+///////
+//-- container definitions
+
 typedef std::vector<Shape *> ShapeContainer;
 typedef std::vector<Geometry *> GeometryContainer;
@@ -495,4 +501,6 @@
 typedef std::vector<Material *> MaterialContainer;
 typedef std::vector<GPUProgramParameters *> GPUParamContainer;
+typedef  std::vector<FrameBufferObject *> FBOContainer;
+typedef  std::vector<ShaderProgram *> ShaderContainer;
 
 typedef std::vector<Vector3> VertexArray;
