games 101 作业1

 先放上代码:

#include "Triangle.hpp"
#include "rasterizer.hpp"
#include <math.h>
#include "Eigen"
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace Eigen;
using namespace std;
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();

    model << cosf(rotation_angle / 360 * 2 * MY_PI), -sinf(rotation_angle / 360 * 2 * MY_PI), 0, 0,
        sinf(rotation_angle / 360 * 2 * MY_PI), cosf(rotation_angle / 360 * 2 * MY_PI), 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1;


    // TODO: Implement this function
    // Create the model matrix for rotating the triangle around the Z axis.
    // Then return it.

    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();
    Matrix4f pro(4, 4);
    pro << zNear, 0, 0, 0,
        0, zNear, 0, 0,
        0, 0, zNear + zFar, -zNear * zFar,
        0, 0, 1, 0;
    Matrix4f orth(4, 4);

    float t = tanf(eye_fov / 2) * zNear * 2;
    float width = aspect_ratio * t;


    Matrix4f move(4, 4);
    move << 1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, -(zFar + zNear) / 2,
        0, 0, 0, 1;
    orth <<
        2 / width, 0, 0, 0,
        0, 2 / t, 0, 0,
        0, 0, 2 / (zFar - zNear), 0,
        0, 0, 0, 1;

    projection = orth * move * pro;

    // TODO: Implement this function
    // Create the projection matrix for the given parameters.
    // Then return it.

    return projection;
}

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

        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::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;
}

易错点: 

数字的正负号,比如这个zNear 和 zFar其实就是表示的距离,是个正数,在矩阵变换的时候要格外小心。

projection matrix 的组成:

首先我们要先进行线性变换,压缩空间使远近平面不一样的四棱台变成一个规则的长方体。

然后移动这个长方体,使得其中心位于原点。

最后再进行拉伸,让其长宽高都在[-1 , 1]的空间之中。

所以我们分别求出对应的三个变换矩阵之后:

projection = orth * move * pro;

名词解释:

eye_fov 与 aspect_ratio :参见 第五讲ppt

aspect_ratio : 宽高比 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值