<p>There are two ways to 'translate' your object. The first is by moving each point of your object by the desired translation. The second is by translating the origin of your coordinate system. In this case it's the latter. Basically it turns out to be the same, whether you translate your object by a vector <span class="math-container">$\vec{t}$</span> or whether you translate your origin by <span class="math-container">$-\vec{t}$</span>.</p>

<p>For 2) note that you only care about the intersected points (since that is what you use to compute the pixel color), so you compute the offset only when you need it. So yes, you do translate the origin of the ray at each intersect, it's the same as translating the object though (at each ray intersect), since <span class="math-container">$\vec{p}+\vec{t} - \vec{o} = \vec{p} - (\vec{o}-\vec{t})$</span>.</p>

CC BY-SA 4.0
2019-02-22T16:11:00.223Z
Yes, that's my intuition. Note 1), when does this translation happen? I am shooting rays from the origin into each pixel, and at some point I 'move' my origin and shoot rays from the new origin. Does this happen only when I hit the class "translation"? Note 2) yes, but since I only offset when both original ray and new ray hit the object, only a small portion of the object will be moved. If the object goes from x=1 to x=4, and I move my origin from 0 to -2, the only portion of the object moved is between x=2 and x=4 (for x<2 the new ray does not hit the object)
maurock
CC BY-SA 4.0
2019-02-22T16:42:31.590Z
@MauroComi I don't get what you mean by 'hit the class translation'. You don't offset only a part of the object, you offset the ray origin, which is the same as offsetting the object. Note that if you translate an object, rays that previously intersected it, will not anymore - it's the same thing. What you should take away from this is that coordinate system translation by $-\vec{t}$ and object translation by $\vec{t}$ are the same thing. If you formulate it in terms of matrices, you can consider even more general transformations.
lightxbulb
CC BY-SA 4.0
2019-02-22T17:36:52.543Z
Thanks for our explanation. With "hit the class translation" I mean that I offset the origin inside the function `translate::hit()`, which means that this function is called when the original ray hits a Hitable object inside the class `translate`?
maurock
CC BY-SA 4.0
2019-02-22T18:42:00.010Z
@MauroComi Yeah as far as I can see it's just an interface to apply the transform before passing the transformed ray to the primitive hit function. People use a similar thing in sdf raymarching to distort the domain (they apply a nonlinear map to the basis).
lightxbulb
CC BY-SA 4.0
2019-02-22T15:51:29.927Z
2019-02-22T15:51:29.927Z
lightxbulb
<p>My question is not about the code, but it's about the approach used by the author Peter Shirley (and apparently common practice in ray tracers) of handling Instances. To understand the question it's necessary to be familiar with the book, because posting the code for every class involved is not feasible.
Something I don't understand is how rays behave. </p>

<p>Let's consider the <i>traslation</i>. The idea is to offset the origin, and check if the new ray - which is now shot from the new origin with the same direction of the original ray - hits the object. If so, we shift the hitting point by an equal and opposite offset.</p>

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

<p>Two questions:</p>

<p>1) Why is it not sufficient to shoot rays from the "standard" origin, and offset the hitting point without actually shifting the origin first? Example: the ray is shot, if it hits the object in point (x,y) I move the hitting point by 2 (x+2, y+2) and store it.</p>

<p>2) The hit function of the class <code>translate</code> is only called when the original ray hits the object? Because in that case, the translation would not result in the whole object, since it would only store those points that are first hit by the original ray, and for which the new ray hits the object again.</p>

<raytracing><shader><transformations><c++>
Ray tracing the next week: understanding Instances
maurock
# Ray tracing the next week: understanding Instances

(Asked by: username_0 on 2019-02-22)

My question is not about the code, but it's about the approach used by the author Peter Shirley (and apparently common practice in ray tracers) of handling Instances. To understand the question it's necessary to be familiar with the book, because posting the code for every class involved is not feasible.
Something I don't understand is how rays behave.

Let's consider the *traslation*. The idea is to offset the origin, and check if the new ray - which is now shot from the new origin with the same direction of the original ray - hits the object. If so, we shift the hitting point by an equal and opposite offset.

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

Two questions:

1) Why is it not sufficient to shoot rays from the "standard" origin, and offset the hitting point without actually shifting the origin first? Example: the ray is shot, if it hits the object in point (x,y) I move the hitting point by 2 (x+2, y+2) and store it.

2) The hit function of the class `translate` is only called when the original ray hits the object? Because in that case, the translation would not result in the whole object, since it would only store those points that are first hit by the original ray, and for which the new ray hits the object again.

**Tags:** raytracing, shader, transformations, c++


---

## 1 answers

### Answer by username_1 on 2019-02-22. Score: 1 | Accepted answer

There are two ways to 'translate' your object. The first is by moving each point of your object by the desired translation. The second is by translating the origin of your coordinate system. In this case it's the latter. Basically it turns out to be the same, whether you translate your object by a vector $\vec{t}$ or whether you translate your origin by $-\vec{t}$.

For 2) note that you only care about the intersected points (since that is what you use to compute the pixel color), so you compute the offset only when you need it. So yes, you do translate the origin of the ray at each intersect, it's the same as translating the object though (at each ray intersect), since $\vec{p}+\vec{t} - \vec{o} = \vec{p} - (\vec{o}-\vec{t})$.

> **Comments:**
> **username_0**: Yes, that's my intuition. Note 1), when does this translation happen? I am shooting rays from the origin into each pixel, and at some point I 'move' my origin and shoot rays from the new origin. Does this happen only when I hit the class "translation"? Note 2) yes, but since I only offset when both original ray and new ray hit the object, only a small portion of the object will be moved. If the object goes from x=1 to x=4, and I move my origin from 0 to -2, the only portion of the object moved is between x=2 and x=4 (for x<2 the new ray does not hit the object)  
> **username_1**: @MauroComi I don't get what you mean by 'hit the class translation'. You don't offset only a part of the object, you offset the ray origin, which is the same as offsetting the object. Note that if you translate an object, rays that previously intersected it, will not anymore - it's the same thing. What you should take away from this is that coordinate system translation by $-\vec{t}$ and object translation by $\vec{t}$ are the same thing. If you formulate it in terms of matrices, you can consider even more general transformations.  
> **username_0**: Thanks for our explanation. With "hit the class translation" I mean that I offset the origin inside the function `translate::hit()`, which means that this function is called when the original ray hits a Hitable object inside the class `translate`?  
> **username_1**: @MauroComi Yeah as far as I can see it's just an interface to apply the transform before passing the transformed ray to the primitive hit function. People use a similar thing in sdf raymarching to distort the domain (they apply a nonlinear map to the basis).  

---

