网课:GAMES101-homewor1

本次作业主要是补充两个函数,实现的目标是一个三角形绕着一个轴旋转.主要是使用课上PPT4里面涉及的内容.

主要讲解了三个矩阵,MVP.
对应在计算机里的拍照,实际上是这三步:

Model:把要成像的物体摆好位置。
View:摆好照相机的位置,对准。
Projection:成像!
它们分别对应着三种变换,这就是 MVP transformation,我们目前要做的,就是搞清楚它们是怎么变换的。

第一个模型的变化矩阵的旋转矩阵是最简单的,直接从2维度的情况推演过来就可以了.
在这里插入图片描述

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.
    model(0,0) = cos(rotation_angle/180*acos(-1));
    model(0,1) = -sin(rotation_angle/180*acos(-1));
    model(1,0) = sin(rotation_angle/180*acos(-1));
    model(1,1) = cos(rotation_angle/180*acos(-1));
    return model;
}

升级难度的版本中,要绕任意轴角度angle来设计,这里主要是有一个罗德里格斯旋转公式.
在这里插入图片描述

Eigen::Matrix4f get_rotation(Eigen::Vector3f axis,float rotation_angle)
{
    rotation_angle = rotation_angle/180*acos(-1);
    Eigen::Matrix3f ones = Eigen::Matrix3f::Identity();
    Eigen::Matrix3f temp;
    temp << 0,-axis[2],axis[1],
            axis[2],0,-axis[0],
            -axis[1],axis[0],0;
    temp = cos(rotation_angle)*ones + (1-cos(rotation_angle))*axis*axis.transpose() + sin(rotation_angle)*temp;
    Eigen::Matrix4f res= Eigen::Matrix4f::Identity();
//    res.topLeftCorner<3,3>() = temp;
//    res.block(0,0,3,3) = temp.block(0,0,3,3);
    res.block(0,0,3,3) = temp;
    return res;
}

投影三步走:透视投影到正交投影,平移中心到原点,放缩到[-1,1]

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.
    
//    return projection;
    // TODO: Copy-paste your implementation from the previous assignment.
    zNear *=-1;
    zFar *=-1;
    std::cout<<"zNear is "<<zNear<<std::endl;
    std::cout<<"zFar is "<<zFar<<std::endl;
    Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
    Eigen::Matrix4f m;
    m<<zNear,0,0,0,
            0,zNear,0,0,
            0,0,zNear+zFar,-zNear*zFar,
            0,0,1,0;
    float halve = eye_fov/2*MY_PI/180;
    float top = tan(halve)*zNear;
    float bottom = -top;
    float right = top*aspect_ratio;
    float left = -right;
    Eigen::Matrix4f n,p;
    n<<2/(right-left),0,0,0,
            0,2/(top-bottom),0,0,
            0,0,2/(zNear-zFar),0,
            0,0,0,1;
    p<<1,0,0,-(right+left)/2,
            0,1,0,-(top+bottom)/2,
            0,0,1,-(zFar+zNear)/2,
            0,0,0,1;
    projection = n*p*m;
    return projection;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

参考博客:
从零开始学图形学:MVP Transformation

games101 第一次作业 三角形的光栅化

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值