DO:
model:
scale,rotation and translation:
you only need to learn about(copy)the scale/rotation/translation matrix then multiply it and identity matrix!
projection:
Let's look at what we're going to do:
We need to solve for the projection matrix,then multiply projection matrix and orthogonal matrix!
orthogonal matrix:
we need totranslate,and scale into “canonical” cube:
Represents the distance from the origin to each surface:
we have fov,ratio,zNear and zFar,
t,b:top,bottom
l,r:left,right
t=tan(fov/2)*ZNear;
b=-t;
r=ratio*t;
l=-r;
You can see how it works by looking at this diagram:
let us use it!
projection matrix:
The position of each point after it has been squashed:
you just use this matrix:
n,0,0,0,
0,n,0, 0,
0, 0,n+f,-nf,
0,0,1,0;
Rotate around any axis:
I don't know where this formula comes from,but is right!
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 rotation;
float angle = rotation_angle*MY_PI/180.0;
rotation<<std::cos(angle),-std::sin(angle),0,0,
std::sin(angle),std::cos(angle),0,0,
0,0,1,0,
0,0,0,1;
return model*rotation;
}
projection:
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();
Eigen::Matrix4f ms,mt,mp;
float r,l,
t,b,
n,f;
n=zNear;
f=zFar;
float angle =(eye_fov*MY_PI)/180.0;
t=std::tan(angle/2)*-n;
b=-t;
r =aspect_ratio*t;
l=-r;
ms<<2/(r-l),0,0,0,
0,2/(t-b),0,0,
0,0,2/(n-f),0,
0,0,0,1;
mt<<1,0,0,-(r+l)/2,
0,1,0,-(t+b)/2,
0,0,1,-(n+f)/2,
0,0,0,1;
mp<<n,0,0,0,
0,n,0,0,
0,0,n+f,-n*f,
0,0,1,0;
// TODO: Implement this function
// Create the projection matrix for the given parameters.
// Then return it.
return ms*mt*mp*projection;
}
Rotate around any axis:
Eigen::Matrix4f get_rotation(Vector3f axis,float angle){
Eigen::Matrix4f res;
angle=angle*MY_PI/180.0;
Eigen::Matrix4f base,N;
Eigen::RowVector4f rowaxis;
Eigen::Vector4f Naxis;
rowaxis<<axis.x(),axis.y(),axis.z(),0;
Naxis<<axis.x(),axis.y(),axis.z(),0;
base<<1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1;
N<<0,-axis.z(),axis.y(),0,
axis.z(),0,-axis.x(),0,
-axis.y(),axis.x(),0,0,
0,0,0,1;
res =std::cos(angle)*base+(1-std::cos(angle))*Naxis*rowaxis+std::sin(angle)*N;
return res;
}
Replace this matrix with the model matrix.
Result:
Rotate around y axis:
Cool!