Advertisement
Guest User

Untitled

a guest
May 27th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. float getValueBilinearClamp( float x, float y ) const
  2. {
  3.     // Clamp negatives to zero (to get correct sub-pixel offset).
  4.     x = max(0.f, x);
  5.     y = max(0.f, y);
  6.  
  7.     // Separate floats into wholes (pixels) and fractions
  8.     const int xi = (int)x;
  9.     const int yi = (int)y;
  10.  
  11.     // Fraction
  12.     const float xFract = (x - xi);
  13.     const float yFract = (y - yi);
  14.  
  15.     // Clamp to max
  16.     const int w = mapWidth - 1;
  17.     const int h = mapHeight - 1;
  18.  
  19.     const int xi0 = min(xi, w);
  20.     const int yi0 = min(yi, h);
  21.     const int xi1 = min(xi + 1, w);
  22.     const int yi1 = min(yi + 1, h);
  23.  
  24.     // Read pixels.
  25.     const float s00 = getValue(xi0, yi0);
  26.     const float s10 = getValue(xi1, yi0);
  27.     const float s01 = getValue(xi0, yi1);
  28.     const float s11 = getValue(xi1, yi1);
  29.  
  30.     // Bilinear filter
  31.     const float bx0 = lerp(xFract, s00, s10);
  32.     const float bx1 = lerp(xFract, s01, s11);
  33.     return lerp(yFract, bx0, bx1);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement