-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject.cpp
88 lines (74 loc) · 1.72 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
#include "object.h"
#include <cmath>
Material::Material()
{
this->kr = 0.5;
this->ks = 0.35;
this->kd = 0.35;
this->n_shiny = 1.5;
this->color = randVec();
}
Material::Material(const Vec&color)
{
this->color = color;
}
Material::Material(const Material &material)
{
*this = material;
}
void Material::setKs(float ks, float ka)
{
this->ks = ks;
this->kd = (1.0 - ka) - ks;
}
bool
Sphere::intersectRay(const Ray &ray, Vec &point, double &distance)const
{
float b, c, t[2];
float delta;
Vec vecAux = ray.orig - pos;
b = glm::dot(ray.dir, vecAux)*2.0;
if(b > 0.0 || b == 0.0) //esfera "atras do observador" ou observador dentro da esfera
return false;
c = glm::dot(vecAux, vecAux) - (this->radius*this->radius);
delta = b*b - 4.0f*c;
if(delta < 0.0)
return false;
else if(delta > 0)
{
t[0] = (float)(-b + sqrt(delta))/(2.0f);
t[1] = (float)(-b - sqrt(delta))/(2.0f);
if(t[0] < t[1])
distance = t[0];
else
distance = t[1];
}else{
distance = -b/(2.0f);
}
point = ray.orig + ray.dir*(float)distance;
return true;
}
bool
Plane::intersectRay(const Ray &ray, Vec &point, double &distance)const
{
//equacao do plano: Ax + By + Cz + D = 0; normal = <A,B,C>
float n_dot_v = glm::dot(Normal, ray.dir);
float t;
if(n_dot_v == 0.0) //caso em que não tem intersecao ou que esta 'atras' do plano
return false;
t = glm::dot(pos - ray.orig, Normal)/n_dot_v;
if(t < 0.01)
return false;
point = ray.dir*(float)t + ray.orig;
distance = t;
return true;
}
void
Sphere::normalAt(const Vec &point, Vec &normal)const
{
normal = glm::normalize(point-pos);
}
void Plane::normalAt(const Vec &point, Vec &normal)const
{
normal = glm::normalize(this->Normal);
}