Games101Homework【1】Rotation and Projection

 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!

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值