Changeset 3059


Ignore:
Timestamp:
10/21/08 20:41:27 (16 years ago)
Author:
mattausch
Message:

working on dynamic objects

Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/Converter/ObjConverter.cpp

    r3012 r3059  
    7070                if (indices.size() > 2) 
    7171                { 
    72 #if 0 
     72#if 1 
    7373                        int idx1 = 0; 
    7474                        int idx2 = (int)indices.size() - 2; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h

    r3053 r3059  
    155155        */ 
    156156        inline int CountPrimitives() const; 
    157  
     157        /** This is an imporantent function once you have one or more 
     158                view point changes within a frame (e.g., camera view and shadow view for 
     159                shadow mapping), because each camera needs its own state in 
     160                order to not break temporal coherency. 
     161        */ 
    158162        static void SetCurrentState(int _state) { sCurrentState = _state; } 
    159163 
     
    170174 
    171175        ////////////// 
    172         //-- members that define the current state 
     176        //-- members that define the current state. There is one state for each camera 
     177 
     178        /// the currently used state 
     179        static int sCurrentState; 
    173180 
    174181        /// stores the visibility related info  
    175182        VisibilityInfo mVisibility[NUM_STATES]; 
    176183         
    177         /// used for view frustum culling 
     184        // members used to speed up view frustum culling 
     185        /// masks out planes of the frustum that do not have to be tested against 
    178186        int mPlaneMask[NUM_STATES]; 
     187        /// the plane that is usually responsible for culling this node.  
     188        /** if our educated guess was right, we save up to 5 comparisons. 
     189        */ 
    179190        int mPreferredPlane[NUM_STATES]; 
    180  
    181191        /// when the node was last rendered 
    182192        int mLastRenderedFrame[NUM_STATES]; 
    183  
    184  
    185         // the current state 
    186         static int sCurrentState; 
    187  
    188193 
    189194        //////////////////// 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r3057 r3059  
    7575Bvh *bvh = NULL; 
    7676/// handles scene loading 
    77 ResourceManager *loader = NULL; 
     77ResourceManager *resourceManager = NULL; 
    7878/// handles scene loading 
    7979ShaderManager *shaderManager = NULL; 
     
    121121int renderMethod = RENDER_FORWARD; 
    122122 
     123static int winWidth = 1024; 
     124static int winHeight = 768; 
     125static float winAspectRatio = 1.0f; 
    123126 
    124127/// these values get scaled with the frame rate 
     
    127130 
    128131/// elapsed time in milliseconds 
    129 double elapsedTime = 1000.0f; 
    130 double algTime = 1000.0f; 
    131  
    132 static int winWidth = 1024; 
    133 static int winHeight = 768; 
     132double elapsedTime = 1000.0; 
     133double algTime = 1000.0; 
     134double accumulatedTime = 1000.0; 
     135float fps = 1e3f; 
    134136 
    135137int shadowSize = 2048; 
    136138 
    137 static float winAspectRatio = 1.0f; 
    138  
    139 double accumulatedTime = 1000; 
    140 float fps = 1e3f; 
    141  
     139/// the hud font 
    142140glfont::GLFont myfont; 
    143141 
     
    157155int numBatches = 0; 
    158156 
    159  
    160157// mouse navigation state 
    161158int xEyeBegin = 0; 
     
    200197 
    201198PerfTimer frameTimer, algTimer; 
    202  
     199/// the performance window 
    203200PerformanceGraph *perfGraph = NULL; 
    204201 
     
    217214DeferredRenderer *ssaoShader = NULL; 
    218215 
    219 SceneEntity *cube = NULL; 
    220 SceneEntity *aeroplane = NULL; 
     216//SceneEntity *cube = NULL; 
     217SceneEntity *buddha = NULL; 
    221218SceneEntity *skyDome = NULL; 
    222219 
    223220 
    224  
    225 // function forward declarations 
     221//////////////////// 
     222//--- function forward declarations 
     223 
    226224void InitExtensions(); 
     225void InitGLstate(); 
     226 
    227227void DisplayVisualization(); 
    228 void InitGLstate(); 
    229 void InitRenderTexture(); 
     228/// destroys all allocated resources 
    230229void CleanUp(); 
    231230void SetupEyeView(); 
    232 void UpdateEyeMtx(); 
    233231void SetupLighting(); 
    234232void DisplayStats(); 
    235 void Output(int x, int y, const char *string); 
     233/// draw the help screen 
    236234void DrawHelpMessage(); 
     235/// render the sky dome 
    237236void RenderSky(); 
     237/// render the objects found visible in the depth pass 
    238238void RenderVisibleObjects(); 
    239239 
    240240void Begin2D(); 
    241241void End2D(); 
     242/// the main loop 
     243void MainLoop(); 
     244 
    242245void KeyBoard(unsigned char c, int x, int y); 
    243 void DrawStatistics(); 
    244 void Display(); 
    245246void Special(int c, int x, int y); 
    246247void KeyUp(unsigned char c, int x, int y); 
     
    251252void RightMotion(int x, int y); 
    252253void MiddleMotion(int x, int y); 
    253 void CalcDecimalPoint(string &str, int d); 
    254  
    255 RenderTraverser *CreateTraverser(Camera *cam); 
    256  
    257254void KeyHorizontalMotion(float shift); 
    258255void KeyVerticalMotion(float shift); 
    259  
     256/// returns the string representation of a number with the decimal points 
     257void CalcDecimalPoint(string &str, int d); 
     258/// Creates the traversal method (vfc, stopandwait, chc, chc++) 
     259RenderTraverser *CreateTraverser(Camera *cam); 
     260 
     261/// place the viewer on the floor plane 
    260262void PlaceViewer(const Vector3 &oldPos); 
     263// initialise the frame buffer objects 
     264void InitFBO(); 
     265/// changes the sunlight direction 
     266void RightMotionLight(int x, int y); 
     267/// render the shader map 
     268void RenderShadowMap(float newfar); 
     269/// function that touches each material once in order to accelarate render queue 
     270void PrepareRenderQueue(); 
     271/// loads the specified model 
     272void LoadModel(const string &model); 
    261273 
    262274inline float KeyRotationAngle() { return keyRotation * elapsedTime * 1e-3f; } 
    263275inline float KeyShift() { return keyForwardMotion * elapsedTime * 1e-3f; } 
    264 // initialise the frame buffer objects 
    265 void InitFBO(); 
    266  
    267 void RightMotionLight(int x, int y); 
    268  
    269 void RenderShadowMap(float newfar); 
    270  
    271  
     276 
     277// the new and the old viewProjection matrix of the current camera 
    272278static Matrix4x4 viewProjMat = IdentityMatrix(); 
    273279static Matrix4x4 oldViewProjMat = IdentityMatrix(); 
     
    291297{ 
    292298#ifdef _CRT_SET 
    293  
    294299        //Now just call this function at the start of your program and if you're 
    295300        //compiling in debug mode (F5), any leaks will be displayed in the Output 
     
    399404        } 
    400405 
    401         glutDisplayFunc(Display); 
     406        glutDisplayFunc(MainLoop); 
    402407        glutKeyboardFunc(KeyBoard); 
    403408        glutSpecialFunc(Special); 
    404409        glutReshapeFunc(Reshape); 
    405410        glutMouseFunc(Mouse); 
    406         glutIdleFunc(Display); 
     411        glutIdleFunc(MainLoop); 
    407412        glutKeyboardUpFunc(KeyUp); 
    408413        glutSpecialUpFunc(SpecialKeyUp); 
     
    421426        perfGraph = new PerformanceGraph(1000); 
    422427 
    423         loader = ResourceManager::GetSingleton(); 
     428        resourceManager = ResourceManager::GetSingleton(); 
    424429        shaderManager = ShaderManager::GetSingleton(); 
    425430 
    426         const string filename = string(model_path + "city.dem"); 
    427          
    428         if (loader->Load(filename, sceneEntities)) 
    429                 cout << "model " << filename << " loaded" << endl; 
    430         else 
    431         { 
    432                 cerr << "loading model " << filename << " failed" << endl; 
    433                 CleanUp(); 
    434                 exit(0); 
    435         } 
    436          
     431        /////////// 
     432        //-- load the static scene geometry 
     433 
     434        LoadModel("city.dem"); 
     435 
     436 
     437        /////////// 
     438        //-- load the associated static bvh 
    437439 
    438440        const string bvh_filename = string(model_path + "city.bvh"); 
     
    447449        } 
    448450 
     451        /// set the depth of the bvh depending on the triangles per leaf node 
     452        bvh->SetVirtualLeaves(trianglesPerVirtualLeaf); 
     453 
    449454        // set far plane based on scene extent 
    450455        farDist = 10.0f * Magnitude(bvh->GetBox().Diagonal()); 
    451         bvh->SetVirtualLeaves(trianglesPerVirtualLeaf); 
    452  
    453         camera->SetFar(Magnitude(bvh->GetBox().Diagonal())); 
    454          
    455         Vector3 cubeCenter(470.398f, 240.364f, 182.5f); 
    456          
    457         Matrix4x4 transl = TranslationMatrix(cubeCenter); 
    458  
    459         string skyDomeStr(model_path + "sky.dem"); 
    460  
    461         if (loader->Load(skyDomeStr, sceneEntities)) 
    462         { 
    463                 cout << "successfully loaded " << sceneEntities.size() << " scene entities" << endl; 
    464         } 
    465         else 
    466         { 
    467                 cerr << "loading file " << skyDomeStr << " failed" << endl; 
    468                 CleanUp(); 
    469                 exit(0); 
    470         } 
    471  
     456        camera->SetFar(farDist); 
     457 
     458 
     459        ////////////////// 
     460        //-- setup the skydome model 
     461 
     462        LoadModel("sky.dem"); 
    472463        skyDome = sceneEntities.back(); 
    473464 
     465        /// the turbitity of the sky (from clear to hazy, use <3 for clear sky) 
    474466        const float turbitiy = 5.0f; 
    475467        preetham = new SkyPreetham(turbitiy, skyDome); 
    476468 
    477         // initialize the render traverser 
     469 
     470        ////////// 
     471        //-- initialize the traversal algorithm 
     472 
    478473        traverser = CreateTraverser(camera); 
    479  
     474         
     475        // the bird-eye visualization 
    480476        visualization = new Visualization(bvh, camera, NULL, &state); 
    481          
    482         for (int i = 0; i < 3; ++ i) 
    483         { 
    484                 state.SetRenderTechnique((RenderState::RenderTechnique)i); 
    485  
    486                 // fill all shapes into the render queue        once so we can establish the buckets 
    487                 ShapeContainer::const_iterator sit, sit_end = (*loader->GetShapes()).end(); 
    488  
    489                 for (sit = (*loader->GetShapes()).begin(); sit != sit_end; ++ sit) 
    490                 { 
    491                         renderQueue->Enqueue(*sit); 
    492                 } 
    493          
    494                 //renderQueue->Apply(); 
    495                 // just clear queue again 
    496                 renderQueue->Clear(); 
    497         } 
    498  
     477 
     478 
     479        LoadModel("hbuddha.dem"); 
     480        buddha = sceneEntities.back(); 
     481 
     482        Vector3 sceneCenter(470.398f, 240.364f, 182.5f); 
     483        Matrix4x4 transl = TranslationMatrix(sceneCenter); 
     484 
     485        buddha->GetTransform()->SetMatrix(transl); 
     486 
     487        // this function assign the render queue bucket ids of the materials in beforehand 
     488        // => probably a little less overhead for new parts of the scene that are not yet assigned 
     489        PrepareRenderQueue(); 
     490         
     491        /// forward rendering is the default 
    499492        state.SetRenderTechnique(RenderState::FORWARD); 
    500493 
    501494        // frame time is restarted every frame 
    502495        frameTimer.Start(); 
     496 
    503497        // the rendering loop 
    504498        glutMainLoop(); 
     499         
    505500        // clean up 
    506501        CleanUp(); 
     
    709704} 
    710705 
    711  
     706/** Setup sunlight 
     707*/ 
    712708void SetupLighting() 
    713709{ 
     
    773769        // set up the camera view 
    774770        camera->SetupCameraView(); 
    775  
    776771                 
    777772        ///////////////// 
     773        //-- compute view projection matrix and store for later use 
    778774 
    779775        Matrix4x4 matViewing, matProjection; 
     
    782778        camera->GetProjectionMatrix(matProjection); 
    783779 
    784         // store matrix for later use 
    785780        viewProjMat = matViewing * matProjection; 
    786781} 
     
    810805 
    811806 
    812 static void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br) 
    813 { 
    814         Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr; 
    815  
    816         camera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr); 
    817  
    818         bl = Normalize(nbl - fbl); 
    819         br = Normalize(nbr - fbr); 
    820         tl = Normalize(ntl - ftl); 
    821         tr = Normalize(ntr - ftr); 
    822 } 
    823  
    824  
     807/** Initialize the deferred rendering pass. 
     808*/ 
    825809void InitDeferredRendering() 
    826810{ 
     
    856840 
    857841 
    858 // the main rendering loop 
    859 void Display()  
     842/** the main rendering loop 
     843*/ 
     844void MainLoop()  
    860845{        
    861846        Vector3 oldPos = camera->GetPosition(); 
     
    896881        // bring eye modelview matrix up-to-date 
    897882        SetupEyeView(); 
    898  
    899         // set GPU related parameters 
     883        // set frame related parameters for GPU programs 
    900884        GPUProgramParameters::InitFrame(camera, light); 
    901885 
    902  
    903         // hack 
     886        // hack: store current rendering method and restore later 
    904887        int oldRenderMethod = renderMethod; 
    905888        // for rendering the light view, we use forward rendering 
     
    907890                renderMethod = RenderState::FORWARD; 
    908891 
     892        /// enable vbo vertex array 
    909893        glEnableClientState(GL_VERTEX_ARRAY); 
    910  
    911894 
    912895        // render with the specified method (forward rendering, forward + depth, deferred) 
     
    980963 
    981964        glDepthFunc(GL_LESS); 
    982  
    983965        glDisable(GL_TEXTURE_2D); 
    984966        glDisableClientState(GL_TEXTURE_COORD_ARRAY); 
    985967                 
    986968 
    987         // reset lod levels for current frame 
     969        // set proper lod levels for current frame using current eye point 
    988970        LODLevel::InitFrame(camera->GetPosition()); 
    989  
    990         // set up lights 
     971        // set up sunlight 
    991972        SetupLighting(); 
    992973 
     
    994975        if (renderLightView) 
    995976        { 
    996                 // change CHC++ set of state variables (must be done for each change of camera because 
    997                 // otherwise the temporal coherency is broken 
     977                // change CHC++ set of state variables:  
     978                // must be done for each change of camera because otherwise  
     979                // the temporal coherency is broken 
    998980                BvhNode::SetCurrentState(LIGHT_PASS); 
    999981                shadowMap->RenderShadowView(shadowTraverser, viewProjMat); 
     
    16611643        ShaderManager::DelSingleton(); 
    16621644 
    1663         loader = NULL; 
     1645        resourceManager = NULL; 
    16641646        shaderManager = NULL; 
    16651647} 
     
    18221804                        int len = 10; 
    18231805                        CalcDecimalPoint(objStr, renderedObjects, len); 
    1824                         CalcDecimalPoint(totalObjStr, (int)loader->GetNumEntities(), len); 
     1806                        CalcDecimalPoint(totalObjStr, (int)resourceManager->GetNumEntities(), len); 
    18251807 
    18261808                        CalcDecimalPoint(triStr, renderedTriangles, len); 
     
    18731855void RenderSky() 
    18741856{ 
     1857        buddha->Render(&state); 
     1858 
    18751859        if ((renderMethod == RENDER_DEFERRED) || (renderMethod == RENDER_DEPTH_PASS_DEFERRED)) 
    18761860                state.SetRenderTechnique(RenderState::DEFERRED); 
     
    18791863                ((renderMethod == RENDER_DEPTH_PASS_DEFERRED) ||  
    18801864                 (renderMethod == RENDER_DEFERRED)) && useHDR; 
    1881  
     1865         
    18821866        preetham->RenderSkyDome(-light->GetDirection(), camera, &state, !useToneMapping); 
    18831867        /// once again reset the state 
     
    19951979        BvhNode::SetCurrentState(CAMERA_PASS); 
    19961980} 
     1981 
     1982/** Toach each material once in order to preload the render queue  
     1983        bucket id of each material 
     1984*/ 
     1985void PrepareRenderQueue() 
     1986{ 
     1987        for (int i = 0; i < 3; ++ i) 
     1988        { 
     1989                state.SetRenderTechnique((RenderState::RenderTechnique)i); 
     1990 
     1991                // fill all shapes into the render queue        once so we can establish the buckets 
     1992                ShapeContainer::const_iterator sit, sit_end = (*resourceManager->GetShapes()).end(); 
     1993 
     1994                for (sit = (*resourceManager->GetShapes()).begin(); sit != sit_end; ++ sit) 
     1995                { 
     1996                        renderQueue->Enqueue(*sit); 
     1997                } 
     1998         
     1999                // just clear queue again 
     2000                renderQueue->Clear(); 
     2001        } 
     2002} 
     2003 
     2004 
     2005void LoadModel(const string &model) 
     2006{ 
     2007        const string filename = string(model_path + model); 
     2008 
     2009        if (resourceManager->Load(filename, sceneEntities)) 
     2010                cout << "model " << filename << " loaded" << endl; 
     2011        else 
     2012        { 
     2013                cerr << "loading model " << filename << " failed" << endl; 
     2014                CleanUp(); 
     2015                exit(0); 
     2016        } 
     2017} 
Note: See TracChangeset for help on using the changeset viewer.