Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

uniform hemisphere sampling

Started by cignox1 May 27, 2009 at 4:46 PM 0 replies 2.4k views
Original Post
cignox1
cignox1
Hi, I wasn't able to find what I need, so I thought to try here. I need to uniformly sample a hemisphere, and I went as follows:

		static inline const vector4<real> SampleUniformHemisphere(const vector2<real> &sample)
		{
			real phi = Q_2PI * sample.x;
			real theta = (Q_PI / real(2)) * sample.y;
		
			return vector4<real>::FromSpherical(theta, phi, real(1));
		}

All the formulas I've found do the conversion spherical->cartesian togheter with the phi and theta calculation, so they mix everything and I can't figure out if my code is correct or not. Could someone more skilled confirm that this code does the sampling right? In addition, if I wanted to limit the sampling range (providing a max theta), I just need to change the code as follows, right?

		static inline const vector4<real> SampleUniformHemisphereLobe(const vector2<real> &sample, real maxTheta)
		{
			real phi = Q_2PI * sample.x;
			real theta = maxTheta * sample.y;
		
			return vector4<real>::FromSpherical(theta, phi, real(1));
		}

Thank you!
cignox1
cignox1
After another long search session, and a bit more thinking, I've come to this formula:

static inline const vector4<real> SampleUniformHemisphere(const vector2<real> &sample)		{			real phi = Q_2PI * sample.x;			real theta = acos(sample.y);					return vector4<real>::FromSpherical(theta, phi, real(1));		}


I suppose that then the equations to limit the theta range becomes:

static inline const vector4<real> SampleUniformHemisphereLobe(const vector2<real> &sample, real maxTheta)		{real maxc = cos(maxTheta);			real phi = Q_2PI * sample.x;			real theta = acos(real(1) - (sample.y * maxc));					return vector4<real>::FromSpherical(theta, phi, real(1));		}


Anyone can confirm or negate that?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.