gl_FragColor = u_xx;
//gl_FragColor = u_ip - 2*u + u_im; this one gives the correct laplacian
}
so gl_FragColor = u_xx; is calculating the backward difference of a forward difference operator giving the laplacian in one direction and this does not gives the correct result.
While gl_FragColor = u_ip - 2*u + u_im; gives the correct laplacian.
I think the problem is that the lookup texture2D(u_tex, u_fx.xy - vec2(0.0,h)) is wrong but I don’t know how I can lookup the u_fx texture?
your texture lookup texture2D(u_tex, u_fx.xy - vec2(0.0,h)) does indeed look wrong.
I’m a little stumped by your question. You know how to get the correct result…what exactly is the issue?
That aside, in terms of the lookup above, remember that the first lookups you are doing use the fragment position (which I assume is how you’ve set up your texcoords) and lookup values spatially around that fragment (forwards and backwards)
Then with your problem texture2D(u_tex, u_fx.xy - vec2(0.0,h)) the u_fx isn’t relative to the fragment anymore. I’d guess, not knowing the specifics of this problem, that you want to be adding or subracting that u_fx from the current fragment position, or the next position to the right or left.
Are you entirely sure of your algorithm? I have never did that in a glsl shader. But I find doubtful your way of using returned texel colors by texture2D as texture coordinates latter. Please correct me if I am mistaken.
If I had to compute laplacian in a texture, I would first compute gradient and then divergence of these gradient vectors. But your method may be faster.