Guest User

Untitled

a guest
Feb 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. float ComputeScreenSize( const Vector &vecOrigin, float flRadius, const ScreenSizeComputeInfo_t& info )
  2. {
  3.     // This is sort of faked, but it's faster that way
  4.     // FIXME: Also, there's a much faster way to do this with similar triangles
  5.     // but I want to make sure it exactly matches the current matrices, so
  6.     // for now, I do it this conservative way
  7.     /*
  8.     Vector4D testPoint1, testPoint2;
  9.     VectorMA( vecOrigin, flRadius, info.m_vecViewUp, testPoint1.AsVector3D() );
  10.     VectorMA( vecOrigin, -flRadius, info.m_vecViewUp, testPoint2.AsVector3D() );
  11.     testPoint1.w = testPoint2.w = 1.0f;
  12.  
  13.     Vector4D clipPos1, clipPos2;
  14.     Vector4DMultiply( info.m_matViewProj, testPoint1, clipPos1 );
  15.     Vector4DMultiply( info.m_matViewProj, testPoint2, clipPos2 );
  16.     if (clipPos1.w >= 0.001f)
  17.     {
  18.         clipPos1.y /= clipPos1.w;
  19.     }
  20.     else
  21.     {
  22.         clipPos1.y *= 1000;
  23.     }
  24.     if (clipPos2.w >= 0.001f)
  25.     {
  26.         clipPos2.y /= clipPos2.w;
  27.     }
  28.     else
  29.     {
  30.         clipPos2.y *= 1000;
  31.     }
  32.  
  33.     // The divide-by-two here is because y goes from -1 to 1 in projection space
  34.     return info.m_nViewportHeight * fabs( clipPos2.y - clipPos1.y ) * 0.5f;
  35.     */
  36.  
  37.     // NOTE: Optimized version of the above algorithm, which only uses y and w components of clip
  38.     // Can also optimize based on clipPos = a +/- b * r
  39.     const float *pViewProjY = info.m_matViewProj[1];
  40.     const float *pViewProjW = info.m_matViewProj[3];
  41.     float flODotY       = pViewProjY[0] * vecOrigin.x           + pViewProjY[1] * vecOrigin.y           + pViewProjY[2] * vecOrigin.z           + pViewProjY[3];
  42.     float flViewDotY    = pViewProjY[0] * info.m_vecViewUp.x    + pViewProjY[1] * info.m_vecViewUp.y    + pViewProjY[2] * info.m_vecViewUp.z;  
  43.     flViewDotY          *= flRadius;
  44.     float flODotW       = pViewProjW[0] * vecOrigin.x           + pViewProjW[1] * vecOrigin.y           + pViewProjW[2] * vecOrigin.z           + pViewProjW[3];
  45.     float flViewDotW    = pViewProjW[0] * info.m_vecViewUp.x    + pViewProjW[1] * info.m_vecViewUp.y    + pViewProjW[2] * info.m_vecViewUp.z;
  46.     flViewDotW          *= flRadius;
  47.     float y0            = flODotY + flViewDotY;
  48.     float w0            = flODotW + flViewDotW;
  49.     y0                  *= ( w0 >= 0.001f ) ? ( 1.0f / w0 ) : 1000.0f;
  50.     float y1            = flODotY - flViewDotY;
  51.     float w1            = flODotW - flViewDotW;
  52.     y1                  *= ( w1 >= 0.001f ) ? ( 1.0f / w1 ) : 1000.0f;
  53.  
  54.     // The divide-by-two here is because y goes from -1 to 1 in projection space
  55.     return info.m_nViewportHeight * fabs( y1 - y0 ) * 0.5f;
  56. }
Add Comment
Please, Sign In to add comment