Ignore:
Timestamp:
10/27/08 03:26:48 (16 years ago)
Author:
mattausch
Message:

included vbo support for dynamic objects

File:
1 edited

Legend:

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

    r3073 r3074  
    207207Bvh::~Bvh()  
    208208{ 
    209         if (mVertices) delete [] mVertices;  
    210         if (mIndices) delete [] mIndices; 
    211         if (mTestIndices) delete [] mTestIndices; 
    212         if (mGeometry) delete [] mGeometry; 
     209        DEL_ARRAY_PTR(mVertices); 
     210        DEL_ARRAY_PTR(mIndices); 
     211        DEL_ARRAY_PTR(mTestIndices); 
     212        DEL_ARRAY_PTR(mGeometry); 
    213213 
    214214        if (mRoot) delete mRoot; 
     
    220220void Bvh::Init() 
    221221{ 
    222         //mStaticRoot = NULL; 
     222        mStaticRoot = NULL; 
    223223        mDynamicRoot = NULL; 
    224224        mRoot = NULL; 
     
    435435 
    436436 
    437 void Bvh::InitFrame(Camera *cam) 
     437void Bvh::InitFrame(Camera *cam, RenderState *state) 
    438438{ 
    439439        // = 0011 1111 which means that at the beginning, all six planes have to frustum culled 
     
    452452        { 
    453453                UpdateDynamicBranch(mDynamicRoot); 
     454                UpdateDynamicBounds(state); 
    454455        } 
    455456} 
     
    532533        /////////////////// 
    533534        //-- for the first time we come here => create vbo and indices 
    534 /* 
     535 
    535536        if (!mIndices) 
    536537        {        
     
    544545                PrepareVertices(); 
    545546        } 
    546 */ 
     547 
    547548        /////////////// 
    548549 
    549550        int numNodes = 0; 
    550 /* 
     551 
    551552        BvhNodeContainer::const_iterator nit, nit_end = nodes.end(); 
    552553 
     
    563564                numNodes += node->mNumTestNodes; 
    564565        } 
    565 */ 
     566 
    566567        return numNodes; 
    567568} 
     
    660661        // collect all nodes 
    661662        BvhNodeContainer nodes; 
    662         CollectNodes(mRoot, nodes); 
    663  
    664         // assign ids to all nodes of the "regular" hierarchy 
     663        //CollectNodes(mRoot, nodes); 
     664        // first collect dynamic nodes so we make sure that they are in the beginning 
     665        CollectNodes(mDynamicRoot, nodes); 
     666        // then collect static nodes 
     667        CollectNodes(mStaticRoot, nodes); 
     668        // also add root 
     669        nodes.push_back(mRoot); 
     670 
     671        // assign ids to all nodes of the hierarchy 
    665672        int i = 0; 
    666673        BvhNodeContainer::const_iterator lit, lit_end = nodes.end(); 
     
    679686 
    680687        nodes.reserve(GetNumNodes()); 
    681         CollectNodes(mRoot, nodes); 
     688        // first collect dynamic nodes so we make sure that they are in the beginning 
     689        CollectNodes(mDynamicRoot, nodes); 
     690        // then collect static nodes 
     691        CollectNodes(mStaticRoot, nodes); 
     692        // also add root 
     693        nodes.push_back(mRoot); 
    682694 
    683695        const unsigned int bufferSize = 8 * (int)nodes.size(); 
     
    704716                            bufferSize * sizeof(Vector3),  
    705717                            mVertices,  
    706                                         GL_STATIC_DRAW_ARB); 
     718                                        //GL_STATIC_DRAW_ARB); 
     719                                        GL_DYNAMIC_DRAW_ARB); 
    707720 
    708721        glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); 
    709722 
    710723        // data handled by graphics driver from now on 
    711         DEL_PTR(mVertices); 
     724        DEL_ARRAY_PTR(mVertices); 
    712725 
    713726        cout << "******** created vbos for tighter bounds *********" << endl; 
     727} 
     728 
     729 
     730void Bvh::UpdateDynamicBounds(RenderState *state) 
     731{ 
     732        // vbos not created yet 
     733        if (mVboId == -1) return; 
     734 
     735        // collect all nodes 
     736        static BvhNodeContainer nodes; 
     737        nodes.clear(); 
     738        //nodes.reserve(GetNumNodes()); 
     739        CollectNodes(mDynamicRoot, nodes); 
     740 
     741        const unsigned int bufferSize = 8 * (int)nodes.size(); 
     742         
     743        if (!mVertices) mVertices = new Vector3[bufferSize]; 
     744         
     745        int i = 0; 
     746         
     747        // store bounding box vertices 
     748        BvhNodeContainer::const_iterator lit, lit_end = nodes.end(); 
     749 
     750        for (lit = nodes.begin(); lit != lit_end; ++ lit, i += 8) 
     751        { 
     752                BvhNode *node = *lit; 
     753 
     754                for (int j = 0; j < 8; ++ j) 
     755                        (static_cast<Vector3 *>(mVertices))[node->GetId() * 8 + j] = node->GetBox().GetVertex(j); 
     756        } 
     757         
     758        if (state->GetCurrentVboId() != mVboId) 
     759        { 
     760                glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId); 
     761                // set the vertex pointer to the vertex buffer 
     762                glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);   
     763 
     764                state->SetCurrentVboId(mVboId); 
     765        } 
     766 
     767        glBufferSubDataARB(GL_ARRAY_BUFFER_ARB, 0, 
     768                                           bufferSize * sizeof(Vector3),  
     769                                           mVertices); 
     770 
     771        glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); 
    714772} 
    715773 
     
    10611119        CreateRoot(); 
    10621120 
    1063         ComputeIds(); 
    10641121        ComputeMaxDepthForVirtualLeaves(); 
    10651122        // set virtual leaves for specified number of triangles 
     
    10671124        /// for each node compute the number of leaves under this node 
    10681125        UpdateNumLeaves(mRoot); 
     1126        // compute new ids 
     1127        ComputeIds(); 
    10691128        // specify bounds for occlusion tests 
    10701129        RecomputeBounds(); 
     1130 
     1131        mBox = mRoot->GetBox(); 
    10711132 
    10721133        // compute and print stats 
     
    12671328        if (TerminationCriteriaMet(leaf)) 
    12681329        { 
    1269                 leaf->mIsVirtualLeaf = true; 
    1270                 leaf->mIsMaxDepthForVirtualLeaf = true; 
    12711330                //cout << "leaf constructed:" << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl; 
    12721331                return leaf; 
     
    14121471        // create new root 
    14131472        mRoot = new BvhInterior(NULL); 
    1414          
     1473 
    14151474        // the separation is a purely logical one 
    14161475        // the bounding boxes of the child nodes are  
Note: See TracChangeset for help on using the changeset viewer.