簡介
GAME101作業1的答案,基礎版本,僅供參考,一直認為,沒有標准版本和評級版本的作業沒有必要做。所以希望分享我的觀點。
問題
實現 MVP中的M和P矩陣的構建。
get_model_matrix(float rotation_angle): 逐個元素地構建模型變換矩陣並返回該矩陣。在此函數中,你只需要實現三維中繞 z 軸旋轉的變換矩陣,而不用處理平移與縮放。
get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar): 使用給定的參數逐個元素地構建透視投影矩陣並返回該矩陣。
關於model矩陣的構建&code
model 在閆老師的課程上說的是,關於物品的擺放。
Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
// TODO: Implement this function
// Create the model matrix for rotating the triangle around the Z axis.
// Then return it.
Eigen::Matrix4f rotate;
float radian = rotation_angle/180.0*MY_PI;
rotate << cos(radian), -1*sin(radian), 0, 0,
sin(radian), cos(radian), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;//單純實現了關於z軸的旋轉矩陣
model = rotate * model;
return model;
}
關於Projection 的構建 & code
Projection 在閆老師的課程上說的是,將三維的物體映射為二維的物體
/*
* eye_fov 視野的大小
* aspect_ratio 長寬比? 猜測是視野的長寬比率
* zNear 最近處的坐標
* zFar 最遠處的坐標
*/
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar)
{
// Students will implement this function
// TODO: Implement this function
// Create the projection matrix for the given parameters.
// Then return it.
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
Eigen::Matrix4f P2O = Eigen::Matrix4f::Identity();//將透視投影轉換為正交投影的矩陣
P2O<<zNear, 0, 0, 0,
0, zNear, 0, 0,
0, 0, zNear+zFar,(-1)*zFar*zNear,
0, 0, 1, 0;// 進行透視投影轉化為正交投影的矩陣
float halfEyeAngelRadian = eye_fov/2.0/180.0*MY_PI;
float t = zNear*std::tan(halfEyeAngelRadian);//top y軸的最高點
float r=t*aspect_ratio;//right x軸的最大值
float l=(-1)*r;//left x軸最小值
float b=(-1)*t;//bottom y軸的最大值
Eigen::Matrix4f ortho1=Eigen::Matrix4f::Identity();
ortho1<<2/(r-l),0,0,0,
0,2/(t-b),0,0,
0,0,2/(zNear-zFar),0,
0,0,0,1;//進行一定的縮放使之成為一個標准的長度為2的正方體
Eigen::Matrix4f ortho2 = Eigen::Matrix4f::Identity();
ortho2<<1,0,0,(-1)*(r+l)/2,
0,1,0,(-1)*(t+b)/2,
0,0,1,(-1)*(zNear+zFar)/2,
0,0,0,1;// 把一個長方體的中心移動到原點
Eigen::Matrix4f Matrix_ortho = ortho1 * ortho2;
projection = Matrix_ortho * P2O;
return projection;
}
測試樣例
Rasterizer -r 0/45/90



QUESTION
整個流程其實還是不是特別清楚
\[M_{persp} = M_{ortho}M_{persp->ortho} \]
這個公式的作用輸入輸出是什么?
GUESS:猜測 先將一個錐視圖轉為正交視圖然后調用正交矩陣就可以得到相應的人類視覺的圖片。
參考資料
viede https://www.bilibili.com/video/av90798049?p=4
pdf https://sites.cs.ucsb.edu/~lingqi/teaching/resources/GAMES101_Lecture_04_supp.pdf
