Advertisement
Guest User

Untitled

a guest
Dec 13th, 2024
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. std::vector<CR_VECTOR4> CMathHelper::CreateOccluderPlanes(const CR_VECTOR3 &pCameraPos, const CR_VECTOR3 *pCorners, const CR_VECTOR3 &pCenter, std::vector<CR_VECTOR3> &pPlanesSigns, std::vector<CR_VECTOR3> &pDebugPoints)
  2. {
  3.     assert(pCorners != nullptr);
  4.     pPlanesSigns.clear();
  5.    
  6.     std::vector<int> cornerIds = { 0, 1, 5, 4, 4, 5, 7, 6, 6, 7, 3, 2, 2, 3, 1, 0, 1, 3, 7, 5 , 0, 4, 6, 2 };   // 6*4 = 24
  7.     std::set<CR_VECTOR2I> visibleEdges;
  8.  
  9.     std::vector<CR_VECTOR4> planes;
  10.  
  11.     // find quads facing the camera (and use for 'near' planes) - planar, so 3 verts per quad is enough
  12.     for(size_t verts=0;verts<cornerIds.size();verts+=4)
  13.     {
  14.         CR_VECTOR3 faceNormal;
  15.         if(TriangleFacingPoint(pCorners[cornerIds[verts+1]], pCorners[cornerIds[verts+2]], pCorners[cornerIds[verts+3]], pCameraPos, faceNormal))
  16.         {
  17.             faceNormal *= -1.0f;    // flip normal, as it was pointing towards camera
  18.             faceNormal.Normalize();
  19.  
  20.             float d = -DotProductVec3(pCorners[cornerIds[verts+1]], faceNormal);
  21.             planes.emplace_back(CR_VECTOR4(faceNormal.x, faceNormal.y, faceNormal.z, d));
  22.             pPlanesSigns.emplace_back(GetSigns(faceNormal));
  23.            
  24.             std::vector<CR_VECTOR2I> newEdges;
  25.             newEdges.emplace_back(CR_VECTOR2I(cornerIds[verts], cornerIds[verts+1]));
  26.             newEdges.emplace_back(CR_VECTOR2I(cornerIds[verts+1], cornerIds[verts+2]));
  27.             newEdges.emplace_back(CR_VECTOR2I(cornerIds[verts+2], cornerIds[verts+3]));
  28.             newEdges.emplace_back(CR_VECTOR2I(cornerIds[verts+3], cornerIds[verts]));
  29.    
  30.             for(auto && edge : newEdges)
  31.             {
  32.                 edge.LowToHigh();
  33.                 auto it = visibleEdges.find(edge);
  34.                 if(it != visibleEdges.end())
  35.                 {
  36.                     visibleEdges.erase(it);
  37.                 }
  38.                 else
  39.                 {
  40.                     visibleEdges.insert(edge);
  41.                 }
  42.             }
  43.         }
  44.     }
  45.    
  46.     // continue with 'sides' of the volume, based on the edges with duplicates
  47.     for(auto && tEdge : visibleEdges)
  48.     {  
  49.         CR_VECTOR3 faceNormal = CalculateFaceNormal(pCameraPos, pCorners[tEdge.x], pCorners[tEdge.y]);
  50.  
  51.         if(!TriangleFacingPoint(faceNormal, pCameraPos, pCenter))
  52.         {
  53.             faceNormal *= -1.0f;
  54.         }
  55.         faceNormal.Normalize();
  56.  
  57.         float d = -DotProductVec3(pCameraPos, faceNormal);
  58.         planes.emplace_back(CR_VECTOR4(faceNormal.x, faceNormal.y, faceNormal.z, d));
  59.         pPlanesSigns.emplace_back(GetSigns(faceNormal));
  60.     }
  61.     return planes;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement