-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
166 lines (128 loc) · 4.43 KB
/
main.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 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();
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.
float n = zNear;
float f = zFar;
float t = -tan( (eye_fov/360)*MY_PI ) * abs(n); //解决颠倒问题 http://games-cn.org/forums/topic/%e7%bb%93%e6%9e%9c%e5%87%ba%e7%8e%b0%e4%b8%8a%e4%b8%8b%e9%a2%a0%e5%80%92%ef%bc%8c%e6%80%8e%e4%b9%88%e5%8a%9e%ef%bc%9f%e5%8f%aa%e5%8a%a0%e4%b8%80%e4%b8%aa%e8%b4%9f%e5%8f%b7%e5%8d%b3%e5%8f%af/
float b = t * (-1);
float r = aspect_ratio * t;
float l = r * (-1);
// matric for orthographic
Eigen::Matrix4f ortho_scale = Eigen::Matrix4f::Identity();
Eigen::Matrix4f ortho_trans = Eigen::Matrix4f::Identity();
ortho_scale(0,0) = 2/(r-l);
ortho_scale(1,1) = 2/(t-b);
ortho_scale(2,2) = 2/(n-f);
ortho_trans(0,3) = (-1) * (r+l)/2;
ortho_trans(1,3) = (-1) * (t+b)/2;
ortho_trans(2,3) = (-1) * (n+f)/2;
Eigen::Matrix4f ortho = ortho_scale * ortho_trans;
// persp -> ortho matrix
Eigen::Matrix4f ptom = Eigen::Matrix4f::Zero();
ptom(0,0) = n;
ptom(1,1) = n;
ptom(3,2) = 1;
ptom(2,2) = n+f;
ptom(2,3) = (-1)*n*f;
// cal projection matrix
projection = ortho * ptom;
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