// clang-format off #include #include #include "rasterizer.hpp" #include "global.hpp" #include "Triangle.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(); return model; } Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar) { // TODO: Copy-paste your implementation from the previous assignment. Eigen::Matrix4f projection = Eigen::Matrix4f::Identity(); Eigen::Matrix4f M_persp2ortho(4, 4); Eigen::Matrix4f M_ortho_scale(4, 4); Eigen::Matrix4f M_ortho_trans(4, 4); float angle = eye_fov * MY_PI / 180.0; float height = zNear * tan(angle) * 2; float width = height * aspect_ratio; auto t = -zNear * tan(angle / 2); // 上截面 auto r = t * aspect_ratio; //右截面 auto l = -r; // 左截面 auto b = -t; // 下截面 // 透视矩阵"挤压" M_persp2ortho << zNear, 0, 0, 0, 0, zNear, 0, 0, 0, 0, zNear + zFar, -zNear * zFar, 0, 0, 1, 0; // 正交矩阵-缩放 M_ortho_scale << 2 / (r - l), 0, 0, 0, 0, 2 / (t - b), 0, 0, 0, 0, 2 / (zNear - zFar), 0, 0, 0, 0, 1; // 正交矩阵-平移 M_ortho_trans << 1, 0, 0, -(r + l) / 2, 0, 1, 0, -(t + b) / 2, 0, 0, 1, -(zNear + zFar) / 2, 0, 0, 0, 1; Eigen::Matrix4f M_ortho = M_ortho_scale * M_ortho_trans; projection = M_ortho * M_persp2ortho * projection; return projection; } int main(int argc, const char** argv) { float angle = 0; bool command_line = false; std::string filename = "output.png"; if (argc == 2) { command_line = true; filename = std::string(argv[1]); } rst::rasterizer r(700, 700); Eigen::Vector3f eye_pos = {0,0,5}; std::vector pos { {2, 0, -2}, {0, 2, -2}, {-2, 0, -2}, {3.5, -1, -5}, {2.5, 1.5, -5}, {-1, 0.5, -5} }; std::vector ind { {0, 1, 2}, {3, 4, 5} }; std::vector cols { {217.0, 238.0, 185.0}, {217.0, 238.0, 185.0}, {217.0, 238.0, 185.0}, {185.0, 217.0, 238.0}, {185.0, 217.0, 238.0}, {185.0, 217.0, 238.0} }; auto pos_id = r.load_positions(pos); auto ind_id = r.load_indices(ind); auto col_id = r.load_colors(cols); 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, col_id, rst::Primitive::Triangle); cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data()); image.convertTo(image, CV_8UC3, 1.0f); cv::cvtColor(image, image, cv::COLOR_RGB2BGR); 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, col_id, rst::Primitive::Triangle); cv::Mat image(700, 700, CV_32FC3, r.frame_buffer().data()); image.convertTo(image, CV_8UC3, 1.0f); cv::cvtColor(image, image, cv::COLOR_RGB2BGR); cv::imshow("image", image); key = cv::waitKey(10); std::cout << "frame count: " << frame_count++ << '\n'; } return 0; } // clang-format on