Advertisement
FocusedWolf

IsPointInFrustum

Jul 3rd, 2011
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.98 KB | None | 0 0
  1. local nearD = 0 //the real distance of nearD bounces around 3e-7. Can use 0 here
  2. local farD = 100000000000 //the real distance of farD is like 1e12. Can use a preferred distance.
  3. function IsPointInFrustum(point)
  4.     //http://www.ijvr.org/issues/issue3-2008/7.pdf
  5.     //http://www.lighthouse3d.com/tutorials/view-frustum-culling/radar-approach-testing-points/
  6.     //http://www.lighthouse3d.com/tutorials/view-frustum-culling/radar-approach-implementation/
  7.     //game programming gems 5 under the title "Improved Frustum Culling"
  8.    
  9.     local player = Client.GetLocalPlayer()
  10.  
  11.  //todo: can be moved out of function so it's not re-calculated for each point tested
  12.     //local cameraCoords = player:GetCameraViewCoords() //more precise...more costly
  13.     //local cameraOrigin = cameraCoords.origin
  14.     local cameraCoords = player:GetViewAngles():GetCoords()    
  15.     local cameraOrigin = player:GetEyePos()
  16.      
  17.  //todo: these only needs to be calcuated during resolution change
  18.     local rFactor = math.tan(player:GetRenderFov() / 2)
  19.     local uFactor = rFactor / GetAspectRatio() //this approach came from game programming gems 5
  20.  
  21.     local v = point - cameraOrigin
  22.  
  23.     // compute and test the Z coordinate
  24.     local P_z = Math.DotProduct(v, cameraCoords.zAxis)
  25.     if P_z < nearD or farD < P_z then
  26.         return false //behind near-plane or in front of far-plane
  27.     end
  28.  
  29.     // compute and test the X coordinate
  30.     local P_x = Math.DotProduct(v, cameraCoords.xAxis)
  31.     local limit = rFactor * P_z //half width of frustum at distance P_z from user
  32.     if P_x < -limit or limit < P_x then
  33.         return false //left of left-plane or right of right-plane
  34.     end
  35.  
  36.     // compute and test the Y coordinate
  37.     local P_y = Math.DotProduct(v, cameraCoords.yAxis)
  38.     limit = uFactor * P_z //half height of frustum at distance P_z from user
  39.     if P_y < -limit or limit < P_y then
  40.         return false //above top-plane or below bottom-plane
  41.     end
  42.  
  43.     return true //is visible
  44. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement