PDA

View Full Version : Convolution mask not working



iris_raj
06-07-2007, 05:14 AM
Dear GLSL programmers,

I am trying to develop effects like sharpening,blurring and Edge detection using GLSL In some cases, I am getting result (but not desired one) and in some other case no output Image is displayed(black screen is getting displayed ). The following is my code.Please advice.




uniform sampler2D tex1;
const float step_w = 1/width;
const float step_h = 1/height;

vec2 offset[9] = {vec2(-step_w,-step_h),vec2(0.0,-step_h),vec2(step_w,-step_h),vec2(-step_w,0.0),vec2(0.0,0.0),vec2(step_w,0.0),vec2(-step_w,step_h),vec2(0.0,step_h),vec2(step_w,step_h )};

//First kernel - This is for gaussian smoothing
//Image is getting displayed,but not smoothed
float kernel2[9] = {0.0,1.0,0.0,1.0,-4.0,1.0,0.0,1.0,0.0};
//Second Kernel - Nothing is getting displayed.
float kernel2[9] = {-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0};
//Third kernel - For Detecting Edge
//Edges are not properly displayed
float kernel2[9] = {-1.0,-1.0,-1.0,0.0,0.0,0.0,1.0,1.0,1.0};
//Shader code

void main()
{
int i=0;
vec4 vsum;
for(i=0;i<9;i++)
{
vec4 tmp = texture2D(tex1,gl_TexCoord[0].st,+ offset[i]);
vsum += tmp.rgba * kernel2[i];
ksum += kernel2[i];
}
if(ksum < 0)
ksum = 1.0;
vec4 vavg = vsum/ksum;
gl_FragColor = vavg;
}
In the application code, I am using tga loader to load images.

Please advice

RAJESH.R

kewuwsi
06-07-2007, 01:43 PM
are you sure the kernel is correct?
is this the right code you are using?

Relic
06-12-2007, 05:11 AM
The code you posted is not what you're running. And if, it's broken.

int i=0; // Initialization is not needed here, the for loop does it.

float ksum; declaration is missing in the above code, that wouldn't even compile.

If you hardcode your kernel2, do not add the if-statement or averaging if the sum is 0.0.

//First kernel - This is for gaussian smoothing//Image is getting displayed,but not smoothed
float kernel2[9] = {0.0,1.0,0.0,1.0,-4.0,1.0,0.0,1.0,0.0};

texture2D(tex1,gl_TexCoord[0].st,+ offset[i]);
// That shouldn't even compile. You meant this:
texture2D(tex1,gl_TexCoord[0].st + offset[i])

How do you get the texture width and height into the shader?

//Second Kernel - Nothing is getting displayed.
float kernel2[9] = {-1.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,-1.0}; // Wrong, it's not nothing, it's deep black when displayed on a fixed point color buffer.

vec4 vsum; //Bug, it's not initialized but you use a "+=" on it! That should result in random results.

vec4 vavg = vsum/ksum;
gl_FragColor = vavg;
// Should be gl_FragColor = vsum / vec4(ksum);