## Create Bi-Cubic and Bi-Linear interpolation with GLSL Shader.

Showing 1-4 of 4 messages
 Create Bi-Cubic and Bi-Linear interpolation with GLSL Shader. SanS 8/8/11 10:54 AM Hi all,Bi-Cubic and Bi-Linear interpolation is implemented with OpenGL.http://www.codeproject.com/KB/GPU-Programming/GLSLBiCubicInterpolation.aspx Re: Create Bi-Cubic and Bi-Linear interpolation with GLSL Shader. Skybuck Flying 8/9/11 6:06 AM "SanS"  wrote in message news:6435e426-5b33-4d60-8de8-47bf45bce1d7@h25g2000prf.googlegroups.com..."Hi all,Bi-Cubic and Bi-Linear interpolation is implemented with OpenGL.http://www.codeproject.com/KB/GPU-Programming/GLSLBiCubicInterpolation.aspx"Looks like crap to me.3D Information in the colors is lost, like shading and highlights.Let the human brain do interpolation I say ;)Bye,  Skybuck. Re: Create Bi-Cubic and Bi-Linear interpolation with GLSL Shader. hanukas 8/11/11 12:52 AM On Aug 8, 8:54 pm, SanS wrote:> Hi all,>> Bi-Cubic and Bi-Linear interpolation is implemented with OpenGL.http://www.codeproject.com/KB/GPU-Programming/GLSLBiCubicInterpolatio...vec4 BiCubic( sampler2D textureSampler, vec2 TexCoord ){  // ... edit ...    for( int m = -1; m <=2; m++ )    {        for( int n =-1; n<= 2; n++)        {                        vec4 vecData = texture2D(textureSampler,                               TexCoord + vec2(texelSizeX *float( m ), texelSizeY * float( n )));        }    }  // ... edit ...}You are doing 16 texture2D reads, this is possible to optimize into 4:vec4 filter(sampler2D texture, vec2 texcoord, vec2 texscale){    float fx = fract(texcoord.x);    float fy = fract(texcoord.y);    texcoord.x -= fx;    texcoord.y -= fy;    vec4 xcubic = cubic(fx);    vec4 ycubic = cubic(fy);    vec4 c = vec4(texcoord.x - 0.5, texcoord.x + 1.5, texcoord.y -0.5, texcoord.y + 1.5);    vec4 s = vec4(xcubic.x + xcubic.y, xcubic.z + xcubic.w, ycubic.x +ycubic.y, ycubic.z + ycubic.w);    vec4 offset = c + vec4(xcubic.y, xcubic.w, ycubic.y, ycubic.w) /s;    vec4 sample0 = texture2D(texture, vec2(offset.x, offset.z) *texscale);    vec4 sample1 = texture2D(texture, vec2(offset.y, offset.z) *texscale);    vec4 sample2 = texture2D(texture, vec2(offset.x, offset.w) *texscale);    vec4 sample3 = texture2D(texture, vec2(offset.y, offset.w) *texscale);    float sx = s.x / (s.x + s.y);    float sy = s.z / (s.z + s.w);    return mix(        mix(sample3, sample2, sx),        mix(sample1, sample0, sx), sy);}The trick is a simple one: the area we are sampling from is 4x4. Webreak this down into four quadrants, 2x2 each. This means we cansample each quadrant (2x2) using hardware texture sampler in bilinearmode. We simply compute texture coordinates so that each texel gets acorrect weight from bicubic function.We now have four samples. We do one last bilinear filter to mix thosetogether (see the three mixes in the return statement!). So basically,bicubic filter is _5_ bilinear ops where 4 of those can be done infixed-function texture sampler hardware. The last one is done using ashader.I used this in OpenGL-accelerated image viewer for Mac OS X, Windowsand Linux, above filter is a plugin for the viewer (the plugins aresimple GLSL shaders with a header which configures the hardware state,for example in this case we would want to use GL_LINEAR min/magfilters so that we get the desired samples out).I left the bicubic function as exercise to the reader, it's trivialanyway. =) Re: Create Bi-Cubic and Bi-Linear interpolation with GLSL Shader. albert_...@yahoo.fr 12/10/12 5:27 AM Hi, Could you share your code for the "cubic" function ? I know this is "trivial anyway" but I tried to implement it with "triangular" function or other functions from here : http://www.codeproject.com/Articles/236394/Bi-Cubic-and-Bi-Linear-Interpolation-with-GLSL#GLSLBiCubic And I didn't get correct images... Thanks !