Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3018)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3019)
@@ -61,4 +61,5 @@
 
 
+ShaderContainer DeferredRenderer::sShaders;
 
 ///////////////////////////////////////
@@ -296,4 +297,19 @@
 
 
+static void InitBuffer(FrameBufferObject *fbo, int index)
+{
+	// read the second buffer, write to the first buffer
+	fbo->Bind();
+	glDrawBuffers(1, mrt + index);
+
+	//glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	glEnd();
+
+	FrameBufferObject::Release();
+}
+
+
 DeferredRenderer::DeferredRenderer(int w, int h, Camera *cam, float scaleFactor):
 mWidth(w), mHeight(h), 
@@ -303,5 +319,5 @@
 mSamplingMethod(SAMPLING_POISSON),
 mShadingMethod(DEFAULT),
-mFboIndex(0)
+mIllumFboIndex(0)
 {
 	// create noise texture for ssao
@@ -312,29 +328,25 @@
 	//-- the flip-flop fbos
 
-	mFbo = new FrameBufferObject(w / 2, h / 2, FrameBufferObject::DEPTH_NONE);
-	//mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
-
-	mFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
-	mFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
-	mFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
-	mFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
+	mIllumFbo = new FrameBufferObject(w / 2, h / 2, FrameBufferObject::DEPTH_NONE);
+	mFBOs.push_back(mIllumFbo);
+
+	for (int i = 0; i < 4; ++ i)
+	{
+		mIllumFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
+		InitBuffer(mIllumFbo, i);
+	}
 
 	mDownSampleFbo = new FrameBufferObject(w / 2, h / 2, FrameBufferObject::DEPTH_NONE);
-
 	mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGBA_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
 	mDownSampleFbo->AddColorBuffer(ColorBufferObject::RGB_FLOAT_16, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR);
-}
-
+
+	mFBOs.push_back(mDownSampleFbo);
+}
 
 
 DeferredRenderer::~DeferredRenderer() 
 {
-	if (sCgSsaoProgram)	cgDestroyProgram(sCgSsaoProgram);
-	if (sCgDeferredProgram)	cgDestroyProgram(sCgDeferredProgram);
-	if (sCgSsaoProgram)	cgDestroyProgram(sCgSsaoProgram);
-	if (sCgGiProgram) cgDestroyProgram(sCgGiProgram);
-	if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram);
-
-	DEL_PTR(mFbo);
+	CLEAR_CONTAINER(sShaders);
+	CLEAR_CONTAINER(mFBOs);
 
 	glDeleteTextures(1, &noiseTex);
@@ -350,4 +362,7 @@
 void DeferredRenderer::Init(CGcontext context)
 {	
+	ShaderProgram *pr;
+
+
 	sCgDeferredProgram = 
 		cgCreateProgramFromFile(context, 
@@ -368,4 +383,7 @@
 		
 		sLightDirParam = cgGetNamedParameter(sCgDeferredProgram, "lightDir");
+
+		pr = new ShaderProgram(sCgDeferredProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -407,4 +425,7 @@
 		sBRParam = cgGetNamedParameter(sCgSsaoProgram, "br"); 
 		sBLParam = cgGetNamedParameter(sCgSsaoProgram, "bl"); 
+
+		pr = new ShaderProgram(sCgSsaoProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -443,4 +464,7 @@
 
 		sEyePosGiParam = cgGetNamedParameter(sCgGiProgram, "eyePos");
+
+		pr = new ShaderProgram(sCgGiProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -462,4 +486,7 @@
 		sSsaoTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "ssaoTex");
 		sIllumTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "illumTex");
+
+		pr = new ShaderProgram(sCgCombinedIllumProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -484,4 +511,7 @@
 		sFilterOffsetsParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "filterOffs");
 		sFilterWeightsParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "filterWeights");
+
+		pr = new ShaderProgram(sCgCombinedSsaoProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -503,4 +533,7 @@
 		sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");  
 		sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
+
+		pr = new ShaderProgram(sCgAntiAliasingProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -538,4 +571,7 @@
 
 		cgGLSetParameterArray2f(sSamplesShadowParam, 0, NUM_PCF_TABS, (const float *)pcfSamples);
+
+		pr = new ShaderProgram(sCgDeferredShadowProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -553,5 +589,8 @@
 	{
 		cgGLLoadProgram(sCgLogLumProgram);
-		sColorsTexLogLumParam = cgGetNamedParameter(sCgLogLumProgram, "colors");  
+		sColorsTexLogLumParam = cgGetNamedParameter(sCgLogLumProgram, "colors"); 
+
+		pr = new ShaderProgram(sCgLogLumProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -576,4 +615,7 @@
 
 		sColorsTexToneParam = cgGetNamedParameter(sCgToneProgram, "colors");  
+
+		pr = new ShaderProgram(sCgToneProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -596,4 +638,7 @@
 		sColorsTexDownSampleParam = cgGetNamedParameter(sCgDownSampleProgram, "colors"); 
 		sDownSampleOffsetParam = cgGetNamedParameter(sCgDownSampleProgram, "downSampleOffs");
+
+		pr = new ShaderProgram(sCgDownSampleProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -618,5 +663,5 @@
 	// the other one will be written
 
-	mFboIndex = 2 - mFboIndex;
+	mIllumFboIndex = 2 - mIllumFboIndex;
 	
 
@@ -726,12 +771,12 @@
 #endif
 
-	GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
+	GLuint oldTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
 
 	glPushAttrib(GL_VIEWPORT_BIT);
-	glViewport(0, 0, mFbo->GetWidth(), mFbo->GetHeight());
+	glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
 
 	// read the second buffer, write to the first buffer
-	mFbo->Bind();
-	glDrawBuffers(1, mrt + mFboIndex);
+	mIllumFbo->Bind();
+	glDrawBuffers(1, mrt + mIllumFboIndex);
 
 	
@@ -850,5 +895,5 @@
 	cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
 
-
+	// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 	glColor3f(1.0f, 1.0f, 1.0f);
 
@@ -939,13 +984,13 @@
 
 	glPushAttrib(GL_VIEWPORT_BIT);
-	glViewport(0, 0, mFbo->GetWidth(), mFbo->GetHeight());
+	glViewport(0, 0, mIllumFbo->GetWidth(), mIllumFbo->GetHeight());
 
 	// read the second buffer, write to the first buffer
-	mFbo->Bind();
-
-	glDrawBuffers(2, mrt + mFboIndex);
-
-	GLuint oldSsaoTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
-	GLuint oldIllumTex = mFbo->GetColorBuffer(2 - mFboIndex + 1)->GetTexture();
+	mIllumFbo->Bind();
+
+	glDrawBuffers(2, mrt + mIllumFboIndex);
+
+	GLuint oldSsaoTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex)->GetTexture();
+	GLuint oldIllumTex = mIllumFbo->GetColorBuffer(2 - mIllumFboIndex + 1)->GetTexture();
 	
 	cgGLBindProgram(sCgGiProgram);
@@ -1024,6 +1069,6 @@
 	GLuint colorsTex = fbo->GetColorBuffer(colorBufferIdx)->GetTexture();
 
-	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
-	GLuint illumTex = mFbo->GetColorBuffer(mFboIndex + 1)->GetTexture();
+	GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
+	GLuint illumTex = mIllumFbo->GetColorBuffer(mIllumFboIndex + 1)->GetTexture();
 
 
@@ -1084,5 +1129,5 @@
 	GLuint positionsTex = fbo->GetColorBuffer(2)->GetTexture();
 	
-	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
+	GLuint ssaoTex = mIllumFbo->GetColorBuffer(mIllumFboIndex)->GetTexture();
 
 	// overwrite old color texture
