Ignore:
Timestamp:
10/27/08 00:25:53 (16 years ago)
Author:
mattausch
Message:

fucked up depth order for some reason!!

File:
1 edited

Legend:

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

    r3071 r3072  
    3030 
    3131using namespace std; 
     32 
    3233 
    3334namespace CHCDemoEngine 
     
    8081static int sClipPlaneAABBVertexIndices[12]; 
    8182 
    82  
    8383#define ALIGN_INDICES 
     84 
    8485 
    8586BvhNode::BvhNode(BvhNode *parent):  
     
    154155inline AxisAlignedBox3 ComputeBoundingBox(SceneEntity **entities, int numEntities) 
    155156{ 
    156         AxisAlignedBox3 box = entities[0]->GetWorldBoundingBox(); 
    157  
    158         for (int i = 1; i < numEntities; ++ i) 
    159         { 
    160                 box.Include(entities[i]->GetWorldBoundingBox()); 
     157        AxisAlignedBox3 box; 
     158         
     159        if (!numEntities) 
     160        { 
     161                // no box=> just initialize 
     162                box.Initialize(); 
     163        } 
     164        else 
     165        { 
     166                box = entities[0]->GetWorldBoundingBox(); 
     167 
     168                for (int i = 1; i < numEntities; ++ i) 
     169                { 
     170                        box.Include(entities[i]->GetWorldBoundingBox()); 
     171                } 
    161172        } 
    162173 
     
    171182 
    172183 
    173 Bvh::Bvh(const SceneEntityContainer &entities) 
     184Bvh::Bvh(const SceneEntityContainer &staticEntities,  
     185                 const SceneEntityContainer &dynamicEntities) 
    174186{ 
    175187        Init(); 
    176188 
    177         mGeometrySize = entities.size(); 
     189        mGeometrySize = staticEntities.size() + dynamicEntities.size(); 
    178190        mGeometry = new SceneEntity*[mGeometrySize]; 
    179191 
    180         SceneEntityContainer::const_iterator it, it_end = entities.end(); 
    181  
    182         int i = 0; 
    183         for (it = entities.begin(); it != it_end; ++ it, ++ i) 
    184         { 
    185                 mGeometry[i] = (*it); 
     192        mStaticGeometrySize = staticEntities.size(); 
     193        mDynamicGeometrySize = dynamicEntities.size(); 
     194 
     195        for (size_t i = 0; i < mStaticGeometrySize; ++ i) 
     196        { 
     197                mGeometry[i] = staticEntities[i]; 
     198        } 
     199 
     200        for (size_t i = 0; i < mDynamicGeometrySize; ++ i) 
     201        { 
     202                mGeometry[mStaticGeometrySize + i] = dynamicEntities[i]; 
    186203        } 
    187204} 
     
    430447        sNear = cam->GetNear(); 
    431448 
    432         // rebuild dynamic part of the hierarchy 
    433         if (!mDynamicEntities.empty()) 
    434         { 
    435                 if (!mDynamicRoot) 
    436                         CreateDynamicBranch(); 
    437                  
     449        // update dynamic part of the hierarchy 
     450        //if (!mDynamicEntities.empty()) 
     451        if (mDynamicGeometrySize) 
     452        { 
    438453                UpdateDynamicBranch(mDynamicRoot); 
    439454        } 
     
    647662        CollectNodes(mRoot, nodes); 
    648663 
    649         // assign unique ids to all nodes of the hierarchy 
     664        // assign ids to all nodes of the "regular" hierarchy 
    650665        int i = 0; 
    651666        BvhNodeContainer::const_iterator lit, lit_end = nodes.end(); 
     
    788803        node->mTestNodesIdx = (int)mTestNodes.size(); 
    789804 
    790         // use the found nodes as nodes during the occlusion tests 
     805        // use the collected nodes as proxy for the occlusion tests 
    791806        for (cit = children.begin(); cit != children.end(); ++ cit) 
    792807        { 
     
    9921007 
    9931008 
    994 void Bvh::PostProcess()  
    995 { 
    996         // this function must be called once after hierarchy creation 
    997  
    998         // We initialize the virtual leaves 
     1009void Bvh::ComputeMaxDepthForVirtualLeaves() 
     1010{ 
     1011        // We initialize the maximal depth for virtual leaves 
    9991012        // of this bvh, i.e., the nodes that are used as 
    10001013        // leaves of the hierarchy during traversal. 
     
    10431056 
    10441057 
     1058// this function must be called once after hierarchy creation 
     1059void Bvh::PostProcess()  
     1060{ 
     1061        CreateRoot(); 
     1062 
     1063        ComputeIds(); 
     1064        ComputeMaxDepthForVirtualLeaves(); 
     1065        // set virtual leaves for specified number of triangles 
     1066        SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES); 
     1067        /// for each node compute the number of leaves under this node 
     1068        UpdateNumLeaves(mRoot); 
     1069        // specify bounds for occlusion tests 
     1070        RecomputeBounds(); 
     1071 
     1072        // compute and print stats 
     1073        ComputeBvhStats(mRoot, mBvhStats); 
     1074        PrintBvhStats(mBvhStats); 
     1075 
     1076        if (mDynamicGeometrySize) 
     1077        { 
     1078                BvhStats bvhStats; 
     1079                ComputeBvhStats(mDynamicRoot, bvhStats); 
     1080 
     1081                cout << "\n=========== Dynamic BVH statistics: =========" << endl; 
     1082                cout << "leaves = " << bvhStats.mLeaves << endl; 
     1083                cout << "interiorNodesSA = " << bvhStats.mInteriorSA << endl; 
     1084                cout << "leafNodesSA = " << bvhStats.mLeafSA << endl; 
     1085                cout << "interiorNodesVolume = " << bvhStats.mInteriorVol  << endl; 
     1086                cout << "leafNodesVolume = " << bvhStats.mLeafVol << endl; 
     1087 
     1088                cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl; 
     1089                cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl; 
     1090                cout << "=============================================" << endl << endl; 
     1091        } 
     1092} 
     1093 
     1094 
    10451095void Bvh::RenderBoundingBoxImmediate(const AxisAlignedBox3 &box) 
    10461096{ 
     
    12191269                leaf->mIsVirtualLeaf = true; 
    12201270                leaf->mIsMaxDepthForVirtualLeaf = true; 
    1221                 cout << "leaf contructed:" << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl; 
     1271                //cout << "leaf constructed:" << leaf->mBox << " " << leaf->mFirst << " " << leaf->mLast << endl; 
    12221272                return leaf; 
    12231273        } 
     
    12251275        //const int axis = (parentAxis + 1) % 3; 
    12261276        const int axis = leaf->mBox.MajorAxis(); 
    1227          
    12281277 
    12291278        const int scale = 20; 
     
    12481297        mNumNodes += 2; 
    12491298        BvhInterior *parent = new BvhInterior(leaf->GetParent()); 
    1250         BvhLeaf *front = new BvhLeaf(parent); 
     1299        parent->mFirst = leaf->mFirst; 
     1300        parent->mLast = leaf->mLast; 
    12511301         
    12521302        parent->mAxis = axis; 
     
    12541304        parent->mDepth = leaf->mDepth; 
    12551305 
     1306        BvhLeaf *front = new BvhLeaf(parent); 
     1307 
    12561308        parent->mBack = leaf; 
    12571309        parent->mFront = front; 
    1258         //parent->mPosition = pos; 
    1259          
     1310                 
    12601311        // now assign the triangles to the subnodes 
    12611312        front->mFirst = split + 1; 
     
    12671318        leaf->mParent = parent; 
    12681319         
    1269         // reset box 
    1270         //leaf->mBox.Initialize(); 
    1271  
    12721320        front->mBox = ComputeBoundingBox(mGeometry + front->mFirst, front->CountPrimitives()); 
    12731321        leaf->mBox = ComputeBoundingBox(mGeometry + leaf->mFirst, leaf->CountPrimitives()); 
     
    13291377 
    13301378        BvhLeaf *l = new BvhLeaf(mRoot); 
    1331  
    1332         l->mBox.Initialize(); 
    1333  
    1334         SceneEntityContainer::const_iterator sit, sit_end = mDynamicEntities.end(); 
    1335  
    1336         for (sit = mDynamicEntities.begin(); sit != sit_end; ++ sit) 
    1337         { 
    1338                 l->mBox.Include((*sit)->GetWorldBoundingBox()); 
    1339         } 
    1340  
    1341         l->mFirst = (int)(mGeometrySize - mDynamicEntities.size()); 
     1379        mDynamicRoot = l; 
     1380 
     1381        l->mBox = ComputeBoundingBox(mGeometry + mStaticGeometrySize, (int)mDynamicGeometrySize); 
     1382 
     1383        l->mFirst = (int)mStaticGeometrySize; 
    13421384        l->mLast = (int)mGeometrySize - 1; 
    13431385        l->mArea = l->mBox.SurfaceArea(); 
     
    13451387        cout << "updating dynamic branch " << l->mFirst << " " << l->mLast << endl; 
    13461388 
    1347         mDynamicRoot = SubdivideLeaf(l, 0); 
    1348  
    1349         BvhStats bvhStats; 
    1350         ComputeBvhStats(mDynamicRoot, bvhStats); 
    1351  
    1352         cout << "\n=========== Dynamic BVH statistics: =========" << endl; 
    1353         cout << "leaves = " << bvhStats.mLeaves << endl; 
    1354         cout << "interiorNodesSA = " << bvhStats.mInteriorSA << endl; 
    1355         cout << "leafNodesSA = " << bvhStats.mLeafSA << endl; 
    1356         cout << "interiorNodesVolume = " << bvhStats.mInteriorVol  << endl; 
    1357         cout << "leafNodesVolume = " << bvhStats.mLeafVol << endl; 
    1358  
    1359         cout << "geometry per leaf: " << bvhStats.mGeometryRatio << endl; 
    1360         cout << "triangles per leaf: " << bvhStats.mTriangleRatio << endl; 
    1361         cout << "=============================================" << endl << endl; 
    1362 } 
    1363  
    1364  
    1365 void Bvh::AddDynamicObjects(const SceneEntityContainer &entities) 
    1366 { 
    1367         // copy old entities 
    1368         SceneEntity **newGeom = new SceneEntity*[mGeometrySize + (int)entities.size()]; 
    1369  
    1370         memcpy(newGeom, mGeometry, mGeometrySize * sizeof(SceneEntity *)); 
    1371  
    1372         delete [] mGeometry; 
    1373         mGeometry = newGeom; 
    1374  
    1375         // now add new entities 
    1376         SceneEntityContainer::const_iterator it, it_end = entities.end(); 
    1377  
    1378         size_t i = mGeometrySize; 
    1379         for (it = entities.begin(); it != it_end; ++ it, ++ i) 
    1380         { 
    1381                 mGeometry[i] = (*it); 
    1382                 mDynamicEntities.push_back(*it); 
    1383         } 
    1384  
    1385  
    1386         mGeometrySize += entities.size(); 
     1389        if (mDynamicGeometrySize) 
     1390                mDynamicRoot = SubdivideLeaf(l, 0); 
    13871391} 
    13881392 
     
    13941398        // especially annoying is this problem when using the frustum  
    13951399        // fitting on the visible objects for shadow mapping 
    1396         // but don't see how to solve this issue without using much costlier calculations 
     1400        // but don't see how to solve this issue without using costly calculations 
    13971401         
    13981402        // we stored the near plane distance => we can use it also here 
     
    14041408 
    14051409 
    1406 } 
     1410void Bvh::CreateRoot() 
     1411{ 
     1412        // create new root 
     1413        mRoot = new BvhInterior(NULL); 
     1414         
     1415        // the separation is a purely logical one 
     1416        // the bounding boxes of the child nodes are  
     1417        // identical to those of the root node 
     1418 
     1419        mRoot->mBox = mStaticRoot->mBox; 
     1420        mRoot->mBox.Include(mDynamicRoot->mBox); 
     1421 
     1422        mRoot->mArea = mRoot->mBox.SurfaceArea(); 
     1423 
     1424        mRoot->mFirst = 0; 
     1425        mRoot->mLast = (int)mGeometrySize - 1; 
     1426 
     1427        cout<<"f: " << mRoot->mFirst<< " l: " <<mRoot->mLast << endl; 
     1428        // add static root on left subtree 
     1429        mRoot->mFront = mStaticRoot; 
     1430        mStaticRoot->mParent = mRoot; 
     1431 
     1432        // add dynamic root on left subtree 
     1433        mRoot->mBack = mDynamicRoot; 
     1434        mDynamicRoot->mParent = mRoot; 
     1435} 
     1436 
     1437 
     1438} 
Note: See TracChangeset for help on using the changeset viewer.