• Create Account

# Fresnel equation

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

15 replies to this topic

### #1Chris_F  Members   -  Reputation: 2975

Like
0Likes
Like

Posted 30 January 2013 - 07:24 AM

Out of curiocity I wanted to compare Schlick's approximation to the real thing, so I had my hand at implementing it, though, admittedly, I'm not very good with the maths... so I may have made a mistake, or more.

float CosTheta = dot(N, L);
float SinTheta = sqrt(1 - CosTheta * CosTheta);
float temp = n1 * (SinTheta / n2);
temp = n2 * sqrt(1 - temp * temp);
temp = (n1 * CosTheta - temp) / (n1 * CosTheta + temp);
float fresnel = temp * temp;

I'm getting a pretty noticable difference between this and Schlick's.

Edited by Chris_F, 30 January 2013 - 07:24 AM.

### #2CryZe  Members   -  Reputation: 768

Like
2Likes
Like

Posted 30 January 2013 - 07:47 AM

You implemented the formula for s-polarized light. But you want to implement the formula for non-polarized light. Which is simply R = (Rs + Rp) / 2.

### #3Chris_F  Members   -  Reputation: 2975

Like
0Likes
Like

Posted 30 January 2013 - 08:01 AM

You implemented the formula for s-polarized light. But you want to implement the formula for non-polarized light. Which is simply R = (Rs + Rp) / 2.

Doh. Second try?

float CosTheta = dot(N, L);
float SinTheta = sqrt(1 - CosTheta * CosTheta);

float t1 = n1 * (SinTheta / n2);
t1 = sqrt(1 - t1 * t1);
float t2 = (n1 * CosTheta - n2 * t1) / (n1 * CosTheta + n2 * t1);
float Rs = t2 * t2;
t2 = (n2 * CosTheta - n1 * t1) / (n2 * CosTheta + n1 * t1);
float Rp = t2 * t2;
float R = (Rs + Rp) / 2;


Edited by Chris_F, 30 January 2013 - 08:11 AM.

### #4CryZe  Members   -  Reputation: 768

Like
0Likes
Like

Posted 30 January 2013 - 08:10 AM

I'd reduce unnecessary calculations even more. Also your nominators were subtracting the values instead of adding them:

float cosThetaIncidence = dot(N, L);
float sinThetaIncidence = sqrt(1 - cosThetaIncidence * cosThetaIncidence);

float sinThetaTransmittance = n1 * (sinThetaIncidence / n2);
float cosThetaTransmittance = sqrt(1 - sinThetaTransmittance * sinThetaTransmittance);

float n1CosThetaTransmittance = n1 * cosThetaTransmittance;
float n2CosThetaTransmittance = n2 * cosThetaTransmittance;
float n1CosThetaIncidence = n1 * cosThetaIncidence;
float n2CosThetaIncidence = n2 * cosThetaIncidence;

float sPolarizedSqrt = (n1CosThetaIncidence - n2CosThetaTransmittance) / (n1CosThetaIncidence + n2CosThetaTransmittance);
float sPolarized = sPolarizedSqrt * sPolarizedSqrt;

float pPolarizedSqrt = (n2CosThetaIncidence - n1CosThetaTransmittance) / (n2CosThetaIncidence + n1CosThetaTransmittance);
float pPolarized = pPolarizedSqrt * pPolarizedSqrt;

float fresnel = (sPolarized + pPolarized) / 2;


Edited by CryZe, 30 January 2013 - 08:26 AM.

### #5Chris_F  Members   -  Reputation: 2975

Like
0Likes
Like

Posted 30 January 2013 - 08:19 AM

I'd reduce unnecessary calculations even more. Also your nominators were subtracting the values instead of adding them:

float CosTheta = dot(N, L);
float SinTheta = sqrt(1 - CosTheta * CosTheta);

float t1 = n1 * (SinTheta / n2);
t1 = sqrt(1 - t1 * t1);

float n1CosThetaTransmittance = n1 * t1;
float n2CosThetaTransmittance = n2 * t1;
float n1CosThetaIncidence = n1 * CosTheta;
float n2CosThetaIncidence = n2 * CosTheta;

float t2 = (n1CosThetaIncidence - n2CosThetaTransmittance) / (n1CosThetaIncidence + n2CosThetaTransmittance);
float Rs = t2 * t2;
t2 = (n2CosThetaIncidence - n1CosThetaTransmittance) / (n2CosThetaIncidence + n1CosThetaTransmittance);
float Rp = t2 * t2;
float R = (Rs + Rp) / 2;


Could do, but it's unlikely to make a difference as shader compilers are pretty sly.

Edit: For me, it seems as though this full implementation is about 24% more instructions than Schlick's method.

Edited by Chris_F, 30 January 2013 - 08:25 AM.

### #6CryZe  Members   -  Reputation: 768

Like
0Likes
Like

Posted 30 January 2013 - 08:38 AM

For me, it seems as though this full implementation is about 24% more instructions than Schlick's method.

Only 24% more? Isn't Schlick Fresnel ks + (1 - ks) * pow(1 - dot(L, H), 5), which would be about 9 instructions? The full fresnel equation would be about 29 instructions.

Also, why is it dot(N, L) in your code? It should be the microfacets normal and not the normal of the macro surface.

Edited by CryZe, 30 January 2013 - 08:39 AM.

### #7Chris_F  Members   -  Reputation: 2975

Like
0Likes
Like

Posted 30 January 2013 - 08:53 AM

For me, it seems as though this full implementation is about 24% more instructions than Schlick's method.

Only 24% more? Isn't Schlick Fresnel ks + (1 - ks) * pow(1 - dot(L, H), 5), which would be about 9 instructions? The full fresnel equation would be about 29 instructions.

Also, why is it dot(N, L) in your code? It should be the microfacets normal and not the normal of the macro surface.

I included the instruction cost of calculating f0 with pow((n2-n1) / (n2+n1) , 2.0), and I used N.L because I was testing this with normalized Blinn-Phong, not a microfaceted BRDF.

Edited by Chris_F, 30 January 2013 - 09:01 AM.

### #8CryZe  Members   -  Reputation: 768

Like
0Likes
Like

Posted 30 January 2013 - 09:17 AM

I was testing this with normalized Blinn-Phong, not a microfaceted BRDF

Blinn-Phong is the microfacet version of Phong. Blinn-Phong describes the percentage of microfacets oriented towards the halfway vector, cause these are the only microfacets reflecting light into the eye. So if you are extending your already partially existing microfacet BRDF by a fresnel term, you should use the microfacets normal, aka your halfway vector.

Edited by CryZe, 30 January 2013 - 09:20 AM.

### #9Chris_F  Members   -  Reputation: 2975

Like
0Likes
Like

Posted 30 January 2013 - 10:14 AM

OK, one last question, since as I said, I am not very familiar with the math yet. I use sqrt(1-cos^2(theta)) to get sin(theta). Is this correct? It seems to work, but as far as I know, sqrt(1-cos^2(theta)) is actually only equal to abs(sin(theta)). Assuming this works, is this method preferable to taking the length of the cross product?

### #10CryZe  Members   -  Reputation: 768

Like
0Likes
Like

Posted 30 January 2013 - 11:22 AM

abs(sin(theta)) is fine in this case. I'd use your method instead of the cross product, since the calculation of the length of the cross product requires a square root as well, so your method is faster.

### #11CryZe  Members   -  Reputation: 768

Like
1Likes
Like

Posted 31 January 2013 - 03:35 AM

I think the most important thing in optimizing a BRDF is, that you reduce linear time to constant time. Some parts of the BRDF don't need to be calculated per light. Just take a look at your version of Schlick's fresnel:

foreach (light)
{
float f0Sqrt = (n1 - n2) / (n1 + n2);
float f0 = f0Sqrt * f0Sqrt;
float fresnel = f0 + (1 - f0) * pow(1 - dot(L, H), 5);
} 

If you implement it this way, you almost reduce the linear code by half of its instructions:

float f0Sqrt = (n1 - n2) / (n1 + n2);
float f0 = f0Sqrt * f0Sqrt;
float cf0 = 1 - f0;

foreach (light)
{
float fresnel = f0 + cf0 * pow(1 - dot(L, H), 5);
} 

I've reduced my BRDF this way. And this is also the reason I'm using this reduced version of Schlick's fresnel (yes I'm using the refractive indices as well) in my BRDF, because the full fresnel equation just can't be reduced this way and is way too expensive in comparison to this one. That's also the reason why I prefer the GGX NDF over any other NDF. It's pretty damn physically accurate and can be reduced into just a few instructions. Actually it's probably even faster than Blinn-Phong.

float roughnessSqr = roughness * roughness;
float numerator = roughnessSqr / PI;
float roughnessSqrSub1 = roughnessSqr - 1;

foreach (light)
{
float NDotH = dot(N, H);
float NDotHSqr = NDotH * NDotH;
float denominatorSqrt = NDotHSqr * roughnessSqrSub1 + 1;
float denominator = denominatorSqrt * denominatorSqrt;
float ggx = numerator / denominator;
}

Edited by CryZe, 31 January 2013 - 06:40 AM.

### #12Chris_F  Members   -  Reputation: 2975

Like
0Likes
Like

Posted 31 January 2013 - 01:43 PM

That's some cool info. However, going back to what you said earlier, I seem to be a bit confused now as to what actually constitutes a microfacet BRDF. Why is Blinn-Phong considered microfacet based, but Phong is not? I know you can use modified phong as a distrobution in a microfacet BRDF, but I didn't think that an ordinary Blinn-Phong shader was considered to be a microfacet BRDF.

Edited by Chris_F, 31 January 2013 - 01:44 PM.

### #13Bacterius  Crossbones+   -  Reputation: 12415

Like
1Likes
Like

Posted 31 January 2013 - 02:43 PM

That's some cool info. However, going back to what you said earlier, I seem to be a bit confused now as to what actually constitutes a microfacet BRDF. Why is Blinn-Phong considered microfacet based, but Phong is not? I know you can use modified phong as a distrobution in a microfacet BRDF, but I didn't think that an ordinary Blinn-Phong shader was considered to be a microfacet BRDF.

At a very low level, the only physical basis governing the behaviour of light on an individual planar surface is, in fact, the Fresnel equations (it's more complicated if you go deeper, or if you consider magnetic media, but in a nutshell that's pretty much it). So what BRDF's do is take into account the fact that surfaces are often not perfectly planar but somewhat rough. A diffuse BRDF is just that, taken to one extreme - the surface is maximally rough in that the incident direction of light has no bearing on its reflected direction. A perfectly specular BRDF is the other extreme. Every other BRDF is somewhere in the middle, with different geometry distributions to attempt to approximate real life materials.

So the only thing distinguishing a "microfacet" BRDF from a non-microfacet one is the fact that the former handles "microfacet" geometry (i.e. a disorganized mess of randomly oriented planar sections) whereas the latter could handle any other type of surface geometry (for instance, a two-layer material)

Blinn-Phong happens to consider microfacet geometry, therefore it is a microfacet BRDF. The specular exponent is a measure of surface roughness. In fact, there exists a direct conversion formula between a Blinn-Phong exponent and a Beckmann roughness term. Blinn-Phong is essentially a glorified microfacet distribution, and is pretty much its own BRDF since it ignores more or less every other physical effect beyond ambient reflection, diffuse reflection and microfacet specular reflection - you can very well use the Blinn-Phong distribution in a Cook-Torrance BRDF, for instance, and you'll basically get Blinn-Phong Specular + Fresnel.

Phong, on the other hand, is not a microfacet BRDF, because it doesn't consider microfacet geometry. In fact, it doesn't consider anything at all, quoting Wikipedia, "It is based on Bui Tuong Phong's informal observation that shiny surfaces have small intense specular highlights, while dull surfaces have large highlights that fall off more gradually.". The Phong model doesn't assume anything regarding the surface's geometry besides "this will look like metal, that will look like plastic". That's it.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #14Chris_F  Members   -  Reputation: 2975

Like
0Likes
Like

Posted 31 January 2013 - 04:15 PM

you can very well use the Blinn-Phong distribution in a Cook-Torrance BRDF, for instance, and you'll basically get Blinn-Phong Specular + Fresnel

That would be Blinn-Phong specular + fresnell + geometry term wouldn't it? So simply measuring the angle of the half vector is litterally all that is nesisarry to transform Phong into a microfacet BRDF? I've never heard of anyone call Blinn a microfacet BRDF before. Usually they are refering to Cook-Torrance or similar when they bring up facets.

### #15Hodgman  Moderators   -  Reputation: 46533

Like
3Likes
Like

Posted 31 January 2013 - 06:09 PM

IIRC -
Phong was made just with intuition and observation, so it's an 'empirical BRDF'.
Blinn took Phong's work and recreated similar results from a theoretical basis this time, instead of from guesswork and collected data. The framework he used was microfacet theory (which says that only the facets oriented towards the half-vector will produce a visible reflection), so it's a 'microfacet BRDF'. So yes, it's the fact that the BRDF only considers the percentage of the surface that is pointing in the H direction, that makes it a 'microfacet BRDF'.

### #16Chris_F  Members   -  Reputation: 2975

Like
1Likes
Like

Posted 12 March 2013 - 05:18 PM

OK, I'm bumping this thread because I'm revisiting the Fresnel equation, this time using complex IOR values. I'm having a hard time converting this to complex numbers.

float Fresnel(float CosThetaI, float n)
{
float CosThetaT = sqrt(max(0, 1 - (1 - CosThetaI * CosThetaI) / (n * n)));
float NCosThetaT = n * CosThetaT;
float NCosThetaI = n * CosThetaI;
float Rs = pow(abs((CosThetaI - NCosThetaT) / (CosThetaI + NCosThetaT)), 2);
float Rp = pow(abs((CosThetaT - NCosThetaI) / (CosThetaT + NCosThetaI)), 2);
return (Rs + Rp) / 2;
}


This is the basic formula, but I need to re-write it so that it looks like:

float Fresnel(float CosThetaI, vec3 n, vec3 k)
{
...
}


Where n and k make up the complex IOR (n + ki). I've taken a few stabs at it, but it's gotten me nowhere. Here is my train wreck of an attempt:

vec3 Fresnel(float CosThetaI, vec3 n, vec3 k)
{
float temp = 1 - CosThetaI * CosThetaI;

vec3 NKSqr_real = n * n - k * k;
vec3 NKSqr_imag = n * k * 2;

vec3 temp2_real = (temp * NKSqr_real) / (NKSqr_real * NKSqr_real + NKSqr_imag * NKSqr_imag);
vec3 temp2_imag = -(temp * NKSqr_imag) / (NKSqr_real * NKSqr_real + NKSqr_imag * NKSqr_imag);

temp2_real = 1 - temp2_real;
temp2_imag = -temp2_imag;

vec3 CosThetaT_real = sqrt((temp2_real + sqrt(temp2_real * temp2_real + temp2_imag * temp2_imag)) / 2);
vec3 CosThetaT_imag = sign(temp2_imag) * sqrt((-temp2_real + sqrt(temp2_real * temp2_real + temp2_imag * temp2_imag)) / 2);

vec3 NCosThetaT_real = n * CosThetaT_real - k * CosThetaT_imag;
vec3 NCosThetaT_imag = k * CosThetaT_real + n * CosThetaT_imag;

vec3 NCosThetaI_real = n * CosThetaI;
vec3 NCosThetaI_imag = k * CosThetaI;

vec3 CosThetaI_minus_NCosThetaT_real = CosThetaI - NCosThetaT_real;
vec3 CosThetaI_minus_NCosThetaT_imag = -NCosThetaT_imag;

vec3 CosThetaI_plus_NCosThetaT_real = CosThetaI + NCosThetaT_real;
vec3 CosThetaI_plus_NCosThetaT_imag = NCosThetaT_imag;

vec3 a, b, c, d;

a = CosThetaI_minus_NCosThetaT_real;
b = CosThetaI_minus_NCosThetaT_imag;
c = CosThetaI_plus_NCosThetaT_real;
d = CosThetaI_plus_NCosThetaT_imag;

vec3 Rs_real = (a * c + b * d) / (c * c + d * d);
vec3 Rs_imag = (b * c + a * d) / (c * c + d * d);

vec3 Rs = sqrt(Rs_real * Rs_real + Rs_imag * Rs_imag);
Rs = Rs * Rs;

vec3 CosThetaT_minus_NCosThetaI_real = CosThetaT_real - NCosThetaI_real;
vec3 CosThetaT_minus_NCosThetaI_imag = CosThetaT_imag - NCosThetaI_imag;

vec3 CosThetaT_plus_NCosThetaI_real = CosThetaT_real + NCosThetaI_real;
vec3 CosThetaT_plus_NCosThetaI_imag = CosThetaT_imag + NCosThetaI_imag;

a = CosThetaT_minus_NCosThetaI_real;
b = CosThetaT_minus_NCosThetaI_imag;
c = CosThetaT_plus_NCosThetaI_real;
d = CosThetaT_plus_NCosThetaI_imag;

vec3 Rp_real = (a * c + b * d) / (c * c + d * d);
vec3 Rp_imag = (b * c + a * d) / (c * c + d * d);

vec3 Rp = sqrt(Rp_real * Rp_real + Rp_imag * Rp_imag);
Rp = Rp * Rp;

return (Rs + Rp) / 2;
}


It would be so much easier if HLSL/GLSL had first class support for complex values.

EDIT:

Never mind. I managed to come up with this.

vec2 CADD(vec2 a, vec2 b) {    return a + b; }
vec2 CSUB(vec2 a, vec2 b) {    return a - b; }
vec2 CMUL(vec2 a, vec2 b) {    return vec2(a.x * b.x - a.y * b.y, a.y * b.x + a.x * b.y); }
vec2 CDIV(vec2 a, vec2 b) {    return vec2((a.x * b.x + a.y * b.y) / (b.x * b.x + b.y * b.y), (a.y * b.x - a.x * b.y) / (b.x * b.x + b.y * b.y)); }
float CABS(vec2 a) { return sqrt(a.x * a.x + a.y * a.y); }
vec2 CSQRT(vec2 a) { return vec2(sqrt((a.x + sqrt(a.x * a.x + a.y * a.y)) / 2), sign(a.y) * sqrt((-a.x + sqrt(a.x * a.x + a.y * a.y)) / 2)); }

float _Fresnel(float _CosThetaI, vec2 n)
{
vec2 CosThetaI = vec2(_CosThetaI, 0);
vec2 CosThetaT = CSQRT(CSUB(vec2(1.0, 0), CDIV(CSUB(vec2(1.0, 0), CMUL(CosThetaI, CosThetaI)), CMUL(n, n))));
vec2 NCosThetaI = CMUL(n, CosThetaI);
vec2 NCosThetaT = CMUL(n, CosThetaT);
float Rs = pow(CABS(CDIV(CSUB(CosThetaI, NCosThetaT), CADD(CosThetaI, NCosThetaT))), 2);
float Rp = pow(CABS(CDIV(CSUB(CosThetaT, NCosThetaI), CADD(CosThetaT, NCosThetaI))), 2);
return (Rs + Rp) / 2;
}

vec3 Fresnel(float CosThetaI, vec3 n, vec3 k)
{
return vec3(
_Fresnel(CosThetaI, vec2(n.r, k.r)),
_Fresnel(CosThetaI, vec2(n.g, k.g)),
_Fresnel(CosThetaI, vec2(n.b, k.b))
);
}


Edited by Chris_F, 13 March 2013 - 09:50 AM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS