Here are some notes I made a while back which should be of some help. The idea here was to be able to specify a point on a sphere, and then find random points around that point that had a uniform distribution. One's first instinct would be to just use polar coordinates to calculate these points, but doing so results in pole clustering.
What's pole clustering?
If you imagine a camera pointing 45 degree up, and you turn, taking a photo every 45 degrees, and then point it straight up and do the same thing, you'll understand that the center of all the images on the second set are actually the same point. That's pole clustering. If you use polar coordinates with the center point being one of the poles, then you'll get a lot more points around the central point than out a ways from it.
Anyway here's the algorithm which avoids the pole clustering issue:
Finding uniform random points around a point on a sphere:
---------------------------------------------------------
Find a point on the unit sphere:
P = (a,b,c)
The first step is to find a point N on the unit sphere which is 90 degrees (arc length) away.
N = (a/sqrt(a^2+b^2) , -b/sqrt(a^2+b^2), 0)
will work unless a=b=0.
now let T = PxN. so P,N,T are at right angles to each other
now let's see. we want to parameterize the circle of radius r centered at P
R = P*cos(r) + N*sin(r)*cos(t) + T*sin(r)*sin(t) should work
pick a random angle t between 0 and 360 and you have a random point in the circle
So in sphere space using P(0,0,1) N(0,1,0) T(1,0,0):
r = radius from point at 0,0,1
t = random number between 0 and 360
Rx = sin(r)*sin(t)
Ry = sin(r)*cos(t)
Rz = cos(r)
So "point on the unit sphere" = Your surface normal.
Radius = Random number from 0 to 180, for a hemisphere centered on the tip of your normal.
T = Random number from 0 to 360.
You'll have to figure out how that works into the stuff above cause I have to go eat dinner and I wrote this so long ago I forget how you calculate it but it's all there in the notes. :-)
What's pole clustering?
If you imagine a camera pointing 45 degree up, and you turn, taking a photo every 45 degrees, and then point it straight up and do the same thing, you'll understand that the center of all the images on the second set are actually the same point. That's pole clustering. If you use polar coordinates with the center point being one of the poles, then you'll get a lot more points around the central point than out a ways from it.
Anyway here's the algorithm which avoids the pole clustering issue:
Finding uniform random points around a point on a sphere:
---------------------------------------------------------
Find a point on the unit sphere:
P = (a,b,c)
The first step is to find a point N on the unit sphere which is 90 degrees (arc length) away.
N = (a/sqrt(a^2+b^2) , -b/sqrt(a^2+b^2), 0)
will work unless a=b=0.
now let T = PxN. so P,N,T are at right angles to each other
now let's see. we want to parameterize the circle of radius r centered at P
R = P*cos(r) + N*sin(r)*cos(t) + T*sin(r)*sin(t) should work
pick a random angle t between 0 and 360 and you have a random point in the circle
So in sphere space using P(0,0,1) N(0,1,0) T(1,0,0):
r = radius from point at 0,0,1
t = random number between 0 and 360
Rx = sin(r)*sin(t)
Ry = sin(r)*cos(t)
Rz = cos(r)
So "point on the unit sphere" = Your surface normal.
Radius = Random number from 0 to 180, for a hemisphere centered on the tip of your normal.
T = Random number from 0 to 360.
You'll have to figure out how that works into the stuff above cause I have to go eat dinner and I wrote this so long ago I forget how you calculate it but it's all there in the notes. :-)