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