 #include <curand_kernel.h>
 
 
+#define PI 3.14159265359f
 #define TWO_PI 6.283185307f
+#define PI_DIV_TWO 1.57079632679f
 #define INV_SQRT_THREE 0.5773502691896257645091487805019574556476f 
 
 __device__ float3 CalculateRayDirectionFromPixel(uint x, uint y, uint width, uint height, DeviceCamera &camera, curandState *randState) {



/**
 * Creates a uniformly random direction in the hemisphere defined by the normal  
 *
 * Based off the algorithm and code in 'Physically Based Rendering, 2nd Ed.' - Pharr & Humphreys
 * Chapter 13 - Example 13.6.1 - Pg. 664
 *
 * @param normal        The normal that defines the hemisphere
 * @param randState     The random state to use for internal random number generation        
 * @return              A uniformly random direction in the hemisphere
 */
__device__ float3 CreateUniformDirectionInHemisphere(float3 normal, curandState *randState) {
	// Create a random coordinate in spherical space
	float z = curand_uniform(randState);
	float r = sqrt(1.0f - z * z);
	float phi = TWO_PI * curand_uniform(randState);
	
	// Find an axis that is not parallel to normal
	float3 majorAxis;
	if (abs(normal.x) < INV_SQRT_THREE) { 

	float3 v = cross(normal, u);
	float3 w = normal;

	// Create random coordinates in the local coordinate system
	float rand = curand_uniform(randState);
	float r = sqrt(rand);
	float theta = curand_uniform(randState) * TWO_PI; 
	
	float x = r * cos(theta);
	float y = r * sin(theta);
    

	// Transform from local coordinates to world coordinates
    return normalize(u * x +
	                 v * y +
	                 w * sqrt(fmaxf(0.0f, 1.0f - rand)));
}
