Ignore:
Timestamp:
10/24/08 12:29:30 (16 years ago)
Author:
mattausch
Message:

found bug with near plane intersection: q? why did it work with vbo rendering? (probably because of tighter bounds ...)

File:
1 edited

Legend:

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

    r3064 r3065  
    2727#endif 
    2828 
     29#define INVALID_TEST ((unsigned int)-1) 
     30 
     31using namespace std; 
    2932 
    3033namespace CHCDemoEngine 
    3134{ 
    32  
    33 #define INVALID_TEST ((unsigned int)-1) 
    34  
    35 using namespace std; 
    36  
    3735 
    3836int BvhNode::sCurrentState = 0; 
     
    7674 
    7775static Plane3 sNearPlane; 
     76static float sNear; 
    7877static Frustum sFrustum; 
    7978 
     
    147146 
    148147 
    149 /***********************************************************/ 
    150 /*                class Bvh implementation                 */ 
    151 /***********************************************************/ 
     148/**********************************************************/ 
     149/*                class Bvh implementation                */ 
     150/**********************************************************/ 
    152151 
    153152 
     
    183182 
    184183        if (mRoot) delete mRoot; 
    185  
    186184        // delete vbo 
    187185        glDeleteBuffersARB(1, &mVboId); 
     
    191189void Bvh::Init() 
    192190{ 
    193         mStaticRoot = NULL; 
     191        //mStaticRoot = NULL; 
    194192        mDynamicRoot = NULL; 
    195193        mRoot = NULL; 
     
    202200         
    203201        // nodes are tested using the subnodes from 3 levels below 
    204         mMaxDepthForTestingChildren = 3; 
     202        mMaxDepthForTestingChildren = 0;//3; 
    205203        //mMaxDepthForTestingChildren = 4; 
    206204 
     
    311309                tStack.pop(); 
    312310 
    313                 // found depth => take this node 
     311                // found node in specified depth => take this node 
    314312                if ((d == depth) || (node->IsLeaf())) 
    315313                { 
     
    338336        // do the test only if necessary 
    339337        if (!(node->mPlaneMask[BvhNode::sCurrentState] & (1 << i))) 
     338        { 
    340339                return true; 
    341                          
     340        } 
     341 
     342 
    342343        //////// 
    343344        //-- test the n-vertex 
     
    375376 
    376377        //////// 
    377         //-- apply frustum culling for the planes [mPreferredPlane - 5] 
     378        //-- apply frustum culling for the planes with index mPreferredPlane to 6 
    378379 
    379380        for (int i = node->mPreferredPlane[BvhNode::sCurrentState]; i < 6; ++ i) 
     
    382383 
    383384        ////////// 
    384         //-- do the view frustum culling for the planes [0 - m_iPreferredPlane) 
     385        //-- apply frustum culling for the planes with index 0 to mPreferredPlane 
    385386 
    386387        for (int i = 0; i < node->mPreferredPlane[BvhNode::sCurrentState]; ++ i) 
     
    413414        // store near plane 
    414415        sNearPlane = Plane3(cam->GetDirection(), cam->GetPosition()); 
     416        sNear = cam->GetNear(); 
    415417 
    416418        // rebuild dynamic part of the hierarchy 
     
    424426void Bvh::UpdateDistance(BvhNode *node) const 
    425427{ 
    426         // use distance to center rather than the distance to the near plane because 
    427         // otherwise problems with big object penetrating the near plane  
     428        // q: should we use distance to center rather than the distance to the near plane? 
     429         
     430        // because otherwise problems with big object penetrating the near plane  
    428431        // (e.g., big objects in the distance which are then always visible) 
    429432        // especially annoying is this problem when using the frustum  
    430433        // fitting on the visible objects for shadow mapping 
    431  
    432         //node->mDistance = node->GetBox()GetMinDistance(sNearPlane); 
     434        // on the other hand, distance to near plane can also be used  
     435        // for near plane intersection 
    433436        node->mDistance = sNearPlane.Distance(node->GetBox().Center()); 
     437        //node->mDistance = node->GetBox().GetMinDistance(sNearPlane); 
    434438} 
    435439 
     
    458462void Bvh::RenderBounds(BvhNode *node, RenderState *state, bool useTightBounds) 
    459463{ 
    460         // if not using tight bounds, rendering boxes in immediate mode 
    461         // is preferable to vertex arrays (less setup time) 
    462         if (!useTightBounds) 
    463         { 
    464                 RenderBoundingBoxImmediate(node->GetBox()); 
    465         } 
    466         else 
    467         { 
    468                 // hack: use dummy wrapper in order to use function 
    469                 static BvhNodeContainer dummy(1); 
    470                 dummy[0] = node; 
    471                 RenderBounds(dummy, state, useTightBounds); 
    472         } 
     464        // hack: use dummy contayiner as wrapper in order to use multibox function 
     465        static BvhNodeContainer dummy(1); 
     466        dummy[0] = node; 
     467        RenderBounds(dummy, state, useTightBounds); 
    473468} 
    474469 
     
    490485                        RenderBoundingBoxImmediate((*nit)->GetBox()); 
    491486                } 
    492 cout<<"y"; 
     487 
    493488                renderedBoxes = (int)nodes.size(); 
    494489        } 
    495490        else 
    496         {cout<<"x"; 
     491        { 
    497492                renderedBoxes = PrepareBoundsWithDrawArrays(nodes); 
    498493                RenderBoundsWithDrawArrays(renderedBoxes, state); 
     
    529524        { 
    530525                BvhNode *node = *nit; 
    531  
    532526        const int numIndices = node->mNumTestNodes * sNumIndicesPerBox; 
    533527                 
     
    569563        // collect bvh nodes 
    570564        BvhNodeContainer nodes; 
    571         CollectNodes(mStaticRoot, nodes); 
     565        CollectNodes(mRoot, nodes); 
    572566 
    573567        cout << "creating new indices" << endl; 
     
    587581        } 
    588582 
    589  
    590583        cout << "creating global indices buffer" << endl; 
    591584 
     
    607600                // resize array 
    608601                node->mIndicesPtr = mCurrentIndicesPtr; 
    609  
    610602                int numIndices = 0; 
    611603 
    612                 // the bounding box of the test nodes are rendered instead of the root node 
     604                // the bounding boxes of the test nodes are rendered instead of the node itself 
    613605                // => store their indices 
    614606                for (int i = 0; i < node->mNumTestNodes; ++ i, numIndices += sNumIndicesPerBox) 
     
    657649 
    658650        nodes.reserve(GetNumStaticNodes()); 
    659         CollectNodes(mStaticRoot, nodes); 
     651        CollectNodes(mRoot, nodes); 
    660652 
    661653        const unsigned int bufferSize = 8 * (int)nodes.size(); 
     
    717709        // collect all nodes 
    718710        BvhNodeContainer nodes; 
    719         CollectNodes(mStaticRoot, nodes); 
     711        CollectNodes(mRoot, nodes); 
    720712 
    721713        cout << "recomputing bounds, children will be tested in depth " << mMaxDepthForTestingChildren << endl; 
     
    930922        // first invalidate old virtual leaves 
    931923        BvhNodeContainer leaves; 
    932         CollectVirtualLeaves(mStaticRoot, leaves); 
     924        CollectVirtualLeaves(mRoot, leaves); 
    933925 
    934926        BvhNodeContainer::const_iterator bit, bit_end = leaves.end(); 
     
    998990 
    999991        std::stack<BvhNode *> nodeStack; 
    1000         nodeStack.push(mStaticRoot); 
     992        nodeStack.push(mRoot); 
    1001993 
    1002994        while (!nodeStack.empty())  
     
    10391031        const Vector3 u = box.Max(); 
    10401032 
     1033        /////////// 
     1034        //-- render AABB as triangle strips 
     1035 
    10411036        glBegin(GL_TRIANGLE_STRIP); 
    1042         { 
    1043                 /////////// 
    1044                 //-- render AABB as triangle strips 
    1045  
    1046                 // render first half of AABB 
    1047                 glVertex3f(l.x, l.y, u.z); 
    1048                 glVertex3f(u.x, l.y, u.z); 
    1049                 glVertex3f(l.x, u.y, u.z); 
    1050                 glVertex3f(u.x, u.y, u.z); 
    1051                 glVertex3f(l.x, u.y, l.z);  
    1052                 glVertex3f(u.x, u.y, l.z); 
    1053                 glVertex3f(l.x, l.y, l.z);  
    1054                 glVertex3f(u.x, l.y, l.z); 
    1055  
    1056                 glPrimitiveRestartNV(); 
    1057  
    1058                 // render second half of AABB 
    1059                 glVertex3f(l.x, u.y, u.z);  
    1060                 glVertex3f(l.x, u.y, l.z); 
    1061                 glVertex3f(l.x, l.y, u.z); 
    1062                 glVertex3f(l.x, l.y, l.z); 
    1063                 glVertex3f(u.x, l.y, u.z);  
    1064                 glVertex3f(u.x, l.y, l.z); 
    1065                 glVertex3f(u.x, u.y, u.z);  
    1066                 glVertex3f(u.x, u.y, l.z); 
    1067         } 
     1037 
     1038        // render first half of AABB 
     1039        glVertex3f(l.x, l.y, u.z); 
     1040        glVertex3f(u.x, l.y, u.z); 
     1041        glVertex3f(l.x, u.y, u.z); 
     1042        glVertex3f(u.x, u.y, u.z); 
     1043        glVertex3f(l.x, u.y, l.z);  
     1044        glVertex3f(u.x, u.y, l.z); 
     1045        glVertex3f(l.x, l.y, l.z);  
     1046        glVertex3f(u.x, l.y, l.z); 
     1047 
     1048        glPrimitiveRestartNV(); 
     1049 
     1050        // render second half of AABB 
     1051        glVertex3f(l.x, u.y, u.z);  
     1052        glVertex3f(l.x, u.y, l.z); 
     1053        glVertex3f(l.x, l.y, u.z); 
     1054        glVertex3f(l.x, l.y, l.z); 
     1055        glVertex3f(u.x, l.y, u.z);  
     1056        glVertex3f(u.x, l.y, l.z); 
     1057        glVertex3f(u.x, u.y, u.z);  
     1058        glVertex3f(u.x, u.y, l.z); 
     1059 
    10681060        glEnd(); 
    10691061} 
    1070  
    1071  
    10721062 
    10731063 
     
    11231113                                                         bool useTightBounds) 
    11241114{ 
    1125         glDisable(GL_TEXTURE_2D); 
    1126         glDisable(GL_LIGHTING); 
    11271115        glColor3f(1, 1, 1); 
    11281116 
    11291117        // hack: for deferred shading we have to define a material 
    11301118        static Technique boxMat; 
     1119        boxMat.SetLightingEnabled(false); 
     1120 
    11311121        boxMat.SetEmmisive(RgbaColor(1.0f, 1.0f, 1.0f, 1.0f)); 
    11321122 
     
    11351125        if (!useTightBounds) 
    11361126        { 
    1137                 RenderBoxForViz(node->GetBox()); 
     1127                //RenderBoxForViz(node->GetBox()); 
     1128                glPolygonMode(GL_FRONT, GL_LINE); 
     1129                RenderBoundingBoxImmediate(node->GetBox()); 
     1130                glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); 
    11381131        } 
    11391132        else 
     
    11441137                } 
    11451138        } 
    1146  
    1147         glEnable(GL_LIGHTING); 
    1148         glEnable(GL_TEXTURE_2D); 
    11491139} 
    11501140 
     
    12971287 
    12981288 
    1299 } 
     1289bool Bvh::IntersectsNearPlane(BvhNode *node) const 
     1290{  
     1291        float distanceToNearPlane = node->GetBox().GetMinDistance(sNearPlane); 
     1292        // we stored the near plane distance => we can use it also here 
     1293        //float distanceToNearPlane = node->GetDistance(); 
     1294         
     1295        return distanceToNearPlane < sNear; 
     1296} 
     1297 
     1298 
     1299} 
Note: See TracChangeset for help on using the changeset viewer.