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

working on dynamic bvh hierarchy: denug version

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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} 
Note: See TracChangeset for help on using the changeset viewer.