# Ray tracing the Next Week - Cornell Box

I'm so excited to learn ray tracing and did a lot of fun. But I'm stuck at rendering empty Cornell Box scene.  It has some darker color along the bright color and it's seem different with the image in the book. Sorry if my English confuses you because I'm still learning it.

• Hi, welcome to GDSE! Please post your solution as an answer to the question, instead of editing it into the question. This way others can find help more easily in the future, as they can see that the question has an answer!
– user35344
Feb 3, 2021 at 9:37
• @Tyyppi_77 thanks for remine me. I just edit my answer. This is first time I post so I don't have experiment. Feb 3, 2021 at 11:42
• For future reference, you might want to ask questions like this on the Computer Graphics StackExchange instead. Uses of raytracing and path tracing in games are still an emerging field, but better established in offline rendering. Feb 3, 2021 at 18:43
• @DMGregory Great site for discussing about Graphics Programming. Thanks for suggestion. Feb 4, 2021 at 1:16

I solved this.

The bright color is look like rectangle because the light that is reflected by Lambertian material is scattered randomly in unit cube (with center is the unit normal) . I fixed this by just allow ray is scattered randomly point in unit sphere.  Final result Example code :

Function to generate random vector

vec3 random(float min, float max)
{
return { Random(min, max), Random(min, max), Random(min, max) };
}


Code show generate random position in unit sphere, with using dot product to check the the vector is in radius of unit sphere, and reject it when vector is generated outside the unit sphere.

vec3 random_in_unit_sphere()
{
vec3 v = random(-1.0f, 1.0f);
while (dot(v, v) >= 1)
v = random(-1.0f, 1.0f);
return v;
}


Then from the hit point we just use normal vector of hit surface like the center of unit sphere, and add with random vector position in unit sphere. This random vector will work as direction vector that bend normal vector to direction of scatter ray.

scatter_ray.direction = hit_surface.normal + random_in_unit_sphere();

• Can you show the code you changed to make the scattering happen in a sphere instead of a cube? Feb 3, 2021 at 12:12
• @DMGregory I just add my code as expend answer. Sorry for my bad English. Every things I learned is from Ray tracing series github.com/RayTracing/raytracing.github.io. Feb 3, 2021 at 13:12

I guess u did something like

vec3 vec3::random_unit_vector()
{
auto x = drand(-1, 1);
auto y = drand(-1, 1);
auto z = drand(-1, 1);
return vec3::unit(vec3(x, y, z);
}


I came across this post because I made the same error, normalize an uniformly distributed random vector is wrong, each axis has to be normally distributed.

Personally I don't like the retry idea, so I use the cylinder surface mapping to get random vec3 on unit sphere surface

vec3 vec3::random_unit_vector()
{
// use cylinder surface mapping
auto theta = 2.0 * M_PI * drand(0, 1);
auto z = drand(-1, 1);
auto r = ::sqrt(1.0 - z * z);

return vec3
{
r * cos(theta),
r * sin(theta),
z
};
}


and

vec3 vec3::random_unit_sphere()
{
double u = drand();
return cbrt(u) * vec3::random_unit_vector();
}


to get random vector inside unit sphere