-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.cpp
More file actions
200 lines (195 loc) · 7.68 KB
/
Copy pathTriangle.cpp
File metadata and controls
200 lines (195 loc) · 7.68 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include<iostream>
#include"Pixel.h"
#include"Tooldev.h"
#include<Eigen/Core>
#include"Triangle.h"
#include"Vertex.h"
#define WIDTH 512.0f
#define HEIGHT 512.0f
#define LEFT -1
#define CENTER 0
#define RIGHT 1
#define OUTSIDE 2
#define CENT 3
#define INSIDE 4
using namespace Eigen;
Triangle::Triangle(const Vector2f& p1, const Vector2f& p2, const Vector2f& p3, const std::string&& p1_color, const std::string&& p2_color, const std::string&& p3_color,float de_p1, float de_p2, float de_p3) :point1(p1), point2(p2), point3(p3),depth_p1(de_p1), depth_p2(de_p2), depth_p3(de_p3) {
p1p2 << p2 - p1;
p2p3 << p3 - p2;
p3p1 << p1 - p3;
turn_3points_color(std::move(p1_color), std::move(p2_color), std::move(p3_color));
std::vector<float> boundbox = get_boundbox();
float max_x, max_y, min_x, min_y;
// x_max,y_max,x_min,y_min
max_x = boundbox[0];
max_y = boundbox[1];
min_x = boundbox[2];
min_y = boundbox[3];
check_bound_width2D(max_x);
check_bound_width2D(min_x);
check_bound_height(max_y);
check_bound_height(min_y);
x_max = max_x;
y_max = max_y;
x_min = min_x;
y_min = min_y;
}
Triangle::Triangle(const Matrix4f& mat_VP,Vertex& v_p1, Vertex& v_p2, Vertex& v_p3) {
Vector2f_1_w pt1 = world_to_screen(mat_VP, v_p1, WIDTH, HEIGHT);
Vector2f_1_w pt2 = world_to_screen(mat_VP, v_p2, WIDTH, HEIGHT);
Vector2f_1_w pt3 = world_to_screen(mat_VP, v_p3, WIDTH, HEIGHT);
point1 = pt1.pos;
point2 = pt2.pos;
point3 = pt3.pos;
inv_1 = pt1.inv_w;
inv_2 = pt2.inv_w;
inv_3 = pt3.inv_w;
p1p2 << point2 - point1;
p2p3 << point3 - point2;
p3p1 << point1 - point3;
// insert color
px1 = v_p1.get_px();
px2 = v_p2.get_px();
px3 = v_p3.get_px();
std::vector<float> boundbox = get_boundbox();
float max_x, max_y, min_x, min_y;
// x_max,y_max,x_min,y_min
max_x = boundbox[0];
max_y = boundbox[1];
min_x = boundbox[2];
min_y = boundbox[3];
check_bound_width2D(max_x);
check_bound_width2D(min_x);
check_bound_height(max_y);
check_bound_height(min_y);
x_max = max_x;
y_max = max_y;
x_min = min_x;
y_min = min_y;
}
void Triangle::turn_3points_color(const std::string&& p1_color, const std::string&& p2_color, const std::string&& p3_color) {
px1.turn_color(std::move(p1_color));
px2.turn_color(std::move(p2_color));
px3.turn_color(std::move(p3_color));
}
void Triangle::draw_black(std::vector<Pixel>& framebuffer) {
for (int y = y_min; y <= y_max; y++) {
for (int x = x_min; x <= x_max; x++) {
//(x,y)
Vector2f p;
p << (x + 0.5f), (y + 0.5f);
if (get_point_to_triggle_location(p) == INSIDE) {
framebuffer[y * WIDTH + x].turn_color(std::move("black"));
}
}
}
}
void Triangle::draw_direct_color(std::vector<Pixel>& framebuffer, std::string&& direct_color) {
for (int y = y_min; y <= y_max; y++) {
for (int x = x_min; x <= x_max; x++) {
//(x,y)
Vector2f p;
p << (x + 0.5f), (y + 0.5f);
if (get_point_to_triggle_location(p) == INSIDE) {
framebuffer[y * WIDTH + x].turn_color(std::move(direct_color));
}
}
}
}
void Triangle::draw(std::vector<Pixel>& framebuffer) {
for (int y = y_min; y <= y_max; y++) {
for (int x = x_min; x <= x_max; x++) {
// v1 (x,y)
Vector2f p;
p << (x + 0.5f), (y + 0.5f);
if (get_point_to_triggle_location(p) == INSIDE || get_point_to_triggle_location(p) == CENT) {
std::vector<float> p1p2p3 = get_weight_location_Line(p);
Pixel curr = Pixel::G_color_depth(px1, px2, px3, p1p2p3, depth_p1, depth_p2, depth_p3);
if (framebuffer[y * WIDTH + x].depth > curr.depth) {
Pixel::change_pixel(framebuffer[y * WIDTH + x], curr);
}
}
}
}
}
int Triangle::get_point_to_vector_location(const Vector2f& p, const Vector2f& vpp1, const Vector2f& start_point) {
Vector2f p_vpp2, edge1;
const float eps = 1e-6f;
p_vpp2 << (p.x() - start_point.x()), (p.y() - start_point.y());
edge1 = vpp1.normalized();
auto cross_result = cross2D(edge1, p_vpp2);
if (fabs(cross_result) < eps) {
return CENTER;
}
else if (cross_result > eps) {
return LEFT;
}
else {
return RIGHT;
}
}
int Triangle::get_point_to_triggle_location(const Vector2f& p) {
if ((get_point_to_vector_location(p, p1p2, point1) == LEFT && get_point_to_vector_location(p, p2p3, point2) == LEFT && get_point_to_vector_location(p, p3p1, point3) == LEFT)|| (get_point_to_vector_location(p, p1p2, point1) == RIGHT && get_point_to_vector_location(p, p2p3, point2) == RIGHT && get_point_to_vector_location(p, p3p1, point3) == RIGHT)) {
return INSIDE;
}
else if (get_point_to_vector_location(p, p1p2, point1) == CENTER || get_point_to_vector_location(p, p2p3, point2) == CENTER || get_point_to_vector_location(p, p3p1, point3) == CENTER) {
return CENT;
}
else {
return OUTSIDE;
}
}
std::vector<float> Triangle::get_boundbox() {
std::vector<float> arrx;
std::vector<float> arry;
arrx.resize(3);
arry.resize(3);
arrx[0] = point1.x();
arrx[1] = point2.x();
arrx[2] = point3.x();
arry[0] = point1.y();
arry[1] = point2.y();
arry[2] = point3.y();
auto x_min = std::min({ arrx[0],arrx[1] ,arrx[2] });
auto y_min = std::min({ arry[0],arry[1] ,arry[2] });
auto x_max = std::max({ arrx[0],arrx[1] ,arrx[2] });
auto y_max = std::max({ arry[0],arry[1] ,arry[2] });
// Êä³öÒ»¸övector(x_max,y_max,x_min,y_min)
std::vector<float> max_min_xy;
max_min_xy.push_back(x_max);
max_min_xy.push_back(y_max);
max_min_xy.push_back(x_min);
max_min_xy.push_back(y_min);
return max_min_xy;
}
std::vector<float> Triangle::get_weight_location_Line(const Vector2f& p) {
//·ÂÉä²åÖµ
float p1p2p, p1p3p, p2p3p, total;
p1p2p = get_triangle_area(point1, point2, p);
p1p3p = get_triangle_area(point1, point3, p);
p2p3p = get_triangle_area(point2, point3, p);
total = get_triangle_area(point1, point2, point3);
float u = p1p2p / total; // p3
float v = p1p3p / total; // p2
float w = 1 - u - v; // p1
std::vector<float> p1p2p3;
p1p2p3.resize(3);
p1p2p3[0] = w;
p1p2p3[1] = v;
p1p2p3[2] = u;
return p1p2p3;
}
std::vector<float> Triangle::get_weight_location_perspective(const Vector2f& p) {
std::vector<float> g_weight = get_weight_location_Line(p);
float k1 = g_weight[0];
float k2 = g_weight[1];
float k3 = g_weight[2];
float w1 = k1 * inv_1;
float w2 = k2 * inv_2;
float w3 = k3 * inv_3;
float sum = w1 + w2 + w3;
w1 /= sum;
w2 /= sum;
w3 /= sum;
return { w1,w2,w3 };
}