-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsphere.cpp
More file actions
92 lines (82 loc) · 3.42 KB
/
sphere.cpp
File metadata and controls
92 lines (82 loc) · 3.42 KB
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
//
// Created by Florent on 10/09/2018.
//
#include "sphere.hpp"
#include "rayon.h"
/**
* It computes an intersection between a ray and the sphere.
* @param rayon the ray to launch
* @param I a pointer to a std::vector of intersection.
* @return it computes correctly or not.
*/
bool Sphere::calculIntersection(const Rayon& rayon, const Scene& sc, std::vector<Intersection>& I, int rec) {
//Équation : a = 1, b = 2* (D.(O - C)), c =|O - C|² - R²
/*glm::vec3 OCVec = rayon.Orig() - this->center;
float a = 1,
b = 2 * glm::dot(rayon.Vect(), OCVec),
c = glm::dot(OCVec, OCVec) - this->radius * this->radius, delta = (b * b) - (4 * a * c);
if (delta < 0)
return false;
if (delta == 0) {
float dist = -0.5f * b / a;
glm::vec3 point = rayon.Orig() + (rayon.Vect() * dist), norm = (point - this->center) / glm::length(point - this->center);
Intersection intersection(dist, norm, this);
intersection.setNormal(this->material->computeColour(intersection, point, sc, rayon, rec));
I.push_back(intersection);
return true;
}
float q = (b > 0) ? -0.5f * (b + sqrtf(delta)) : -0.5f * (b - sqrtf(delta)),
x0 = q / a,
x1 = c / q;
if (x0 > x1) std::swap(x0, x1);
glm::vec3 point = rayon.Orig() + (rayon.Vect() * x0), norm = (point - this->center) / glm::length(point - this->center);
Intersection intersection(x0, norm, this);
intersection.setNormal(this->material->computeColour(intersection, point, sc, rayon, rec));
I.push_back(intersection);*/
float dist;
if (!glm::intersectRaySphere(rayon.Orig(), rayon.Vect(), this->center, this->radius, dist)) {
return false;
}
glm::vec3 point = rayon.Orig() + (rayon.Vect() * dist), norm = (point - this->center) / glm::length(point - this->center);
Intersection intersection(dist, norm, this);
intersection.setNormal(this->material->computeColour(intersection, point, sc, rayon, rec));
I.push_back(intersection);
return true;
}
/**
* Constructor without colour
* @param x coordinate of the center.
* @param y coordinate of the center.
* @param z coordinate of the center.
* @param r Radius of the sphere.
*/
Sphere::Sphere(const float x, const float y, const float z, const int r) : center(x, y, z), radius(r) {}
/**
* Colour is added
* @param r Red
* @param g Green
* @param b Blue
* @sa Sphere(float, float, float, r)
*/
Sphere::Sphere(const float x, const float y, const float z, const int radius, const float r, const float g,
const float b) : Objet(r, g, b),
center(x, y, z),
radius(radius) {}
/**
* It constructs the sphere with a material.
* @param x coordinate of the center.
* @param y coordinate of the center.
* @param z coordinate of the center.
* @param r radius of the sphere.
* @param material The material of the sphere.
*/
Sphere::Sphere(const float x, const float y, const float z, const int r, Material* const material) : Objet(material),
center(x, y, z),
radius(r) {}
Sphere::~Sphere() {
delete material;
}
Sphere::Sphere(glm::vec3&& color, glm::vec3&& center, int radius) : Objet(color), center(center), radius(radius) {}
Sphere::Sphere(const glm::vec3& color, const glm::vec3& center, int radius) : Objet(color), center(center), radius(radius) {}
Sphere::Sphere(Material* material, glm::vec3&& center, int radius) : Objet(material), center(center), radius(radius) {}
Sphere::Sphere(Material* material, const glm::vec3& center, int radius) : Objet(material), center(center), radius(radius) {}