-
Notifications
You must be signed in to change notification settings - Fork 0
/
polygon.hpp
168 lines (135 loc) · 3.9 KB
/
polygon.hpp
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
#ifndef POLY_H
#define POLY_H
#include "vec3.hpp"
#include "vertex.hpp"
#include "vec2.hpp"
#include "ray.hpp"
#include <math.h>
#include "shadingModel.hpp"
#include "hittable.hpp"
#include <vector>
#include "vertex.hpp"
using namespace std;
const double ZERO_BIAS = 0.0001;
const int SHADE_SMOOTH = 0;
class polygon : public hittable
{
public:
polygon(vector<shared_ptr<vertex>> vertices, shadingModel* material)
{
this->material = material;
this->normal = compute_normal(vertices);
this->distance = compute_dist(vertices.at(0), normal);
this->vertices = vertices;
calculate_extents();
}
bool hit(const ray& cam_ray, double t_min, double t_max, hit_record& rec) override;
private:
shadingModel* material;
point3 normal;
double distance;
vector<shared_ptr<vertex>> vertices;
void calculate_extents() override;
vec3 compute_normal(vector<shared_ptr<vertex>> vertices)
{
vec3 v1 = *vertices.at(1) - *vertices.at(0);
vec3 v2 = *vertices.at(2) - *vertices.at(0);
return unit_vector(cross(v1, v2));
}
double compute_dist(shared_ptr<vertex> point, vec3 normal)
{
double d = -(point->x() * normal.x()+ point->y() * normal.y() + point->z() * normal.z());
return d;
}
};
bool polygon::hit(const ray& cam_ray, double t_min, double t_max, hit_record& rec)
{
//supporting plane tests
auto denom = dot(normal,cam_ray.direction());
if (denom < ZERO_BIAS && denom > -ZERO_BIAS)
{
return false;
}
auto d = dot(normal, *vertices.at(0));
auto t = (d - dot(normal, cam_ray.origin())) / denom;
if (t < t_min || t > t_max)
{
return false;
}
auto Q = cam_ray.at(t);
//inside triangle tests and barycentric numerators
auto BA = *vertices.at(1) - *vertices.at(0);
auto numer_1 = dot(cross(BA, (Q - *vertices.at(0))), normal);
auto numer_2 = dot(cross((*vertices.at(2) - *vertices.at(1)), (Q - *vertices.at(1))), normal);
auto numer_3 = dot(cross((*vertices.at(0) - *vertices.at(2)), (Q - *vertices.at(2))), normal);
if (numer_1 < 0)
{
return false;
}
if (numer_2 < 0)
{
return false;
}
if (numer_3 < 0)
{
return false;
}
auto bary_denom = dot(cross(BA, (*vertices.at(2) - *vertices.at(0))), normal);
auto alpha = numer_2 / bary_denom;
auto beta = numer_3 / bary_denom;
auto gamma = numer_1 / bary_denom;
if (SHADE_SMOOTH)
{
auto bary_norm = (alpha * vertices.at(0)->n) + (beta * vertices.at(1)->n) + (gamma * vertices.at(2)->n);
normal = bary_norm/bary_norm.length();
}
rec.set_face_normal(cam_ray, normal);
rec.d = cam_ray.direction();
rec.t = t;
rec.p = Q;
rec.material = material;
rec.uv = alpha * vertices.at(0)->uv + beta * vertices.at(1)->uv + gamma * vertices.at(2)->uv;
return true;
}
void polygon::calculate_extents()
{
double max_x = 0;
double max_y = 0;
double max_z = 0;
double min_x = 0;
double min_y = 0;
double min_z = 0;
for (shared_ptr<point3> point : vertices)
{
//x component
if (point->x() > max_x)
{
max_x = point->x();
}
else if(point->x() < min_x)
{
min_x = point->x();
}
//y component
if (point->y() > max_y)
{
max_y = point->y();
}
else if(point->y() < min_y)
{
min_y = point->y();
}
//z component
if (point->z() > max_z)
{
max_z = point->z();
}
else if(point->z() < min_z)
{
min_z = point->z();
}
}
this->lower_extent = point3(min_x, min_y, min_z);
this->upper_extent = point3(max_x, max_y, max_z);
}
#endif