Shader theory (3): transformation related matrix

Table of contents

Foreword:

1. What is the essence of matrix?

2. Several basic transformation applications of matrices

2.1 Scale scaling

2.2 Reflection mirroring

2.3 Shear

2.4 Rotate rotation

2.5 Translation translation

3. Homogenous Coordinates

3.1 Scale

3.2 Rotation

3.3 Translation 

4. Others

References


Foreword:

This part is the basic knowledge about matrix transformation in the GAMERS101 course (Lecture 3 Transformation), and the pre-requisite knowledge is the basic operation of matrix

1. What is the essence of matrix?

Q: What is the essence of matrix?

A: Transform.

2. Several basic transformation applications of matrices

Note: All the following operations are temporarily based on two-dimensional transformation, and do not involve three-dimensional transformation operations

2.1 Scale scaling

\begin{pmatrix} x'\\ y' \end{pmatrix} = \begin{pmatrix} s&0 \\ 0&s \end{pmatrix} * \begin{pmatrix} x\\ y \end{pmatrix}=\begin{pmatrix} sx\\ sy \end{pmatrix}

where s is the scaling factor

2.2 Reflection mirroring

\begin{pmatrix} x'\\ y' \end{pmatrix}=\begin{pmatrix} -1&0\\ 0&1 \end{pmatrix}*\begin{pmatrix} x\\ y \end{pmatrix}=\begin{pmatrix} -x\\ y \end{pmatrix}

Mirror along the y-axis

2.3 Shear

\begin{pmatrix} x'\\ y' \end{pmatrix}=\begin{pmatrix} 1&a\\ 0&1 \end{pmatrix}*\begin{pmatrix} x\\ y \end{pmatrix}=\begin{pmatrix} x+ay\\ y \end{pmatrix}

Make a bevel along the x-axis, a controls the degree of bevel

2.4 Rotate rotation

\begin{pmatrix} x'\\ y' \end{pmatrix}=\begin{pmatrix} cos\theta &-sin\theta\\ sin\theta&cos\theta \end{pmatrix}*\begin{pmatrix} x\\ y \end{pmatrix}=\begin{pmatrix} x*cos\theta-sin\theta\\ sin\theta+y*cos\theta \end{pmatrix}

θ is the rotation angle, the default is counterclockwise, and it is based on the origin

The above four transformations are linear transformations (Linear Transform), and their forms can be written uniformly as\begin{pmatrix} x'\\ y' \end{pmatrix}=M*\begin{pmatrix} x\\ y \end{pmatrix}


2.5 Translation translation

Translation is not a linear transformation, and its matrix form is different from the above four

x' = x + t_{x}

y' = y + t_{y}

\begin{pmatrix} x'\\ y' \end{pmatrix} = \begin{pmatrix} x\\ y \end{pmatrix}+\begin{pmatrix} t_{x}\\ t_{y} \end{pmatrix}

Therefore, the concept of homogeneous coordinates is introduced

3. Homogenous Coordinates

That is, on the basis of the original matrix, one more row and one column is added, so that the translation transformation can also be written in \begin{pmatrix} x'\\ y' \end{pmatrix}=M*\begin{pmatrix} x\\ y \end{pmatrix}the form

 So the translation transformation can be written as:

\begin{pmatrix} x'\\ y'\\ w' \end{pmatrix}=\begin{pmatrix} 1&0&t_{x}\\ 0&1&t_{y}\\ 0&0&1 \end{pmatrix}*\begin{pmatrix} x\\ y\\ 1 \end{pmatrix}=\begin{pmatrix} x+t_{x}\\ y+t_{y}\\ 1 \end{pmatrix}

Note: The meaning of w can be understood as, when it is 1, it is a point point written in homogeneous coordinates; when it is 0, it is a vector vector

vector + vector = vector

point - point = vector

point + vector = point

point + point = midpoint*2

Therefore, the above transformation matrix M can be written as follows:

3.1 Scale

S(s_{x},s_{y}) = \begin{pmatrix} s_{x}&0&0\\ 0&s_{y}&0\\ 0&0&1 \end{pmatrix}

3.2 Rotation

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

3.3 Translation 

T(t_{x},t_{y}) = \begin{pmatrix} 1&0&t_{x}\\ 0&1&t_{y}\\ 0&0&1 \end{pmatrix}


Since the matrix operation does not satisfy the commutative law, but satisfies the associative law, so

A_{n}(...A_{2}(A_{1})(X)) = (A_{n}...A_{2}A{1})X

Finally, the transformation matrix for a point in 3D space looks like this:

\begin{pmatrix} x'\\ y'\\ z'\\ 1 \end{pmatrix}=\begin{pmatrix} a&b&c&t_{x}\\ d&e&f&t_{y}\\ g&h&i&t_{z}\\ 0&0&0&1 \end{pmatrix}*\begin{pmatrix} x\\ y\\ z\\ 1 \end{pmatrix}


4. Others

In the development of C# for Grasshopper, objects have scale and rotation methods, but often there is no translation method, because the coordinates at this time are stored in float3, so it is difficult to perform translation transformation. At this time, it is necessary to define a variable (generally var xf), and write all transformation operations in a Transform for application. This is the principle.

References:

[1]  GAMES101-Introduction to Modern Computer Graphics-Yan Lingqi_哔哩哔哩_bilibili

Guess you like

Origin blog.csdn.net/qq_41904236/article/details/125156688