Homework0
Homework0
#include <cmath>
#include <Eigen/Dense>
#include <Eigen/Core>
#include <iostream>
using namespace std;
using namespace Eigen;
#define pi 3.1415926
int main()
{
Vector3d p(2.0f, 1.0f, 1.0f);
double th = (45.0 / 180.0) * pi;
Matrix3d rotate,trans;
rotate << cos(th), -1 * sin(th), 0,
sin(th), cos(th), 0,
0, 0, 1;
trans << 1, 0, 1,
0, 1, 2,
0, 0, 1;
p = trans * rotate * p;
cout << p.head(2) << endl;
return 0;
}
知识点:
图片来自这里
Homework1
Eigen::Matrix4f get_model_matrix(float rotation_angle)
{
Eigen::Matrix4f model = Eigen::Matrix4f::Identity();
rotation_angle=rotation_angle/180.0f*MY_PI;
// TODO: Implement this function
// Create the model matrix for rotating the triangle around the Z axis.
// Then return it.
model<<cos(rotation_angle),-sin(rotation_angle),0,0,
sin(rotation_angle),cos(rotation_angle),0,0,
0,0,1,0,
0,0,0,1;
return model;
}
//提高项代码如下段
Eigen::Matrix4f get_rotation(Vector3f axis,float angle)
{
angle=angle/180.0f*MY_PI;
Eigen::Matrix4f Result = Eigen::Matrix4f::Identity();
Eigen::Matrix3f E = Eigen::Matrix3f::Identity();
Eigen::Matrix3f N = Eigen::Matrix3f::Identity();
Eigen::Matrix3f ResultMat3 = Eigen::Matrix3f::Identity();
N<< 0,-axis[2],axis[1],
axis[2],0,-axis[0],
-axis[1],axis[0],0;
ResultMat3 = E*cos(angle) +(1-cos(angle))*axis*axis.transpose()+ sin(angle)*N;
Result<<ResultMat3(0,0),ResultMat3(0,1),ResultMat3(0,2),0,
ResultMat3(1,0),ResultMat3(1,1),ResultMat3(1,2),0,
ResultMat3(2,0),ResultMat3(2,1),ResultMat3(2,2),0,
0,0,0,1;
return Result;
}
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio,
float zNear, float zFar)
{
// Students will implement this function
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
// TODO: Implement this function
// Create the projection matrix for the given parameters.
// Then return it.
eye_fov=eye_fov/180*MY_PI;
projection<<1/(aspect_ratio*tan(eye_fov/2.0f)) ,0,0,0,
0,1/tan(eye_fov/2.0f),0,0,
0,0,-(zFar+zNear)/(zFar-zNear),2*zFar*zNear/(zNear-zFar),
0,0,-1,0;
return projection;
}
//https://zhuanlan.zhihu.com/p/448904350
//理解过程见上诉博客,我直接带的透视投影公式
知识点:
正交投影
透视投影:
补充:我使用的本地的VS,作业中的文件如下图所示。