[10] | 1 | // occquery.cpp : Defines the entry point for the console application. |
---|
| 2 | // |
---|
| 3 | |
---|
| 4 | #include "stdafx.h" |
---|
| 5 | |
---|
| 6 | extern "C" |
---|
| 7 | { |
---|
| 8 | #include "MathStuff.h" |
---|
| 9 | #include "DataTypes.h" |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | #include "glInterface.h" |
---|
| 13 | #include "RenderTraverser.h" |
---|
| 14 | #include <math.h> |
---|
| 15 | #include <time.h> |
---|
| 16 | |
---|
| 17 | double nearDist = 0.1; // eye near plane distance |
---|
| 18 | int winWidth, winHeight; |
---|
| 19 | int objectType = Geometry::TEAPOT; |
---|
| 20 | int nextObjectType = objectType; |
---|
| 21 | |
---|
| 22 | float visZoomFactor = 1.5f; |
---|
| 23 | |
---|
| 24 | bool showHelp = false; |
---|
| 25 | bool showStatistics = true; |
---|
| 26 | bool showBoundingVolumes = false; |
---|
| 27 | bool visMode = false; |
---|
| 28 | bool showCreateParams = false; |
---|
| 29 | |
---|
| 30 | // traverses and renders the hierarchy |
---|
| 31 | RenderTraverser traverser; |
---|
| 32 | |
---|
| 33 | Vector3 eyePos = {0.0, 0.0, 3.0}; // eye position |
---|
| 34 | Vector3 viewDir = {0.0, 0.0, -1.0}; // eye view dir |
---|
| 35 | Vector3 lightDir = {0.0, 0.0, 1.0}; // light dir |
---|
| 36 | |
---|
| 37 | Matrix4x4 eyeView; // eye view matrix |
---|
| 38 | Matrix4x4 eyeProjection; // eye projection matrix |
---|
| 39 | Matrix4x4 eyeProjView; //= eyeProjection*eyeView |
---|
| 40 | Matrix4x4 invEyeProjView; //= eyeProjView^(-1) |
---|
| 41 | Matrix4x4 visView; // visualisation view matrix |
---|
| 42 | |
---|
| 43 | //mouse navigation state |
---|
| 44 | int xEyeBegin, yEyeBegin, yMotionBegin, verticalMotionBegin, horizontalMotionBegin = 0; |
---|
| 45 | int renderMode = RenderTraverser::RENDER_COHERENT; |
---|
| 46 | |
---|
| 47 | // relative size of an object |
---|
| 48 | float objectSize = 3.5f; |
---|
| 49 | |
---|
| 50 | int numObjects = 2000; |
---|
| 51 | int numNextObjects = numObjects; |
---|
| 52 | float zLength = 30.0f; |
---|
| 53 | |
---|
| 54 | // this defines the volume where objects can be drawn |
---|
| 55 | Vector3 minTranslation = {-2.5f, -2.5f, -3.0f}; |
---|
| 56 | Vector3 maxTranslation = {2.5f, 2.5f, -3.0 - zLength}; |
---|
| 57 | |
---|
| 58 | const float minAngle = 0; |
---|
| 59 | const float maxAngle = 360; |
---|
| 60 | |
---|
| 61 | const int renderTimeSize = 100; |
---|
| 62 | long renderTimes[renderTimeSize]; |
---|
| 63 | int renderTimesIdx = 0; |
---|
| 64 | int renderTimesValid = 0; |
---|
| 65 | |
---|
| 66 | typedef vector<Geometry *> GeometryList; |
---|
| 67 | GeometryList geometry; |
---|
| 68 | |
---|
| 69 | bool useOptimization = true; |
---|
| 70 | |
---|
| 71 | Vector3 amb[2]; |
---|
| 72 | Vector3 dif[2]; |
---|
| 73 | Vector3 spec[2]; |
---|
| 74 | |
---|
| 75 | void begin2D(void); |
---|
| 76 | void end2D(void); |
---|
| 77 | void output(const int x, const int y, const char *string); |
---|
| 78 | void initGLstate(void); |
---|
| 79 | void keyboard(const unsigned char c, const int x, const int y); |
---|
| 80 | void drawHelpMessage(void); |
---|
| 81 | void drawStatistics(void); |
---|
| 82 | void display(void); |
---|
| 83 | void special(const int c, const int x, const int y); |
---|
| 84 | void reshape(const int w, const int h); |
---|
| 85 | void mouse(int button, int state, int x, int y); |
---|
| 86 | void initExtensions(void); |
---|
| 87 | void leftMotion(int x, int y); |
---|
| 88 | //void rightMotion(int x, int y); |
---|
| 89 | void middleMotion(int x, int y); |
---|
| 90 | void drawEyeView(void); |
---|
| 91 | void setupEyeView(void); |
---|
| 92 | void setupVisView(void); |
---|
| 93 | void updateEyeMtx(void); |
---|
| 94 | long calcRenderTime(void); |
---|
| 95 | void resetTimer(void); |
---|
| 96 | void cleanUp(void); |
---|
| 97 | void calcDecimalPoint(string &str, int d); |
---|
| 98 | |
---|
| 99 | HierarchyNode* generateHierarchy(int numObjects); |
---|
| 100 | Geometry *generateGeometry(Vector3 translateRatio, float xRotRatio, |
---|
| 101 | float yRotRatio, float zRotRatio, int materialIdx); |
---|
| 102 | void displayVisualization(); |
---|
| 103 | |
---|
| 104 | void deleteGeometry(); |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | int _tmain(int argc, _TCHAR* argv[]) |
---|
| 108 | { |
---|
| 109 | glutInitWindowSize(800,600); |
---|
| 110 | glutInit(&argc,argv); |
---|
| 111 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); |
---|
| 112 | |
---|
| 113 | glutCreateWindow("Coherent Hierarchical Culling"); |
---|
| 114 | |
---|
| 115 | glutDisplayFunc(display); |
---|
| 116 | glutKeyboardFunc(keyboard); |
---|
| 117 | glutSpecialFunc(special); |
---|
| 118 | glutReshapeFunc(reshape); |
---|
| 119 | glutMouseFunc(mouse); |
---|
| 120 | glutIdleFunc(display); |
---|
| 121 | initExtensions(); |
---|
| 122 | initGLstate(); |
---|
| 123 | |
---|
| 124 | leftMotion(0,0); |
---|
| 125 | middleMotion(0,0); |
---|
| 126 | |
---|
| 127 | HierarchyNode *hierarchy = generateHierarchy(numObjects); |
---|
| 128 | traverser.SetHierarchy(hierarchy); |
---|
| 129 | |
---|
| 130 | /// initialise rendertime array |
---|
| 131 | for(int i=0; i<renderTimeSize; i++) |
---|
| 132 | renderTimes[i] = 0; |
---|
| 133 | |
---|
| 134 | glutMainLoop(); |
---|
| 135 | |
---|
| 136 | // clean up |
---|
| 137 | cleanUp(); |
---|
| 138 | |
---|
| 139 | return 0; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | |
---|
| 143 | void initGLstate(void) |
---|
| 144 | { |
---|
| 145 | glClearColor(0.6, 0.6, 0.8, 1.0); |
---|
| 146 | |
---|
| 147 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
---|
| 148 | glPixelStorei(GL_PACK_ALIGNMENT,1); |
---|
| 149 | |
---|
| 150 | glDepthRange(0.0, 1.0); |
---|
| 151 | glClearDepth(1.0); |
---|
| 152 | glDepthFunc(GL_LESS); |
---|
| 153 | |
---|
| 154 | glEnable(GL_LIGHTING); |
---|
| 155 | glEnable(GL_LIGHT0); |
---|
| 156 | |
---|
| 157 | glShadeModel(GL_SMOOTH); |
---|
| 158 | |
---|
| 159 | GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 }; |
---|
| 160 | GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; |
---|
| 161 | GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 }; |
---|
| 162 | GLfloat position[] = { 0.0, 3.0, 3.0, 0.0 }; |
---|
| 163 | |
---|
| 164 | GLfloat lmodel_ambient[] = { 0.2, 0.2, 0.2, 1.0 }; |
---|
| 165 | GLfloat local_view[] = { 0.0 }; |
---|
| 166 | |
---|
| 167 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); |
---|
| 168 | glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); |
---|
| 169 | glLightfv(GL_LIGHT0, GL_POSITION, position); |
---|
| 170 | |
---|
| 171 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); |
---|
| 172 | glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view); |
---|
| 173 | |
---|
| 174 | glMaterialf(GL_FRONT, GL_SHININESS, 64); |
---|
| 175 | glEnable(GL_NORMALIZE); |
---|
| 176 | |
---|
| 177 | glFrontFace(GL_CCW); |
---|
| 178 | glCullFace(GL_BACK); |
---|
| 179 | glEnable(GL_CULL_FACE); |
---|
| 180 | |
---|
| 181 | glClearColor(0.2, 0.2, 0.8, 0.0); |
---|
| 182 | |
---|
| 183 | setupVisView(); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | void drawHelpMessage(void) |
---|
| 188 | { |
---|
| 189 | const char *message[] = |
---|
| 190 | { |
---|
| 191 | "Help information", |
---|
| 192 | "", |
---|
| 193 | "'F1' - shows/dismisses this message", |
---|
| 194 | "'F2' - decreases number of objects (valid after scene recreation)", |
---|
| 195 | "'F3' - increases number of objects (valid after scene recreation)", |
---|
| 196 | "'F4' - decreases box length in z direction (valid after scene recreation)", |
---|
| 197 | "'F5' - increases box length in z direction (valid after scene recreation)", |
---|
| 198 | "'F6' - decreases object size (valid after scene recreation)", |
---|
| 199 | "'F7' - increases object size (valid after scene recreation)", |
---|
| 200 | "'F8' - cycles through object types (teapot, ...) (valid after scene recreation)", |
---|
| 201 | "", |
---|
| 202 | "'MOUSE-LEFT' - turn left/right, move forward/backward", |
---|
| 203 | "'MOUSE-RIGHT' - turn left/right, move forward/backward", |
---|
| 204 | "'MOUSE-MIDDLE' - move up/down, left/right", |
---|
| 205 | "'CURSOR UP' - move forward", |
---|
| 206 | "'CURSOR BACK' - move backward", |
---|
| 207 | "'CURSOR RIGHT' - turn right", |
---|
| 208 | "'CURSOR LEFT' - turn left", |
---|
| 209 | "", |
---|
| 210 | "'SPACE' - cycles through occlusion culling algorithms", |
---|
| 211 | "'-' - decreases visibility threshold", |
---|
| 212 | "'+' - increases visibility threshold", |
---|
| 213 | "'C' - recreates the scene hierarchy", |
---|
| 214 | "'G' - enables/disables optimization to take geometry as occluder", |
---|
| 215 | "", |
---|
| 216 | "'R' - shows/hides recreation parameters", |
---|
| 217 | "'S' - shows/hides statistics", |
---|
| 218 | "'V' - shows/hides bounding volumes", |
---|
| 219 | "", |
---|
| 220 | "'1' - shows/hides visualization", |
---|
| 221 | "'2' - zooms out visualization", |
---|
| 222 | "'3' - zooms in visualization", |
---|
| 223 | 0, |
---|
| 224 | }; |
---|
| 225 | |
---|
| 226 | int i; |
---|
| 227 | int x = 40, y = 42; |
---|
| 228 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
| 229 | glEnable(GL_BLEND); |
---|
| 230 | glColor4f(0.0,1.0,0.0,0.2); // 20% green. |
---|
| 231 | |
---|
| 232 | // Drawn clockwise because the flipped Y axis flips CCW and CW. |
---|
| 233 | glRecti(winWidth - 30, 30, 30, winHeight - 30); |
---|
| 234 | |
---|
| 235 | glDisable(GL_BLEND); |
---|
| 236 | |
---|
| 237 | |
---|
| 238 | glColor3f(1.0,1.0,1.0); |
---|
| 239 | for(i = 0; message[i] != 0; i++) { |
---|
| 240 | if(message[i][0] == '\0') { |
---|
| 241 | y += 7; |
---|
| 242 | } else { |
---|
| 243 | output(x,y,message[i]); |
---|
| 244 | y += 14; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | // generates a teapot, all parameters between zero and one |
---|
| 251 | Geometry *generateGeometry(Vector3 translationRatio, float xRotRatio, |
---|
| 252 | float yRotRatio, float zRotRatio, int materialIdx) |
---|
| 253 | { |
---|
| 254 | float xRot = minAngle + xRotRatio * (maxAngle - minAngle); |
---|
| 255 | float yRot = minAngle + yRotRatio * (maxAngle - minAngle); |
---|
| 256 | float zRot = minAngle + zRotRatio * (maxAngle - minAngle); |
---|
| 257 | |
---|
| 258 | Vector3 translation; |
---|
| 259 | translation[0] = minTranslation[0] + translationRatio[0] * (maxTranslation[0] - minTranslation[0]); |
---|
| 260 | translation[1] = minTranslation[1] + translationRatio[1] * (maxTranslation[1] - minTranslation[1]); |
---|
| 261 | translation[2] = minTranslation[2] + translationRatio[2] * (maxTranslation[2] - minTranslation[2]); |
---|
| 262 | |
---|
| 263 | Geometry *result = new Geometry(translation, xRot, yRot, zRot, objectSize, objectType); |
---|
| 264 | |
---|
| 265 | result->SetAmbientColor(amb[materialIdx][0], amb[materialIdx][1], amb[materialIdx][2]); |
---|
| 266 | result->SetDiffuseColor(dif[materialIdx][0], dif[materialIdx][1], dif[materialIdx][2]); |
---|
| 267 | result->SetSpecularColor(spec[materialIdx][0], spec[materialIdx][1], spec[materialIdx][2]); |
---|
| 268 | |
---|
| 269 | return result; |
---|
| 270 | } |
---|
| 271 | |
---|
| 272 | // generates a the scene hierarchy with random values |
---|
| 273 | HierarchyNode* generateHierarchy(int numObjects) |
---|
| 274 | { |
---|
| 275 | HierarchyNode *hierarchy = new HierarchyNode(); |
---|
| 276 | |
---|
| 277 | // initialise materials |
---|
| 278 | copyVector3Values(amb[0], 0.0215, 0.1745, 0.0215); |
---|
| 279 | copyVector3Values(dif[0], 0.727811, 0.633, 0.6); |
---|
| 280 | copyVector3Values(spec[0], 0.633, 0.727811, 0.633); |
---|
| 281 | |
---|
| 282 | copyVector3Values(amb[1], 0.1745, 0.01175, 0.01175); |
---|
| 283 | copyVector3Values(dif[1], 0.61424, 0.04136, 0.04136); |
---|
| 284 | copyVector3Values(spec[1], 0.727811, 0.626959, 0.626959); |
---|
| 285 | |
---|
| 286 | srand (time (0)); |
---|
| 287 | |
---|
| 288 | printf("generating geometry with random position and orientation ... "); |
---|
| 289 | |
---|
| 290 | for(int i=0; i < numObjects; i++) |
---|
| 291 | { |
---|
| 292 | float xRotRatio = rand() / (float) RAND_MAX; |
---|
| 293 | float yRotRatio = rand() / (float) RAND_MAX; |
---|
| 294 | float zRotRatio = rand() / (float) RAND_MAX; |
---|
| 295 | |
---|
| 296 | Vector3 translationRatio = {rand() / (float) RAND_MAX, |
---|
| 297 | rand() / (float) RAND_MAX, |
---|
| 298 | rand() / (float) RAND_MAX}; |
---|
| 299 | |
---|
| 300 | int materialIdx = int(2 * rand() / (float) RAND_MAX); |
---|
| 301 | |
---|
| 302 | Geometry *geo = generateGeometry(translationRatio, xRotRatio, |
---|
| 303 | yRotRatio, zRotRatio, materialIdx); |
---|
| 304 | hierarchy->AddGeometry(geo); |
---|
| 305 | |
---|
| 306 | // put into global geometry list for later deletion |
---|
| 307 | geometry.push_back(geo); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | printf("finished\n"); |
---|
| 311 | printf("generating new kd-tree hierarchy ... "); |
---|
| 312 | HierarchyNode::InitKdTree(hierarchy); |
---|
| 313 | hierarchy->GenerateKdTree(); |
---|
| 314 | printf("finished\n"); |
---|
| 315 | |
---|
| 316 | return hierarchy; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | |
---|
| 320 | void updateEyeMtx(void) |
---|
| 321 | { |
---|
| 322 | const Vector3 up = {0.0, 1.0, 0.0}; |
---|
| 323 | |
---|
| 324 | look(eyeView, eyePos, viewDir, up); |
---|
| 325 | mult(eyeProjView, eyeProjection, eyeView); //eyeProjView = eyeProjection*eyeView |
---|
| 326 | invert(invEyeProjView, eyeProjView); //invert matrix |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | |
---|
| 330 | void setupEyeView(void) |
---|
| 331 | { |
---|
| 332 | glMatrixMode(GL_PROJECTION); |
---|
| 333 | glLoadMatrixd(eyeProjection); |
---|
| 334 | |
---|
| 335 | glMatrixMode(GL_MODELVIEW); |
---|
| 336 | glLoadMatrixd(eyeView); |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | void display(void) |
---|
| 341 | { |
---|
| 342 | char * msg[] = {"Frustum culling only", "Hierarchical stop and wait", |
---|
| 343 | "Coherent hierarchical culling"}; |
---|
| 344 | |
---|
| 345 | char msg2[200]; |
---|
| 346 | char msg3[200]; |
---|
| 347 | char msg4[100]; |
---|
| 348 | char msg5[100]; |
---|
| 349 | char msg6[100]; |
---|
| 350 | char msg7[100]; |
---|
| 351 | char msg8[100]; |
---|
| 352 | |
---|
| 353 | sprintf(msg2, "Traversed: %4d, frustum culled: %4d, query culled: %4d (of %d nodes)", |
---|
| 354 | traverser.GetNumTraversedNodes(), traverser.GetNumFrustumCulledNodes(), |
---|
| 355 | traverser.GetNumQueryCulledNodes(), |
---|
| 356 | traverser.GetHierarchy()->GetNumHierarchyNodes()); |
---|
| 357 | |
---|
| 358 | char *optstr[2] = {"", ", using optimization"}; |
---|
| 359 | |
---|
| 360 | float fps = 1000.0; |
---|
| 361 | long renderTime = calcRenderTime(); |
---|
| 362 | if(renderTime) fps = 1000000.0f / (float)calcRenderTime(); |
---|
| 363 | |
---|
| 364 | sprintf(msg3, "Threshold: %4d, algorithm rendering time: %ld ms (%3.3f fps)%s", |
---|
| 365 | traverser.GetVisibilityThreshold(), renderTime / 1000, fps, optstr[useOptimization]); |
---|
| 366 | |
---|
| 367 | string str; |
---|
| 368 | string str2; |
---|
| 369 | |
---|
| 370 | calcDecimalPoint(str, Geometry::CountTriangles(objectType) * traverser.GetNumRenderedGeometry()); |
---|
| 371 | calcDecimalPoint(str2, Geometry::CountTriangles(objectType) * numObjects); |
---|
| 372 | |
---|
| 373 | sprintf(msg4, "Rendered objects %d (of %d), rendered triangles: %s (of %s)", |
---|
| 374 | traverser.GetNumRenderedGeometry(), numObjects, str.c_str(), str2.c_str()); |
---|
| 375 | |
---|
| 376 | sprintf(msg5, "Next object num: %d", numNextObjects); |
---|
| 377 | sprintf(msg6, "Next length in z dir: %3.3f", zLength); |
---|
| 378 | |
---|
| 379 | char *objectTypeStr[3] = {"teapot", "torus", "sphere"}; |
---|
| 380 | |
---|
| 381 | sprintf(msg7, "Next object type: %s", objectTypeStr[nextObjectType]); |
---|
| 382 | sprintf(msg8, "Next object size: %3.3f", objectSize); |
---|
| 383 | |
---|
| 384 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 385 | |
---|
| 386 | updateEyeMtx(); // bring eye modelview matrix up-to-date |
---|
| 387 | setupEyeView(); |
---|
| 388 | |
---|
| 389 | traverser.SetViewpoint(eyePos); |
---|
| 390 | traverser.SetProjViewMatrix(eyeProjView); |
---|
| 391 | traverser.Render(renderMode); |
---|
| 392 | |
---|
| 393 | // cycle through rendertime array |
---|
| 394 | renderTimes[renderTimesIdx] = traverser.GetRenderTime(); |
---|
| 395 | renderTimesIdx = (renderTimesIdx + 1) % renderTimeSize; |
---|
| 396 | |
---|
| 397 | if(renderTimesIdx > renderTimesValid) |
---|
| 398 | renderTimesValid = renderTimesIdx; |
---|
| 399 | |
---|
| 400 | if(visMode) |
---|
| 401 | { |
---|
| 402 | displayVisualization(); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | begin2D(); |
---|
| 406 | if(showHelp) |
---|
| 407 | drawHelpMessage(); |
---|
| 408 | else |
---|
| 409 | { |
---|
| 410 | glColor3f(1.0,1.0,1.0); |
---|
| 411 | output(10,winHeight-10, msg[renderMode]); |
---|
| 412 | |
---|
| 413 | if(showStatistics) |
---|
| 414 | { |
---|
| 415 | if(showCreateParams) |
---|
| 416 | { |
---|
| 417 | output(10, winHeight-150, msg8); |
---|
| 418 | output(10, winHeight-130, msg7); |
---|
| 419 | output(10, winHeight-110, msg6); |
---|
| 420 | output(10, winHeight-90, msg5); |
---|
| 421 | } |
---|
| 422 | output(10, winHeight-70, msg3); |
---|
| 423 | output(10, winHeight-50, msg4); |
---|
| 424 | output(10, winHeight-30, msg2); |
---|
| 425 | } |
---|
| 426 | } |
---|
| 427 | end2D(); |
---|
| 428 | |
---|
| 429 | glutSwapBuffers(); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | |
---|
| 433 | #pragma warning( disable : 4100 ) |
---|
| 434 | void keyboard(const unsigned char c, const int x, const int y) |
---|
| 435 | { |
---|
| 436 | int threshold; |
---|
| 437 | HierarchyNode *hierarchy; |
---|
| 438 | |
---|
| 439 | switch(c) |
---|
| 440 | { |
---|
| 441 | case 27: |
---|
| 442 | exit(0); |
---|
| 443 | break; |
---|
| 444 | case 32: //space |
---|
| 445 | renderMode = (renderMode + 1) % RenderTraverser::NUM_RENDERMODES; |
---|
| 446 | |
---|
| 447 | resetTimer(); |
---|
| 448 | traverser.Render(renderMode); // render once so stats are updated |
---|
| 449 | break; |
---|
| 450 | case 'h': |
---|
| 451 | case 'H': |
---|
| 452 | showHelp = !showHelp; |
---|
| 453 | break; |
---|
| 454 | case 'v': |
---|
| 455 | case 'V': |
---|
| 456 | showBoundingVolumes = !showBoundingVolumes; |
---|
| 457 | HierarchyNode::SetRenderBoundingVolume(showBoundingVolumes); |
---|
| 458 | break; |
---|
| 459 | case 's': |
---|
| 460 | case 'S': |
---|
| 461 | showStatistics = !showStatistics; |
---|
| 462 | break; |
---|
| 463 | case '+': |
---|
| 464 | threshold = traverser.GetVisibilityThreshold() + 10; |
---|
| 465 | traverser.SetVisibilityThreshold(threshold); |
---|
| 466 | break; |
---|
| 467 | case '-': |
---|
| 468 | threshold = traverser.GetVisibilityThreshold() - 10; |
---|
| 469 | if(threshold < 0) threshold = 0; |
---|
| 470 | |
---|
| 471 | traverser.SetVisibilityThreshold(threshold); |
---|
| 472 | break; |
---|
| 473 | case '1': |
---|
| 474 | visMode = !visMode; |
---|
| 475 | break; |
---|
| 476 | |
---|
| 477 | case '2': |
---|
| 478 | visZoomFactor += 0.1; |
---|
| 479 | setupVisView(); |
---|
| 480 | break; |
---|
| 481 | case '3': |
---|
| 482 | visZoomFactor -= 0.1; |
---|
| 483 | if(visZoomFactor < 0.1) visZoomFactor = 0.1; |
---|
| 484 | |
---|
| 485 | setupVisView(); |
---|
| 486 | break; |
---|
| 487 | case 'r': |
---|
| 488 | case 'R': |
---|
| 489 | showCreateParams = !showCreateParams; |
---|
| 490 | break; |
---|
| 491 | case 'g': |
---|
| 492 | case 'G': |
---|
| 493 | useOptimization = !useOptimization; |
---|
| 494 | traverser.SetUseOptimization(useOptimization); |
---|
| 495 | break; |
---|
| 496 | case 'c': |
---|
| 497 | case 'C': |
---|
| 498 | |
---|
| 499 | hierarchy = traverser.GetHierarchy(); |
---|
| 500 | // delete old hierarchy |
---|
| 501 | if(hierarchy) delete hierarchy; |
---|
| 502 | deleteGeometry(); |
---|
| 503 | |
---|
| 504 | maxTranslation[2] = -3.0 - zLength; |
---|
| 505 | numObjects = numNextObjects; |
---|
| 506 | objectType = nextObjectType; |
---|
| 507 | |
---|
| 508 | hierarchy = generateHierarchy(numObjects); |
---|
| 509 | traverser.SetHierarchy(hierarchy); |
---|
| 510 | |
---|
| 511 | showCreateParams = false; |
---|
| 512 | |
---|
| 513 | traverser.Render(renderMode); // render once to update stats |
---|
| 514 | resetTimer(); |
---|
| 515 | break; |
---|
| 516 | |
---|
| 517 | default: |
---|
| 518 | return; |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | glutPostRedisplay(); |
---|
| 522 | } |
---|
| 523 | |
---|
| 524 | |
---|
| 525 | void special(const int c, const int x, const int y) |
---|
| 526 | { |
---|
| 527 | // used to avoid vertical motion with the keys |
---|
| 528 | Vector3 hvec = {viewDir[0], 0, viewDir[2]}; |
---|
| 529 | |
---|
| 530 | switch(c) |
---|
| 531 | { |
---|
| 532 | case GLUT_KEY_F1: |
---|
| 533 | showHelp = !showHelp; |
---|
| 534 | break; |
---|
| 535 | case GLUT_KEY_F2: |
---|
| 536 | numNextObjects -= 100; |
---|
| 537 | if(numNextObjects < 100) numNextObjects = 100; |
---|
| 538 | break; |
---|
| 539 | case GLUT_KEY_F3: |
---|
| 540 | numNextObjects += 100; |
---|
| 541 | break; |
---|
| 542 | case GLUT_KEY_F4: |
---|
| 543 | zLength -= 1; |
---|
| 544 | if(zLength < 0) zLength = 0; |
---|
| 545 | break; |
---|
| 546 | case GLUT_KEY_F5: |
---|
| 547 | zLength += 1; |
---|
| 548 | break; |
---|
| 549 | case GLUT_KEY_F6: |
---|
| 550 | objectSize -= 0.1; |
---|
| 551 | if(objectSize < 0.1) objectSize = 0.1; |
---|
| 552 | break; |
---|
| 553 | case GLUT_KEY_F7: |
---|
| 554 | objectSize += 0.1; |
---|
| 555 | break; |
---|
| 556 | case GLUT_KEY_F8: |
---|
| 557 | nextObjectType = (nextObjectType + 1) % Geometry::NUM_OBJECTS; |
---|
| 558 | break; |
---|
| 559 | case GLUT_KEY_LEFT: |
---|
| 560 | rotateVectorY(viewDir, 0.2); |
---|
| 561 | break;
|
---|
| 562 | case GLUT_KEY_RIGHT:
|
---|
| 563 | rotateVectorY(viewDir, -0.2);
|
---|
| 564 | break;
|
---|
| 565 | case GLUT_KEY_UP:
|
---|
| 566 | linCombVector3(eyePos, eyePos, hvec, 0.6);
|
---|
| 567 | break;
|
---|
| 568 | case GLUT_KEY_DOWN: |
---|
| 569 | linCombVector3(eyePos, eyePos, hvec, -0.6); |
---|
| 570 | break; |
---|
| 571 | default: |
---|
| 572 | return; |
---|
| 573 | |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | glutPostRedisplay(); |
---|
| 577 | } |
---|
| 578 | #pragma warning( default : 4100 ) |
---|
| 579 | |
---|
| 580 | |
---|
| 581 | void reshape(const int w, const int h) |
---|
| 582 | { |
---|
| 583 | double winAspectRatio = 1.0; |
---|
| 584 | |
---|
| 585 | glViewport(0, 0, w, h); |
---|
| 586 | |
---|
| 587 | winWidth = w; |
---|
| 588 | winHeight = h; |
---|
| 589 | |
---|
| 590 | if(w) winAspectRatio = (double) h / (double) w; |
---|
| 591 | |
---|
| 592 | perspectiveDeg(eyeProjection, 60.0, 1.0/winAspectRatio, nearDist, 150.0); |
---|
| 593 | |
---|
| 594 | glutPostRedisplay(); |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | |
---|
| 598 | void mouse(int button, int state, int x, int y) |
---|
| 599 | { |
---|
| 600 | if(((button == GLUT_LEFT_BUTTON) || (button == GLUT_RIGHT_BUTTON)) && state == GLUT_DOWN) |
---|
| 601 | { |
---|
| 602 | xEyeBegin = x; |
---|
| 603 | yMotionBegin = y; |
---|
| 604 | |
---|
| 605 | glutMotionFunc(leftMotion); |
---|
| 606 | } |
---|
| 607 | else if(button == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) |
---|
| 608 | { |
---|
| 609 | horizontalMotionBegin = x; |
---|
| 610 | verticalMotionBegin = y; |
---|
| 611 | glutMotionFunc(middleMotion); |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | glutPostRedisplay(); |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | /** |
---|
| 618 | rotation for left/right mouse drag |
---|
| 619 | motion for up/down mouse drag |
---|
| 620 | */ |
---|
| 621 | void leftMotion(int x, int y) |
---|
| 622 | { |
---|
| 623 | static double eyeXAngle = 0.0; |
---|
| 624 | // not move in the vertical direction |
---|
| 625 | Vector3 horView = {viewDir[0], 0, viewDir[2]}; |
---|
| 626 | |
---|
| 627 | eyeXAngle = 0.6 * PI * (xEyeBegin - x) / 180.0; |
---|
| 628 | linCombVector3(eyePos, eyePos, horView, (yMotionBegin - y) * 0.1); |
---|
| 629 | |
---|
| 630 | rotateVectorY(viewDir, eyeXAngle); |
---|
| 631 | |
---|
| 632 | xEyeBegin = x; |
---|
| 633 | yMotionBegin = y; |
---|
| 634 | |
---|
| 635 | glutPostRedisplay(); |
---|
| 636 | } |
---|
| 637 | |
---|
| 638 | // strafe |
---|
| 639 | void middleMotion(int x, int y) |
---|
| 640 | { |
---|
| 641 | // the 90 degree rotated view vector |
---|
| 642 | // y zero so we don't move in the vertical |
---|
| 643 | Vector3 rVec = {viewDir[0], 0, viewDir[2]}; |
---|
| 644 | |
---|
| 645 | rotateVectorY(rVec, PI / 2.0); |
---|
| 646 | linCombVector3(eyePos, eyePos, rVec, (horizontalMotionBegin - x) * 0.1); |
---|
| 647 | |
---|
| 648 | eyePos[1] += (verticalMotionBegin - y) * 0.1; |
---|
| 649 | |
---|
| 650 | horizontalMotionBegin = x; |
---|
| 651 | verticalMotionBegin = y; |
---|
| 652 | |
---|
| 653 | glutPostRedisplay(); |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | |
---|
| 657 | void initExtensions(void) |
---|
| 658 | { |
---|
| 659 | GLenum err = glewInit(); |
---|
| 660 | if(GLEW_OK != err) { |
---|
| 661 | // problem: glewInit failed, something is seriously wrong |
---|
| 662 | fprintf(stderr,"Error: %s\n",glewGetErrorString(err)); |
---|
| 663 | exit(1); |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | if(!GLEW_ARB_occlusion_query) { |
---|
| 667 | printf("I require the GL_ARB_occlusion_query OpenGL extension to work.\n"); |
---|
| 668 | exit(1); |
---|
| 669 | } |
---|
| 670 | } |
---|
| 671 | |
---|
| 672 | |
---|
| 673 | void begin2D(void) |
---|
| 674 | { |
---|
| 675 | glDisable(GL_LIGHTING); |
---|
| 676 | glDisable(GL_DEPTH_TEST); |
---|
| 677 | |
---|
| 678 | glPushMatrix(); |
---|
| 679 | glLoadIdentity(); |
---|
| 680 | |
---|
| 681 | glMatrixMode(GL_PROJECTION); |
---|
| 682 | glPushMatrix(); |
---|
| 683 | glLoadIdentity(); |
---|
| 684 | gluOrtho2D(0, winWidth, winHeight, 0); |
---|
| 685 | } |
---|
| 686 | |
---|
| 687 | |
---|
| 688 | void end2D(void) |
---|
| 689 | { |
---|
| 690 | glPopMatrix(); |
---|
| 691 | glMatrixMode(GL_MODELVIEW); |
---|
| 692 | glPopMatrix(); |
---|
| 693 | |
---|
| 694 | glEnable(GL_LIGHTING); |
---|
| 695 | glEnable(GL_DEPTH_TEST); |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | |
---|
| 699 | void output(const int x, const int y, const char *string) |
---|
| 700 | { |
---|
| 701 | if(string != 0) |
---|
| 702 | { |
---|
| 703 | int len, i; |
---|
| 704 | glRasterPos2f(x,y); |
---|
| 705 | len = (int) strlen(string); |
---|
| 706 | |
---|
| 707 | for (i = 0; i < len; i++) |
---|
| 708 | { |
---|
| 709 | glutBitmapCharacter(GLUT_BITMAP_8_BY_13,string[i]); |
---|
| 710 | } |
---|
| 711 | } |
---|
| 712 | } |
---|
| 713 | |
---|
| 714 | // explicitly deletes geometry generated for hierarchy |
---|
| 715 | // (deleting the hierarchy does not delete the geometry) |
---|
| 716 | void deleteGeometry() |
---|
| 717 | { |
---|
| 718 | for (GeometryList::iterator it = geometry.begin(); it != geometry.end(); it++) |
---|
| 719 | if(*it) delete (*it); |
---|
| 720 | |
---|
| 721 | geometry.clear(); |
---|
| 722 | } |
---|
| 723 | |
---|
| 724 | // displays the visualisation of the kd tree node culling |
---|
| 725 | void displayVisualization() |
---|
| 726 | { |
---|
| 727 | begin2D(); |
---|
| 728 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
---|
| 729 | glEnable(GL_BLEND); |
---|
| 730 | glColor4f(0.0,0.0,0.0,0.5); |
---|
| 731 | |
---|
| 732 | glRecti(winWidth, 0, winWidth / 2, winHeight / 2); |
---|
| 733 | glDisable(GL_BLEND); |
---|
| 734 | end2D(); |
---|
| 735 | |
---|
| 736 | glViewport(winWidth / 2, winHeight / 2, winWidth, winHeight); |
---|
| 737 | glPushMatrix(); |
---|
| 738 | glLoadMatrixd(visView); |
---|
| 739 | |
---|
| 740 | glClear(GL_DEPTH_BUFFER_BIT); |
---|
| 741 | |
---|
| 742 | // --- visualization of the occlusion culling |
---|
| 743 | HierarchyNode::SetRenderBoundingVolume(true); |
---|
| 744 | traverser.RenderVisualization(); |
---|
| 745 | HierarchyNode::SetRenderBoundingVolume(showBoundingVolumes); |
---|
| 746 | |
---|
| 747 | glPopMatrix(); |
---|
| 748 | glViewport(0, 0, winWidth, winHeight); |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | /** |
---|
| 752 | sets up view matrix in order to get a good position |
---|
| 753 | for viewing the kd tree node culling |
---|
| 754 | */ |
---|
| 755 | void setupVisView() |
---|
| 756 | { |
---|
| 757 | const Vector3 up = {0.0, 1.0, 0.0}; |
---|
| 758 | |
---|
| 759 | Vector3 visPos = {24, 23, -6}; |
---|
| 760 | |
---|
| 761 | visPos[0] *= visZoomFactor; |
---|
| 762 | visPos[1] *= visZoomFactor; |
---|
| 763 | visPos[2] *= visZoomFactor; |
---|
| 764 | |
---|
| 765 | Vector3 visDir = {-1.3,-1,-1}; |
---|
| 766 | |
---|
| 767 | normalize(visDir); |
---|
| 768 | look(visView, visPos, visDir, up); |
---|
| 769 | } |
---|
| 770 | |
---|
| 771 | // we take a couple of measurements and compute the average |
---|
| 772 | long calcRenderTime() |
---|
| 773 | { |
---|
| 774 | long result = 0; |
---|
| 775 | |
---|
| 776 | for(int i=0; i<renderTimesValid; i++) |
---|
| 777 | result += renderTimes[i]; |
---|
| 778 | |
---|
| 779 | if(renderTimesValid) |
---|
| 780 | result /= renderTimesValid; |
---|
| 781 | |
---|
| 782 | return result; |
---|
| 783 | } |
---|
| 784 | |
---|
| 785 | |
---|
| 786 | // reset the timer array for a new traversal method |
---|
| 787 | void resetTimer() |
---|
| 788 | { |
---|
| 789 | renderTimesValid = 0; |
---|
| 790 | renderTimesIdx = 0; |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | // cleanup routine after the main loop |
---|
| 794 | void cleanUp() |
---|
| 795 | { |
---|
| 796 | if(traverser.GetHierarchy()) |
---|
| 797 | delete traverser.GetHierarchy(); |
---|
| 798 | |
---|
| 799 | deleteGeometry(); |
---|
| 800 | |
---|
| 801 | Geometry::CleanUp(); |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | // this function inserts a dezimal point after each 1000 |
---|
| 805 | void calcDecimalPoint(string &str, int d) |
---|
| 806 | { |
---|
| 807 | vector<int> numbers; |
---|
| 808 | char hstr[100]; |
---|
| 809 | |
---|
| 810 | while(d != 0) |
---|
| 811 | { |
---|
| 812 | numbers.push_back(d % 1000); |
---|
| 813 | d /= 1000; |
---|
| 814 | } |
---|
| 815 | |
---|
| 816 | // first element without leading zeros |
---|
| 817 | if(numbers.size() > 0) |
---|
| 818 | { |
---|
| 819 | sprintf(hstr, "%d", numbers.back()); |
---|
| 820 | str.append(hstr); |
---|
| 821 | } |
---|
| 822 | |
---|
| 823 | for(int i=numbers.size()-2; i >= 0; i--) |
---|
| 824 | { |
---|
| 825 | sprintf(hstr, ",%03d", numbers[i]); |
---|
| 826 | str.append(hstr); |
---|
| 827 | } |
---|
| 828 | } |
---|