Mmm.. spherical harmonix?

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Mmm.. spherical harmonix?

Post by devsh »

I only studied spherical geometry a little and I totally dont get Spherical Harmonics at all... despite the fact I am 16 I can integrate, differentiate and poop but wikipedia and all the other sites that mention them are totally NOT HELPFUL... I heard Mel did a spherical harmonics render and I would really like to know how to calculate a spherical harmonic from a cubemap and how to read it back :) Most of all how the whole thing works
griffin2000
Posts: 22
Joined: Mon Nov 15, 2010 9:18 pm

Post by griffin2000 »

It's a pretty complicated subject, you have to be unafraid of dealing with alot of mathematics notation to get to grips with it (and really reading it not just skipping past the maths and reading the code).

A couple of things I found helpful:
This book: http://www.amazon.com/Advanced-Lighting ... 1556222920
This paper + source code: http://www-graphics.stanford.edu/papers/envmap/
This paper: http://www.cs.columbia.edu/~cs4162/slid ... ghting.pdf
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

wait... does it mean that I basically have to go through the environment map, pixel by pixel.... treating them all like diffuse parameters and unprojecting the envmap texcoords as normals/lightdir then adding them to THE 9 xyz/rgb values of a 2nd order spherical harmonic?

Then render a virtual envmap on a plane mapping texcoords as normals using the all mystifying lookup function and compare results?
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

I just cant wait to precompute lighting for an entire level and mix it with deferred rendering :) per pixel global illumination + a bit of SSGI for dynamic objects :) :)
griffin2000
Posts: 22
Joined: Mon Nov 15, 2010 9:18 pm

Post by griffin2000 »

devsh wrote:wait... does it mean that I basically have to go through the environment map, pixel by pixel.... treating them all like diffuse parameters and unprojecting the envmap texcoords as normals/lightdir then adding them to THE 9 xyz/rgb values of a 2nd order spherical harmonic?

Then render a virtual envmap on a plane mapping texcoords as normals using the all mystifying lookup function and compare results?
Something like that.... Basically its a 3D (in spherical coodinates) equivalent of Fourier transform with the le Grange Polynomials (I think, may have got that name wrong) replacing the usual trig functions.
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

I hate you AAA people... you try to make games with dead easy techniques and then you go on about
equivalent of Fourier transform with the le Grange Polynomials
trying to make the thing sound complex and requiring a lot of maths
fmx

Post by fmx »

Its not the difference between AAA and simpletons, its the difference between knowing the lingo or not knowing it :wink:

Spherical Harmonics is primarily a scientific phenomenon which uses mathematics to describe atomic properties.

Much like physics and lighting-models, a simplified application of it for games makes it seem like a simple trick in the end, even though there is a lot more to it than you might guess just by looking at it in a gaming context
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

yh but tbh we are not interested in the LoT more of it.... all that crap just makes us confused... praise Tom Forsyth because he is the only one who's explanation made sense
macron12388
Posts: 126
Joined: Wed Sep 29, 2010 8:23 pm

Post by macron12388 »

fmx wrote:Its not the difference between AAA and simpletons, its the difference between knowing the lingo or not knowing it :wink:

Spherical Harmonics is primarily a scientific phenomenon which uses mathematics to describe atomic properties.

Much like physics and lighting-models, a simplified application of it for games makes it seem like a simple trick in the end, even though there is a lot more to it than you might guess just by looking at it in a gaming context
:) Or describing electron cloud properties? Don't forget that one, that one's a biggie in S.H.
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

I am having problems with converting cube maps to SH.... help?
I also posted a topic on gamedev with some example code

http://www.gamedev.net/community/forums ... _id=589393
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Post by Mel »

No, spherical harmonics are much simpler than that. They are simply functions mapped over a sphere. Just that.

The point is that these functions can be used to represent complex physical phenomena using simple numbers.

WARNING...LONG ASS EXPLANATION AHEAD.:P

In the case we are dealing with, the physical phenomenom to be described is the irradiance of a given point.

The irradiance is the amount of light that comes to a certain point. And it is complex to sample every point in space, and keep a trace of all of them to transform all those sample in complex formulae. But it was found that this problem could be much easier if the irradiance was represented as the sumatory of these spherical harmonics functions.

Same as using a Fourier transform to recreate a sound wave, these functions can be used to recreate a function over a sphere. Hence their name "harmonic" and "spherical", and the irradiance is a function over a sphere in its definition.

There are many spherical harmonics functions. a constant, X, Y, Z, (X^2-Y^2),.... And they have what it is called "order". This "order" is the maximum exponent an spherical harmonic function has. A constant has order 0, X, Y and Z have order 1, or are of first order,( XX-YY) has order 2 or it is of second order, and so on, in the functions that are spherical harmonics. Not all functions are.

It was then found that with a sumatory of these functions and their respective coeficients up to the second order was enough to represent the irradiance at a given point with an average error below 3% over the original calculations. And these calculations only needed 9 coeficients, which corresponded to the spherical harmonics functions up to the second order.

Code: Select all

order 0: a Constant:
-----------------------
order 1: maximum exponent = 1
y:
z:
x:
-----------------------
order2: maximum exponent = 2
xy:
yz:
xz:
3zz-1:
xx-yy:
-----------------------
Giving values to these fuctions (the coeficients c1-c9), and adding up all of them gives us a polynomical function.

Code: Select all

c1 + c2y +c3z +c4x + c5(xy) + c6(yz) + c7(xz) +c8(3zz-1) + c9(xx-yy)
Giving values to X, Y and Z (a 3d normalized vector) gives us the value of this function, which is that of the point in the "sphere" where that vector is pointing at.

Spherical harmonics are a form of representation, and can be seen as a vectorial space basis. The coeficients of these equations are 9-dimensional vectors.

Representing the incoming light in the form of SH becomes a matter of inverting the process. We have 9 coeficients to determine, and we have as input a vector and a light intensity.

Lets call the normalized vector XYZ and the intensity I.
Then, directly, without explanations...

Code: Select all

c1 = I
c2 = YI
c3 = ZI
c4 = XI
c5 = XYI
c6 = YZI
c7 = XZI
c8 = (3ZZ-1)I
c9 = (XX-YY)I
Then, also, in the problem we have at hand, apply another constants that were found so the problem mapped exactly to the irradiance. These constans are 5:

Code: Select all

k1=0.429043;
k2=0.511664;
k3=0.743125;
k4=0.886227;
k5=0.247708;
And the irradiance function becomes this.

Code: Select all

k4c1 + k2c2(y) + k2c3(z) + k2c4(x) + k1c5(xy) + k1c6(yz) + k1c7(xz) + k3c8(3zz-1) - k1c9(xx-yy) = I;
I hope this throws in a bit of knowledge of these odd 9-dimensional dudes...
Last edited by Mel on Mon Dec 06, 2010 11:48 am, edited 1 time in total.
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
ultramedia
Posts: 175
Joined: Wed Dec 20, 2006 12:04 pm

Post by ultramedia »

wibbly wobbly timey wimey...
Mel
Competition winner
Posts: 2292
Joined: Wed May 07, 2008 11:40 am
Location: Granada, Spain

Post by Mel »

I warned, damm it! now people starts having seizures because of reading it! :lol:
"There is nothing truly useless, it always serves as a bad example". Arthur A. Schmitt
devsh
Competition winner
Posts: 2057
Joined: Tue Dec 09, 2008 6:00 pm
Location: UK
Contact:

Post by devsh »

it's a very good explanation... too bad its a bit late... hope its useful to someone else... when I finish irrlicht XML material loaders I will get back on the spherical harmonix and release a demo where you shine a spotlight from a camera and affect the level lighting!
Post Reply