<p>You can construct a rotation matrix from an &quot;axis&quot;, or 3 vectors. This is done by calculating 3 direction (normalized) vectors for the 3 axis of our new rotated coordinate system, they are forward, up and right vectors.</p>
<p>In your case let's say we have 2 vectors called v1 and v2.</p>
<p>we can produce a direction from them via (glsl psuedo code):</p>
<pre><code>vec3 forwardVector = normalize(v2 - v1);
</code></pre>
<p>Then for our up vector we can pick the world-up axis (e.g. 0,1,0), although this may give you problems if the forward vector is very close to the up axis, in that case you can pick another arbitrary axis that's not nearby, like {0,0,1}. For the sake of simplicity we'll stick to the world up axis of {0,1,0}.</p>
<p>The last vector can be derived automatically from the other 2 vectors using a cross product.</p>
<p>so now we should have something like:</p>
<pre><code>vec3 forwardVector = normalize(v2 - v1);
vec3 upVector = vec3(0,1,0);
vec3 rightVector = normalize(cross(forwardVector, upVector));
</code></pre>
<p>These 3 vectors can be directly plugged into a 3x3 matrix to form the rotation matrix.</p>
<pre><code>r.x r.y r.z
u.x u.y u.z 
f.x f.y f.z 
</code></pre>
<p>where f=forwardVector, u=upVector and r=rightVector.</p>
<p>This is similar to an openGL &quot;LookAt&quot; matrix.</p>
<p>Be aware there are many rotation matrices that are valid solutions because 2 vectors cannot describe what the rotation is along the v1-&gt;v2 axis (That's what the 'up' axis fixes in this case).</p>

CC BY-SA 4.0
2021-03-22T07:08:15.693Z
2021-03-22T08:46:45.830Z
2021-03-22T08:46:45.830Z
PaulHK
<p>So I am making a raytracer following the very helpful <a href="https://www.gabrielgambetta.com/computer-graphics-from-scratch/05-extending-the-raytracer.html" rel="nofollow noreferrer">online book of Gabriel Gambetta</a> but I stumble on the rotation matrix part.</p>
<p>My linear algebra background is 3Blue1Brown video series &quot;The essence of Linear Algebra&quot;.</p>
<p>I simulate a camera placed at the origin that's looking forward ie whose direction is the vector <code>{0,0,1}</code>.
Let's say I am given another camera direction with direction <code>{a,b,c}</code>.
From what I understand if I find the rotation matrix R that takes as an input <code>{0,0,1}</code> and spits out a normalized <code>{a,b,c}</code> as an output.
Then I can apply that rotation matrix to all my rays in order to rotate them.
I can't get my head around on how to find R. Any help/explanation/correction much welcome.</p>
<p>I am in a similar situation than <a href="https://computergraphics.stackexchange.com/questions/10613/transforming-a-ray-from-camera-space-to-world-space">the person asking this question</a> except that he seems to know already what needs be done and is just asking
about if his rationale is good.</p>
<p>Thanks</p>
<p>EDIT : The answer below is valid considering how I framed my problem but note that if you were like me trying to compute a camera to world rotation matrix and expected the viewport of your camera to follow along, you need an additional step because the answer below only gives you a shear matrix. In order to get a rotation matrix you need to compute the <code>upVector</code> with <code>normalize(cross_product(rightVector, forwardVector))</code> (in a right hand system) and feed its coordinates into the matrix instead of <code>{0,1,0}</code></p>

CC BY-SA 4.0
2021-03-22T09:36:52.817Z
Here is a [related question from Math SE](https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d). I don't have the time right now to give you a fully qualified answer, but you can also find the quaternion between two vectors and turn it into a matrix afterward. To find the quaternion, have a look into [this question](https://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another)
wychmaster
<raytracing><camera><camera-matrix>
Given two 3d vectors, find the rotation matrix between the two
cassepipe
# Given two 3d vectors, find the rotation matrix between the two

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

So I am making a raytracer following the very helpful [online book of Gabriel Gambetta](https://www.gabrielgambetta.com/computer-graphics-from-scratch/05-extending-the-raytracer.html) but I stumble on the rotation matrix part.

My linear algebra background is 3Blue1Brown video series "The essence of Linear Algebra".

I simulate a camera placed at the origin that's looking forward ie whose direction is the vector `{0,0,1}`.
Let's say I am given another camera direction with direction `{a,b,c}`.
From what I understand if I find the rotation matrix R that takes as an input `{0,0,1}` and spits out a normalized `{a,b,c}` as an output.
Then I can apply that rotation matrix to all my rays in order to rotate them.
I can't get my head around on how to find R. Any help/explanation/correction much welcome.

I am in a similar situation than [the person asking this question](https://computergraphics.stackexchange.com/questions/10613/transforming-a-ray-from-camera-space-to-world-space) except that he seems to know already what needs be done and is just asking
about if his rationale is good.

Thanks

EDIT : The answer below is valid considering how I framed my problem but note that if you were like me trying to compute a camera to world rotation matrix and expected the viewport of your camera to follow along, you need an additional step because the answer below only gives you a shear matrix. In order to get a rotation matrix you need to compute the `upVector` with `normalize(cross_product(rightVector, forwardVector))` (in a right hand system) and feed its coordinates into the matrix instead of `{0,1,0}`

**Tags:** raytracing, camera, camera-matrix

> **Comments:**
> **username_1**: Here is a [related question from Math SE](https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d). I don't have the time right now to give you a fully qualified answer, but you can also find the quaternion between two vectors and turn it into a matrix afterward. To find the quaternion, have a look into [this question](https://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another)  

---

## 1 answers

### Answer by username_2 on 2021-03-22. Score: 2 | Accepted answer

You can construct a rotation matrix from an "axis", or 3 vectors. This is done by calculating 3 direction (normalized) vectors for the 3 axis of our new rotated coordinate system, they are forward, up and right vectors.

In your case let's say we have 2 vectors called v1 and v2.

we can produce a direction from them via (glsl psuedo code):

```
vec3 forwardVector = normalize(v2 - v1);

```

Then for our up vector we can pick the world-up axis (e.g. 0,1,0), although this may give you problems if the forward vector is very close to the up axis, in that case you can pick another arbitrary axis that's not nearby, like {0,0,1}. For the sake of simplicity we'll stick to the world up axis of {0,1,0}.

The last vector can be derived automatically from the other 2 vectors using a cross product.

so now we should have something like:

```
vec3 forwardVector = normalize(v2 - v1);
vec3 upVector = vec3(0,1,0);
vec3 rightVector = normalize(cross(forwardVector, upVector));

```

These 3 vectors can be directly plugged into a 3x3 matrix to form the rotation matrix.

```
r.x r.y r.z
u.x u.y u.z 
f.x f.y f.z 

```

where f=forwardVector, u=upVector and r=rightVector.

This is similar to an openGL "LookAt" matrix.

Be aware there are many rotation matrices that are valid solutions because 2 vectors cannot describe what the rotation is along the v1->v2 axis (That's what the 'up' axis fixes in this case).


---

