Hopefully this is a very simple problem for someone here to help me solve:
I am trying to modify an existing mipmap blur shader that blurs everything (periphery)
but a region around the cursor location (fovea). I need it to do the opposite, i.e., apply the blur to the fovea but leave the periphery alone.
I have had some success with that, but it is still not quite what I need:
in addition to the blurred fovea I want to also have a blur intensity factor, but these
two parameters need to be independent so that changing the blur strength does not change
the size of the foveal blur (except maybe for a small increase as the blur weakens around the
periphery of the strong blur).
Here is the code I have been working with. I have had some minor success, but cannot figure out how to finish it. Thank you for any insights:
/* BlurredMipmapDemoShader.frag.txt
* Shader used for BlurredMipmapDemo.m
*
* This shader computes the level of resolution for each output texel
* during Screen('DrawTexture') of the video image texture. Resolution
* directly depends on radial distance to the provided simulated center of gaze.
*
* Resolution level is used to determine the mip-map miplevel (lod) to use for
* lookup of the mip-map filtered texel in the images mipmap pyramid.
*
* (C) 2012 Mario Kleiner - Licensed under MIT license.
*
* Attempts to modify function by Jonathan Jacobs (jxj24@case.edu)
*
*/
#extension GL_ARB_shader_texture_lod : enable
/* Image is the mip-mapped texture with the image resolution pyramid: */
uniform sampler2D Image;
/* Passed from vertex shader: */
varying vec4 baseColor;
varying vec2 gazePosition;
varying float gazeRadius;
varying float blurStrength;
void main(void)
{
/* Output pixel position in absolute window coordinates: */
vec2 outpos = gl_FragCoord.xy;
/* Create center position. We will compute distance from it */
/* Does not work */
vec2 centerpos = outpos * 1.0;
/* Compute distance to center of gaze, normalized to units of gazeRadius:
* We take log2 of it, because lod selects mip-level, which selects for a
* 2^lod decrease in resolution: */
/* Original function: */
/* float lod = log2(distance(outpos, gazePosition) / gazeRadius); */
/* Too large an effect with blurStrength: */
/* float lod = log2((gazeRadius*blurStrength - distance(centerpos, gazePosition)) / gazeRadius); */
/* Just grasping at straws. Unblurs the periphery, but does not blur foveal region */
/*float lod = -log2(blurStrength * distance(outpos, gazePosition) / gazeRadius); */
/* Best(?) result: Cause the blur to appear around the cursor, but I need to
* understand how to separate the radius and strength because higher strength
* increases the blur effect on y-axis */
float lod = log2((blurStrength+gazeRadius - distance(centerpos, gazePosition)));
/* Lookup texel color in image pyramid at input texture coordinate and
* specific mip-map level 'lod': */
vec4 texel = texture2DLod(Image, gl_TexCoord[0].st, lod);
/* Apply modulation baseColor and write to framebuffer: */
gl_FragColor = texel * baseColor;
}