-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.cpp
More file actions
33 lines (30 loc) · 935 Bytes
/
Copy pathSphere.cpp
File metadata and controls
33 lines (30 loc) · 935 Bytes
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
//
// Created by Jiang Kairong on 3/10/18.
//
#include "Sphere.h"
Sphere::Sphere(const Vector3d ¢erPosition, double radius, const ColorRGB32f &colorAmbient,
const ColorRGB32f &colorDiffuse, const ColorRGB32f &colorSpecular, double phongExponent) : Surface(
colorAmbient, colorDiffuse, colorSpecular, phongExponent), centerPosition(
centerPosition), radius(radius) {
}
bool Sphere::intersect(const Ray &ray, IntersectionInfo &info) {
Vector3d k = ray.getS() - centerPosition;
double b = ray.getD().dot(k);
double c = k.dot(k) - radius * radius;
double delta = b * b - c;
if (delta < 0.) {
return false;
} else {
double t = -b - std::sqrt(delta);
if (t < 0.) {
t = -b + std::sqrt(delta);
}
if (t < 0.) {
return false;
}
if (updateIntersectInfo(info, ray, t)) {
info.hitNormal = (info.hitPoint - centerPosition).normalize();
}
return true;
}
}