-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSphere.cs
46 lines (42 loc) · 1.45 KB
/
Sphere.cs
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
using System;
using System.Numerics;
namespace RayTracer
{
class Sphere : IHittable
{
private readonly Vector3 center;
private readonly float radius;
private readonly Material material;
public Sphere(Vector3 center, float radius, Material material)
{
this.center = center;
this.radius = radius;
this.material = material;
}
public bool Hit(in Ray r, float tMin, float tMax, ref HitRecord hit)
{
Vector3 oc = r.Origin - center;
float a = Vector3.Dot(r.Direction, r.Direction);
float b = 2.0f * Vector3.Dot(oc, r.Direction);
float c = Vector3.Dot(oc, oc) - radius * radius;
float discriminant = b*b - 4*a*c;
if (discriminant < 0.0f)
return false;
else
{
float t = (-b - (float)Math.Sqrt(discriminant)) / (2.0f * a);
// if (t < tMin || t > tMax)
// t = (-b + (float)Math.Sqrt(discriminant)) / (2.0f * a);
if (t >= tMin && t <= tMax)
{
hit.T = t;
hit.P = r.PointAtParameter(t);
hit.Normal = (hit.P - center) / radius;
hit.Material = material;
return true;
}
return false;
}
}
}
}