In computer graphics, all operations are matrix, and the translation, rotation and scaling of a graph can be expressed as the operations of graph vertex coordinates and related matrices.

Translation matrix


[ 1 0 0 T x 0 1 0 T y 0 0 1 T z 0 0 0 1 ] [ x y z 1 ] = [ x + T x y + T y z + T z 1 ] \begin{bmatrix} 1 & 0 & 0 & T_x \\ 0 & 1 & 0 & T_y \\ 0 & 0 & 1 & T_z\\ 0 & 0 & 0& 1\end{bmatrix} \begin{bmatrix} x \\ y \\ z\\ 1\end{bmatrix} = \begin{bmatrix} x+T_x \\ y+T_y \\ z+T_z\\ 1\end{bmatrix}

The corresponding shader language is as follows:

attribute vec4 apos; Void main () {/ / create translation matrix (along the x axis translation - 0.4) / / 1 0 0-0.4 / / 1 0 0 0 / / 0 0 0 / / 0 0 0 1 mat4 m4 = mat4 (,0,1,0,1,0,0,0,0,0 1, 0, 0, 0.4, 0, 1); gl_Position = m4*apos; }Copy the code

Rotation matrix

  • We rotate it around the X-axis

[ 1 0 0 0 0 cos Theta. sin Theta. 0 0 sin Theta. cos Theta. 0 0 0 0 1 ] [ x y z 1 ] = [ x y cos Theta. z sin Theta. y sin Theta. + z cos Theta. 1 ] \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos\theta & -\sin\theta & 0 \\ 0 & \sin\theta & \cos\theta & 0\\ 0 & 0 & 0& 1\end{bmatrix} \begin{bmatrix} x \\ y \\ z\\ 1\end{bmatrix} = \begin{bmatrix} x \\ y\cos\theta-z\sin\theta \\ y\sin\theta+z\cos\theta\\ 1\end{bmatrix}
  • We rotate it around the y axis

[ cos Theta. 0 sin Theta. 0 0 1 0 0 sin Theta. 0 cos Theta. 0 0 0 0 1 ] [ x y z 1 ] = [ z sin Theta. + x cos Theta. y z cos Theta. x sin Theta. 1 ] \begin{bmatrix} \cos\theta & 0 & \sin\theta & 0 \\ 0 & 1 & 0 & 0 \\ -\sin\theta & 0 & \cos\theta & 0\\ 0 & 0 & 0& 1\end{bmatrix} \begin{bmatrix} x \\ y \\ z\\ 1\end{bmatrix} = \begin{bmatrix} z\sin\theta+x\cos\theta \\ y \\ z\cos\theta-x\sin\theta\\ 1\end{bmatrix}
  • It’s rotating around the z axis

[ sin Theta. cos Theta. 0 0 cos Theta. sin Theta. 0 0 0 0 1 0 0 0 0 1 ] [ x y z 1 ] = [ x cos Theta. y sin Theta. x sin Theta. + y cos Theta. z 1 ] \begin{bmatrix} -\sin\theta & \cos\theta & 0 & 0 \\\cos\theta & \sin\theta & 0 & 0 \\ 0 & 0& 1& 0\\ 0 & 0 & 0& 1\end{bmatrix} \begin{bmatrix} x \\ y \\ z\\ 1\end{bmatrix} = \begin{bmatrix} x\cos\theta-y\sin\theta \\ x\sin\theta+y\cos\theta\\ z\\1\end{bmatrix}

The corresponding shader language is as follows:

//attribute declare vec4 type variable apos attribute vec4 apos; Void main() {float radian = radians(30.0); float radian = radians(30.0); Float cos = cos(radian); float cos = cos(radian); Float sine = sine (radian); float sine = sine (radian); // Reference the above calculation data, // 1 0 0 // 0 cosα sinα 0 // 0 0 0 1 mat4 mx = mat4(1,0,0,0, 0,cos,-sin,0, 0,sin,cos,0, 0,0,0, 0,0, sine,cos,0, 0) 0,0,0,1); // Reference the above calculation data, Create around y axis rotation matrix / / sine cosine beta 0 beta / / 0 0 0 0 1 / / - sine beta zero cosine beta / / 0 0 0 0 1 mat4 my = mat4 (cos, 0, - sin, 0, 0,1,0,0, sin, 0, cos, 0, 0,0,0,1); Gl_Position = mx*my*apos; }Copy the code

Scaling matrix


[ S x 0 0 0 0 S y 0 0 0 0 S z 0 0 0 0 1 ] [ x y z 1 ] = [ x S x y S y z S z 1 ] \begin{bmatrix} S_x & 0 & 0 & 0 \\ 0 & S_y & 0 & 0 \\ 0 & 0 & S_z & 0\\ 0 & 0 & 0& 1\end{bmatrix} \begin{bmatrix} x \\ y \\ z\\ 1\end{bmatrix} = \begin{bmatrix} x*S_x \\ y*S_y \\ z*S_z\\ 1\end{bmatrix}

The corresponding shader language is as follows:

attribute vec4 apos; Void main () {/ / create a matrix (0.5) x zoom zoom / / 0.5 0 0 0 / / 0 1 0 0 / / 0 0 0 / / 0 0 0 1 mat4 m4 = mat4 (0.5, 0, 0, 0,1,0,0, 0,0,1,0, 0,0,0,1); gl_Position = m4*apos; }Copy the code