source: GTP/trunk/App/Demos/Vis/CHC_revisited/chcdemo.cpp @ 2778

Revision 2778, 20.4 KB checked in by mattausch, 16 years ago (diff)

updated the camera model

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