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