Should I use the gradient to get the Normal of a Sin wave?

Started by
3 comments, last by HypnotiC 9 years, 3 months ago

Hi,

I'm making a small 3d waves demo computing the waves based on the distance of the vertex from the camera.

Acoording to this older post to compute the Normal I should compute the partial derivatives of the function I'm using for the waves... I tried the example they're using (a 2d wave) and it works just fine...

The formula I'm using is quite simple:

f(x,z) = amplitude * Sin( (sqrt( x^2 + z^2) + fase) * frequency );

so, the partial derivatives for each component are:
fx = amplitude * frequency * x * Cos( (sqrt(x^2 + y^2) + fase) * frequency ) / sqrt(x^2 + y^2);
fy = 0;
fz = amplitude * frequency * z * Cos( (sqrt(x^2 + y^2) + fase) * frequency ) / sqrt(x^2 + y^2);

thus the gradient vector would be <fx, fy, fz> which according to some math references it should be the normal of the given surface at any point by itself.

however, if I normalize it and use it in my vertex shader, the lighting doesn't look right...

Am I not doing it right?.... am I missing something?

Thanks

"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"
Advertisement

Look here for the equation of a surface normal. The normal at a point (x,z) of the function f(x,z) is (fx(x,z), -1, fz(x,z)) given your notation. Note that your fy should be -1 according to the link, not 0 as in your example.

Thanks Brother Bob!

That was the trick... although I'm using a left handed coordinate system, so had to use positive 1 instead, and that seems to work properly.

"lots of shoulddas, coulddas, woulddas in the air, thinking about things they shouldda couldda wouldda donne, however all those shoulddas coulddas woulddas ran away when they saw the little did to come"

Maths aside, my first thought was: How could the y component of the normal be always 0 if your wave surface is in the xz plane. Shouldn't it be 1, pointing up by default? No-brainer, really. :)

Should you also change "y^2" in your partial direvative to "z^2"?

This topic is closed to new replies.

Advertisement