GL_LINEAR到底使用什么算法?

Win*_*ade 6 opengl textures linear-interpolation

refpages 说“返回最接近指定纹理坐标的四个纹理元素的加权平均值。” 它们的权重到底是怎样的?那么 3D 纹理呢,它仍然只使用 4 个或更多纹理像素进行插值吗?

Spe*_*tre 2

in 2D textures are 4 samples used which means bi-linear interpolation so 3x linear interpolation. The weight is the normalized distance of target texel to its 4 neighbors.

So for example you want the texel at

(s,t)=(0.21,0.32)
Run Code Online (Sandbox Code Playgroud)

but the texture 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)
Run Code Online (Sandbox Code Playgroud)

the weights are:

ws = (s-s0)/(s1-s0) = 0.2
wt = (t-t0)/(t1-t0) = 0.4
Run Code Online (Sandbox Code Playgroud)

so linear interpolate textels at s direction

c0 = texture(s0,t0) + (texture(s1,t0)-texture(s0,t0))*ws
c1 = texture(s0,t1) + (texture(s1,t1)-texture(s0,t1))*ws
Run Code Online (Sandbox Code Playgroud)

and finally in t direction:

c = c0 + (c1-c0)*wt
Run Code Online (Sandbox Code Playgroud)

where texture(s,t) returns texel color at s,t while the coordinate corresponds to exact texel and c is the final interpolated texel color.

In reality the s,t coordinates are multiplied by the texture resolution (xs,ys) which converts them to texel units. after that s-s0 and t-t0 is already normalized so no need to divide by s1-s0 and t1-t0 as they are booth equal to one. so:

(s,t)=(0.21,0.32)
Run Code Online (Sandbox Code Playgroud)

I never used 3D textures before but in such case it use 8 textels and it is called tri-linear interpolation which is 2x bi-linear interpolation simply take 2 nearest textures and compute each with bi-linear interpolation and the just compute the final texel by linear interpolation based on the u coordinate in the exact same way ... so

u=u*zs; u0=floor(u); u1=u0+1; wu=u-u0;
c = cu0 + (cu1-cu0)*wu;
Run Code Online (Sandbox Code Playgroud)

where zs is count of textures, cu0 is result of bi-linear interpolation in texture at u0 and cu1 at u1. This same principle is used also for mipmaps...

All the coordinates may have been offseted by 0.5 texel and also the resolution multiplication can be done with xs-1 instead of xs based on your clamp settings ...