Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/lenseFlare.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/lenseFlare.cg	(revision 3214)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/lenseFlare.cg	(revision 3215)
@@ -34,5 +34,6 @@
 				  uniform sampler2D flareTex5,
 				  uniform float2 vectorToLight, // vector to current light position
-				  uniform float distanceToLight // distance to current light position
+				  uniform float distanceToLight, // distance to current light position
+				  uniform float sunVisiblePixels
 				  ): COLOR
 {
@@ -58,6 +59,8 @@
 
 	//result.xyz = float3(0);
+	sunVisiblePixels = clamp(sunVisiblePixels, .0f, 100.0f) * 0.01f;
+
 	for (int i = 0; i < 8; ++ i)
-		result.xyz += flare[i].xyz;
+		result.xyz += flare[i].xyz * sunVisiblePixels;
 
 	return result;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3154)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3215)
@@ -449,3 +449,13 @@
 }
 
-}
+
+
+Material *ResourceManager::CreateMaterial()
+{
+	Material *mat = new Material();
+	mMaterials.push_back(mat);
+
+	return mat;
+}
+
+}
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3214)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3215)
@@ -42,5 +42,4 @@
 				bool useToneMapping,
 				bool useAntiAliasing,
-				bool isLightSourceVisible,
 				ShadowMap *shadowMap = NULL
 				);
@@ -50,5 +49,7 @@
 
 	enum SAMPLING_METHOD {SAMPLING_POISSON, SAMPLING_QUADRATIC, SAMPLING_DEFAULT};
+	
 	enum SHADING_METHOD {DEFAULT, SSAO, GI};
+
 	/** Set the samplig method for the indirect illumination
 	*/
@@ -58,9 +59,12 @@
 	void SetShadingMethod(SHADING_METHOD s);
 
-	void SetSortSamples(bool sortSamples) { mSortSamples = sortSamples; }
+	void SetSortSamples(bool sortSamples);
 
 	void SetSampleIntensity(float sampleIntensity);
 
 	void SetKernelRadius(float kernelRadius);
+	/** Sets the number of visible pixels of the sun
+	*/
+	void SetSunVisiblePixels(int visiblePixels);
 
 
@@ -125,5 +129,7 @@
 	////////////
 
+	/// deferred shading output image width
 	int mWidth;
+	/// deferred shading output image height
 	int mHeight;
 
@@ -139,5 +145,5 @@
 
 	int mIllumFboIndex;
-	// the fbo for indirect illumination (ssao + color bleeding)
+	/// the fbo for indirect illumination (ssao + color bleeding)
 	FrameBufferObject *mIllumFbo;
 
@@ -161,4 +167,6 @@
 	float mKernelRadius;
 	float mSampleIntensity;
+
+	int mSunVisiblePixels;
 };
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h	(revision 3127)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.h	(revision 3215)
@@ -50,12 +50,15 @@
 	*/
 	static void DelSingleton();
-	/** Adds a scene entity to be handled by the resource mananger.
+	/** Adds a scene entity to the resource mananger, which from now on is handled by the manager.
 	*/
 	void AddSceneEntity(SceneEntity *ent);
-	/** Adds a scene entity to be handled by the resource mananger.
+	/** Creates a new transform.
 	*/
 	Transform3 *CreateTransform(const Matrix4x4 &m);
+	/** Creates a new default material that is handled by the manager.
+	*/
+	Material *CreateMaterial();
 
-
+	/// giant hack: set this to true if the geometry to load has tangent data for normal mapping
 	bool mUseNormalMapping;
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3214)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3215)
@@ -217,4 +217,5 @@
 bool useHDR = true;
 bool useAntiAliasing = true;
+bool useLenseFlare = true;
 
 PerfTimer frameTimer, algTimer;
@@ -276,5 +277,5 @@
 void RenderVisibleObjects();
 
-bool TestSunVisible();
+int TestSunVisible();
 
 void Begin2D();
@@ -391,4 +392,5 @@
 		env.GetBoolParam(string("useHDR"), useHDR);
 		env.GetBoolParam(string("useAA"), useAntiAliasing);
+		env.GetBoolParam(string("useLenseFlare"), useLenseFlare);
 		env.GetBoolParam(string("useAdvancedShading"), useAdvancedShading);
 
@@ -418,4 +420,5 @@
 		cout << "render method: " << renderMethod << endl;
 		cout << "use antialiasing: " << useAntiAliasing << endl;
+		cout << "use lense flare: " << useLenseFlare << endl;
 		cout << "use advanced shading: " << useAdvancedShading << endl;
 		cout << "turbitity: " << turbitity << endl;
@@ -584,5 +587,5 @@
 
 	// toto clean up material
-	Material *mat = new Material();
+	Material *mat = resourceManager->CreateMaterial();
 
 	mat->GetTechnique(0)->SetDepthWriteEnabled(false);
@@ -591,4 +594,7 @@
 	sunBox = conv.ConvertBox(sbox, mat, trafo);
 	
+	resourceManager->AddSceneEntity(sunBox);
+
+	/// create single occlusion query that handles sun visibility
 	glGenQueriesARB(1, &sunQuery);
 
@@ -1158,7 +1164,10 @@
 		RenderVisibleObjects();
 	}
-	
-
-	bool sunVisible = TestSunVisible();
+
+	const bool useDeferred = 
+		((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED));
+
+	// if no lense flare => just set sun to invisible
+	int sunVisiblePixels = useLenseFlare  &&  useDeferred ? TestSunVisible() : 0;
 
 
@@ -1172,6 +1181,5 @@
 
 
-	if ((renderMethod == RENDER_DEFERRED) ||
-		(renderMethod == RENDER_DEPTH_PASS_DEFERRED))
+	if (useDeferred)
 	{
 		FrameBufferObject::Release();
@@ -1192,4 +1200,5 @@
 			shadingMethod = DeferredRenderer::DEFAULT;
 
+		deferredShader->SetSunVisiblePixels(sunVisiblePixels);
 		deferredShader->SetShadingMethod(shadingMethod);
 		deferredShader->SetSamplingMethod(samplingMethod);
@@ -1200,5 +1209,5 @@
 
 		ShadowMap *sm = showShadowMap ? shadowMap : NULL;
-		deferredShader->Render(fbo, ssaoTempCohFactor, light, useHDR, useAntiAliasing, sunVisible, sm);
+		deferredShader->Render(fbo, ssaoTempCohFactor, light, useHDR, useAntiAliasing, sm);
 	}
 
@@ -1400,8 +1409,8 @@
 		useAntiAliasing = !useAntiAliasing;
 		break;
-	/*case '?':
-		sortSamples = !sortSamples;
-		break;
-	*/
+	case 'c':
+	case 'C':
+		useLenseFlare = !useLenseFlare;
+		break;
 	default:
 		return;
@@ -2248,6 +2257,8 @@
 }
 
-
-bool TestSunVisible()
+/** This function returns the number of visible pixels of a
+	bounding box representing the sun.
+*/
+int TestSunVisible()
 {
 	// assume sun is at a far away point along the light vector
@@ -2267,4 +2278,4 @@
 	glGetQueryObjectuivARB(sunQuery, GL_QUERY_RESULT_ARB, &sampleCount);
 
-	return (sampleCount > 0);
-}
+	return sampleCount;
+}
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3214)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3215)
@@ -328,5 +328,6 @@
 mSortSamples(true),
 mKernelRadius(1e-8f),
-mSampleIntensity(0.2f)
+mSampleIntensity(0.2f),
+mSunVisiblePixels(0)
 {
 	///////////
@@ -393,11 +394,4 @@
 	glDeleteTextures(1, &noiseTex1D);
 }
-
-
-void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
-{
-	mUseTemporalCoherence = temporal;
-}
-
 
 
@@ -501,7 +495,7 @@
 	string lenseFlareParams[] = 
 		{"colorsTex", "flareTex1", "flareTex2", "flareTex3", "flareTex4", 
-		 "flareTex5", "vectorToLight", "distanceToLight"};
-
-	sCgLenseFlareProgram->AddParameters(lenseFlareParams, 0, 8);
+		 "flareTex5", "vectorToLight", "distanceToLight", "sunVisiblePixels"};
+
+	sCgLenseFlareProgram->AddParameters(lenseFlareParams, 0, 9);
 
 
@@ -555,5 +549,4 @@
 							  bool useToneMapping,
 							  bool useAntiAliasing,
-							  bool lightSourceVisible,
 							  ShadowMap *shadowMap
 							  )
@@ -597,7 +590,6 @@
 	}
 
-
-	if (lightSourceVisible)
-		LenseFlare(fbo, light);
+	/// compute lense flare
+	LenseFlare(fbo, light);
 
 	// multisampling is difficult / costly with deferred shading
@@ -989,46 +981,4 @@
 
 
-void DeferredRenderer::SetSamplingMethod(SAMPLING_METHOD s) 
-{ 
-	if (s != mSamplingMethod)
-	{
-		mSamplingMethod = s; 
-		mRegenerateSamples = true;
-	}
-}
-
-
-void DeferredRenderer::SetShadingMethod(SHADING_METHOD s)
-{
-	if (s != mShadingMethod)
-	{
-		mShadingMethod = s; 
-		mRegenerateSamples = true;
-	}
-}
-
-
-#if TODO
-
-void DeferredRenderer::SetNumSamples(int numSamples)
-{
-	mNumSamples = numSamples;
-}
-
-#endif
-
-
-void DeferredRenderer::SetSampleIntensity(float sampleIntensity)
-{
-	mSampleIntensity = sampleIntensity;
-}
-
-
-void DeferredRenderer::SetKernelRadius(float kernelRadius)
-{
-	mKernelRadius = kernelRadius;
-}
-
-
 void DeferredRenderer::ComputeToneParameters(FrameBufferObject *fbo, 
 											 DirectionalLight *light,
@@ -1273,7 +1223,10 @@
 
 void DeferredRenderer::LenseFlare(FrameBufferObject *fbo,
-								  DirectionalLight *light
+								  DirectionalLight *light							
 								  )
 {
+	// light source not visible
+	if (!mSunVisiblePixels) return;
+
 	// the sun is a large distance in the reverse light direction
 	// => extrapolate light pos
@@ -1285,12 +1238,4 @@
 	projLightPos = projLightPos * 0.5f + Vector3(0.5f);
 
-	//Vector3 dummy = mProjViewMatrix * lightPos;
-
-	// check if light out of image range
-	/*if ((projLightPos.x < -0.2f) || (projLightPos.y < -0.2f) ||
-		(projLightPos.x >= 1.2f) || (projLightPos.y >= 1.2f) || 
-		(w < .0f))
-		return;
-*/
 	// vector to light from screen center in texture space
 	Vector3 vectorToLight = projLightPos - Vector3(0.5f);
@@ -1318,4 +1263,5 @@
 	sCgLenseFlareProgram->SetValue2f(i ++, vectorToLight.x, vectorToLight.y);
 	sCgLenseFlareProgram->SetValue1f(i ++, distanceToLight);
+	sCgLenseFlareProgram->SetValue1f(i ++, (float)mSunVisiblePixels);
 
 	DrawQuad(sCgLenseFlareProgram);
@@ -1325,3 +1271,65 @@
 
 
+
+void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
+{
+	mUseTemporalCoherence = temporal;
+}
+
+
+void DeferredRenderer::SetSamplingMethod(SAMPLING_METHOD s) 
+{ 
+	if (s != mSamplingMethod)
+	{
+		mSamplingMethod = s; 
+		mRegenerateSamples = true;
+	}
+}
+
+
+void DeferredRenderer::SetShadingMethod(SHADING_METHOD s)
+{
+	if (s != mShadingMethod)
+	{
+		mShadingMethod = s; 
+		mRegenerateSamples = true;
+	}
+}
+
+
+#if TODO
+
+void DeferredRenderer::SetNumSamples(int numSamples)
+{
+	mNumSamples = numSamples;
+}
+
+#endif
+
+
+void DeferredRenderer::SetSampleIntensity(float sampleIntensity)
+{
+	mSampleIntensity = sampleIntensity;
+}
+
+
+void DeferredRenderer::SetKernelRadius(float kernelRadius)
+{
+	mKernelRadius = kernelRadius;
+}
+
+
+void DeferredRenderer::SetSortSamples(bool sortSamples) 
+{
+	mSortSamples = sortSamples; 
+}
+
+
+void DeferredRenderer::SetSunVisiblePixels(int pixels) 
+{
+	mSunVisiblePixels = pixels;
+}
+
+
+
 } // namespace
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3212)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 3215)
@@ -12,5 +12,5 @@
 # triangles per bvh leaf (influences hierarchy depth vs. occlusion power)
 trianglesPerVirtualLeaf=300
-
+# max depth for searching for nodes for tight bounds
 maxDepthForTestingChildren=3
 
@@ -34,19 +34,6 @@
 #camDirection=1 0 0
 
-#camPosition=468.025 267.591 182.478
-#camDirection=0.937282 0.348573 -0
-
-# bad view point (culling bug!)
-camPosition=469.381 267.293 184.778
-camDirection=0.142006 0.645885 -0.750111
-
-#camPosition=458.412 255.767 182.632
-#camDirection=0.891691 0.42723 0.149535
-
-#camPosition=740.137 487.855 188.506
-#camDirection=0.669256 0.727829 0.149535
-
-#camPosition=492.678 249.827 187.232
-#camDirection=-0.651041 -0.693292 -0.309017
+camPosition=468.025 267.591 182.478
+camDirection=0.937282 0.348573 -0
 
 #lightDirection=-0.8f 1.0f -0.7f
@@ -57,6 +44,6 @@
 # window options
 
-//winWidth=800
-//winHeight=600
+#winWidth=800
+#winHeight=600
 
 winWidth=1024
@@ -83,4 +70,7 @@
 useAA=1
 
+# show lense flare
+useLenseFlare=1
+
 # use ssao / color bleeding
 useAdvancedShading=1
@@ -89,8 +79,4 @@
 turbitity=3.0f
 
-#second bad view point
-//camPosition=465.626 248.788 184.978
-//camDirection=0.0592959 0.998021 0.0209425
-
 ssaoKernelRadius=8e-1f
 
