4x4変換行列

- 回転方向は一例です。資料によっては回転方向を逆向きに定義している場合もあるようです。

# OpenGL

https://open.gl/transformations

右手系、列ベクトル

## 座標軸周りの回転

### X軸周り

```math
\left(\begin{array}{c}
X \\ Y \\ Z \\ 1
\end{array}\right) =
\left(\begin{array}{c}
x \\
y\cos\theta_x - z\sin\theta_x \\ 
y\sin\theta_x + z\cos\theta_x \\ 
1
\end{array}\right) =
\left(\begin{array}{cccc}
1 & 0 & 0 & 0 \\
0 & \cos\theta_x & -\sin\theta_x & 0 \\
0 & \sin\theta_x & \cos\theta_x & 0 \\
0 & 0 & 0 & 1
\end{array}\right)
\left(\begin{array}{c}
x \\ y \\ z \\ 1
\end{array}\right)
```

### Y軸周り

```math
\left(\begin{array}{c}
X \\ Y \\ Z \\ 1
\end{array}\right) =
\left(\begin{array}{c}
x\cos\theta_y + z\sin\theta_y \\ 
y \\ 
-x\sin\theta_y + z\cos\theta_y \\ 
1
\end{array}\right) =
\left(\begin{array}{cccc}
\cos\theta_y & 0 & \sin\theta_y & 0 \\
0 & 1 & 0 & 0 \\
-\sin\theta_y & 0 & \cos\theta_y & 0 \\
0 & 0 & 0 & 1
\end{array}\right)
\left(\begin{array}{c}
x \\ y \\ z \\ 1
\end{array}\right)
```

### Z軸周り

```math
\left(\begin{array}{c}
X \\ Y \\ Z \\ 1
\end{array}\right) =
\left(\begin{array}{c}
x\cos\theta_z - y\sin\theta_z \\ 
x\sin\theta_z + z\cos\theta_z \\ 
z \\ 
1
\end{array}\right) =
\left(\begin{array}{cccc}
\cos\theta_z & -\sin\theta_z & 0 & 0 \\
\sin\theta_z & \cos\theta_z & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{array}\right)
\left(\begin{array}{c}
x \\ y \\ z \\ 1
\end{array}\right)
```

## 拡大縮小+平行移動

```math
\left(\begin{array}{c}
X \\ Y \\ Z \\ 1
\end{array}\right) =
\left(\begin{array}{c}
s_x \cdot x + a_x \\ 
s_y \cdot y + a_y \\ 
s_z \cdot z + a_z \\ 
1
\end{array}\right) =
\left(\begin{array}{cccc}
s_x & 0 & 0 & a_x \\
0 & s_y & 0 & a_y \\
0 & 0 & s_z & a_z \\
0 & 0 & 0 & 1
\end{array}\right)
\left(\begin{array}{c}
x \\ y \\ z \\ 1
\end{array}\right)
```

# DirectX

https://msdn.microsoft.com/ja-jp/library/bb206269.aspx

左手系、行ベクトル

## 座標軸周りの回転

### X軸周り

```math
\left(\begin{array}{cccc}
X & Y & Z & 1
\end{array}\right) =
\left(\begin{array}{c}
X \\ Y \\ Z \\ 1
\end{array}\right)^T =
\left(\begin{array}{c}
x \\
y\cos\theta_x - z\sin\theta_x \\ 
y\sin\theta_x + z\cos\theta_x \\ 
1
\end{array}\right)^T =
\left(\begin{array}{cccc}
x & y & z & 1
\end{array}\right)
\left(\begin{array}{cccc}
1 & 0 & 0 & 0 \\
0 & \cos\theta_x & \sin\theta_x & 0 \\
0 & -\sin\theta_x & \cos\theta_x & 0 \\
0 & 0 & 0 & 1
\end{array}\right)
```

### Y軸周り

```math
\left(\begin{array}{cccc}
X & Y & Z & 1
\end{array}\right) =
\left(\begin{array}{c}
X \\ Y \\ Z \\ 1
\end{array}\right)^T =
\left(\begin{array}{c}
x\cos\theta_y + z\sin\theta_y \\ 
y \\ 
-x\sin\theta_y + z\cos\theta_y \\ 
1
\end{array}\right)^T =
\left(\begin{array}{cccc}
x & y & z & 1
\end{array}\right)
\left(\begin{array}{cccc}
\cos\theta_y & 0 & -\sin\theta_y & 0 \\
0 & 1 & 0 & 0 \\
\sin\theta_y & 0 & \cos\theta_y & 0 \\
0 & 0 & 0 & 1
\end{array}\right)
```

### Z軸周り

```math
\left(\begin{array}{cccc}
X & Y & Z & 1
\end{array}\right) =
\left(\begin{array}{c}
X \\ Y \\ Z \\ 1
\end{array}\right)^T =
\left(\begin{array}{c}
x\cos\theta_z - y\sin\theta_z \\ 
x\sin\theta_z + y\cos\theta_z \\ 
z \\ 
1
\end{array}\right)^T =
\left(\begin{array}{cccc}
x & y & z & 1
\end{array}\right)
\left(\begin{array}{cccc}
\cos\theta_z & \sin\theta_z & 0 & 0 \\
-\sin\theta_z & \cos\theta_z & 0 & 0 \\
0 & 0 & 1 & 0 \\
0 & 0 & 0 & 1
\end{array}\right)
```

## 拡大縮小+平行移動

```math
\left(\begin{array}{cccc}
X & Y & Z & 1
\end{array}\right) =
\left(\begin{array}{c}
X \\ Y \\ Z \\ 1
\end{array}\right)^T =
\left(\begin{array}{c}
s_x \cdot x + a_x \\ 
s_y \cdot y + a_y \\ 
s_z \cdot z + a_z \\ 
1
\end{array}\right)^T =
\left(\begin{array}{cccc}
x & y & z & 1
\end{array}\right)
\left(\begin{array}{cccc}
s_x & 0 & 0 & 0 \\
0 & s_y & 0 & 0 \\
0 & 0 & s_z & 0 \\
a_x & a_y & a_z & 1
\end{array}\right)
```