What algorithm does GL_LINEAR use?
in 2D-textures used sample 4, which means bilinear interpolation , however 3x linear interpolation . Weight is the normalized distance of the target texel to its 4 neighbors.
So, for example, you want texel on
(s,t)=(0.21,0.32)
but the texture of nearby texels has coordinates:
(s0,t0)=(0.20,0.30) (s0,t1)=(0.20,0.35) (s1,t0)=(0.25,0.30) (s1,t1)=(0.25,0.35)
weight:
ws = (s-s0)/(s1-s0) = 0.2
wt = (t-t0)/(t1-t0) = 0.4
therefore linear interpolation texts in the direction s
c0 = texture(s0,t0) + (texture(s1,t0)-texture(s0,t0))*ws
c1 = texture(s0,t1) + (texture(s1,t1)-texture(s0,t1))*ws
and finally in the direction t
:
c = c0 + (c1-c0)*wt
where texture(s,t)
returns the texel color in s,t
, while the coordinate corresponds to the current texel, and c
is the final interpolated texel color.
In reality, the coordinates s,t
are multiplied by the texture resolution ( xs,ys
), which converts them to texel units. after that s-s0
and are t-t0
already normalized, so there is no need to divide by s1-s0
and t1-t0
, since they are stand equal to one. So:
s=s*xs; s0=floor(s); s1=s0+1; ws=s-s0;
t=t*ys; t0=floor(t); t1=t0+1; wt=t-t0;
c0 = texture(s0,t0) + (texture(s1,t0)-texture(s0,t0))*ws;
c1 = texture(s0,t1) + (texture(s1,t1)-texture(s0,t1))*ws;
c = c0 + (c1-c0)*wt;
I have never used 3D textures before , but in this case it uses 8 text elements and is called tri-linear interpolation which is 2x bilinear interpolation just take 2 nearest textures and calculate each with bilinear interpolation and just calculate the final texel with linear interpolation based on the coordinate u
exactly the same ... so
u=u*zs; u0=floor(u); u1=u0+1; wu=u-u0;
c = cu0 + (cu1-cu0)*wu;
where zs
is the number of textures, cu0
is the result of a "strong" two-line interpolation in the texture u0
and cu1
in u1
. The same principle is also used for mipmaps ...
All coordinates can be offset by 0.5 texel, and resolution multiplication can be done with xs-1
instead of xs
based on your clamp settings ...
source to share