1 | // occquery.cpp : Defines the entry point for the console application.
|
---|
2 | //
|
---|
3 | #include <math.h>
|
---|
4 | #include <time.h>
|
---|
5 | #include "common.h"
|
---|
6 | #include "glInterface.h"
|
---|
7 | #include "RenderTraverser.h"
|
---|
8 | #include "SceneEntity.h"
|
---|
9 | #include "Vector3.h"
|
---|
10 | #include "Matrix4x4.h"
|
---|
11 | #include "BinaryLoader.h"
|
---|
12 | #include "Bvh.h"
|
---|
13 | #include "Camera.h"
|
---|
14 | #include "Geometry.h"
|
---|
15 | #include "BvhLoader.h"
|
---|
16 | #include "FrustumCullingTraverser.h"
|
---|
17 | #include "StopAndWaitTraverser.h"
|
---|
18 | #include "CHCTraverser.h"
|
---|
19 | #include "CHCPlusPlusTraverser.h"
|
---|
20 | #include "Visualization.h"
|
---|
21 | #include "RenderState.h"
|
---|
22 |
|
---|
23 |
|
---|
24 |
|
---|
25 | using namespace std;
|
---|
26 | using namespace CHCDemoEngine;
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 | /// the renderable scene geometry
|
---|
31 | SceneEntityContainer sceneEntities;
|
---|
32 | // traverses and renders the hierarchy
|
---|
33 | RenderTraverser *traverser = NULL;
|
---|
34 | /// the hierarchy
|
---|
35 | Bvh *bvh = NULL;
|
---|
36 | /// the scene camera
|
---|
37 | Camera *camera = NULL;
|
---|
38 | /// the visualization
|
---|
39 | Visualization *visualization = NULL;
|
---|
40 | /// the current render state
|
---|
41 | RenderState state;
|
---|
42 | /// the rendering algorithm
|
---|
43 | int renderMode = RenderTraverser::CULL_FRUSTUM;
|
---|
44 | // eye near plane distance
|
---|
45 | float nearDist = 0.1f;
|
---|
46 | /// the pixel threshold where a node is still considered invisible
|
---|
47 | int threshold;
|
---|
48 |
|
---|
49 | int assumedVisibleFrames = 10;
|
---|
50 | int maxBatchSize = 50;
|
---|
51 | bool useMultiQueries = true;
|
---|
52 |
|
---|
53 | const float keyForwardMotion = 1.0f;
|
---|
54 | const float keyRotation = 0.2f;
|
---|
55 |
|
---|
56 | bool useRenderQueue = false;
|
---|
57 |
|
---|
58 | int winWidth = 1024;
|
---|
59 | int winHeight = 768;
|
---|
60 | float winAspectRatio = 1.0f;
|
---|
61 |
|
---|
62 | float visZoomFactor = 1.5f;
|
---|
63 |
|
---|
64 | double accumulatedTime = 1000;
|
---|
65 | float fps = 1e3f;
|
---|
66 |
|
---|
67 | int renderedObjects = 0;
|
---|
68 | int renderedNodes = 0;
|
---|
69 | int renderedTriangles = 0;
|
---|
70 |
|
---|
71 | int issuedQueries = 0;
|
---|
72 | int traversedNodes = 0;
|
---|
73 | int frustumCulledNodes = 0;
|
---|
74 | int queryCulledNodes = 0;
|
---|
75 | int stateChanges = 0;
|
---|
76 |
|
---|
77 | bool showHelp = false;
|
---|
78 | bool showStatistics = true;
|
---|
79 | bool showBoundingVolumes = false;
|
---|
80 | bool visMode = false;
|
---|
81 |
|
---|
82 | //mouse navigation state
|
---|
83 | int xEyeBegin, yEyeBegin, yMotionBegin, verticalMotionBegin, horizontalMotionBegin = 0;
|
---|
84 |
|
---|
85 | bool useOptimization = false;
|
---|
86 |
|
---|
87 |
|
---|
88 | void InitExtensions();
|
---|
89 | void DisplayVisualization();
|
---|
90 | void InitGLstate();
|
---|
91 | void CleanUp();
|
---|
92 | void SetupEyeView();
|
---|
93 | void UpdateEyeMtx();
|
---|
94 | void SetupLighting();
|
---|
95 | void DisplayStats();
|
---|
96 | void Output(int x, int y, const char *string);
|
---|
97 |
|
---|
98 | void begin2D();
|
---|
99 | void end2D();
|
---|
100 | void keyboard(unsigned char c, int x, int y);
|
---|
101 | void drawHelpMessage();
|
---|
102 | void drawStatistics();
|
---|
103 | void display(void);
|
---|
104 | void special(int c, int x, int y);
|
---|
105 | void reshape(int w, int h);
|
---|
106 | void mouse(int button, int state, int x, int y);
|
---|
107 | void leftMotion(int x, int y);
|
---|
108 | void rightMotion(int x, int y);
|
---|
109 | void middleMotion(int x, int y);
|
---|
110 | void drawEyeView(void);
|
---|
111 | void setupVisView(void);
|
---|
112 | void CalcDecimalPoint(std::string &str, int d);
|
---|
113 | void ResetTraverser();
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 | int main(int argc, char* argv[])
|
---|
118 | {
|
---|
119 | camera = new Camera(winWidth, winHeight);
|
---|
120 | camera->SetNear(nearDist);
|
---|
121 |
|
---|
122 | glutInitWindowSize(winWidth, winHeight);
|
---|
123 | glutInit(&argc, argv);
|
---|
124 | glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
|
---|
125 |
|
---|
126 | glutCreateWindow("Coherent Hierarchical Culling");
|
---|
127 |
|
---|
128 | glutDisplayFunc(display);
|
---|
129 | glutKeyboardFunc(keyboard);
|
---|
130 | glutSpecialFunc(special);
|
---|
131 | glutReshapeFunc(reshape);
|
---|
132 | glutMouseFunc(mouse);
|
---|
133 | glutIdleFunc(display);
|
---|
134 |
|
---|
135 | InitExtensions();
|
---|
136 | InitGLstate();
|
---|
137 |
|
---|
138 | leftMotion(0, 0);
|
---|
139 | middleMotion(0, 0);
|
---|
140 |
|
---|
141 | BinaryLoader loader;
|
---|
142 |
|
---|
143 | //const string filename("house_test.dem");
|
---|
144 | //const string filename("city_demo.dem");
|
---|
145 | const string filename("city.dem");
|
---|
146 | //const string filename("roofs.dem");
|
---|
147 |
|
---|
148 | if (loader.Load(filename, sceneEntities))
|
---|
149 | cout << "scene " << filename << " loaded" << endl;
|
---|
150 | else
|
---|
151 | cerr << "loading scene " << filename << " failed" << endl;
|
---|
152 |
|
---|
153 | BvhLoader bvhLoader;
|
---|
154 | //bvh = bvhLoader.Load("roofs.bvh", sceneEntities);
|
---|
155 | bvh = bvhLoader.Load("city.bvh", sceneEntities);
|
---|
156 | //bvh = bvhLoader.Load("city_demo.bvh", sceneEntities);
|
---|
157 | //bvh = bvhLoader.Load("house_test.bvh", sceneEntities);
|
---|
158 |
|
---|
159 | bvh->SetCamera(camera);
|
---|
160 | ResetTraverser();
|
---|
161 |
|
---|
162 | //camera->LookAtBox(sceneBox);
|
---|
163 | //camera->LookInBox(bvh->GetBox());
|
---|
164 |
|
---|
165 | camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f));
|
---|
166 | camera->SetPosition(Vector3(483.398f, 242.364f, 186.078f));
|
---|
167 |
|
---|
168 | glutMainLoop();
|
---|
169 |
|
---|
170 | // clean up
|
---|
171 | CleanUp();
|
---|
172 |
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | void InitGLstate(void)
|
---|
178 | {
|
---|
179 | glClearColor(0.5f, 0.5f, 0.8f, 0.0f);
|
---|
180 |
|
---|
181 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
---|
182 | glPixelStorei(GL_PACK_ALIGNMENT,1);
|
---|
183 |
|
---|
184 | glDepthFunc(GL_LESS);
|
---|
185 | glEnable(GL_DEPTH_TEST);
|
---|
186 |
|
---|
187 | SetupLighting();
|
---|
188 |
|
---|
189 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
190 | glShadeModel(GL_SMOOTH);
|
---|
191 |
|
---|
192 | glMaterialf(GL_FRONT, GL_SHININESS, 64);
|
---|
193 | glEnable(GL_NORMALIZE);
|
---|
194 |
|
---|
195 | //glEnable(GL_ALPHA_TEST);
|
---|
196 | glDisable(GL_ALPHA_TEST);
|
---|
197 | glAlphaFunc(GL_GEQUAL, 0.1f);
|
---|
198 |
|
---|
199 | glFrontFace(GL_CCW);
|
---|
200 | glCullFace(GL_BACK);
|
---|
201 | glEnable(GL_CULL_FACE);
|
---|
202 | //glDisable(GL_CULL_FACE);
|
---|
203 |
|
---|
204 |
|
---|
205 | GLfloat ambientColor[] = {0.5, 0.5, 0.5, 1.0};
|
---|
206 | GLfloat diffuseColor[] = {1.0, 0.0, 0.0, 1.0};
|
---|
207 | GLfloat specularColor[] = {0.0, 0.0, 0.0, 1.0};
|
---|
208 |
|
---|
209 | glMaterialfv(GL_FRONT, GL_AMBIENT, ambientColor);
|
---|
210 | glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseColor);
|
---|
211 | glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
|
---|
212 | //setupVisView();
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | void drawHelpMessage(void)
|
---|
217 | {
|
---|
218 | const char *message[] =
|
---|
219 | {
|
---|
220 | "Help information",
|
---|
221 | "",
|
---|
222 | "'F1' - shows/dismisses this message",
|
---|
223 | "",
|
---|
224 | "'MOUSE-LEFT' - turn left/right, move forward/backward",
|
---|
225 | "'MOUSE-RIGHT' - turn left/right, move forward/backward",
|
---|
226 | "'MOUSE-MIDDLE' - move up/down, left/right",
|
---|
227 | "'CURSOR UP' - move forward",
|
---|
228 | "'CURSOR BACK' - move backward",
|
---|
229 | "'CURSOR RIGHT' - turn right",
|
---|
230 | "'CURSOR LEFT' - turn left",
|
---|
231 | "",
|
---|
232 | "'SPACE' - cycles through occlusion culling algorithms",
|
---|
233 | "'-' - decreases max batch size",
|
---|
234 | "'+' - increases max batch size",
|
---|
235 | "'6' - decrease assumed visible frames",
|
---|
236 | "'7' - increase assumed visible frames",
|
---|
237 | "'8' - upward motion",
|
---|
238 | "'9' - downward motion",
|
---|
239 | "",
|
---|
240 | "'G' - enables/disables optimization to take geometry as occluder",
|
---|
241 | "",
|
---|
242 | "'S' - shows/hides statistics",
|
---|
243 | "'V' - shows/hides bounding volumes",
|
---|
244 | "",
|
---|
245 | "'1' - shows/hides visualization",
|
---|
246 | "'2' - zooms out visualization",
|
---|
247 | "'3' - zooms in visualization",
|
---|
248 | 0,
|
---|
249 | };
|
---|
250 |
|
---|
251 |
|
---|
252 | int x = 40, y = 42;
|
---|
253 |
|
---|
254 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
255 | glEnable(GL_BLEND);
|
---|
256 | glColor4f(0.0f, 1.0f , 0.0f, 0.2f); // 20% green.
|
---|
257 |
|
---|
258 | // Drawn clockwise because the flipped Y axis flips CCW and CW.
|
---|
259 | glRecti(winWidth - 30, 30, 30, winHeight - 30);
|
---|
260 |
|
---|
261 | glDisable(GL_BLEND);
|
---|
262 |
|
---|
263 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
264 |
|
---|
265 | for(int i = 0; message[i] != 0; i++)
|
---|
266 | {
|
---|
267 | if(message[i][0] == '\0')
|
---|
268 | {
|
---|
269 | y += 7;
|
---|
270 | }
|
---|
271 | else
|
---|
272 | {
|
---|
273 | Output(x, y, message[i]);
|
---|
274 | y += 14;
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | }
|
---|
279 |
|
---|
280 |
|
---|
281 | void ResetTraverser()
|
---|
282 | {
|
---|
283 | DEL_PTR(traverser);
|
---|
284 |
|
---|
285 | bvh->ResetNodeClassifications();
|
---|
286 |
|
---|
287 | switch (renderMode)
|
---|
288 | {
|
---|
289 | case RenderTraverser::CULL_FRUSTUM:
|
---|
290 | traverser = new FrustumCullingTraverser();
|
---|
291 | break;
|
---|
292 | case RenderTraverser::STOP_AND_WAIT:
|
---|
293 | traverser = new StopAndWaitTraverser();
|
---|
294 | break;
|
---|
295 | case RenderTraverser::CHC:
|
---|
296 | traverser = new CHCTraverser();
|
---|
297 | break;
|
---|
298 | case RenderTraverser::CHCPLUSPLUS:
|
---|
299 | traverser = new CHCPlusPlusTraverser();
|
---|
300 | break;
|
---|
301 |
|
---|
302 | default:
|
---|
303 | traverser = new FrustumCullingTraverser();
|
---|
304 | }
|
---|
305 |
|
---|
306 | traverser->SetCamera(camera);
|
---|
307 | traverser->SetHierarchy(bvh);
|
---|
308 | traverser->SetRenderState(&state);
|
---|
309 | traverser->SetUseOptimization(useOptimization);
|
---|
310 | traverser->SetUseRenderQueue(useRenderQueue);
|
---|
311 | traverser->SetVisibilityThreshold(threshold);
|
---|
312 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
313 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
314 | traverser->SetUseMultiQueries(useMultiQueries);
|
---|
315 | }
|
---|
316 |
|
---|
317 |
|
---|
318 | void SetupLighting()
|
---|
319 | {
|
---|
320 | glEnable(GL_LIGHTING);
|
---|
321 | glEnable(GL_LIGHT0);
|
---|
322 | glEnable(GL_LIGHT1);
|
---|
323 |
|
---|
324 | //glDisable(GL_LIGHT1);
|
---|
325 | //glDisable(GL_LIGHTING);
|
---|
326 |
|
---|
327 | //GLfloat ambient[] = {0.5, 0.5, 0.5, 1.0};
|
---|
328 | GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
|
---|
329 | GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
|
---|
330 | GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};
|
---|
331 |
|
---|
332 | GLfloat lmodel_ambient[] = {0.5f, 0.5f, 0.5f, 1.0f};
|
---|
333 | //GLfloat lmodel_ambient[] = {0.2f, 0.2f, 0.2f, 1.0f};
|
---|
334 |
|
---|
335 | glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
|
---|
336 | glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
|
---|
337 | glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
|
---|
338 |
|
---|
339 | GLfloat position[] = {1.0, 1.0, 1.0, 0.0};
|
---|
340 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
341 |
|
---|
342 |
|
---|
343 | ////////////
|
---|
344 | //-- second light
|
---|
345 |
|
---|
346 | GLfloat ambient1[] = {0.5, 0.5, 0.5, 1.0};
|
---|
347 | //GLfloat diffuse1[] = {1.0, 1.0, 1.0, 1.0};
|
---|
348 | GLfloat diffuse1[] = {0.5, 0.5, 0.5, 1.0};
|
---|
349 | GLfloat specular1[] = {0.5, 0.5, 0.5, 1.0};
|
---|
350 |
|
---|
351 | glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
|
---|
352 | glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);
|
---|
353 | glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
|
---|
354 |
|
---|
355 | GLfloat position1[] = {0.0, 1.0, 0.0, 1.0};
|
---|
356 | glLightfv(GL_LIGHT1, GL_POSITION, position1);
|
---|
357 |
|
---|
358 | glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
|
---|
359 | glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
|
---|
360 | //glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
|
---|
361 | //glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SEPARATE_SPECULAR_COLOR_EXT);
|
---|
362 | glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL_EXT, GL_SINGLE_COLOR_EXT);
|
---|
363 |
|
---|
364 | }
|
---|
365 |
|
---|
366 | void SetupEyeView(void)
|
---|
367 | {
|
---|
368 | glMatrixMode(GL_PROJECTION);
|
---|
369 | glLoadIdentity();
|
---|
370 |
|
---|
371 | gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal()));
|
---|
372 |
|
---|
373 | glMatrixMode(GL_MODELVIEW);
|
---|
374 | camera->SetupCameraView();
|
---|
375 |
|
---|
376 | GLfloat position[] = {0.8f, 1.0f, 1.5f, 0.0f};
|
---|
377 | glLightfv(GL_LIGHT0, GL_POSITION, position);
|
---|
378 |
|
---|
379 | GLfloat position1[] = {bvh->GetBox().Center().x, bvh->GetBox().Max().y, bvh->GetBox().Center().z, 1.0f};
|
---|
380 | glLightfv(GL_LIGHT1, GL_POSITION, position1);
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | void display(void)
|
---|
385 | {
|
---|
386 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
387 |
|
---|
388 | // bring eye modelview matrix up-to-date
|
---|
389 | SetupEyeView();
|
---|
390 |
|
---|
391 | //cout << camera->GetDirection() << " " << camera->GetPosition() << endl;
|
---|
392 | // actually render the scene geometry using one of the specified algorithms
|
---|
393 | traverser->RenderScene();
|
---|
394 |
|
---|
395 | //if(visMode) displayVisualization();
|
---|
396 |
|
---|
397 | DisplayStats();
|
---|
398 |
|
---|
399 | glutSwapBuffers();
|
---|
400 | }
|
---|
401 |
|
---|
402 |
|
---|
403 | #pragma warning( disable : 4100 )
|
---|
404 | void keyboard(const unsigned char c, const int x, const int y)
|
---|
405 | {
|
---|
406 | // used to avoid vertical motion with the keys
|
---|
407 | Vector3 hvec = Vector3(camera->GetDirection()[0], camera->GetDirection()[1], 0.0f);
|
---|
408 | Vector3 uvec = Vector3(0, 0, 1);
|
---|
409 |
|
---|
410 | switch(c)
|
---|
411 | {
|
---|
412 | case 27:
|
---|
413 | exit(0);
|
---|
414 | break;
|
---|
415 | case 32: //space
|
---|
416 | renderMode = (renderMode + 1) % RenderTraverser::NUM_RENDERMODES;
|
---|
417 | ResetTraverser();
|
---|
418 | break;
|
---|
419 | case 'h':
|
---|
420 | case 'H':
|
---|
421 | showHelp = !showHelp;
|
---|
422 | break;
|
---|
423 | case 'v':
|
---|
424 | case 'V':
|
---|
425 | showBoundingVolumes = !showBoundingVolumes;
|
---|
426 | //HierarchyNode::SetRenderBoundingVolume(showBoundingVolumes);
|
---|
427 | break;
|
---|
428 | case 'O':
|
---|
429 | case 'o':
|
---|
430 | showStatistics = !showStatistics;
|
---|
431 | break;
|
---|
432 | case '+':
|
---|
433 | maxBatchSize += 10;
|
---|
434 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
435 | break;
|
---|
436 | case '-':
|
---|
437 | maxBatchSize -= 10;
|
---|
438 | if (maxBatchSize < 0) maxBatchSize = 1;
|
---|
439 | traverser->SetMaxBatchSize(maxBatchSize);
|
---|
440 | break;
|
---|
441 | case '6':
|
---|
442 | assumedVisibleFrames -= 1;
|
---|
443 | if (assumedVisibleFrames < 1) assumedVisibleFrames = 1;
|
---|
444 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
445 | break;
|
---|
446 | case '7':
|
---|
447 | assumedVisibleFrames += 1;
|
---|
448 | traverser->SetAssumedVisibleFrames(assumedVisibleFrames);
|
---|
449 | break;
|
---|
450 | case 'M':
|
---|
451 | case 'm':
|
---|
452 | useMultiQueries = !useMultiQueries;
|
---|
453 | traverser->SetUseMultiQueries(useMultiQueries);
|
---|
454 | break;
|
---|
455 | case '1':
|
---|
456 | visMode = !visMode;
|
---|
457 | break;
|
---|
458 | case '2':
|
---|
459 | visZoomFactor += 0.1;
|
---|
460 | setupVisView();
|
---|
461 | break;
|
---|
462 | case '3':
|
---|
463 | visZoomFactor -= 0.1;
|
---|
464 | if(visZoomFactor < 0.1) visZoomFactor = 0.1;
|
---|
465 |
|
---|
466 | setupVisView();
|
---|
467 | break;
|
---|
468 | case '8':
|
---|
469 | {
|
---|
470 | Vector3 pos = camera->GetPosition();
|
---|
471 | pos += uvec * keyForwardMotion;
|
---|
472 | camera->SetPosition(pos);
|
---|
473 | break;
|
---|
474 | }
|
---|
475 | case '9':
|
---|
476 | {
|
---|
477 | Vector3 pos = camera->GetPosition();
|
---|
478 | pos -= uvec * keyForwardMotion;
|
---|
479 | camera->SetPosition(pos);
|
---|
480 | break;
|
---|
481 | }
|
---|
482 | case 'g':
|
---|
483 | case 'G':
|
---|
484 | useOptimization = !useOptimization;
|
---|
485 | traverser->SetUseOptimization(useOptimization);
|
---|
486 | break;
|
---|
487 | case 'a':
|
---|
488 | case 'A':
|
---|
489 | {
|
---|
490 | Vector3 viewDir = camera->GetDirection();
|
---|
491 | // rotate view vector
|
---|
492 | Matrix4x4 rot = RotationZMatrix(keyRotation);
|
---|
493 | viewDir = rot * viewDir;
|
---|
494 | camera->SetDirection(viewDir);
|
---|
495 | }
|
---|
496 | break;
|
---|
497 | case 'd':
|
---|
498 | case 'D':
|
---|
499 | {
|
---|
500 | Vector3 viewDir = camera->GetDirection();
|
---|
501 | // rotate view vector
|
---|
502 | Matrix4x4 rot = RotationZMatrix(-keyRotation);
|
---|
503 | viewDir = rot * viewDir;
|
---|
504 | camera->SetDirection(viewDir);
|
---|
505 | }
|
---|
506 | break;
|
---|
507 | case 'w':
|
---|
508 | case 'W':
|
---|
509 | {
|
---|
510 | Vector3 pos = camera->GetPosition();
|
---|
511 | pos += hvec * keyForwardMotion;
|
---|
512 | camera->SetPosition(pos);
|
---|
513 | }
|
---|
514 | break;
|
---|
515 | case 'x':
|
---|
516 | case 'X':
|
---|
517 | {
|
---|
518 | Vector3 pos = camera->GetPosition();
|
---|
519 | pos -= hvec * keyForwardMotion;
|
---|
520 | camera->SetPosition(pos);
|
---|
521 | }
|
---|
522 | break;
|
---|
523 | case 'r':
|
---|
524 | case 'R':
|
---|
525 | {
|
---|
526 | useRenderQueue = !useRenderQueue;
|
---|
527 | traverser->SetUseRenderQueue(useRenderQueue);
|
---|
528 | }
|
---|
529 | default:
|
---|
530 | return;
|
---|
531 | }
|
---|
532 |
|
---|
533 | glutPostRedisplay();
|
---|
534 | }
|
---|
535 |
|
---|
536 |
|
---|
537 | void special(const int c, const int x, const int y)
|
---|
538 | {
|
---|
539 | // used to avoid vertical motion with the keys
|
---|
540 | Vector3 hvec = Vector3(camera->GetDirection()[0], camera->GetDirection()[1], 0.0f);
|
---|
541 |
|
---|
542 | switch(c)
|
---|
543 | {
|
---|
544 | case GLUT_KEY_F1:
|
---|
545 | showHelp = !showHelp;
|
---|
546 | break;
|
---|
547 | case GLUT_KEY_LEFT:
|
---|
548 | {
|
---|
549 | Vector3 viewDir = camera->GetDirection();
|
---|
550 | // rotate view vector
|
---|
551 | Matrix4x4 rot = RotationZMatrix(keyRotation);
|
---|
552 | viewDir = rot * viewDir;
|
---|
553 | camera->SetDirection(viewDir);
|
---|
554 | }
|
---|
555 | break;
|
---|
556 | case GLUT_KEY_RIGHT:
|
---|
557 | {
|
---|
558 | Vector3 viewDir = camera->GetDirection();
|
---|
559 | // rotate view vector
|
---|
560 | Matrix4x4 rot = RotationZMatrix(-keyRotation);
|
---|
561 | viewDir = rot * viewDir;
|
---|
562 | camera->SetDirection(viewDir);
|
---|
563 | }
|
---|
564 | break;
|
---|
565 | case GLUT_KEY_UP:
|
---|
566 | {
|
---|
567 | Vector3 pos = camera->GetPosition();
|
---|
568 | pos += hvec * 0.6f;
|
---|
569 | camera->SetPosition(pos);
|
---|
570 | }
|
---|
571 | break;
|
---|
572 | case GLUT_KEY_DOWN:
|
---|
573 | {
|
---|
574 | Vector3 pos = camera->GetPosition();
|
---|
575 | pos -= hvec * 0.6f;
|
---|
576 | camera->SetPosition(pos);
|
---|
577 | }
|
---|
578 | break;
|
---|
579 | default:
|
---|
580 | return;
|
---|
581 |
|
---|
582 | }
|
---|
583 |
|
---|
584 | glutPostRedisplay();
|
---|
585 | }
|
---|
586 |
|
---|
587 | #pragma warning( default : 4100 )
|
---|
588 |
|
---|
589 |
|
---|
590 | void reshape(const int w, const int h)
|
---|
591 | {
|
---|
592 | winAspectRatio = 1.0f;
|
---|
593 |
|
---|
594 | glViewport(0, 0, w, h);
|
---|
595 |
|
---|
596 | winWidth = w;
|
---|
597 | winHeight = h;
|
---|
598 |
|
---|
599 | if (w) winAspectRatio = (float) h / (float) w;
|
---|
600 |
|
---|
601 | glMatrixMode(GL_PROJECTION);
|
---|
602 | glLoadIdentity();
|
---|
603 |
|
---|
604 | gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal()));
|
---|
605 | glMatrixMode(GL_MODELVIEW);
|
---|
606 |
|
---|
607 | glutPostRedisplay();
|
---|
608 | }
|
---|
609 |
|
---|
610 |
|
---|
611 | void mouse(int button, int state, int x, int y)
|
---|
612 | {
|
---|
613 | if ((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN))
|
---|
614 | {
|
---|
615 | xEyeBegin = x;
|
---|
616 | yMotionBegin = y;
|
---|
617 |
|
---|
618 | glutMotionFunc(leftMotion);
|
---|
619 | }
|
---|
620 | else if ((button == GLUT_RIGHT_BUTTON) && (state == GLUT_DOWN))
|
---|
621 | {
|
---|
622 | yEyeBegin = y;
|
---|
623 | yMotionBegin = y;
|
---|
624 |
|
---|
625 | glutMotionFunc(rightMotion);
|
---|
626 | }
|
---|
627 | else if ((button == GLUT_MIDDLE_BUTTON) && (state == GLUT_DOWN))
|
---|
628 | {
|
---|
629 | horizontalMotionBegin = x;
|
---|
630 | verticalMotionBegin = y;
|
---|
631 | glutMotionFunc(middleMotion);
|
---|
632 | }
|
---|
633 |
|
---|
634 | glutPostRedisplay();
|
---|
635 | }
|
---|
636 |
|
---|
637 |
|
---|
638 | /** rotation for left/right mouse drag
|
---|
639 | motion for up/down mouse drag
|
---|
640 | */
|
---|
641 | void leftMotion(int x, int y)
|
---|
642 | {
|
---|
643 | static float eyeXAngle = 0.0f;
|
---|
644 |
|
---|
645 | Vector3 viewDir = camera->GetDirection();
|
---|
646 | Vector3 pos = camera->GetPosition();
|
---|
647 |
|
---|
648 | // don't move in the vertical direction
|
---|
649 | Vector3 horView(viewDir[0], viewDir[1], 0);
|
---|
650 |
|
---|
651 | eyeXAngle = 0.2f * M_PI * (xEyeBegin - x) / 180.0;
|
---|
652 |
|
---|
653 | // rotate view vector
|
---|
654 | Matrix4x4 rot = RotationZMatrix(eyeXAngle);
|
---|
655 | viewDir = rot * viewDir;
|
---|
656 |
|
---|
657 | pos += horView * (yMotionBegin - y) * 0.2f;
|
---|
658 |
|
---|
659 | camera->SetDirection(viewDir);
|
---|
660 | camera->SetPosition(pos);
|
---|
661 |
|
---|
662 | xEyeBegin = x;
|
---|
663 | yMotionBegin = y;
|
---|
664 |
|
---|
665 | glutPostRedisplay();
|
---|
666 | }
|
---|
667 |
|
---|
668 |
|
---|
669 | /** rotation for left / right mouse drag
|
---|
670 | motion for up / down mouse drag
|
---|
671 | */
|
---|
672 | void rightMotion(int x, int y)
|
---|
673 | {
|
---|
674 | static float eyeYAngle = 0.0f;
|
---|
675 |
|
---|
676 | Vector3 viewDir = camera->GetDirection();
|
---|
677 | Vector3 right = camera->GetRightVector();
|
---|
678 |
|
---|
679 | eyeYAngle = -0.2f * M_PI * (yEyeBegin - y) / 180.0;
|
---|
680 |
|
---|
681 | // rotate view vector
|
---|
682 | Matrix4x4 rot = RotationAxisMatrix(right, eyeYAngle);
|
---|
683 | viewDir = rot * viewDir;
|
---|
684 |
|
---|
685 | camera->SetDirection(viewDir);
|
---|
686 |
|
---|
687 | yEyeBegin = y;
|
---|
688 |
|
---|
689 | glutPostRedisplay();
|
---|
690 | }
|
---|
691 |
|
---|
692 |
|
---|
693 | // strafe
|
---|
694 | void middleMotion(int x, int y)
|
---|
695 | {
|
---|
696 | Vector3 viewDir = camera->GetDirection();
|
---|
697 | Vector3 pos = camera->GetPosition();
|
---|
698 |
|
---|
699 | // the 90 degree rotated view vector
|
---|
700 | // y zero so we don't move in the vertical
|
---|
701 | //Vector3 rVec(viewDir[0], 0, viewDir[2]);
|
---|
702 | Vector3 rVec(viewDir[0], viewDir[1], 0);
|
---|
703 |
|
---|
704 | //Matrix4x4 rot = RotationYMatrix(M_PI * 0.5f);
|
---|
705 | Matrix4x4 rot = RotationZMatrix(M_PI * 0.5f);
|
---|
706 | rVec = rot * rVec;
|
---|
707 |
|
---|
708 | pos -= rVec * (x - horizontalMotionBegin) * 0.1f;
|
---|
709 | //pos[1] += (verticalMotionBegin - y) * 0.1f;
|
---|
710 | pos[2] += (verticalMotionBegin - y) * 0.1f;
|
---|
711 |
|
---|
712 | camera->SetPosition(pos);
|
---|
713 |
|
---|
714 | horizontalMotionBegin = x;
|
---|
715 | verticalMotionBegin = y;
|
---|
716 |
|
---|
717 | glutPostRedisplay();
|
---|
718 | }
|
---|
719 |
|
---|
720 |
|
---|
721 | void InitExtensions(void)
|
---|
722 | {
|
---|
723 | GLenum err = glewInit();
|
---|
724 |
|
---|
725 | if (GLEW_OK != err)
|
---|
726 | {
|
---|
727 | // problem: glewInit failed, something is seriously wrong
|
---|
728 | fprintf(stderr,"Error: %s\n", glewGetErrorString(err));
|
---|
729 | exit(1);
|
---|
730 | }
|
---|
731 | if (!GLEW_ARB_occlusion_query)
|
---|
732 | {
|
---|
733 | printf("I require the GL_ARB_occlusion_query to work.\n");
|
---|
734 | exit(1);
|
---|
735 | }
|
---|
736 | }
|
---|
737 |
|
---|
738 |
|
---|
739 | void begin2D(void)
|
---|
740 | {
|
---|
741 | glDisable(GL_LIGHTING);
|
---|
742 | glDisable(GL_DEPTH_TEST);
|
---|
743 |
|
---|
744 | glPushMatrix();
|
---|
745 | glLoadIdentity();
|
---|
746 |
|
---|
747 | glMatrixMode(GL_PROJECTION);
|
---|
748 | glPushMatrix();
|
---|
749 | glLoadIdentity();
|
---|
750 | gluOrtho2D(0, winWidth, winHeight, 0);
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 | void end2D(void)
|
---|
755 | {
|
---|
756 | glPopMatrix();
|
---|
757 | glMatrixMode(GL_MODELVIEW);
|
---|
758 | glPopMatrix();
|
---|
759 |
|
---|
760 | glEnable(GL_LIGHTING);
|
---|
761 | glEnable(GL_DEPTH_TEST);
|
---|
762 | }
|
---|
763 |
|
---|
764 |
|
---|
765 | void Output(int x, int y, const char *string)
|
---|
766 | {
|
---|
767 | if (string != 0)
|
---|
768 | {
|
---|
769 | size_t len, i;
|
---|
770 | glRasterPos2f(x, y);
|
---|
771 | len = strlen(string);
|
---|
772 |
|
---|
773 | for (i = 0; i < len; ++ i)
|
---|
774 | {
|
---|
775 | glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
|
---|
776 | }
|
---|
777 | }
|
---|
778 | }
|
---|
779 |
|
---|
780 |
|
---|
781 | // displays the visualisation of the kd tree node culling
|
---|
782 | void displayVisualization()
|
---|
783 | {
|
---|
784 | begin2D();
|
---|
785 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
---|
786 | glEnable(GL_BLEND);
|
---|
787 | glColor4f(0.0,0.0,0.0,0.5);
|
---|
788 |
|
---|
789 | glRecti(winWidth, 0, winWidth / 2, winHeight / 2);
|
---|
790 | glDisable(GL_BLEND);
|
---|
791 | end2D();
|
---|
792 |
|
---|
793 | glViewport(winWidth / 2, winHeight / 2, winWidth, winHeight);
|
---|
794 | glPushMatrix();
|
---|
795 | //glLoadMatrixf((float *)visView.x);
|
---|
796 |
|
---|
797 | glClear(GL_DEPTH_BUFFER_BIT);
|
---|
798 |
|
---|
799 | ////////////
|
---|
800 | // --- visualization of the occlusion culling
|
---|
801 | visualization->Render();
|
---|
802 |
|
---|
803 | glPopMatrix();
|
---|
804 |
|
---|
805 | glViewport(0, 0, winWidth, winHeight);
|
---|
806 | }
|
---|
807 |
|
---|
808 |
|
---|
809 | /** Sets up view matrix in order to get a good position
|
---|
810 | for viewing the kd tree node culling
|
---|
811 | */
|
---|
812 | void setupVisView()
|
---|
813 | {
|
---|
814 | const Vector3 up(0.0, 0.0, 1.0);
|
---|
815 |
|
---|
816 | Vector3 visPos(24, 23, -6);
|
---|
817 |
|
---|
818 | visPos[0] *= visZoomFactor;
|
---|
819 | visPos[1] *= visZoomFactor;
|
---|
820 | visPos[2] *= visZoomFactor;
|
---|
821 |
|
---|
822 | Vector3 visDir = Vector3(-1.3f, -1, -1);
|
---|
823 | }
|
---|
824 |
|
---|
825 |
|
---|
826 | // cleanup routine after the main loop
|
---|
827 | void CleanUp()
|
---|
828 | {
|
---|
829 | CLEAR_CONTAINER(sceneEntities);
|
---|
830 | DEL_PTR(traverser);
|
---|
831 |
|
---|
832 | DEL_PTR(bvh);
|
---|
833 | DEL_PTR(visualization);
|
---|
834 | }
|
---|
835 |
|
---|
836 |
|
---|
837 | // this function inserts a dezimal point after each 1000
|
---|
838 | void CalcDecimalPoint(string &str, int d)
|
---|
839 | {
|
---|
840 | vector<int> numbers;
|
---|
841 | char hstr[100];
|
---|
842 |
|
---|
843 | while (d != 0)
|
---|
844 | {
|
---|
845 | numbers.push_back(d % 1000);
|
---|
846 | d /= 1000;
|
---|
847 | }
|
---|
848 |
|
---|
849 | // first element without leading zeros
|
---|
850 | if (numbers.size() > 0)
|
---|
851 | {
|
---|
852 | sprintf_s(hstr, "%d", numbers.back());
|
---|
853 | str.append(hstr);
|
---|
854 | }
|
---|
855 |
|
---|
856 | for (int i = (int)numbers.size() - 2; i >= 0; i--)
|
---|
857 | {
|
---|
858 | sprintf_s(hstr, ",%03d", numbers[i]);
|
---|
859 | str.append(hstr);
|
---|
860 | }
|
---|
861 | }
|
---|
862 |
|
---|
863 |
|
---|
864 | void DisplayStats()
|
---|
865 | {
|
---|
866 | char *msg[] = {"Frustum Culling", "Stop and Wait",
|
---|
867 | "CHC", "CHC ++"};
|
---|
868 |
|
---|
869 | char msg2[400];
|
---|
870 | char msg3[200];
|
---|
871 | char msg4[200];
|
---|
872 | char msg5[200];
|
---|
873 |
|
---|
874 |
|
---|
875 | static double renderTime = traverser->GetStats().mRenderTime;
|
---|
876 | const float expFactor = 0.3f;
|
---|
877 | renderTime = traverser->GetStats().mRenderTime * expFactor + (1.0f - expFactor) * renderTime;
|
---|
878 |
|
---|
879 | accumulatedTime += renderTime;
|
---|
880 |
|
---|
881 | if (accumulatedTime > 500) // update every fraction of a second
|
---|
882 | {
|
---|
883 | accumulatedTime = 0;
|
---|
884 | if (renderTime) fps = 1e3f / (float)renderTime;
|
---|
885 |
|
---|
886 | renderedObjects = traverser->GetStats().mNumRenderedGeometry;
|
---|
887 | renderedNodes = traverser->GetStats().mNumRenderedNodes;
|
---|
888 | renderedTriangles = traverser->GetStats().mNumRenderedTriangles;
|
---|
889 |
|
---|
890 | traversedNodes = traverser->GetStats().mNumTraversedNodes;
|
---|
891 | frustumCulledNodes = traverser->GetStats().mNumFrustumCulledNodes;
|
---|
892 | queryCulledNodes = traverser->GetStats().mNumQueryCulledNodes;
|
---|
893 | issuedQueries = traverser->GetStats().mNumIssuedQueries;
|
---|
894 | stateChanges = traverser->GetStats().mNumStateChanges;
|
---|
895 | }
|
---|
896 |
|
---|
897 | sprintf_s(msg2, "assumed visible frames: %4d, max batch size: %4d, using multiqueries: %d, using render queue: %d", assumedVisibleFrames, maxBatchSize, useMultiQueries, useRenderQueue);
|
---|
898 |
|
---|
899 | string str;
|
---|
900 | string str2;
|
---|
901 |
|
---|
902 | CalcDecimalPoint(str, renderedTriangles);
|
---|
903 | CalcDecimalPoint(str2, bvh->GetBvhStats().mTriangles);
|
---|
904 |
|
---|
905 | sprintf_s(msg3, "rendered nodes: %6d (of %6d), rendered triangles: %s (of %s)",
|
---|
906 | renderedNodes, bvh->GetNumVirtualNodes(), str.c_str(), str2.c_str());
|
---|
907 |
|
---|
908 | sprintf_s(msg4, "traversed: %5d, frustum culled: %5d, query culled: %5d, issued queries: %5d, state changes: %5d",
|
---|
909 | traversedNodes, frustumCulledNodes, queryCulledNodes, issuedQueries, stateChanges);
|
---|
910 |
|
---|
911 | sprintf_s(msg5, "fps: %6.1f", fps);
|
---|
912 | //cout << "previously visible node queries: " << traverser->GetStats().mNumPreviouslyVisibleNodeQueries << endl;
|
---|
913 |
|
---|
914 | begin2D();
|
---|
915 |
|
---|
916 | if(showHelp)
|
---|
917 | {
|
---|
918 | drawHelpMessage();
|
---|
919 | }
|
---|
920 | else
|
---|
921 | {
|
---|
922 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
923 | Output(850, 30, msg[renderMode]);
|
---|
924 |
|
---|
925 | if(showStatistics)
|
---|
926 | {
|
---|
927 | Output(20, 30, msg2);
|
---|
928 | Output(20, 60, msg3);
|
---|
929 | Output(20, 90, msg4);
|
---|
930 | Output(20, 120, msg5);
|
---|
931 | }
|
---|
932 | }
|
---|
933 |
|
---|
934 | end2D();
|
---|
935 | }
|
---|