-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
52 lines (39 loc) · 1.21 KB
/
object.cpp
File metadata and controls
52 lines (39 loc) · 1.21 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
#include "object.h"
#include <cmath>
#include <limits>
Object::Object() : location(), color() {}
Object::Object(const Vec3f& v, const Color& c) : location(v), color(c) {}
Vec3f Object::getlocation() const {
return location;
}
Color Object::getColor() const {
return color;
}
std::istream& operator>>(std::istream& in, Object& obj) {
in >> obj.location >> obj.color;
return in;
}
Sphere::Sphere() : Object(), radius(0) {}
Sphere::Sphere(const Vec3f& v, const Color& c, float r) : Object(v, c), radius(r) {}
void Sphere::checkRay(const Vec3f& origin, const Vec3f& direction, float& t1, float& t2) const {
Vec3f oc = origin - location;
float k1 = direction * direction;
float k2 = 2 * oc * direction;
float k3 = oc * oc - radius * radius;
float d = k2 * k2 - 4 * k1 * k3;
if (d < 0) {
t1 = t2 = std::numeric_limits<float>::infinity();
return;
}
t1 = (-k2 + sqrt(d)) / (2*k1);
t2 = (-k2 - sqrt(d)) / (2*k1);
}
Vec3f Sphere::getNormalVector(const Vec3f& v) const {
Vec3f norm = v - location;
norm.normalize();
return norm;
}
std::istream& operator>>(std::istream& in, Sphere& sph) {
in >> sph.location >> sph.color >> sph.radius;
return in;
}