Index: GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.h
===================================================================
--- GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.h	(revision 2778)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.h	(revision 2780)
@@ -20,4 +20,5 @@
 	{
 		/// the 6 clip planes
+		//float mClipPlane[6][4];
 		Plane3 mClipPlanes[6];
 
@@ -29,11 +30,18 @@
 	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; }
-	Vector3 GetDirection() const;
-	Vector3 GetUpVector() const;
-	Vector3 GetRightVector() const;
+	inline Vector3 GetDirection() const { return mDirection; }
+	inline Vector3 GetUpVector() const { return mUp; }
+	inline Vector3 GetRightVector() const { return mRight; }
 
 	inline float GetFov() const { return mFovy; }
@@ -52,26 +60,17 @@
 	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;
@@ -80,16 +79,4 @@
 
 	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 2778)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/SceneQuery.cpp	(revision 2780)
@@ -63,8 +63,8 @@
 
 	Camera *orthoCam = new Camera(len.x, len.y);
-	orthoCam->SetOrtho(true);
+	//orthoCam->SetOrtho(true);
 
 	orthoCam->SetNear(0.0f);
-	orthoCam->SetFar(len.z);
+	//orthoCam->SetFar(len.z);
 
 	orthoCam->SetPosition(pos);
@@ -72,5 +72,5 @@
 	cout << "fov: " << orthoCam->GetFov() << endl;
 	cout << "near: " << orthoCam->GetNear() << endl;
-	cout << "far: " << orthoCam->GetFar() << endl;
+	//cout << "far: " << orthoCam->GetFar() << endl;
 	cout << "aspect: " << orthoCam->GetAspect() << endl;
 
@@ -80,5 +80,5 @@
 
 	//orthoCam->SetDirection(Vector3(0, 1, 0));
-	orthoCam->Yaw(M_PI * 0.5f);
+	//orthoCam->Yaw(M_PI * 0.5f);
 	
 	renderer->SetCamera(orthoCam);
Index: GTP/trunk/App/Demos/Vis/CHC_revisited/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/CHC_revisited/chcdemo.cpp	(revision 2779)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/chcdemo.cpp	(revision 2780)
@@ -163,6 +163,5 @@
 	//camera->LookInBox(bvh->GetBox());
 
-	//camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f));
-	//camera->SetDirection(Vector3(0.0f, 1.0f, 0.0f));
+	camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f));
 	camera->SetPosition(Vector3(483.398f, 242.364f, 186.078f));
 
@@ -295,5 +294,4 @@
 		break;
 	case RenderTraverser::CHC:
-		cout << "using chc" << endl;
 		traverser = new CHCTraverser();
 		break;
@@ -490,5 +488,9 @@
 	case 'A':
 		{
-			camera->Pitch(keyRotation);
+			Vector3 viewDir = camera->GetDirection();
+			// rotate view vector
+			Matrix4x4 rot = RotationZMatrix(keyRotation);
+			viewDir = rot * viewDir;
+			camera->SetDirection(viewDir);
 		}
 		break;
@@ -496,9 +498,21 @@
 	case 'D':
 	{
-			camera->Pitch(-keyRotation);
+			Vector3 viewDir = camera->GetDirection();
+			// rotate view vector
+			Matrix4x4 rot = RotationZMatrix(-keyRotation);
+			viewDir = rot * viewDir;
+			camera->SetDirection(viewDir);
 		}
 		break;
 	case 'w':
 	case 'W':
+		{
+			Vector3 pos = camera->GetPosition();
+			pos += hvec * keyForwardMotion;
+			camera->SetPosition(pos);
+		}
+		break;
+	case 'x':
+	case 'X':
 		{
 			Vector3 pos = camera->GetPosition();
@@ -507,12 +521,4 @@
 		}
 		break;
-	case 'x':
-	case 'X':
-		{
-			Vector3 pos = camera->GetPosition();
-			pos += hvec * keyForwardMotion;
-			camera->SetPosition(pos);
-		}
-		break;
 	case 'r':
 	case 'R':
@@ -541,23 +547,31 @@
 	case GLUT_KEY_LEFT:
 		{
-			camera->Pitch(keyRotation);
+			Vector3 viewDir = camera->GetDirection();
+			// rotate view vector
+			Matrix4x4 rot = RotationZMatrix(keyRotation);
+			viewDir = rot * viewDir;
+			camera->SetDirection(viewDir);
 		}
 		break;
 	case GLUT_KEY_RIGHT:
 		{
-			camera->Pitch(-keyRotation);
+			Vector3 viewDir = camera->GetDirection();
+			// rotate view vector
+			Matrix4x4 rot = RotationZMatrix(-keyRotation);
+			viewDir = rot * viewDir;
+			camera->SetDirection(viewDir);
 		}
 		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);
 		}
@@ -627,4 +641,6 @@
 void leftMotion(int x, int y) 
 {
+	static float eyeXAngle = 0.0f;
+
 	Vector3 viewDir = camera->GetDirection();
 	Vector3 pos = camera->GetPosition();
@@ -633,10 +649,13 @@
 	Vector3 horView(viewDir[0], viewDir[1], 0);
 	
-	float eyeXAngle = 0.2f *  M_PI * (xEyeBegin - x) / 180.0;
-
-	camera->Pitch(eyeXAngle);
-
-	pos -= horView * (yMotionBegin - y) * 0.2f;
-	
+	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);
 	camera->SetPosition(pos);
 	
@@ -653,8 +672,17 @@
 void rightMotion(int x, int y) 
 {
-	float eyeYAngle = -0.2f *  M_PI * (yEyeBegin - y) / 180.0;
-
-	camera->Yaw(eyeYAngle);
-	
+	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);
+		
 	yEyeBegin = y;
 	
@@ -678,5 +706,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;
@@ -771,5 +799,4 @@
 	////////////
 	// --- 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 2779)
+++ GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp	(revision 2780)
@@ -7,6 +7,4 @@
 {
 
-using namespace std;
-
 
 Camera::Camera() 
@@ -15,10 +13,4 @@
 	mHeight = 100;
 	mFovy = 90.0f * M_PI / 180.0f;
-	mIsOrtho = false;
-	
-	SetPosition(Vector3(0, 0, 0));
-
-	mPitch = mYaw = 0;
-	Precompute();
 }
 
@@ -30,13 +22,4 @@
 	
 	mFovy = fieldOfView * M_PI / 180.0f;
-
-	mIsOrtho = false;
-
-	SetPosition(Vector3(0, 0, 0));
-
-	SetPosition(Vector3(0, 0, 0));
-
-	mPitch = mYaw = 0;
-	Precompute();
 }
 
@@ -44,16 +27,17 @@
 void Camera::Precompute() 
 {
-	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;
-	cout<<"view: " << direction << " right: " << right << " up " << up << endl;
-}
-
+	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;
+}
 
 
@@ -61,4 +45,12 @@
 {
 	mPosition = pos;
+	Precompute();
+}
+
+
+void Camera::SetDirection(const Vector3 &dir) 
+{
+	mDirection = dir;
+	Precompute();
 }
 
@@ -67,4 +59,24 @@
 {
 	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();
 }
 
@@ -147,14 +159,10 @@
 void Camera::SetupCameraView()
 {
-	Matrix4x4 viewOrientation = mViewOrientation;
-	Vector3 pos = -mPosition;
-	pos = mViewOrientation * pos;
-
-	viewOrientation.x[3][0] = pos.x;
-	viewOrientation.x[3][1] = pos.y;
-	viewOrientation.x[3][2] = pos.z;
-
 	glLoadIdentity();
-	glMultMatrixf((float *)viewOrientation.x);
+	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);
+
+	//std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl;
 }
 
@@ -172,10 +180,10 @@
 	const float w_far = h_far * GetAspect();
 
-	const Vector3 view = GetDirection();
+	const Vector3 view = mDirection;
 	const Vector3 fc = mPosition + view * z_far; 
 	
-	const Vector3 up = GetUpVector();
-
-	const Vector3 right = GetRightVector();
+	const Vector3 up = mUp;
+
+	const Vector3 right = mRight;
 
 	Vector3 t1, t2;
@@ -189,5 +197,5 @@
 	fbr = fc - t1 + t2;
 
-	const Vector3 nc = mPosition + view * z_near;
+	const Vector3 nc = mPosition + mDirection * z_near;
 	
 	t1 = h_near * 0.5f * up;
@@ -201,64 +209,4 @@
 
 
-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]);
-		
-}
-
-
-}
-
+}
+
