<p>Yes, in the simple case, primary rays conform to the frustum. If you're doing depth-of-field optically, then the rays don't quite conform to the frustum, because you need to vary the ray origins slightly as well as the directions. How exactly the variation works depends on how closely you're simulating the lens system and aperture. You can picture it as sampling from several frustums, one for each pinhole camera you're averaging.</p>

<p>I've never done full Monte Carlo for ray sampling: generating the rays according to a uniform distribution is bad at distributing them usefully around the image. Popular options include:</p>

<ul>
<li>You can generate the ray directions according to a space-filling curve on the image plane, and then adaptively supersample once you have enough samples to estimate contrast.</li>
<li>Start with a regular grid that goes through the centre of each pixel, but randomly jitter the directions within each pixel. This was popular for many years but (done naively) it's still slightly biased because you're too likely to sample the corners of pixels.</li>
<li>Start with a uniform random sampling but use Poisson discs to avoid putting rays too close together. This is closer to a pure random sampling, and it has the advantage you can keep using the technique as you refine/supersample the image by decreasing the disc radius over time.</li>
</ul>

CC BY-SA 3.0
2017-12-08T12:39:22.667Z
Note that DoF corresponds to combining pinhole cameras each having their own view frustum. So it still corresponds to a view frustum, but not "the" view frustum.
Matthias
CC BY-SA 3.0
2017-12-08T14:49:23.107Z
Yes, that's a nice way of looking at it.
Dan Hulme
CC BY-SA 3.0
2017-12-08T21:06:54.220Z
Thank you! So the "random" part of the algorithm only comes into effect when rays interact with diffuse surfaces?
Paul Ferris
CC BY-SA 3.0
2017-12-09T10:15:47.263Z
It's up to you really. If you purely sample on a space-filling curve, you don't need any randomness, but you can still jitter if you're worried about structured artefacts. If you jitter a regular grid, you'd definitely want to use random numbers for that. I forgot to mention that using Poisson discs is another possibility for structured random sampling; I'll add that to my answer.
Dan Hulme
CC BY-SA 3.0
2017-12-09T14:21:12.747Z
To be explicit: if you shoot all your primary rays from the same location in the same direction each sample, you'll have aliasing. Randomly jittering the start position by amounts smaller than the size of a pixel will give you antialiasing through "super sampling"
Alan Wolfe
CC BY-SA 3.0
2017-12-17T00:37:27.640Z
So the jitter is an AA technique, not something strictly related to lighting?
Paul Ferris
CC BY-SA 3.0
2017-12-17T10:10:42.463Z
@PaulFerris That's right, but *especially* in a full MC path tracer, you need to consider AA in your sampling strategy. If you're just getting started, you can ignore it for now but you'll have to come back to it later.
Dan Hulme
CC BY-SA 3.0
2017-12-17T20:08:07.200Z
Good to know.
My whole renderer runs through DirectCompute so supersampling is trivial (render with n-times more thread groups into an n-times larger buffer, average pixel neighbourhoods in post-processing); I was mostly just very confused about the "correct" way to organise my ray directions when they're emitted from the camera. Thanks for all the help :)
Paul Ferris
CC BY-SA 3.0
2017-12-08T12:13:33.037Z
2017-12-09T10:24:59.560Z
2017-12-09T10:24:59.560Z
Dan Hulme
<p>Do primary rays conform to the frustum? If they're randomly directed, is the randomness fitted to some distribution (as it would be for a diffuse BRDF? I'm building my existing raymarcher into a monte carlo path tracer and I'm really unsure about how to orient my rays before tracing actually starts.</p>

<pathtracing><monte-carlo>
Primary ray directions (MC Path tracer)
Paul Ferris
# Primary ray directions (MC Path tracer)

(Asked by: username_0 on 2017-12-08)

Do primary rays conform to the frustum? If they're randomly directed, is the randomness fitted to some distribution (as it would be for a diffuse BRDF? I'm building my existing raymarcher into a monte carlo path tracer and I'm really unsure about how to orient my rays before tracing actually starts.

**Tags:** pathtracing, monte-carlo


---

## 1 answers

### Answer by username_1 on 2017-12-08. Score: 1 | Accepted answer

Yes, in the simple case, primary rays conform to the frustum. If you're doing depth-of-field optically, then the rays don't quite conform to the frustum, because you need to vary the ray origins slightly as well as the directions. How exactly the variation works depends on how closely you're simulating the lens system and aperture. You can picture it as sampling from several frustums, one for each pinhole camera you're averaging.

I've never done full Monte Carlo for ray sampling: generating the rays according to a uniform distribution is bad at distributing them usefully around the image. Popular options include:

* You can generate the ray directions according to a space-filling curve on the image plane, and then adaptively supersample once you have enough samples to estimate contrast.
* Start with a regular grid that goes through the centre of each pixel, but randomly jitter the directions within each pixel. This was popular for many years but (done naively) it's still slightly biased because you're too likely to sample the corners of pixels.
* Start with a uniform random sampling but use Poisson discs to avoid putting rays too close together. This is closer to a pure random sampling, and it has the advantage you can keep using the technique as you refine/supersample the image by decreasing the disc radius over time.

> **Comments:**
> **username_2**: Note that DoF corresponds to combining pinhole cameras each having their own view frustum. So it still corresponds to a view frustum, but not "the" view frustum.  
> **username_1**: Yes, that's a nice way of looking at it.  
> **username_0**: Thank you! So the "random" part of the algorithm only comes into effect when rays interact with diffuse surfaces?  
> **username_1**: It's up to you really. If you purely sample on a space-filling curve, you don't need any randomness, but you can still jitter if you're worried about structured artefacts. If you jitter a regular grid, you'd definitely want to use random numbers for that. I forgot to mention that using Poisson discs is another possibility for structured random sampling; I'll add that to my answer.  
> **username_3**: To be explicit: if you shoot all your primary rays from the same location in the same direction each sample, you'll have aliasing. Randomly jittering the start position by amounts smaller than the size of a pixel will give you antialiasing through "super sampling"  
> **username_0**: So the jitter is an AA technique, not something strictly related to lighting?  
> **username_1**: @username_0 That's right, but *especially* in a full MC path tracer, you need to consider AA in your sampling strategy. If you're just getting started, you can ignore it for now but you'll have to come back to it later.  
> **username_0**: Good to know. My whole renderer runs through DirectCompute so supersampling is trivial (render with n-times more thread groups into an n-times larger buffer, average pixel neighbourhoods in post-processing); I was mostly just very confused about the "correct" way to organise my ray directions when they're emitted from the camera. Thanks for all the help :)  

---

