-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.cpp
110 lines (88 loc) · 2.91 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
#include "Object.h"
Object::~Object() = default;
Sphere::Sphere(Material &material, Vector ¢erVertex, float radius) {
this->material = material;
this->centerVertex = centerVertex;
this->radius = radius;
}
void Sphere::intersect(Ray& ray) {
float t, t1, t2;
float A,B,C;
float delta;
C = (ray.origin-this->centerVertex)*(ray.origin-this->centerVertex) - this->radius*this->radius;
B = (ray.direction*(ray.origin-this->centerVertex))*2;
A = ray.direction*ray.direction;
delta = B*B-4*A*C;
if(delta<0) {
t = -1;
} else if (delta == 0) {
t = -B / (2*A);
} else {
delta = sqrt(delta);
t1 = (-B + delta) / (2.f*A);
t2 = (-B - delta) / (2.f*A);
t = t1 < t2 ? t1 : t2;
}
ray.t = t;
}
void Sphere::normal(Ray &ray) {
ray.n = !(ray.p-this->centerVertex);
}
Sphere::~Sphere() {
//std::cout << "sphere destructed\n";
}
Triangle::Triangle(Material &material, Vector &v0, Vector &v1, Vector &v2) {
this->material = material;
this->v0 = v0;
this->v1 = v1;
this->v2 = v2;
this->n = !((this->v2-this->v1)&(this->v0-this->v1));
}
void Triangle::intersect(Ray &ray) {
float A, alpha, beta, t;
A = det(this->v0.x-this->v1.x,this->v0.y-this->v1.y,this->v0.z-this->v1.z,
this->v0.x-this->v2.x,this->v0.y-this->v2.y,this->v0.z-this->v2.z,
ray.direction.x, ray.direction.y, ray.direction.z);
beta = det(this->v0.x-ray.origin.x,this->v0.y-ray.origin.y,this->v0.z-ray.origin.z,
this->v0.x-this->v2.x,this->v0.y-this->v2.y,this->v0.z-this->v2.z,
ray.direction.x, ray.direction.y, ray.direction.z) / A;
alpha = det(this->v0.x-this->v1.x,this->v0.y-this->v1.y,this->v0.z-this->v1.z,
this->v0.x-ray.origin.x,this->v0.y-ray.origin.y,this->v0.z-ray.origin.z,
ray.direction.x, ray.direction.y, ray.direction.z) / A;
t = det(this->v0.x-this->v1.x,this->v0.y-this->v1.y,this->v0.z-this->v1.z,
this->v0.x-this->v2.x,this->v0.y-this->v2.y,this->v0.z-this->v2.z,
this->v0.x-ray.origin.x,this->v0.y-ray.origin.y,this->v0.z-ray.origin.z) / A;
if(t>= 0 && t<= MAX_T && alpha + beta <= 1 && alpha >= 0 && beta >= 0) {
ray.t = t;
} else {
ray.t = -1;
}
}
void Triangle::normal(Ray &ray) {
ray.n = this->n;
}
Triangle::~Triangle() {
//std::cout << "triangle destructed\n";
}
Mesh::Mesh(Material &material) {
this->material = material;
}
void Mesh::intersect(Ray &ray) {
float minT = MAX_T;
for(auto triangle : this->triangles){
triangle->intersect(ray);
if(ray.t != -1 && ray.t < minT) {
minT = ray.t;
this->n = triangle->n;
}
}
ray.t = minT;
}
void Mesh::normal(Ray &ray) {
ray.n = this->n;
}
Mesh::~Mesh() {
for(auto triangle : this->triangles)
delete triangle;
//std::cout << "mesh destructed\n";
}