Ignore:
Timestamp:
06/17/08 15:20:32 (16 years ago)
Author:
mattausch
Message:
 
File:
1 edited

Legend:

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

    r2765 r2767  
    1717#include "StopAndWaitTraverser.h" 
    1818#include "CHCTraverser.h" 
     19#include "CHCPlusPlusTraverser.h" 
     20#include "Visualization.h" 
     21 
    1922 
    2023 
     
    2730SceneEntityContainer sceneEntities; 
    2831// traverses and renders the hierarchy 
    29 RenderTraverser *traverser; 
     32RenderTraverser *traverser = NULL; 
    3033/// the hierarchy 
    31 Bvh *bvh; 
     34Bvh *bvh = NULL; 
    3235/// the scene camera 
    33 Camera *camera; 
    34 /// the scene bounding box 
    35 AxisAlignedBox3 sceneBox; 
     36Camera *camera = NULL; 
     37/// the visualization 
     38Visualization *visualization = NULL; 
    3639/// the current render state 
    3740RenderState state; 
     
    4346float nearDist = 0.1f;  
    4447 
     48const float keyForwardMotion = 1.0f; 
     49const float keyRotation = 0.2f; 
     50 
     51bool useRenderQueue = false; 
     52 
    4553int winWidth = 1024; 
    4654int winHeight = 768; 
     
    5563 
    5664 
    57 int currentFrame = -1; 
    58  
    5965// visualisation view matrix 
    6066Matrix4x4 visView;  
     
    6369int xEyeBegin, yEyeBegin, yMotionBegin, verticalMotionBegin, horizontalMotionBegin = 0; 
    6470 
    65 const int renderTimeSize = 100; 
    66 long renderTimes[renderTimeSize]; 
    67 int renderTimesIdx = 0; 
    68 int renderTimesValid = 0; 
    69  
    7071bool useOptimization = true; 
    7172 
    72  
    73 Vector3 amb[2]; 
    74 Vector3 dif[2]; 
    75 Vector3 spec[2]; 
    7673 
    7774void InitExtensions(); 
     
    10097void setupVisView(void); 
    10198long calcRenderTime(void); 
    102 void resetTimer(void); 
    10399void CalcDecimalPoint(std::string &str, int d); 
    104100void ResetTraverser(); 
     
    148144        //bvh = bvhLoader.Load("house_test.bvh", sceneEntities); 
    149145 
    150         sceneBox = bvh->GetBox(); 
    151  
    152146        ResetTraverser(); 
    153147 
    154148        //camera->LookAtBox(sceneBox); 
    155         camera->LookInBox(sceneBox); 
    156  
    157         /// initialise rendertime array 
    158         for(int i=0; i<renderTimeSize; i++) 
    159                 renderTimes[i] = 0; 
     149        //camera->LookInBox(bvh->GetBox()); 
     150 
     151        camera->SetDirection(Vector3(0.961829f, 0.273652f, 0.0f)); 
     152        camera->SetPosition(Vector3(483.398f, 242.364f, 186.078f)); 
    160153 
    161154        glutMainLoop(); 
     
    170163void InitGLstate(void)  
    171164{ 
    172         glClearColor(0.5f, 0.5f, 0.8f, 0.0); 
     165        glClearColor(0.5f, 0.5f, 0.8f, 0.0f); 
    173166         
    174167        glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
     
    185178        glMaterialf(GL_FRONT, GL_SHININESS, 64); 
    186179        glEnable(GL_NORMALIZE); 
    187                          
     180                 
     181        //glEnable(GL_ALPHA_TEST); 
     182        //glAlphaFunc(GL_GEQUAL, 0.01); 
     183 
     184        glDisable(GL_ALPHA_TEST); 
     185 
    188186        glFrontFace(GL_CCW); 
    189187        glCullFace(GL_BACK); 
     
    288286                traverser = new CHCTraverser(); 
    289287                break; 
     288        case RenderTraverser::CHCPLUSPLUS: 
     289                traverser = new CHCPlusPlusTraverser(); 
     290                break; 
     291         
    290292        default: 
    291293                traverser = new FrustumCullingTraverser(); 
    292294        } 
    293295 
     296        traverser->SetCamera(camera); 
    294297        traverser->SetHierarchy(bvh); 
    295298        traverser->SetRenderState(&state); 
     
    302305        glEnable(GL_LIGHT0); 
    303306        glEnable(GL_LIGHT1); 
     307 
    304308        //glDisable(GL_LIGHT1); 
    305309        //glDisable(GL_LIGHTING); 
     
    349353        glLoadIdentity(); 
    350354 
    351         gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(sceneBox.Diagonal())); 
     355        gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal())); 
    352356 
    353357        glMatrixMode(GL_MODELVIEW); 
     
    357361        glLightfv(GL_LIGHT0, GL_POSITION, position); 
    358362 
    359         GLfloat position1[] = {sceneBox.Center().x, sceneBox.Max().y, sceneBox.Center().z, 1.0f}; 
    360         //GLfloat position1[] = {-2.0f, 1.0f, 0.0f, 0.0f}; 
     363        GLfloat position1[] = {bvh->GetBox().Center().x, bvh->GetBox().Max().y, bvh->GetBox().Center().z, 1.0f}; 
    361364        glLightfv(GL_LIGHT1, GL_POSITION, position1); 
    362365} 
     
    365368void display(void)  
    366369{ 
    367         ++ currentFrame; 
    368  
    369370        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    370371 
     
    372373        SetupEyeView(); 
    373374 
    374  
    375         traverser->InitFrame(camera, currentFrame); 
    376  
    377  
    378         InitTiming(); 
    379         long t1, t2; 
    380  
    381         t1 = GetTime(); 
    382  
    383         glEnableClientState(GL_VERTEX_ARRAY); 
    384         glEnableClientState(GL_NORMAL_ARRAY); 
    385  
    386         traverser->Render(); 
    387  
    388         glDisableClientState(GL_VERTEX_ARRAY); 
    389         glDisableClientState(GL_NORMAL_ARRAY); 
    390  
    391         glDisable(GL_TEXTURE_2D); 
    392         glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
    393          
    394  
    395         t2 = GetTime(); 
    396         long loop_time = TimeDiff(t1, t2); 
    397          
    398         // cycle through rendertime array 
    399         renderTimes[renderTimesIdx] = loop_time;// traverser.GetRenderTime(); 
    400         renderTimesIdx = (renderTimesIdx + 1) % renderTimeSize; 
    401  
    402         if(renderTimesIdx  > renderTimesValid)  
    403                 renderTimesValid = renderTimesIdx; 
    404  
     375        //cout << camera->GetDirection() << " " << camera->GetPosition() << endl; 
     376        // actually render the scene geometry using one of the specified algorithms 
     377        traverser->RenderScene(); 
     378         
    405379        //if(visMode) displayVisualization(); 
    406380         
    407381        DisplayStats(); 
     382 
    408383        glutSwapBuffers(); 
    409384} 
     
    413388void keyboard(const unsigned char c, const int x, const int y)  
    414389{ 
     390        // used to avoid vertical motion with the keys 
     391        Vector3 hvec = Vector3(camera->GetDirection()[0], camera->GetDirection()[1], 0.0f); 
     392        Vector3 uvec = Vector3(0, 0, 1); 
     393         
    415394        switch(c)  
    416395        { 
     
    420399        case 32: //space 
    421400                renderMode = (renderMode + 1) % RenderTraverser::NUM_RENDERMODES; 
    422                 resetTimer(); 
    423401                ResetTraverser(); 
    424402                break; 
     
    432410                //HierarchyNode::SetRenderBoundingVolume(showBoundingVolumes); 
    433411                break; 
    434         case 's': 
    435         case 'S': 
     412        case 'O': 
     413        case 'o': 
    436414                showStatistics = !showStatistics; 
    437415                break; 
     
    449427                visMode = !visMode; 
    450428                break; 
    451          
    452429        case '2': 
    453430                visZoomFactor += 0.1;    
     
    460437                setupVisView(); 
    461438                break; 
     439        case '8': 
     440                { 
     441                        Vector3 pos = camera->GetPosition(); 
     442                        pos += uvec * keyForwardMotion; 
     443                        camera->SetPosition(pos); 
     444                        break; 
     445                } 
     446        case '9': 
     447                { 
     448                        Vector3 pos = camera->GetPosition(); 
     449                        pos -= uvec * keyForwardMotion; 
     450                        camera->SetPosition(pos); 
     451                        break; 
     452                }        
    462453        case 'g': 
    463454        case 'G': 
    464455                useOptimization = !useOptimization; 
    465456                traverser->SetUseOptimization(useOptimization); 
     457                break; 
     458        case 'a': 
     459        case 'A': 
     460                { 
     461                        Vector3 viewDir = camera->GetDirection(); 
     462                        // rotate view vector 
     463                        Matrix4x4 rot = RotationZMatrix(keyRotation); 
     464                        viewDir = rot * viewDir; 
     465                        camera->SetDirection(viewDir); 
     466                } 
     467                break; 
     468        case 'd': 
     469        case 'D': 
     470        { 
     471                        Vector3 viewDir = camera->GetDirection(); 
     472                        // rotate view vector 
     473                        Matrix4x4 rot = RotationZMatrix(-keyRotation); 
     474                        viewDir = rot * viewDir; 
     475                        camera->SetDirection(viewDir); 
     476                } 
     477                break; 
     478        case 'w': 
     479        case 'W': 
     480                { 
     481                        Vector3 pos = camera->GetPosition(); 
     482                        pos += hvec * keyForwardMotion; 
     483                        camera->SetPosition(pos); 
     484                } 
     485                break; 
     486        case 'x': 
     487        case 'X': 
     488                { 
     489                        Vector3 pos = camera->GetPosition(); 
     490                        pos -= hvec * keyForwardMotion; 
     491                        camera->SetPosition(pos); 
     492                } 
     493                break; 
     494        case 'r': 
     495        case 'R': 
     496                { 
     497                        useRenderQueue = !useRenderQueue; 
     498                        traverser->SetUseRenderQueue(useRenderQueue); 
     499                } 
    466500        default: 
    467501                return; 
     
    475509{ 
    476510        // used to avoid vertical motion with the keys 
    477         //Vector3 hvec = Vector3(viewDir[0], 0, viewDir[2]); 
     511        Vector3 hvec = Vector3(camera->GetDirection()[0], camera->GetDirection()[1], 0.0f); 
    478512         
    479513        switch(c)  
     
    482516                showHelp = !showHelp; 
    483517                break; 
    484         case GLUT_KEY_F2: 
    485                 //numNextObjects -= 100; 
    486                 //if(numNextObjects < 100) numNextObjects = 100; 
    487                 break; 
    488         case GLUT_KEY_F3: 
    489        // numNextObjects += 100; 
    490                 break; 
    491         case GLUT_KEY_F4: 
    492                 //zLength -= 1; 
    493                 //if(zLength < 0) zLength = 0; 
    494                 break; 
    495         case GLUT_KEY_F5: 
    496                 //zLength += 1; 
    497                 break;           
    498         case GLUT_KEY_F6: 
    499                 //objectSize -= 0.1; 
    500                 //if(objectSize < 0.1) objectSize = 0.1; 
    501                 break; 
    502         case GLUT_KEY_F7: 
    503                 //objectSize += 0.1; 
    504                 break; 
    505         case GLUT_KEY_F8: 
    506                 //nextObjectType = (nextObjectType + 1) % Geometry::NUM_OBJECTS; 
    507                 break; 
    508518        case GLUT_KEY_LEFT: 
    509         //      rotateVectorY(viewDir, 0.2); 
     519                { 
     520                        Vector3 viewDir = camera->GetDirection(); 
     521                        // rotate view vector 
     522                        Matrix4x4 rot = RotationZMatrix(keyRotation); 
     523                        viewDir = rot * viewDir; 
     524                        camera->SetDirection(viewDir); 
     525                } 
    510526                break; 
    511527        case GLUT_KEY_RIGHT: 
    512                 //rotateVectorY(viewDir, -0.2); 
     528                { 
     529                        Vector3 viewDir = camera->GetDirection(); 
     530                        // rotate view vector 
     531                        Matrix4x4 rot = RotationZMatrix(-keyRotation); 
     532                        viewDir = rot * viewDir; 
     533                        camera->SetDirection(viewDir); 
     534                } 
    513535                break; 
    514536        case GLUT_KEY_UP: 
    515         //      linCombVector3(eyePos, eyePos, hvec, 0.6); 
     537                { 
     538                        Vector3 pos = camera->GetPosition(); 
     539                        pos += hvec * 0.6f; 
     540                        camera->SetPosition(pos); 
     541                } 
    516542                break; 
    517543        case GLUT_KEY_DOWN: 
    518                 //linCombVector3(eyePos, eyePos, hvec, -0.6); 
     544                { 
     545                        Vector3 pos = camera->GetPosition(); 
     546                        pos -= hvec * 0.6f; 
     547                        camera->SetPosition(pos); 
     548                } 
    519549                break; 
    520550        default: 
     
    525555        glutPostRedisplay(); 
    526556} 
     557 
    527558#pragma warning( default : 4100 ) 
    528559 
     
    542573        glLoadIdentity(); 
    543574 
    544         gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(sceneBox.Diagonal())); 
     575        gluPerspective(60.0f, 1.0f / winAspectRatio, nearDist, 2.0f * Magnitude(bvh->GetBox().Diagonal())); 
    545576        glMatrixMode(GL_MODELVIEW); 
    546577 
     
    587618 
    588619        // don't move in the vertical direction 
    589         //Vector3 horView(viewDir[0], 0, viewDir[2]); 
    590620        Vector3 horView(viewDir[0], viewDir[1], 0); 
    591621         
     
    593623 
    594624        // rotate view vector 
    595         //Matrix4x4 rot = RotationYMatrix(eyeXAngle); 
    596625        Matrix4x4 rot = RotationZMatrix(eyeXAngle); 
    597626        viewDir = rot * viewDir; 
     
    609638 
    610639 
    611 /**     rotation for left/right mouse drag 
    612         motion for up/down mouse drag 
     640/**     rotation for left / right mouse drag 
     641        motion for up / down mouse drag 
    613642*/ 
    614643void rightMotion(int x, int y)  
     
    707736void output(const int x, const int y, const char *string)  
    708737{ 
    709         if(string != 0)  
    710         { 
    711                 int len, i; 
    712                 glRasterPos2f(x,y); 
    713                 len = (int) strlen(string); 
     738        if (string != 0)  
     739        { 
     740                size_t len, i; 
     741                glRasterPos2f(x, y); 
     742                len = strlen(string); 
    714743                 
    715                 for (i = 0; i < len; i++)  
     744                for (i = 0; i < len; ++ i)  
    716745                { 
    717746                        glutBitmapCharacter(GLUT_BITMAP_8_BY_13, string[i]); 
     
    719748        } 
    720749} 
     750 
    721751 
    722752// displays the visualisation of the kd tree node culling 
     
    738768        glClear(GL_DEPTH_BUFFER_BIT); 
    739769 
     770        //////////// 
    740771        // --- visualization of the occlusion culling 
    741         /*HierarchyNode::SetRenderBoundingVolume(true); 
    742         traverser.RenderVisualization(); 
    743         HierarchyNode::SetRenderBoundingVolume(showBoundingVolumes); 
    744 */ 
     772        visualization->Render(); 
     773         
    745774        glPopMatrix(); 
     775         
    746776        glViewport(0, 0, winWidth, winHeight); 
    747777} 
     778 
    748779 
    749780/** Sets up view matrix in order to get a good position  
     
    752783void setupVisView() 
    753784{ 
    754         const Vector3 up(0.0, 1.0, 0.0); 
     785        const Vector3 up(0.0, 0.0, 1.0); 
    755786         
    756787        Vector3 visPos(24, 23, -6); 
     
    760791        visPos[2] *= visZoomFactor; 
    761792 
    762         Vector3 visDir = Vector3(-1.3, -1, -1); 
    763          
    764         //normalize(visDir); 
    765         //look(visView, visPos, visDir, up); 
    766 } 
    767  
    768 // we take a couple of measurements and compute the average 
    769 long calcRenderTime() 
    770 { 
    771         long result = 0; 
    772  
    773         for(int i=0; i<renderTimesValid; i++) 
    774                 result += renderTimes[i]; 
    775      
    776         if(renderTimesValid) 
    777                 result /= renderTimesValid; 
    778  
    779         return result; 
    780 } 
    781  
    782  
    783 // reset the timer array for a new traversal method 
    784 void resetTimer() 
    785 { 
    786         renderTimesValid = 0; 
    787         renderTimesIdx = 0; 
     793        Vector3 visDir = Vector3(-1.3f, -1, -1); 
    788794} 
    789795 
     
    796802 
    797803        DEL_PTR(bvh); 
     804        DEL_PTR(visualization); 
    798805} 
    799806 
     
    829836{ 
    830837        char *msg[] = {"Frustum Culling", "Stop and Wait", 
    831                                     "Coherent Culling"}; 
     838                                    "CHC", "CHC ++"}; 
    832839 
    833840        char msg2[200]; 
     
    843850         
    844851        float fps = 1e3f; 
    845         long renderTime = calcRenderTime(); 
     852        static long renderTime = traverser->GetStats().mRenderTime; 
     853 
     854        const float expFactor = 0.9f; 
     855 
     856        renderTime = renderTime * expFactor + (1.0f - expFactor) * traverser->GetStats().mRenderTime; 
     857 
    846858        if (renderTime) fps /= (float)renderTime; 
    847859 
     
    868880        else 
    869881        { 
    870                 glColor3f(1.0f ,1.0f ,1.0f ); 
    871                 output(20, winHeight - 10, msg[renderMode]); 
     882                glColor3f(1.0f, 1.0f , 1.0f); 
     883                output(10, winHeight - 10, msg[renderMode]); 
    872884 
    873885                if(showStatistics) 
Note: See TracChangeset for help on using the changeset viewer.