Jump to content
  • Advertisement
Sign in to follow this  
Ishraam

[HLSL]Fresnel - this one is working... but why ?

This topic is 4955 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, After a few weeks, I'm finally back on my water rendering stuff. I have just achieved a first step in introducing the Fresnel term to mix reflection and refraction -without distorting the water with bumps yet. My latest try is a success but why...? In short, I first tried to calculate the Fresnel term using this =========================================================================== // -- VS -- output.Normal = normalize(mul(input.normal,WorldViewProj)); output.outPos = mul(input.inPos, WorldViewProj); output.eye_vec = normalize(camera_pos - output.outPos.xyz); // -- PS -- float fresnel = R0 + (1 - R0)*pow(1- dot(normal,eye_vec),5); =========================================================================== The problem was that in some angles it looked weird. Now, here is how I think things are working with this shader ( and I'm obviously mistaking...). - I send the camera position (World Space) to the VS - in the VS, I must calculate the 'eye_vector' (camera_pos - input.position) so since the camera_pos is in World Space I change the vertex position from Object Space to World Space with output.outPos = mul(input.inPos, WorldViewProj); and then do the same for the normal vector. - in the PS I calculate the fresnel term with the normal and eye_vector received from the VS. So I tried another one, which I thought was wrong, but in the end it looked all ok. Here is this second one : =========================================================================== // -- VS -- output.Normal = input.normal; output.eye_vec = normalize(camera_pos - input.inPos.xyz); // -- PS -- float fresnel = R0 + (1 - R0)*pow(1- dot(normal,eye_vec),5); =========================================================================== I guess there are a few basic stuff I haven't properly understood yet with shaders - or is it my math 'skill' ? Could anyone tell me what's wrong in the first one? (And thus how come the 2nd one is ok ?)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
output.Normal = normalize(mul(input.normal,WorldViewProj));

You're passing normals to the PS in projection space. If you want normals in world space, multiply them by the world matrix only. If you want them in view space, multiply them by the world-view matrix.
(It depends on how you're doing the calculation with the normal, i.e. which space do you expect it to be in)

Share this post


Link to post
Share on other sites
Ok, I get it.
So here is another question : in a VS, the (input) position of a vertex is in object space, right?
And (here) my camera_pos is in WorldSpace (since I've send the camera world position), so I think I should have them both in the same space to compute the 'eye_vector'. But since I didn't modify the world matrix (it's still and always Identity), I guess the object space and the world space are the same. Which explains why the second try was a success.

Am I right?

Share this post


Link to post
Share on other sites
Cool - is that the formula for fresnel or an approximation? I thought it had a cosine or something in it.
float fresnel = R0 + (1 - R0)*pow(1- dot(normal,eye_vec),5);

Of, course the approximation probably looks visually identical so that's all that's needed.

Share this post


Link to post
Share on other sites
It's a good approximation of the formula.
And the cosine is hidden in the dot product (dot product of 2 unit vectors equals their angle cosine).

Share this post


Link to post
Share on other sites
Quote:
Original post by Ishraam
Ok, I get it.
So here is another question : in a VS, the (input) position of a vertex is in object space, right?
And (here) my camera_pos is in WorldSpace (since I've send the camera world position), so I think I should have them both in the same space to compute the 'eye_vector'. But since I didn't modify the world matrix (it's still and always Identity), I guess the object space and the world space are the same. Which explains why the second try was a success.

Am I right?

Right [smile]
But you shouldn't rely on the fact that the world matrix is identity unless you're 100% sure it'll always be identity.

Share this post


Link to post
Share on other sites
Ok, thanks for the answers !
^_^

And here is a little video showing what I have done up to now http://www.chez.com/ishraam/videos/WaterDemo_3.mpg

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!