Ignore:
Timestamp:
06/22/08 05:24:22 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r2794 r2795  
    99#include "Vector3.h" 
    1010#include "Matrix4x4.h" 
    11 #include "BinaryLoader.h" 
     11#include "ResourceManager.h" 
    1212#include "Bvh.h" 
    1313#include "Camera.h" 
     
    2020#include "Visualization.h" 
    2121#include "RenderState.h" 
    22  
     22#include "Timer/PerfTimer.h" 
    2323 
    2424 
     
    3535Bvh *bvh = NULL; 
    3636/// handles scene loading 
    37 BinaryLoader *loader = NULL; 
     37ResourceManager *loader = NULL; 
    3838/// the scene camera 
    3939Camera *camera = NULL; 
     40/// the scene camera 
     41Camera *visCamera = NULL; 
    4042/// the visualization 
    4143Visualization *visualization = NULL; 
     
    4345RenderState state; 
    4446/// the rendering algorithm 
    45 int renderMode = RenderTraverser::CULL_FRUSTUM; 
     47int renderMode = RenderTraverser::CHCPLUSPLUS; 
    4648// eye near plane distance 
    4749float nearDist = 0.1f;  
     
    4951int threshold; 
    5052 
     53float fov = 50.0f; 
     54 
    5155int assumedVisibleFrames = 10; 
    5256int maxBatchSize = 50; 
    5357 
    54 const float keyForwardMotion = 0.5f; 
    55 const float keyRotation = 0.05f; 
     58int trianglesPerVirtualLeaf = 1000; 
     59 
     60/// these values get scaled with the frame rate 
     61const float keyForwardMotion = 50.0f; 
     62const float keyRotation = 2.0f; 
     63/// elapsed time in seconds 
     64double elapsedTime = 1.0f; 
    5665 
    5766int winWidth = 1024; 
     
    8493bool useOptimization = false; 
    8594bool useTightBounds = true; 
    86 bool useRenderQueue = false; 
     95bool useRenderQueue = true; 
    8796bool useMultiQueries = true; 
    8897 
     98/// the accumulated z axis rotation 
    8999float rotation = 0; 
    90100 
    91101Matrix4x4 visView = IdentityMatrix(); 
     102 
     103SceneEntityContainer skyGeometry; 
    92104 
    93105bool leftKeyPressed = false; 
     
    129141void KeyHorizontalMotion(float shift); 
    130142void KeyVerticalMotion(float shift); 
    131 void KeyRotate(float angle); 
     143 
     144 
     145inline float KeyRotationAngle() { return keyRotation * elapsedTime; } 
     146inline float KeyShift() { return keyForwardMotion * elapsedTime; } 
    132147 
    133148 
     
    137152        int returnCode = 0; 
    138153 
    139 #ifdef _CRT_SET 
    140  
    141         //Now just call this function at the start of your program and if you're 
    142         //compiling in debug mode (F5), any leaks will be displayed in the Output 
    143         //window when the program shuts down. If you're not in debug mode this will 
    144         //be ignored. Use it as you will! 
    145         //note: from GDNet Direct [3.8.04 - 3.14.04] void detectMemoryLeaks() { 
    146  
    147         _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF|_CRTDBG_ALLOC_MEM_DF); 
    148         _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE); 
    149         _CrtSetReportFile(_CRT_ASSERT,_CRTDBG_FILE_STDERR);  
    150 #endif 
    151  
    152  
    153         camera = new Camera(winWidth, winHeight, 60); 
     154        camera = new Camera(winWidth, winHeight, fov); 
    154155        camera->SetNear(nearDist); 
     156 
     157        visCamera = new Camera(winWidth, winHeight, fov); 
     158        visCamera->SetNear(nearDist); 
    155159 
    156160        glutInitWindowSize(winWidth, winHeight); 
     
    158162        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); 
    159163 
    160         glutCreateWindow("Coherent Hierarchical Culling"); 
     164        glutCreateWindow("FriendlyCulling"); 
    161165 
    162166        glutDisplayFunc(Display); 
     
    177181 
    178182 
    179         loader = new BinaryLoader(); 
     183        loader = new ResourceManager(); 
    180184 
    181185        //const string filename("data/city/model/city.dem"); 
     
    190194                exit(0); 
    191195        } 
     196 
     197        SceneEntityContainer dummy; 
     198 
     199        const string envname = string(model_path + "env.dem"); 
     200 
     201        if (loader->Load(envname, skyGeometry)) 
     202                cout << "sky box " << filename << " loaded" << endl; 
     203        else 
     204        { 
     205                cerr << "loading sky box " << filename << " failed" << endl; 
     206                CleanUp(); 
     207                exit(0); 
     208        } 
     209 
    192210 
    193211        const string bvh_filename = string(model_path + "city.bvh"); 
     
    206224        ResetTraverser(); 
    207225 
    208         camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f)); 
     226        //camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f)); 
     227        camera->Pitch(-M_PI * 0.5); 
    209228        camera->SetPosition(Vector3(483.398f, 242.364f, 186.078f)); 
    210229 
     
    265284                "", 
    266285                "'F1'           - shows/dismisses this message", 
    267                 "'F2'           - shows/hides statistics", 
     286                "'F2'           - shows/hides bird eye view", 
    268287                "'F3'           - shows/hides bounds (boxes or tight bounds)", 
    269                 "'F4'           - shows/hides bird eye view", 
    270                 "'f5',          - load helpScreen", 
     288                "'F4'           - shows/hides statistics", 
     289                "'F5',          - shows/hides parameters", 
    271290                "'SPACE'        - cycles through occlusion culling algorithms", 
    272291                "", 
     
    281300                "'-'            - decreases max batch size", 
    282301                "'+'            - increases max batch size", 
     302                "'4'            - decrease triangles per virtual leaf (sets depth of bvh)", 
     303                "'5'            - increase triangles per virtual leaf (sets depth of bvh)", 
    283304                "'6'            - decrease assumed visible frames", 
    284305                "'7'            - increase assumed visible frames", 
    285                 "'8'            - upward motion", 
    286                 "'9'            - downward motion", 
     306                "'8'            - downward motion", 
     307                "'9'            - upward motion", 
    287308                "", 
    288309                "'R'            - use render queue", 
     
    405426} 
    406427 
    407 void SetupEyeView(void) 
     428void SetupEyeView() 
    408429{ 
    409430        glMatrixMode(GL_PROJECTION); 
    410431        glLoadIdentity(); 
    411432 
    412         gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal())); 
     433        gluPerspective(fov, 1.0f / winAspectRatio, nearDist, 10.0f * Magnitude(bvh->GetBox().Diagonal())); 
    413434 
    414435        glMatrixMode(GL_MODELVIEW); 
     436        glLoadIdentity(); 
    415437        camera->SetupCameraView(); 
    416438 
     
    423445 
    424446 
    425 void KeyRotate(float angle) 
    426 { 
    427         Vector3 viewDir = camera->GetDirection(); 
    428         // rotate view vector 
    429         Matrix4x4 rot = RotationZMatrix(angle); 
    430         viewDir = rot * viewDir; 
    431         camera->SetDirection(viewDir); 
    432 } 
    433  
    434  
    435447void KeyHorizontalMotion(float shift) 
    436448{ 
     
    459471void Display()  
    460472{ 
     473        PerfTimer frameTimer; 
     474 
     475        frameTimer.Start(); 
     476 
    461477        if (leftKeyPressed) 
    462                 KeyRotate(keyRotation); 
     478                camera->Pitch(KeyRotationAngle()); 
    463479        if (rightKeyPressed) 
    464                 KeyRotate(-keyRotation); 
     480                camera->Pitch(-KeyRotationAngle()); 
    465481        if (upKeyPressed) 
    466                 KeyHorizontalMotion(keyForwardMotion); 
     482                KeyHorizontalMotion(KeyShift()); 
    467483        if (downKeyPressed) 
    468                 KeyHorizontalMotion(-keyForwardMotion); 
     484                KeyHorizontalMotion(-KeyShift()); 
    469485        if (eightKeyPressed) 
    470                 KeyVerticalMotion(keyForwardMotion); 
     486                KeyVerticalMotion(-KeyShift()); 
    471487        if (nineKeyPressed) 
    472                 KeyVerticalMotion(-keyForwardMotion); 
     488                KeyVerticalMotion(KeyShift()); 
    473489 
    474490        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
     
    479495        // actually render the scene geometry using one of the specified algorithms 
    480496        traverser->RenderScene(); 
    481          
     497 
     498         
     499        glEnableClientState(GL_VERTEX_ARRAY); 
     500        glEnableClientState(GL_NORMAL_ARRAY); 
     501 
     502        SceneEntityContainer::const_iterator sit, sit_end = skyGeometry.end(); 
     503 
     504        for (sit = skyGeometry.begin(); sit != sit_end; ++ sit) 
     505                (*sit)->Render(&state); 
     506 
     507        glDisableClientState(GL_VERTEX_ARRAY); 
     508        glDisableClientState(GL_NORMAL_ARRAY); 
     509 
     510        state.Reset(); 
     511 
    482512        if (visMode) DisplayVisualization(); 
    483513         
     
    485515 
    486516        glutSwapBuffers(); 
     517 
     518        elapsedTime = frameTimer.Elapsed(); 
    487519} 
    488520 
     
    531563                { 
    532564                        eightKeyPressed = true; 
    533                         KeyVerticalMotion(keyForwardMotion); 
     565                        KeyVerticalMotion(-KeyShift()); 
    534566                        break; 
    535567                } 
     
    537569                { 
    538570                        nineKeyPressed = true; 
    539                         KeyVerticalMotion(-keyForwardMotion); 
     571                        KeyVerticalMotion(+KeyShift()); 
    540572                        break; 
    541573                }        
     
    549581                { 
    550582                        leftKeyPressed = true; 
    551                         KeyRotate(keyRotation); 
     583                        camera->Pitch(KeyRotationAngle()); 
    552584                } 
    553585                break; 
     
    556588        { 
    557589                        rightKeyPressed = true; 
    558                         KeyRotate(-keyRotation); 
     590                        camera->Pitch(-KeyRotationAngle()); 
    559591                } 
    560592                break; 
     
    563595                { 
    564596                        upKeyPressed = true; 
    565                         KeyHorizontalMotion(keyForwardMotion); 
     597                        KeyHorizontalMotion(KeyShift()); 
    566598                } 
    567599                break; 
     
    570602                { 
    571603                        downKeyPressed = true; 
    572                         KeyHorizontalMotion(-keyForwardMotion); 
     604                        KeyHorizontalMotion(-KeyShift()); 
    573605                } 
    574606                break; 
     
    587619                } 
    588620                break; 
    589  
    590  
    591  
     621        case '4': 
     622                if (trianglesPerVirtualLeaf >= 100) 
     623                        trianglesPerVirtualLeaf -= 100; 
     624 
     625                bvh->SetVirtualLeaves(trianglesPerVirtualLeaf); 
     626                break; 
     627        case '5': 
     628                trianglesPerVirtualLeaf += 100; 
     629                bvh->SetVirtualLeaves(trianglesPerVirtualLeaf); 
     630                break; 
    592631        default: 
    593632                return; 
     
    658697void Special(int c, int x, int y)  
    659698{ 
    660         // used to avoid vertical motion with the keys 
    661         Vector3 hvec = Vector3(camera->GetDirection()[0], camera->GetDirection()[1], 0.0f); 
    662          
    663699        switch(c)  
    664700        { 
     
    667703                break; 
    668704        case GLUT_KEY_F2: 
    669                 showStatistics = !showStatistics; 
     705                visMode = !visMode; 
    670706                break; 
    671707        case GLUT_KEY_F3: 
     
    674710                break; 
    675711        case GLUT_KEY_F4: 
    676                 visMode = !visMode; 
     712                showStatistics = !showStatistics; 
    677713                break; 
    678714        case GLUT_KEY_F5: 
     
    682718                { 
    683719                        leftKeyPressed = true; 
    684                         KeyRotate(keyRotation); 
     720                        camera->Pitch(KeyRotationAngle()); 
    685721                } 
    686722                break; 
     
    688724                { 
    689725                        rightKeyPressed = true; 
    690                         KeyRotate(-keyRotation); 
     726                        camera->Pitch(-KeyRotationAngle()); 
    691727                } 
    692728                break; 
     
    694730                { 
    695731                        upKeyPressed = true; 
    696                         KeyHorizontalMotion(keyForwardMotion); 
     732                        KeyHorizontalMotion(KeyShift()); 
    697733                } 
    698734                break; 
     
    700736                { 
    701737                        downKeyPressed = true; 
    702                         KeyHorizontalMotion(-keyForwardMotion); 
     738                        KeyHorizontalMotion(-KeyShift()); 
    703739                } 
    704740                break; 
     
    728764        glLoadIdentity(); 
    729765 
    730         gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal())); 
     766        gluPerspective(fov, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal())); 
    731767 
    732768        glMatrixMode(GL_MODELVIEW); 
     
    768804void LeftMotion(int x, int y)  
    769805{ 
    770         static float eyeXAngle = 0.0f; 
    771  
    772806        Vector3 viewDir = camera->GetDirection(); 
    773807        Vector3 pos = camera->GetPosition(); 
     
    776810        Vector3 horView(viewDir[0], viewDir[1], 0); 
    777811         
    778         eyeXAngle = 0.2f *  M_PI * (xEyeBegin - x) / 180.0; 
    779  
    780         rotation += eyeXAngle; 
    781  
    782         // rotate view vector 
    783         Matrix4x4 rot = RotationZMatrix(eyeXAngle); 
    784         viewDir = rot * viewDir; 
     812        float eyeXAngle = 0.2f *  M_PI * (xEyeBegin - x) / 180.0; 
     813 
     814        camera->Pitch(eyeXAngle); 
    785815 
    786816        pos += horView * (yMotionBegin - y) * 0.2f; 
    787  
    788         camera->SetDirection(viewDir); 
     817         
    789818        camera->SetPosition(pos); 
    790819         
     
    801830void RightMotion(int x, int y)  
    802831{ 
    803         static float eyeYAngle = 0.0f; 
    804  
    805         Vector3 viewDir = camera->GetDirection(); 
    806         Vector3 right = camera->GetRightVector(); 
    807  
    808         eyeYAngle = -0.2f *  M_PI * (yEyeBegin - y) / 180.0; 
    809  
    810         // rotate view vector 
    811         Matrix4x4 rot = RotationAxisMatrix(right, eyeYAngle); 
    812         viewDir = rot * viewDir; 
    813  
    814         camera->SetDirection(viewDir); 
    815                  
     832        float eyeYAngle = -0.2f *  M_PI * (yEyeBegin - y) / 180.0; 
     833 
     834        camera->Yaw(eyeYAngle); 
     835 
    816836        yEyeBegin = y; 
    817          
    818837        glutPostRedisplay(); 
    819838} 
     
    907926// displays the visualisation of culling algorithm 
    908927void DisplayVisualization() 
    909 { 
     928{cout<<"here34"<<endl; 
    910929        SetupVisView(); 
    911930 
     
    923942        glViewport(winWidth - winWidth / 4, winHeight - winHeight / 3, winWidth, winHeight); 
    924943 
    925  
    926944        glMatrixMode(GL_PROJECTION); 
    927945        glLoadIdentity(); 
    928946         
    929         //gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal())); 
     947        gluPerspective(fov, 1.0f / winAspectRatio, nearDist, 20.0f * Magnitude(bvh->GetBox().Diagonal())); 
     948         
    930949        AxisAlignedBox3 box = bvh->GetBox(); 
    931950        const float offs = 2000; 
    932         glOrtho(box.Min().y - offs, box.Max().y + offs, box.Min().y - offs, box.Max().y + offs, 0.1, 20.0f * Magnitude(bvh->GetBox().Diagonal()));  
    933          
    934          
    935         glPushMatrix(); 
    936  
     951         
     952        //glOrtho(box.Min().y - offs, box.Max().y + offs, box.Min().y - offs, box.Max().y + offs, 0.1, 20.0f * Magnitude(bvh->GetBox().Diagonal()));  
     953        //glOrtho(-offs, offs, offs, -offs, 0.1, 20.0f * Magnitude(bvh->GetBox().Diagonal()));  
     954        //glPushMatrix(); 
     955 
     956        Vector3 vizpos = Vector3(box.Center().x, box.Center().y, box.Max().z); 
     957        visCamera->SetPosition(vizpos); 
     958        visCamera->Yaw(-0.5 * M_PI); 
     959        cout<<"pos: " << visCamera->GetPosition()<<endl; 
    937960        glMatrixMode(GL_MODELVIEW); 
    938961        glLoadIdentity(); 
    939962 
    940         glMultMatrixf((float *)visView.x); 
     963        visCamera->SetupCameraView(); 
     964        //glMultMatrixf((float *)visView.x); 
    941965 
    942966        //SetupLighting(); 
     
    947971        glLightfv(GL_LIGHT1, GL_POSITION, position1); 
    948972 
    949  
    950  
    951973        glClear(GL_DEPTH_BUFFER_BIT); 
    952974 
     
    956978        visualization->Render(); 
    957979         
    958         glPopMatrix(); 
     980        //glPopMatrix(); 
    959981         
    960982        glViewport(0, 0, winWidth, winHeight); 
     
    9911013void CleanUp() 
    9921014{ 
    993         CLEAR_CONTAINER(sceneEntities); 
    9941015        DEL_PTR(traverser); 
    995  
    9961016        DEL_PTR(bvh); 
    9971017        DEL_PTR(visualization); 
    9981018        DEL_PTR(camera); 
    999  
    10001019        DEL_PTR(loader); 
    10011020} 
     
    10401059        char msg6[200]; 
    10411060        char msg7[200]; 
     1061        char msg8[200]; 
    10421062 
    10431063 
     
    10841104        sprintf_s(msg6, "issued queries: %5d, state changes: %5d", issuedQueries, stateChanges); 
    10851105 
     1106        sprintf_s(msg8, "triangles per virtual leaf: %5d", trianglesPerVirtualLeaf); 
     1107 
    10861108        sprintf_s(msg7, "fps: %6.1f", fps); 
    10871109 
     
    11111133                        Output(20, 150, msg2); 
    11121134                        Output(20, 180, msg3); 
     1135                        Output(20, 210, msg8); 
    11131136                } 
    11141137        } 
Note: See TracChangeset for help on using the changeset viewer.