Games101作业答案:作业1

作业1,第一个正式作业,做出一个rasterizer(光栅化器),粗浅的说就是把一个空间上的物体显示在屏幕上,闫老师那边已经给我们做出来框架,需要我们补充的是:

  1. 模型变换函数,这里指三角形绕Z轴旋转的旋转变换
  2. 视角变换函数,这里指将模型放置在给出的人眼位置的平移操作
  3. 透视投影变换函数,这里指将原z轴处的模型投影到近z处的[-1,1]^3

前两个分别代入绕Z轴的旋转矩阵和平移变换矩阵即可,最后一个稍有难度,根据课程我们已知透视投影变换需先做从透视投影到正交投影的变换①,再做正交投影变换②。根据函数给出的参数,①对应的矩阵很好写出,但是②矩阵需要计算r,l,t,b,便需要利用宽高比ratio和可看角度fov进行计算,如下图所示,可以自行推导一下,详见下方代码:

另有一提高作业,绕任一过原点轴旋转,这个需要利用老师之前所讲的任意轴旋转的旋转公式,要注意的是,该公式给出的旋转矩阵是3x3的,需要对第四列、第四行单独赋值。

#include "Triangle.hpp"
#include "rasterizer.hpp"
#include <eigen3/Eigen/Eigen>
#include <iostream>
#include <opencv2/opencv.hpp>

constexpr double MY_PI = 3.1415926;

Eigen::Matrix4f get_view_matrix(Eigen::Vector3f eye_pos)
{
    Eigen::Matrix4f view = Eigen::Matrix4f::Identity();

    Eigen::Matrix4f translate;
    translate << 1, 0, 0, -eye_pos[0], 0, 1, 0, -eye_pos[1], 0, 0, 1,
        -eye_pos[2], 0, 0, 0, 1;

    view = translate * view;

    return view;
}

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 translate;

    translate << cos(rotation_angle/180*acos(-1)), -sin(rotation_angle/180*acos(-1)), 0, 0,
                 sin(rotation_angle/180*acos(-1)), cos(rotation_angle/180*acos(-1)), 0, 0,
                 0, 0, 1, 0, 0, 0, 0, 1;

    model = translate * model;    

    return model;
}

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.
    // perspective to orthographic
    Eigen::Matrix4f p2o;
    p2o << zNear,0,0,0, 
           0,zNear,0,0, 
           0,0,zNear+zFar,-zNear*zFar, 
           0,0,1,0;
    float zTop = tan(eye_fov*0.5)*abs(zNear);
    float zRight = zTop*aspect_ratio;
    float zLeft = -zRight;
    float zBottom = -zTop;

    Eigen::Matrix4f orthoT;
    orthoT << 1,0,0,0,
              0,1,0,0,
              0,0,1,-(zNear+zFar)*0.5,
              0,0,0,1;

    Eigen::Matrix4f orthoS;
    orthoS << 2/(zRight-zLeft),0,0,0,
              0,2/(zTop-zBottom),0,0,
              0,0,2/(zNear-zFar),0,
              0,0,0,1;
    projection = orthoS * orthoT * p2o * projection;
    return projection;
}

Eigen::Matrix4f get_rotation(Vector3f axis,float angle){
    Eigen::Matrix4f rotate = Eigen::Matrix4f::Identity();
    Eigen::Matrix3f A1;
    Eigen::Matrix3f A2;
    Eigen::Matrix3f A3;
    A1 << 1,0,0,
    0,1,0,
    0,0,1;
    A1 = cos(angle/180*acos(-1))*A1;
    A2 = axis * axis.transpose();
    A2 = (1-cos(angle/180*acos(-1)))*A2;
    float nx = axis[0], ny = axis[1], nz = axis[2];
    A3 << 0,-nz,ny,
    nz,0,-nx,
    -ny,nx,0;
    A3 = sin(angle/180*acos(-1))*A3;
    rotate.block<3,3>(0,0) = A1+A2+A3;
    rotate(0,3) = 0.0f;
    rotate(1,3) = 0.0f;
    rotate(2,3) = 0.0f;
    rotate(3,3) = 1.0f;
    return rotate;
}

int main(int argc, const char** argv)
{
    float angle = 0;
    bool command_line = false;
    std::string filename = "output.png";

    if (argc >= 3) {
        command_line = true;
        angle = std::stof(argv[2]); // -r by default
        if (argc == 4) {
            filename = std::string(argv[3]);
        }
        else
            return 0;
    }
   // 屏幕大小
    rst::rasterizer r(700, 700);
    // 人眼位置
    Eigen::Vector3f eye_pos = {0, 0, 5};
    // 三角形的三个顶点
    std::vector<Eigen::Vector3f> pos{{2, 0, -2}, {0, 2, -2}, {-2, 0, -2}};

    std::vector<Eigen::Vector3i> ind{{0, 1, 2}};

    auto pos_id = r.load_positions(pos);
    auto ind_id = r.load_indices(ind);

    int key = 0;
    int frame_count = 0;

    if (command_line) {
        r.clear(rst::Buffers::Color | rst::Buffers::Depth);

        r.set_model(get_model_matrix(angle));
        r.set_view(get_view_matrix(eye_pos));
        r.set_projection(get_projection_matrix(45, 1, 0.1, 50));

        r.draw(pos_id, ind_id, rst::Primitive::Triangle);
        cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());
        image.convertTo(image, CV_8UC3, 1.0f);

        cv::imwrite(filename, image);

        return 0;
    }

    while (key != 27) {
        r.clear(rst::Buffers::Color | rst::Buffers::Depth);
        //绕Z轴
        //r.set_model(get_model_matrix(angle));
        //绕axis轴
	    Eigen::Vector3f axis(2.0f,3.0f,3.0f);
	    r.set_model(get_rotation(axis,angle));

        r.set_view(get_view_matrix(eye_pos));
        r.set_projection(get_projection_matrix(45, 1, 0.1, 50));

        r.draw(pos_id, ind_id, rst::Primitive::Triangle);

        cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data());
        image.convertTo(image, CV_8UC3, 1.0f);
        cv::imshow("image", image);
        key = cv::waitKey(10);

        std::cout << "frame count: " << frame_count++ << '\n';

        if (key == 'a') {
            angle += 10;
        }
        else if (key == 'd') {
            angle -= 10;
        }
    }

    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值