Skip to content

Instantly share code, notes, and snippets.

@dpasca
Created September 26, 2016 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpasca/556f3ac1761b6e62a22a438051b967e8 to your computer and use it in GitHub Desktop.
Save dpasca/556f3ac1761b6e62a22a438051b967e8 to your computer and use it in GitHub Desktop.
bilinear texture sample with clamping
//==================================================================
template <typename _T>
inline _T SampleClamp2D(
const DVec<_T> &samp,
int sampW,
int sampH,
float u,
float v )
{
u = std::max( u, _T(0) ); // only clamp to min.. max comes below
v = std::max( v, _T(0) );
auto ui0 = (float)sampW * u;
auto ui0_int = std::min( (int)ui0, sampW-1 );
auto tu_sub = ui0 - (float)ui0_int;
auto ui1_int = std::min( ui0_int+1, sampW-1 );
auto vi0 = (float)sampH * v;
auto vi0_int = std::min( (int)vi0, sampH-1 );
auto tv_sub = vi0 - (float)vi0_int;
auto vi1_int = std::min( vi0_int+1, sampH-1 );
auto off0 = vi0_int * sampW;
auto off1 = vi1_int * sampW;
auto val_u_v0 = DLerp( samp[ui0_int + off0], samp[ui1_int + off0], tu_sub );
auto val_u_v1 = DLerp( samp[ui0_int + off1], samp[ui1_int + off1], tu_sub );
return DLerp( val_u_v0, val_u_v1, tv_sub );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment