Index: GTP/trunk/App/Demos/Vis/CHC_revisited/SceneQuery.h
===================================================================
--- GTP/trunk/App/Demos/Vis/CHC_revisited/SceneQuery.h	(revision 2776)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/SceneQuery.h	(revision 2778)
@@ -8,4 +8,7 @@
 {
 
+class RenderTraverser;
+
+
 /** A simple class that computes the first intersection of a horizontal ray with
 	the scene. Can be used to "drop" objects in the scene.
@@ -15,11 +18,12 @@
 public:
 
-	SceneQuery(const AxisAlignedBox3 &sceneBox);
+	SceneQuery(const AxisAlignedBox3 &sceneBox, RenderTraverser *traverser);
 
 	bool CalcIntersection(Vector3 &pt);
 
+
 protected:
 
-	void Prepare();
+	void Prepare(RenderTraverser *traverser);
 
 
Index: GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.h
===================================================================
--- GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.h	(revision 2776)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.h	(revision 2778)
@@ -20,5 +20,4 @@
 	{
 		/// the 6 clip planes
-		//float mClipPlane[6][4];
 		Plane3 mClipPlanes[6];
 
@@ -30,18 +29,11 @@
 	Camera(int width, int height, float fieldOfView = 90.f);
 
-	void Precompute();
-
-	void LookInBox(const AxisAlignedBox3 &box);
-
-	void LookAtBox(const AxisAlignedBox3 &box);
 
 	void SetPosition(const Vector3 &pos);
 
-	void SetDirection(const Vector3 &dir);
-
 	inline Vector3 GetPosition() const { return mPosition; }
-	inline Vector3 GetDirection() const { return mDirection; }
-	inline Vector3 GetUpVector() const { return mUp; }
-	inline Vector3 GetRightVector() const { return mRight; }
+	Vector3 GetDirection() const;
+	Vector3 GetUpVector() const;
+	Vector3 GetRightVector() const;
 
 	inline float GetFov() const { return mFovy; }
@@ -60,17 +52,26 @@
 	inline float GetNear() const { return mNear; }
 	void SetNear(float nearDist);
+	
+	inline float GetFar() const { return mFar; }
+
+	void SetFar(float farDist) { mFar = farDist; }
+
+	void SetOrtho(bool ortho);
+
+	void Yaw(float angle);
+	void Pitch(float angle);
+	/** Sets up viewing in gl
+	*/
+	void Render();
 
 
 protected:
 
+	void Precompute();
+
+	void CalculateFromPitchAndYaw();
+
 	////////////////
 
-	Vector3 mPosition;
-	Vector3 mDirection;
-
-	/// up vector takes into account the FOV at a unit distance from the origin
-	Vector3 mUp;
-	/// right vector takes into account the FOV at a unit distance from the origin
-	Vector3 mRight;
 
 	float mFovy;
@@ -79,4 +80,16 @@
 
 	float mNear;
+
+	float mFar;
+
+	bool mIsOrtho;
+
+	Matrix4x4 mBaseOrientation;
+	Matrix4x4 mViewOrientation;
+
+	float mPitch;
+	float mYaw;
+
+	Vector3 mPosition;
 };
 
Index: GTP/trunk/App/Demos/Vis/CHC_revisited/SceneQuery.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/CHC_revisited/SceneQuery.cpp	(revision 2776)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/SceneQuery.cpp	(revision 2778)
@@ -3,4 +3,6 @@
 #include "Vector3.h"
 #include "Camera.h"
+#include "SceneQuery.h"
+#include "RenderTraverser.h"
 
 
@@ -20,8 +22,8 @@
 
 
-SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox): 
+SceneQuery::SceneQuery(const AxisAlignedBox3 &sceneBox, RenderTraverser *renderer): 
 mSceneBox(sceneBox)
 {
-	Prepare();
+	Prepare(renderer);
 }
 
@@ -45,5 +47,6 @@
 }
 
-void SceneQuery::Prepare()
+
+void SceneQuery::Prepare(RenderTraverser *renderer)
 {
 	cout << "Preparing scene queries" << endl;
@@ -56,14 +59,9 @@
 	pos.z = mSceneBox.Center(2);
 
-	Camera *orthoCam = new Camera();
-	//orthoCam->SetOrtho(true);
-
-	// contain match scene bounding box;
-
 	// for ortho cam this is the width
 	const Vector3 len = mSceneBox.Size();
-/*
-	orthoCam->SetFov(len.x);
-	orthoCam->SetAspect(len.x / len.y);
+
+	Camera *orthoCam = new Camera(len.x, len.y);
+	orthoCam->SetOrtho(true);
 
 	orthoCam->SetNear(0.0f);
@@ -72,28 +70,25 @@
 	orthoCam->SetPosition(pos);
 
-	OUT1("fov: " << orthoCam->GetFov());
-	OUT1("near: " << orthoCam->GetNear());
-	OUT1("far: " << orthoCam->GetFar());
-	OUT1("aspect: " << orthoCam->GetAspect());
+	cout << "fov: " << orthoCam->GetFov() << endl;
+	cout << "near: " << orthoCam->GetNear() << endl;
+	cout << "far: " << orthoCam->GetFar() << endl;
+	cout << "aspect: " << orthoCam->GetAspect() << endl;
 
-	OUT1("pos: " << orthoCam->GetPosition());
-	OUT1("view: " << orthoCam->GetViewDirection());
-	OUT1("up: " << orthoCam->GetViewUpVector());
+	cout << "pos: " << orthoCam->GetPosition() << endl;
+	cout << "view: " << orthoCam->GetDirection() << endl;
+	cout << "up: " << orthoCam->GetUpVector() << endl;
 
-	const float pitch = 0;
-	const float yaw = YA_PI * 0.5f;
+	//orthoCam->SetDirection(Vector3(0, 1, 0));
+	orthoCam->Yaw(M_PI * 0.5f);
 	
-	OffscreenRenderArea *of = new OffscreenRenderArea();
+	renderer->SetCamera(orthoCam);
 
-	of->Init(viewport[2], viewport[3]);
-	of->SetRenderAction(new GLRenderAction);
-	
-	of->SetCamera(orthoCam);
-	of->SetSceneRoot(mSceneRoot);
-	
-	of->Render();
+	//OffscreenRenderArea *of = new OffscreenRenderArea();
+	//of->Init(viewport[2], viewport[3]);
 
-	sDepth = of->GetDepthFloat();
-	*/
+	renderer->RenderScene();
+	//sDepth = of->GetDepthFloat();
+
+	delete orthoCam;
 }
 
Index: GTP/trunk/App/Demos/Vis/CHC_revisited/chc_revisited.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/CHC_revisited/chc_revisited.vcproj	(revision 2776)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/chc_revisited.vcproj	(revision 2778)
@@ -190,5 +190,5 @@
 	<Files>
 		<Filter
-			Name="utils"
+			Name="util"
 			>
 			<File
Index: GTP/trunk/App/Demos/Vis/CHC_revisited/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/CHC_revisited/chcdemo.cpp	(revision 2776)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/chcdemo.cpp	(revision 2778)
@@ -163,5 +163,6 @@
 	//camera->LookInBox(bvh->GetBox());
 
-	camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f));
+	//camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f));
+	//camera->SetDirection(Vector3(0.0f, 1.0f, 0.0f));
 	camera->SetPosition(Vector3(483.398f, 242.364f, 186.078f));
 
@@ -488,9 +489,5 @@
 	case 'A':
 		{
-			Vector3 viewDir = camera->GetDirection();
-			// rotate view vector
-			Matrix4x4 rot = RotationZMatrix(keyRotation);
-			viewDir = rot * viewDir;
-			camera->SetDirection(viewDir);
+			camera->Pitch(keyRotation);
 		}
 		break;
@@ -498,13 +495,17 @@
 	case 'D':
 	{
-			Vector3 viewDir = camera->GetDirection();
-			// rotate view vector
-			Matrix4x4 rot = RotationZMatrix(-keyRotation);
-			viewDir = rot * viewDir;
-			camera->SetDirection(viewDir);
+			camera->Pitch(-keyRotation);
 		}
 		break;
 	case 'w':
 	case 'W':
+		{
+			Vector3 pos = camera->GetPosition();
+			pos -= hvec * keyForwardMotion;
+			camera->SetPosition(pos);
+		}
+		break;
+	case 'x':
+	case 'X':
 		{
 			Vector3 pos = camera->GetPosition();
@@ -513,12 +514,4 @@
 		}
 		break;
-	case 'x':
-	case 'X':
-		{
-			Vector3 pos = camera->GetPosition();
-			pos -= hvec * keyForwardMotion;
-			camera->SetPosition(pos);
-		}
-		break;
 	case 'r':
 	case 'R':
@@ -547,31 +540,23 @@
 	case GLUT_KEY_LEFT:
 		{
-			Vector3 viewDir = camera->GetDirection();
-			// rotate view vector
-			Matrix4x4 rot = RotationZMatrix(keyRotation);
-			viewDir = rot * viewDir;
-			camera->SetDirection(viewDir);
+			camera->Pitch(keyRotation);
 		}
 		break;
 	case GLUT_KEY_RIGHT:
 		{
-			Vector3 viewDir = camera->GetDirection();
-			// rotate view vector
-			Matrix4x4 rot = RotationZMatrix(-keyRotation);
-			viewDir = rot * viewDir;
-			camera->SetDirection(viewDir);
+			camera->Pitch(-keyRotation);
 		}
 		break;
 	case GLUT_KEY_UP:
+		{
+			Vector3 pos = camera->GetPosition();
+			pos -= hvec * 0.6f;
+			camera->SetPosition(pos);
+		}
+		break;
+	case GLUT_KEY_DOWN:
 		{
 			Vector3 pos = camera->GetPosition();
 			pos += hvec * 0.6f;
-			camera->SetPosition(pos);
-		}
-		break;
-	case GLUT_KEY_DOWN:
-		{
-			Vector3 pos = camera->GetPosition();
-			pos -= hvec * 0.6f;
 			camera->SetPosition(pos);
 		}
@@ -641,6 +626,4 @@
 void leftMotion(int x, int y) 
 {
-	static float eyeXAngle = 0.0f;
-
 	Vector3 viewDir = camera->GetDirection();
 	Vector3 pos = camera->GetPosition();
@@ -649,13 +632,10 @@
 	Vector3 horView(viewDir[0], viewDir[1], 0);
 	
-	eyeXAngle = 0.2f *  M_PI * (xEyeBegin - x) / 180.0;
-
-	// rotate view vector
-	Matrix4x4 rot = RotationZMatrix(eyeXAngle);
-	viewDir = rot * viewDir;
-
-	pos += horView * (yMotionBegin - y) * 0.2f;
-
-	camera->SetDirection(viewDir);
+	float eyeXAngle = 0.2f *  M_PI * (xEyeBegin - x) / 180.0;
+
+	camera->Pitch(eyeXAngle);
+
+	pos -= horView * (yMotionBegin - y) * 0.2f;
+	
 	camera->SetPosition(pos);
 	
@@ -672,17 +652,8 @@
 void rightMotion(int x, int y) 
 {
-	static float eyeYAngle = 0.0f;
-
-	Vector3 viewDir = camera->GetDirection();
-	Vector3 right = camera->GetRightVector();
-
-	eyeYAngle = -0.2f *  M_PI * (yEyeBegin - y) / 180.0;
-
-	// rotate view vector
-	Matrix4x4 rot = RotationAxisMatrix(right, eyeYAngle);
-	viewDir = rot * viewDir;
-
-	camera->SetDirection(viewDir);
-		
+	float eyeYAngle = -0.2f *  M_PI * (yEyeBegin - y) / 180.0;
+
+	camera->Yaw(eyeYAngle);
+	
 	yEyeBegin = y;
 	
@@ -706,5 +677,5 @@
 	rVec = rot * rVec;
 	
-	pos -= rVec * (x - horizontalMotionBegin) * 0.1f;
+	pos += rVec * (x - horizontalMotionBegin) * 0.1f;
 	//pos[1] += (verticalMotionBegin - y) * 0.1f;
 	pos[2] += (verticalMotionBegin - y) * 0.1f;
@@ -799,4 +770,5 @@
 	////////////
 	// --- visualization of the occlusion culling
+
 	visualization->Render();
 	
Index: GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp	(revision 2776)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp	(revision 2778)
@@ -7,4 +7,6 @@
 {
 
+using namespace std;
+
 
 Camera::Camera() 
@@ -13,4 +15,10 @@
 	mHeight = 100;
 	mFovy = 90.0f * M_PI / 180.0f;
+	mIsOrtho = false;
+	
+	SetPosition(Vector3(0, 0, 0));
+
+	mPitch = mYaw = 0;
+	Precompute();
 }
 
@@ -22,4 +30,13 @@
 	
 	mFovy = fieldOfView * M_PI / 180.0f;
+
+	mIsOrtho = false;
+
+	SetPosition(Vector3(0, 0, 0));
+
+	SetPosition(Vector3(0, 0, 0));
+
+	mPitch = mYaw = 0;
+	Precompute();
 }
 
@@ -27,17 +44,15 @@
 void Camera::Precompute() 
 {
-	mDirection.Normalize();
-
-	//Vector3 side = CrossProd(Vector3(0, 1, 0), mDirection);
-	Vector3 side = CrossProd(Vector3(0, 0, 1), mDirection);
-	
-	mUp = -Normalize(CrossProd(side, mDirection));
-	mRight = -Normalize(CrossProd(mDirection, mUp));
-
-
-	float k = tan(mFovy/2);
-	mUp *= k;
-	mRight *= k*mWidth/mHeight;
-}
+	Vector3 direction = Vector3(0, 1, 0);
+
+	Vector3 side = CrossProd(Vector3(1, 0, 0), direction);
+	
+	Vector3 up = -Normalize(CrossProd(side, direction));
+	Vector3 right = -Normalize(CrossProd(direction, up));
+
+	mBaseOrientation = Matrix4x4(right, up, direction);
+	mViewOrientation = mBaseOrientation;
+}
+
 
 
@@ -45,12 +60,4 @@
 {
 	mPosition = pos;
-	Precompute();
-}
-
-
-void Camera::SetDirection(const Vector3 &dir) 
-{
-	mDirection = dir;
-	Precompute();
 }
 
@@ -59,24 +66,4 @@
 {
 	mNear = nearDist;
-}
-
-
-void Camera::LookInBox(const AxisAlignedBox3 &box)
-{
-	//mDirection = Vector3(0, 0, 1);
-	mDirection = Vector3(0, 1, 0);
-	mPosition = box.Center();
-	mPosition.z = box.Min(2) + 0.9f * box.Size(2);
-
-	Precompute();
-}
-
-
-void Camera::LookAtBox(const AxisAlignedBox3 &box)
-{
-	mDirection = Vector3(0, box.Min().y - box.Max().y, 0);
-	mPosition = Vector3(0);//box.Max() - mDirection;
-
-	Precompute();
 }
 
@@ -159,9 +146,32 @@
 void Camera::SetupCameraView()
 {
+#if 1
+	//Matrix4x4 viewOrientation(mRight, mDirection, mUp);
+	//Matrix4x4 viewOrientation(mRight, mUp, mDirection);
+	Matrix4x4 tview = mViewOrientation;
+
+	//Vector3 pos = tview * mPosition;
+	Vector3 pos = -mPosition;
+	pos = tview * pos;
+
+	Debug << "vieworient:\n" << mViewOrientation << " pos " << pos << " position " << mPosition << endl;
+
+	Matrix4x4 viewOrientation = mViewOrientation;
+
+	viewOrientation.x[3][0] = pos.x;
+	viewOrientation.x[3][1] = pos.y;
+	viewOrientation.x[3][2] = pos.z;
+
 	glLoadIdentity();
+	glMultMatrixf((float *)viewOrientation.x);
+	//glMultMatrixf((float *)tm.x);
+	Debug << "matrix:\n" << viewOrientation << endl;
+#else
+	glLoadIdentity();
+
 	gluLookAt(mPosition.x, mPosition.y, mPosition.z,
          	  mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z, 
 			  mUp.x, mUp.y, mUp.z);
-
+#endif
 	//std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl;
 }
@@ -180,10 +190,10 @@
 	const float w_far = h_far * GetAspect();
 
-	const Vector3 view = mDirection;
+	const Vector3 view = GetDirection();
 	const Vector3 fc = mPosition + view * z_far; 
 	
-	const Vector3 up = mUp;
-
-	const Vector3 right = mRight;
+	const Vector3 up = GetUpVector();
+
+	const Vector3 right = GetRightVector();
 
 	Vector3 t1, t2;
@@ -197,5 +207,5 @@
 	fbr = fc - t1 + t2;
 
-	const Vector3 nc = mPosition + mDirection * z_near;
+	const Vector3 nc = mPosition + view * z_near;
 	
 	t1 = h_near * 0.5f * up;
@@ -209,4 +219,64 @@
 
 
-}
-
+void Camera::SetOrtho(bool ortho)
+{
+	mIsOrtho = true;
+}
+
+
+void Camera::Yaw(float angle)
+{
+	mYaw += angle;
+	CalculateFromPitchAndYaw();
+	/*
+	Matrix4x4 viewOrientation(mRight, mUp, mDirection);
+	Matrix4x4 rot = RotationYMatrix(angle);
+
+	viewOrientation = viewOrientation * rot;
+
+	mDirection = Vector3(viewOrientation.x[2][0], viewOrientation.x[2][1], viewOrientation.x[2][2]);
+	mRight =     Vector3(viewOrientation.x[0][0], viewOrientation.x[0][1], viewOrientation.x[0][2]);
+	mUp =        Vector3(viewOrientation.x[1][0], viewOrientation.x[1][1], viewOrientation.x[1][2]);*/
+}
+
+
+void Camera::Pitch(float angle)
+{
+	mPitch += angle;
+	CalculateFromPitchAndYaw();
+}
+
+
+void Camera::CalculateFromPitchAndYaw()
+{
+	mViewOrientation = mBaseOrientation;
+
+	Matrix4x4 roty = RotationYMatrix(mPitch);
+	Matrix4x4 rotx = RotationXMatrix(mYaw);
+
+	mViewOrientation *= roty;
+	mViewOrientation *= rotx;
+}
+
+
+Vector3 Camera::GetDirection() const
+{ 
+	return Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]);
+}
+
+
+Vector3 Camera::GetUpVector() const 
+{ 
+	return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]);
+}
+
+
+Vector3 Camera::GetRightVector() const 
+{ 
+	return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]);
+		
+}
+
+
+}
+
