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