Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/IrradianceMapping.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/IrradianceMapping.vcproj	(revision 3017)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/IrradianceMapping.vcproj	(revision 3037)
@@ -183,18 +183,18 @@
 	<Files>
 		<Filter
-			Name="Source Files"
-			Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
-			UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
-			>
-			<File
-				RelativePath=".\src\irradiance.cpp"
-				>
-			</File>
-		</Filter>
-		<Filter
 			Name="Header Files"
 			Filter="h;hpp;hxx;hm;inl;inc;xsd"
 			UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
 			>
+			<Filter
+				Name="Source Files"
+				Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+				UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+				>
+				<File
+					RelativePath=".\src\irradiance.cpp"
+					>
+				</File>
+			</Filter>
 		</Filter>
 	</Files>
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln	(revision 3019)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.sln	(revision 3037)
@@ -7,4 +7,9 @@
 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}
@@ -29,4 +34,8 @@
 		{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/ResourceManager.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3036)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3037)
@@ -32,4 +32,6 @@
 {
 
+// only instance of the resource manager
+ResourceManager *ResourceManager::sResourceManager = NULL;
 
 ResourceManager::~ResourceManager()
@@ -44,4 +46,5 @@
 	CLEAR_CONTAINER(mLODs);
 	CLEAR_CONTAINER(mGPUParameters);
+	CLEAR_CONTAINER(mShaders);
 }
 
@@ -324,4 +327,11 @@
 }
 
-}
-
+
+ShaderProgram *CreateShaderProgram(const std::string &filename, const std::string &funcName, bool isFragment)
+{
+	ShaderProgram *p;
+	return p;
+}
+
+}
+
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h	(revision 3036)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h	(revision 3037)
@@ -33,5 +33,23 @@
 public:
 
-	~ResourceManager();
+	/** Returns the resource manager as a singleton.
+	*/
+	static ResourceManager *GetSingleton()
+	{
+		if (!sResourceManager)
+		{
+			sResourceManager = new ResourceManager();
+		}
+
+		return sResourceManager;
+	}
+
+	/** We also want full control over the delete.
+	*/
+	static void DelSingleton()
+	{
+		DEL_PTR(sResourceManager);
+	}
+
 	/** Loads a model
 	*/
@@ -43,7 +61,18 @@
 	*/
 	GPUProgramParameters *CreateGPUProgramParameters(ShaderProgram *p);
+	/** Creates a new shader program.
+	*/
+	ShaderProgram *CreateShaderProgram(const std::string &filename, const std::string &funcName, bool isFragment);
 
 protected:
 	
+	/** Default constructor. The constructor is protected
+		to have control over instantiation using the 
+		singleton pattern.
+	*/
+	ResourceManager() {}
+
+	~ResourceManager();
+
 	void LoadTextures(igzstream &str);
 	void LoadShapes(igzstream &str);
@@ -74,5 +103,10 @@
 	/// the scene entities
 	SceneEntityContainer mSceneEntities;
+
+private:
+
+	static ResourceManager *sResourceManager;
 };
+
 
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/irradiance.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/irradiance.cpp	(revision 3020)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/irradiance.cpp	(revision 3037)
@@ -1,8 +1,26 @@
 // chcdemo.cpp : Defines the entry point for the console application.
 //
-#include "glInterface.h"
+
+
+#include "common.h"
+
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
+
 #include <math.h>
 #include <time.h>
-#include "common.h"
+#include "glInterface.h"
+
+#include <Cg/cg.h>
+#include <Cg/cgGL.h>
+
+
 #include "RenderTraverser.h"
 #include "SceneEntity.h"
@@ -24,6 +42,4 @@
 #include "RenderQueue.h"
 #include "Material.h"
-#include <Cg/cg.h>
-#include <Cg/cgGL.h>
 #include "glfont2.h"
 #include "PerformanceGraph.h"
@@ -42,6 +58,4 @@
 #include "EntityMerger.h"
 
-
-
 using namespace std;
 using namespace CHCDemoEngine;
@@ -59,6 +73,5 @@
 /// the renderable scene geometry
 SceneEntityContainer sceneEntities;
-SceneEntityContainer sceneEntities2;
-SceneEntity *buddha = NULL;
+
 // traverses and renders the hierarchy
 RenderTraverser *traverser = NULL;
@@ -255,5 +268,5 @@
 inline float KeyRotationAngle() { return keyRotation * elapsedTime * 1e-3f; }
 inline float KeyShift() { return keyForwardMotion * elapsedTime * 1e-3f; }
-
+// initialise the frame buffer objects
 void InitFBO();
 
@@ -268,20 +281,7 @@
 
 static CGcontext sCgContext = NULL;
-static CGprogram sCgMrtVertexProgram = NULL;
-
-static CGparameter sMaxDepthParam;
-static CGparameter sMaxDepthParamTex;
+
 static CGparameter sEyePosParamTex;
 static CGparameter sEyePosParam;
-
-static CGparameter sTLParam;
-static CGparameter sTRParam;
-static CGparameter sBRParam;
-static CGparameter sBLParam;
-
-static CGparameter sTLParamTex;
-static CGparameter sTRParamTex;
-static CGparameter sBRParamTex;
-static CGparameter sBLParamTex;
 
 static Matrix4x4 viewProjMat = IdentityMatrix();
@@ -321,4 +321,20 @@
 int main(int argc, char* argv[])
 {
+/*
+#ifdef _CRT_SET
+
+	//Now just call this function at the start of your program and if you're
+	//compiling in debug mode (F5), any leaks will be displayed in the Output
+	//window when the program shuts down. If you're not in debug mode this will
+	//be ignored. Use it as you will!
+	//note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
+
+	_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
+	_CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
+	_CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR); 
+#endif
+*/
+	cout << "=== reading environment file ===" << endl << endl;
+
 	int returnCode = 0;
 
@@ -329,5 +345,5 @@
 	cout << "=== reading environment file ===" << endl << endl;
 
-	string envFileName = "default.env";
+	const string envFileName = "default.env";
 	if (!env.Read(envFileName))
 	{
@@ -359,5 +375,4 @@
 		//env.GetStringParam(string("modelPath"), model_path);
 		//env.GetIntParam(string("numSssaoSamples"), numSsaoSamples);
-
 
 		cout << "assumedVisibleFrames: " << assumedVisibleFrames << endl; 
@@ -407,5 +422,4 @@
 	SceneEntity::SetUseLODs(useLODs);
 
-
 	if (!useFullScreen)
 	{
@@ -432,5 +446,4 @@
 	InitGLstate();
 
-
 	glEnable(GL_MULTISAMPLE_ARB);
 	glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
@@ -439,10 +452,13 @@
 	MiddleMotion(0, 0);
 
+	// init cg shader programs
+	InitCg();
+
 	perfGraph = new PerformanceGraph(1000);
 
-	loader = new ResourceManager();
+	loader = ResourceManager::GetSingleton();
 
 	const string filename = string(model_path + "city.dem");
-
+	
 	if (loader->Load(filename, sceneEntities))
 		cout << "model " << filename << " loaded" << endl;
@@ -453,4 +469,5 @@
 		exit(0);
 	}
+	
 
 	const string bvh_filename = string(model_path + "city.bvh");
@@ -464,65 +481,4 @@
 		exit(0);
 	}
-	
-	
-	const string buddhafilename = string(model_path + "hbuddha.dem");
-
-	if (loader->Load(buddhafilename, sceneEntities2))
-		cout << "model " << buddhafilename << " loaded" << endl;
-	else
-	{
-		cerr << "loading model " << buddhafilename << " failed" << endl;
-		CleanUp();
-		exit(0);
-	}
-
-	buddha = sceneEntities2[0];
-
-	Matrix4x4 translation = TranslationMatrix(camera->GetPosition());
-
-	buddha->GetTransform()->SetMatrix(translation);
-
-
-#if 0
-
-	int merged = 0;
-
-	SceneEntity *oldEnt = NULL;
-
-	cout << "merging entities .. " << endl;
-	SceneEntityContainer::const_iterator sit, sit_end = sceneEntities.end();
-
-	for (sit = sceneEntities.begin(); sit < sit_end; ++ sit)
-	{
-		SceneEntity *newEnt = (*sit);
-
-		if (!newEnt->GetTransform()->IsIdentity())
-		{
-			sceneEntities2.push_back(newEnt);
-			continue;
-		}
-
-		if (oldEnt)
-		{
-			EntityMerger merger(newEnt, oldEnt);
-			SceneEntity *ent = merger.Merge();
-
-			sceneEntities2.push_back(ent);
-
-			oldEnt = NULL;
-
-			++ merged;
-		}
-		else
-		{
-			oldEnt = newEnt;
-		}
-	}
-
-	if (oldEnt && oldEnt->GetTransform()->IsIdentity())
-		sceneEntities2.push_back(oldEnt);
-
-	cout << "merged " << merged << " of " << (int)sceneEntities.size() << " entities " << endl;
-#endif
 
 	// set far plane based on scene extent
@@ -532,13 +488,7 @@
 	camera->SetFar(Magnitude(bvh->GetBox().Diagonal()));
 	
-	InitCg();
-
-	DeferredRenderer::Init(sCgContext);
-	SkyPreetham::Init(sCgContext);
-
 	Vector3 cubeCenter(470.398f, 240.364f, 182.5f);
 	
 	Matrix4x4 transl = TranslationMatrix(cubeCenter);
-	
 
 	SceneEntityContainer dummy;
@@ -546,6 +496,8 @@
 	string skyDomeStr(model_path + "sky.dem");
 
-	if (loader->Load(skyDomeStr, dummy))
-		cout << "successfully loaded " << dummy.size() << " scene entities" << endl;
+	if (loader->Load(skyDomeStr, sceneEntities))
+	{
+		cout << "successfully loaded " << sceneEntities.size() << " scene entities" << endl;
+	}
 	else
 	{
@@ -556,6 +508,7 @@
 	}
 
-	skyDome = dummy[0];
-
+	skyDome = sceneEntities.back();
+
+	//InitCg();
 
 	const float turbitiy = 5.0f;
@@ -568,5 +521,4 @@
 	
 	state.SetRenderPassType(RenderState::FIXED);
-	sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
 
 	// frame time is restarted every frame
@@ -578,5 +530,5 @@
 	// clean up
 	CleanUp();
-
+	
 	return 0;
 }
@@ -585,11 +537,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);
@@ -598,73 +550,33 @@
 	cgGLSetOptimalOptions(RenderState::sCgVertexProfile);
 
-	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");
-	}
+	// 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 = 
-		cgCreateProgramFromFile(sCgContext, 
-								CG_SOURCE,
-								"src/shaders/mrt.cg", 
-								RenderState::sCgFragmentProfile,
-								"fragtex",
-								NULL);
-
-	if (RenderState::sCgMrtFragmentTexProgram != NULL)
-	{
-		cgGLLoadProgram(RenderState::sCgMrtFragmentTexProgram);
-
-		sMaxDepthParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "maxDepth");
-		sEyePosParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "eyePos");
-		RenderState::sTexParam = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "tex");
-	
-		cgGLSetParameter1f(sMaxDepthParamTex, MAX_DEPTH_CONST / farDist);
-
-		sTLParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "tl"); 
-		sTRParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "tr"); 
-		sBRParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "br"); 
-		sBLParamTex = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "bl"); 
-	}
-	else
+		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 = 
-		cgCreateProgramFromFile(sCgContext, 
-								CG_SOURCE,
-								"src/shaders/mrt.cg", 
-								RenderState::sCgFragmentProfile,
-								"frag",
-								NULL);
-
-	if (RenderState::sCgMrtFragmentProgram != NULL)
-	{
-		cgGLLoadProgram(RenderState::sCgMrtFragmentProgram);
-
-		sMaxDepthParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "maxDepth");
-		sEyePosParam = cgGetNamedParameter(RenderState::sCgMrtFragmentTexProgram, "eyePos");
-		
-		cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
-
-		sTLParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "tl"); 
-		sTRParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "tr"); 
-		sBRParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "br"); 
-		sBLParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "bl"); 
-	}
-	else
+		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;
@@ -682,12 +594,12 @@
 
 	// the diffuse color buffer
-	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
+	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, ColorBufferObject::FILTER_NEAREST);
 	// the normals buffer
-	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
+	fbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
 	// the positions buffer
 	//fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
 	fbo->AddColorBuffer(ColorBufferObject::RGB_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST);
 	// another color buffer
-	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, ColorBufferObject::FILTER_NEAREST);
+	fbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, ColorBufferObject::FILTER_NEAREST);
 
 	PrintGLerror("fbo");
@@ -701,4 +613,5 @@
 	glGenTextures(1, &fontTex);
 	glBindTexture(GL_TEXTURE_2D, fontTex);
+
 	if (!myfont.Create("data/fonts/verdana.glf", fontTex))
 		return false;
@@ -942,9 +855,4 @@
 	// store matrix for later use
 	viewProjMat = matViewing * matProjection;
-	
-	if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED))
-	{
-		cgGLSetMatrixParameterfc(Transform3::sModelMatrixParam, (const float *)IdentityMatrix().x);
-	}
 }
 
@@ -997,26 +905,11 @@
 
 	cgGLEnableProfile(RenderState::sCgVertexProfile);
-	cgGLBindProgram(sCgMrtVertexProgram);
+	RenderState::sCgMrtVertexProgram->Bind();
 
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-	cgGLBindProgram(RenderState::sCgMrtFragmentProgram);
+	RenderState::sCgMrtFragmentProgram->Bind();
 
 	const Vector3 pos = camera->GetPosition();
 
-	cgGLSetParameter3f(sEyePosParam, pos.x, pos.y, pos.z);
-	cgGLSetParameter3f(sEyePosParamTex, pos.x, pos.y, pos.z);
-
-	Vector3 tl, tr, bl, br;
-	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);
-
-	cgGLSetParameter3f(sBLParamTex, bl.x, bl.y, bl.z);
-	cgGLSetParameter3f(sBRParamTex, br.x, br.y, br.z);
-	cgGLSetParameter3f(sTLParamTex, tl.x, tl.y, tl.z);
-	cgGLSetParameter3f(sTRParamTex, tr.x, tr.y, tr.z);
 
 	// draw to 3 color buffers
@@ -1075,5 +968,4 @@
 
 	}
-
 	
 	// bring eye modelview matrix up-to-date
@@ -1192,7 +1084,4 @@
 		// actually render the scene geometry using the specified algorithm
 		traverser->RenderScene();
-		
-		buddha->Render(&state);
-		renderQueue->Apply();
 	}
 
@@ -1843,18 +1732,23 @@
 	DEL_PTR(visualization);
 	DEL_PTR(camera);
-	DEL_PTR(loader);
 	DEL_PTR(renderQueue);
 	DEL_PTR(perfGraph);
-
 	DEL_PTR(fbo);
 	DEL_PTR(ssaoShader);
-
-	if (sCgMrtVertexProgram)
-		cgDestroyProgram(sCgMrtVertexProgram);
-	if (RenderState::sCgMrtFragmentProgram)
-		cgDestroyProgram(RenderState::sCgMrtFragmentProgram);
-	if (RenderState::sCgMrtFragmentTexProgram)
-		cgDestroyProgram(RenderState::sCgMrtFragmentTexProgram);
-	
+	DEL_PTR(light);
+	DEL_PTR(visCamera);
+	DEL_PTR(preetham);
+	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);
@@ -2074,5 +1968,7 @@
 		state.SetRenderPassType(RenderState::DEFERRED);
 
-	const bool useToneMapping = ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) || (renderMethod == RENDER_DEFERRED)) && useHDR;
+	const bool useToneMapping = 
+		((renderMethod == RENDER_DEPTH_PASS_DEFERRED) || 
+		 (renderMethod == RENDER_DEFERRED)) && useHDR;
 
 	preetham->RenderSkyDome(-light->GetDirection(), camera, &state, !useToneMapping);
@@ -2138,4 +2034,7 @@
 void PlaceViewer(const Vector3 &oldPos)
 {
+	if (!sceneQuery)
+		sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
+
 	Vector3 playerPos = camera->GetPosition();
 	bool validIntersect = sceneQuery->CalcIntersection(playerPos);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3036)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3037)
@@ -457,5 +457,5 @@
 	perfGraph = new PerformanceGraph(1000);
 
-	loader = new ResourceManager();
+	loader = ResourceManager::GetSingleton();
 
 	const string filename = string(model_path + "city.dem");
@@ -521,5 +521,4 @@
 	
 	state.SetRenderPassType(RenderState::FIXED);
-	sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
 
 	// frame time is restarted every frame
@@ -1733,5 +1732,4 @@
 	DEL_PTR(visualization);
 	DEL_PTR(camera);
-	DEL_PTR(loader);
 	DEL_PTR(renderQueue);
 	DEL_PTR(perfGraph);
@@ -1747,4 +1745,7 @@
 	DEL_PTR(RenderState::sCgMrtVertexProgram);
 	
+	ResourceManager::DelSingleton();
+	loader = NULL;
+
 	DeferredRenderer::ReleaseCG();
 	SkyPreetham::ReleaseCG();
@@ -2033,4 +2034,7 @@
 void PlaceViewer(const Vector3 &oldPos)
 {
+	if (!sceneQuery)
+		sceneQuery = new SceneQuery(bvh->GetBox(), traverser);
+
 	Vector3 playerPos = camera->GetPosition();
 	bool validIntersect = sceneQuery->CalcIntersection(playerPos);
