## Spherical Gaussian approximation for Blinn-Phong, Phong and Fresnel

Spherical Gaussian approximation for lighting calculation is not new to the graphic community[4][5][6][7][8] but its use has  recently been adopted by the game developer community [1][2][3].
Spherical Gaussian (SG) is a type of spherical radial basis function (SRBF) [8] which can be used to approximate spherical lobes with Gaussian-like function.
This post will describe how SG can be use to approximate Blinn-Phong lighting, Phong lighting and Fresnel.

Why care about SG approximation ?
In the context of realtime rendering for games, the SG approximation allows to save a few instructions when performing lighting calculations. For modern graphics cards, saving few ALU in a shader is not always beneficial, but for older hardware like one can find in the PS3, every GPU cycle counts. This is less true for XBOX360 GPU, which performs better with arithmetic instructions, though this optimization can still be beneficial. It can also be used to schedule instructions in a different (low loaded) pipe on SPUs to increase performance [2].

Part of the work presented here credits to Matthew Jones (from Criterion Games) [9].

## Different spherical radial basis function

The post talk about SG, but it is good to know that there is several different types of SRBF found in graphics papers.
For completeness, I will talk quickly about SG and von Mises-Fisher (vMF) as this can be confusing.

SG definition can be found in [4]:
$G(v; p,\lambda,\mu)=\mu e^{\lambda(v.p -1)}$ where p ∈ 𝕊2 is the lobe axis, λ ∈ (0,+∞) is the lobe sharpness,
and μ ∈ ℝ is the lobe amplitude (μ ∈ ℝ3 for RGB color).

vMF is detailed in [8]:
$\gamma(n.\mu;\theta) = \frac{k}{4\pi \sinh(k)} e^{k(n.\mu)}$ with inverse width k and central direction μ. vMFs are normalized to integrate
to 1. Note: the notation from the paper [8] doesn’t match the notation from above.
The paper presents an approximation of this formula for k > 2 (which is almost always the case) : $\gamma(n.\mu;\theta)\approx \frac{k}{2\pi} e^{-k(1-n.\mu)}$

As you can see, the formulation stays the same for a lobe sharpness > 2, the difference lies only in the normalization constant for vMF, which we will omit. In the following, I will only refer to the SG function.

## Approximate Blinn-Phong with SG

An approximation of the Blinn-Phong model with SG is provided in the supplementary material of [4]:
$D(h)=(h.n)^k \approx e^{-k(1-(h.n))}$

Here is the comparison of the accuracy of the approximation for low specular power (<10), medium (25) and high( > 50)

Version : 1.31 – Living blog – First version was 2 August 2011

With permission of my company : Dontnod entertainmenhttp://www.dont-nod.com/

This last year sees a growing interest for physically based rendering. Physically based shading simplify parameters control for artists, allow more consistent look under different lighting condition and have better realistic look. As many game developers, I decided to introduce physical based shading model to my company. I started this blog to share what we learn. The blog post is divided in two-part.

I will first present the physical shading model we chose and what we add in our engine to support it : This is the subject of this post. Then I will describe the process of making good data to feed this lighting model: Feeding a physically based shading model . I hope you will enjoy it and will share your own way of working with physically based shading model. Feedback are welcomed!

Notation of this post can be found in siggraph 2010 Physically-Based Shading Models in Film and Game Production Naty Hoffman’s paper [2].

Working with a physically based shading model imply some changes in a game engine to fully support it. I will expose here the physically based rendering (PBR) way we chosed for our game engine.

When talking about PBR, we talk about BRDF, Fresnel, energy conserving, Microfacet theory, punctual light sources equation… All these concepts are very well described in [2] and will not be reexplained here.

Our main lighting model is composed of two-part: Ambient lighting and direct lighting. But before digging into these subjects, I will talk about some magic numbers.

## Normalization factor

I would like to clarify the constant we find in various lighting model. The energy conservation constraint (the outgoing energy cannot be greater than the incoming energy) requires the BRDF to be normalized. There are two different approaches to normalize a BRDF.

Normalize the entire BRDF

Normalizing a BRDF means that the directional-hemispherical reflectance (the reflectance of a surface under direct illumination) must always be between 0 and 1 : $R(l)=\int_\Omega f(l,v) \cos{\theta_o} \mathrm{d}\omega_o\leq 1$ . This is an integral over the hemisphere. In game $R(l)$ corresponds to the diffuse color $c_{diff}$.

For lambertian BRDF, $f(l,v)$ is constant. It mean that $R(l)=\pi f(l,v)$ and we can write $f(l,v)=\frac{R(l)}{\pi}$
As a result, the normalization factor of a lambertian BRDF is $\frac{1}{\pi}$

For original Phong (the Phong model most game programmer use) $\underline{(r\cdot v)}^{\alpha_p}c_{spec}$ normalization factor  is $\frac{\alpha_p+1}{2\pi}$
For Phong BRDF (just mul Phong by $\cos{\theta_i}$ See [1][8]) $\underline{(r\cdot v)}^{\alpha_p}c_{spec}\underline{(n\cdot l)}$ normalization factor  becomes $\frac{\alpha_p+2}{2\pi}$
For Binn-Phong $\underline{(n\cdot h)}^{\alpha_p}c_{spec}$ normalization factor  is $\frac{(\alpha_p+2)}{4\pi(2-2^\frac{-\alpha_p}{2})}$
For Binn-Phong BRDF $\underline{(n\cdot h)}^{\alpha_p}c_{spec}\underline{(n\cdot l)}$ normalization factor  is $\frac{(\alpha_p+2)(\alpha_p+4)}{8\pi(2^\frac{-\alpha_p}{2}+\alpha_p)}$
Derivation of these constants can be found in [3] and [13]. Another good sum up is provide in [27].

Note that for Blinn-Phong BRDF, a cheap approximation is given in [1] as : $\frac{\alpha_p+8}{8\pi}$
There is a discussion about this constant in [4] and here is the interesting comment from Naty Hoffmann

About the approximation we chose, we were not trying to be strictly conservative (that is important for multi-bounce GI solutions to converge, but not for rasterization).
We were trying to choose a cheap approximation which is close to 1, and we thought it more important to be close for low specular powers.
Low specular powers have highlights that cover a lot of pixels and are unlikely to be saturating past 1.

When working with microfacet BRDFs, normalize only microfacet normal distribution function (NDF)

A Microfacet distribution requires that the (signed) projected area of the microsurface is the same as the projected area of the macrosurface for any direction v [6]. In the special case v = n:
$\int_\theta D(m)(n\cdot m)\mathrm{d}\omega_m=1$
The integral is over the sphere and cosine factor is not clamped.

For Phong distribution (or Blinn distribution, two name, same distribution) the NDF normalization constant is  $\frac{\alpha_p+2}{2\pi}$
Derivation can be found in [7]

## Direct Lighting

Our direct lighting model is composed of two-parts : direct diffuse + direct specular
Direct diffuse is the usual Lambertian BRDF : $\frac{c_{diff}}{\pi}$
Direct specular is the microfacet BRDF describe by Naty Hoffman in [2] : $F_{schilck}(c_{spec},l_c,h)\frac{\alpha_p+2}{8\pi}\underline{(n\cdot h)}^{\alpha_p}$