<p>A rotation around the x-axis is achieved by this matrix
$$
\mathtt{T}_\mathrm{rot X} = \begin{bmatrix} 1 &amp; 0 &amp; 0 &amp; 0 \\ 0&amp; \cos \alpha
            &amp; -\sin \alpha&amp; 0 \\ 0 &amp; \sin \alpha &amp; \cos \alpha &amp; 0 \\ 0 &amp; 0 &amp; 0 &amp;1 \\ \end{bmatrix} 
$$</p>

<p>In order to rotate around the x-axis in the viewing space, one needs to think about when to apply the above transformation in the chain of transformations that is applied to the 3D model vertices $\mathbf{X}_{\mathrm{3d}}$ in the employed rendering pipeline.</p>

<p>A typical transformation chain is:</p>

<p>$$
\mathbf{X}_{\mathrm{view}} = \mathtt{T}_{\mathrm{projection}} \,\mathtt{T}_{\mathrm{camera\ transform}}\,
\mathtt{T}_{\mathrm{mesh\ transform}}\,\mathbf{X}_{\mathrm{3d}}
$$</p>

<p>Let's split the camera tranformation into its rotational and translational part:
$$
\mathbf{X}_{\mathrm{view}} = \mathtt{T}_{\mathrm{projection}} \,\underbrace{\mathtt{T}_{\mathrm{cam\ trans}}\,\mathtt{T}_{\mathrm{cam\ rot}}}_{\mathtt{T}_{\mathrm{camera\ transform}}}\,
\mathtt{T}_{\mathrm{mesh\ transform}}\,\mathbf{X}_{\mathrm{3d}}
$$</p>

<p>Then we need to apply the x-rotation in-between the camera rotation and translation, because we want to have the rotation of the camera applied beforehand, but not the camera's translation:</p>

<p>$$
\mathbf{X}_{\mathrm{view}} = \mathtt{T}_{\mathrm{projection}} \,\underbrace{\mathtt{T}_{\mathrm{cam\ trans}}\,\mathtt{T}_\mathrm{rot X}\,\mathtt{T}_{\mathrm{cam\ rot}}}_{\mathtt{T}_{\mathrm{new\ camera\ transform}}}\,
\mathtt{T}_{\mathrm{mesh\ transform}}\,\mathbf{X}_{\mathrm{3d}}
$$</p>

<p>This would achieve the desired rotation, but typically we do not want to change the camera transformation but the the mesh transformation. This is achieved by
$$
\mathbf{X}_{\mathrm{view}} = \mathtt{T}_{\mathrm{projection}} \,\mathtt{T}_{\mathrm{cam\ trans}}\,\mathtt{T}_\mathrm{rot X}\,\mathtt{T}_{\mathrm{cam\ rot}}\,
\mathtt{T}_{\mathrm{mesh\ transform}}\,\mathbf{X}_{\mathrm{3d}}\\
\Leftrightarrow \mathbf{X}_{\mathrm{view}} = \mathtt{T}_{\mathrm{projection}} \,\mathtt{T}_{\mathrm{cam\ trans}}\,(\mathtt{T}_{\mathrm{cam\ rot}}\,\mathtt{T}_{\mathrm{cam\ rot}}^{-1})\,\mathtt{T}_\mathrm{rot X}\,\mathtt{T}_{\mathrm{cam\ rot}}\,
\mathtt{T}_{\mathrm{mesh\ transform}}\,\mathbf{X}_{\mathrm{3d}}\\
\Leftrightarrow \mathbf{X}_{\mathrm{view}} = \mathtt{T}_{\mathrm{projection}} \,\mathtt{T}_{\mathrm{camera\ transform}}\,\underbrace{\mathtt{T}_{\mathrm{cam\ rot}}^{-1}\,\mathtt{T}_\mathrm{rot X}\,\mathtt{T}_{\mathrm{cam\ rot}}\,
\mathtt{T}_{\mathrm{mesh\ transform}}}_{\mathtt{T}_{\mathrm{new\ mesh\ transform}}}\,\mathbf{X}_{\mathrm{3d}}
$$
<strong>Therefore, the short answer to your question is:</strong></p>

<p>Left-multiply the current transformation matrix of your model with $\mathtt{T}_{\mathrm{cam\ rot}}^{-1}\,\mathtt{T}_\mathrm{rot X}\,\mathtt{T}_{\mathrm{cam\ rot}}$.</p>

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

<p>I have generated an "cow" example scene in the GSN Composer that can be used for interactive testing in a WebGL-capable browser:</p>

<p><a href="https://www.gsn-lib.org/docs/gallery.php?projectName=CowQuestion&amp;graphName=cow" rel="nofollow noreferrer">Example: CowQuestion | GSN Composer</a></p>

CC BY-SA 3.0
2018-01-05T16:31:55.403Z
2018-03-07T11:54:23.580Z
2018-03-07T11:54:23.580Z
NodeCode
<p>To get the axis of rotation you could multiply the x-axis by the inverse of the view matrix:</p>

<p>$b = V^{-1} e_1$</p>

<p>Where $b$ is the axis of rotation in world space, $V$ is the view matrix and $e_1$ is the x-axis unit vector.</p>

<p>Now, to make the object to spin around the axis of rotation $b$, we have to translate the object back to the origin, spin it, and then translate it back to its position in world space.</p>

<p>Suppose you have a translation matrix $T$ that send the origin point to the object's location. The translated rotation (spin) of the object $M$ would be:</p>

<p>$M = T R T^{-1}$</p>

<p>Where $R$ is the rotation matrix around the $b$ axis. The matrix $R$ could be obtained using the Rodrigues formula or from a Quaternion or through some other methods out there.</p>

<p>Finally you need to multiply the matrix $M$ to the existing transformation matrix of the object $W$. $W' = M W$ and use $W'$ for transforming the object.</p>

CC BY-SA 3.0
2018-01-06T12:27:57.693Z
2018-01-06T12:27:57.693Z
Mauricio Cele Lopez Belon
<p>I have an object(cow).</p>

<p>This cow was translated and rotated(with arbitrary axis) in the modeling space for several times.</p>

<p>And I can't know how much cow is translated or rotated.</p>

<p>Only I can obtain is this cow's current matrix and it's inverse.</p>

<p>In this circumstance, how can I rotate the cow around the x-axis in the viewing space?</p>

<p>(center of rotation is at the center of modeling space)</p>

<opengl><transformations><affine-transformations>
OpenGL : How to rotate an object around viewing space x-axis
BlakStar
# OpenGL : How to rotate an object around viewing space x-axis

(Asked by: username_0 on 2017-11-11)

I have an object(cow).

This cow was translated and rotated(with arbitrary axis) in the modeling space for several times.

And I can't know how much cow is translated or rotated.

Only I can obtain is this cow's current matrix and it's inverse.

In this circumstance, how can I rotate the cow around the x-axis in the viewing space?

(center of rotation is at the center of modeling space)

**Tags:** opengl, transformations, affine-transformations


---

## 2 answers

### Answer by username_1 on 2018-01-05. Score: 1 | Accepted answer

A rotation around the x-axis is achieved by this matrix
$$
\mathtt{T}\_\mathrm{rot X} = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0& \cos \alpha
& -\sin \alpha& 0 \\ 0 & \sin \alpha & \cos \alpha & 0 \\ 0 & 0 & 0 &1 \\ \end{bmatrix}
$$

In order to rotate around the x-axis in the viewing space, one needs to think about when to apply the above transformation in the chain of transformations that is applied to the 3D model vertices $\mathbf{X}\_{\mathrm{3d}}$ in the employed rendering pipeline.

A typical transformation chain is:

$$
\mathbf{X}\_{\mathrm{view}} = \mathtt{T}\_{\mathrm{projection}} \,\mathtt{T}\_{\mathrm{camera\ transform}}\,
\mathtt{T}\_{\mathrm{mesh\ transform}}\,\mathbf{X}\_{\mathrm{3d}}
$$

Let's split the camera tranformation into its rotational and translational part:
$$
\mathbf{X}\_{\mathrm{view}} = \mathtt{T}\_{\mathrm{projection}} \,\underbrace{\mathtt{T}\_{\mathrm{cam\ trans}}\,\mathtt{T}\_{\mathrm{cam\ rot}}}\_{\mathtt{T}\_{\mathrm{camera\ transform}}}\,
\mathtt{T}\_{\mathrm{mesh\ transform}}\,\mathbf{X}\_{\mathrm{3d}}
$$

Then we need to apply the x-rotation in-between the camera rotation and translation, because we want to have the rotation of the camera applied beforehand, but not the camera's translation:

$$
\mathbf{X}\_{\mathrm{view}} = \mathtt{T}\_{\mathrm{projection}} \,\underbrace{\mathtt{T}\_{\mathrm{cam\ trans}}\,\mathtt{T}\_\mathrm{rot X}\,\mathtt{T}\_{\mathrm{cam\ rot}}}\_{\mathtt{T}\_{\mathrm{new\ camera\ transform}}}\,
\mathtt{T}\_{\mathrm{mesh\ transform}}\,\mathbf{X}\_{\mathrm{3d}}
$$

This would achieve the desired rotation, but typically we do not want to change the camera transformation but the the mesh transformation. This is achieved by
$$
\mathbf{X}\_{\mathrm{view}} = \mathtt{T}\_{\mathrm{projection}} \,\mathtt{T}\_{\mathrm{cam\ trans}}\,\mathtt{T}\_\mathrm{rot X}\,\mathtt{T}\_{\mathrm{cam\ rot}}\,
\mathtt{T}\_{\mathrm{mesh\ transform}}\,\mathbf{X}\_{\mathrm{3d}}\\
\Leftrightarrow \mathbf{X}\_{\mathrm{view}} = \mathtt{T}\_{\mathrm{projection}} \,\mathtt{T}\_{\mathrm{cam\ trans}}\,(\mathtt{T}\_{\mathrm{cam\ rot}}\,\mathtt{T}\_{\mathrm{cam\ rot}}^{-1})\,\mathtt{T}\_\mathrm{rot X}\,\mathtt{T}\_{\mathrm{cam\ rot}}\,
\mathtt{T}\_{\mathrm{mesh\ transform}}\,\mathbf{X}\_{\mathrm{3d}}\\
\Leftrightarrow \mathbf{X}\_{\mathrm{view}} = \mathtt{T}\_{\mathrm{projection}} \,\mathtt{T}\_{\mathrm{camera\ transform}}\,\underbrace{\mathtt{T}\_{\mathrm{cam\ rot}}^{-1}\,\mathtt{T}\_\mathrm{rot X}\,\mathtt{T}\_{\mathrm{cam\ rot}}\,
\mathtt{T}\_{\mathrm{mesh\ transform}}}\_{\mathtt{T}\_{\mathrm{new\ mesh\ transform}}}\,\mathbf{X}\_{\mathrm{3d}}
$$
**Therefore, the short answer to your question is:**

Left-multiply the current transformation matrix of your model with $\mathtt{T}\_{\mathrm{cam\ rot}}^{-1}\,\mathtt{T}\_\mathrm{rot X}\,\mathtt{T}\_{\mathrm{cam\ rot}}$.

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

I have generated an "cow" example scene in the GSN Composer that can be used for interactive testing in a WebGL-capable browser:

[Example: CowQuestion | GSN Composer](https://www.gsn-lib.org/docs/gallery.php?projectName=CowQuestion&graphName=cow)


---

### Answer by username_2 on 2018-01-06. Score: 0

To get the axis of rotation you could multiply the x-axis by the inverse of the view matrix:

$b = V^{-1} e\_1$

Where $b$ is the axis of rotation in world space, $V$ is the view matrix and $e\_1$ is the x-axis unit vector.

Now, to make the object to spin around the axis of rotation $b$, we have to translate the object back to the origin, spin it, and then translate it back to its position in world space.

Suppose you have a translation matrix $T$ that send the origin point to the object's location. The translated rotation (spin) of the object $M$ would be:

$M = T R T^{-1}$

Where $R$ is the rotation matrix around the $b$ axis. The matrix $R$ could be obtained using the Rodrigues formula or from a Quaternion or through some other methods out there.

Finally you need to multiply the matrix $M$ to the existing transformation matrix of the object $W$. $W' = M W$ and use $W'$ for transforming the object.


---

