-
Notifications
You must be signed in to change notification settings - Fork 3
/
Sphere.c
65 lines (47 loc) · 1.49 KB
/
Sphere.c
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
#include "Sphere.h"
#include "Ray.h"
#include <math.h>
//#include "randf.h"
#include "pi.h"
Sphere makeSphere(const Vector position, const float radius) {
return (Sphere) {position, radius};
}
Intersection sIntersect(const Sphere sphere, const Ray ray) {
Intersection intersection;
intersection.hitType = missed;
float b = vDot(vSub(ray.origin, sphere.position), ray.direction);
float c = vLengthSquared(vSub(ray.origin, sphere.position)) - sphere.radius*sphere.radius;
float det = b*b - c;
if (det>=0) {
det = sqrtf(det);
float t1 = -b - det;
if (t1>0.0001) {
intersection.distance = t1;
// Identical to next block.
intersection.hitType = surface;
intersection.position = vAdd(ray.origin, vsMul(ray.direction, intersection.distance));
intersection.normal = vNormalized(vSub(intersection.position, sphere.position));
} else {
float t2=-b+det;
if (t2>0.0001) {
intersection.distance = t2;
// Identical to previous block.
intersection.hitType = surface;
intersection.position = vAdd(ray.origin, vsMul(ray.direction, intersection.distance));
intersection.normal = vNormalized(vSub(intersection.position, sphere.position));
}
}
}
return intersection;
}
//Vector sSampleSurface(const Sphere s) {
//
// return vAdd(s.position, vRotated(
// vRotated(makeVector(s.radius, 0, 0), makeVector(0, 1, 0), acosf(randf()*2-1)),
// makeVector(1, 0, 0),
// randf() * 2*PI
// ));
//}
float sSurfaceArea(const Sphere s) {
return s.radius * s.radius * PI * 4;
}