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