-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.cpp
298 lines (244 loc) · 7.52 KB
/
renderer.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "renderer.h"
Renderer::Renderer(Scene* scene, Frame* frame) :
scene_(scene),
frame_(frame),
wireframe_(false)
{
}
bool Renderer::Render()
{
// Generate edge list
for each (Polygon* poly in scene_->GetPolygons()) {
if (!poly->GetCull()) {
for each (Edge* edge in poly->GetEdges()) {
// Add first ever edge
if (edge_list_.size() == 0) {
edge_list_.push_back(edge);
} else {
// Loop though edges and compare to current edge
// Only add to list if it isn't already there
bool exists = false;
for each (Edge* test_edge in edge_list_) {
if (edge->Compare(test_edge)) {
exists = true;
// If we hit an edge more than once it can't be a boundary edge
edge->SetBoundary(false);
test_edge->SetBoundary(false);
}
}
if (!exists) {
edge_list_.push_back(edge);
}
}
}
}
}
// Generate boundary edges
/*
for each (Polygon * poly in scene_->GetPolygons()) {
if (poly->GetCull()) {
for each (Edge* edge in poly->GetEdges()) {
for each (Edge* test_edge in edge_list_) {
if (edge->Compare(test_edge)) {
boundary_edges_.push_back(test_edge);
break;
}
}
}
}
}*/
for each (Edge * edge in edge_list_) {
if (edge->GetBoundary()) {
boundary_edges_.push_back(edge);
}
}
std::cout << "number of Boundary Edges: " << boundary_edges_.size() << std::endl;
// Draw Edges
int edge_count = 0;
for each (Edge * edge in edge_list_) {
//std::cout << std::endl << "--- Line: " << ++edge_count << " ---" << std::endl;
// Calculate QI
int QI = 0;
for each (Polygon * poly in scene_->GetPolygons()) {
if (!poly->GetCull()) {
if (!poly->ContainsEdge(edge)) {
if (FaceVertexCompare(poly, edge->GetA())) {
QI++;
}
}
}
}
//std::cout << "QI: " << QI << std::endl;
// Find intersections
//std::vector<Intersection> intersection_list;
std::map<double, int> intersection_list = BoundaryEdgeCompare(edge);
// Draw Edge
std::vector<vec3> edge_divided_by_w = edge->GetEdgeDividedByW();
if (wireframe_) {
if (intersection_list.size() == 0) {
//continue;
}
frame_->MoveTo(edge_divided_by_w.at(0));
frame_->DrawTo(edge_divided_by_w.at(1));
}
else {
if (QI == 0) {
frame_->MoveTo(edge_divided_by_w.at(0));
}
for (auto it = intersection_list.begin(); it != intersection_list.end(); ++it)
{
// it->first = alpha, it->second = deltaIQ
//std::cout << "alpha: " << it->first << ", deltaIQ: " << it->second << std::endl;
vec3 new_point;
new_point.x = edge_divided_by_w.at(0).x + it->first * (edge_divided_by_w.at(1).x - edge_divided_by_w.at(0).x);
new_point.y = edge_divided_by_w.at(0).y + it->first * (edge_divided_by_w.at(1).y - edge_divided_by_w.at(0).y);
new_point.z = 0.0;
if (QI == 1 && it->second == -1) {
frame_->MoveTo(new_point);
//QI += it->second;
//continue;
}
else if (QI == 0 && it->second == 1) {
frame_->DrawTo(new_point);
//QI += it->second;
//continue;
}
QI += it->second;
if (QI < 0) {
std::cout << "---ERROR--- QI < 0" << std::endl;
}
}
if (QI == 0) {
frame_->DrawTo(edge_divided_by_w.at(1));
}
}
}
return true;
}
bool Renderer::FaceVertexCompare(Polygon* poly, vec4 vertex)
{
bool flag = false;
vec3 v = DivideByW(vertex);
bool on_corner = false;
for each (vec4 test_vertex in poly->GetVertices()) {
if (vertex.x == test_vertex.x && vertex.y == test_vertex.y) {
//std::cout << "Vertex on corner" << std::endl;
flag = true;
on_corner = true;
break;
}
}
if (!flag) {
for each (Edge * edge in poly->GetEdges()) {
std::vector<vec3> scene_edge = edge->GetEdgeDividedByW();
vec3 v_i = scene_edge.at(0);
vec3 v_i_plus_one = scene_edge.at(1);
double d_i = v_i.y - v.y;
double d_i_1 = v_i_plus_one.y - v.y;
if (signbit(d_i * d_i_1) == 0) {
continue;
}
double d_inf = v_i_plus_one.y - v_i.y;
double d = (v_i_plus_one.y - v.y) * (v_i.x - v.x) - (v_i_plus_one.x - v.x) * (v_i.y - v.y);
if (signbit(d * d_inf) == 0) {
continue;
}
flag = !flag;
}
}
// If we cross an edge an odd number of times flag is true and we are inside a polygon
if (flag) {
// Find normal of plane
// Create vector between point on plane and test point if it were on the plane
// Dot product normal and this vector and rearrange to find the z of this imaginary point
// if actual z is greater than imaginary z the point is behind plane as z axis goes away form eye.
vec3 normal = poly->GetScreenNormal();
vec3 point_on_plane = DivideByW(poly->GetVertices().at(0));
//double z = ((-normal.x * (v.x - point_on_plane.x) - normal.y * (v.y - point_on_plane.y)) / normal.z) + point_on_plane.z;
double z = ((normal.x * (v.x - point_on_plane.x) + normal.y * (v.y - point_on_plane.y)) / -normal.z) + point_on_plane.z;
if (on_corner && z != v.z) std::cout << (z - v.z) << std::endl;
// Vec3Print(normal);// std::cout << "DIFF Z: " << z << ", " << v.z << std::endl;
if (abs(z - v.z) < 0.001) {
return false;
}
if (z < v.z) {
//std::cout << "DIFF Z: " << z << ", " << v.z << std::endl;
//Vec3Print(point_on_plane);
//Vec3Print(v);
return true;
}
//std::cout << "DIFF: " << z << ", " << v.z << std::endl;
}
return false;
}
Renderer::Intersection Renderer::EdgeEdgeCompare(Edge* edge, Edge* test_edge)
{
Renderer::Intersection intersection;
intersection.valid = false;
std::vector<vec3> a_screen = edge->GetEdgeDividedByW();
std::vector<vec3> b_screen = test_edge->GetEdgeDividedByW();
double pz = edge->GetA().z;
double qz = edge->GetB().z;
double rz = test_edge->GetA().z;
double sz = test_edge->GetB().z;
vec3 p = a_screen.at(0);
vec3 q = a_screen.at(1);
vec3 r = b_screen.at(1);
vec3 s = b_screen.at(0);
double d_1 = (s.x - r.x) * (p.y - r.y) - (p.x - r.x) * (s.y - r.y);
double d_2 = (s.x - r.x) * (q.y - r.y) - (q.x - r.x) * (s.y - r.y);
if (signbit(d_1 * d_2) == 0) {
return intersection;
}
double d_3 = (p.x - r.x) * (q.y - r.y) - (q.x - r.x) * (p.y - r.y);
double d_4 = d_1 - d_2 + d_3;
if (signbit(d_3 * d_4) == 0) {
return intersection;
}
//std::cout << "d_1: " << d_1 << " d_2: " << d_2 << " d_3: " << d_3 << " d_4: " << d_4 << std::endl;
double alpha = d_1 / (d_1 - d_2);
double beta = d_3 / (d_3 - d_4);
//std::cout << "alpha: " << alpha << " beta: " << beta << std::endl;
if (alpha <= 0 || beta <= 0 || alpha >= 1 || beta >= 1) {
return intersection;
}
//std::cout.precision(17);
//
double z_i = p.z + alpha * (q.z - p.z);
double z_j = s.z + (1-beta) * (r.z - s.z);
//std::cout << "z_i: " << z_i << " z_j:" << z_j << std::endl;
//Vec3Print(p);
//Vec3Print(q);
//Vec3Print(s);
//Vec3Print(r);
if (z_i < z_j) {
return intersection;
}
if (alpha == 1.0) {
//continue;
}
if (abs(z_i - z_j) < 0.001) {
return intersection;;
}
//std::cout << "z_i: " << z_i << " z_j:" << z_j << std::endl;
int deltaIQ = d_1 > 0 ? 1 : -1;
//std::cout << "d_1: " << d_1 << " d_2: " << d_2 << " d_3: " << d_3 << " d_4: " << d_4 << std::endl;
intersection.alpha = alpha;
intersection.deltaIQ = deltaIQ;
intersection.valid = true;
return intersection;
}
std::map<double, int> Renderer::BoundaryEdgeCompare(Edge* edge)
{
std::map<double, int> intersection_list;
for each (Edge * boundary_edge in boundary_edges_) {
if (edge->Compare(boundary_edge)) {
continue;
}
Renderer::Intersection intersection = EdgeEdgeCompare(edge, boundary_edge);
if (intersection.valid) {
intersection_list.emplace(intersection.alpha, intersection.deltaIQ);
}
}
return intersection_list;
}