<pre><code>Sphere(1e5, Vec(50,40.8,-1e5+170), Vec(),Vec(),           DIFF),//Frnt
</code></pre>
<p>The sphere with index 3 would intersect each ray if the ray origin would not be shifted forward.</p>
<p>You can easily verify this by returning surface normals from the radiance and not shifting the ray origin. (or just remove this one sphere). It also will look like a tunnel behind the camera ;)</p>

CC BY-SA 4.0
2021-06-08T18:45:37.777Z
Oh, thank you, so it was much simpler than I thought. I ended up borrowing parts from "Ray Tracing In One Weekend" for the camera anyway because I wasn't comfortable with using the one that comes with smallpt.
Alexandru Ica
CC BY-SA 4.0
2021-06-07T17:07:04.067Z
2021-06-07T23:31:58.770Z
2021-06-07T23:31:58.770Z
Marcus Borkenhagen
<p>In <a href="https://www.kevinbeason.com/smallpt/" rel="nofollow noreferrer">smallpt's</a> source at line 90 the direction is multiplied by <code>140</code> and I was wondering where that number is coming from. I was hoping for it to be explained in the <a href="https://drive.google.com/file/d/0B8g97JkuSSBwUENiWTJXeGtTOHFmSm51UC01YWtCZw/view" rel="nofollow noreferrer">slides</a> attached to the source but I couldn't find an explanation for where that number is coming from.</p>
<p>If I increase the number(to <code>160.0</code> for example) it starts giving some kind of weird zoom-out effect:
<a href="https://i.sstatic.net/o9XWE.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/o9XWE.jpg" alt="enter image description here" /></a></p>
<p>And if I decrease the number(in this example to <code>125.0</code>) I get this partially black picture:
<a href="https://i.sstatic.net/7KkJ2.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7KkJ2.png" alt="enter image description here" /></a></p>
<p>As I understand it, after I've simplified smallpt a little bit, in the <code>main</code> function we get a random point in each subpixel normalized by the width/height between <code>-0.5</code> and <code>0.5</code>, and then calculate the new ray direction by multiplying the x and y axis by these offsets, then adding everything together to obtain the new direction, something like this:</p>
<pre><code>fov_scale = 2.0 * tan(vertical_fov / 2.0)
cam_x_axis = vec3(aspect_ratio * fov_scale, 0, 0)
cam_y_axis = normalize(cross(cam_x_axis, cam_direction))

for x, y:
    for sx, sy, samps:
        // not using smallpt's tent filter
        x_in_subpixel = x + sx * 0.5 + erand48(Xi) * 0.5
        y_in_subpixel = y + sy * 0.5 + erand48(Xi) * 0.5
        // normalize them between -0.5 and 0.5
        new_x = x_in_subpixel / w - 0.5
        new_y = y_in_subpixel / h - 0.5

        offset_x = cam_x_axis * new_x
        offset_y = cam_y_axis * new_y
        new_direction = cam_direction + offset_x + offset_y
        // here we can see that magic number
        new_ray = ray(cam_position + new_direction * 140, normalize(new_direction))
</code></pre>
<p>So my questions are:</p>
<ul>
<li>What does this number mean?</li>
<li>Can I calculate it programmatically to be exactly what is expected(maybe depending on the field of view, width/height of the image)?</li>
</ul>

CC BY-SA 4.0
2021-03-06T18:48:13.387Z
It's a rescaling of the virtual film.
lightxbulb
CC BY-SA 4.0
2021-03-06T20:26:46.560Z
@lightxbulb And how is it calculated? I mean why 140?
Alexandru Ica
CC BY-SA 4.0
2021-03-06T22:24:31.280Z
It's just fitted to the specific scene as far as I can tell. Likely by trial and error.
lightxbulb
CC BY-SA 4.0
2021-03-07T11:24:43.953Z
@lightxbulb Thanks for commenting, I ended up replacing the code for the camera entirely(with inspiration from "Ray Tracing in One Weekend"), I really wanted to get rid of all of these constants!
Alexandru Ica
<raytracing><pathtracing><camera>
Magic number meaning in smallpt's camera ray calculation
Alexandru Ica
# Magic number meaning in smallpt's camera ray calculation

(Asked by: username_0 on 2021-03-06)

In [smallpt's](https://www.kevinbeason.com/smallpt/) source at line 90 the direction is multiplied by `140` and I was wondering where that number is coming from. I was hoping for it to be explained in the [slides](https://drive.google.com/file/d/0B8g97JkuSSBwUENiWTJXeGtTOHFmSm51UC01YWtCZw/view) attached to the source but I couldn't find an explanation for where that number is coming from.

If I increase the number(to `160.0` for example) it starts giving some kind of weird zoom-out effect:
[![enter image description here](https://i.sstatic.net/o9XWE.jpg)](https://i.sstatic.net/o9XWE.jpg)

And if I decrease the number(in this example to `125.0`) I get this partially black picture:
[![enter image description here](https://i.sstatic.net/7KkJ2.png)](https://i.sstatic.net/7KkJ2.png)

As I understand it, after I've simplified smallpt a little bit, in the `main` function we get a random point in each subpixel normalized by the width/height between `-0.5` and `0.5`, and then calculate the new ray direction by multiplying the x and y axis by these offsets, then adding everything together to obtain the new direction, something like this:

```
fov_scale = 2.0 * tan(vertical_fov / 2.0)
cam_x_axis = vec3(aspect_ratio * fov_scale, 0, 0)
cam_y_axis = normalize(cross(cam_x_axis, cam_direction))

for x, y:
    for sx, sy, samps:
        // not using smallpt's tent filter
        x_in_subpixel = x + sx * 0.5 + erand48(Xi) * 0.5
        y_in_subpixel = y + sy * 0.5 + erand48(Xi) * 0.5
        // normalize them between -0.5 and 0.5
        new_x = x_in_subpixel / w - 0.5
        new_y = y_in_subpixel / h - 0.5

        offset_x = cam_x_axis * new_x
        offset_y = cam_y_axis * new_y
        new_direction = cam_direction + offset_x + offset_y
        // here we can see that magic number
        new_ray = ray(cam_position + new_direction * 140, normalize(new_direction))

```

So my questions are:

* What does this number mean?
* Can I calculate it programmatically to be exactly what is expected(maybe depending on the field of view, width/height of the image)?

**Tags:** raytracing, pathtracing, camera

> **Comments:**
> **username_1**: It's a rescaling of the virtual film.  
> **username_0**: @username_1 And how is it calculated? I mean why 140?  
> **username_1**: It's just fitted to the specific scene as far as I can tell. Likely by trial and error.  
> **username_0**: @username_1 Thanks for commenting, I ended up replacing the code for the camera entirely(with inspiration from "Ray Tracing in One Weekend"), I really wanted to get rid of all of these constants!  

---

## 1 answers

### Answer by username_2 on 2021-06-07. Score: 0 | Accepted answer

```
Sphere(1e5, Vec(50,40.8,-1e5+170), Vec(),Vec(),           DIFF),//Frnt

```

The sphere with index 3 would intersect each ray if the ray origin would not be shifted forward.

You can easily verify this by returning surface normals from the radiance and not shifting the ray origin. (or just remove this one sphere). It also will look like a tunnel behind the camera ;)

> **Comments:**
> **username_0**: Oh, thank you, so it was much simpler than I thought. I ended up borrowing parts from "Ray Tracing In One Weekend" for the camera anyway because I wasn't comfortable with using the one that comes with smallpt.  

---

