DAY 11
0

# 今日目標

• 位移、旋轉、縮放

## 位移、縮放

``````/*
Translate:
1  0  0  x
0  1  0  y
0  0  1  z
0  0  0  1
*/
Mat4 Mat4Translate(Mat4 origin, float x, float y) {
Mat4 translate = MAT4_IDENTITY;

translate.ary2d[3][0] = x;
translate.ary2d[3][1] = y;

return Mat4MulMat4(origin, translate);
}

/*
Scale:
x  0  0  0
0  y  0  0
0  0  z  0
0  0  0  1
*/
Mat4 Mat4Scale(Mat4 origin, float x, float y) {
Mat4 scale = MAT4_IDENTITY;

scale.ary2d[0][0] = x;
scale.ary2d[1][1] = y;

return Mat4MulMat4(origin, scale);
}
``````

## 旋轉

``````/*
Rotate:
s = sin(angle), c = cos(angle)

c   -s   0   0
s    c   0   0
0    0   1   0
0    0   0   1
*/
Mat4 Mat4EulerRotate(Mat4 origin, float angle) {
Mat4 rotate = MAT4_IDENTITY;

float s = sinf(rad), c = cosf(rad);

rotate.ary2d[0][0] = c;
rotate.ary2d[0][1] = s;

rotate.ary2d[1][0] = -s;
rotate.ary2d[1][1] = c;

return Mat4MulMat4(origin, rotate);
}
``````

# 請注意順序

``````// Mat4MulMat4是我製作的矩陣相乘的function
MVP = Mat4MulMat4(Projection, Model);
``````

# 補充: 隨著世界改動

``````glViewport(int x, int y, int width, int height)
``````

``````// framebuffer size callback
void FramebufferSizeCallback(GLFWwindow* window, int w, int h) {
glViewport(0, 0, w, h);
}

// in `CreateWindow`
glfwSetFramebufferSizeCallback(WINDOW.wnd, FramebufferSizeCallback);
``````