Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2794)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 2795)
@@ -9,5 +9,5 @@
 #include "Vector3.h"
 #include "Matrix4x4.h"
-#include "BinaryLoader.h"
+#include "ResourceManager.h"
 #include "Bvh.h"
 #include "Camera.h"
@@ -20,5 +20,5 @@
 #include "Visualization.h"
 #include "RenderState.h"
-
+#include "Timer/PerfTimer.h"
 
 
@@ -35,7 +35,9 @@
 Bvh *bvh = NULL;
 /// handles scene loading
-BinaryLoader *loader = NULL;
+ResourceManager *loader = NULL;
 /// the scene camera
 Camera *camera = NULL;
+/// the scene camera
+Camera *visCamera = NULL;
 /// the visualization
 Visualization *visualization = NULL;
@@ -43,5 +45,5 @@
 RenderState state;
 /// the rendering algorithm
-int renderMode = RenderTraverser::CULL_FRUSTUM;
+int renderMode = RenderTraverser::CHCPLUSPLUS;
 // eye near plane distance
 float nearDist = 0.1f; 
@@ -49,9 +51,16 @@
 int threshold;
 
+float fov = 50.0f;
+
 int assumedVisibleFrames = 10;
 int maxBatchSize = 50;
 
-const float keyForwardMotion = 0.5f;
-const float keyRotation = 0.05f;
+int trianglesPerVirtualLeaf = 1000;
+
+/// these values get scaled with the frame rate
+const float keyForwardMotion = 50.0f;
+const float keyRotation = 2.0f;
+/// elapsed time in seconds
+double elapsedTime = 1.0f;
 
 int winWidth = 1024;
@@ -84,10 +93,13 @@
 bool useOptimization = false;
 bool useTightBounds = true;
-bool useRenderQueue = false;
+bool useRenderQueue = true;
 bool useMultiQueries = true;
 
+/// the accumulated z axis rotation
 float rotation = 0;
 
 Matrix4x4 visView = IdentityMatrix();
+
+SceneEntityContainer skyGeometry;
 
 bool leftKeyPressed = false;
@@ -129,5 +141,8 @@
 void KeyHorizontalMotion(float shift);
 void KeyVerticalMotion(float shift);
-void KeyRotate(float angle);
+
+
+inline float KeyRotationAngle() { return keyRotation * elapsedTime; }
+inline float KeyShift() { return keyForwardMotion * elapsedTime; }
 
 
@@ -137,20 +152,9 @@
 	int returnCode = 0;
 
-#ifdef _CRT_SET
-
-	//Now just call this function at the start of your program and if you're
-	//compiling in debug mode (F5), any leaks will be displayed in the Output
-	//window when the program shuts down. If you're not in debug mode this will
-	//be ignored. Use it as you will!
-	//note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() {
-
-	_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF);
-	_CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE);
-	_CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR); 
-#endif
-
-
-	camera = new Camera(winWidth, winHeight, 60);
+	camera = new Camera(winWidth, winHeight, fov);
 	camera->SetNear(nearDist);
+
+	visCamera = new Camera(winWidth, winHeight, fov);
+	visCamera->SetNear(nearDist);
 
 	glutInitWindowSize(winWidth, winHeight);
@@ -158,5 +162,5 @@
 	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
 
-	glutCreateWindow("Coherent Hierarchical Culling");
+	glutCreateWindow("FriendlyCulling");
 
 	glutDisplayFunc(Display);
@@ -177,5 +181,5 @@
 
 
-	loader = new BinaryLoader();
+	loader = new ResourceManager();
 
 	//const string filename("data/city/model/city.dem");
@@ -190,4 +194,18 @@
 		exit(0);
 	}
+
+	SceneEntityContainer dummy;
+
+	const string envname = string(model_path + "env.dem");
+
+	if (loader->Load(envname, skyGeometry))
+		cout << "sky box " << filename << " loaded" << endl;
+	else
+	{
+		cerr << "loading sky box " << filename << " failed" << endl;
+		CleanUp();
+		exit(0);
+	}
+
 
 	const string bvh_filename = string(model_path + "city.bvh");
@@ -206,5 +224,6 @@
 	ResetTraverser();
 
-	camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f));
+	//camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f));
+	camera->Pitch(-M_PI * 0.5);
 	camera->SetPosition(Vector3(483.398f, 242.364f, 186.078f));
 
@@ -265,8 +284,8 @@
 		"",
 		"'F1'           - shows/dismisses this message",
-		"'F2'           - shows/hides statistics",
+		"'F2'           - shows/hides bird eye view",
 		"'F3'           - shows/hides bounds (boxes or tight bounds)",
-		"'F4'           - shows/hides bird eye view",
-		"'f5',          - load helpScreen",
+		"'F4'           - shows/hides statistics",
+		"'F5',          - shows/hides parameters",
 		"'SPACE'        - cycles through occlusion culling algorithms",
 		"",
@@ -281,8 +300,10 @@
 		"'-'            - decreases max batch size",
 		"'+'            - increases max batch size",
+		"'4'            - decrease triangles per virtual leaf (sets depth of bvh)",
+		"'5'            - increase triangles per virtual leaf (sets depth of bvh)",
 		"'6'            - decrease assumed visible frames",
 		"'7'            - increase assumed visible frames",
-		"'8'            - upward motion",
-		"'9'            - downward motion",
+		"'8'            - downward motion",
+		"'9'            - upward motion",
 		"",
 		"'R'            - use render queue",
@@ -405,12 +426,13 @@
 }
 
-void SetupEyeView(void)
+void SetupEyeView()
 {
 	glMatrixMode(GL_PROJECTION);
 	glLoadIdentity();
 
-	gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal()));
+	gluPerspective(fov, 1.0f / winAspectRatio, nearDist, 10.0f * Magnitude(bvh->GetBox().Diagonal()));
 
 	glMatrixMode(GL_MODELVIEW);
+	glLoadIdentity();
 	camera->SetupCameraView();
 
@@ -423,14 +445,4 @@
 
 
-void KeyRotate(float angle)
-{
-	Vector3 viewDir = camera->GetDirection();
-	// rotate view vector
-	Matrix4x4 rot = RotationZMatrix(angle);
-	viewDir = rot * viewDir;
-	camera->SetDirection(viewDir);
-}
-
-
 void KeyHorizontalMotion(float shift)
 {
@@ -459,16 +471,20 @@
 void Display() 
 {
+	PerfTimer frameTimer;
+
+	frameTimer.Start();
+
 	if (leftKeyPressed)
-		KeyRotate(keyRotation);
+		camera->Pitch(KeyRotationAngle());
 	if (rightKeyPressed)
-		KeyRotate(-keyRotation);
+		camera->Pitch(-KeyRotationAngle());
 	if (upKeyPressed)
-		KeyHorizontalMotion(keyForwardMotion);
+		KeyHorizontalMotion(KeyShift());
 	if (downKeyPressed)
-		KeyHorizontalMotion(-keyForwardMotion);
+		KeyHorizontalMotion(-KeyShift());
 	if (eightKeyPressed)
-		KeyVerticalMotion(keyForwardMotion);
+		KeyVerticalMotion(-KeyShift());
 	if (nineKeyPressed)
-		KeyVerticalMotion(-keyForwardMotion);
+		KeyVerticalMotion(KeyShift());
 
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -479,5 +495,19 @@
 	// actually render the scene geometry using one of the specified algorithms
 	traverser->RenderScene();
-	
+
+	
+	glEnableClientState(GL_VERTEX_ARRAY);
+	glEnableClientState(GL_NORMAL_ARRAY);
+
+	SceneEntityContainer::const_iterator sit, sit_end = skyGeometry.end();
+
+	for (sit = skyGeometry.begin(); sit != sit_end; ++ sit)
+		(*sit)->Render(&state);
+
+	glDisableClientState(GL_VERTEX_ARRAY);
+	glDisableClientState(GL_NORMAL_ARRAY);
+
+	state.Reset();
+
 	if (visMode) DisplayVisualization();
 	
@@ -485,4 +515,6 @@
 
 	glutSwapBuffers();
+
+	elapsedTime = frameTimer.Elapsed();
 }
 
@@ -531,5 +563,5 @@
 		{
 			eightKeyPressed = true;
-			KeyVerticalMotion(keyForwardMotion);
+			KeyVerticalMotion(-KeyShift());
 			break;
 		}
@@ -537,5 +569,5 @@
 		{
 			nineKeyPressed = true;
-			KeyVerticalMotion(-keyForwardMotion);
+			KeyVerticalMotion(+KeyShift());
 			break;
 		}	
@@ -549,5 +581,5 @@
 		{
 			leftKeyPressed = true;
-			KeyRotate(keyRotation);
+			camera->Pitch(KeyRotationAngle());
 		}
 		break;
@@ -556,5 +588,5 @@
 	{
 			rightKeyPressed = true;
-			KeyRotate(-keyRotation);
+			camera->Pitch(-KeyRotationAngle());
 		}
 		break;
@@ -563,5 +595,5 @@
 		{
 			upKeyPressed = true;
-			KeyHorizontalMotion(keyForwardMotion);
+			KeyHorizontalMotion(KeyShift());
 		}
 		break;
@@ -570,5 +602,5 @@
 		{
 			downKeyPressed = true;
-			KeyHorizontalMotion(-keyForwardMotion);
+			KeyHorizontalMotion(-KeyShift());
 		}
 		break;
@@ -587,7 +619,14 @@
 		}
 		break;
-
-
-
+	case '4':
+		if (trianglesPerVirtualLeaf >= 100)
+			trianglesPerVirtualLeaf -= 100;
+
+		bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
+		break;
+	case '5':
+		trianglesPerVirtualLeaf += 100;
+		bvh->SetVirtualLeaves(trianglesPerVirtualLeaf);
+		break;
 	default:
 		return;
@@ -658,7 +697,4 @@
 void Special(int c, int x, int y) 
 {
-	// used to avoid vertical motion with the keys
-	Vector3 hvec = Vector3(camera->GetDirection()[0], camera->GetDirection()[1], 0.0f);
-	
 	switch(c) 
 	{
@@ -667,5 +703,5 @@
 		break;
 	case GLUT_KEY_F2:
-		showStatistics = !showStatistics;
+		visMode = !visMode;
 		break;
 	case GLUT_KEY_F3:
@@ -674,5 +710,5 @@
 		break;
 	case GLUT_KEY_F4:
-		visMode = !visMode;
+		showStatistics = !showStatistics;
 		break;
 	case GLUT_KEY_F5:
@@ -682,5 +718,5 @@
 		{
 			leftKeyPressed = true;
-			KeyRotate(keyRotation);
+			camera->Pitch(KeyRotationAngle());
 		}
 		break;
@@ -688,5 +724,5 @@
 		{
 			rightKeyPressed = true;
-			KeyRotate(-keyRotation);
+			camera->Pitch(-KeyRotationAngle());
 		}
 		break;
@@ -694,5 +730,5 @@
 		{
 			upKeyPressed = true;
-			KeyHorizontalMotion(keyForwardMotion);
+			KeyHorizontalMotion(KeyShift());
 		}
 		break;
@@ -700,5 +736,5 @@
 		{
 			downKeyPressed = true;
-			KeyHorizontalMotion(-keyForwardMotion);
+			KeyHorizontalMotion(-KeyShift());
 		}
 		break;
@@ -728,5 +764,5 @@
 	glLoadIdentity();
 
-	gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal()));
+	gluPerspective(fov, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal()));
 
 	glMatrixMode(GL_MODELVIEW);
@@ -768,6 +804,4 @@
 void LeftMotion(int x, int y) 
 {
-	static float eyeXAngle = 0.0f;
-
 	Vector3 viewDir = camera->GetDirection();
 	Vector3 pos = camera->GetPosition();
@@ -776,15 +810,10 @@
 	Vector3 horView(viewDir[0], viewDir[1], 0);
 	
-	eyeXAngle = 0.2f *  M_PI * (xEyeBegin - x) / 180.0;
-
-	rotation += eyeXAngle;
-
-	// rotate view vector
-	Matrix4x4 rot = RotationZMatrix(eyeXAngle);
-	viewDir = rot * viewDir;
+	float eyeXAngle = 0.2f *  M_PI * (xEyeBegin - x) / 180.0;
+
+	camera->Pitch(eyeXAngle);
 
 	pos += horView * (yMotionBegin - y) * 0.2f;
-
-	camera->SetDirection(viewDir);
+	
 	camera->SetPosition(pos);
 	
@@ -801,19 +830,9 @@
 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;
-	
 	glutPostRedisplay();
 }
@@ -907,5 +926,5 @@
 // displays the visualisation of culling algorithm
 void DisplayVisualization()
-{
+{cout<<"here34"<<endl;
 	SetupVisView();
 
@@ -923,20 +942,25 @@
 	glViewport(winWidth - winWidth / 4, winHeight - winHeight / 3, winWidth, winHeight);
 
-
 	glMatrixMode(GL_PROJECTION);
 	glLoadIdentity();
 	
-	//gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal()));
+	gluPerspective(fov, 1.0f / winAspectRatio, nearDist, 20.0f * Magnitude(bvh->GetBox().Diagonal()));
+	
 	AxisAlignedBox3 box = bvh->GetBox();
 	const float offs = 2000;
-	glOrtho(box.Min().y - offs, box.Max().y + offs, box.Min().y - offs, box.Max().y + offs, 0.1, 20.0f * Magnitude(bvh->GetBox().Diagonal())); 
-	
-	
-	glPushMatrix();
-
+	
+	//glOrtho(box.Min().y - offs, box.Max().y + offs, box.Min().y - offs, box.Max().y + offs, 0.1, 20.0f * Magnitude(bvh->GetBox().Diagonal())); 
+	//glOrtho(-offs, offs, offs, -offs, 0.1, 20.0f * Magnitude(bvh->GetBox().Diagonal())); 
+	//glPushMatrix();
+
+	Vector3 vizpos = Vector3(box.Center().x, box.Center().y, box.Max().z);
+	visCamera->SetPosition(vizpos);
+	visCamera->Yaw(-0.5 * M_PI);
+	cout<<"pos: " << visCamera->GetPosition()<<endl;
 	glMatrixMode(GL_MODELVIEW);
 	glLoadIdentity();
 
-	glMultMatrixf((float *)visView.x);
+	visCamera->SetupCameraView();
+	//glMultMatrixf((float *)visView.x);
 
 	//SetupLighting();
@@ -947,6 +971,4 @@
 	glLightfv(GL_LIGHT1, GL_POSITION, position1);
 
-
-
 	glClear(GL_DEPTH_BUFFER_BIT);
 
@@ -956,5 +978,5 @@
 	visualization->Render();
 	
-	glPopMatrix();
+	//glPopMatrix();
 	
 	glViewport(0, 0, winWidth, winHeight);
@@ -991,11 +1013,8 @@
 void CleanUp()
 {
-	CLEAR_CONTAINER(sceneEntities);
 	DEL_PTR(traverser);
-
 	DEL_PTR(bvh);
 	DEL_PTR(visualization);
 	DEL_PTR(camera);
-
 	DEL_PTR(loader);
 }
@@ -1040,4 +1059,5 @@
 	char msg6[200];
 	char msg7[200];
+	char msg8[200];
 
 
@@ -1084,4 +1104,6 @@
 	sprintf_s(msg6, "issued queries: %5d, state changes: %5d", issuedQueries, stateChanges);
 
+	sprintf_s(msg8, "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf);
+
 	sprintf_s(msg7, "fps: %6.1f", fps);
 
@@ -1111,4 +1133,5 @@
 			Output(20, 150, msg2);
 			Output(20, 180, msg3);
+			Output(20, 210, msg8);
 		}
 	}
