Advertisement
Guest User

havok broadphase culling not accurate

a guest
Mar 30th, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Make note of what scenery falls outside of our view frustum.
  2. void HK_CALL cull_scenery( hkpWorld* world,
  3.     tbb::concurrent_vector<Physics_Scenery*> vec_scenery,
  4.     std::vector<hkUint32>* ids_in_frustum ){
  5.  
  6.         Camera_Alt const* pcam = World_Manager::GetPlayereCamera();
  7.         glm::vec3 look_from = World_Manager::player->get_lookFrom();
  8.         glm::vec3 look_to = World_Manager::player->get_lookTo();
  9.         hkVector4 transFrom(look_from.x,look_from.y,look_from.z,0);
  10.         hkVector4 transTo(look_to.x,look_to.y,look_to.z,0);
  11.  
  12.         // Lock the world and get the tree broad-phase.
  13.         world->lock();
  14.         hkpTreeBroadPhase*  broadphase = static_cast<hkpTreeBroadPhase*>(world->getBroadPhase());
  15.         //broadphase->fullOptimize();
  16.  
  17.  
  18.         glm::vec3 f_r = -pcam->getXAxis();
  19.         glm::vec3 f_u = -pcam->getYAxis();
  20.         glm::vec3 f_f = -pcam->getZAxis();
  21.  
  22.         // organize our axes...................................
  23.         float sc = 50.0f;
  24.         hkVector4 start_fu( f_u.x, f_u.y, f_u.z, 1 );
  25.         hkVector4 end_fu( f_u.x*sc, f_u.y*sc, f_u.z*sc, 1 );
  26.         hkVector4 start_fr( f_r.x, f_r.y, f_r.z, 1 );
  27.         hkVector4 end_fr( f_r.x*sc, f_r.y*sc, f_r.z*sc, 1 );
  28.         hkVector4 start_ff( f_f.x, f_f.y, f_f.z, 1 );
  29.         hkVector4 end_ff( f_f.x*sc, f_f.y*sc, f_f.z*sc, 1 );
  30.         start_fu.add(transFrom);
  31.         end_fu.add(transFrom);
  32.         start_fr.add(transFrom);
  33.         end_fr.add(transFrom);
  34.         start_ff.add(transFrom);
  35.         end_ff.add(transFrom);
  36.         //.....................................................
  37.         if( VISUALIZE_AXES ){
  38.             // Visualize the axes in the vdb........................
  39.             HK_DISPLAY_LINE( start_fu, end_fu ,hkColor::RED);
  40.             HK_DISPLAY_LINE( start_fr, end_fr ,hkColor::AZURE);
  41.             HK_DISPLAY_LINE( start_ff, end_ff ,hkColor::GREEN);
  42.         }
  43.         //........................................................
  44.  
  45.         // Create frustum vertices.
  46.         hkVector4   nearPlaneQuad[4];
  47.  
  48.         // Find corners according to our direction.
  49.         float s1=10.0f;
  50.         glm::vec3 bl( f_r*-s1 + f_u*-s1 );
  51.         glm::vec3 br( f_r*s1 + f_u*-s1 );
  52.         glm::vec3 tr( f_r*s1 + f_u*s1 );
  53.         glm::vec3 tl( f_r*-s1 + f_u*s1 );
  54.  
  55.         nearPlaneQuad[0].set( bl.x, bl.y, bl.z );
  56.         nearPlaneQuad[1].set( br.x, br.y, br.z );
  57.         nearPlaneQuad[2].set( tr.x, tr.y, tr.z );
  58.         nearPlaneQuad[3].set( tl.x, tl.y, tl.z );
  59.  
  60.         hkVector4   farPlaneQuad[4];
  61.         hkSimdReal  fovFactor = pcam->getFov() * 6.5f;
  62.         hkReal      farOffset = pcam->getZFar();
  63.         for(int i=0; i<4; ++i)
  64.         {
  65.             farPlaneQuad[i].setMul4(fovFactor, nearPlaneQuad[i]);
  66.  
  67.             // Translate frustum.
  68.             nearPlaneQuad[i].add( transFrom );
  69.             farPlaneQuad[i].add( transTo );
  70.         }
  71.  
  72.  
  73.  
  74.         // Draw frustum.
  75.         if( VISUALIZE_FRUSTUM ){
  76.             HK_DISPLAY_LINE(nearPlaneQuad[0], nearPlaneQuad[1], hkColor::YELLOW);
  77.             HK_DISPLAY_LINE(nearPlaneQuad[1], nearPlaneQuad[2], hkColor::YELLOW);
  78.             HK_DISPLAY_LINE(nearPlaneQuad[2], nearPlaneQuad[3], hkColor::YELLOW);
  79.             HK_DISPLAY_LINE(nearPlaneQuad[3], nearPlaneQuad[0], hkColor::YELLOW);
  80.  
  81.             HK_DISPLAY_LINE(farPlaneQuad[0], farPlaneQuad[1], hkColor::YELLOW);
  82.             HK_DISPLAY_LINE(farPlaneQuad[1], farPlaneQuad[2], hkColor::YELLOW);
  83.             HK_DISPLAY_LINE(farPlaneQuad[2], farPlaneQuad[3], hkColor::YELLOW);
  84.             HK_DISPLAY_LINE(farPlaneQuad[3], farPlaneQuad[0], hkColor::YELLOW);
  85.  
  86.             HK_DISPLAY_LINE(farPlaneQuad[0], nearPlaneQuad[0], hkColor::YELLOW);
  87.             HK_DISPLAY_LINE(farPlaneQuad[1], nearPlaneQuad[1], hkColor::YELLOW);
  88.             HK_DISPLAY_LINE(farPlaneQuad[2], nearPlaneQuad[2], hkColor::YELLOW);
  89.             HK_DISPLAY_LINE(farPlaneQuad[3], nearPlaneQuad[3], hkColor::YELLOW);
  90.         }
  91.  
  92.         // Compute frustum planes.
  93.         hkVector4   planes[6];     
  94.  
  95.         // Side planes
  96.         planes[0] = computePlaneFromTriangle(nearPlaneQuad[0], farPlaneQuad[0], nearPlaneQuad[1]);
  97.         planes[1] = computePlaneFromTriangle(nearPlaneQuad[1], farPlaneQuad[1], nearPlaneQuad[2]);
  98.         planes[2] = computePlaneFromTriangle(nearPlaneQuad[2], farPlaneQuad[2], nearPlaneQuad[3]);
  99.         planes[3] = computePlaneFromTriangle(nearPlaneQuad[3], farPlaneQuad[3], nearPlaneQuad[0]);
  100.  
  101.         // Near plane
  102.         planes[4] = computePlaneFromTriangle(nearPlaneQuad[0], nearPlaneQuad[1], nearPlaneQuad[2]);    
  103.  
  104.         // Far plane
  105.         planes[5] = computePlaneFromTriangle(farPlaneQuad[0], farPlaneQuad[2], farPlaneQuad[1]);
  106.  
  107.  
  108.         broadphase->lock();
  109.         // Cull using frustum.
  110.         hkArray<const hkpBroadPhaseHandle*> handles;
  111.         {
  112.             HK_TIME_CODE_BLOCK("Culling",this);
  113.             handles.reserve(4096 / sizeof(const hkpBroadPhaseHandle*));
  114.             broadphase->queryConvex(planes, 6, handles);
  115.         }
  116.  
  117.         // Get out objects visible from the frustum.
  118.         for(int i=0; i<handles.getSize(); ++i)
  119.         {
  120.             ids_in_frustum->push_back( handles[i]->m_id );
  121.         }
  122.  
  123.         broadphase->unlock();
  124.         world->unlock();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement