Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: modified radial blur shader question

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2012
    Posts
    3

    modified radial blur shader question

    I'm hoping someone can help with this shader I've modified/written. It was originally a radial blur shader that I've added some RGBtoHSV/HSVtoRGB code to. The idea is to cycle through the different colors over each iteration of a for loop that transforms the fragment in the x y of the texture coordinates. Basically with the end result is each layer of the image is a different hue. I've successfully been able to do RGB/HSV conversions but I can't get the hue to change on each iteration. I'm hoping someone could take a look at my code and maybe point me in the right direction.

    I'm trying to shift the hue in the line:
    Code :
    float hueChange = (Hue/360) + HueShift*(float(i)/(nSamples-1.0));

    Here's the whole fragment shader:

    Code :
    uniform sampler2D sInput1;
    uniform float Hue;
    uniform float Sat;
    uniform float Value;
    uniform float HueShift;
     
    uniform float BlurStart;
    uniform float BlurWidth; 
    uniform vec2 Center;
    uniform float nSamples;
     
     
    void main()
    {
     
    	vec4 finalColor;
    	vec4 c = vec4(0.0);
    	vec3 grey= vec3(0.0);
    	vec4 color = vec4(0.0);
    	float alpha = 0.0;
     
     
    	for(int i=0; i<int(nSamples); i++) 
    	{
     
    		float cMax, cMin;
        	float D;
     
    		float H, S, V;
        	float vR, vG, vB;
    		float dR,dG,dB;
     
    		float scale = BlurStart + BlurWidth*(float(i)/(nSamples-1.0));
    		c = texture2D(sInput1, gl_TexCoord[0].st*scale + Center);
     
    		//convert to single color
    		float luma = (0.3*c.r+0.59*c.g+0.11*c.b);
    		//float luma = max(c.r,max(c.g,c.b));	
    		grey = vec3(luma);
    		if (luma > 0.0)
    		{
    			alpha = 1.0;
    		}
    		else 
    		{
    			alpha = 0.0;
    		}
     
    		color += vec4(grey * vec3(1.0,0.0,0.0),alpha);
     
        	vR = color.r;
        	vG = color.g;
        	vB = color.b;
     
    		//gl_FragColor=color;
    		//return;
     
    		// convert to HSV
     
        	cMax = max(vR,max(vG,vB));
        	cMin = min(vR,min(vG,vB));
    		D = cMax - cMin;
     
    		V= cMax;
     
     
    		if (D == 0.0)
    		{
    			H = 0.0;
    			S = 0.0;
    		}
    		else
    		{
    			S = D/cMax;
     
    			dR = (((cMax-vR)/6)+(D/2))/D;
    			dG = (((cMax-vG)/6)+(D/2))/D;
    			dB = (((cMax-vB)/6)+(D/2))/D;
     
    			if (vR==cMax) H = dB-dG;
    			else if (vG==cMax) H = (1/3)+dR-dB;
    			else if (vB==cMax) H = (2/3)+dG-dR;
     
    			if (H<0) H += 1;
    			if (H>1) H -= 1;
    		}
     
     
     
    		float hueChange = (Hue/360) + HueShift*(float(i)/(nSamples-1.0));
     
    		H += hueChange;
    		S *= Sat;
    		V *= Value;
     
     
    		// convert to RGB
     
    		float R,G,B;
    		float vh,vi,vr,vg,vb,v1,v2,v3;
     
    		if (S == 0)
    		{
    			R = V;
    			B = V;
    		}
    		else
    		{
    			vh = H*6;
    			if (vh==6) vh=0;
    			vi = int(vh);
    			v1 = V * (1-S);
    			v2 = V * (1-S*(vh-vi));
    			v3 = V * (1-S*(1-(vh-vi)));
     
    			if 	 (vi == 0) { vr = V ; vg = v3; vb = v1;}
    			else if (vi == 1) { vr = v2; vg = V ; vb = v1;}
    			else if (vi == 2) { vr = v1; vg = V ; vb = v3;}
    			else if (vi == 3) { vr = v1; vg = v2; vb = V ;}
    			else if (vi == 4) { vr = v3; vg = v1; vb = V ;}
    			else    		  { vr = V ; vg = v1; vb = v2;}
     
    			R = vr;
    			G = vg;
    			B = vb;
     
    			finalColor= vec4(R,G,B,1.0);	
     
     
    		}
    	}
     
     
     
    finalColor /= nSamples;
    gl_FragColor = finalColor;
     
    }

    Any help would be greatly appreciated.
    Thanks
    Keith


  2. #2
    Junior Member Newbie
    Join Date
    Mar 2012
    Posts
    3

    Re: modified radial blur shader question

    I've come to the conclusion that with the way the original radial blur shader works it would be impossible to do a hue shift to each "layer" of the image. At first I didn't really understand how the shader worked but now I'm pretty sure of what's happening.

    Code :
        for(int i=0; i<int(nSamples); i++)
        {
           float scale = BlurStart + BlurWidth*(float(i)/(nSamples-1));
           color = texture2D(sInput1, gl_TexCoord[0].st*scale + Center);
           c += color;   
        }
        c /= (nSamples);
        gl_FragColor = c;


    For every pixel (fragment) it samples another pixel determined by the offset and scale and then repeats (with offset tex coord) the process for the number of samples specified. For each iteration it adds each sample's rgba to the previous samples and finally draws the summed (divided by the number of samples) color of all the pixels sampled. So basically there are no separate layers to apply any color shifts to. There are just single pixels with new colors. If you want to shift one pixel's color by a set amount you have to shift them all, unless you incorporate the texcoordinates of each of the pixels but that won't really relate the perceived "layers".

    I think to do this I would need to do a color shift to the whole image (with the tex coord transform) and then write that layer to memory, then shift the next layer and add it to the previous layer(summed layers) in memory.

    I don't really have any experience writing to texture memory are a there any suggestions? Is it possible with a shader to store a texture and recall it? Also please correct me if I'm wrong about any of the above.

    thanks
    Keith

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •