<pre><code>vec3 target = rec.p + rec.normal + random_in_unit_sphere();
return 0.5*color(ray(rec.p, target-rec.p), world);
</code></pre>

<p>this is the exact same as </p>

<pre><code>vec3 targetDir = rec.normal + random_in_unit_sphere();
return 0.5*color(ray(rec.p, targetDir), world);
</code></pre>

<p>In other words the sphere is a way to pick a random direction which is biased towards the normal direction without doing a lot of fancy maths.</p>

<p>And yeah if that hits again it will repeat the process again and again until it hits the sky box. this can be avoided by adding a parameter to avoid unlimited recursion. </p>

<p>However that function is bad in general for a few reasons:</p>

<ol>
<li><p>The color on hit should depend on the object's material (the color) and the relation between the normal vector the incoming eye ray and the outgoing light ray.</p></li>
<li><p>The light sky box is hardcoded to be a gradiant.</p></li>
<li><p>only a single recursive sample on each hit. This gives very grainy images and requires a lot of samples per pixel to converge to a proper image.</p></li>
</ol>

CC BY-SA 3.0
2017-04-10T10:54:42.507Z
Since the objective of the chapter is to implement Lambert could you suggest an alternative implementation? Where does the grey color from the sphere come from, is it set anywhere?
Arjan Singh
CC BY-SA 3.0
2017-04-10T10:36:13.927Z
2017-04-10T10:36:13.927Z
ratchet freak
<p>Good answer above.    (I like that the book adds in p then subtracts it.)</p>

<p>This is a hard-coded grey world as a "hello world" kind of ray tracer.   The 0.5 is the hard-coded reflectance.   Keep working through book and the object reflectance and max-recursion will be addressed.   Ray branching I avoid like the plague.   In cost benefit I am always a skeptic on ray trees versus ray paths.</p>

CC BY-SA 3.0
2017-04-10T18:29:00.203Z
2017-04-10T18:29:00.203Z
Peter Shirley
<p>I am currently reading through Peter Shirley's <a href="https://www.amazon.in/Ray-Tracing-Weekend-Minibooks-Book-ebook/dp/B01B5AODD8" rel="nofollow noreferrer">Ray Tracing in One Weekend</a>. In the beginning chapters where the author introduces diffuse surfaces we a presented with this function:</p>

<pre><code>vec3 color(const ray&amp; r, hitable *world)
{
hit_record rec;
if(world-&gt;hit(r, 0.0, MAXFLOAT,rec))
  {
   vec3 target = rec.p + rec.normal + random_in_unit_sphere();
   return 0.5*color(ray(rec.p, target-rec.p), world);
  }
else
  {
   vec3 unit_direction = unit_vector(r.direction());
   float t = 0.5*(unit_direction.y() + 1.0);
   return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
  }
}
</code></pre>

<p>Can somebody where the actual definition for the function is? Because in the first part of the <code>if</code>the return <code>value</code> is <code>0.5*color(ray(rec.p, target-rec.p), world);</code>. Will it execute the function again except go through the <code>else</code>? </p>

<p>Regarding what the function actually does here is what I have gathered from the book based on my current understanding:</p>

<ol>
<li>Pick a random point s from the unit radius sphere that is tangent to the hitpoint, and send a
ray from the hitpoint p to the random point s. That sphere has center (p+N)</li>
</ol>

<p><a href="https://i.sstatic.net/BmPFx.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/BmPFx.png" alt="enter image description here"></a></p>

<ol start="2">
<li>Which then gives us:</li>
</ol>

<p><a href="https://i.sstatic.net/edqsj.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/edqsj.png" alt="enter image description here"></a></p>

<raytracing><c++><vectors>
Can somebody explain this Ray Tracing Function?
Arjan Singh
# Can somebody explain this Ray Tracing Function?

(Asked by: username_0 on 2017-04-10)

I am currently reading through username_2's [Ray Tracing in One Weekend](https://www.amazon.in/Ray-Tracing-Weekend-Minibooks-Book-ebook/dp/B01B5AODD8). In the beginning chapters where the author introduces diffuse surfaces we a presented with this function:

```
vec3 color(const ray& r, hitable *world)
{
hit_record rec;
if(world->hit(r, 0.0, MAXFLOAT,rec))
  {
   vec3 target = rec.p + rec.normal + random_in_unit_sphere();
   return 0.5*color(ray(rec.p, target-rec.p), world);
  }
else
  {
   vec3 unit_direction = unit_vector(r.direction());
   float t = 0.5*(unit_direction.y() + 1.0);
   return (1.0-t)*vec3(1.0, 1.0, 1.0) + t*vec3(0.5, 0.7, 1.0);
  }
}

```

Can somebody where the actual definition for the function is? Because in the first part of the `if`the return `value` is `0.5*color(ray(rec.p, target-rec.p), world);`. Will it execute the function again except go through the `else`?

Regarding what the function actually does here is what I have gathered from the book based on my current understanding:

1. Pick a random point s from the unit radius sphere that is tangent to the hitpoint, and send a
   ray from the hitpoint p to the random point s. That sphere has center (p+N)

[![enter image description here](https://i.sstatic.net/BmPFx.png)](https://i.sstatic.net/BmPFx.png)

2. Which then gives us:

[![enter image description here](https://i.sstatic.net/edqsj.png)](https://i.sstatic.net/edqsj.png)

**Tags:** raytracing, c++, vectors


---

## 2 answers

### Answer by username_1 on 2017-04-10. Score: 3 | Accepted answer

```
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
return 0.5*color(ray(rec.p, target-rec.p), world);

```

this is the exact same as

```
vec3 targetDir = rec.normal + random_in_unit_sphere();
return 0.5*color(ray(rec.p, targetDir), world);

```

In other words the sphere is a way to pick a random direction which is biased towards the normal direction without doing a lot of fancy maths.

And yeah if that hits again it will repeat the process again and again until it hits the sky box. this can be avoided by adding a parameter to avoid unlimited recursion.

However that function is bad in general for a few reasons:

1. The color on hit should depend on the object's material (the color) and the relation between the normal vector the incoming eye ray and the outgoing light ray.
2. The light sky box is hardcoded to be a gradiant.
3. only a single recursive sample on each hit. This gives very grainy images and requires a lot of samples per pixel to converge to a proper image.

> **Comments:**
> **username_0**: Since the objective of the chapter is to implement Lambert could you suggest an alternative implementation? Where does the grey color from the sphere come from, is it set anywhere?  

---

### Answer by username_2 on 2017-04-10. Score: 4

Good answer above. (I like that the book adds in p then subtracts it.)

This is a hard-coded grey world as a "hello world" kind of ray tracer. The 0.5 is the hard-coded reflectance. Keep working through book and the object reflectance and max-recursion will be addressed. Ray branching I avoid like the plague. In cost benefit I am always a skeptic on ray trees versus ray paths.


---

