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