Matrixtransformationen in der Computergrafik

In der Computergrafik sind Matrizen grundlegende Werkzeuge, die verwendet werden, um Objekte im 2D- und 3D-Raum zu transformieren. Diese Transformationen umfassen Translation, Rotation und Skalierung. Diese Seite erklärt anhand mathematischer Beispiele und einer interaktiven App, wie Matrizen diese Operationen ermöglichen.

Homogene Koordinaten

Bevor wir uns den Transformationen widmen, müssen wir homogene Koordinaten verstehen. In einem 3D-System repräsentieren wir einen Punkt als \((x, y, z, 1)\) und einen Vektor als \((x, y, z, 0)\). Dies ermöglicht es uns, sowohl Transformationen als auch Translationen mithilfe von Matrizen darzustellen.

Grundlegende Transformationen

1. Translation (Verschiebung)

Die Translation verschiebt ein Objekt um einen Vektor \((t_x, t_y, t_z)\). Die Translationsmatrix lautet:

\[ T(t_x, t_y, t_z) = \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Für einen Punkt \(P = (x, y, z, 1)\) ist der verschobene Punkt \(P'\):

\[ P' = T \cdot P = \begin{pmatrix} 1 & 0 & 0 & t_x \\ 0 & 1 & 0 & t_y \\ 0 & 0 & 1 & t_z \\ 0 & 0 & 0 & 1 \end{pmatrix} \begin{pmatrix} x \\ y \\ z \\ 1 \end{pmatrix} = \begin{pmatrix} x + t_x \\ y + t_y \\ z + t_z \\ 1 \end{pmatrix} \]

2. Rotation

Die Rotation dreht ein Objekt um eine Achse. Hier sind die Rotationsmatrizen für die drei Hauptachsen:

Rotation um die X-Achse um den Winkel \(\theta\):

\[ R_x(\theta) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\theta & -\sin\theta & 0 \\ 0 & \sin\theta & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Rotation um die Y-Achse um den Winkel \(\theta\):

\[ R_y(\theta) = \begin{pmatrix} \cos\theta & 0 & \sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin\theta & 0 & \cos\theta & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Rotation um die Z-Achse um den Winkel \(\theta\):

\[ R_z(\theta) = \begin{pmatrix} \cos\theta & -\sin\theta & 0 & 0 \\ \sin\theta & \cos\theta & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

3. Skalierung

Die Skalierung verändert die Größe eines Objekts um Faktoren \((s_x, s_y, s_z)\). Die Skalierungsmatrix lautet:

\[ S(s_x, s_y, s_z) = \begin{pmatrix} s_x & 0 & 0 & 0 \\ 0 & s_y & 0 & 0 \\ 0 & 0 & s_z & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Komplexes Beispiel: Zusammengesetzte Transformationen

In der Praxis wenden wir oft mehrere Transformationen hintereinander an. Die Matrizenmultiplikation ermöglicht es uns, diese Operationen effizient zu kombinieren.

Betrachten wir das Rendern eines Würfels mit den Eckpunkten bei \((\pm 1, \pm 1, \pm 1)\).

Wir möchten einen Würfel wie folgt transformieren:

  1. Skalieren des Würfels um die Faktoren (2, 1, 0,5)
  2. Rotieren um 45\(^\circ{}\) um die Y-Achse
  3. Verschieben um (10, 5, -3)

Die zusammengesetzte Transformationsmatrix lautet:

\[ M = T(10, 5, -3) \cdot R_y(45^\circ{}) \cdot S(2, 1, 0.5) \]

Einsetzen der Matrizen:

\[ M = \begin{pmatrix} 1 & 0 & 0 & 10 \\ 0 & 1 & 0 & 5 \\ 0 & 0 & 1 & -3 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} \cos(45^{\circ}) & 0 & \sin(45^{\circ}) & 0 \\ 0 & 1 & 0 & 0 \\ -\sin(45^{\circ}) & 0 & \cos(45^{\circ}) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} 2 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0.5 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Mit \(\cos(45^{\circ}) = \sin(45^{\circ}) = \frac{\sqrt{2}}{2} \approx 0.7071\):

\[ M = \begin{pmatrix} 1 & 0 & 0 & 10 \\ 0 & 1 & 0 & 5 \\ 0 & 0 & 1 & -3 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} 0.7071 & 0 & 0.7071 & 0 \\ 0 & 1 & 0 & 0 \\ -0.7071 & 0 & 0.7071 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} 2 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0.5 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Multiplikation der letzten beiden Matrizen zuerst:

\[ R_y(45^{\circ}) \cdot S(2, 1, 0.5) = \begin{pmatrix} 1.4142 & 0 & 0.3536 & 0 \\ 0 & 1 & 0 & 0 \\ -1.4142 & 0 & 0.3536 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Anschließende Multiplikation mit der Translationsmatrix:

\[ M = \begin{pmatrix} 1.4142 & 0 & 0.3536 & 10 \\ 0 & 1 & 0 & 5 \\ -1.4142 & 0 & 0.3536 & -3 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]

Wenden wir alle drei Transformationen beispielsweise auf den Eckpunkt \( (1,1,1) \) an, erhalten wir einen neuen Eckpunkt bei:

\[ M \cdot \begin{pmatrix} 1 \\ 1 \\ 1 \\ 1 \end{pmatrix} = \begin{pmatrix} 1.4142 & 0 & 0.3536 & 10 \\ 0 & 1 & 0 & 5 \\ -1.4142 & 0 & 0.3536 & -3 \\ 0 & 0 & 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} 1 \\ 1 \\ 1 \\ 1 \end{pmatrix} = \begin{pmatrix}11.7678\\ 6\\ -4.0606\\ 1\end{pmatrix} \]

Hinweis: In der Computergrafik werden Transformationen bei der Matrizenmultiplikation typischerweise von rechts nach links angewendet, entgegen der Reihenfolge, in der wir die Operationen auf Deutsch beschreiben.

Fazit

Matrizen bieten eine elegante und effiziente Möglichkeit, Transformationen in der Computergrafik darzustellen und zu kombinieren. Durch die Verwendung homogener Koordinaten und der Matrizenmultiplikation können wir problemlos komplexe 3D-Szenen mit Rotation, Translation und Skalierung implementieren – allesamt grundlegende Operationen in modernen Rendering-Engines.

Die Stärke des Matrix-Ansatzes liegt darin, dass eine beliebige Sequenz von Transformationen zu einer einzigen Matrix zusammengefasst werden kann, die dann effizient auf alle Eckpunkte eines 3D-Modells angewendet wird.

Interaktive Demo

Transformieren Sie den auf dem Bildschirm sichtbaren Würfel durch Drehen, Verschieben und Skalieren. Verwenden Sie jeweils eine Transformation, um die jeweilige Auswirkung besser zu verstehen. Die Transformationsmatrizen werden auch am Ende der Seite angezeigt.

Translation (Verschiebung)

Rotation (Grad)

Skalierung

Transformationsmatrizen