I just wanted to test the effect of dFdx(One of Built-in Functions in GLSL).This function is used to get Derivatives of variables.And I tested it in the way as follows.
vertex shader:
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}
fragment shader:
#define lineWidth 0.02
const float C_PI = 3.1415926;
void main()
{
vec4 SinColor, BackColor, finalColor;
BackColor = vec4(1.0, 1.0, 0.0, 1.0);
float px, sx, x;
x = gl_TexCoord[0].s * 2.0 * C_PI;
px = sin(x);
sx = (dFdx(px) / (2.0 * C_PI));
float scaledS = sx;
float scaledT = (gl_TexCoord[0].t - 0.5) * 2.0;
if(abs(scaledT - scaledS) <= lineWidth){
SinColor = vec4(1.0, 0.0, 0.0, 1.0);
finalColor = SinColor;
}else{
finalColor = BackColor;
}
gl_FragColor = finalColor;
}
According to Calculus, “dFdx(sin(2πx)) = 2πcos(2πx)”,so I tested it as stated above.In my idea, if dFdx() is correctly supported by hardware, the curve of cosine can be displayed with above processing. But the final effect looks like a line which is parallel to X axis.
But when I modified the fragment shader as follows(just amplify
Y axis 350 times):
void main()
{
vec4 SinColor, BackColor, finalColor;
BackColor = vec4(1.0, 1.0, 0.0, 1.0);
float px, sx, x;
x = gl_TexCoord[0].s * 2.0 * C_PI;
px = sin(x);
sx = (dFdx(px) / (2.0 * C_PI)) * [color:#FF0000]350.0[/color];
float scaledS = sx;
float scaledT = (gl_TexCoord[0].t - 0.5) * 2.0;
if(abs(scaledT - scaledS) <= lineWidth){
SinColor = vec4(1.0, 0.0, 0.0, 1.0);
finalColor = SinColor;
}else{
finalColor = BackColor;
}
gl_FragColor = finalColor;
}
then the cosine curve was displayed correctly.(My graphics card is GeForce 8800GT.)
My question is why this phenomenon happend?
Anybody could give me some accurate definition of dFdx().
Any help would be appreciated,thank you very much!