# How Do I Raycast for a Spherical Harmonic Shader Using the iOS GPU?

I am beginning an Spherical Harmonics shader project for an iOS app I am writing. I have begun by reading this excellent in-depth paper on the subject (PDF) - http://bit.ly/aQmax3.

The paper describes a scene pre-processing step that involves ray-casting (one bounce, shadow feeler). Can someone describe how ray-casting can be performed using GLSL on iOS?

-
What is it that you want to use the spherical harmonics for? Is it just for lighting? – OriginalDaemon Apr 25 '12 at 18:56
That's an indepth paper on the subject? It simply pops out equations with 0 effort to justify them. It's simple to understand for someone who has previous experience with probability, integration, function reconstruction/approximation (Fourier series at least), but for someone who would indeed like the gritty details, it's really a failure to illustrate such concepts. Especially for someone whose notions of the lighting equation is the allmighty dot product, which he mentions briefly as the common lighting model at the time (and still is, but that paper is from ~2003.) – Domagoj Pandža Apr 27 '12 at 1:46
That is the best introductory paper on the subject. There is also an implementation here and a section in a book called "Advanced Lighting and Materials with Shaders" – bobobobo Jul 4 '12 at 14:51
Also note this paper – bobobobo Aug 10 '12 at 20:07

The raycasting is a preprocess. It has nothing to do with GLSL.

You raycast to find ambient occlusion, really. Where a ray does not strike any geometry, it is "open to ambient lighting" from that direction.

What Green is trying to do on page 26 is estimate the ambient occlusion function in spherical harmonics. To do so he first generates a "sphere of rays" in every which direction (page 7). He evaluates the SH function on each (l,m) band and saves that in a data structure coupled with the ray direction.

### Want: To estimate the ambient occlusion function at VERTEX in spherical harmonics

To estimate the ambient occlusion function at VERTEX in SH, you cast all the rays in the "sphere of rays collection". Call an example ray `RAY`. Where `RAY` DOESN'T strike anything, it should be used to estimate the ambient occlusion function at VERTEX. So, you add a scaled copy of the SH coefficients of RAY to the SH approx to of the ambient occlusion function at VERTEX. The scaled of the SH coefficients of RAY is the dot product of RAY with the VERTEX's normal.

-

Ray casting which therefore is ray tracing is very very expensive. If you want it in real-time, that would be impossible or only possible within very restricted scene.

I do not recommend calculating spherical harmonics for general scene in iOS. Alternatively, you can pre-calculate spherical harmonics values via PC and use them in iOS such as sh light map(or sh value for each vertex).

The method for ray casting is same as ray tracing. You can find much references and sources codes about it.

-
If you really want ray casting(ray tracing) within glsl shader, it is POSSIBLE to implement it with glsl shader by the way. But this way differs from the way you use glsl shader in a standard way. – Hybrid Jul 4 '12 at 15:18
Not even close to impossible. I have it working on the 360 via XNA, real time in a single pass. Nontrivial, but very possible. – David Lively Jul 4 '12 at 18:10
You're right. But I meant it is almost impossible in practically. You can do it with sphere, or very restricted scene. But, in practically, I don't think ray tracing can be used for games in iOS. – Hybrid Jul 5 '12 at 18:29
I don't know about iOS, but my implementation doesn't have any restrictions on scene complexity. Render time for a fixed size frame buffer is basically constant, and a function of the depth of your geometry tree, ray step algorithm and number of pixels to draw. – David Lively Jul 6 '12 at 14:57
I'm not sure this actually answers or addresses the question at all, it should perhaps be a comment. – Josh Petrie Oct 16 '12 at 15:53