Changeset 3072 for GTP


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

fucked up depth order for some reason!!

Location:
GTP/trunk/App/Demos/Vis/FriendlyCulling/src
Files:
8 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} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.h

    r3070 r3072  
    137137        */ 
    138138        inline void SetId(int id); 
    139  
     139         
    140140 
    141141        ////////// 
     
    648648        void RenderBoundsForViz(BvhNode *node, RenderState *state, bool useTightBounds); 
    649649 
    650         void AddDynamicObjects(const SceneEntityContainer &entities); 
    651  
    652  
    653650 
    654651protected: 
     
    660657        Bvh(); 
    661658        /** Protected constructor taking scene geometry into account 
    662         */ 
    663         const Bvh(const SceneEntityContainer &entities); 
     659                Sets the static and dynamic objects for the hierarchy. 
     660        */ 
     661        const Bvh(const SceneEntityContainer &staticEntities,  
     662                      const SceneEntityContainer &dynamicEntities); 
    664663        /** Called by the constructor. Initializes important members. 
    665664        */ 
     
    740739        inline int GetNumStaticNodes() const { return mNumNodes; } 
    741740 
     741        void ComputeMaxDepthForVirtualLeaves(); 
     742 
     743        void CreateRoot(); 
     744 
     745 
    742746        //////////////////////// 
    743747         
     
    745749        AxisAlignedBox3 mBox; 
    746750        /// the root of the hierarchy 
    747         BvhNode *mRoot; 
     751        BvhInterior *mRoot; 
    748752        /// the root of static part of the the hierarchy 
    749         //BvhNode *mStaticRoot; 
     753        BvhNode *mStaticRoot; 
    750754        /// the root of dynamic part of the the hierarchy 
    751755        BvhNode *mDynamicRoot; 
     
    754758        /// #of entities 
    755759        size_t mGeometrySize; 
     760        /// #of static entities 
     761        size_t mStaticGeometrySize; 
     762        /// #of dynamic entities 
     763        size_t mDynamicGeometrySize; 
    756764 
    757765 
     
    799807        int mMaxDepthForDynamicBranch; 
    800808 
    801         SceneEntityContainer mDynamicEntities; 
     809        //SceneEntityContainer mDynamicEntities; 
    802810}; 
    803811 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp

    r3070 r3072  
    6363 
    6464Bvh *BvhLoader::Load(const string &filename,   
    65                                          const SceneEntityContainer &entities) 
     65                                         const SceneEntityContainer &staticEntities, 
     66                                         const SceneEntityContainer &dynamicEntities) 
    6667{ 
    6768        queue<BvhNode *> tQueue; 
     
    7273        cout << "loading bvh" << endl; 
    7374 
    74         Bvh *bvh = new Bvh(entities); 
     75        Bvh *bvh = new Bvh(staticEntities, dynamicEntities); 
    7576 
    7677        BvhNode *root = LoadNextNode(stream, NULL); 
    7778 
    78         bvh->mRoot = root; 
    79         //bvh->mRoot = bvh->mStaticRoot; 
    80         bvh->mNumNodes = 1; 
     79        bvh->mStaticRoot = root; 
     80        bvh->mNumNodes = 3; 
    8181 
    82         tQueue.push(bvh->mRoot); 
     82        tQueue.push(root); 
    8383         
    8484        while(!tQueue.empty()) 
     
    108108 
    109109 
    110         bvh->mBox = bvh->mRoot->GetBox(); 
     110        //////////// 
     111        //-- create dynamic part of the hierarchy 
    111112 
    112         cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl; 
    113         cout << "scene box: " << bvh->mBox << endl; 
     113        bvh->CreateDynamicBranch(); 
    114114 
    115115         
     
    119119        /// this function must be called once after creation 
    120120        bvh->PostProcess(); 
    121          
    122         // set virtual leaves for specified number of triangles 
    123         bvh->SetVirtualLeaves(INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES); 
    124         /// update the numleaves parameter of the bvh nodes 
    125         bvh->UpdateNumLeaves(bvh->mRoot); 
    126         // compute unique ids 
    127         bvh->ComputeIds(); 
    128         // specify bounds for occlusion tests 
    129         bvh->RecomputeBounds(); 
    130121 
    131         // compute and print stats 
    132         bvh->ComputeBvhStats(bvh->mRoot, bvh->mBvhStats); 
    133         bvh->PrintBvhStats(bvh->mBvhStats); 
     122 
     123        cout << "... finished loading " << bvh->mNumNodes << " nodes" << endl; 
     124        cout << "scene box: " << bvh->mBox << endl; 
    134125 
    135126        return bvh; 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.h

    r2795 r3072  
    1717{ 
    1818public: 
    19  
    20         Bvh *Load(const std::string &filename, const SceneEntityContainer &entities); 
     19        /** Creates and loads bvh with static and dynamic objects. 
     20                The static part of the hierarchy is loaded from the disc, 
     21                the dynamic part is created and changed on the fly. 
     22        */ 
     23        Bvh *Load(const std::string &filename,  
     24                          const SceneEntityContainer &staticEntities, 
     25                          const SceneEntityContainer &dynamicEntities); 
    2126 
    2227protected: 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/RenderTraverser.cpp

    r3071 r3072  
    157157        // add root node to queue 
    158158        EnqueueNode(mBvh->GetRoot()); 
    159         // add dynamic root 
    160         if (mBvh->GetDynamicRoot()) 
    161                 EnqueueNode(mBvh->GetDynamicRoot()); 
    162  
     159         
    163160 
    164161        /////////// 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.cpp

    r3071 r3072  
    55#include "SceneEntity.h" 
    66#include "Transform3.h" 
     7#include "glInterface.h" 
     8 
    79 
    810 
     
    2628        if (mMaterial)  
    2729                mMaterial->Render(state); 
    28  
     30        //mDepthWriteEnabled = true; 
     31glDepthMask(GL_TRUE);   glDepthFunc(GL_LESS); 
     32glEnable(GL_DEPTH_TEST); 
    2933        mGeometry->Render(state); 
    3034} 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Shape.h

    r3071 r3072  
    22#define __SHAPE_H 
    33 
    4 #include "glInterface.h" 
    5 #include <Cg/cg.h> 
    6 #include <Cg/cgGL.h> 
    74#include "common.h" 
    85#include "Vector3.h" 
  • GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp

    r3071 r3072  
    184184 
    185185bool useOptimization = false; 
    186 bool useTightBounds = true; 
    187 //bool useTightBounds = false; 
     186//bool useTightBounds = true; 
     187bool useTightBounds = false; 
    188188bool useRenderQueue = true; 
    189189bool useMultiQueries = true; 
     
    453453        LoadModel("city.dem", sceneEntities); 
    454454 
    455  
     455/* 
     456        ////////// 
     457        //-- load some dynamic stuff 
     458 
     459        LoadModel("hbuddha.dem", dynamicObjects); 
     460        buddha = dynamicObjects.back(); 
     461         
     462        const Vector3 sceneCenter(470.398f, 240.364f, 182.5f); 
     463        Matrix4x4 transl = TranslationMatrix(sceneCenter); 
     464        buddha->GetTransform()->SetMatrix(transl); 
     465 
     466        for (int i = 0; i < 10; ++ i) 
     467        { 
     468                SceneEntity *ent = new SceneEntity(*buddha); 
     469                resourceManager->AddSceneEntity(ent); 
     470 
     471                Vector3 offs = Vector3::ZERO(); 
     472 
     473                offs.x = RandomValue(.0f, 150.0f); 
     474                offs.y = RandomValue(.0f, 150.0f); 
     475 
     476                transl = TranslationMatrix(sceneCenter + offs); 
     477                Transform3 *transform = resourceManager->CreateTransform(transl); 
     478 
     479                ent->SetTransform(transform); 
     480                dynamicObjects.push_back(ent); 
     481        } 
     482 
     483*/ 
    456484        /////////// 
    457485        //-- load the associated static bvh 
    458486 
    459487        const string bvh_filename = string(model_path + "city.bvh"); 
     488 
    460489        BvhLoader bvhLoader; 
    461         bvh = bvhLoader.Load(bvh_filename, sceneEntities); 
     490        bvh = bvhLoader.Load(bvh_filename, sceneEntities, dynamicObjects); 
    462491 
    463492        if (!bvh) 
     
    494523        // the bird-eye visualization 
    495524        visualization = new Visualization(bvh, camera, NULL, &state); 
    496  
    497         LoadModel("hbuddha.dem", dynamicObjects); 
    498         buddha = dynamicObjects.back(); 
    499          
    500         const Vector3 sceneCenter(470.398f, 240.364f, 182.5f); 
    501         Matrix4x4 transl = TranslationMatrix(sceneCenter); 
    502         buddha->GetTransform()->SetMatrix(transl); 
    503  
    504         for (int i = 0; i < 10; ++ i) 
    505         { 
    506                 SceneEntity *ent = new SceneEntity(*buddha); 
    507                 resourceManager->AddSceneEntity(ent); 
    508  
    509                 Vector3 offs = Vector3::ZERO(); 
    510  
    511                 offs.x = RandomValue(.0f, 150.0f); 
    512                 offs.y = RandomValue(.0f, 150.0f); 
    513  
    514                 transl = TranslationMatrix(sceneCenter + offs); 
    515                 Transform3 *transform = resourceManager->CreateTransform(transl); 
    516  
    517                 ent->SetTransform(transform); 
    518                 dynamicObjects.push_back(ent); 
    519         } 
    520          
    521         bvh->AddDynamicObjects(dynamicObjects); 
    522525 
    523526        // this function assign the render queue bucket ids of the materials in beforehand 
     
    10031006        } 
    10041007        else 
    1005         { 
     1008        {glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    10061009                // actually render the scene geometry using the specified algorithm 
    10071010                traverser->RenderScene(); 
     1011                //for (int i = 0; i < sceneEntities.size(); ++ i) 
     1012                //      sceneEntities[i]->Render(&state); 
    10081013        } 
    10091014 
Note: See TracChangeset for help on using the changeset viewer.