-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.cpp
170 lines (145 loc) · 3.92 KB
/
Object.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
#include "Object.h"
#include <cmath>
Ray make_ray(float a, float b, float c, float d, float e, float f){
vector<float> x(4,1), y(4,1);
x[0] = a;
x[1] = b;
x[2] = c;
y[0] = d;
y[1] = e;
y[2] = f;
return Ray(x,y);
}
float dot(Ray &p, Ray &q){
auto p_d = p.get_d();
auto q_d = q.get_d();
float ans = 0;
for(int i=0; i<3; ++i){
ans += p_d[i] * q_d[i];
}
return ans/sqrt(get<0>(p.get_abc()) * get<0>(q.get_abc()));
}
void add_vecs(vector<float> &a1, vector<float> &a2)
{
for (int i = 0; i < 3; i++)
a1[i] += a2[i];
}
void mult_const(vector<float> &a, float f)
{
for (int i = 0; i < 3; i++)
a[i] *= f;
}
Object::Object(){
k_ads = vector<float>(3);
color = vector<float>(3);
}
void Object::set_color(int r, int g, int b){
color[2] = std::max(0, std::min(255, r));
color[1] = std::max(0, std::min(255, g));
color[0] = std::max(0, std::min(255, b));
}
Ray Object::reflected(Ray &r, Ray &n){
auto r_d = r.get_d();
auto n_d = n.get_d();
float factor = -2 * dot(r,n) * sqrt(get<0>(r.get_abc()) / get<0>(n.get_abc()));
for(int i=0; i<3; ++i){
n_d[i] = n_d[i] * factor + r_d[i];
}
return Ray(n_d, n.get_p());
}
Ray Object::refracted(Ray &l, Ray &n){
auto l_d = l.get_d();
auto n_d = n.get_d();
float cos_i = dot(l,n);
float cos_t = sqrt(eta*eta - 1 + cos_i*cos_i)/eta;
for(int i=0; i<3; ++i){
l_d[i] = eta * (l_d[i] + cos_i*n_d[i]) - cos_t * n_d[i];
}
return Ray(l_d, n.get_p());
}
Ray Sphere::normal(Ray &r, pair<float, vector<float> > &pr){
return Ray(t.transform_inv_transpose(pr.second), r.get_point(pr.first));
}
pair<float, vector<float> > Sphere::intersection(Ray &r){
Ray t_r = t.transform_inv(r);
auto abc = t_r.get_abc();
get<2>(abc) -= radius*radius;
float disc = get<1>(abc) * get<1>(abc) - get<0>(abc) * get<2>(abc);
if(disc < 0) return make_pair(-2, vector<float>(4,1));
else{
float tt = (-get<1>(abc) - sqrt(disc))/get<0>(abc);
// if(tt < eps*100) tt = (-get<1>(abc) + sqrt(disc))/get<0>(abc);
return make_pair(tt, t_r.get_point(tt));
}
}
Triangle::Triangle()
{
a = vector<float> (4,1);
b = vector<float> (4,1);
c = vector<float> (4,1);
nml = vector<float> (4,1);
}
void Triangle::Calc_Normal()
{
// cross prod (a-b)*(a-c)
float axbx = a[0] - b[0];
float ayby = a[1] - b[1];
float azbz = a[2] - b[2];
float axcx = a[0] - c[0];
float aycy = a[1] - c[1];
float azcz = a[2] - c[2];
nml[0] = ayby*azcz - azbz*aycy;
nml[1] = azbz*axcx - axbx*azcz;
nml[2] = axbx*aycy - ayby*axcx;
a[0] = axcx;
a[1] = aycy;
a[2] = azcz;
b[0] -= c[0];
b[1] -= c[1];
b[2] -= c[2];
}
Ray Triangle::normal(Ray &r, pair<float, vector<float> > &pr){
vector<float> p = r.get_point(pr.first);
auto d = r.get_d();
// cout << "Normal TRI : ";
if(d[0]*nml[0] + d[1]*nml[1] + d[2]*nml[2] < 0)
{
// cout << nml[0] << " " << nml[1] << " " << nml[2] << endl;
return Ray(nml, p);
}
else
{
for (int i = 0; i < 3; i++)
{
nml[i] *= -1;
// cout << nml[i] << " ";
}
// cout << endl;
return Ray(nml,p);
}
}
pair<float, vector<float> > Triangle::intersection(Ray &r){
// p -
// x.print();
// x.print_Inv();
vector<float> s = r.get_d();
vector<float> p = r.get_p();
float b2s1_b1s2 = b[2]*s[1] - b[1]*s[2];
float b0s2_b2s0 = b[0]*s[2] - b[2]*s[0];
float b1s0_b0s1 = b[1]*s[0] - b[0]*s[1];
float denom = a[0]*b2s1_b1s2 + a[1]*b0s2_b2s0 + a[2]*b1s0_b0s1;
float u = (float(1.0)/denom)*( (p[0] - c[0])*b2s1_b1s2 + (p[1] - c[1])*b0s2_b2s0 + (p[2] - c[2])*b1s0_b0s1);
float v = (float(1.0)/denom)*( (p[0] - c[0])*(a[1]*s[2] - a[2]*s[1]) + (p[1] - c[1])*(a[2]*s[0] - a[0]*s[2]) + (p[2] - c[2])*(a[0]*s[1] - a[1]*s[0]) );
float t = (float(1.0)/denom)*( (p[0] - c[0])*(a[1]*b[2] - a[2]*b[1]) + (p[1] - c[1])*(a[2]*b[0] - a[0]*b[2]) + (p[2] - c[2])*(a[0]*b[1] - a[1]*b[0]) );
// for (int i = 0; i < 4; i++)
// cout << uvwt[i] << " ";
if (u >= eps && (v >= eps) && ((1 - u - v) >= eps))
{
// PoI inside triangle!
return make_pair(t,vector<float> (0));
}
else
{
return make_pair(-1,vector<float> (0));
}
}