Changeset 3064 for GTP


Ignore:
Timestamp:
10/23/08 23:10:16 (16 years ago)
Author:
mattausch
Message:

working on dynamic bvh hierarchy: denug version

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

Legend:

Unmodified
Added
Removed
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/default.env

    r3003 r3064  
    1010winHeight=768 
    1111camPosition=483.398f 242.364f 186.078f 
    12 #camDirection=1 0 0 
     12camDirection=1 0 0 
    1313lightDirection=-0.8f 1.0f -0.7f 
    1414#lightDirection=0.0f 0.0f -1.0f 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp

    r3061 r3064  
    191191void Bvh::Init() 
    192192{ 
     193        mStaticRoot = NULL; 
     194        mDynamicRoot = NULL; 
    193195        mRoot = NULL; 
     196 
    194197        mVertices = NULL; 
    195198        mIndices = NULL; 
     
    198201        mNumNodes = 0; 
    199202         
     203        // nodes are tested using the subnodes from 3 levels below 
    200204        mMaxDepthForTestingChildren = 3; 
    201205        //mMaxDepthForTestingChildren = 4; 
     206 
     207        // the ratio of area between node and subnodes where  
     208        // testing the subnodes as proxy is still considered feasable 
    202209        mAreaRatioThreshold = 2.0f; 
    203210        //mAreaRatioThreshold = 1.4f; 
     
    209216 
    210217 
    211 void Bvh::PullUpLastVisited(BvhNode *node, const int frameId) const 
     218 
     219 
     220////////////////////// 
     221//-- functions that are used during the main traversal 
     222 
     223void Bvh::PullUpLastVisited(BvhNode *node, int frameId) const 
    212224{                
    213225        BvhNode *parent = node->GetParent(); 
     
    232244} 
    233245 
     246 
     247//////////////////////////////// 
    234248 
    235249void Bvh::CollectLeaves(BvhNode *node, BvhLeafContainer &leaves) 
     
    403417        if (!mDynamicEntities.empty()) 
    404418        { 
    405                 UpdateDynamicBranch(); 
     419                //UpdateDynamicBranch(); 
    406420        } 
    407421} 
     
    464478                                          bool useTightBounds) 
    465479{ 
    466         // if not using tight bounds, rendering boxes in immediate mode 
    467         // is preferable to vertex arrays (less setup time) 
    468  
    469480        int renderedBoxes; 
    470481 
    471482        if (!useTightBounds) 
    472483        { 
     484                // if not using tight bounds, rendering boxes in immediate mode 
     485                // is preferable to vertex arrays (less setup time) 
    473486                BvhNodeContainer::const_iterator nit, nit_end = nodes.end(); 
    474487                         
     
    477490                        RenderBoundingBoxImmediate((*nit)->GetBox()); 
    478491                } 
    479  
     492cout<<"y"; 
    480493                renderedBoxes = (int)nodes.size(); 
    481494        } 
    482495        else 
    483         { 
     496        {cout<<"x"; 
    484497                renderedBoxes = PrepareBoundsWithDrawArrays(nodes); 
    485498                RenderBoundsWithDrawArrays(renderedBoxes, state); 
     
    492505int Bvh::PrepareBoundsWithDrawArrays(const BvhNodeContainer &nodes) 
    493506{ 
    494         ////// 
    495         //-- for the first time we come here ... 
     507        /////////////////// 
     508        //-- for the first time we come here => create vbo and indices 
    496509 
    497510        if (!mIndices) 
    498         {       // create list of indices 
     511        {        
     512                // create list of indices 
    499513                CreateIndices(); 
    500514        } 
     
    532546void Bvh::RenderBoundsWithDrawArrays(int numNodes, RenderState *state) 
    533547{ 
    534         ////// 
    535         //-- Rendering the vbo 
     548        ///////// 
     549        //-- Render the vbo 
    536550 
    537551        if (state->GetCurrentVboId() != mVboId) 
     
    555569        // collect bvh nodes 
    556570        BvhNodeContainer nodes; 
    557         CollectNodes(mRoot, nodes); 
     571        CollectNodes(mStaticRoot, nodes); 
    558572 
    559573        cout << "creating new indices" << endl; 
     
    567581                int offset = (*lit)->mNumTestNodes * sNumIndicesPerBox; 
    568582#ifdef ALIGN_INDICES 
    569                 // align with 32 
     583                // align with 32 in order to speed up memcopy 
    570584                offset = (offset / 32) * 32 + 32;  
    571585#endif 
     
    582596        // to allocate memory from the chunks of the node 
    583597        mIndices = new unsigned int[numMaxIndices]; 
    584  
    585598        // create new index buffer for the individual nodes 
    586599        mTestIndices = new unsigned int[numMaxIndices]; 
     
    627640        CollectNodes(mRoot, nodes); 
    628641 
    629         // assign ids to all nodes of the "regular" hierarchy 
     642        // assign unique ids to all nodes of the hierarchy 
    630643        int i = 0; 
    631644        BvhNodeContainer::const_iterator lit, lit_end = nodes.end(); 
     
    643656        BvhNodeContainer nodes; 
    644657 
    645         nodes.reserve(GetNumNodes()); 
    646         CollectNodes(mRoot, nodes); 
     658        nodes.reserve(GetNumStaticNodes()); 
     659        CollectNodes(mStaticRoot, nodes); 
    647660 
    648661        const unsigned int bufferSize = 8 * (int)nodes.size(); 
     
    659672 
    660673                for (int j = 0; j < 8; ++ j) 
    661                         ((Vector3 *)mVertices)[node->GetId() * 8 + j] = node->GetBox().GetVertex(j); 
     674                        (static_cast<Vector3 *>(mVertices))[node->GetId() * 8 + j] = node->GetBox().GetVertex(j); 
    662675        } 
    663676 
     
    676689        DEL_PTR(mVertices); 
    677690 
    678         cout << "***** created vbos for tighter bounds *********" << endl; 
     691        cout << "******** created vbos for tighter bounds *********" << endl; 
    679692} 
    680693 
     
    704717        // collect all nodes 
    705718        BvhNodeContainer nodes; 
    706         CollectNodes(mRoot, nodes); 
     719        CollectNodes(mStaticRoot, nodes); 
    707720 
    708721        cout << "recomputing bounds, children will be tested in depth " << mMaxDepthForTestingChildren << endl; 
     
    739752        // using the tighter bounds is not feasable in case 
    740753        // that the tighter bounds represent nearly the same projected area 
    741         // as the old bounding box. Find this out using either volume or area 
    742         // heuristics 
     754        // as the old bounding box. Test this property using either  
     755        // volume or area heuristics 
    743756 
    744757        float vol = 0; 
     
    915928void Bvh::SetVirtualLeaves(int numTriangles)  
    916929{ 
    917         // first invalidate old leaves 
     930        // first invalidate old virtual leaves 
    918931        BvhNodeContainer leaves; 
    919  
    920         CollectVirtualLeaves(mRoot, leaves); 
     932        CollectVirtualLeaves(mStaticRoot, leaves); 
    921933 
    922934        BvhNodeContainer::const_iterator bit, bit_end = leaves.end(); 
     
    929941        mNumVirtualNodes = 0; 
    930942 
    931         // assign new virtual leaves based on specified number of triangles per leaf 
     943        // assign new virtual leaves based on specified #triangles per leaf 
    932944        std::stack<BvhNode *> nodeStack; 
     945 
    933946        nodeStack.push(mRoot); 
    934947 
     
    952965                        BvhNode *b = interior->mBack; 
    953966 
    954                         if (node->mIsMaxDepthForVirtualLeaf || (CountTriangles(node) <= numTriangles)) 
     967                        if (node->mIsMaxDepthForVirtualLeaf ||  
     968                                (CountTriangles(node) <= numTriangles)) 
    955969                        { 
    956970                                 node->mIsVirtualLeaf = true; 
     
    964978        } 
    965979 
     980        /// Reset the node states 
    966981        ResetNodeClassifications(); 
    967982} 
     
    970985void Bvh::PostProcess()  
    971986{ 
     987        // this function must be called once after hierarchy creation 
     988 
     989        // We initialize the virtual leaves 
     990        // of this bvh, i.e., the nodes that are used as 
     991        // leaves of the hierarchy during traversal. 
     992 
     993        // Initially they are set either 
     994        // a) to the real leaves of the hierarchy or 
     995        // b) the point where the subdivision on object level ends 
     996        // and the subsequent nodes are just used to provide tighter bounds 
     997        // (see article for the notations) 
     998 
    972999        std::stack<BvhNode *> nodeStack; 
    973         nodeStack.push(mRoot); 
     1000        nodeStack.push(mStaticRoot); 
    9741001 
    9751002        while (!nodeStack.empty())  
     
    9891016                        BvhNode *b = interior->mBack; 
    9901017 
    991                         // point reached where we cannot subdivide further on object level 
    9921018                        if ((f->mFirst == b->mFirst) && (f->mLast == b->mLast)) 
    9931019                        { 
     1020                                // point reached where beyond there would be no further reduction  
     1021                                // as both subtrees contain the same objects => stop here 
     1022                                // The tree beyond the current node is used to describe 
     1023                                // tighter bounds on the geometry contained  in it 
    9941024                                node->mIsMaxDepthForVirtualLeaf = true; 
    9951025                        } 
     
    10141044                //-- render AABB as triangle strips 
    10151045 
     1046                // render first half of AABB 
    10161047                glVertex3f(l.x, l.y, u.z); 
    10171048                glVertex3f(u.x, l.y, u.z); 
     
    10251056                glPrimitiveRestartNV(); 
    10261057 
    1027                 //-- render second half of AABB 
     1058                // render second half of AABB 
    10281059                glVertex3f(l.x, u.y, u.z);  
    10291060                glVertex3f(l.x, u.y, l.z); 
     
    10881119 
    10891120 
    1090 void Bvh::RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds) 
     1121void Bvh::RenderBoundsForViz(BvhNode *node,  
     1122                                                         RenderState *state,  
     1123                                                         bool useTightBounds) 
    10911124{ 
    10921125        glDisable(GL_TEXTURE_2D); 
     
    11191152 
    11201153//////////////////////// 
    1121 // construction of the dynamic hierarchy 
     1154//-- functions for construction of the dynamic hierarchy 
    11221155 
    11231156int Bvh::SortTriangles(BvhLeaf *leaf,  
     
    11371170                        -- j; 
    11381171 
    1139                 if (i < j)  
    1140                 { 
    1141                         swap(entities[i], entities[j]); 
     1172                // sorting finished 
     1173                if (i >= j) break; 
     1174 
     1175                // swap entities 
     1176                swap(entities[i], entities[j]); 
    11421177                         
    1143                         ++ i; 
    1144                         -- j; 
    1145                 } 
    1146                 else 
    1147                 { 
    1148                         break; 
    1149                 } 
     1178                ++ i; 
     1179                -- j; 
    11501180        } 
    11511181 
     
    11691199{ 
    11701200        if (TerminationCriteriaMet(leaf)) 
     1201        { 
     1202                leaf->mIsVirtualLeaf = true; 
    11711203                return leaf; 
    1172          
     1204        } 
     1205 
    11731206        //int axis = leaf->mBox.MajorAxis(); 
    11741207        int axis = (parentAxis + 1) % 3; 
     
    12091242        front->mLast = leaf->mLast; 
    12101243        front->mDepth = leaf->mDepth + 1; 
     1244 
    12111245        leaf->mLast = split; 
    12121246        leaf->mDepth = front->mDepth; 
     
    12381272        BvhNode *dynamicRoot = static_cast<BvhInterior *>(mRoot)->mBack; 
    12391273 
    1240         cout << "updating dynamic branch" << endl; 
    1241  
    12421274        // delete old branch 
    12431275        if (!dynamicRoot->IsLeaf()) 
     
    12511283        } 
    12521284 
     1285        cout << "updating dynamic branch" << endl; 
     1286 
    12531287        dynamicRoot = SubdivideLeaf(static_cast<BvhLeaf *>(dynamicRoot), 0, mDynamicEntities); 
    1254 } 
    1255  
    1256 } 
     1288 
     1289        cout << "finished updating dynamic branch" << endl; 
     1290} 
     1291 
     1292 
     1293void Bvh::AddDynamicObject(SceneEntity *ent) 
     1294{ 
     1295        mDynamicEntities.push_back(ent); 
     1296} 
     1297 
     1298 
     1299} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h

    r3059 r3064  
    478478        */ 
    479479        BvhNode *GetRoot() { return mRoot; } 
    480         /** Sets the scene camera 
    481         */ 
    482         //void SetCamera(Camera * camera) { mCamera = camera; } 
    483  
     480         
    484481        /////////////// 
    485482        //-- functions collecting nodes based on some criteria 
    486483 
     484        /** Collect all nodes higher or equal to a given depth. 
     485        */ 
    487486        void CollectNodes(BvhNode *node, BvhNodeContainer &nodes, int depth); 
    488487        void CollectNodes(BvhNode *node, BvhNodeContainer &nodes); 
     488        /** Collect the "physical" leaves of the hierarchy 
     489        */ 
    489490        void CollectLeaves(BvhNode *node, BvhLeafContainer &leaves); 
    490         /** Collect only the virtual leaves. 
     491        /** Collect only the virtual leaves (can be anywhere in the Žhierarchy). 
    491492        */ 
    492493        void CollectVirtualLeaves(BvhNode *node, BvhNodeContainer &leaves); 
     
    543544        */ 
    544545        void ResetNodeClassifications(); 
    545         /** Helper function that renders the bounding boxes of the leaves as 
    546                 wireframes for visualization purpose. 
    547         */ 
    548         //void RenderBoundingBoxesForViz(int mode); 
    549546        /** Count triangles the node contains. 
    550547        */ 
     
    557554        void ComputeIds(); 
    558555        /** Assign virtual leaves based on specified number of triangles per leaf. 
     556                That means that we try to set the leaves at a point where we 
     557                have less than numTriangles triangles within this leaf and beyond. 
     558 
     559                Virtual leaves are nodes that are determined to be the leaves 
     560                of the hierarchy during render traversal. These nodes are not necessarily  
     561                the same as the real leaves of the hierarchy and can be anywhere 
     562                in the hierarchy.  
     563                Please refer to the article for more info about virtual leaves 
    559564        */ 
    560565        void SetVirtualLeaves(int numTriangles); 
    561566         
    562567 
    563         //////// 
    564         //-- functions influencing tighter bounds 
     568        //////////// 
     569        //-- functions influencing the tightness of the bounds that are used for testing 
    565570 
    566571 
    567572        /** Sets maximal depth for taking the bounding boxes to test the 
    568573                visibility of a node.  
    569                 Deeper => the bounds adapt more to the geometry. 
     574                Deeper level means that the bounds adapt more to the geometry but 
     575                also that more boxes are rendered 
    570576        */ 
    571577        void SetMaxDepthForTestingChildren(int maxDepth); 
    572  
     578        /** The ratio of area between node and subnodes where  
     579                testing the subnodes as proxy is still considered feasable 
     580        */ 
    573581        void SetAreaRatioThresholdForTestingChildren(float ratio); 
    574582 
     
    583591        void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds); 
    584592 
     593        void AddDynamicObject(SceneEntity *ent); 
     594 
    585595 
    586596protected: 
     
    608618         
    609619        void PrepareVertices(); 
    610  
     620        /** Prepare nodes for vbo rendering. 
     621        */ 
    611622        int PrepareBoundsWithDrawArrays(const BvhNodeContainer &nodes); 
     623        /** Render the nodes from the vbo prepared previously. 
     624        */ 
    612625        void RenderBoundsWithDrawArrays(int numNodes, RenderState *state); 
    613626 
     
    643656 
    644657        ///////////////////////////// 
    645  
     658        // functions that are required to build the dynamic part of the hierarchy 
    646659        int SortTriangles(BvhLeaf *leaf,  
    647660                              int axis,  
     
    656669                                   int parentAxis,  
    657670                                                   SceneEntityContainer &entities); 
    658  
    659671        /** Recompute the dynamic branch of the hierarchy. 
    660672        */ 
     
    663675        inline bool TerminationCriteriaMet(BvhLeaf *leaf) const; 
    664676 
     677        /** Returns number of bvh nodes. 
     678        */ 
     679        inline int GetNumStaticNodes() const { return mNumNodes; } 
    665680 
    666681        //////////////////////// 
     
    670685        /// the root of the hierarchy 
    671686        BvhNode *mRoot; 
     687        /// the root of static part of the the hierarchy 
     688        BvhNode *mStaticRoot; 
     689        /// the root of dynamic part of the the hierarchy 
     690        BvhNode *mDynamicRoot; 
    672691        /// pointers to the geometry associated with this node 
    673692        SceneEntity **mGeometry; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp

    r3052 r3064  
    7373 
    7474        Bvh *bvh = new Bvh(entities); 
     75        bvh->mStaticRoot = LoadNextNode(stream, NULL); 
    7576 
    76         BvhNode *root = LoadNextNode(stream, NULL); 
     77#if 1 
     78        bvh->mRoot = bvh->mStaticRoot; 
     79        bvh->mNumNodes = 1; 
     80#else 
    7781 
    78 #if 0 
     82        // we are setting a new root node  
     83        // and adds one level of indirection to the bvh  
     84        // It allows us to use dynamic objects 
    7985 
    80         bvh->mRoot = root; 
    81  
    82 #else 
    83         // we are setting a new root node 
    84         // which adds one level of indirection to the bvh but  
    85         // allows us to use dynamic objects 
    8686        // the new bvh has two main branches 
    87         // one is the static branch (the old root), 
    88         // one is the dynamic branch  
     87        // a static branch (the old root), and adynamic branch  
    8988        // we create a 'dynamic' leaf which basically is a container 
    9089        // for all dynamic objects underneath 
     
    9392        // the movements of the objects within 
    9493 
     94        // create new root 
    9595        BvhInterior *newRoot = new BvhInterior(NULL); 
    9696        bvh->mRoot = newRoot; 
    9797 
    98         BvhLeaf *dynamicLeaf = new BvhLeaf(newRoot); 
     98        // the separation is a purely logical one 
     99        // the bounding boxes of the child nodes are  
     100        // identical to those of the root node 
    99101 
    100         newRoot->mFront = root; 
    101         newRoot->mBack = dynamicLeaf; 
    102         root->mParent = newRoot; 
     102        newRoot->mBox = bvh->mStaticRoot->mBox; 
     103        newRoot->mArea = newRoot->mBox.SurfaceArea(); 
    103104 
    104         // the separation is a purely logical one 
    105         // the bounding boxes of the child nodes is the 
    106         // same as for the root node 
    107         newRoot->mBox = root->mBox; 
    108         dynamicLeaf->mBox = root->mBox; 
     105        newRoot->mFirst = bvh->mStaticRoot->mFirst; 
     106        newRoot->mLast = bvh->mStaticRoot->mLast; 
    109107 
    110         newRoot->mArea = newRoot->mBox.SurfaceArea(); 
    111         dynamicLeaf->mArea = dynamicLeaf->mBox.SurfaceArea(); 
     108        // add static root on left subtree 
     109        newRoot->mFront = bvh->mStaticRoot; 
     110        bvh->mStaticRoot->mParent = newRoot; 
     111         
     112        // create and add dynamic root 
     113        BvhLeaf *dynamicRoot = new BvhLeaf(newRoot); 
     114         
     115        dynamicRoot->mFirst = 0; 
     116        dynamicRoot->mLast = 0; 
     117        dynamicRoot->mBox = bvh->mStaticRoot->mBox; 
     118        dynamicRoot->mArea = dynamicRoot->mBox.SurfaceArea(); 
    112119 
    113         newRoot->mFirst = root->mFirst; 
    114         newRoot->mLast = root->mLast; 
     120        bvh->mDynamicRoot = dynamicRoot; 
     121        newRoot->mBack = dynamicRoot; 
    115122 
    116         dynamicLeaf->mFirst = 0; 
    117         dynamicLeaf->mLast = 0; 
     123        bvh->mNumNodes = 1; 
     124 
    118125#endif 
    119126 
    120         tQueue.push(root); 
    121         bvh->mNumNodes = 1; 
     127        tQueue.push(bvh->mStaticRoot); 
    122128 
    123129        while(!tQueue.empty()) 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r3063 r3064  
    178178 
    179179bool useOptimization = false; 
    180 bool useTightBounds = true; 
     180//bool useTightBounds = true; 
     181bool useTightBounds = false; 
    181182bool useRenderQueue = true; 
    182183bool useMultiQueries = true; 
     
    483484 
    484485        buddha->GetTransform()->SetMatrix(transl); 
     486 
     487        bvh->AddDynamicObject(buddha); 
     488 
    485489 
    486490        // this function assign the render queue bucket ids of the materials in beforehand 
Note: See TracChangeset for help on using the changeset viewer.