Spherical Harmonics Diffuse CubeMap. How to get coefficients? envmap.pdf | An Efficient Representation for Irradiance Environment Maps
Spherical Harmonics and applications in real time graphics


I am using Spherical Harmonics to turn reflective(mirror like) cube map to diffuse light cube map.

From what I understand for an image, I calculate 9 coefficient(for red green blue) that describes the color of the cube map.

The nine coefficients are this...



And when you visualize them they become...



Then I do something with these coefficients to get color of the cube map.

The part that I am not understanding is using an image to get that 9 coefficients. All the papers kinda assume that you somehow got that coefficient calculated because it is "easy".

I don't know how to do that calculation.



Update

So while I were looking at Spherical Harmonic Lighting | Simon's Tech Blog

I found this equation...


So now I know that the mysterious yellow part of the equation was "solid angle related". I do know how to do solid angle but I still don't understand how to do it.
So at start you have samples from your cube map. Each sample has color and normal (dir) at which you sampled that color.
This is how I do it. I use coeffs from this paper (the same you linked), there are values for the first 9 of them.

So for constructing for each sample you:


Compute your SH basis using normal from sample




float Y00     = 0.282095;
float Y11     = 0.488603 * normal.x;
float Y10     = 0.488603 * normal.z;
float Y1_1    = 0.488603 * normal.y;
float Y21     = 1.092548 * normal.x*normal.z;
float Y2_1    = 1.092548 * normal.y*normal.z;
float Y2_2    = 1.092548 * normal.y*normal.x;
float Y20     = 0.946176 * normal.z * normal.z - 0.315392;
float Y22     = 0.546274 * (normal.x*normal.x - normal.y*normal.y);



Then multiply these coeffs by your color (L)




vec3 L00   = L * Y00;
vec3 L11   = L * Y11;
vec3 L10   = L * Y10;
vec3 L1_1  = L * Y1_1;
vec3 L21   = L * Y21;
vec3 L2_1  = L * Y2_1;
vec3 L2_2  = L * Y2_2;
vec3 L20   = L * Y20;
vec3 L22   = L * Y22;


And thats it, you have your coeffs. Now, you can store them in some data structure. You accumulate each of them like:

someArray[0] += L00;
someArray[1] += L11;
etc...


And you divide these accumulated coeffs by a number of samples;

Then when reconstructing :


compute your SH basis one more time for normal at point of the scene.
for approximating diffuse irradiance at given point of scene,
convolution with cosine lobe is performed using normal from that point.
Convolution means, for each SH order, multiplication by $
   \hat{A}=[\pi, \frac{2}{3}\pi, \frac{\pi}{4}]$


$\hat{A_0} = 3.141593$, 
$\hat{A_1} = 2.094395$, 
$\hat{A_2} = 0.785398$


L coeffs are sampled from data structure.




vec3 color = A0*Y00*L00  
             + A1*Y1_1*L1_1 + A1*Y10*L10 + A1*Y11*L11 
             + A2*Y2_2*L2_2 + A2*Y2_1*L2_1 + A2*Y20*L20 + A2*Y21*L21 + A2*Y22*L22;


Disclaimer: This basically works (for me) but I am not an expert and something might be missing or not perfect because this IS confusing topic...