Path tracing: How to ensure we are sampling a direction vector within the visible hemisphere of a surface?

Given a surface Normal vector of an intersection point, how can one generate a random direction vector that is guaranteed to be in the visible hemisphere? The way I am currently doing this is to sample a random direction vector d and dot it with the Normal and keep that vector if the dot product is greater than 0. This method is not guaranteed to generate a vector in the visible hemisphere of BRDF as ~50% of the random vectors would have a 0 or negative dot product result.

I saw somewhere that there is a way to transform the randomly generated vector d to place it in the visible hemisphere using a transformation matrix but I don't know how to do it. Can someone write a [pseudo]code for how one generate a direction vector using the transformation method?

If it’s on the wrong side of the normal, you don’t have to throw it away—negating it will give you a vector that’s in the visible hemisphere.

Answering your comment, to get a vector that’s within some angle θ of the normal, this should work (GLSL):

vec3 direction(vec3 normal, vec2 randomValues, float maxTheta) {
// pick an orthogonal tangent vector using the method described here: http://lolengine.net/blog/2013/09/21/picking-orthogonal-vector-combing-coconuts
vec3 tangent = (normal.x > normal.z) ? vec3(-normal.y, normal.x, 0.0) : vec3(0.0, -normal.z, normal.y);
// and a bitangent vector orthogonal to both
vec3 bitangent = cross(normal, tangent);

float phi = 2.0 * PI * randomValues.x;
float theta = randomValues.y * maxTheta;
float sinTheta = sin(theta);
return tangent * cos(phi) * sinTheta + bitangent * sin(phi) * sinTheta + normal * cos(theta);
}

• Thanks. I think that's a super simply and effective way of doing what I explained. However, what if the goal is to generate random direction vectors that are within θ degrees of the Normal?
– Amir
Feb 2 '21 at 21:06
• Added a way to do that to the answer. Feb 2 '21 at 21:38