Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2891)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaderenv.h	(revision 2892)
@@ -9,10 +9,11 @@
 #define SAMPLE_INTENSITY 0.28f
 
+//#define AREA_SIZE 50e-1f
 #define AREA_SIZE 5e-1f
-//#define AREA_SIZE 3e-1f
 
 #define VIEW_CORRECTION_SCALE 0.0f
 //#define VIEW_CORRECTION_SCALE 0.1f
 
+//#define DISTANCE_SCALE 1e-6f
 #define DISTANCE_SCALE 1e-6f
 
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2887)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg	(revision 2892)
@@ -49,5 +49,5 @@
 		   uniform sampler2D colors,
 		   uniform sampler2D positions,
-		   uniform sampler2D normals
+		   uniform sampler2D normals	
 		   )
 {
@@ -72,2 +72,42 @@
 	return OUT;
 }
+
+/** The mrt shader for standard rendering
+*/
+pixel main_shadow(fragment IN, 
+				  uniform sampler2D colors,
+				  uniform sampler2D positions,
+				  uniform sampler2D normals,		   
+				  uniform sampler2D shadowMap,
+				  uniform float4x4 shadowMatrix
+				  )
+{
+	pixel OUT;
+
+	float4 norm = tex2D(normals, IN.texCoord.xy);
+	float4 color = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
+	float4 position = tex2D(positions, IN.texCoord.xy);
+
+	float4 lightSpacePos = mul(shadowMatrix, position);
+
+	float shadowDepth = tex2D(shadowMap, IN.texCoord.xy);
+
+	// an ambient color term
+	float amb = norm.w;
+
+	float3 normal = normalize(norm.xyz);
+	float4 col = shade(IN, color, position, normal, amb);
+	
+	OUT.color = col;
+	
+	if (lightSpacePos.z / lightSpacePos.w < shadowDepth)
+	{
+		OUT.color *= 0.1f;
+	}
+	
+	//OUT.color = float4(lightSpacePos.z / lightSpacePos.w);
+	//OUT.color = float4(shadowDepth);
+	OUT.color.w = color.w;
+
+	return OUT;
+}
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2891)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg	(revision 2892)
@@ -56,6 +56,6 @@
 	for (int i = 0; i < NUM_SAMPLES; ++ i) 
 	{
-		//const float2 offset = samples[i];
-		const float3 offset = float3(samples[i], 0);
+		const float2 offset = samples[i];
+		//const float3 offset = float3(samples[i], 0);
 
 		////////////////////
@@ -63,8 +63,8 @@
 
 		//const float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
-		//float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
-		float3 mynoise = float3(tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy,0);
+		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);
+		const float2 offsetTransformed = myreflect(offset, mynoise);
 
 		// weight with projected coordinate to reach similar kernel size for near and far
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2891)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg	(revision 2892)
@@ -13,5 +13,4 @@
 	float3 view: COLOR0;
 };
-
 
 
@@ -67,13 +66,13 @@
 	for (int i = 0; i < NUM_SAMPLES; i ++) 
 	{
-		//float2 offset = samples[i];
-		float3 offset = float3(samples[i], 0);
+		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;
-		float3 mynoise = float3(tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy, 0);
-
-		// rotation
-		float2 offsetTransformed = reflect(offset, mynoise);
+		float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
+		//float3 mynoise = float3(tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy, 0);
+
+		// reflect sampling using the noise texture
+		float2 offsetTransformed = myreflect(offset, mynoise);
 
 		// weight with projected coordinate to reach similar kernel size for near and far
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp	(revision 2889)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp	(revision 2892)
@@ -166,5 +166,4 @@
 	glPushMatrix();
 
-
 	glOrtho(+xlen, -xlen, ylen, -ylen, 0.0f, mSceneBox.Size().z); 
 
@@ -188,5 +187,5 @@
 
 	GrabDepthBuffer(mDepth, fbo->GetDepthTex());
-	ExportDepthBuffer(mDepth); PrintGLerror("grab");
+	//ExportDepthBuffer(mDepth); PrintGLerror("grab");
 
 	DEL_PTR(fbo);
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 2892)
@@ -42,5 +42,5 @@
 	void ComputeShadowMap(Light *light, RenderTraverser *traverser);
 
-protected:
+//protected:
 
 	FrameBufferObject *mFbo;
@@ -52,5 +52,6 @@
 	Camera *mShadowCam;
 
-	Matrix4x4 mShadowMatrix;
+	Matrix4x4 mLightViewMatrix;
+	Matrix4x4 mLightProjectionMatrix;
 
 	Light *mLight;
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h	(revision 2879)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.h	(revision 2892)
@@ -12,4 +12,6 @@
 
 class FrameBufferObject;
+class ShadowMapping;
+class Matrix4x4;
 
 
@@ -36,8 +38,19 @@
 
 
+	void Render(FrameBufferObject *fbo, 
+ 		        const Matrix4x4 &matViewing, 
+				ShadowMapping *shadowMapping);
+
 protected:
 
 	void FirstPass(FrameBufferObject *fbo);
+	void FirstPassShadow(FrameBufferObject *fbo, 
+						 const Matrix4x4 &matViewing, 
+						 ShadowMapping *shadowMapping);
+
 	void AntiAliasing(FrameBufferObject *fbo);
+
+
+	//////////////////
 
 	int mWidth;
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 2892)
@@ -93,7 +93,5 @@
 ShadowMapping::~ShadowMapping()
 {
-	if (sCgShadowProgram)
-		cgDestroyProgram(sCgShadowProgram);
-
+	//if (sCgShadowProgram)	cgDestroyProgram(sCgShadowProgram);
 	DEL_PTR(mFbo);
 }
@@ -126,6 +124,6 @@
 	mLight = light;
 
-	const float xlen = Magnitude(mSceneBox.Diagonal() * 0.5f);
-	const float ylen = Magnitude(mSceneBox.Diagonal() * 0.5f);
+	const float xlen = Magnitude(mSceneBox.Diagonal());// * 0.5f);
+	const float ylen = Magnitude(mSceneBox.Diagonal());// * 0.5f);
 	
 	mShadowCam->SetDirection(light->GetDirection());
@@ -138,5 +136,5 @@
 
 	mFbo->Bind();
-
+	
 	glDrawBuffers(1, mrt);
 
@@ -150,6 +148,6 @@
 	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
 
-	glPolygonOffset(1.0f, 4000.0f);
-	//glEnable(GL_POLYGON_OFFSET_FILL);
+	glPolygonOffset(1.0f, 40.0f);
+	glEnable(GL_POLYGON_OFFSET_FILL);
 
 	glShadeModel(GL_FLAT);
@@ -158,18 +156,25 @@
 	glMatrixMode(GL_PROJECTION);
 	glPushMatrix();
-
-	glOrtho(+xlen, -xlen, ylen, -ylen, 0.0f, Magnitude(mSceneBox.Diagonal())); 
+	glLoadIdentity();
+	
+	glOrtho(+xlen, -xlen, +ylen, -ylen, 0.0f, 100);//Magnitude(mSceneBox.Diagonal())); 
+	
 
 	glMatrixMode(GL_MODELVIEW);
 	glPushMatrix();
 
+	glLoadIdentity();
+
+
 	mShadowCam->SetupCameraView();
-
-	mShadowCam->GetModelViewMatrix(mShadowMatrix);
+	mShadowCam->GetModelViewMatrix(mLightViewMatrix);
+
+	mShadowCam->GetProjectionMatrix(mLightProjectionMatrix);
+	//glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mLightProjectionMatrix.x);
 
 	traverser->RenderScene();
 	
 	glDisable(GL_POLYGON_OFFSET_FILL);
-
+	glMatrixMode(GL_MODELVIEW);
 	glPopMatrix();
 
@@ -184,5 +189,7 @@
 	ExportDepthBuffer(data, mSize);
 
-	delete [] data;*/
+	delete [] data;
+*/
+	PrintGLerror("shadow map");
 
 	FrameBufferObject::Release();
@@ -270,5 +277,5 @@
 								0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1]
 
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+	/*glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
 	// update the camera, so that the user can have a free look
@@ -280,5 +287,5 @@
 	
 	cam->SetupCameraView();
-
+*/
 	// store the inverse of the resulting modelview matrix for the shadow computation
 	glGetFloatv(GL_MODELVIEW_MATRIX, (float *)cam_inverse_modelview.x);
@@ -287,9 +294,4 @@
 	// 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
@@ -300,5 +302,5 @@
 
 	glLoadMatrixf((float *)biasMatrix.x);
-	glMultMatrixf((float *)mShadowMatrix.x);
+	glMultMatrixf((float *)mLightViewMatrix.x);
 	
 	// multiply the light's (bias * crop * proj * modelview) by the inverse camera modelview
@@ -308,4 +310,9 @@
 	RgbaColor white(1, 1, 1, 0);
 	RgbaColor dark(0.2, 0.2, 0.2, 0);
+
+	glDisable(GL_LIGHTING);
+
+	/*glDisable(GL_LIGHT0);
+	glDisable(GL_LIGHT1);
 
 	//Use dim light to represent shadowed areas
@@ -315,34 +322,39 @@
 	//glLightfv(GL_LIGHT1, GL_SPECULAR, black);
 	glEnable(GL_LIGHT1);
-	glEnable(GL_LIGHTING);
-
-	
+	glEnable(GL_LIGHTING);*/
+
+	PrintGLerror("firstpass");
+
 	// finally, draw the scene 
 	traverser->RenderScene();
 
-	
+
+	glEnable(GL_LIGHTING);
+
 
 	//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;
+	//glLightfv(GL_LIGHT1, GL_DIFFUSE, (float *)&white.r);
+	//glLightfv(GL_LIGHT1, GL_SPECULAR, (float *)&white.r);
+
+	Matrix4x4 textureMatrix = biasMatrix * mLightProjectionMatrix * mLightViewMatrix;
+
+	Matrix4x4 texMatT = Transpose(textureMatrix);
 
 	//Set up texture coordinate generation.
 	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
-	//glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix.GetRow(0));
+	glTexGenfv(GL_S, GL_EYE_PLANE, texMatT.x[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));
+	glTexGenfv(GL_T, GL_EYE_PLANE, texMatT.x[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));
+	glTexGenfv(GL_R, GL_EYE_PLANE, texMatT.x[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));
+	glTexGenfv(GL_Q, GL_EYE_PLANE, texMatT.x[3]);
 	glEnable(GL_TEXTURE_GEN_Q);
 
@@ -363,4 +375,6 @@
 	glAlphaFunc(GL_GEQUAL, 0.99f);
 	glEnable(GL_ALPHA_TEST);
+
+	PrintGLerror("texture");
 
 	traverser->RenderScene();
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2891)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2892)
@@ -94,4 +94,9 @@
 static int winWidth = 1024;
 static int winHeight = 768;
+
+//static int winWidth = 512;
+//static int winHeight = 512;
+
+
 static float winAspectRatio = 1.0f;
 
@@ -107,9 +112,4 @@
 static int texHeight = 768;
 
-//static int texWidth = 512;
-//static int texHeight = 384;
-
-//static int texWidth = 2048;
-//static int texHeight = 2048;
 
 int renderedObjects = 0;
@@ -175,4 +175,5 @@
 
 static Matrix4x4 matProjectionView = IdentityMatrix();
+static Matrix4x4 matViewing = IdentityMatrix();
 
 ShadowMapping *shadowMapping = NULL;
@@ -434,7 +435,4 @@
 	ShadowMapping::Init(sCgContext);
 
-	//deferredShader = new DeferredShader(texWidth, texHeight);
-	//ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f);
-
 
 	// initialize the render traverser
@@ -448,6 +446,5 @@
 
 	shadowMapping = new ShadowMapping(bvh->GetBox(), 512);
-	shadowMapping->ComputeShadowMap(light, traverser);
-
+	
 	// frame time is restarted every frame
 	frameTimer.Start();
@@ -795,15 +792,8 @@
 	camera->SetupCameraView();
 
-	// set lights
-	GLfloat position[] = {0.8f, -1.0f, 0.7f, 0.0f};
-	glLightfv(GL_LIGHT0, GL_POSITION, position);
-
-	GLfloat position1[] = {-0.5f, 0.5f, 0.4f, 0.0f};
-	glLightfv(GL_LIGHT1, GL_POSITION, position1);
-
-	
+		
 	/////////////////
 
-	Matrix4x4 matViewing, matProjection;
+	Matrix4x4 matProjection;
 
 	camera->GetModelViewMatrix(matViewing);
@@ -876,9 +866,4 @@
 	}
 	
-	
-	//shadowMapping->ComputeShadowMap(light, traverser);
-
-
-
 	// render without shading
 	switch (renderType)
@@ -918,4 +903,6 @@
 	case RenderState::DEFERRED:
 
+		shadowMapping->ComputeShadowMap(light, traverser);
+
 		if (!fbo) InitFBO();
 
@@ -958,7 +945,13 @@
 	SetupEyeView();
 
-	// actually render the scene geometry using one of the specified algorithms
+	// set up lights
+	GLfloat position[] = {0.8f, -1.0f, 0.7f, 0.0f};
+	glLightfv(GL_LIGHT0, GL_POSITION, position);
+
+	GLfloat position1[] = {-0.5f, 0.5f, 0.4f, 0.0f};
+	glLightfv(GL_LIGHT1, GL_POSITION, position1);
+
+	// actually render the scene geometry using the specified algorithm
 	traverser->RenderScene();
-
 
 
@@ -999,5 +992,4 @@
 		{
 			if (!ssaoShader) ssaoShader = new SsaoShader(texWidth, texHeight, camera, myfar / 10.0f);
-			//DEL_PTR(deferredShader);
 			ssaoShader->Render(fbo, oldViewProjMatrix, ssaoExpFactor);
 
@@ -1006,15 +998,10 @@
 		{
 			if (!deferredShader) deferredShader = new DeferredShader(texWidth, texHeight);
-			//DEL_PTR(ssaoShader);
-			deferredShader->Render(fbo);
+			
+			//deferredShader->Render(fbo);
+			deferredShader->Render(fbo, matViewing, shadowMapping);
 		}
 	}
-	else
-	{
-		/*DEL_PTR(fbo);
-		DEL_PTR(ssaoShader);
-		DEL_PTR(deferredShader);
-		*/
-	}
+	
 	
 	///////////
@@ -1498,11 +1485,9 @@
 	const float offs = box.Size().x * 0.3f;
 	
-	//Vector3 vizpos = Vector3(box.Min().x, box.Min().y + box.Size().y * 0.5f, box.Min().z + box.Size().z * 50);
 	Vector3 vizpos = Vector3(box.Min().x, box.Min().y  - box.Size().y * 0.35f, box.Min().z + box.Size().z * 50);
 	
 	visCamera->SetPosition(vizpos);
 	visCamera->ResetPitchAndYaw();
-	//visCamera->Pitch(M_PI);
-
+	
 	glPushAttrib(GL_VIEWPORT_BIT);
 	glViewport(winWidth - winWidth / 3, winHeight - winHeight / 3, winWidth / 3, winHeight / 3);
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2879)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp	(revision 2892)
@@ -2,4 +2,5 @@
 #include "FrameBufferObject.h"
 #include "RenderState.h"
+#include "ShadowMapping.h"
 
 
@@ -28,4 +29,5 @@
 
 static CGprogram sCgDeferredProgram;
+static CGprogram sCgDeferredShadowProgram;
 static CGprogram sCgAntiAliasingProgram;
 
@@ -37,4 +39,13 @@
 static CGparameter sColorsTexAntiAliasingParam;
 static CGparameter sNormalsTexAntiAliasingParam;
+
+static CGparameter sShadowMapParam;
+static CGparameter sPositionsTexShadowParam;  
+static CGparameter sColorsTexShadowParam;  
+static CGparameter sNormalsTexShadowParam;
+
+static CGparameter sShadowMatrixParam;
+
+
 
 
@@ -96,4 +107,26 @@
 		cerr << "antialiasing program failed to load" << endl;
 
+	sCgDeferredShadowProgram = 
+		cgCreateProgramFromFile(context, 
+								CG_SOURCE,
+								"src/shaders/deferred.cg", 
+								RenderState::sCgFragmentProfile,
+								"main_shadow",
+								NULL);
+
+	if (sCgDeferredShadowProgram != NULL)
+	{
+		cgGLLoadProgram(sCgDeferredShadowProgram);
+
+		// we need size of texture for scaling
+		sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions");  
+		sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors");  
+		sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals"); 
+		sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");  
+
+		sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
+	}
+	else
+		cerr << "deferred program failed to load" << endl;
 
 	PrintGLerror("init");
@@ -103,6 +136,82 @@
 void DeferredShader::Render(FrameBufferObject *fbo)
 {
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+
+	glDisable(GL_ALPHA_TEST);
+	glDisable(GL_TEXTURE_2D);
+	glDisable(GL_LIGHTING);
+
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, mWidth, mHeight);
+
+	glMatrixMode(GL_PROJECTION);
+	glPushMatrix();
+	glLoadIdentity();
+
+	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
+	glLoadIdentity();
+
+	const float offs = 0.5f;
+	glOrtho(-offs, offs, -offs, offs, 0, 1);
+
+
 	FirstPass(fbo);
 	AntiAliasing(fbo);
+
+	glEnable(GL_LIGHTING);
+	glDisable(GL_TEXTURE_2D);
+	
+	glMatrixMode(GL_PROJECTION);
+	glPopMatrix();
+
+	glMatrixMode(GL_MODELVIEW);
+	glPopMatrix();
+
+	glPopAttrib();
+
+	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+}
+
+
+void DeferredShader::Render(FrameBufferObject *fbo, 
+							const Matrix4x4 &matViewing, 
+							ShadowMapping *shadowMapping)
+{
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+
+	glDisable(GL_ALPHA_TEST);
+	glDisable(GL_TEXTURE_2D);
+	glDisable(GL_LIGHTING);
+
+	glPushAttrib(GL_VIEWPORT_BIT);
+	glViewport(0, 0, mWidth, mHeight);
+
+	glMatrixMode(GL_PROJECTION);
+	glPushMatrix();
+	glLoadIdentity();
+
+	glMatrixMode(GL_MODELVIEW);
+	glPushMatrix();
+	glLoadIdentity();
+
+	const float offs = 0.5f;
+	glOrtho(-offs, offs, -offs, offs, 0, 1);
+
+	FirstPassShadow(fbo, matViewing, shadowMapping);
+	AntiAliasing(fbo);
+
+	glEnable(GL_LIGHTING);
+	glDisable(GL_TEXTURE_2D);
+	
+	glMatrixMode(GL_PROJECTION);
+	glPopMatrix();
+
+	glMatrixMode(GL_MODELVIEW);
+	glPopMatrix();
+
+	glPopAttrib();
+
+	cgGLDisableProfile(RenderState::sCgFragmentProfile);
 }
 
@@ -117,27 +226,7 @@
 	glDrawBuffers(1, mymrt + 3);
 
-
-	glPushAttrib(GL_VIEWPORT_BIT);
-	glViewport(0, 0, mWidth, mHeight);
-
-	glDisable(GL_ALPHA_TEST);
-	glDisable(GL_TEXTURE_2D);
-	glDisable(GL_LIGHTING);
-	
-	glMatrixMode(GL_PROJECTION);
-	glPushMatrix();
-	glLoadIdentity();
-
-	glMatrixMode(GL_MODELVIEW);
-	glPushMatrix();
-	glLoadIdentity();
-
-	const float offs = 0.5f;
-	
-	glOrtho(-offs, offs, -offs, offs, 0, 1);
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
 	cgGLEnableProfile(RenderState::sCgFragmentProfile);
-
 	cgGLBindProgram(sCgDeferredProgram);
 
@@ -170,15 +259,4 @@
 	cgGLDisableProfile(RenderState::sCgFragmentProfile);
 	
-	glEnable(GL_LIGHTING);
-	glDisable(GL_TEXTURE_2D);
-	
-	glMatrixMode(GL_PROJECTION);
-	glPopMatrix();
-
-	glMatrixMode(GL_MODELVIEW);
-	glPopMatrix();
-
-	glPopAttrib();
-
 	FrameBufferObject::Release();
 
@@ -207,22 +285,5 @@
 	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
 
-	glPushAttrib(GL_VIEWPORT_BIT);
-	glViewport(0, 0, mWidth, mHeight);
-
-	glDisable(GL_ALPHA_TEST);
-	glDisable(GL_TEXTURE_2D);
-	glDisable(GL_LIGHTING);
-	
-	glMatrixMode(GL_PROJECTION);
-	glPushMatrix();
-	glLoadIdentity();
-
-	glMatrixMode(GL_MODELVIEW);
-	glPushMatrix();
-	glLoadIdentity();
-
-	const float offs = 0.5f;
-	
-	glOrtho(-offs, offs, -offs, offs, 0, 1);
+	
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
@@ -258,15 +319,93 @@
 
 	cgGLDisableProfile(RenderState::sCgFragmentProfile);
-	
-	glEnable(GL_LIGHTING);
-	glDisable(GL_TEXTURE_2D);
-	
-	glMatrixMode(GL_PROJECTION);
-	glPopMatrix();
-
-	glMatrixMode(GL_MODELVIEW);
-	glPopMatrix();
-
-	glPopAttrib();
+
+	PrintGLerror("antialiasing");
+}
+
+
+void DeferredShader::FirstPassShadow(FrameBufferObject *fbo, 
+									 const Matrix4x4 &matViewing, 
+									 ShadowMapping *shadowMapping)
+{
+	GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
+	GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
+	GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
+	GLuint shadowMap = shadowMapping->mFbo->GetDepthTex();
+
+	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]
+
+	Matrix4x4 inverseView = matViewing;
+	inverseView.Invert();
+
+	Matrix4x4 lightProjView = shadowMapping->mLightViewMatrix * shadowMapping->mLightProjectionMatrix;
+
+	//cout << "matview:\n" << matViewing << endl;
+	//cout << "proj:\n" <<  shadowMapping->mLightProjectionMatrix << endl;
+	//cout << "view:\n" <<  shadowMapping->mLightViewMatrix << endl;
+
+	// compute combined matrix that transforms pixels into light texture space
+	/*Matrix4x4 shadowMatrix = biasMatrix * 
+							 shadowMapping->mLightProjectionMatrix * 
+							 shadowMapping->mLightViewMatrix *
+							 inverseView;
+*/
+	Matrix4x4 shadowMatrix = //inverseView * 
+							 lightProjView * 
+							 biasMatrix;
+/*
+Matrix4x4 shadowMatrix = biasMatrix * 
+							 lightProjView; 
+							 //inverseView;
+*/
+							 cout << "combined:\n" <<  shadowMatrix << endl;
+
+	fbo->Bind();
+	glDrawBuffers(1, mymrt + 3);
+
+	
+	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+	cgGLEnableProfile(RenderState::sCgFragmentProfile);
+
+	cgGLBindProgram(sCgDeferredShadowProgram);
+
+	cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
+	cgGLEnableTextureParameter(sColorsTexShadowParam);
+
+	cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
+	cgGLEnableTextureParameter(sPositionsTexShadowParam);
+
+	cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
+	cgGLEnableTextureParameter(sNormalsTexShadowParam);
+	
+	cgGLSetTextureParameter(sShadowMapParam, shadowMap);
+	cgGLEnableTextureParameter(sShadowMapParam);
+
+	cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
+
+	glColor3f(1.0f, 1.0f, 1.0f);
+	
+	glBegin(GL_QUADS);
+
+	float offs2 = 0.5f;
+
+	glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
+	glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
+	glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
+	glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
+
+	glEnd();
+
+	cgGLDisableTextureParameter(sColorsTexShadowParam);
+	cgGLDisableTextureParameter(sPositionsTexShadowParam);
+	cgGLDisableTextureParameter(sNormalsTexShadowParam);
+	cgGLDisableTextureParameter(sShadowMapParam);
+
+	cgGLDisableProfile(RenderState::sCgFragmentProfile);
+	
+	FrameBufferObject::Release();
 
 	PrintGLerror("deferred shading");
@@ -274,3 +413,4 @@
 
 
+
 } // namespace
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RndGauss.h
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RndGauss.h	(revision 2886)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RndGauss.h	(revision 2892)
@@ -43,5 +43,5 @@
     xf = 1e-20f;
   assert( xf <= 1.0f);
-  assert( (input.yy >= 0.f) && (input.yy <= 1.0f));
+  assert( (input.y >= 0.f) && (input.y <= 1.0f));
 
   // Here we have to use natural logarithm function !
Index: /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env
===================================================================
--- /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 2888)
+++ /GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env	(revision 2892)
@@ -10,6 +10,5 @@
 winHeight=768
 camPosition=483.398f 242.364f 186.078f
-camDirection=0 1 0
-//camDirection=1 0 0
+camDirection=1 0 0
 useFullScreen=0
 useLODs=1
