<p>Let the horizontal FOV be given as theta (in radians), and the vertical FOV be given as phi (in radians). The we know that:</p>

<p><span class="math-container">$$\tan\frac{\theta}{2}=\frac{w}{f}, \tan\frac{\phi}{2}=\frac{h}{f}$$</span></p>

<p>Let us pick <span class="math-container">$f = 1$</span>, then <span class="math-container">$w = \tan\frac{\theta}{2}, h = \tan\frac{\phi}{2}$</span>. Now your ray generation direction generation formula looks like:</p>

<p><span class="math-container">$$\pmb{d} = x w\pmb{u} + yh\pmb{v} + \pmb{w}$$</span></p>

<p>Where <span class="math-container">$\pmb{u},\pmb{v}, \pmb{w}$</span> are your sx,sy, forward vectors. Essentially, I found the extent of the smallest/largest x and y respectively (<span class="math-container">$1w = w, -1w=-w$</span>, etc.). You can premultiply <span class="math-container">$\pmb{u}, \pmb{v}$</span> with <span class="math-container">$w, h$</span> respectively, if you don't want to perform the multiplication per ray.</p>

CC BY-SA 4.0
2019-10-30T14:53:26.653Z
hm I am still having trouble recreating the last reference image provided for this task. this is how I am doing my ray now, but that doesnt seem to work for the last image ( probably missing something ): `sx=norm(cross(forward, up)) * tan(horizontal/2)`, `sy=norm(cross(forward, sx)) * tan(vertical/2)` ( up may not be orthogonal to forward ), `r(t)=origin + t * norm(x*sx + y*sy + forward)`. Am I missing something?
Yamahari
CC BY-SA 4.0
2019-10-30T16:35:18.623Z
@Yamahari Looks correct. Maybe your fov us given in half angles? Then remove the `/2`. Also is forward normalized?
lightxbulb
CC BY-SA 4.0
2019-10-30T17:14:54.270Z
i doubt the angle is only half, for the last image the given vertical/horizontal angles are pi * 0.9. Yes the forward vector is normalized
Yamahari
CC BY-SA 4.0
2019-10-30T17:24:31.753Z
@Yamahari Then all should be fine, there is probably a different reason you do not get the reference.
lightxbulb
CC BY-SA 4.0
2019-10-30T17:42:52.943Z
oh man I just found out what happened, I was too stupid to use c++ correctly xD thx
Yamahari
CC BY-SA 4.0
2019-10-27T22:37:55.493Z
2019-10-27T22:43:05.430Z
2019-10-27T22:43:05.430Z
lightxbulb
<p>Given the up and focal vector of the perspective camera, the position of the camera and the vertical and horizontal opening angle of the FOV. How can I calculate a ray for given screen space coordinates (x, y) where x, y are between -1 and 1 (inclusive).
I tried this:</p>

<p>1) Calculate the vectors sx, sy that span the image plane</p>

<p>sy = -norm(up)</p>

<p>sx = norm(cross(focal, sy))</p>

<p>2) Calculate the focal length f</p>

<p>f = 1 * sin(90 - vertical) / sin(vertical) </p>

<p>3) Create the ray</p>

<p>dir = (center + x * sx + y * sy + f * forward) - center</p>

<p>r(t) = center + t * norm(dir)</p>

<p>But the resulting image does not turn out like the reference image. Someone know what I am doing wrong?</p>

CC BY-SA 4.0
2019-10-27T21:58:14.227Z
`sin` most likely accepts radians. Also you can cancel out the `+center-center`: `dir = x * sx + y * sy + f * forward`. I am unsure why you calculate the focal length like this either.
lightxbulb
CC BY-SA 4.0
2019-10-27T22:16:09.890Z
Oh yes in my code i have pi/2 not 90, but what do you mean with the focal length?
Yamahari
<raytracing><camera>
Perspective Raytracing
Yamahari
# Perspective Raytracing

(Asked by: username_0 on 2019-10-27)

Given the up and focal vector of the perspective camera, the position of the camera and the vertical and horizontal opening angle of the FOV. How can I calculate a ray for given screen space coordinates (x, y) where x, y are between -1 and 1 (inclusive).
I tried this:

1) Calculate the vectors sx, sy that span the image plane

sy = -norm(up)

sx = norm(cross(focal, sy))

2) Calculate the focal length f

f = 1 \* sin(90 - vertical) / sin(vertical)

3) Create the ray

dir = (center + x \* sx + y \* sy + f \* forward) - center

r(t) = center + t \* norm(dir)

But the resulting image does not turn out like the reference image. Someone know what I am doing wrong?

**Tags:** raytracing, camera

> **Comments:**
> **username_1**: `sin` most likely accepts radians. Also you can cancel out the `+center-center`: `dir = x * sx + y * sy + f * forward`. I am unsure why you calculate the focal length like this either.  
> **username_0**: Oh yes in my code i have pi/2 not 90, but what do you mean with the focal length?  

---

## 1 answers

### Answer by username_1 on 2019-10-27. Score: 1 | Accepted answer

Let the horizontal FOV be given as theta (in radians), and the vertical FOV be given as phi (in radians). The we know that:

$$\tan\frac{\theta}{2}=\frac{w}{f}, \tan\frac{\phi}{2}=\frac{h}{f}$$

Let us pick $f = 1$, then $w = \tan\frac{\theta}{2}, h = \tan\frac{\phi}{2}$. Now your ray generation direction generation formula looks like:

$$\pmb{d} = x w\pmb{u} + yh\pmb{v} + \pmb{w}$$

Where $\pmb{u},\pmb{v}, \pmb{w}$ are your sx,sy, forward vectors. Essentially, I found the extent of the smallest/largest x and y respectively ($1w = w, -1w=-w$, etc.). You can premultiply $\pmb{u}, \pmb{v}$ with $w, h$ respectively, if you don't want to perform the multiplication per ray.

> **Comments:**
> **username_0**: hm I am still having trouble recreating the last reference image provided for this task. this is how I am doing my ray now, but that doesnt seem to work for the last image ( probably missing something ): `sx=norm(cross(forward, up)) * tan(horizontal/2)`, `sy=norm(cross(forward, sx)) * tan(vertical/2)` ( up may not be orthogonal to forward ), `r(t)=origin + t * norm(x*sx + y*sy + forward)`. Am I missing something?  
> **username_1**: @username_0 Looks correct. Maybe your fov us given in half angles? Then remove the `/2`. Also is forward normalized?  
> **username_0**: i doubt the angle is only half, for the last image the given vertical/horizontal angles are pi * 0.9. Yes the forward vector is normalized  
> **username_1**: @username_0 Then all should be fine, there is probably a different reason you do not get the reference.  
> **username_0**: oh man I just found out what happened, I was too stupid to use c++ correctly xD thx  

---

