games101-hw/02/代码框架/main.cpp

166 lines
4.1 KiB
C++
Raw Permalink Normal View History

2023-06-19 17:02:04 +08:00
// clang-format off
#include <iostream>
#include <opencv2/opencv.hpp>
#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();
float r = rotation_angle/180.0* MY_PI;
model <<
cos(r),-sin(r),0,0,
sin(r),cos(r),0,0,
0,0,1,0,
0,0,0,1;
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 pro_to_ortho = Eigen::Matrix4f::Identity();
pro_to_ortho <<
zNear,0,0,0,
0,zNear,0,0,
0,0,zNear+zFar,-zNear*zFar,
0,0,1,0;
float half_eye_fov = eye_fov/2/180.0*MY_PI;
float top =zNear*tan(half_eye_fov);
float bottom = -top;
float right = aspect_ratio*top;
float left = -right;
Eigen::Matrix4f ortho_translate = Eigen::Matrix4f::Identity();
ortho_translate <<
2/(right-left),0,0,0,
0,2/(top-bottom),0,0,
0,0,2/(zNear-zFar),0,
0,0,0,1;
Eigen::Matrix4f ortho_scale = Eigen::Matrix4f::Identity();
ortho_scale<<
2/(right-left),0,0,0,
0,2/(top-bottom),0,0,
0,0,2/(zNear-zFar),-(zNear*zFar),
0,0,1,0;
projection = ortho_scale*ortho_translate*pro_to_ortho;
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<Eigen::Vector3f> 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<Eigen::Vector3i> ind
{
{0, 1, 2},
{3, 4, 5}
};
std::vector<Eigen::Vector3f> 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