Fresnel equation

Started by
14 comments, last by Chris_F 11 years, 1 month ago

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;
}
Advertisement

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.

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.”

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.

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'.

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. wacko.png

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))
        );
}

This topic is closed to new replies.

Advertisement