1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
| #include "Triangle.hpp" #include "rasterizer.hpp" #include <eigen3/Eigen/Eigen> #include <iostream> #include <opencv2/opencv.hpp> #include <cmath>
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();
Eigen::Matrix4f rotate; float rotation_radian = rotation_angle * MY_PI / 180.0f; rotate << std::cos(rotation_radian), -std::sin(rotation_radian), 0, 0, std::sin(rotation_radian), std::cos(rotation_radian), 0, 0, 0, 0, 1, 0, 0, 0, 0, 1;
model = rotate * model;
return model; }
Eigen::Matrix4f get_projection_matrix(float eye_fov, float aspect_ratio, float zNear, float zFar) {
Eigen::Matrix4f projection = Eigen::Matrix4f::Identity();
Eigen::Matrix4f projToOrth, orth; projToOrth << -zNear, 0, 0, 0, 0, -zNear, 0, 0, 0, 0, -zNear - zFar, -zNear * zFar, 0, 0, 1, 0; float t = std::tan(eye_fov/2) * std::abs(zNear); float r = aspect_ratio * t; orth << 1 / r, 0, 0, 0, 0, 1 / t, 0, 0, 0, 0, 2 / (zFar - zNear), 0, 0, 0, 0, 1;
projection = projection * orth * projToOrth;
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]); if (argc == 4) { filename = std::string(argv[3]); } }
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; }
|