Search this blog

23 July, 2009

Analytic diffuse shading

No big news yet, I've been studying a little bit analytic methods for shading and occlusion, I can't report anything really now, even because I'm not yet satisfied with what I've done.

But I'd like to share this link:, differential element to finite area is what you’ll need.

Also, you might find this useful, if you're starting to play around with spherical harmonics, it's a small snipped of what I've been doing, with Mathematica:
(* analytic solution for real spherical harmonics test *)

shIndices[level_] := (Range[-#1, #1] & ) /@ Range[0, level]
shGetNormFn[l_, m_] := Sqrt[((2*l + 1)*(l - m)!)/(4*Pi*(l + m)!)]
shGetFn[l_, m_] :=
Piecewise[{{shGetNormFn[l, 0]*LegendreP[l, 0, Cos[\[Theta]]],
m == 0}, {Sqrt[2]*shGetNormFn[l, m]*Cos[m*\[Phi]]*
LegendreP[l, m, Cos[\[Theta]]],
m > 0}, {Sqrt[2]*shGetNormFn[l, -m]*Sin[(-m)*\[Phi]]*
LegendreP[l, -m, Cos[\[Theta]]], m <>
shFunctions[level_] :=
Function[{list, currlevel}, (shGetFn[currlevel - 1, #1] & ) /@
list], shIndices[level]]
shGenCoeffs[shfns_, fn_] :=
Map[Integrate[#1*fn[\[Theta], \[Phi]]*Sin[\[Theta]], {\[Theta], 0,
Pi}, {\[Phi], 0, 2*Pi}] & , shfns, {2}]
shReconstruct[shfns_, shcoeffs_] :=
Simplify[Plus @@ (Flatten[shcoeffs]*Flatten[shfns]),
Assumptions -> {Element[\[Theta], Reals],
Element[\[Phi], Reals], \[Theta] >= 0, \[Phi] >= 0, \[Theta] <=
Pi, \[Phi] <= 2*Pi}]

shIsZonal[shcoeffs_, level_] :=
Plus @@ (Flatten[shIndices[level]] Flatten[shcoeffs]) == 0
shGetSymConvolveNorm[level_] :=
Function[{list, currlevel},
Table[Sqrt[(4 \[Pi])/(2 currlevel + 1)], {Length[list]}]],
shGetSymCoeffs[shcoeffs_] :=
Table[#1[[Ceiling[Length[#1]/2]]], {Length[#1]}] & /@ shcoeffs
shSymConvolve[shcoeffs_, shsymkerncoeffs_,
level_] := (Check[shIsZonal[shsymkerncoeffs], err];
shGetSymConvolveNorm[level] shcoeffs shGetSymCoeffs[

(* tests.... *)

testnumlevels = 2
testfn[a_, b_] :=
Cos[a]^10*UnitStep[Cos[a]] (*symmetric on the z axis*)
(*testfn[a_,b_]:= (a/Pi)^4*)
shfns = shFunctions[testnumlevels]
testfncoeffs = shGenCoeffs[shfns, testfn]
shIsZonal[testfncoeffs, testnumlevels]
testfnrec = {\[Theta], \[Phi]} \[Function]
Evaluate[shReconstruct[shfns, testfncoeffs]]
SphericalPlot3D[{testfn[\[Theta], \[Phi]],
testfnrec[\[Theta], \[Phi]]}, {\[Theta], 0, Pi}, {\[Phi], 0, 2 Pi},
Mesh -> False, PlotRange -> Full]

testfn2[a_, b_] := UnitStep[Cos[a] Sin[b]](*asymmetric*)
testfn2coeffs = shGenCoeffs[shfns, testfn2]
testfn3coeffs =
shSymConvolve[testfn2coeffs, testfncoeffs, testnumlevels]
testfn2rec = {\[Theta], \[Phi]} \[Function]
Evaluate[shReconstruct[shfns, testfn2coeffs]]
testfn3rec = {\[Theta], \[Phi]} \[Function]
Evaluate[shReconstruct[shfns, testfn3coeffs]]
SphericalPlot3D[{testfn2[\[Theta], \[Phi]],(*testfn2rec[\[Theta],\
\[Phi]],*)testfn3rec[\[Theta], \[Phi]]}, {\[Theta], 0, Pi}, {\[Phi],
0, 2 Pi}, Mesh -> False, PlotRange -> Full]


Anonymous said...


I'm playing around with spot lights but I don't have mathematica I use the free opensource maxima.

Can you explain what test suite you set up?

Anonymous said...

uhh I meant spherical harmonics - I wrote spot lights because my coworker said that as I was typing the comment :P

DEADC0DE said...

Wolfram offers a 30 day trial version of Mathematica that you can use. The functions I posted are just the bare minimum for SH, I wrote the projection, the convolution and the reconstruction functions, but done analytically.

Anonymous said...

I just got mathematica. Unfortunately, the code doesn't run because of syntax errors. It also lost all of its formating so deciphering it is a nightmare to a new mathematica user.

I'm going to play with it tonight and try and get it working though.

DEADC0DE said...

The code should have no errors, probably pasting it into or from the blog added a few carriage returns at the end of the lines. Check the lines you've pasted...

Also Mathematica can convert from one visualization format to another, just select the text you've entered and on the right mouse button menu you have the options to convert to a better format.

Anonymous said...
This comment has been removed by a blog administrator.