Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Light.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Light.cpp	(revision 2891)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Light.cpp	(revision 2891)
@@ -0,0 +1,29 @@
+#include "LODInfo.h"
+#include "Vector3.h"
+#include "Shape.h"
+#include "Geometry.h"
+
+
+
+namespace CHCDemoEngine 
+{
+
+int LODLevel::sFrameId = 0;
+Vector3 LODLevel::sViewPoint(0.0f, 0.0f, 0.0f);
+
+
+void LODLevel::InitFrame(const Vector3 &viewPoint)
+{
+	sViewPoint = viewPoint;
+	++ sFrameId;
+}
+
+
+void LODLevel::AddShape(Shape *shape)
+{
+	mShapes.push_back(shape);
+	mNumTriangles += shape->GetGeometry()->GetNumTriangles();
+}
+
+
+}
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2887)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2891)
@@ -2,10 +2,10 @@
 #define __SHADERENV_H
 
-//#define NUM_SAMPLES 8
-#define NUM_SAMPLES 16
+#define NUM_SAMPLES 8
+//#define NUM_SAMPLES 16
 
 // rule of thumb: approx 1 / NUM_SAMPLES
-#define SAMPLE_INTENSITY 0.14f
-//#define SAMPLE_INTENSITY 0.28f
+//#define SAMPLE_INTENSITY 0.14f
+#define SAMPLE_INTENSITY 0.28f
 
 #define AREA_SIZE 5e-1f
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2873)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/mrt.cg	(revision 2891)
@@ -24,14 +24,15 @@
 struct fragin
 {
-  float4 position: POSITION; // eye space
-  float4 texCoord: TEXCOORD0;    
+	float4 color: COLOR0;  
+	float4 position: POSITION; // eye space
+	float4 texCoord: TEXCOORD0;    
 
-  float4 projPos: WPOS;
-  float4 color: COLOR0;  
-  float4 worldPos: TEXCOORD1; // world position
-  float3 normal: TEXCOORD2;
+	float4 projPos: WPOS;
+	float4 worldPos: TEXCOORD1; // world position
 
-  float4 mypos: TEXCOORD3;
-  //float DEPTH;
+	float3 normal: TEXCOORD2;
+
+	float4 mypos: TEXCOORD3;
+	//float DEPTH;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2887)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/antialiasing.cg	(revision 2891)
@@ -13,7 +13,7 @@
 uniform float4 e_barrier = float4(5e-5, 5e-5, 0, 0); // x = normal, y = depth
 // the weights for normal / depth discontinuity
-//uniform float4 e_weights = float4(10.0f, 0.1f, 1.0f, 1.0f); // x = normal, y = depth
-uniform float4 e_weights = float4(1.0f, 1.0f, 1.0f, 1.0f); // x = normal, y = depth
-uniform float4 e_kernel = float4(0.35f, 1.0f, 1.0f, 1.0f); 
+uniform float4 e_weights = float4(0.5f, 0.5f, 1.0f, 1.0f); // x = normal, y = depth
+//uniform float4 e_weights = float4(1.0f, 1.0f, 1.0f, 1.0f); // x = normal, y = depth
+uniform float4 e_kernel = float4(0.5f, 1.0f, 1.0f, 1.0f); 
 
 
@@ -34,5 +34,5 @@
 	nd.w = dot(nc, float3(tex2D(normals, IN.lb.xy)));
 
-	nd.x -= e_barrier.x;
+	nd -= e_barrier.x;
 	nd = step((float4)0.0f, nd);
 
@@ -84,7 +84,8 @@
 	float4 s3 = tex2Dlod(colors, float4(offset + IN.lb.xy * w, 0, 0));
 
-	//float4 sx = tex2D(colors, IN.c.xy);
+	float4 sc = tex2Dlod(colors, float4(IN.c.xy, 0, 0));
 
-	return (s0 + s1 + s2 + s3) * 0.25f;
+	return (s0 + s1 + s2 + s3 + sc) * 0.2f;
+	//return (s0 + s1 + s2 + s3) * 0.25f;
 	//return float4(w);
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/shadow.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/shadow.cg	(revision 2882)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/shadow.cg	(revision 2891)
@@ -1,56 +1,57 @@
-//----------------------------------------------------------------------------------
-// File:   shadow_single_fragment.glsl
-// Author: Rouslan Dimitrov
-// Email:  sdkfeedback@nvidia.com
-// Cascaded shadows maps, single shadow sample
-// Copyright (c) NVIDIA Corporation. All rights reserved.
-//----------------------------------------------------------------------------------
-#version 120
-#extension GL_EXT_texture_array : enable
+struct appdata {
+	float3 Position : POSITION;
+	float3 Normal : NORMAL;
+};
 
-uniform sampler2D tex;
-uniform vec4 far_d;
 
-varying vec4 vPos;
+struct vpconn {
+	float4 Hposition : POSITION;
+	float4 TexCoord0 : TEXCOORD0;
+	float4 TexCoord1 : TEXCOORD1;
+	float4 Color0 : COLOR0;
+};
 
-uniform sampler2DArray stex;
-float shadowCoef()
+
+vpconn main(appdata IN,
+			uniform float4x4 WorldViewProj,
+			uniform float4x4 TexTransform,
+			uniform float3x3 WorldIT,
+			uniform float3 LightVec)
 {
-	int index = 3;
+	vpconn OUT;
 	
-	// find the appropriate depth map to look up in based on the depth of this fragment
-	if(gl_FragCoord.z < far_d.x)
-		index = 0;
-	else if(gl_FragCoord.z < far_d.y)
-		index = 1;
-	else if(gl_FragCoord.z < far_d.z)
-		index = 2;
+	float3 worldNormal = normalize(mul(WorldIT, IN.Normal));
 	
-	// transform this fragment's position from view space to scaled light clip space
-	// such that the xy coordinates are in [0;1]
-	// note there is no need to divide by w for othogonal light sources
-	vec4 shadow_coord = gl_TextureMatrix[index]*vPos;
-
-	shadow_coord.w = shadow_coord.z;
+	float ldotn = max(dot(LightVec, worldNormal), 0.0);
+	OUT.Color0.xyz = ldotn.xxx;
 	
-	// tell glsl in which layer to do the look up
-	shadow_coord.z = float(index);
+	float4 tempPos;
+	tempPos.xyz = IN.Position.xyz;
+	tempPos.w = 1.0;
 	
-	// get the stored depth
-	float shadow_d = texture2DArray(stex, shadow_coord.xyz).x;
+	OUT.TexCoord0 = mul(TexTransform, tempPos);
+	OUT.TexCoord1 = mul(TexTransform, tempPos);
 	
-	// get the difference of the stored depth and the distance of this fragment to the light
-	float diff = shadow_d - shadow_coord.w;
+	OUT.Hposition = mul(WorldViewProj, tempPos);
 	
-	// smoothen the result a bit, to avoid aliasing at shadow contact point
-	return clamp( diff*250.0 + 1.0, 0.0, 1.0);
+	return OUT;
 }
 
-void main()
+
+struct v2f_simple {
+	float4 Hposition : POSITION;
+	float4 TexCoord0 : TEXCOORD0;
+	float4 TexCoord1 : TEXCOORD1;
+	float4 Color0 : COLOR0;
+};
+
+
+float4 main(v2f_simple IN,
+			uniform sampler2D ShadowMap,
+			uniform sampler2D SpotLight) : COLOR
 {
-    const float shadow_ambient = 0.9;
-	vec4 color_tex = texture2D(tex, gl_TexCoord[0].st);
-	float shadow_coef = shadowCoef();
-	float fog = clamp(gl_Fog.scale*(gl_Fog.end + vPos.z), 0.0, 1.0);
-	gl_FragColor = mix(gl_Fog.color, (shadow_ambient * shadow_coef * gl_Color * color_tex + (1.0 - shadow_ambient) * color_tex), fog);
+	float4 shadow = tex2D(ShadowMap, IN.TexCoord0.xy);
+	float4 spotlight = tex2D(SpotLight, IN.TexCoord1.xy);
+	float4 lighting = IN.Color0;
+	return shadow * spotlight * lighting;
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2890)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2891)
@@ -4,5 +4,4 @@
 // Screen Spaced Ambient Occlusion shader
 // based on shader of Alexander Kusternig
-
 
 
@@ -22,5 +21,5 @@
 
 
-float2 reflect(float2 pt, float2 n)
+float2 myreflect(float2 pt, float2 n)
 {
 	// distance to plane
@@ -57,5 +56,6 @@
 	for (int i = 0; i < NUM_SAMPLES; ++ i) 
 	{
-		const float2 offset = samples[i];
+		//const float2 offset = samples[i];
+		const float3 offset = float3(samples[i], 0);
 
 		////////////////////
@@ -63,5 +63,6 @@
 
 		//const float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
-		float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
+		//float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
+		float3 mynoise = float3(tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy,0);
 
 		const float2 offsetTransformed = reflect(offset, mynoise);
@@ -123,7 +124,7 @@
 	// expand normal
 	float3 normal = normalize(norm.xyz);
+	
 	/// the current view direction
-	//float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
-	float3 viewDir = normalize(IN.view);
+	float3 viewDir;// = normalize(IN.view);
 
 	// the current world position
@@ -182,6 +183,6 @@
 	float4 ao = tex2D(ssaoTex, IN.texCoord.xy);
 
-	//OUT.illum_col = col * ao.x;
-	OUT.illum_col = ao;
+	OUT.illum_col = col * ao.x;
+	//OUT.illum_col = ao;
 
 	OUT.illum_col.w = ao.w;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2887)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2891)
@@ -29,5 +29,5 @@
 
 
-float2 reflect(float2 pt, float2 n)
+float2 myreflect(float2 pt, float2 n)
 {
 	// distance to plane
@@ -67,9 +67,10 @@
 	for (int i = 0; i < NUM_SAMPLES; i ++) 
 	{
-		float2 offset = samples[i];
+		//float2 offset = samples[i];
+		float3 offset = float3(samples[i], 0);
 
 		//sample noisetex; r stores costheta, g stores sintheta
-		//float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
-		float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
+		//float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
+		float3 mynoise = float3(tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy, 0);
 
 		// rotation
@@ -133,6 +134,7 @@
 	// expand normal
 	float3 normal = normalize(norm.xyz);
+
 	/// the current view direction
-	float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
+	float3 viewDir;// = normalize(IN.view);
 
 	// the current world position
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2890)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.cpp	(revision 2891)
@@ -193,5 +193,6 @@
 mUseTemporalCoherence(true),
 mUseGlobIllum(false),
-mSampling(POISSON)
+mSampling(POISSON),
+mFboIndex(0)
 {
 	// create noise texture for ssao
@@ -201,5 +202,5 @@
 	//-- the flip-flop fbos
 
-	mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
+	/*mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
 	
 	mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
@@ -212,8 +213,12 @@
 	
 	mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-	mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
-	
-	//mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
-	//mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);*/
+	mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);*/
+	
+	mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
+
+	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
 }
 
@@ -227,7 +232,7 @@
 	if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram);
 
-	DEL_PTR(mNewFbo);
-	DEL_PTR(mOldFbo);
-	//DEL_PTR(mFbo);
+	//DEL_PTR(mNewFbo);
+	//DEL_PTR(mOldFbo);
+	DEL_PTR(mFbo);
 
 	glDeleteTextures(1, &noiseTex);
@@ -418,6 +423,6 @@
 	// contais the color buffer from the last frame, 
 	// the other one will be written
-	swap(mNewFbo, mOldFbo);	
-
+	//swap(mNewFbo, mOldFbo);	
+	mFboIndex = 2 - mFboIndex;
 	FrameBufferObject::Release();
 
@@ -492,9 +497,11 @@
 
 	// read the second buffer, write to the first buffer
-	mNewFbo->Bind();
-	glDrawBuffers(1, mymrt);
-
-
-	GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
+	//mNewFbo->Bind();
+	mFbo->Bind();
+	glDrawBuffers(1, mymrt + mFboIndex);
+
+
+	//GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
+	GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
 
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -545,10 +552,5 @@
 	//const float new_offs = 0.55f;
 	const float new_offs = 0.5f;
-	/*
-	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs, -new_offs, -0.5f);
-	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs,  new_offs, -0.5f);
-	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
-*/
+	
 	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
 	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
@@ -715,9 +717,14 @@
 
 	// read the second buffer, write to the first buffer
-	mNewFbo->Bind();
-	glDrawBuffers(2, mymrt);
-
-	GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
-	GLuint oldIllumTex = mOldFbo->GetColorBuffer(1)->GetTexture();
+	//mNewFbo->Bind();
+	mFbo->Bind();
+
+	glDrawBuffers(2, mymrt + mFboIndex);
+
+	//GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
+	//GLuint oldIllumTex = mOldFbo->GetColorBuffer(1)->GetTexture();
+	
+	GLuint oldSsaoTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
+	GLuint oldIllumTex = mFbo->GetColorBuffer(2 - mFboIndex + 1)->GetTexture();
 
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -766,6 +773,4 @@
 	ComputeViewVectors(tl, tr, bl, br);
 
-	glColor3f(1.0f, 1.0f, 1.0f);
-
 	glBegin(GL_QUADS);
 
@@ -773,9 +778,9 @@
 	//const float new_offs = 0.55f;
 	const float new_offs = 0.5f;
-	
-	glColor3f(bl.x, bl.y, bl.z); glTexCoord2f(0, 0); glVertex3f(-new_offs, -new_offs, -0.5f);
-	glColor3f(br.x, br.y, br.z); glTexCoord2f(1, 0); glVertex3f( new_offs, -new_offs, -0.5f);
-	glColor3f(tr.x, tr.y, tr.z); glTexCoord2f(1, 1); glVertex3f( new_offs,  new_offs, -0.5f);
-	glColor3f(tl.x, tl.y, tl.z); glTexCoord2f(0, 1); glVertex3f(-new_offs,  new_offs, -0.5f);
+		
+	glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
+	glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
+	glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs,  new_offs, -0.5f);
+	glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs,  new_offs, -0.5f);
 
 	glEnd();
@@ -798,6 +803,9 @@
 	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
 
-	GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
-	GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
+	//GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
+	//GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
+	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
+	GLuint illumTex = mFbo->GetColorBuffer(mFboIndex + 1)->GetTexture();
+
 
 	fbo->Bind();
@@ -850,5 +858,6 @@
 {
 	GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
-	GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
+	//GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
+	GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
 
 	fbo->Bind();
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp	(revision 2889)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Visualization.cpp	(revision 2891)
@@ -151,4 +151,20 @@
 
 	RenderFrustum();
+
+	Vector3 pos = mCamera->GetPosition();
+
+	// coordinates
+	glColor3f(0.0f, 1.0f, 0.0f);
+	glBegin(GL_LINES);
+	glVertex3d(pos.x, pos.y, pos.z);
+	glVertex3d(pos.x + 100, pos.y, pos.z);
+	glEnd();
+
+	glColor3f(0.0f, 0.0f, 1.0f);
+	glBegin(GL_LINES);
+	glVertex3d(pos.x, pos.y, pos.z);
+	glVertex3d(pos.x, pos.y + 100, pos.z);
+	glEnd();
+
 	//RenderBoxForViz(mBvh->GetBox());
 	
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2891)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.h	(revision 2891)
@@ -0,0 +1,60 @@
+#ifndef _ShadowMapping_H__
+#define _ShadowMapping_H__
+
+#include "common.h"
+#include "glInterface.h"
+#include "AxisAlignedBox3.h"
+#include "Matrix4x4.h"
+#include <Cg/cg.h>
+#include <Cg/cgGL.h>
+
+
+
+namespace CHCDemoEngine 
+{ 
+
+class FrameBufferObject;
+class RenderTraverser;
+class Vector3;
+class Camera;
+class Light;
+
+/** This class implements a shadow mapping algorithm
+*/
+class ShadowMapping
+{
+public:
+	/** constructor for a deferred shader taking the requested shadow size
+	*/
+	ShadowMapping(const AxisAlignedBox3 &sceneBox, int size);
+
+	~ShadowMapping();
+	/** Renders the shadowed scene
+	*/
+	void Render(RenderTraverser *traverser, Camera *cam);
+	/** Initialises shadow mapping and loads the required shaders:
+		This function has to be called only once.
+	*/
+	static void Init(CGcontext context);
+
+//protected:
+
+	void ComputeShadowMap(Light *light, RenderTraverser *traverser);
+
+protected:
+
+	FrameBufferObject *mFbo;
+
+	int mSize;
+
+	AxisAlignedBox3 mSceneBox;
+
+	Camera *mShadowCam;
+
+	Matrix4x4 mShadowMatrix;
+
+	Light *mLight;
+};
+
+} // namespace 
+#endif // _DeferredShader_H__
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h	(revision 2889)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SsaoShader.h	(revision 2891)
@@ -80,7 +80,7 @@
 	Camera *mCamera;
 
-	FrameBufferObject *mOldFbo;
-	FrameBufferObject *mNewFbo;
-	//FrameBufferObject *mFbo;
+	//FrameBufferObject *mOldFbo;
+	//FrameBufferObject *mNewFbo;
+	FrameBufferObject *mFbo;
 
 	bool mUseGlobIllum;
@@ -88,4 +88,5 @@
 
 	int mSampling;
+	int mFboIndex;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2891)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 2891)
@@ -0,0 +1,382 @@
+#include "ShadowMapping.h"
+#include "FrameBufferObject.h"
+#include "RenderState.h"
+#include "RenderTraverser.h"
+#include "Light.h"
+#include <IL/il.h>
+#include <assert.h>
+
+using namespace std;
+
+
+namespace CHCDemoEngine
+{
+
+static GLenum mrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
+
+static void PrintGLerror(char *msg)
+{
+	GLenum errCode;
+	const GLubyte *errStr;
+	
+	if ((errCode = glGetError()) != GL_NO_ERROR) 
+	{
+		errStr = gluErrorString(errCode);
+		fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
+	}
+}
+
+
+
+static CGprogram sCgShadowProgram;
+static CGparameter sShadowParam;
+
+
+static void GrabDepthBuffer(float *data, GLuint depthTexture)
+{
+	glEnable(GL_TEXTURE_2D);
+	glBindTexture(GL_TEXTURE_2D, depthTexture);
+
+	glGetTexImage(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, GL_FLOAT, data);
+
+	glBindTexture(GL_TEXTURE_2D, 0);
+	glDisable(GL_TEXTURE_2D);
+}
+
+
+static void ExportDepthBuffer(float *data, int size)
+{
+	ilInit();
+	assert(ilGetError() == IL_NO_ERROR);
+
+	ILstring filename = ILstring("shadow.tga");
+	ilRegisterType(IL_FLOAT);
+
+	const int depth = 1;
+	const int bpp = 1;
+
+	if (!ilTexImage(size, size, depth, bpp, IL_LUMINANCE, IL_FLOAT, data))
+	{
+		cerr << "IL error " << ilGetError() << endl;
+	
+		ilShutDown();
+		assert(ilGetError() == IL_NO_ERROR);
+
+		return;
+	}
+
+	if (!ilSaveImage(filename))
+	{
+		cerr << "TGA write error " << ilGetError() << endl;
+	}
+
+	ilShutDown();
+	assert(ilGetError() == IL_NO_ERROR);
+
+	cout << "exported depth buffer" << endl;
+}
+
+
+
+ShadowMapping::ShadowMapping(const AxisAlignedBox3 &sceneBox, int size):
+mSceneBox(sceneBox), mSize(size)
+{
+	mFbo = new FrameBufferObject(size, size, FrameBufferObject::DEPTH_32, true);
+	// the diffuse color buffer
+	mFbo->AddColorBuffer(ColorBufferObject::BUFFER_UBYTE, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
+
+	mShadowCam = new Camera(mSceneBox.Size().x * 0.5f, mSceneBox.Size().y * 0.5f);
+	mShadowCam->SetOrtho(true);
+}
+
+
+ShadowMapping::~ShadowMapping()
+{
+	if (sCgShadowProgram)
+		cgDestroyProgram(sCgShadowProgram);
+
+	DEL_PTR(mFbo);
+}
+
+
+void ShadowMapping::Init(CGcontext context)
+{/*
+	sCgShadowProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/shadow.cg", 
+								RenderState::sCgFragmentProfile,
+								"main",
+								NULL);
+
+	if (sCgShadowProgram != NULL)
+	{
+		cgGLLoadProgram(sCgShadowProgram);
+		sShadowParam = cgGetNamedParameter(sCgShadowProgram, "shadowMap");
+	}
+	else
+		cerr << "shadow program failed to load" << endl;
+*/
+	PrintGLerror("init");
+}
+
+
+void ShadowMapping::ComputeShadowMap(Light *light, RenderTraverser *traverser)
+{
+	mLight = light;
+
+	const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f);
+	const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f);
+	
+	mShadowCam->SetDirection(light->GetDirection());
+	
+	// set position so that we can see the whole scene
+	Vector3 pos = mSceneBox.Center();
+	pos.z = mSceneBox.Max().z;
+	//mShadowCam->SetPosition(pos - Magnitude(mSceneBox.Diagonal() * light->GetDirection()));
+	mShadowCam->SetPosition(pos);
+
+	mFbo->Bind();
+
+	glDrawBuffers(1, mrt);
+
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, mSize, mSize);
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	glDisable(GL_LIGHTING);
+	glDisable(GL_TEXTURE_2D);
+	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
+
+	glPolygonOffset(1.0f, 4000.0f);
+	//glEnable(GL_POLYGON_OFFSET_FILL);
+
+	glShadeModel(GL_FLAT);
+	glEnable(GL_DEPTH_TEST);
+
+	glMatrixMode(GL_PROJECTION);
+	glPushMatrix();
+
+	glOrtho(+xlen, -xlen, ylen, -ylen, 0.0f, Magnitude(mSceneBox.Diagonal())); 
+
+	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
+
+	mShadowCam->SetupCameraView();
+
+	mShadowCam->GetModelViewMatrix(mShadowMatrix);
+
+	traverser->RenderScene();
+	
+	glDisable(GL_POLYGON_OFFSET_FILL);
+
+	glPopMatrix();
+
+	glMatrixMode(GL_PROJECTION);
+	glPopMatrix();
+
+	glPopAttrib();
+	
+	/*float *data = new float[mSize * mSize];
+
+	GrabDepthBuffer(data, mFbo->GetDepthTex());
+	ExportDepthBuffer(data, mSize);
+
+	delete [] data;*/
+
+	FrameBufferObject::Release();
+}
+
+/*
+void ShadowMapping::Render(RenderTraverser *traverser, Camera *cam)
+{
+	Matrix4x4 cam_proj;
+	Matrix4x4 cam_inverse_modelview;
+
+	const float bias[16] = 
+	{0.5f, 0.0f, 0.0f, 0.0f, 
+	0.0f, 0.5f, 0.0f, 0.0f,
+	0.0f, 0.0f, 0.5f, 0.0f,
+	0.5f, 0.5f, 0.5f, 1.0f	};
+
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	// update the camera, so that the user can have a free look
+	glMatrixMode(GL_MODELVIEW);
+	glLoadIdentity();
+	
+	glMatrixMode(GL_PROJECTION);
+	glLoadIdentity();
+	
+	cam->SetupCameraView();
+
+	// store the inverse of the resulting modelview matrix for the shadow computation
+	glGetFloatv(GL_MODELVIEW_MATRIX, (float *)cam_inverse_modelview.x);
+	cam_inverse_modelview.Invert();
+
+	// bind shadow map
+	glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, mFbo->GetDepthTex());
+
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
+	//glTexParameteri(GL_TEXTURE_2D_ARRAY_EXT, GL_TEXTURE_COMPARE_MODE, GL_NONE);
+
+	PrintGLerror("texture");
+
+	//float light_m[16];
+	
+	// compute a matrix that transforms from camera eye space to light clip space
+	// and pass it to the shader through the OpenGL texture matrices, since we
+	// don't use them now
+	glEnable(GL_TEXTURE_2D);
+	glMatrixMode(GL_TEXTURE);
+
+	glLoadMatrixf(bias);
+	glMultMatrixf((float *)mShadowMatrix.x);
+	
+	// multiply the light's (bias * crop * proj * modelview) by the inverse camera modelview
+	// so that we can transform a pixel as seen from the camera
+	glMultMatrixf((float *)cam_inverse_modelview.x);
+	
+		
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+	cgGLBindProgram(sCgShadowProgram);
+	
+	cgGLSetTextureParameter(sShadowParam, mFbo->GetDepthTex());
+	cgGLEnableTextureParameter(sShadowParam);
+	
+	// finally, draw the scene 
+	traverser->RenderScene();
+
+	cgGLDisableTextureParameter(sShadowParam);
+
+	PrintGLerror("shadow");
+}
+*/
+
+
+//Called to draw scene
+void ShadowMapping::Render(RenderTraverser *traverser, Camera *cam)
+{
+	Matrix4x4 cam_inverse_modelview;
+
+	//Calculate texture matrix for projection
+	//This matrix takes us from eye space to the light's clip space
+	//It is postmultiplied by the inverse of the current view matrix when specifying texgen
+	static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
+								0.0f, 0.5f, 0.0f, 0.0f,
+								0.0f, 0.0f, 0.5f, 0.0f,
+								0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1]
+
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	// update the camera, so that the user can have a free look
+	glMatrixMode(GL_MODELVIEW);
+	glLoadIdentity();
+	
+	glMatrixMode(GL_PROJECTION);
+	glLoadIdentity();
+	
+	cam->SetupCameraView();
+
+	// store the inverse of the resulting modelview matrix for the shadow computation
+	glGetFloatv(GL_MODELVIEW_MATRIX, (float *)cam_inverse_modelview.x);
+	cam_inverse_modelview.Invert();
+
+	// bind shadow map
+	glBindTexture(GL_TEXTURE_2D_ARRAY_EXT, mFbo->GetDepthTex());
+
+	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
+	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
+
+	PrintGLerror("texture");
+	
+	// compute a matrix that transforms from camera eye space to light clip space
+	// and pass it to the shader through the OpenGL texture matrices, since we
+	// don't use them now
+	glEnable(GL_TEXTURE_2D);
+	glMatrixMode(GL_TEXTURE);
+
+	glLoadMatrixf((float *)biasMatrix.x);
+	glMultMatrixf((float *)mShadowMatrix.x);
+	
+	// multiply the light's (bias * crop * proj * modelview) by the inverse camera modelview
+	// so that we can transform a pixel as seen from the camera
+	glMultMatrixf((float *)cam_inverse_modelview.x);
+
+	RgbaColor white(1, 1, 1, 0);
+	RgbaColor dark(0.2, 0.2, 0.2, 0);
+
+	//Use dim light to represent shadowed areas
+	glLightfv(GL_LIGHT1, GL_POSITION, mLight->GetDirection());
+	glLightfv(GL_LIGHT1, GL_AMBIENT, (float *)&dark.r);
+	glLightfv(GL_LIGHT1, GL_DIFFUSE, (float *)&white.r);
+	//glLightfv(GL_LIGHT1, GL_SPECULAR, black);
+	glEnable(GL_LIGHT1);
+	glEnable(GL_LIGHTING);
+
+	
+	// finally, draw the scene 
+	traverser->RenderScene();
+
+	
+
+	//3rd pass
+	//Draw with bright light
+	glLightfv(GL_LIGHT1, GL_DIFFUSE, (float *)&white.r);
+	glLightfv(GL_LIGHT1, GL_SPECULAR, (float *)&white.r);
+
+	Matrix4x4 textureMatrix;// = biasMatrix * mLightProjectionMatrix * mLightViewMatrix;
+
+	//Set up texture coordinate generation.
+	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
+	//glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix.GetRow(0));
+	glEnable(GL_TEXTURE_GEN_S);
+
+	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
+	//glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix.GetRow(1));
+	glEnable(GL_TEXTURE_GEN_T);
+
+	glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
+	//glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix.GetRow(2));
+	glEnable(GL_TEXTURE_GEN_R);
+
+	glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
+	//glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix.GetRow(3));
+	glEnable(GL_TEXTURE_GEN_Q);
+
+	//Bind & enable shadow map texture
+	glBindTexture(GL_TEXTURE_2D, mFbo->GetDepthTex());
+	glEnable(GL_TEXTURE_2D);
+
+	//Enable shadow comparison
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE);
+
+	//Shadow comparison should be true (ie not in shadow) if r<=texture
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);
+
+	//Shadow comparison should generate an INTENSITY result
+	glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);
+
+	//Set alpha test to discard false comparisons
+	glAlphaFunc(GL_GEQUAL, 0.99f);
+	glEnable(GL_ALPHA_TEST);
+
+	traverser->RenderScene();
+
+	//Disable textures and texgen
+	glDisable(GL_TEXTURE_2D);
+
+	glDisable(GL_TEXTURE_GEN_S);
+	glDisable(GL_TEXTURE_GEN_T);
+	glDisable(GL_TEXTURE_GEN_R);
+	glDisable(GL_TEXTURE_GEN_Q);
+
+	//Restore other states
+	glDisable(GL_LIGHTING);
+	glDisable(GL_ALPHA_TEST);
+}
+
+
+} // namespace
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2889)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2891)
@@ -550,6 +550,6 @@
 
 	// the diffuse color buffer
-	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_MIPMAP_LINEAR, true);
-	//fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
+	//fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_MIPMAP_LINEAR, true);
+	fbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_NEAREST, false);
 
 	// the positions buffer
@@ -1544,16 +1544,5 @@
 	visualization->Render();
 
-	// coordinates
-	glColor3f(0.0f, 1.0f, 0.0f);
-	glBegin(GL_LINES);
-	glVertex3d(pos.x, pos.y, pos.z);
-	glVertex3d(pos.x + 100, pos.y, pos.z);
-	glEnd();
-
-	glColor3f(0.0f, 0.0f, 1.0f);
-	glBegin(GL_LINES);
-	glVertex3d(pos.x, pos.y, pos.z);
-	glVertex3d(pos.x, pos.y + 100, pos.z);
-	glEnd();
+	
 	
 	// reset previous settings
@@ -1564,46 +1553,4 @@
 	glMatrixMode(GL_MODELVIEW);
 	glPopMatrix();
-
-	Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
-
-	camera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
-
-	Vector3 bl, br, tl, tr;
-
-	bl = Normalize(nbl - fbl) * 100;
-	br = Normalize(nbr - fbr) * 100;
-	tl = Normalize(ntl - ftl) * 100;
-	tr = Normalize(ntr - ftr) * 100;
-
-	Vector3 dum = pos + camera->GetDirection() * 20;
-
-	// coordinates
-	glColor3f(0.0f, 1.0f, 0.0f);
-
-	glBegin(GL_LINES);
-	glVertex3d(dum.x, dum.y, dum.z);
-	glVertex3d(dum.x + bl.x, dum.y + bl.y, dum.z + bl.z);
-	glEnd();
-
-/*	glColor3f(1.0f, 0.0f, 0.0f);
-
-	glBegin(GL_LINES);
-	glVertex3d(dum.x, dum.y, dum.z);
-	glVertex3d(dum.x + br.x, dum.y + br.y, dum.z + br.z);
-	glEnd();
-
-*/	glColor3f(0.0f, 0.0f, 1.0f);
-
-	glBegin(GL_LINES);
-	glVertex3d(dum.x, dum.y, dum.z);
-	glVertex3d(dum.x + tl.x, dum.y + tl.y, dum.z + tl.z);
-	glEnd();
-/*
-	glColor3f(1.0f, 0.0f, 1.0f);
-
-	glBegin(GL_LINES);
-	glVertex3d(dum.x, dum.y, dum.z);
-	glVertex3d(dum.x + tr.x, dum.y + tr.y, dum.z + tr.z);
-	glEnd();*/
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Light.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Light.h	(revision 2891)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Light.h	(revision 2891)
@@ -0,0 +1,33 @@
+#ifndef __LIGHT_H
+#define __LIGHT_H
+
+#include "common.h"
+#include "Vector3.h"
+#include "Material.h"
+
+
+namespace CHCDemoEngine 
+{
+
+/** Class representing a directional light source
+*/
+class Light
+{
+public:
+	
+	Light(const Vector3 &dir, const RgbaColor &col): mDirection(dir), mColor(col) {}
+
+	Vector3 GetDirection() const { return mDirection; }
+
+	RgbaColor GetColor() const { return mColor; }
+
+
+protected:
+
+	Vector3 mDirection;
+	RgbaColor mColor;
+};
+
+}
+
+#endif // __LIGHT_H
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2890)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp	(revision 2891)
@@ -175,5 +175,5 @@
 	float z_far = mFar;
 
-	float fov = 120 * M_PI / 180.0f;//mFovy;
+	float fov = mFovy;
 
 	const float w_near = 2.0f * tan(fov * 0.5f) * z_near;
