-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsphere.cpp
More file actions
96 lines (74 loc) · 2.42 KB
/
sphere.cpp
File metadata and controls
96 lines (74 loc) · 2.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
93
#include "sphere.h"
#include <cmath>
Ray Sphere::interaction(Ray ray)
{
float3 u = ray.direction;
float3 o = ray.origin;
float3 c = center;
float3 oc = o-c;
float r = radius;
const float dotUoc = dot(u, oc);
const float delta = dotUoc * dotUoc - dot(oc, oc) + r * r;
/* std::cout << "u: " << u << std::endl;
std::cout << "o: " << o << std::endl;
std::cout << "c: " << c << std::endl;
std::cout << "oc: " << oc << std::endl;
std::cout << "r: " << r<< std::endl;
std::cout << "dotUoc: " << dotUoc << std::endl;
std::cout << "delta: " << delta << std::endl;
std::cout << std::endl; */
if(delta < 0.f) return Ray();
Ray outRay;
float d1 = -dotUoc + std::sqrt(delta);
float d2 = -dotUoc - std::sqrt(delta);
const float3 p1 = o + (u * d1);
const float3 p2 = o + (u * d2);
float3 op1 = o - p1;
float3 op2 = o - p2;
if(op1.length() < op2.length()) outRay.origin = p1;
else outRay.origin = p2;
if (dot(outRay.origin - ray.origin, ray.direction) < 0)
return Ray();
float3 normal = outRay.origin - center;
normal = normal.normalize();
//std::cout << "\nnormal: " << normal << std::endl;
//std::cout << "\norigin: " << outRay.origin << std::endl;
float3 p = outRay.origin + ray.direction;
float t = dot(ray.direction * -1.f , normal);
float3 pp = p + normal * 2.f * t;
//std::cout << "P: " << p << std::endl;
//std::cout << "PP: " << pp << std::endl;
//std::cout << "T: " << t << std::endl;
outRay.direction = pp - outRay.origin;
outRay.color = color;
outRay.isEnd = false;
return outRay;
}
bool Sphere::intersection(const float3& origin, const float3& normal, float3& output)
{
float3 u = normal;
float3 o = origin;
float3 c = center;
float3 oc = o-c;
float r = radius;
const float dotUoc = dot(u, oc);
const float delta = dotUoc * dotUoc - dot(oc, oc) + r * r;
/* std::cout << "u: " << u << std::endl;
std::cout << "o: " << o << std::endl;
std::cout << "c: " << c << std::endl;
std::cout << "oc: " << oc << std::endl;
std::cout << "r: " << r<< std::endl;
std::cout << "dotUoc: " << dotUoc << std::endl;
std::cout << "delta: " << delta << std::endl;
std::cout << std::endl; */
if(delta < 0.f) return false;
float d1 = -dotUoc + std::sqrt(delta);
float d2 = -dotUoc - std::sqrt(delta);
const float3 p1 = o + (u * d1);
const float3 p2 = o + (u * d2);
float3 op1 = o - p1;
float3 op2 = o - p2;
if(op1.length() < op2.length()) output = p1;
else output = p2;
return true;
}