Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp	(revision 522)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Beam.cpp	(revision 525)
@@ -67,19 +67,31 @@
 
 
-void Beam::ComputeFrustum(float &near, float &far,
-						  float &left, float &right,
+void Beam::ComputeFrustum(float &left, float &right,
 						  float &bottom, float &top,
-						  const AxisAlignedBox3 &sceneBBox)
+						  float &near, float &far,
+						  const AxisAlignedBox3 &sceneBBox) const
 {
 	const float xDirRange = mDirBox.Max().x - mDirBox.Min().x;
 	const float yDirRange = mDirBox.Max().y - mDirBox.Min().y;
 
-	near = 1; // NOTE: what is the best value for near and far plane?
-	far = sceneBBox.Size(sceneBBox.Size().DrivingAxis());
+	near = 0.1; // NOTE: what is the best value for near and far plane?
+	far = 2.0f * Magnitude(sceneBBox.Diagonal());
 
-	left = 1.0f / tan(xDirRange * 0.5);
-	right = 1.0f / tan(xDirRange * 0.5);
+	left = near / tan(xDirRange * 0.5);
+	right = near / tan(xDirRange * 0.5);
 
-	bottom = 1.0f / tan(yDirRange * 0.5);
-	top = 1.0f / tan(yDirRange * 0.5);
+	bottom = near / tan(yDirRange * 0.5);
+	top = near / tan(yDirRange * 0.5);
 }
+
+Vector3 Beam::GetMainDirection() const
+{
+	 Vector3 directions[4];
+	 
+	 directions[0] = VssRay::GetDirection(dBox.Min().x, dBox.Min().y);
+	 directions[1] = VssRay::GetDirection(dBox.Max().x, dBox.Min().y);
+	 directions[2] = VssRay::GetDirection(dBox.Max().x, dBox.Max().y);
+	 directions[3] = VssRay::GetDirection(dBox.Min().x, dBox.Max().y);
+
+	return Normalize(direction[0], direction[1], direction[2], direction[3]);
+}
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/Beam.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/Beam.h	(revision 522)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/Beam.h	(revision 525)
@@ -35,10 +35,12 @@
 	  const AxisAlignedBox3 &dBox);
   
-  void ComputeFrustum(float &near, float &far,
-					  float &left, float &right,
+  void ComputeFrustum(float &left, float &right,
 					  float &bottom, float &top,
-					  const AxisAlignedBox3 &sceneBBox);
+					  float &near, float &far,
+					  const AxisAlignedBox3 &sceneBBox) const;
 
   int ComputeIntersection(const AxisAlignedBox3 &box);
+
+  Vector3 GetMainDirection() const;
 
   bool IsValid() { return mFlags & VALID; }
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/dual_depth.cg
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/dual_depth.cg	(revision 525)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/dual_depth.cg	(revision 525)
@@ -0,0 +1,35 @@
+struct fragment
+{
+	float4 position  : POSITION;	//screen position
+	float4 color0    : COLOR0;
+	float4 color1    : COLOR1;		//lighting
+	float2 texcoord0 : TEXCOORD0;
+	float4 depthTexCoord: TEXCOORD1;
+	float3 normal	 : TEXCOORD2;
+};
+
+
+struct pixel
+{
+	float4 color : COLOR;	
+};
+
+
+pixel main(fragment IN,   
+           uniform sampler2D shadowMap: TEXUNIT1,
+           uniform float doTexturing)
+{
+	pixel OUT;
+	
+	float4 col = IN.color0;
+	
+	// the depth buffer	
+	float4 depth = tex2Dproj(shadowMap, IN.SMTexCoord);
+	float test = IN.depthTexCoord.z / IN.depthTexCoord.w;
+	
+	float shadow;
+	shadow = (test < depth.x) ? 1.0 : 0.0;
+	OUT.color = localcolor * shadow * IN.color1;
+
+	return OUT;
+}
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 514)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.h	(revision 525)
@@ -175,5 +175,7 @@
 private:
 	static void GenQueries(const int numQueries);
-
+	void SetupProjectionForViewPoint(const Vector3 &viewPoint, 
+									 const Beam &beam, 
+									 Intersectable *sourceObject);
 };
 
Index: /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp
===================================================================
--- /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 524)
+++ /trunk/VUT/GtpVisibilityPreprocessor/src/GlRenderer.cpp	(revision 525)
@@ -53,5 +53,5 @@
 static void handleCgError() 
 {
-    fprintf(stderr, "Cg error: %s\n", cgGetErrorString(cgGetError()));
+    Debug << "Cg error: " << cgGetErrorString(cgGetError()) << endl;
     exit(1);
 }
@@ -639,6 +639,9 @@
 {
 	// 1. setup the view port to match the desired samples
+	glViewport(0, 0, desiredSamples, desiredSamples);
+
 	// 2. setup the projection matrix and view matrix to match the viewpoint + beam.mDirBox
-  
+	SetupProjectionForViewPoint(viewPoint, beam, sourceObject);
+	
 	// 3. reset z-buffer to 0 and render the source object for the beam
 	//    with glCullFace(Enabled) and glFrontFace(GL_CW)
@@ -740,7 +743,7 @@
 	{
 		glBeginOcclusionQueryNV(sQueries[queryIdx ++]);
-  
+
 		RenderIntersectable(*vit);
-  
+		
 		glEndOcclusionQueryNV();
 	}
@@ -785,2 +788,19 @@
 	}
 }
+
+void GlRendererBuffer::SetupProjectionForViewPoint(const Vector3 &viewPoint, 
+												 const Beam &beam, 
+												 Intersectable *sourceObject)
+{
+	float left, right, bottom, top, znear, zfar;
+
+	beam.ComputeFrustum(left, right, bottom, top, znear, zfar,
+						mSceneGraph->GetBox());
+
+	glFrustum(left, right, bottom, top, znear, zfar);
+
+    const Vector3 eye = viewPoint + beam.GetMainDirection;
+	const Vector3 up = AbitraryNormal(eye);
+
+	gluLookAt(eye, viewPoint, up);
+}			  
