#### Archived

This topic is now archived and is closed to further replies.

# The Fresnel effect - help

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

## Recommended Posts

I have been trying to implement the fresnel effect with no success. I am using the code below (in c): ////////////////////////////start code/////////////////////////// #define Fresnel(FaceX, FaceY, FaceZ, NormalX, NormalY, NormalZ) \ vx = FaceX; \ vy = FaceY; \ vz = FaceZ; \ ix = vx - (g_Camera.pos[0]); iy = vy - (g_Camera.pos[1]); iz = vz - (g_Camera.pos[2]); imag = sqrt(ix * ix + iy * iy + iz * iz); \ cosine = -(ix * NormalX + iy * NormalY + iz * NormalZ) / imag; \ if (cosine < 0) cosine = 0; if (cosine > 1) cosine = 1; \ fresnel = 1 / ((cosine + 1) * (cosine + 1)); \ fresnel = fresnel * fresnel * fresnel * fresnel; /*/ (cosine + 1);*/ \ /*imag = (float) exp(-imag * 0.0025f);*/\ cosine = 2 * cosine * cosine - 1; \ cosine = 0.8f - 0.05f * cosine; \ output_color.red = (0.0f * cosine * (1 - fresnel) + 0.37f * fresnel); \ output_color.green = (0.1275f * cosine * (1 - fresnel) + 0.47f * fresnel); \ output_color.blue = (0.1913f * cosine * (1 - fresnel) + 0.55f * fresnel); ////////////////////////////end code/////////////////////////// [FaceX, FaceY, FaceZ] are the x,y,z cor-ords of point to be calculated [NormalX, NormalY, NormalZ] are the normals for the face [output_color] contains rgb+a color values. the values used to calculate the colors are constance for water. has anyone seen a better implementation or knows what is wrong? my card doesn''t support shaders, no I wanted to implement it in software mode. any help would be great! thanks

##### Share on other sites
Sorry I''m not commenting about your question, but I really think a macro is a bad choice here. Looks like a good place for a regular function, or if you actually need the optimization, make it inline.

##### Share on other sites
Its very hard to tell from your code what equations you based this on. Maybe try these?

http://www.wikipedia.org/wiki/Fresnel_equations

There are also faster polynomial approximations courtesy of Schlick (you can google for it).

##### Share on other sites
Can you tell us what is wrong? What do the results look like?

It looks like plausible code, assuming you''re plugging everything in correctly.

You are computing the Fresnel term to be

1/(1+Cos(theta_i))^8 .

This seems like a reasonable approximation.

I plotted this and the exact Fresnel formula (formula 44 of Tessendorf''s paper "Simulating Ocean Water" -- plug in formula 40 for theta_t = ArcSin[3 Sin[theta_i]/4]).

Your approximation always underestimates the correct formula. For theta=theta_i between 0 and 1 (in radians), the underestimate is about .02, after that it peaks at about theta=1.4 at .07.

As a percent deviation from the Fresnel values, you are 80% too low at theta=0, dropping to 50% too low at theta=pi/4, then steadily falling to 0. So I''d guess that the water is too dark (although you may have compensated for this effect elsewhere, e.g., in the base water color)?

Using 1/(1+Cos[theta])^7 -- just something I tried -- gives a more accurate Fresnel term. The error is less than 10% over the range theta in [.8,pi/2], increasing to 60% (too low) at theta=0.

Okay so far. Next it looks like you are multiplying the fresnel term by a constant for the sky color, and 1-fresnel by a water color.

What is that extra cosine factor doing there by the 1-fresnel term? I guess it is a term to modify the water color according to the angle of the transmitted view ray. Higher angles make this factor larger, which makes sense since the transmitted rays would be closer to the surface, so scatter more sunlight.

Still, I''d suggest removing this multiplication until you debug the rest. (Just have (1-fresnel), not cosine * (1-fresnel).)

1. 1
2. 2
3. 3
Rutin
23
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 11
• ### Forum Statistics

• Total Topics
633653
• Total Posts
3013156
×