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